History

test_kprobes: clear kprobes between test runs

Running the kprobes sanity tests twice makes all tests fail and eventually crashes the kernel. [root@martin-riscv-1 ~]# echo 1 > /sys/kernel/debug/kunit/kprobes_test/run ... # Totals: pass:5 fail:0 skip:0 total:5 ok 1 kprobes_test [root@martin-riscv-1 ~]# echo 1 > /sys/kernel/debug/kunit/kprobes_test/run ... # test_kprobe: EXPECTATION FAILED at lib/tests/test_kprobes.c:64 Expected 0 == register_kprobe(&kp), but register_kprobe(&kp) == -22 (0xffffffffffffffea) ... Unable to handle kernel paging request ... The testsuite defines several kprobes and kretprobes as static variables that are preserved across test runs. After register_kprobe and unregister_kprobe, a kprobe contains some leftover data that must be cleared before the kprobe can be registered again. The tests are setting symbol_name to define the probe location. Address and flags must be cleared. The existing code clears some of the probes between subsequent tests, but not between two test runs. The leftover data from a previous test run makes the registrations fail in the next run. Move the cleanups for all kprobes into kprobes_test_init, this function is called before each single test (including the first test of a test run). Link: https://lore.kernel.org/all/20260507134615.1010905-1-martin@kaiser.cx/

Martin Kaiser committed 6 months ago

Commit ef5581b1 parent 307abfa1 file · +18 −11Browse files

Unified · Split

lib/tests/test_kprobes.c+18−11
@@ -12,6 +12,12 @@ · 11 unmodified lines
1212
1313 #define div_factor 3
1414
15+#define KP_CLEAR(_kp) \
16+do { \
17+ (_kp).addr = NULL; \
18+ (_kp).flags = 0; \
19+} while (0)
20+
1521 static u32 rand1, preh_val, posth_val;
1622 static u32 (*target)(u32 value);
1723 static u32 (*recursed_target)(u32 value);
@@ -125,10 +131,6 @@ static void test_kprobes(struct kunit *test) · 107 unmodified lines
125131
126132 current_test = test;
127133
128− /* addr and flags should be cleard for reusing kprobe. */
129− kp.addr = NULL;
130− kp.flags = 0;
131−
132134 KUNIT_EXPECT_EQ(test, 0, register_kprobes(kps, 2));
133135 preh_val = 0;
134136 posth_val = 0;
@@ -226,9 +228,6 @@ static void test_kretprobes(struct kunit *test) · 91 unmodified lines
226228 struct kretprobe *rps[2] = {&rp, &rp2};
227229
228230 current_test = test;
229− /* addr and flags should be cleard for reusing kprobe. */
230− rp.kp.addr = NULL;
231− rp.kp.flags = 0;
232231 KUNIT_EXPECT_EQ(test, 0, register_kretprobes(rps, 2));
233232
234233 krph_val = 0;
@@ -290,8 +289,6 @@ static void test_stacktrace_on_kretprobe(struct kunit *test) · 55 unmodified lines
290289 unsigned long myretaddr = (unsigned long)__builtin_return_address(0);
291290
292291 current_test = test;
293− rp3.kp.addr = NULL;
294− rp3.kp.flags = 0;
295292
296293 /*
297294 * Run the stacktrace_driver() to record correct return address in
@@ -352,8 +349,6 @@ static void test_stacktrace_on_nested_kretprobe(struct kunit *test) · 54 unmodified lines
352349 struct kretprobe *rps[2] = {&rp3, &rp4};
353350
354351 current_test = test;
355− rp3.kp.addr = NULL;
356− rp3.kp.flags = 0;
357352
358353 //KUNIT_ASSERT_NE(test, myretaddr, stacktrace_driver());
359354
@@ -367,6 +362,18 @@ static void test_stacktrace_on_nested_kretprobe(struct kunit *test) · 7 unmodified lines
367362
368363 static int kprobes_test_init(struct kunit *test)
369364 {
365+ KP_CLEAR(kp);
366+ KP_CLEAR(kp2);
367+ KP_CLEAR(kp_missed);
368+#ifdef CONFIG_KRETPROBES
369+ KP_CLEAR(rp.kp);
370+ KP_CLEAR(rp2.kp);
371+#ifdef CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE
372+ KP_CLEAR(rp3.kp);
373+ KP_CLEAR(rp4.kp);
374+#endif
375+#endif
376+
370377 target = kprobe_target;
371378 target2 = kprobe_target2;
372379 recursed_target = kprobe_recursed_target;