test_unwind.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Test module for unwind_for_each_frame
  4. */
  5. #include <kunit/test.h>
  6. #include <asm/unwind.h>
  7. #include <linux/completion.h>
  8. #include <linux/kallsyms.h>
  9. #include <linux/kthread.h>
  10. #include <linux/ftrace.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. static struct kunit *current_test;
  19. #define BT_BUF_SIZE (PAGE_SIZE * 4)
  20. static bool force_bt;
  21. module_param_named(backtrace, force_bt, bool, 0444);
  22. MODULE_PARM_DESC(backtrace, "print backtraces for all tests");
  23. /*
  24. * To avoid printk line limit split backtrace by lines
  25. */
  26. static void print_backtrace(char *bt)
  27. {
  28. char *p;
  29. while (true) {
  30. p = strsep(&bt, "\n");
  31. if (!p)
  32. break;
  33. kunit_err(current_test, "%s\n", p);
  34. }
  35. }
  36. /*
  37. * Calls unwind_for_each_frame(task, regs, sp) and verifies that the result
  38. * contains unwindme_func2 followed by unwindme_func1.
  39. */
  40. static noinline int test_unwind(struct task_struct *task, struct pt_regs *regs,
  41. unsigned long sp)
  42. {
  43. int frame_count, prev_is_func2, seen_func2_func1, seen_kretprobe_trampoline;
  44. const int max_frames = 128;
  45. struct unwind_state state;
  46. size_t bt_pos = 0;
  47. int ret = 0;
  48. char *bt;
  49. bt = kmalloc(BT_BUF_SIZE, GFP_ATOMIC);
  50. if (!bt) {
  51. kunit_err(current_test, "failed to allocate backtrace buffer\n");
  52. return -ENOMEM;
  53. }
  54. /* Unwind. */
  55. frame_count = 0;
  56. prev_is_func2 = 0;
  57. seen_func2_func1 = 0;
  58. seen_kretprobe_trampoline = 0;
  59. unwind_for_each_frame(&state, task, regs, sp) {
  60. unsigned long addr = unwind_get_return_address(&state);
  61. char sym[KSYM_SYMBOL_LEN];
  62. if (frame_count++ == max_frames)
  63. break;
  64. if (state.reliable && !addr) {
  65. kunit_err(current_test, "unwind state reliable but addr is 0\n");
  66. ret = -EINVAL;
  67. break;
  68. }
  69. sprint_symbol(sym, addr);
  70. if (bt_pos < BT_BUF_SIZE) {
  71. bt_pos += snprintf(bt + bt_pos, BT_BUF_SIZE - bt_pos,
  72. state.reliable ? " [%-7s%px] %pSR\n" :
  73. "([%-7s%px] %pSR)\n",
  74. stack_type_name(state.stack_info.type),
  75. (void *)state.sp, (void *)state.ip);
  76. if (bt_pos >= BT_BUF_SIZE)
  77. kunit_err(current_test, "backtrace buffer is too small\n");
  78. }
  79. frame_count += 1;
  80. if (prev_is_func2 && str_has_prefix(sym, "unwindme_func1"))
  81. seen_func2_func1 = 1;
  82. prev_is_func2 = str_has_prefix(sym, "unwindme_func2");
  83. if (str_has_prefix(sym, "__kretprobe_trampoline+0x0/"))
  84. seen_kretprobe_trampoline = 1;
  85. }
  86. /* Check the results. */
  87. if (unwind_error(&state)) {
  88. kunit_err(current_test, "unwind error\n");
  89. ret = -EINVAL;
  90. }
  91. if (!seen_func2_func1) {
  92. kunit_err(current_test, "unwindme_func2 and unwindme_func1 not found\n");
  93. ret = -EINVAL;
  94. }
  95. if (frame_count == max_frames) {
  96. kunit_err(current_test, "Maximum number of frames exceeded\n");
  97. ret = -EINVAL;
  98. }
  99. if (seen_kretprobe_trampoline) {
  100. kunit_err(current_test, "__kretprobe_trampoline+0x0 in unwinding results\n");
  101. ret = -EINVAL;
  102. }
  103. if (ret || force_bt)
  104. print_backtrace(bt);
  105. kfree(bt);
  106. return ret;
  107. }
  108. /* State of the task being unwound. */
  109. struct unwindme {
  110. int flags;
  111. int ret;
  112. struct task_struct *task;
  113. struct completion task_ready;
  114. wait_queue_head_t task_wq;
  115. unsigned long sp;
  116. };
  117. static struct unwindme *unwindme;
  118. /* Values of unwindme.flags. */
  119. #define UWM_DEFAULT 0x0
  120. #define UWM_THREAD 0x1 /* Unwind a separate task. */
  121. #define UWM_REGS 0x2 /* Pass regs to test_unwind(). */
  122. #define UWM_SP 0x4 /* Pass sp to test_unwind(). */
  123. #define UWM_CALLER 0x8 /* Unwind starting from caller. */
  124. #define UWM_SWITCH_STACK 0x10 /* Use call_on_stack. */
  125. #define UWM_IRQ 0x20 /* Unwind from irq context. */
  126. #define UWM_PGM 0x40 /* Unwind from program check handler */
  127. #define UWM_KPROBE_ON_FTRACE 0x80 /* Unwind from kprobe handler called via ftrace. */
  128. #define UWM_FTRACE 0x100 /* Unwind from ftrace handler. */
  129. #define UWM_KRETPROBE 0x200 /* Unwind through kretprobed function. */
  130. #define UWM_KRETPROBE_HANDLER 0x400 /* Unwind from kretprobe handler. */
  131. static __always_inline struct pt_regs fake_pt_regs(void)
  132. {
  133. struct pt_regs regs;
  134. memset(&regs, 0, sizeof(regs));
  135. regs.gprs[15] = current_stack_pointer;
  136. asm volatile(
  137. "basr %[psw_addr],0\n"
  138. : [psw_addr] "=d" (regs.psw.addr));
  139. return regs;
  140. }
  141. static int kretprobe_ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  142. {
  143. struct unwindme *u = unwindme;
  144. if (!(u->flags & UWM_KRETPROBE_HANDLER))
  145. return 0;
  146. u->ret = test_unwind(NULL, (u->flags & UWM_REGS) ? regs : NULL,
  147. (u->flags & UWM_SP) ? u->sp : 0);
  148. return 0;
  149. }
  150. static noinline notrace int test_unwind_kretprobed_func(struct unwindme *u)
  151. {
  152. struct pt_regs regs;
  153. if (!(u->flags & UWM_KRETPROBE))
  154. return 0;
  155. regs = fake_pt_regs();
  156. return test_unwind(NULL, (u->flags & UWM_REGS) ? &regs : NULL,
  157. (u->flags & UWM_SP) ? u->sp : 0);
  158. }
  159. static noinline int test_unwind_kretprobed_func_caller(struct unwindme *u)
  160. {
  161. return test_unwind_kretprobed_func(u);
  162. }
  163. static int test_unwind_kretprobe(struct unwindme *u)
  164. {
  165. int ret;
  166. struct kretprobe my_kretprobe;
  167. if (!IS_ENABLED(CONFIG_KPROBES))
  168. kunit_skip(current_test, "requires CONFIG_KPROBES");
  169. u->ret = -1; /* make sure kprobe is called */
  170. unwindme = u;
  171. memset(&my_kretprobe, 0, sizeof(my_kretprobe));
  172. my_kretprobe.handler = kretprobe_ret_handler;
  173. my_kretprobe.maxactive = 1;
  174. my_kretprobe.kp.addr = (kprobe_opcode_t *)test_unwind_kretprobed_func;
  175. ret = register_kretprobe(&my_kretprobe);
  176. if (ret < 0) {
  177. kunit_err(current_test, "register_kretprobe failed %d\n", ret);
  178. return -EINVAL;
  179. }
  180. ret = test_unwind_kretprobed_func_caller(u);
  181. unregister_kretprobe(&my_kretprobe);
  182. unwindme = NULL;
  183. if (u->flags & UWM_KRETPROBE_HANDLER)
  184. ret = u->ret;
  185. return ret;
  186. }
  187. static int kprobe_pre_handler(struct kprobe *p, struct pt_regs *regs)
  188. {
  189. struct unwindme *u = unwindme;
  190. u->ret = test_unwind(NULL, (u->flags & UWM_REGS) ? regs : NULL,
  191. (u->flags & UWM_SP) ? u->sp : 0);
  192. return 0;
  193. }
  194. extern const char test_unwind_kprobed_insn[];
  195. static noinline void test_unwind_kprobed_func(void)
  196. {
  197. asm volatile(
  198. " nopr %%r7\n"
  199. "test_unwind_kprobed_insn:\n"
  200. " nopr %%r7\n"
  201. :);
  202. }
  203. static int test_unwind_kprobe(struct unwindme *u)
  204. {
  205. struct kprobe kp;
  206. int ret;
  207. if (!IS_ENABLED(CONFIG_KPROBES))
  208. kunit_skip(current_test, "requires CONFIG_KPROBES");
  209. if (!IS_ENABLED(CONFIG_KPROBES_ON_FTRACE) && u->flags & UWM_KPROBE_ON_FTRACE)
  210. kunit_skip(current_test, "requires CONFIG_KPROBES_ON_FTRACE");
  211. u->ret = -1; /* make sure kprobe is called */
  212. unwindme = u;
  213. memset(&kp, 0, sizeof(kp));
  214. kp.pre_handler = kprobe_pre_handler;
  215. kp.addr = u->flags & UWM_KPROBE_ON_FTRACE ?
  216. (kprobe_opcode_t *)test_unwind_kprobed_func :
  217. (kprobe_opcode_t *)test_unwind_kprobed_insn;
  218. ret = register_kprobe(&kp);
  219. if (ret < 0) {
  220. kunit_err(current_test, "register_kprobe failed %d\n", ret);
  221. return -EINVAL;
  222. }
  223. test_unwind_kprobed_func();
  224. unregister_kprobe(&kp);
  225. unwindme = NULL;
  226. return u->ret;
  227. }
  228. static void notrace __used test_unwind_ftrace_handler(unsigned long ip,
  229. unsigned long parent_ip,
  230. struct ftrace_ops *fops,
  231. struct ftrace_regs *fregs)
  232. {
  233. struct unwindme *u = (struct unwindme *)fregs->regs.gprs[2];
  234. u->ret = test_unwind(NULL, (u->flags & UWM_REGS) ? &fregs->regs : NULL,
  235. (u->flags & UWM_SP) ? u->sp : 0);
  236. }
  237. static noinline int test_unwind_ftraced_func(struct unwindme *u)
  238. {
  239. return READ_ONCE(u)->ret;
  240. }
  241. static int test_unwind_ftrace(struct unwindme *u)
  242. {
  243. int ret;
  244. #ifdef CONFIG_DYNAMIC_FTRACE
  245. struct ftrace_ops *fops;
  246. fops = kunit_kzalloc(current_test, sizeof(*fops), GFP_KERNEL);
  247. fops->func = test_unwind_ftrace_handler;
  248. fops->flags = FTRACE_OPS_FL_DYNAMIC |
  249. FTRACE_OPS_FL_RECURSION |
  250. FTRACE_OPS_FL_SAVE_REGS |
  251. FTRACE_OPS_FL_PERMANENT;
  252. #else
  253. kunit_skip(current_test, "requires CONFIG_DYNAMIC_FTRACE");
  254. #endif
  255. ret = ftrace_set_filter_ip(fops, (unsigned long)test_unwind_ftraced_func, 0, 0);
  256. if (ret) {
  257. kunit_err(current_test, "failed to set ftrace filter (%d)\n", ret);
  258. return -1;
  259. }
  260. ret = register_ftrace_function(fops);
  261. if (!ret) {
  262. ret = test_unwind_ftraced_func(u);
  263. unregister_ftrace_function(fops);
  264. } else {
  265. kunit_err(current_test, "failed to register ftrace handler (%d)\n", ret);
  266. }
  267. ftrace_set_filter_ip(fops, (unsigned long)test_unwind_ftraced_func, 1, 0);
  268. return ret;
  269. }
  270. /* This function may or may not appear in the backtrace. */
  271. static noinline int unwindme_func4(struct unwindme *u)
  272. {
  273. if (!(u->flags & UWM_CALLER))
  274. u->sp = current_frame_address();
  275. if (u->flags & UWM_THREAD) {
  276. complete(&u->task_ready);
  277. wait_event(u->task_wq, kthread_should_park());
  278. kthread_parkme();
  279. return 0;
  280. } else if (u->flags & (UWM_PGM | UWM_KPROBE_ON_FTRACE)) {
  281. return test_unwind_kprobe(u);
  282. } else if (u->flags & (UWM_KRETPROBE | UWM_KRETPROBE_HANDLER)) {
  283. return test_unwind_kretprobe(u);
  284. } else if (u->flags & UWM_FTRACE) {
  285. return test_unwind_ftrace(u);
  286. } else {
  287. struct pt_regs regs = fake_pt_regs();
  288. return test_unwind(NULL,
  289. (u->flags & UWM_REGS) ? &regs : NULL,
  290. (u->flags & UWM_SP) ? u->sp : 0);
  291. }
  292. }
  293. /* This function may or may not appear in the backtrace. */
  294. static noinline int unwindme_func3(struct unwindme *u)
  295. {
  296. u->sp = current_frame_address();
  297. return unwindme_func4(u);
  298. }
  299. /* This function must appear in the backtrace. */
  300. static noinline int unwindme_func2(struct unwindme *u)
  301. {
  302. unsigned long flags;
  303. int rc;
  304. if (u->flags & UWM_SWITCH_STACK) {
  305. local_irq_save(flags);
  306. local_mcck_disable();
  307. rc = call_on_stack(1, S390_lowcore.nodat_stack,
  308. int, unwindme_func3, struct unwindme *, u);
  309. local_mcck_enable();
  310. local_irq_restore(flags);
  311. return rc;
  312. } else {
  313. return unwindme_func3(u);
  314. }
  315. }
  316. /* This function must follow unwindme_func2 in the backtrace. */
  317. static noinline int unwindme_func1(void *u)
  318. {
  319. return unwindme_func2((struct unwindme *)u);
  320. }
  321. static void unwindme_timer_fn(struct timer_list *unused)
  322. {
  323. struct unwindme *u = READ_ONCE(unwindme);
  324. if (u) {
  325. unwindme = NULL;
  326. u->task = NULL;
  327. u->ret = unwindme_func1(u);
  328. complete(&u->task_ready);
  329. }
  330. }
  331. static struct timer_list unwind_timer;
  332. static int test_unwind_irq(struct unwindme *u)
  333. {
  334. unwindme = u;
  335. init_completion(&u->task_ready);
  336. timer_setup(&unwind_timer, unwindme_timer_fn, 0);
  337. mod_timer(&unwind_timer, jiffies + 1);
  338. wait_for_completion(&u->task_ready);
  339. return u->ret;
  340. }
  341. /* Spawns a task and passes it to test_unwind(). */
  342. static int test_unwind_task(struct unwindme *u)
  343. {
  344. struct task_struct *task;
  345. int ret;
  346. /* Initialize thread-related fields. */
  347. init_completion(&u->task_ready);
  348. init_waitqueue_head(&u->task_wq);
  349. /*
  350. * Start the task and wait until it reaches unwindme_func4() and sleeps
  351. * in (task_ready, unwind_done] range.
  352. */
  353. task = kthread_run(unwindme_func1, u, "%s", __func__);
  354. if (IS_ERR(task)) {
  355. kunit_err(current_test, "kthread_run() failed\n");
  356. return PTR_ERR(task);
  357. }
  358. /*
  359. * Make sure task reaches unwindme_func4 before parking it,
  360. * we might park it before kthread function has been executed otherwise
  361. */
  362. wait_for_completion(&u->task_ready);
  363. kthread_park(task);
  364. /* Unwind. */
  365. ret = test_unwind(task, NULL, (u->flags & UWM_SP) ? u->sp : 0);
  366. kthread_stop(task);
  367. return ret;
  368. }
  369. struct test_params {
  370. int flags;
  371. char *name;
  372. };
  373. /*
  374. * Create required parameter list for tests
  375. */
  376. #define TEST_WITH_FLAGS(f) { .flags = f, .name = #f }
  377. static const struct test_params param_list[] = {
  378. TEST_WITH_FLAGS(UWM_DEFAULT),
  379. TEST_WITH_FLAGS(UWM_SP),
  380. TEST_WITH_FLAGS(UWM_REGS),
  381. TEST_WITH_FLAGS(UWM_SWITCH_STACK),
  382. TEST_WITH_FLAGS(UWM_SP | UWM_REGS),
  383. TEST_WITH_FLAGS(UWM_CALLER | UWM_SP),
  384. TEST_WITH_FLAGS(UWM_CALLER | UWM_SP | UWM_REGS),
  385. TEST_WITH_FLAGS(UWM_CALLER | UWM_SP | UWM_REGS | UWM_SWITCH_STACK),
  386. TEST_WITH_FLAGS(UWM_THREAD),
  387. TEST_WITH_FLAGS(UWM_THREAD | UWM_SP),
  388. TEST_WITH_FLAGS(UWM_THREAD | UWM_CALLER | UWM_SP),
  389. TEST_WITH_FLAGS(UWM_IRQ),
  390. TEST_WITH_FLAGS(UWM_IRQ | UWM_SWITCH_STACK),
  391. TEST_WITH_FLAGS(UWM_IRQ | UWM_SP),
  392. TEST_WITH_FLAGS(UWM_IRQ | UWM_REGS),
  393. TEST_WITH_FLAGS(UWM_IRQ | UWM_SP | UWM_REGS),
  394. TEST_WITH_FLAGS(UWM_IRQ | UWM_CALLER | UWM_SP),
  395. TEST_WITH_FLAGS(UWM_IRQ | UWM_CALLER | UWM_SP | UWM_REGS),
  396. TEST_WITH_FLAGS(UWM_IRQ | UWM_CALLER | UWM_SP | UWM_REGS | UWM_SWITCH_STACK),
  397. TEST_WITH_FLAGS(UWM_PGM),
  398. TEST_WITH_FLAGS(UWM_PGM | UWM_SP),
  399. TEST_WITH_FLAGS(UWM_PGM | UWM_REGS),
  400. TEST_WITH_FLAGS(UWM_PGM | UWM_SP | UWM_REGS),
  401. TEST_WITH_FLAGS(UWM_KPROBE_ON_FTRACE),
  402. TEST_WITH_FLAGS(UWM_KPROBE_ON_FTRACE | UWM_SP),
  403. TEST_WITH_FLAGS(UWM_KPROBE_ON_FTRACE | UWM_REGS),
  404. TEST_WITH_FLAGS(UWM_KPROBE_ON_FTRACE | UWM_SP | UWM_REGS),
  405. TEST_WITH_FLAGS(UWM_FTRACE),
  406. TEST_WITH_FLAGS(UWM_FTRACE | UWM_SP),
  407. TEST_WITH_FLAGS(UWM_FTRACE | UWM_REGS),
  408. TEST_WITH_FLAGS(UWM_FTRACE | UWM_SP | UWM_REGS),
  409. TEST_WITH_FLAGS(UWM_KRETPROBE),
  410. TEST_WITH_FLAGS(UWM_KRETPROBE | UWM_SP),
  411. TEST_WITH_FLAGS(UWM_KRETPROBE | UWM_REGS),
  412. TEST_WITH_FLAGS(UWM_KRETPROBE | UWM_SP | UWM_REGS),
  413. TEST_WITH_FLAGS(UWM_KRETPROBE_HANDLER),
  414. TEST_WITH_FLAGS(UWM_KRETPROBE_HANDLER | UWM_SP),
  415. TEST_WITH_FLAGS(UWM_KRETPROBE_HANDLER | UWM_REGS),
  416. TEST_WITH_FLAGS(UWM_KRETPROBE_HANDLER | UWM_SP | UWM_REGS),
  417. };
  418. /*
  419. * Parameter description generator: required for KUNIT_ARRAY_PARAM()
  420. */
  421. static void get_desc(const struct test_params *params, char *desc)
  422. {
  423. strscpy(desc, params->name, KUNIT_PARAM_DESC_SIZE);
  424. }
  425. /*
  426. * Create test_unwind_gen_params
  427. */
  428. KUNIT_ARRAY_PARAM(test_unwind, param_list, get_desc);
  429. static void test_unwind_flags(struct kunit *test)
  430. {
  431. struct unwindme u;
  432. const struct test_params *params;
  433. current_test = test;
  434. params = (const struct test_params *)test->param_value;
  435. u.flags = params->flags;
  436. if (u.flags & UWM_THREAD)
  437. KUNIT_EXPECT_EQ(test, 0, test_unwind_task(&u));
  438. else if (u.flags & UWM_IRQ)
  439. KUNIT_EXPECT_EQ(test, 0, test_unwind_irq(&u));
  440. else
  441. KUNIT_EXPECT_EQ(test, 0, unwindme_func1(&u));
  442. }
  443. static struct kunit_case unwind_test_cases[] = {
  444. KUNIT_CASE_PARAM(test_unwind_flags, test_unwind_gen_params),
  445. {}
  446. };
  447. static struct kunit_suite test_unwind_suite = {
  448. .name = "test_unwind",
  449. .test_cases = unwind_test_cases,
  450. };
  451. kunit_test_suites(&test_unwind_suite);
  452. MODULE_LICENSE("GPL");