1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * test_fprobe.c - simple sanity test for fprobe
6 #include <linux/kernel.h>
7 #include <linux/fprobe.h>
8 #include <linux/random.h>
9 #include <kunit/test.h>
13 static struct kunit *current_test;
15 static u32 rand1, entry_val, exit_val;
17 /* Use indirect calls to avoid inlining the target functions */
18 static u32 (*target)(u32 value);
19 static u32 (*target2)(u32 value);
20 static unsigned long target_ip;
21 static unsigned long target2_ip;
23 static noinline u32 fprobe_selftest_target(u32 value)
25 return (value / div_factor);
28 static noinline u32 fprobe_selftest_target2(u32 value)
30 return (value / div_factor) + 1;
33 static notrace void fp_entry_handler(struct fprobe *fp, unsigned long ip,
34 struct pt_regs *regs, void *data)
36 KUNIT_EXPECT_FALSE(current_test, preemptible());
37 /* This can be called on the fprobe_selftest_target and the fprobe_selftest_target2 */
39 KUNIT_EXPECT_EQ(current_test, ip, target2_ip);
40 entry_val = (rand1 / div_factor);
43 static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,
44 struct pt_regs *regs, void *data)
46 unsigned long ret = regs_return_value(regs);
48 KUNIT_EXPECT_FALSE(current_test, preemptible());
49 if (ip != target_ip) {
50 KUNIT_EXPECT_EQ(current_test, ip, target2_ip);
51 KUNIT_EXPECT_EQ(current_test, ret, (rand1 / div_factor) + 1);
53 KUNIT_EXPECT_EQ(current_test, ret, (rand1 / div_factor));
54 KUNIT_EXPECT_EQ(current_test, entry_val, (rand1 / div_factor));
55 exit_val = entry_val + div_factor;
58 /* Test entry only (no rethook) */
59 static void test_fprobe_entry(struct kunit *test)
61 struct fprobe fp_entry = {
62 .entry_handler = fp_entry_handler,
67 /* Before register, unregister should be failed. */
68 KUNIT_EXPECT_NE(test, 0, unregister_fprobe(&fp_entry));
69 KUNIT_EXPECT_EQ(test, 0, register_fprobe(&fp_entry, "fprobe_selftest_target*", NULL));
74 KUNIT_EXPECT_NE(test, 0, entry_val);
75 KUNIT_EXPECT_EQ(test, 0, exit_val);
80 KUNIT_EXPECT_NE(test, 0, entry_val);
81 KUNIT_EXPECT_EQ(test, 0, exit_val);
83 KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp_entry));
86 static void test_fprobe(struct kunit *test)
89 .entry_handler = fp_entry_handler,
90 .exit_handler = fp_exit_handler,
94 KUNIT_EXPECT_EQ(test, 0, register_fprobe(&fp, "fprobe_selftest_target*", NULL));
99 KUNIT_EXPECT_NE(test, 0, entry_val);
100 KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val);
105 KUNIT_EXPECT_NE(test, 0, entry_val);
106 KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val);
108 KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp));
111 static void test_fprobe_syms(struct kunit *test)
113 static const char *syms[] = {"fprobe_selftest_target", "fprobe_selftest_target2"};
115 .entry_handler = fp_entry_handler,
116 .exit_handler = fp_exit_handler,
120 KUNIT_EXPECT_EQ(test, 0, register_fprobe_syms(&fp, syms, 2));
125 KUNIT_EXPECT_NE(test, 0, entry_val);
126 KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val);
131 KUNIT_EXPECT_NE(test, 0, entry_val);
132 KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val);
134 KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp));
137 static unsigned long get_ftrace_location(void *func)
139 unsigned long size, addr = (unsigned long)func;
141 if (!kallsyms_lookup_size_offset(addr, &size, NULL) || !size)
144 return ftrace_location_range(addr, addr + size - 1);
147 static int fprobe_test_init(struct kunit *test)
150 rand1 = get_random_u32();
151 } while (rand1 <= div_factor);
153 target = fprobe_selftest_target;
154 target2 = fprobe_selftest_target2;
155 target_ip = get_ftrace_location(target);
156 target2_ip = get_ftrace_location(target2);
161 static struct kunit_case fprobe_testcases[] = {
162 KUNIT_CASE(test_fprobe_entry),
163 KUNIT_CASE(test_fprobe),
164 KUNIT_CASE(test_fprobe_syms),
168 static struct kunit_suite fprobe_test_suite = {
169 .name = "fprobe_test",
170 .init = fprobe_test_init,
171 .test_cases = fprobe_testcases,
174 kunit_test_suites(&fprobe_test_suite);
176 MODULE_LICENSE("GPL");