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 u32 (*target_nest)(u32 value, u32 (*nest)(u32));
21 static unsigned long target_ip;
22 static unsigned long target2_ip;
23 static unsigned long target_nest_ip;
24 static int entry_return_value;
26 static noinline u32 fprobe_selftest_target(u32 value)
28 return (value / div_factor);
31 static noinline u32 fprobe_selftest_target2(u32 value)
33 return (value / div_factor) + 1;
36 static noinline u32 fprobe_selftest_nest_target(u32 value, u32 (*nest)(u32))
38 return nest(value + 2);
41 static notrace int fp_entry_handler(struct fprobe *fp, unsigned long ip,
42 struct pt_regs *regs, void *data)
44 KUNIT_EXPECT_FALSE(current_test, preemptible());
45 /* This can be called on the fprobe_selftest_target and the fprobe_selftest_target2 */
47 KUNIT_EXPECT_EQ(current_test, ip, target2_ip);
48 entry_val = (rand1 / div_factor);
49 if (fp->entry_data_size) {
50 KUNIT_EXPECT_NOT_NULL(current_test, data);
52 *(u32 *)data = entry_val;
54 KUNIT_EXPECT_NULL(current_test, data);
56 return entry_return_value;
59 static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,
60 struct pt_regs *regs, void *data)
62 unsigned long ret = regs_return_value(regs);
64 KUNIT_EXPECT_FALSE(current_test, preemptible());
65 if (ip != target_ip) {
66 KUNIT_EXPECT_EQ(current_test, ip, target2_ip);
67 KUNIT_EXPECT_EQ(current_test, ret, (rand1 / div_factor) + 1);
69 KUNIT_EXPECT_EQ(current_test, ret, (rand1 / div_factor));
70 KUNIT_EXPECT_EQ(current_test, entry_val, (rand1 / div_factor));
71 exit_val = entry_val + div_factor;
72 if (fp->entry_data_size) {
73 KUNIT_EXPECT_NOT_NULL(current_test, data);
75 KUNIT_EXPECT_EQ(current_test, *(u32 *)data, entry_val);
77 KUNIT_EXPECT_NULL(current_test, data);
80 static notrace int nest_entry_handler(struct fprobe *fp, unsigned long ip,
81 struct pt_regs *regs, void *data)
83 KUNIT_EXPECT_FALSE(current_test, preemptible());
87 static notrace void nest_exit_handler(struct fprobe *fp, unsigned long ip,
88 struct pt_regs *regs, void *data)
90 KUNIT_EXPECT_FALSE(current_test, preemptible());
91 KUNIT_EXPECT_EQ(current_test, ip, target_nest_ip);
94 /* Test entry only (no rethook) */
95 static void test_fprobe_entry(struct kunit *test)
97 struct fprobe fp_entry = {
98 .entry_handler = fp_entry_handler,
103 /* Before register, unregister should be failed. */
104 KUNIT_EXPECT_NE(test, 0, unregister_fprobe(&fp_entry));
105 KUNIT_EXPECT_EQ(test, 0, register_fprobe(&fp_entry, "fprobe_selftest_target*", NULL));
110 KUNIT_EXPECT_NE(test, 0, entry_val);
111 KUNIT_EXPECT_EQ(test, 0, exit_val);
116 KUNIT_EXPECT_NE(test, 0, entry_val);
117 KUNIT_EXPECT_EQ(test, 0, exit_val);
119 KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp_entry));
122 static void test_fprobe(struct kunit *test)
125 .entry_handler = fp_entry_handler,
126 .exit_handler = fp_exit_handler,
130 KUNIT_EXPECT_EQ(test, 0, register_fprobe(&fp, "fprobe_selftest_target*", NULL));
135 KUNIT_EXPECT_NE(test, 0, entry_val);
136 KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val);
141 KUNIT_EXPECT_NE(test, 0, entry_val);
142 KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val);
144 KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp));
147 static void test_fprobe_syms(struct kunit *test)
149 static const char *syms[] = {"fprobe_selftest_target", "fprobe_selftest_target2"};
151 .entry_handler = fp_entry_handler,
152 .exit_handler = fp_exit_handler,
156 KUNIT_EXPECT_EQ(test, 0, register_fprobe_syms(&fp, syms, 2));
161 KUNIT_EXPECT_NE(test, 0, entry_val);
162 KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val);
167 KUNIT_EXPECT_NE(test, 0, entry_val);
168 KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val);
170 KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp));
173 /* Test private entry_data */
174 static void test_fprobe_data(struct kunit *test)
177 .entry_handler = fp_entry_handler,
178 .exit_handler = fp_exit_handler,
179 .entry_data_size = sizeof(u32),
183 KUNIT_EXPECT_EQ(test, 0, register_fprobe(&fp, "fprobe_selftest_target", NULL));
187 KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp));
190 /* Test nr_maxactive */
191 static void test_fprobe_nest(struct kunit *test)
193 static const char *syms[] = {"fprobe_selftest_target", "fprobe_selftest_nest_target"};
195 .entry_handler = nest_entry_handler,
196 .exit_handler = nest_exit_handler,
201 KUNIT_EXPECT_EQ(test, 0, register_fprobe_syms(&fp, syms, 2));
203 target_nest(rand1, target);
204 KUNIT_EXPECT_EQ(test, 1, fp.nmissed);
206 KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp));
209 static void test_fprobe_skip(struct kunit *test)
212 .entry_handler = fp_entry_handler,
213 .exit_handler = fp_exit_handler,
217 KUNIT_EXPECT_EQ(test, 0, register_fprobe(&fp, "fprobe_selftest_target", NULL));
219 entry_return_value = 1;
223 KUNIT_EXPECT_NE(test, 0, entry_val);
224 KUNIT_EXPECT_EQ(test, 0, exit_val);
225 KUNIT_EXPECT_EQ(test, 0, fp.nmissed);
226 entry_return_value = 0;
228 KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp));
231 static unsigned long get_ftrace_location(void *func)
233 unsigned long size, addr = (unsigned long)func;
235 if (!kallsyms_lookup_size_offset(addr, &size, NULL) || !size)
238 return ftrace_location_range(addr, addr + size - 1);
241 static int fprobe_test_init(struct kunit *test)
243 rand1 = get_random_u32_above(div_factor);
244 target = fprobe_selftest_target;
245 target2 = fprobe_selftest_target2;
246 target_nest = fprobe_selftest_nest_target;
247 target_ip = get_ftrace_location(target);
248 target2_ip = get_ftrace_location(target2);
249 target_nest_ip = get_ftrace_location(target_nest);
254 static struct kunit_case fprobe_testcases[] = {
255 KUNIT_CASE(test_fprobe_entry),
256 KUNIT_CASE(test_fprobe),
257 KUNIT_CASE(test_fprobe_syms),
258 KUNIT_CASE(test_fprobe_data),
259 KUNIT_CASE(test_fprobe_nest),
260 KUNIT_CASE(test_fprobe_skip),
264 static struct kunit_suite fprobe_test_suite = {
265 .name = "fprobe_test",
266 .init = fprobe_test_init,
267 .test_cases = fprobe_testcases,
270 kunit_test_suites(&fprobe_test_suite);