1 // SPDX-License-Identifier: GPL-2.0-only
3 * Test module for unwind_for_each_frame
6 #define pr_fmt(fmt) "test_unwind: " fmt
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>
19 #define BT_BUF_SIZE (PAGE_SIZE * 4)
22 * To avoid printk line limit split backtrace by lines
24 static void print_backtrace(char *bt
)
29 p
= strsep(&bt
, "\n");
37 * Calls unwind_for_each_frame(task, regs, sp) and verifies that the result
38 * contains unwindme_func2 followed by unwindme_func1.
40 static noinline
int test_unwind(struct task_struct
*task
, struct pt_regs
*regs
,
43 int frame_count
, prev_is_func2
, seen_func2_func1
;
44 const int max_frames
= 128;
45 struct unwind_state state
;
50 bt
= kmalloc(BT_BUF_SIZE
, GFP_ATOMIC
);
52 pr_err("failed to allocate backtrace buffer\n");
59 unwind_for_each_frame(&state
, task
, regs
, sp
) {
60 unsigned long addr
= unwind_get_return_address(&state
);
61 char sym
[KSYM_SYMBOL_LEN
];
63 if (frame_count
++ == max_frames
)
65 if (state
.reliable
&& !addr
) {
66 pr_err("unwind state reliable but addr is 0\n");
70 sprint_symbol(sym
, addr
);
71 if (bt_pos
< BT_BUF_SIZE
) {
72 bt_pos
+= snprintf(bt
+ bt_pos
, BT_BUF_SIZE
- bt_pos
,
73 state
.reliable
? " [%-7s%px] %pSR\n" :
75 stack_type_name(state
.stack_info
.type
),
76 (void *)state
.sp
, (void *)state
.ip
);
77 if (bt_pos
>= BT_BUF_SIZE
)
78 pr_err("backtrace buffer is too small\n");
81 if (prev_is_func2
&& str_has_prefix(sym
, "unwindme_func1"))
83 prev_is_func2
= str_has_prefix(sym
, "unwindme_func2");
86 /* Check the results. */
87 if (unwind_error(&state
)) {
88 pr_err("unwind error\n");
91 if (!seen_func2_func1
) {
92 pr_err("unwindme_func2 and unwindme_func1 not found\n");
95 if (frame_count
== max_frames
) {
96 pr_err("Maximum number of frames exceeded\n");
105 /* State of the task being unwound. */
109 struct task_struct
*task
;
110 struct completion task_ready
;
111 wait_queue_head_t task_wq
;
115 static struct unwindme
*unwindme
;
117 /* Values of unwindme.flags. */
118 #define UWM_DEFAULT 0x0
119 #define UWM_THREAD 0x1 /* Unwind a separate task. */
120 #define UWM_REGS 0x2 /* Pass regs to test_unwind(). */
121 #define UWM_SP 0x4 /* Pass sp to test_unwind(). */
122 #define UWM_CALLER 0x8 /* Unwind starting from caller. */
123 #define UWM_SWITCH_STACK 0x10 /* Use CALL_ON_STACK. */
124 #define UWM_IRQ 0x20 /* Unwind from irq context. */
125 #define UWM_PGM 0x40 /* Unwind from program check handler. */
127 static __always_inline
unsigned long get_psw_addr(void)
129 unsigned long psw_addr
;
132 "basr %[psw_addr],0\n"
133 : [psw_addr
] "=d" (psw_addr
));
137 #ifdef CONFIG_KPROBES
138 static int pgm_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
140 struct unwindme
*u
= unwindme
;
142 u
->ret
= test_unwind(NULL
, (u
->flags
& UWM_REGS
) ? regs
: NULL
,
143 (u
->flags
& UWM_SP
) ? u
->sp
: 0);
148 /* This function may or may not appear in the backtrace. */
149 static noinline
int unwindme_func4(struct unwindme
*u
)
151 if (!(u
->flags
& UWM_CALLER
))
152 u
->sp
= current_frame_address();
153 if (u
->flags
& UWM_THREAD
) {
154 complete(&u
->task_ready
);
155 wait_event(u
->task_wq
, kthread_should_park());
158 #ifdef CONFIG_KPROBES
159 } else if (u
->flags
& UWM_PGM
) {
164 memset(&kp
, 0, sizeof(kp
));
165 kp
.symbol_name
= "do_report_trap";
166 kp
.pre_handler
= pgm_pre_handler
;
167 ret
= register_kprobe(&kp
);
169 pr_err("register_kprobe failed %d\n", ret
);
174 * trigger specification exception
182 unregister_kprobe(&kp
);
189 memset(®s
, 0, sizeof(regs
));
190 regs
.psw
.addr
= get_psw_addr();
191 regs
.gprs
[15] = current_stack_pointer();
192 return test_unwind(NULL
,
193 (u
->flags
& UWM_REGS
) ? ®s
: NULL
,
194 (u
->flags
& UWM_SP
) ? u
->sp
: 0);
198 /* This function may or may not appear in the backtrace. */
199 static noinline
int unwindme_func3(struct unwindme
*u
)
201 u
->sp
= current_frame_address();
202 return unwindme_func4(u
);
205 /* This function must appear in the backtrace. */
206 static noinline
int unwindme_func2(struct unwindme
*u
)
211 if (u
->flags
& UWM_SWITCH_STACK
) {
212 local_irq_save(flags
);
213 local_mcck_disable();
214 rc
= CALL_ON_STACK(unwindme_func3
, S390_lowcore
.nodat_stack
, 1, u
);
216 local_irq_restore(flags
);
219 return unwindme_func3(u
);
223 /* This function must follow unwindme_func2 in the backtrace. */
224 static noinline
int unwindme_func1(void *u
)
226 return unwindme_func2((struct unwindme
*)u
);
229 static void unwindme_timer_fn(struct timer_list
*unused
)
231 struct unwindme
*u
= READ_ONCE(unwindme
);
236 u
->ret
= unwindme_func1(u
);
237 complete(&u
->task_ready
);
241 static struct timer_list unwind_timer
;
243 static int test_unwind_irq(struct unwindme
*u
)
246 init_completion(&u
->task_ready
);
247 timer_setup(&unwind_timer
, unwindme_timer_fn
, 0);
248 mod_timer(&unwind_timer
, jiffies
+ 1);
249 wait_for_completion(&u
->task_ready
);
253 /* Spawns a task and passes it to test_unwind(). */
254 static int test_unwind_task(struct unwindme
*u
)
256 struct task_struct
*task
;
259 /* Initialize thread-related fields. */
260 init_completion(&u
->task_ready
);
261 init_waitqueue_head(&u
->task_wq
);
264 * Start the task and wait until it reaches unwindme_func4() and sleeps
265 * in (task_ready, unwind_done] range.
267 task
= kthread_run(unwindme_func1
, u
, "%s", __func__
);
269 pr_err("kthread_run() failed\n");
270 return PTR_ERR(task
);
273 * Make sure task reaches unwindme_func4 before parking it,
274 * we might park it before kthread function has been executed otherwise
276 wait_for_completion(&u
->task_ready
);
279 ret
= test_unwind(task
, NULL
, (u
->flags
& UWM_SP
) ? u
->sp
: 0);
284 static int test_unwind_flags(int flags
)
289 if (u
.flags
& UWM_THREAD
)
290 return test_unwind_task(&u
);
291 else if (u
.flags
& UWM_IRQ
)
292 return test_unwind_irq(&u
);
294 return unwindme_func1(&u
);
297 static int test_unwind_init(void)
301 #define TEST(flags) \
303 pr_info("[ RUN ] " #flags "\n"); \
304 if (!test_unwind_flags((flags))) { \
305 pr_info("[ OK ] " #flags "\n"); \
307 pr_err("[ FAILED ] " #flags "\n"); \
315 TEST(UWM_SWITCH_STACK
);
316 TEST(UWM_SP
| UWM_REGS
);
317 TEST(UWM_CALLER
| UWM_SP
);
318 TEST(UWM_CALLER
| UWM_SP
| UWM_REGS
);
319 TEST(UWM_CALLER
| UWM_SP
| UWM_REGS
| UWM_SWITCH_STACK
);
321 TEST(UWM_THREAD
| UWM_SP
);
322 TEST(UWM_THREAD
| UWM_CALLER
| UWM_SP
);
324 TEST(UWM_IRQ
| UWM_SWITCH_STACK
);
325 TEST(UWM_IRQ
| UWM_SP
);
326 TEST(UWM_IRQ
| UWM_REGS
);
327 TEST(UWM_IRQ
| UWM_SP
| UWM_REGS
);
328 TEST(UWM_IRQ
| UWM_CALLER
| UWM_SP
);
329 TEST(UWM_IRQ
| UWM_CALLER
| UWM_SP
| UWM_REGS
);
330 TEST(UWM_IRQ
| UWM_CALLER
| UWM_SP
| UWM_REGS
| UWM_SWITCH_STACK
);
331 #ifdef CONFIG_KPROBES
333 TEST(UWM_PGM
| UWM_SP
);
334 TEST(UWM_PGM
| UWM_REGS
);
335 TEST(UWM_PGM
| UWM_SP
| UWM_REGS
);
342 static void test_unwind_exit(void)
346 module_init(test_unwind_init
);
347 module_exit(test_unwind_exit
);
348 MODULE_LICENSE("GPL");