^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/hardirq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/nmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) const char *stack_type_name(enum stack_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (type == STACK_TYPE_IRQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) return "IRQ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (type == STACK_TYPE_SOFTIRQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return "SOFTIRQ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (type == STACK_TYPE_ENTRY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return "ENTRY_TRAMPOLINE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (type == STACK_TYPE_EXCEPTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return "#DF";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static bool in_hardirq_stack(unsigned long *stack, struct stack_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned long *begin = (unsigned long *)this_cpu_read(hardirq_stack_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * This is a software stack, so 'end' can be a valid stack pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * It just means the stack is empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (stack < begin || stack > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) info->type = STACK_TYPE_IRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) info->begin = begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) info->end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * See irq_32.c -- the next stack pointer is stored at the beginning of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * the stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) info->next_sp = (unsigned long *)*begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static bool in_softirq_stack(unsigned long *stack, struct stack_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned long *begin = (unsigned long *)this_cpu_read(softirq_stack_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * This is a software stack, so 'end' can be a valid stack pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * It just means the stack is empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (stack < begin || stack > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) info->type = STACK_TYPE_SOFTIRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) info->begin = begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) info->end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * The next stack pointer is stored at the beginning of the stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * See irq_32.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) info->next_sp = (unsigned long *)*begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static bool in_doublefault_stack(unsigned long *stack, struct stack_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct cpu_entry_area *cea = get_cpu_entry_area(raw_smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct doublefault_stack *ss = &cea->doublefault_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) void *begin = ss->stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) void *end = begin + sizeof(ss->stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if ((void *)stack < begin || (void *)stack >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) info->type = STACK_TYPE_EXCEPTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) info->begin = begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) info->end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) info->next_sp = (unsigned long *)this_cpu_read(cpu_tss_rw.x86_tss.sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int get_stack_info(unsigned long *stack, struct task_struct *task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct stack_info *info, unsigned long *visit_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) task = task ? : current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (in_task_stack(stack, task, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) goto recursion_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (task != current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (in_entry_stack(stack, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto recursion_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (in_hardirq_stack(stack, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) goto recursion_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (in_softirq_stack(stack, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) goto recursion_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (in_doublefault_stack(stack, info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) goto recursion_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) recursion_check:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Make sure we don't iterate through any given stack more than once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * If it comes up a second time then there's something wrong going on:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * just break out and report an unknown stack type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (visit_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (*visit_mask & (1UL << info->type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) *visit_mask |= 1UL << info->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) unknown:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) info->type = STACK_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }