kprobes: convert tests to kunit
Subsystems (2)
PATCH v2 1/1 kprobes: convert tests to kunit
This converts the kprobes testcases to use the kunit framework. It adds a dependency on CONFIG_KUNIT, and the output will change to TAP:
TAP version 14
1..1
# Subtest: kprobes_test
1..4
random: crng init done
ok 1 - test_kprobe
ok 2 - test_kprobes
ok 3 - test_kretprobe
ok 4 - test_kretprobes
ok 1 - kprobes_test
Note that the kprobes testcases are no longer run immediately after kprobes initialization, but as a late initcall when kunit is initialized. kprobes itself is initialized with an early initcall, so the order is still correct.
--- kernel/kprobes.c | 3 - kernel/test_kprobes.c | 221 +++++++++++------------------------------- lib/Kconfig.debug | 1 + 3 files changed, 59 insertions(+), 166 deletions(-)
kernel/kprobes.c +0 -3
@@ -2508,9 +2508,6 @@ static int __init init_kprobes(void) err =register_module_notifier (&kprobe_module_nb); kprobes_initialized = (err ==0 ); - -if (!err) -init_test_probes ();return err; }early_initcall (init_kprobes);
kernel/test_kprobes.c +58 -163
337 lines not on this page · raw message
lib/Kconfig.debug +1 -0
@@ -2083,6 +2083,7 @@ config KPROBES_SANITY_TEST bool "Kprobes sanity tests" depends on DEBUG_KERNEL depends on KPROBES + depends on KUNIT help This option provides for testing basic kprobes functionality on boot. Samples of kprobe and kretprobe are inserted and
Signature · 1 line
2.25.1
Quoted 15 lines · On Tue, 14 Sep 2021 09:08:16 +0200 Sven Schnelle <[email protected]> wrote:
This converts the kprobes testcases to use the kunit framework. It adds a dependency on
CONFIG_KUNIT, and the output will change to TAP:TAP version 14
1..1
# Subtest:kprobes_test
1..4
random: crng init done
ok 1 -test_kprobe
ok 2 -test_kprobes
ok 3 -test_kretprobe
ok 4 -test_kretprobes
ok 1 -kprobes_test
Quoted 5 lines
Note that the kprobes testcases are no longer run immediately after kprobes initialization, but as a late initcall when kunit is initialized. kprobes itself is initialized with an early initcall, so the order is still correct.
Great! Thank you!
Quoted 387 lines
Signed-off-by Sven Schnelle <[email protected]>--- kernel/kprobes.c | 3 - kernel/test_kprobes.c | 221 +++++++++++------------------------------- lib/Kconfig.debug | 1 + 3 files changed, 59 insertions(+), 166 deletions(-)kernel/kprobes.c +0
-3@@ -2508,9 +2508,6 @@ static int __init init_kprobes(void) err =register_module_notifier (&kprobe_module_nb); kprobes_initialized = (err ==0 ); - -if (!err) -init_test_probes ();return err; }early_initcall (init_kprobes);kernel/test_kprobes.c +58
-163@@ -5,18 +5,17 @@* Copyright IBM Corp. 2008 */ -#define pr_fmt (fmt) "Kprobe smoke test: " fmt -#include <linux/kernel.h> #include <linux/kprobes.h> #include <linux/random.h> +#include <kunit/test.h> #define div_factor 3static u32 rand1, preh_val, posth_val; -static int errors, handler_errors, num_tests;static u32 (*target)(u32 value);static u32 (*target2)(u32 value); +static struct kunit *current_test; static noinline u32 kprobe_target(u32 value) {@@ -25,10 +24,7 @@ static noinline u32 kprobe_target(u32 value)static int kp_pre_handler (struct kprobe *p,struct pt_regs *regs) { -if (preemptible ()) { - handler_errors++; -pr_err ("pre-handler is preemptible\n" ); - } +KUNIT_EXPECT_FALSE (current_test,preemptible ()); preh_val = (rand1 / div_factor);return 0 ; }@@ -36,14 +32,8 @@ static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs)static void kp_post_handler (struct kprobe *p,struct pt_regs *regs,unsigned long flags) { -if (preemptible ()) { - handler_errors++; -pr_err ("post-handler is preemptible\n" ); - } -if (preh_val != (rand1 / div_factor)) { - handler_errors++; -pr_err ("incorrect value in post_handler\n" ); - } +KUNIT_EXPECT_FALSE (current_test,preemptible ()); +KUNIT_EXPECT_EQ (current_test, preh_val, (rand1 / div_factor)); posth_val = preh_val + div_factor; }@@ -53,30 +43,14 @@ static struct kprobe kp = { .post_handler = kp_post_handler }; -static int test_kprobe (void ) +static void test_kprobe (struct kunit *test) { -int ret; - - ret =register_kprobe (&kp); -if (ret <0 ) { -pr_err ("register_kprobe returned %d\n" , ret); -return ret; - } - - ret =target (rand1); + current_test = test; +KUNIT_EXPECT_EQ (test,0 ,register_kprobe (&kp)); +target (rand1);unregister_kprobe (&kp); - -if (preh_val ==0 ) { -pr_err ("kprobe pre_handler not called\n" ); - handler_errors++; - } - -if (posth_val ==0 ) { -pr_err ("kprobe post_handler not called\n" ); - handler_errors++; - } - -return 0 ; +KUNIT_EXPECT_NE (test,0 , preh_val); +KUNIT_EXPECT_NE (test,0 , posth_val); } static noinline u32 kprobe_target2(u32 value)@@ -93,10 +67,7 @@ static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)static void kp_post_handler2 (struct kprobe *p,struct pt_regs *regs,unsigned long flags) { -if (preh_val != (rand1 / div_factor) +1 ) { - handler_errors++; -pr_err ("incorrect value in post_handler2\n" ); - } +KUNIT_EXPECT_EQ (current_test, preh_val, (rand1 / div_factor) +1 ); posth_val = preh_val + div_factor; }@@ -106,51 +77,31 @@ static struct kprobe kp2 = { .post_handler = kp_post_handler2 }; -static int test_kprobes (void ) +static void test_kprobes (struct kunit *test) { -int ret;struct kprobe *kps[2 ] = {&kp, &kp2}; + current_test = test; +/* addr and flags should be cleard for reusing kprobe. */ kp.addr =NULL ; kp.flags =0 ; - ret =register_kprobes (kps,2 ); -if (ret <0 ) { -pr_err ("register_kprobes returned %d\n" , ret); -return ret; - } +KUNIT_EXPECT_EQ (test,0 ,register_kprobes (kps,2 )); preh_val =0 ; posth_val =0 ; - ret =target (rand1); +target (rand1); -if (preh_val ==0 ) { -pr_err ("kprobe pre_handler not called\n" ); - handler_errors++; - } - -if (posth_val ==0 ) { -pr_err ("kprobe post_handler not called\n" ); - handler_errors++; - } +KUNIT_EXPECT_NE (test,0 , preh_val); +KUNIT_EXPECT_NE (test,0 , posth_val); preh_val =0 ; posth_val =0 ; - ret =target2 (rand1); - -if (preh_val ==0 ) { -pr_err ("kprobe pre_handler2 not called\n" ); - handler_errors++; - } - -if (posth_val ==0 ) { -pr_err ("kprobe post_handler2 not called\n" ); - handler_errors++; - } +target2 (rand1); +KUNIT_EXPECT_NE (test,0 , preh_val); +KUNIT_EXPECT_NE (test,0 , posth_val);unregister_kprobes (kps,2 ); -return 0 ; - }#ifdef CONFIG_KRETPROBES @@ -158,10 +109,7 @@ static u32 krph_val;static int entry_handler (struct kretprobe_instance *ri,struct pt_regs *regs) { -if (preemptible ()) { - handler_errors++; -pr_err ("kretprobe entry handler is preemptible\n" ); - } +KUNIT_EXPECT_FALSE (current_test,preemptible ()); krph_val = (rand1 / div_factor);return 0 ; }@@ -170,19 +118,9 @@ static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs) {unsigned long ret =regs_return_value (regs); -if (preemptible ()) { - handler_errors++; -pr_err ("kretprobe return handler is preemptible\n" ); - } -if (ret != (rand1 / div_factor)) { - handler_errors++; -pr_err ("incorrect value in kretprobe handler\n" ); - } -if (krph_val ==0 ) { - handler_errors++; -pr_err ("call to kretprobe entry handler failed\n" ); - } - +KUNIT_EXPECT_FALSE (current_test,preemptible ()); +KUNIT_EXPECT_EQ (current_test, ret, rand1 / div_factor); +KUNIT_EXPECT_NE (current_test, krph_val,0 ); krph_val = rand1;return 0 ; }@@ -193,39 +131,21 @@ static struct kretprobe rp = { .kp.symbol_name = "kprobe_target" }; -static int test_kretprobe (void ) +static void test_kretprobe (struct kunit *test) { -int ret; - - ret =register_kretprobe (&rp); -if (ret <0 ) { -pr_err ("register_kretprobe returned %d\n" , ret); -return ret; - } - - ret =target (rand1); + current_test = test; +KUNIT_EXPECT_EQ (test,0 ,register_kretprobe (&rp)); +target (rand1);unregister_kretprobe (&rp); -if (krph_val != rand1) { -pr_err ("kretprobe handler not called\n" ); - handler_errors++; - } - -return 0 ; +KUNIT_EXPECT_EQ (test, krph_val, rand1); }static int return_handler2 (struct kretprobe_instance *ri,struct pt_regs *regs) {unsigned long ret =regs_return_value (regs); -if (ret != (rand1 / div_factor) +1 ) { - handler_errors++; -pr_err ("incorrect value in kretprobe handler2\n" ); - } -if (krph_val ==0 ) { - handler_errors++; -pr_err ("call to kretprobe entry handler failed\n" ); - } - +KUNIT_EXPECT_EQ (current_test, ret, (rand1 / div_factor) +1 ); +KUNIT_EXPECT_NE (current_test, krph_val,0 ); krph_val = rand1;return 0 ; }@@ -236,78 +156,53 @@ static struct kretprobe rp2 = { .kp.symbol_name = "kprobe_target2" }; -static int test_kretprobes (void ) +static void test_kretprobes (struct kunit *test) { -int ret;struct kretprobe *rps[2 ] = {&rp, &rp2}; + current_test = test;/* addr and flags should be cleard for reusing kprobe. */ rp.kp.addr =NULL ; rp.kp.flags =0 ; - ret =register_kretprobes (rps,2 ); -if (ret <0 ) { -pr_err ("register_kretprobe returned %d\n" , ret); -return ret; - } +KUNIT_EXPECT_EQ (test,0 ,register_kretprobes (rps,2 )); krph_val =0 ; - ret =target (rand1); -if (krph_val != rand1) { -pr_err ("kretprobe handler not called\n" ); - handler_errors++; - } +target (rand1); +KUNIT_EXPECT_EQ (test, krph_val, rand1); krph_val =0 ; - ret =target2 (rand1); -if (krph_val != rand1) { -pr_err ("kretprobe handler2 not called\n" ); - handler_errors++; - } +target2 (rand1); +KUNIT_EXPECT_EQ (test, krph_val, rand1);unregister_kretprobes (rps,2 ); -return 0 ; }#endif /* CONFIG_KRETPROBES */ -int init_test_probes (void ) +static int kprobes_test_init (struct kunit *test) { -int ret; - target = kprobe_target; target2 = kprobe_target2;do { rand1 =prandom_u32 (); }while (rand1 <= div_factor); +return 0 ; +} -pr_info ("started\n" ); - num_tests++; - ret =test_kprobe (); -if (ret <0 ) - errors++; - - num_tests++; - ret =test_kprobes (); -if (ret <0 ) - errors++; - +static struct kunit_case kprobes_testcases[] = { +KUNIT_CASE (test_kprobe), +KUNIT_CASE (test_kprobes), #ifdef CONFIG_KRETPROBES - num_tests++; - ret =test_kretprobe (); -if (ret <0 ) - errors++; - - num_tests++; - ret =test_kretprobes (); -if (ret <0 ) - errors++; -#endif /* CONFIG_KRETPROBES */ +KUNIT_CASE (test_kretprobe), +KUNIT_CASE (test_kretprobes), +#endif + {} +}; -if (errors) -pr_err ("BUG: %d out of %d tests failed\n" , errors, num_tests); -else if (handler_errors) -pr_err ("BUG: %d error(s) running handlers\n" , handler_errors); -else -pr_info ("passed successfully\n" ); +static struct kunit_suite kprobes_test_suite = { + .name ="kprobes_test" , + .init = kprobes_test_init, + .test_cases = kprobes_testcases, +}; + +kunit_test_suites (&kprobes_test_suite); -return 0 ; -}lib/Kconfig.debug +1
-0@@ -2083,6 +2083,7 @@ config KPROBES_SANITY_TEST bool "Kprobes sanity tests" depends on DEBUG_KERNEL depends on KPROBES + depends on KUNIT help This option provides for testing basic kprobes functionality on boot. Samples of kprobe and kretprobe are inserted andSignature · 1 line
2.25.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Sven,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v5.15-rc1 next-20210914]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Sven-Schnelle/kprobes-convert-tests-to-kunit/20210914-151132 base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git d0ee23f9d78be5531c4b055ea424ed0b489dfe9b config: ia64-buildonly-randconfig-r006-20210914 (attached as .config) compiler: ia64-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/f7a2203bb543d14bde5ad006d585c7ab43fbe987
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Sven-Schnelle/kprobes-convert-tests-to-kunit/20210914-151132 git checkout f7a2203bb543d14bde5ad006d585c7ab43fbe987
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=ia64 SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
ia64-linux-ld: kernel/test_kprobes.o: in function `entry_handler':
>> test_kprobes.c:(.text+0x150): undefined reference to `kunit_unary_assert_format' >> ia64-linux-ld: test_kprobes.c:(.text+0x212): undefined reference to `kunit_do_assertion' ia64-linux-ld: kernel/test_kprobes.o: in function `return_handler2': >> test_kprobes.c:(.text+0x2f0): undefined reference to `kunit_binary_assert_format' ia64-linux-ld: test_kprobes.c:(.text+0x452): undefined reference to `kunit_do_assertion' ia64-linux-ld: test_kprobes.c:(.text+0x522): undefined reference to `kunit_do_assertion' ia64-linux-ld: kernel/test_kprobes.o: in function `return_handler': test_kprobes.c:(.text+0x5a0): undefined reference to `kunit_unary_assert_format' >> ia64-linux-ld: test_kprobes.c:(.text+0x5d0): undefined reference to `kunit_binary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0x692): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x7a2): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x862): undefined reference to `kunit_do_assertion'
ia64-linux-ld: kernel/test_kprobes.o: in function `kp_post_handler2':
test_kprobes.c:(.text+0x8d2): undefined reference to `kunit_binary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0xa32): undefined reference to `kunit_do_assertion'
ia64-linux-ld: kernel/test_kprobes.o: in function `kp_post_handler':
test_kprobes.c:(.text+0xaa0): undefined reference to `kunit_unary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0xb42): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0xb72): undefined reference to `kunit_binary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0xc72): undefined reference to `kunit_do_assertion'
ia64-linux-ld: kernel/test_kprobes.o: in function `kp_pre_handler':
test_kprobes.c:(.text+0xcd0): undefined reference to `kunit_unary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0xd92): undefined reference to `kunit_do_assertion'
ia64-linux-ld: kernel/test_kprobes.o: in function `test_kretprobes':
test_kprobes.c:(.text+0xee0): undefined reference to `kunit_binary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0xfe2): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x10d2): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x11c2): undefined reference to `kunit_do_assertion'
ia64-linux-ld: kernel/test_kprobes.o: in function `test_kretprobe':
test_kprobes.c:(.text+0x1280): undefined reference to `kunit_binary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0x1362): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x1462): undefined reference to `kunit_do_assertion'
ia64-linux-ld: kernel/test_kprobes.o: in function `test_kprobes':
test_kprobes.c:(.text+0x1540): undefined reference to `kunit_binary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0x1652): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x1732): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x17d2): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x18c2): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x1962): undefined reference to `kunit_do_assertion'
ia64-linux-ld: kernel/test_kprobes.o: in function `test_kprobe':
test_kprobes.c:(.text+0x1a20): undefined reference to `kunit_binary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0x1b02): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x1c02): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x1cb2): undefined reference to `kunit_do_assertion'
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 4 lines · kernel test robot
Hi Sven,
Thank you for the patch! Yet something to improve:
[..]
Quoted 7 lines
ia64-linux-ld:
kernel/test_kprobes.o: in function `entry_handler':Quoted 2 lines
Quoted 2 lines
test_kprobes.c:(.text+0x150): undefined reference to `kunit_unary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0x212): undefined reference to `kunit_do_assertion'ia64-linux-ld:
kernel/test_kprobes.o: in function `return_handler2':Quoted 1 line
Quoted 1 line
test_kprobes.c:(.text+0x2f0): undefined reference to `kunit_binary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0x452): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x522): undefined reference to `kunit_do_assertion'
I missed that kunit can be built as module. I'll fix it an send a v3.
Masami Hiramatsu
maintainerQuoted 8 lines · On Fri, 24 Sep 2021 20:19:20 +0200 Sven Schnelle <[email protected]> wrote:
Quoted 4 lines · kernel test robot
Hi Sven,
Thank you for the patch! Yet something to improve:
[..]
Quoted 1 line
ia64-linux-ld:
kernel/test_kprobes.o: in function `entry_handler':
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
test_kprobes.c:(.text+0x150): undefined reference to `kunit_unary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0x212): undefined reference to `kunit_do_assertion'ia64-linux-ld:
kernel/test_kprobes.o: in function `return_handler2':Quoted 1 line
Quoted 1 line
test_kprobes.c:(.text+0x2f0): undefined reference to `kunit_binary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0x452): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x522): undefined reference to `kunit_do_assertion'I missed that kunit can be built as module. I'll fix it an send a v3.
Does this mean we can not use KUnit for the feature which is not exposed to module?
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Hi Masami,
Quoted 13 lines · Masami Hiramatsu
Quoted 10 lines · On Fri, 24 Sep 2021 20:19:20 +0200 Sven Schnelle <[email protected]> wrote:
Quoted 4 lines · kernel test robot
Hi Sven,
Thank you for the patch! Yet something to improve:
[..]
Quoted 3 lines
ia64-linux-ld:
kernel/test_kprobes.o: in function `entry_handler':Quoted 2 lines
Quoted 2 lines
test_kprobes.c:(.text+0x150): undefined reference to `kunit_unary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0x212): undefined reference to `kunit_do_assertion'
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
ia64-linux-ld:
kernel/test_kprobes.o: in function `return_handler2':Quoted 1 line
Quoted 1 line
test_kprobes.c:(.text+0x2f0): undefined reference to `kunit_binary_assert_format'
ia64-linux-ld: test_kprobes.c:(.text+0x452): undefined reference to `kunit_do_assertion'
ia64-linux-ld: test_kprobes.c:(.text+0x522): undefined reference to `kunit_do_assertion'I missed that kunit can be built as module. I'll fix it an send a v3.
Does this mean we can not use KUnit for the feature which is not exposed to module?
I changed the test_kprobes so that it can be build as a module now. (i guess you already noticed that when looking at the v3 patch).