Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * test_kprobes.c - simple sanity test for *probes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright IBM Corp. 2008
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define pr_fmt(fmt) "Kprobe smoke test: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #define div_factor 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) static u32 rand1, preh_val, posth_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static int errors, handler_errors, num_tests;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static u32 (*target)(u32 value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static u32 (*target2)(u32 value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static noinline u32 kprobe_target(u32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	return (value / div_factor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (preemptible()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		pr_err("pre-handler is preemptible\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	preh_val = (rand1 / div_factor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static void kp_post_handler(struct kprobe *p, struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (preemptible()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		pr_err("post-handler is preemptible\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (preh_val != (rand1 / div_factor)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		pr_err("incorrect value in post_handler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	posth_val = preh_val + div_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static struct kprobe kp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	.symbol_name = "kprobe_target",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	.pre_handler = kp_pre_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	.post_handler = kp_post_handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static int test_kprobe(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	ret = register_kprobe(&kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		pr_err("register_kprobe returned %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	ret = target(rand1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	unregister_kprobe(&kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (preh_val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		pr_err("kprobe pre_handler not called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (posth_val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		pr_err("kprobe post_handler not called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static noinline u32 kprobe_target2(u32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return (value / div_factor) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	preh_val = (rand1 / div_factor) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (preh_val != (rand1 / div_factor) + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		pr_err("incorrect value in post_handler2\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	posth_val = preh_val + div_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static struct kprobe kp2 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	.symbol_name = "kprobe_target2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	.pre_handler = kp_pre_handler2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	.post_handler = kp_post_handler2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int test_kprobes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct kprobe *kps[2] = {&kp, &kp2};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/* addr and flags should be cleard for reusing kprobe. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	kp.addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	kp.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	ret = register_kprobes(kps, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		pr_err("register_kprobes returned %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	preh_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	posth_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	ret = target(rand1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (preh_val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		pr_err("kprobe pre_handler not called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (posth_val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		pr_err("kprobe post_handler not called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	preh_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	posth_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	ret = target2(rand1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (preh_val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		pr_err("kprobe pre_handler2 not called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (posth_val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		pr_err("kprobe post_handler2 not called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	unregister_kprobes(kps, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) #ifdef CONFIG_KRETPROBES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static u32 krph_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (preemptible()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		pr_err("kretprobe entry handler is preemptible\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	krph_val = (rand1 / div_factor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	unsigned long ret = regs_return_value(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (preemptible()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		pr_err("kretprobe return handler is preemptible\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (ret != (rand1 / div_factor)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		pr_err("incorrect value in kretprobe handler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (krph_val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		pr_err("call to kretprobe entry handler failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	krph_val = rand1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static struct kretprobe rp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	.handler	= return_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	.entry_handler  = entry_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	.kp.symbol_name = "kprobe_target"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static int test_kretprobe(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	ret = register_kretprobe(&rp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		pr_err("register_kretprobe returned %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	ret = target(rand1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	unregister_kretprobe(&rp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (krph_val != rand1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		pr_err("kretprobe handler not called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	unsigned long ret = regs_return_value(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (ret != (rand1 / div_factor) + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		pr_err("incorrect value in kretprobe handler2\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (krph_val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		pr_err("call to kretprobe entry handler failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	krph_val = rand1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static struct kretprobe rp2 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	.handler	= return_handler2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	.entry_handler  = entry_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	.kp.symbol_name = "kprobe_target2"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static int test_kretprobes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	struct kretprobe *rps[2] = {&rp, &rp2};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	/* addr and flags should be cleard for reusing kprobe. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	rp.kp.addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	rp.kp.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	ret = register_kretprobes(rps, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		pr_err("register_kretprobe returned %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	krph_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	ret = target(rand1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (krph_val != rand1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		pr_err("kretprobe handler not called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	krph_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	ret = target2(rand1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (krph_val != rand1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		pr_err("kretprobe handler2 not called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		handler_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	unregister_kretprobes(rps, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) #endif /* CONFIG_KRETPROBES */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) int init_test_probes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	target = kprobe_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	target2 = kprobe_target2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		rand1 = prandom_u32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	} while (rand1 <= div_factor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	pr_info("started\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	num_tests++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	ret = test_kprobe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	num_tests++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	ret = test_kprobes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) #ifdef CONFIG_KRETPROBES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	num_tests++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	ret = test_kretprobe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	num_tests++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	ret = test_kretprobes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) #endif /* CONFIG_KRETPROBES */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	if (errors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		pr_err("BUG: %d out of %d tests failed\n", errors, num_tests);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	else if (handler_errors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		pr_err("BUG: %d error(s) running handlers\n", handler_errors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		pr_info("passed successfully\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }