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