nvme-core: declare local symbols static
[linux/fpc-iii.git] / kernel / test_kprobes.c
blob7bca480151b02417d114747494e86e4ee2b1eac8
1 /*
2 * test_kprobes.c - simple sanity test for *probes
4 * Copyright IBM Corp. 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it would be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
17 #define pr_fmt(fmt) "Kprobe smoke test: " fmt
19 #include <linux/kernel.h>
20 #include <linux/kprobes.h>
21 #include <linux/random.h>
23 #define div_factor 3
25 static u32 rand1, preh_val, posth_val;
26 static int errors, handler_errors, num_tests;
27 static u32 (*target)(u32 value);
28 static u32 (*target2)(u32 value);
30 static noinline u32 kprobe_target(u32 value)
32 return (value / div_factor);
35 static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs)
37 if (preemptible()) {
38 handler_errors++;
39 pr_err("pre-handler is preemptible\n");
41 preh_val = (rand1 / div_factor);
42 return 0;
45 static void kp_post_handler(struct kprobe *p, struct pt_regs *regs,
46 unsigned long flags)
48 if (preemptible()) {
49 handler_errors++;
50 pr_err("post-handler is preemptible\n");
52 if (preh_val != (rand1 / div_factor)) {
53 handler_errors++;
54 pr_err("incorrect value in post_handler\n");
56 posth_val = preh_val + div_factor;
59 static struct kprobe kp = {
60 .symbol_name = "kprobe_target",
61 .pre_handler = kp_pre_handler,
62 .post_handler = kp_post_handler
65 static int test_kprobe(void)
67 int ret;
69 ret = register_kprobe(&kp);
70 if (ret < 0) {
71 pr_err("register_kprobe returned %d\n", ret);
72 return ret;
75 ret = target(rand1);
76 unregister_kprobe(&kp);
78 if (preh_val == 0) {
79 pr_err("kprobe pre_handler not called\n");
80 handler_errors++;
83 if (posth_val == 0) {
84 pr_err("kprobe post_handler not called\n");
85 handler_errors++;
88 return 0;
91 static noinline u32 kprobe_target2(u32 value)
93 return (value / div_factor) + 1;
96 static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)
98 preh_val = (rand1 / div_factor) + 1;
99 return 0;
102 static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs,
103 unsigned long flags)
105 if (preh_val != (rand1 / div_factor) + 1) {
106 handler_errors++;
107 pr_err("incorrect value in post_handler2\n");
109 posth_val = preh_val + div_factor;
112 static struct kprobe kp2 = {
113 .symbol_name = "kprobe_target2",
114 .pre_handler = kp_pre_handler2,
115 .post_handler = kp_post_handler2
118 static int test_kprobes(void)
120 int ret;
121 struct kprobe *kps[2] = {&kp, &kp2};
123 /* addr and flags should be cleard for reusing kprobe. */
124 kp.addr = NULL;
125 kp.flags = 0;
126 ret = register_kprobes(kps, 2);
127 if (ret < 0) {
128 pr_err("register_kprobes returned %d\n", ret);
129 return ret;
132 preh_val = 0;
133 posth_val = 0;
134 ret = target(rand1);
136 if (preh_val == 0) {
137 pr_err("kprobe pre_handler not called\n");
138 handler_errors++;
141 if (posth_val == 0) {
142 pr_err("kprobe post_handler not called\n");
143 handler_errors++;
146 preh_val = 0;
147 posth_val = 0;
148 ret = target2(rand1);
150 if (preh_val == 0) {
151 pr_err("kprobe pre_handler2 not called\n");
152 handler_errors++;
155 if (posth_val == 0) {
156 pr_err("kprobe post_handler2 not called\n");
157 handler_errors++;
160 unregister_kprobes(kps, 2);
161 return 0;
165 #ifdef CONFIG_KRETPROBES
166 static u32 krph_val;
168 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
170 if (preemptible()) {
171 handler_errors++;
172 pr_err("kretprobe entry handler is preemptible\n");
174 krph_val = (rand1 / div_factor);
175 return 0;
178 static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
180 unsigned long ret = regs_return_value(regs);
182 if (preemptible()) {
183 handler_errors++;
184 pr_err("kretprobe return handler is preemptible\n");
186 if (ret != (rand1 / div_factor)) {
187 handler_errors++;
188 pr_err("incorrect value in kretprobe handler\n");
190 if (krph_val == 0) {
191 handler_errors++;
192 pr_err("call to kretprobe entry handler failed\n");
195 krph_val = rand1;
196 return 0;
199 static struct kretprobe rp = {
200 .handler = return_handler,
201 .entry_handler = entry_handler,
202 .kp.symbol_name = "kprobe_target"
205 static int test_kretprobe(void)
207 int ret;
209 ret = register_kretprobe(&rp);
210 if (ret < 0) {
211 pr_err("register_kretprobe returned %d\n", ret);
212 return ret;
215 ret = target(rand1);
216 unregister_kretprobe(&rp);
217 if (krph_val != rand1) {
218 pr_err("kretprobe handler not called\n");
219 handler_errors++;
222 return 0;
225 static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
227 unsigned long ret = regs_return_value(regs);
229 if (ret != (rand1 / div_factor) + 1) {
230 handler_errors++;
231 pr_err("incorrect value in kretprobe handler2\n");
233 if (krph_val == 0) {
234 handler_errors++;
235 pr_err("call to kretprobe entry handler failed\n");
238 krph_val = rand1;
239 return 0;
242 static struct kretprobe rp2 = {
243 .handler = return_handler2,
244 .entry_handler = entry_handler,
245 .kp.symbol_name = "kprobe_target2"
248 static int test_kretprobes(void)
250 int ret;
251 struct kretprobe *rps[2] = {&rp, &rp2};
253 /* addr and flags should be cleard for reusing kprobe. */
254 rp.kp.addr = NULL;
255 rp.kp.flags = 0;
256 ret = register_kretprobes(rps, 2);
257 if (ret < 0) {
258 pr_err("register_kretprobe returned %d\n", ret);
259 return ret;
262 krph_val = 0;
263 ret = target(rand1);
264 if (krph_val != rand1) {
265 pr_err("kretprobe handler not called\n");
266 handler_errors++;
269 krph_val = 0;
270 ret = target2(rand1);
271 if (krph_val != rand1) {
272 pr_err("kretprobe handler2 not called\n");
273 handler_errors++;
275 unregister_kretprobes(rps, 2);
276 return 0;
278 #endif /* CONFIG_KRETPROBES */
280 int init_test_probes(void)
282 int ret;
284 target = kprobe_target;
285 target2 = kprobe_target2;
287 do {
288 rand1 = prandom_u32();
289 } while (rand1 <= div_factor);
291 pr_info("started\n");
292 num_tests++;
293 ret = test_kprobe();
294 if (ret < 0)
295 errors++;
297 num_tests++;
298 ret = test_kprobes();
299 if (ret < 0)
300 errors++;
302 #ifdef CONFIG_KRETPROBES
303 num_tests++;
304 ret = test_kretprobe();
305 if (ret < 0)
306 errors++;
308 num_tests++;
309 ret = test_kretprobes();
310 if (ret < 0)
311 errors++;
312 #endif /* CONFIG_KRETPROBES */
314 if (errors)
315 pr_err("BUG: %d out of %d tests failed\n", errors, num_tests);
316 else if (handler_errors)
317 pr_err("BUG: %d error(s) running handlers\n", handler_errors);
318 else
319 pr_info("passed successfully\n");
321 return 0;