Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp247300ybh; Mon, 20 Jul 2020 15:46:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1mc0cASYqVV+DaYo6LbscnUfexbIFC7jBOOh14vrrOVfE78Y8Y6cMIPbtieDqzxOqViGO X-Received: by 2002:a50:8e53:: with SMTP id 19mr24110099edx.185.1595285174773; Mon, 20 Jul 2020 15:46:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595285174; cv=none; d=google.com; s=arc-20160816; b=EX5F6xktnux7UMqvQcSbBwkkK57l3ESM68wQwcYkOjbYtBRtfjW8/Iz/5IAi5UG/rw 9SMgrGpsN6DP6f44cqwMnGA3sKf/fC7CTv2lIW9GH9VFAeKYwxQMZtTwrzXF3pIu6B6m P0TFbDIEgbCxZNcetkBAF3NtbsJ9enHMbTiksPpmyqCr948w1hQW93lU8+9CNMIMtUaZ aW0ZQU/Op+PJaNm+YU6GmLKv/rJoS4b9qpj8GofIcMDs7qGbzRl00d2pCUPatbtj/kf+ wuzSCVQHIIFUDChLkYALH6yoIrJxC7Z/9Z0PGQXVlMS9fecsQC3yKz64Uf7DWl2CtcdP 1azQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=ag3CH9gIrCGq60wqLtIh8GQRzVO4EAvpufO2Zb/zlwc=; b=U+bJPYmeYWnxT7aSYBKTAHkkfqkO+VrPQkpz+JSm9YMfNZAxj9A2Ye9KZN5jd1Yajx plANlLQEwK+im01GAbHcBdryykCx1jUTGI9TpAf+79dsrUx+BnOeCCb1JYGMHXLIQQMy zjY7Lqx8h9xH/14Bbhfzn57gkCYVIZlWsiKXvYr+UxqaF3gUK2Tx1ZE0SQPvqKqESfMu PLaB9VX/SJjT3JcBh0z36sBkJPyewwbkNKkVAxB4cOjSkkTp9fXAn/xklvU8KpZMDkNV BprzDB9aBDo1zBMIM/Y5KxkKeEzPsVR6LGs0wpfglnaZmn1iaPdgD4tV2QaXCJZ/Ky2x rcfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@massaru-org.20150623.gappssmtp.com header.s=20150623 header.b=MijXTMv2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c20si11437149edj.314.2020.07.20.15.45.51; Mon, 20 Jul 2020 15:46:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@massaru-org.20150623.gappssmtp.com header.s=20150623 header.b=MijXTMv2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727828AbgGTWo0 (ORCPT + 99 others); Mon, 20 Jul 2020 18:44:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726817AbgGTWoZ (ORCPT ); Mon, 20 Jul 2020 18:44:25 -0400 Received: from mail-qt1-x842.google.com (mail-qt1-x842.google.com [IPv6:2607:f8b0:4864:20::842]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F06C1C0619D2 for ; Mon, 20 Jul 2020 15:44:24 -0700 (PDT) Received: by mail-qt1-x842.google.com with SMTP id 6so14753571qtt.0 for ; Mon, 20 Jul 2020 15:44:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=massaru-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ag3CH9gIrCGq60wqLtIh8GQRzVO4EAvpufO2Zb/zlwc=; b=MijXTMv2wmOpRT0tSxn67usaHrw7v5/wM9T09O2XbEPBquJps5+8tk8hPgi+9s8haX DwenNjaAEUmqapQyTASXTIMFLrqfJ5maiAbJQLOCfogISvfTxHrXjb/P3vx8ORIPJZON IKLq7gwEwA4XcBWmR2Z24Pi7TAlfL9mNK6ntT2H9RvN/4/UbbmwNRY/A9QLg8S1sVL4p kQBzA5Al3O8Ct7hjyr+Q6j6YqSAcL+/GJIPjnN6I9OuxTl2lrbQPHmJy048u9YeXUZDo /0Y5h6zcEukIuboddjdF2QFrEnX1ablLh0vlr2c8qn2PzSq2CTzpap7gEEC7oJdQxH6k JlYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ag3CH9gIrCGq60wqLtIh8GQRzVO4EAvpufO2Zb/zlwc=; b=smsMSjmBGI0PImZtxpMW3sQdYQTY/xlOjnIH47uk/ivALD1Q/PuuJR5oaC7YxTdxbv 5v1FVe1fQLZodOzInpcFUC2yw/8PsXGgdZ8o5kdt92C8kvaX0Njzy2ZFSPQtyBZbtHCS Bd6uCyOS2HZCzzEaN1quEmN48ftjUp1pvVzbajEZuEY+q/7m5Qbi1N1WSjpJy4j9+EoE VooSjp0IFx60kEgEWBPsIzSRJBBHEw2g2i5AZukSGgRvkdq2F0PcLaU4NrsyY8UhvZwJ f5UsHb7IiUHaxc8urspO2HQXAQEhYI1IxoABg6MGREPYfv8T3QU3AfM8I6zt0S5mOboC OLoQ== X-Gm-Message-State: AOAM530qXDq7cpat/BjqdN20u2O/Sa5kqB6k0jrVFOIGFylIWkwQZtDX BTZza4hvmvJZxKKQORDR+RNh2g== X-Received: by 2002:ac8:3985:: with SMTP id v5mr25758367qte.337.1595285064006; Mon, 20 Jul 2020 15:44:24 -0700 (PDT) Received: from bbking.lan ([2804:14c:4a5:36c::cd2]) by smtp.gmail.com with ESMTPSA id g136sm795513qke.82.2020.07.20.15.44.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jul 2020 15:44:22 -0700 (PDT) From: Vitor Massaru Iha To: kunit-dev@googlegroups.com Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, brendanhiggins@google.com, keescook@chromium.org, davidgow@google.com, skhan@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org Subject: [PATCH v3] lib: overflow_kunit: add KUnit test conversion of check_*_overflow Date: Mon, 20 Jul 2020 19:44:18 -0300 Message-Id: <20200720224418.200495-1-vitor@massaru.org> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds the conversion of the runtime tests of check_*_overflow functions, from `lib/test_overflow.c`to KUnit tests. The log similar to the one seen in dmesg running test_overflow.c can be seen in `test.log`. Signed-off-by: Vitor Massaru Iha Tested-by: David Gow --- v2: * moved lib/test_overflow.c to lib/overflow-test.c; * back to original license; * fixed style code; * keeps __initconst and added _refdata on overflow_test_cases variable; * keeps macros intact making asserts with the variable err; * removed duplicate test_s8_overflow(); * fixed typos on commit message; v3: * changed filename to overflow_kunit.c; * replace _refdata by _inidata; * added expects/asserts on individual tests; --- lib/Kconfig.debug | 20 +++- lib/Makefile | 2 +- lib/{test_overflow.c => overflow_kunit.c} | 122 +++++++++------------- 3 files changed, 70 insertions(+), 74 deletions(-) rename lib/{test_overflow.c => overflow_kunit.c} (91%) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 9ad9210d70a1..230aaf418dc0 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1999,9 +1999,6 @@ config TEST_UUID config TEST_XARRAY tristate "Test the XArray code at runtime" -config TEST_OVERFLOW - tristate "Test check_*_overflow() functions at runtime" - config TEST_RHASHTABLE tristate "Perform selftest on resizable hash table" help @@ -2154,6 +2151,23 @@ config SYSCTL_KUNIT_TEST If unsure, say N. +config OVERFLOW_KUNIT + tristate "KUnit test for overflow" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This builds the overflow KUnit tests. + + KUnit tests run during boot and output the results to the debug log + in TAP format (http://testanything.org/). Only useful for kernel devs + running KUnit test harness and are not for inclusion into a production + build. + + For more information on KUnit and unit tests in general please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. + config LIST_KUNIT_TEST tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/lib/Makefile b/lib/Makefile index b1c42c10073b..c3cf72ec6c52 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -75,7 +75,6 @@ obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o obj-$(CONFIG_TEST_MIN_HEAP) += test_min_heap.o obj-$(CONFIG_TEST_LKM) += test_module.o obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o -obj-$(CONFIG_TEST_OVERFLOW) += test_overflow.o obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o obj-$(CONFIG_TEST_SORT) += test_sort.o obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o @@ -318,3 +317,4 @@ obj-$(CONFIG_OBJAGG) += objagg.o # KUnit tests obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o +obj-$(CONFIG_OVERFLOW_KUNIT) += overflow_kunit.o diff --git a/lib/test_overflow.c b/lib/overflow_kunit.c similarity index 91% rename from lib/test_overflow.c rename to lib/overflow_kunit.c index 7a4b6f6c5473..475d0daeb801 100644 --- a/lib/test_overflow.c +++ b/lib/overflow_kunit.c @@ -4,14 +4,11 @@ */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#include #include #include -#include #include -#include #include -#include -#include #include #define DEFINE_TEST_ARRAY(t) \ @@ -248,7 +245,7 @@ static int __init do_test_ ## t(const struct test_ ## t *p) \ return err; \ } \ \ -static int __init test_ ## t ## _overflow(void) { \ +static int __init test_ ## t ## _overflow(struct kunit *test) { \ int err = 0; \ unsigned i; \ \ @@ -256,6 +253,7 @@ static int __init test_ ## t ## _overflow(void) { \ ARRAY_SIZE(t ## _tests)); \ for (i = 0; i < ARRAY_SIZE(t ## _tests); ++i) \ err |= do_test_ ## t(&t ## _tests[i]); \ + KUNIT_EXPECT_FALSE(test, err); \ return err; \ } @@ -270,25 +268,25 @@ DEFINE_TEST_FUNC(u64, "%llu"); DEFINE_TEST_FUNC(s64, "%lld"); #endif -static int __init test_overflow_calculation(void) +static void __init overflow_calculation_test(struct kunit *test) { int err = 0; - err |= test_u8_overflow(); - err |= test_s8_overflow(); - err |= test_u16_overflow(); - err |= test_s16_overflow(); - err |= test_u32_overflow(); - err |= test_s32_overflow(); + err |= test_u8_overflow(test); + err |= test_s8_overflow(test); + err |= test_u16_overflow(test); + err |= test_s16_overflow(test); + err |= test_u32_overflow(test); + err |= test_s32_overflow(test); #if BITS_PER_LONG == 64 - err |= test_u64_overflow(); - err |= test_s64_overflow(); + err |= test_u64_overflow(test); + err |= test_s64_overflow(test); #endif - return err; + KUNIT_EXPECT_FALSE(test, err); } -static int __init test_overflow_shift(void) +static void __init overflow_shift_test(struct kunit *test) { int err = 0; @@ -313,9 +311,9 @@ static int __init test_overflow_shift(void) pr_warn("got %llu\n", (u64)__d); \ __failed = 1; \ } \ - if (!__failed) \ - pr_info("ok: (%s)(%s << %s) == %s\n", #t, #a, #s, \ - of ? "overflow" : #expect); \ + KUNIT_EXPECT_FALSE_MSG(test, __failed, \ + "ok: (%s)(%s << %s) == %s\n", #t, #a, #s,\ + of ? "overflow" : #expect); \ __failed; \ }) @@ -479,7 +477,7 @@ static int __init test_overflow_shift(void) err |= TEST_ONE_SHIFT(0, 31, s32, 0, false); err |= TEST_ONE_SHIFT(0, 63, s64, 0, false); - return err; + KUNIT_EXPECT_FALSE(test, err); } /* @@ -499,7 +497,7 @@ static int __init test_overflow_shift(void) #define TEST_SIZE (5 * 4096) #define DEFINE_TEST_ALLOC(func, free_func, want_arg, want_gfp, want_node)\ -static int __init test_ ## func (void *arg) \ +static int __init test_ ## func (struct kunit *test, void *arg) \ { \ volatile size_t a = TEST_SIZE; \ volatile size_t b = (SIZE_MAX / TEST_SIZE) + 1; \ @@ -507,19 +505,15 @@ static int __init test_ ## func (void *arg) \ \ /* Tiny allocation test. */ \ ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, 1);\ - if (!ptr) { \ - pr_warn(#func " failed regular allocation?!\n"); \ - return 1; \ - } \ + KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, \ + #func " failed regular allocation?!\n"); \ free ## want_arg (free_func, arg, ptr); \ \ /* Wrapped allocation test. */ \ ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \ a * b); \ - if (!ptr) { \ - pr_warn(#func " unexpectedly failed bad wrapping?!\n"); \ - return 1; \ - } \ + KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, \ + #func " unexpectedly failed bad wrapping?!\n"); \ free ## want_arg (free_func, arg, ptr); \ \ /* Saturated allocation test. */ \ @@ -555,7 +549,7 @@ DEFINE_TEST_ALLOC(kvzalloc_node, kvfree, 0, 1, 1); DEFINE_TEST_ALLOC(devm_kmalloc, devm_kfree, 1, 1, 0); DEFINE_TEST_ALLOC(devm_kzalloc, devm_kfree, 1, 1, 0); -static int __init test_overflow_allocation(void) +static void __init overflow_allocation_test(struct kunit *test) { const char device_name[] = "overflow-test"; struct device *dev; @@ -563,52 +557,40 @@ static int __init test_overflow_allocation(void) /* Create dummy device for devm_kmalloc()-family tests. */ dev = root_device_register(device_name); - if (IS_ERR(dev)) { - pr_warn("Cannot register test device\n"); - return 1; - } - - err |= test_kmalloc(NULL); - err |= test_kmalloc_node(NULL); - err |= test_kzalloc(NULL); - err |= test_kzalloc_node(NULL); - err |= test_kvmalloc(NULL); - err |= test_kvmalloc_node(NULL); - err |= test_kvzalloc(NULL); - err |= test_kvzalloc_node(NULL); - err |= test_vmalloc(NULL); - err |= test_vmalloc_node(NULL); - err |= test_vzalloc(NULL); - err |= test_vzalloc_node(NULL); - err |= test_devm_kmalloc(dev); - err |= test_devm_kzalloc(dev); + KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev), "Cannot register test device\n"); + + err |= test_kmalloc(test, NULL); + err |= test_kmalloc_node(test, NULL); + err |= test_kzalloc(test, NULL); + err |= test_kzalloc_node(test, NULL); + err |= test_kvmalloc(test, NULL); + err |= test_kvmalloc_node(test, NULL); + err |= test_kvzalloc(test, NULL); + err |= test_kvzalloc_node(test, NULL); + err |= test_vmalloc(test, NULL); + err |= test_vmalloc_node(test, NULL); + err |= test_vzalloc(test, NULL); + err |= test_vzalloc_node(test, NULL); + err |= test_devm_kmalloc(test, dev); + err |= test_devm_kzalloc(test, dev); device_unregister(dev); - return err; + KUNIT_EXPECT_FALSE(test, err); } -static int __init test_module_init(void) -{ - int err = 0; - - err |= test_overflow_calculation(); - err |= test_overflow_shift(); - err |= test_overflow_allocation(); - - if (err) { - pr_warn("FAIL!\n"); - err = -EINVAL; - } else { - pr_info("all tests passed\n"); - } +static struct kunit_case __initdata overflow_test_cases[] = { + KUNIT_CASE(overflow_calculation_test), + KUNIT_CASE(overflow_shift_test), + KUNIT_CASE(overflow_allocation_test), + {} +}; - return err; -} +static struct kunit_suite __initdata overflow_test_suite = { + .name = "overflow", + .test_cases = overflow_test_cases, +}; -static void __exit test_module_exit(void) -{ } +kunit_test_suites(&overflow_test_suite); -module_init(test_module_init); -module_exit(test_module_exit); MODULE_LICENSE("Dual MIT/GPL"); base-commit: c63d2dd7e134ebddce4745c51f9572b3f0d92b26 -- 2.26.2