1 // SPDX-License-Identifier: GPL-2.0-or-later 1 2 /* 3 * test_fprobe.c - simple sanity test for fpro 4 */ 5 6 #include <linux/kernel.h> 7 #include <linux/fprobe.h> 8 #include <linux/random.h> 9 #include <kunit/test.h> 10 11 #define div_factor 3 12 13 static struct kunit *current_test; 14 15 static u32 rand1, entry_val, exit_val; 16 17 /* Use indirect calls to avoid inlining the ta 18 static u32 (*target)(u32 value); 19 static u32 (*target2)(u32 value); 20 static u32 (*target_nest)(u32 value, u32 (*nes 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; 25 26 static noinline u32 fprobe_selftest_target(u32 27 { 28 return (value / div_factor); 29 } 30 31 static noinline u32 fprobe_selftest_target2(u3 32 { 33 return (value / div_factor) + 1; 34 } 35 36 static noinline u32 fprobe_selftest_nest_targe 37 { 38 return nest(value + 2); 39 } 40 41 static notrace int fp_entry_handler(struct fpr 42 unsigned l 43 struct pt_ 44 { 45 KUNIT_EXPECT_FALSE(current_test, preem 46 /* This can be called on the fprobe_se 47 if (ip != target_ip) 48 KUNIT_EXPECT_EQ(current_test, 49 entry_val = (rand1 / div_factor); 50 if (fp->entry_data_size) { 51 KUNIT_EXPECT_NOT_NULL(current_ 52 if (data) 53 *(u32 *)data = entry_v 54 } else 55 KUNIT_EXPECT_NULL(current_test 56 57 return entry_return_value; 58 } 59 60 static notrace void fp_exit_handler(struct fpr 61 unsigned l 62 struct pt_ 63 { 64 unsigned long ret = regs_return_value( 65 66 KUNIT_EXPECT_FALSE(current_test, preem 67 if (ip != target_ip) { 68 KUNIT_EXPECT_EQ(current_test, 69 KUNIT_EXPECT_EQ(current_test, 70 } else 71 KUNIT_EXPECT_EQ(current_test, 72 KUNIT_EXPECT_EQ(current_test, entry_va 73 exit_val = entry_val + div_factor; 74 if (fp->entry_data_size) { 75 KUNIT_EXPECT_NOT_NULL(current_ 76 if (data) 77 KUNIT_EXPECT_EQ(curren 78 } else 79 KUNIT_EXPECT_NULL(current_test 80 } 81 82 static notrace int nest_entry_handler(struct f 83 unsigned 84 struct p 85 { 86 KUNIT_EXPECT_FALSE(current_test, preem 87 return 0; 88 } 89 90 static notrace void nest_exit_handler(struct f 91 unsigned 92 struct p 93 { 94 KUNIT_EXPECT_FALSE(current_test, preem 95 KUNIT_EXPECT_EQ(current_test, ip, targ 96 } 97 98 /* Test entry only (no rethook) */ 99 static void test_fprobe_entry(struct kunit *te 100 { 101 struct fprobe fp_entry = { 102 .entry_handler = fp_entry_hand 103 }; 104 105 current_test = test; 106 107 /* Before register, unregister should 108 KUNIT_EXPECT_NE(test, 0, unregister_fp 109 KUNIT_EXPECT_EQ(test, 0, register_fpro 110 111 entry_val = 0; 112 exit_val = 0; 113 target(rand1); 114 KUNIT_EXPECT_NE(test, 0, entry_val); 115 KUNIT_EXPECT_EQ(test, 0, exit_val); 116 117 entry_val = 0; 118 exit_val = 0; 119 target2(rand1); 120 KUNIT_EXPECT_NE(test, 0, entry_val); 121 KUNIT_EXPECT_EQ(test, 0, exit_val); 122 123 KUNIT_EXPECT_EQ(test, 0, unregister_fp 124 } 125 126 static void test_fprobe(struct kunit *test) 127 { 128 struct fprobe fp = { 129 .entry_handler = fp_entry_hand 130 .exit_handler = fp_exit_handle 131 }; 132 133 current_test = test; 134 KUNIT_EXPECT_EQ(test, 0, register_fpro 135 136 entry_val = 0; 137 exit_val = 0; 138 target(rand1); 139 KUNIT_EXPECT_NE(test, 0, entry_val); 140 KUNIT_EXPECT_EQ(test, entry_val + div_ 141 142 entry_val = 0; 143 exit_val = 0; 144 target2(rand1); 145 KUNIT_EXPECT_NE(test, 0, entry_val); 146 KUNIT_EXPECT_EQ(test, entry_val + div_ 147 148 KUNIT_EXPECT_EQ(test, 0, unregister_fp 149 } 150 151 static void test_fprobe_syms(struct kunit *tes 152 { 153 static const char *syms[] = {"fprobe_s 154 struct fprobe fp = { 155 .entry_handler = fp_entry_hand 156 .exit_handler = fp_exit_handle 157 }; 158 159 current_test = test; 160 KUNIT_EXPECT_EQ(test, 0, register_fpro 161 162 entry_val = 0; 163 exit_val = 0; 164 target(rand1); 165 KUNIT_EXPECT_NE(test, 0, entry_val); 166 KUNIT_EXPECT_EQ(test, entry_val + div_ 167 168 entry_val = 0; 169 exit_val = 0; 170 target2(rand1); 171 KUNIT_EXPECT_NE(test, 0, entry_val); 172 KUNIT_EXPECT_EQ(test, entry_val + div_ 173 174 KUNIT_EXPECT_EQ(test, 0, unregister_fp 175 } 176 177 /* Test private entry_data */ 178 static void test_fprobe_data(struct kunit *tes 179 { 180 struct fprobe fp = { 181 .entry_handler = fp_entry_hand 182 .exit_handler = fp_exit_handle 183 .entry_data_size = sizeof(u32) 184 }; 185 186 current_test = test; 187 KUNIT_EXPECT_EQ(test, 0, register_fpro 188 189 target(rand1); 190 191 KUNIT_EXPECT_EQ(test, 0, unregister_fp 192 } 193 194 /* Test nr_maxactive */ 195 static void test_fprobe_nest(struct kunit *tes 196 { 197 static const char *syms[] = {"fprobe_s 198 struct fprobe fp = { 199 .entry_handler = nest_entry_ha 200 .exit_handler = nest_exit_hand 201 .nr_maxactive = 1, 202 }; 203 204 current_test = test; 205 KUNIT_EXPECT_EQ(test, 0, register_fpro 206 207 target_nest(rand1, target); 208 KUNIT_EXPECT_EQ(test, 1, fp.nmissed); 209 210 KUNIT_EXPECT_EQ(test, 0, unregister_fp 211 } 212 213 static void test_fprobe_skip(struct kunit *tes 214 { 215 struct fprobe fp = { 216 .entry_handler = fp_entry_hand 217 .exit_handler = fp_exit_handle 218 }; 219 220 current_test = test; 221 KUNIT_EXPECT_EQ(test, 0, register_fpro 222 223 entry_return_value = 1; 224 entry_val = 0; 225 exit_val = 0; 226 target(rand1); 227 KUNIT_EXPECT_NE(test, 0, entry_val); 228 KUNIT_EXPECT_EQ(test, 0, exit_val); 229 KUNIT_EXPECT_EQ(test, 0, fp.nmissed); 230 entry_return_value = 0; 231 232 KUNIT_EXPECT_EQ(test, 0, unregister_fp 233 } 234 235 static unsigned long get_ftrace_location(void 236 { 237 unsigned long size, addr = (unsigned l 238 239 if (!kallsyms_lookup_size_offset(addr, 240 return 0; 241 242 return ftrace_location_range(addr, add 243 } 244 245 static int fprobe_test_init(struct kunit *test 246 { 247 rand1 = get_random_u32_above(div_facto 248 target = fprobe_selftest_target; 249 target2 = fprobe_selftest_target2; 250 target_nest = fprobe_selftest_nest_tar 251 target_ip = get_ftrace_location(target 252 target2_ip = get_ftrace_location(targe 253 target_nest_ip = get_ftrace_location(t 254 255 return 0; 256 } 257 258 static struct kunit_case fprobe_testcases[] = 259 KUNIT_CASE(test_fprobe_entry), 260 KUNIT_CASE(test_fprobe), 261 KUNIT_CASE(test_fprobe_syms), 262 KUNIT_CASE(test_fprobe_data), 263 KUNIT_CASE(test_fprobe_nest), 264 KUNIT_CASE(test_fprobe_skip), 265 {} 266 }; 267 268 static struct kunit_suite fprobe_test_suite = 269 .name = "fprobe_test", 270 .init = fprobe_test_init, 271 .test_cases = fprobe_testcases, 272 }; 273 274 kunit_test_suites(&fprobe_test_suite); 275 276
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.