1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Test module for unwind_for_each_frame 4 */ 5 6 #include <kunit/test.h> 7 #include <asm/unwind.h> 8 #include <linux/completion.h> 9 #include <linux/kallsyms.h> 10 #include <linux/kthread.h> 11 #include <linux/module.h> 12 #include <linux/timer.h> 13 #include <linux/slab.h> 14 #include <linux/string.h> 15 #include <linux/kprobes.h> 16 #include <linux/wait.h> 17 #include <asm/irq.h> 18 19 struct kunit *current_test; 20 21 #define BT_BUF_SIZE (PAGE_SIZE * 4) 22 23 /* 24 * To avoid printk line limit split backtrace by lines 25 */ 26 static void print_backtrace(char *bt) 27 { 28 char *p; 29 30 while (true) { 31 p = strsep(&bt, "\n"); 32 if (!p) 33 break; 34 kunit_err(current_test, "%s\n", p); 35 } 36 } 37 38 /* 39 * Calls unwind_for_each_frame(task, regs, sp) and verifies that the result 40 * contains unwindme_func2 followed by unwindme_func1. 41 */ 42 static noinline int test_unwind(struct task_struct *task, struct pt_regs *regs, 43 unsigned long sp) 44 { 45 int frame_count, prev_is_func2, seen_func2_func1; 46 const int max_frames = 128; 47 struct unwind_state state; 48 size_t bt_pos = 0; 49 int ret = 0; 50 char *bt; 51 52 bt = kmalloc(BT_BUF_SIZE, GFP_ATOMIC); 53 if (!bt) { 54 kunit_err(current_test, "failed to allocate backtrace buffer\n"); 55 return -ENOMEM; 56 } 57 /* Unwind. */ 58 frame_count = 0; 59 prev_is_func2 = 0; 60 seen_func2_func1 = 0; 61 unwind_for_each_frame(&state, task, regs, sp) { 62 unsigned long addr = unwind_get_return_address(&state); 63 char sym[KSYM_SYMBOL_LEN]; 64 65 if (frame_count++ == max_frames) 66 break; 67 if (state.reliable && !addr) { 68 kunit_err(current_test, "unwind state reliable but addr is 0\n"); 69 ret = -EINVAL; 70 break; 71 } 72 sprint_symbol(sym, addr); 73 if (bt_pos < BT_BUF_SIZE) { 74 bt_pos += snprintf(bt + bt_pos, BT_BUF_SIZE - bt_pos, 75 state.reliable ? " [%-7s%px] %pSR\n" : 76 "([%-7s%px] %pSR)\n", 77 stack_type_name(state.stack_info.type), 78 (void *)state.sp, (void *)state.ip); 79 if (bt_pos >= BT_BUF_SIZE) 80 kunit_err(current_test, "backtrace buffer is too small\n"); 81 } 82 frame_count += 1; 83 if (prev_is_func2 && str_has_prefix(sym, "unwindme_func1")) 84 seen_func2_func1 = 1; 85 prev_is_func2 = str_has_prefix(sym, "unwindme_func2"); 86 } 87 88 /* Check the results. */ 89 if (unwind_error(&state)) { 90 kunit_err(current_test, "unwind error\n"); 91 ret = -EINVAL; 92 } 93 if (!seen_func2_func1) { 94 kunit_err(current_test, "unwindme_func2 and unwindme_func1 not found\n"); 95 ret = -EINVAL; 96 } 97 if (frame_count == max_frames) { 98 kunit_err(current_test, "Maximum number of frames exceeded\n"); 99 ret = -EINVAL; 100 } 101 if (ret) 102 print_backtrace(bt); 103 kfree(bt); 104 return ret; 105 } 106 107 /* State of the task being unwound. */ 108 struct unwindme { 109 int flags; 110 int ret; 111 struct task_struct *task; 112 struct completion task_ready; 113 wait_queue_head_t task_wq; 114 unsigned long sp; 115 }; 116 117 static struct unwindme *unwindme; 118 119 /* Values of unwindme.flags. */ 120 #define UWM_DEFAULT 0x0 121 #define UWM_THREAD 0x1 /* Unwind a separate task. */ 122 #define UWM_REGS 0x2 /* Pass regs to test_unwind(). */ 123 #define UWM_SP 0x4 /* Pass sp to test_unwind(). */ 124 #define UWM_CALLER 0x8 /* Unwind starting from caller. */ 125 #define UWM_SWITCH_STACK 0x10 /* Use call_on_stack. */ 126 #define UWM_IRQ 0x20 /* Unwind from irq context. */ 127 #define UWM_PGM 0x40 /* Unwind from program check handler. */ 128 129 static __always_inline unsigned long get_psw_addr(void) 130 { 131 unsigned long psw_addr; 132 133 asm volatile( 134 "basr %[psw_addr],0\n" 135 : [psw_addr] "=d" (psw_addr)); 136 return psw_addr; 137 } 138 139 #ifdef CONFIG_KPROBES 140 static int pgm_pre_handler(struct kprobe *p, struct pt_regs *regs) 141 { 142 struct unwindme *u = unwindme; 143 144 u->ret = test_unwind(NULL, (u->flags & UWM_REGS) ? regs : NULL, 145 (u->flags & UWM_SP) ? u->sp : 0); 146 return 0; 147 } 148 #endif 149 150 /* This function may or may not appear in the backtrace. */ 151 static noinline int unwindme_func4(struct unwindme *u) 152 { 153 if (!(u->flags & UWM_CALLER)) 154 u->sp = current_frame_address(); 155 if (u->flags & UWM_THREAD) { 156 complete(&u->task_ready); 157 wait_event(u->task_wq, kthread_should_park()); 158 kthread_parkme(); 159 return 0; 160 #ifdef CONFIG_KPROBES 161 } else if (u->flags & UWM_PGM) { 162 struct kprobe kp; 163 int ret; 164 165 unwindme = u; 166 memset(&kp, 0, sizeof(kp)); 167 kp.symbol_name = "do_report_trap"; 168 kp.pre_handler = pgm_pre_handler; 169 ret = register_kprobe(&kp); 170 if (ret < 0) { 171 kunit_err(current_test, "register_kprobe failed %d\n", ret); 172 return -EINVAL; 173 } 174 175 /* 176 * Trigger operation exception; use insn notation to bypass 177 * llvm's integrated assembler sanity checks. 178 */ 179 asm volatile( 180 " .insn e,0x0000\n" /* illegal opcode */ 181 "0: nopr %%r7\n" 182 EX_TABLE(0b, 0b) 183 :); 184 185 unregister_kprobe(&kp); 186 unwindme = NULL; 187 return u->ret; 188 #endif 189 } else { 190 struct pt_regs regs; 191 192 memset(®s, 0, sizeof(regs)); 193 regs.psw.addr = get_psw_addr(); 194 regs.gprs[15] = current_stack_pointer(); 195 return test_unwind(NULL, 196 (u->flags & UWM_REGS) ? ®s : NULL, 197 (u->flags & UWM_SP) ? u->sp : 0); 198 } 199 } 200 201 /* This function may or may not appear in the backtrace. */ 202 static noinline int unwindme_func3(struct unwindme *u) 203 { 204 u->sp = current_frame_address(); 205 return unwindme_func4(u); 206 } 207 208 /* This function must appear in the backtrace. */ 209 static noinline int unwindme_func2(struct unwindme *u) 210 { 211 unsigned long flags; 212 int rc; 213 214 if (u->flags & UWM_SWITCH_STACK) { 215 local_irq_save(flags); 216 local_mcck_disable(); 217 rc = call_on_stack(1, S390_lowcore.nodat_stack, 218 int, unwindme_func3, struct unwindme *, u); 219 local_mcck_enable(); 220 local_irq_restore(flags); 221 return rc; 222 } else { 223 return unwindme_func3(u); 224 } 225 } 226 227 /* This function must follow unwindme_func2 in the backtrace. */ 228 static noinline int unwindme_func1(void *u) 229 { 230 return unwindme_func2((struct unwindme *)u); 231 } 232 233 static void unwindme_timer_fn(struct timer_list *unused) 234 { 235 struct unwindme *u = READ_ONCE(unwindme); 236 237 if (u) { 238 unwindme = NULL; 239 u->task = NULL; 240 u->ret = unwindme_func1(u); 241 complete(&u->task_ready); 242 } 243 } 244 245 static struct timer_list unwind_timer; 246 247 static int test_unwind_irq(struct unwindme *u) 248 { 249 unwindme = u; 250 init_completion(&u->task_ready); 251 timer_setup(&unwind_timer, unwindme_timer_fn, 0); 252 mod_timer(&unwind_timer, jiffies + 1); 253 wait_for_completion(&u->task_ready); 254 return u->ret; 255 } 256 257 /* Spawns a task and passes it to test_unwind(). */ 258 static int test_unwind_task(struct kunit *test, struct unwindme *u) 259 { 260 struct task_struct *task; 261 int ret; 262 263 /* Initialize thread-related fields. */ 264 init_completion(&u->task_ready); 265 init_waitqueue_head(&u->task_wq); 266 267 /* 268 * Start the task and wait until it reaches unwindme_func4() and sleeps 269 * in (task_ready, unwind_done] range. 270 */ 271 task = kthread_run(unwindme_func1, u, "%s", __func__); 272 if (IS_ERR(task)) { 273 kunit_err(test, "kthread_run() failed\n"); 274 return PTR_ERR(task); 275 } 276 /* 277 * Make sure task reaches unwindme_func4 before parking it, 278 * we might park it before kthread function has been executed otherwise 279 */ 280 wait_for_completion(&u->task_ready); 281 kthread_park(task); 282 /* Unwind. */ 283 ret = test_unwind(task, NULL, (u->flags & UWM_SP) ? u->sp : 0); 284 kthread_stop(task); 285 return ret; 286 } 287 288 struct test_params { 289 int flags; 290 char *name; 291 }; 292 293 /* 294 * Create required parameter list for tests 295 */ 296 static const struct test_params param_list[] = { 297 {.flags = UWM_DEFAULT, .name = "UWM_DEFAULT"}, 298 {.flags = UWM_SP, .name = "UWM_SP"}, 299 {.flags = UWM_REGS, .name = "UWM_REGS"}, 300 {.flags = UWM_SWITCH_STACK, 301 .name = "UWM_SWITCH_STACK"}, 302 {.flags = UWM_SP | UWM_REGS, 303 .name = "UWM_SP | UWM_REGS"}, 304 {.flags = UWM_CALLER | UWM_SP, 305 .name = "WM_CALLER | UWM_SP"}, 306 {.flags = UWM_CALLER | UWM_SP | UWM_REGS, 307 .name = "UWM_CALLER | UWM_SP | UWM_REGS"}, 308 {.flags = UWM_CALLER | UWM_SP | UWM_REGS | UWM_SWITCH_STACK, 309 .name = "UWM_CALLER | UWM_SP | UWM_REGS | UWM_SWITCH_STACK"}, 310 {.flags = UWM_THREAD, .name = "UWM_THREAD"}, 311 {.flags = UWM_THREAD | UWM_SP, 312 .name = "UWM_THREAD | UWM_SP"}, 313 {.flags = UWM_THREAD | UWM_CALLER | UWM_SP, 314 .name = "UWM_THREAD | UWM_CALLER | UWM_SP"}, 315 {.flags = UWM_IRQ, .name = "UWM_IRQ"}, 316 {.flags = UWM_IRQ | UWM_SWITCH_STACK, 317 .name = "UWM_IRQ | UWM_SWITCH_STACK"}, 318 {.flags = UWM_IRQ | UWM_SP, 319 .name = "UWM_IRQ | UWM_SP"}, 320 {.flags = UWM_IRQ | UWM_REGS, 321 .name = "UWM_IRQ | UWM_REGS"}, 322 {.flags = UWM_IRQ | UWM_SP | UWM_REGS, 323 .name = "UWM_IRQ | UWM_SP | UWM_REGS"}, 324 {.flags = UWM_IRQ | UWM_CALLER | UWM_SP, 325 .name = "UWM_IRQ | UWM_CALLER | UWM_SP"}, 326 {.flags = UWM_IRQ | UWM_CALLER | UWM_SP | UWM_REGS, 327 .name = "UWM_IRQ | UWM_CALLER | UWM_SP | UWM_REGS"}, 328 {.flags = UWM_IRQ | UWM_CALLER | UWM_SP | UWM_REGS | UWM_SWITCH_STACK, 329 .name = "UWM_IRQ | UWM_CALLER | UWM_SP | UWM_REGS | UWM_SWITCH_STACK"}, 330 #ifdef CONFIG_KPROBES 331 {.flags = UWM_PGM, .name = "UWM_PGM"}, 332 {.flags = UWM_PGM | UWM_SP, 333 .name = "UWM_PGM | UWM_SP"}, 334 {.flags = UWM_PGM | UWM_REGS, 335 .name = "UWM_PGM | UWM_REGS"}, 336 {.flags = UWM_PGM | UWM_SP | UWM_REGS, 337 .name = "UWM_PGM | UWM_SP | UWM_REGS"}, 338 #endif 339 }; 340 341 /* 342 * Parameter description generator: required for KUNIT_ARRAY_PARAM() 343 */ 344 static void get_desc(const struct test_params *params, char *desc) 345 { 346 strscpy(desc, params->name, KUNIT_PARAM_DESC_SIZE); 347 } 348 349 /* 350 * Create test_unwind_gen_params 351 */ 352 KUNIT_ARRAY_PARAM(test_unwind, param_list, get_desc); 353 354 static void test_unwind_flags(struct kunit *test) 355 { 356 struct unwindme u; 357 const struct test_params *params; 358 359 current_test = test; 360 params = (const struct test_params *)test->param_value; 361 u.flags = params->flags; 362 if (u.flags & UWM_THREAD) 363 KUNIT_EXPECT_EQ(test, 0, test_unwind_task(test, &u)); 364 else if (u.flags & UWM_IRQ) 365 KUNIT_EXPECT_EQ(test, 0, test_unwind_irq(&u)); 366 else 367 KUNIT_EXPECT_EQ(test, 0, unwindme_func1(&u)); 368 } 369 370 static struct kunit_case unwind_test_cases[] = { 371 KUNIT_CASE_PARAM(test_unwind_flags, test_unwind_gen_params), 372 {} 373 }; 374 375 static struct kunit_suite test_unwind_suite = { 376 .name = "test_unwind", 377 .test_cases = unwind_test_cases, 378 }; 379 380 kunit_test_suites(&test_unwind_suite); 381 382 MODULE_LICENSE("GPL"); 383