1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * test_kprobes.c - simple sanity test for *probes
5 * Copyright IBM Corp. 2008
8 #define pr_fmt(fmt) "Kprobe smoke test: " fmt
10 #include <linux/kernel.h>
11 #include <linux/kprobes.h>
12 #include <linux/random.h>
16 static u32 rand1
, preh_val
, posth_val
;
17 static int errors
, handler_errors
, num_tests
;
18 static u32 (*target
)(u32 value
);
19 static u32 (*target2
)(u32 value
);
21 static noinline u32
kprobe_target(u32 value
)
23 return (value
/ div_factor
);
26 static int kp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
30 pr_err("pre-handler is preemptible\n");
32 preh_val
= (rand1
/ div_factor
);
36 static void kp_post_handler(struct kprobe
*p
, struct pt_regs
*regs
,
41 pr_err("post-handler is preemptible\n");
43 if (preh_val
!= (rand1
/ div_factor
)) {
45 pr_err("incorrect value in post_handler\n");
47 posth_val
= preh_val
+ div_factor
;
50 static struct kprobe kp
= {
51 .symbol_name
= "kprobe_target",
52 .pre_handler
= kp_pre_handler
,
53 .post_handler
= kp_post_handler
56 static int test_kprobe(void)
60 ret
= register_kprobe(&kp
);
62 pr_err("register_kprobe returned %d\n", ret
);
67 unregister_kprobe(&kp
);
70 pr_err("kprobe pre_handler not called\n");
75 pr_err("kprobe post_handler not called\n");
82 static noinline u32
kprobe_target2(u32 value
)
84 return (value
/ div_factor
) + 1;
87 static int kp_pre_handler2(struct kprobe
*p
, struct pt_regs
*regs
)
89 preh_val
= (rand1
/ div_factor
) + 1;
93 static void kp_post_handler2(struct kprobe
*p
, struct pt_regs
*regs
,
96 if (preh_val
!= (rand1
/ div_factor
) + 1) {
98 pr_err("incorrect value in post_handler2\n");
100 posth_val
= preh_val
+ div_factor
;
103 static struct kprobe kp2
= {
104 .symbol_name
= "kprobe_target2",
105 .pre_handler
= kp_pre_handler2
,
106 .post_handler
= kp_post_handler2
109 static int test_kprobes(void)
112 struct kprobe
*kps
[2] = {&kp
, &kp2
};
114 /* addr and flags should be cleard for reusing kprobe. */
117 ret
= register_kprobes(kps
, 2);
119 pr_err("register_kprobes returned %d\n", ret
);
128 pr_err("kprobe pre_handler not called\n");
132 if (posth_val
== 0) {
133 pr_err("kprobe post_handler not called\n");
139 ret
= target2(rand1
);
142 pr_err("kprobe pre_handler2 not called\n");
146 if (posth_val
== 0) {
147 pr_err("kprobe post_handler2 not called\n");
151 unregister_kprobes(kps
, 2);
156 #ifdef CONFIG_KRETPROBES
159 static int entry_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
163 pr_err("kretprobe entry handler is preemptible\n");
165 krph_val
= (rand1
/ div_factor
);
169 static int return_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
171 unsigned long ret
= regs_return_value(regs
);
175 pr_err("kretprobe return handler is preemptible\n");
177 if (ret
!= (rand1
/ div_factor
)) {
179 pr_err("incorrect value in kretprobe handler\n");
183 pr_err("call to kretprobe entry handler failed\n");
190 static struct kretprobe rp
= {
191 .handler
= return_handler
,
192 .entry_handler
= entry_handler
,
193 .kp
.symbol_name
= "kprobe_target"
196 static int test_kretprobe(void)
200 ret
= register_kretprobe(&rp
);
202 pr_err("register_kretprobe returned %d\n", ret
);
207 unregister_kretprobe(&rp
);
208 if (krph_val
!= rand1
) {
209 pr_err("kretprobe handler not called\n");
216 static int return_handler2(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
218 unsigned long ret
= regs_return_value(regs
);
220 if (ret
!= (rand1
/ div_factor
) + 1) {
222 pr_err("incorrect value in kretprobe handler2\n");
226 pr_err("call to kretprobe entry handler failed\n");
233 static struct kretprobe rp2
= {
234 .handler
= return_handler2
,
235 .entry_handler
= entry_handler
,
236 .kp
.symbol_name
= "kprobe_target2"
239 static int test_kretprobes(void)
242 struct kretprobe
*rps
[2] = {&rp
, &rp2
};
244 /* addr and flags should be cleard for reusing kprobe. */
247 ret
= register_kretprobes(rps
, 2);
249 pr_err("register_kretprobe returned %d\n", ret
);
255 if (krph_val
!= rand1
) {
256 pr_err("kretprobe handler not called\n");
261 ret
= target2(rand1
);
262 if (krph_val
!= rand1
) {
263 pr_err("kretprobe handler2 not called\n");
266 unregister_kretprobes(rps
, 2);
269 #endif /* CONFIG_KRETPROBES */
271 int init_test_probes(void)
275 target
= kprobe_target
;
276 target2
= kprobe_target2
;
279 rand1
= prandom_u32();
280 } while (rand1
<= div_factor
);
282 pr_info("started\n");
289 ret
= test_kprobes();
293 #ifdef CONFIG_KRETPROBES
295 ret
= test_kretprobe();
300 ret
= test_kretprobes();
303 #endif /* CONFIG_KRETPROBES */
306 pr_err("BUG: %d out of %d tests failed\n", errors
, num_tests
);
307 else if (handler_errors
)
308 pr_err("BUG: %d error(s) running handlers\n", handler_errors
);
310 pr_info("passed successfully\n");