Blame SOURCES/0001-arm64-Support-overflow-stack-panic.patch

52efa0
From 995db8ab88916b6397676b67be98c0a4f82cca49 Mon Sep 17 00:00:00 2001
52efa0
From: Hong YANG <hong.yang3@nio.com>
52efa0
Date: Mon, 15 Nov 2021 15:41:01 +0800
52efa0
Subject: [PATCH 1/3] arm64: Support overflow stack panic
52efa0
52efa0
Kernel commit <872d8327ce89> ("arm64: add VMAP_STACK overflow detection")
52efa0
has supported the overflow stack exception handling. Without the patch, the
52efa0
"bt" command will make crash generate a core dump because of segmentation
52efa0
fault. With the patch, the "bt" command can display the overflow stack.
52efa0
52efa0
Before:
52efa0
crash> bt
52efa0
PID: 3607   TASK: ffffffcbf9a4da00  CPU: 2   COMMAND: "sh"
52efa0
Segmentation fault (core dumped)
52efa0
52efa0
After:
52efa0
crash> bt
52efa0
PID: 3607   TASK: ffffffcbf9a4da00  CPU: 2   COMMAND: "sh"
52efa0
 #0 [ffffffccbfd85f50] __delay at ffffff8008ceded8
52efa0
...
52efa0
 #5 [ffffffccbfd85fd0] emergency_restart at ffffff80080d49fc
52efa0
 #6 [ffffffccbfd86140] panic at ffffff80080af4c0
52efa0
 #7 [ffffffccbfd86150] nmi_panic at ffffff80080af150
52efa0
 #8 [ffffffccbfd86190] handle_bad_stack at ffffff800808b0b8
52efa0
 #9 [ffffffccbfd862d0] __bad_stack at ffffff800808285c
52efa0
     PC: ffffff8008082e80  [el1_sync]
52efa0
     LR: ffffff8000d6c214  [stack_overflow_demo+84]
52efa0
     SP: ffffff1a79930070  PSTATE: 204003c5
52efa0
    X29: ffffff8011b03d00  X28: ffffffcbf9a4da00  X27: ffffff8008e02000
52efa0
    X26: 0000000000000040  X25: 0000000000000124  X24: ffffffcbf9a4da00
52efa0
    X23: 0000007daec2e288  X22: ffffffcbfe03b800  X21: 0000007daec2e288
52efa0
    X20: 0000000000000002  X19: 0000000000000002  X18: 0000000000000002
52efa0
    X17: 00000000000003e7  X16: 0000000000000000  X15: 0000000000000000
52efa0
    X14: ffffffcc17facb00  X13: ffffffccb4c25c00  X12: 0000000000000000
52efa0
    X11: ffffffcc17fad660  X10: 0000000000000af0   X9: 0000000000000000
52efa0
     X8: ffffff1a799334f0   X7: 0000000000000000   X6: 000000000000003f
52efa0
     X5: 0000000000000040   X4: 0000000000000010   X3: 00000065981d07f0
52efa0
     X2: 00000065981d07f0   X1: 0000000000000000   X0: ffffff1a799334f0
52efa0
52efa0
Signed-off-by: Hong YANG <hong.yang3@nio.com>
52efa0
---
52efa0
 arm64.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++------
52efa0
 defs.h  |   6 ++
52efa0
 2 files changed, 159 insertions(+), 16 deletions(-)
52efa0
52efa0
diff --git a/arm64.c b/arm64.c
52efa0
index 94681d1a37db..23c3d75d85aa 100644
52efa0
--- a/arm64.c
52efa0
+++ b/arm64.c
52efa0
@@ -45,6 +45,7 @@ static int arm64_vtop_3level_4k(ulong, ulong, physaddr_t *, int);
52efa0
 static int arm64_vtop_4level_4k(ulong, ulong, physaddr_t *, int);
52efa0
 static ulong arm64_get_task_pgd(ulong);
52efa0
 static void arm64_irq_stack_init(void);
52efa0
+static void arm64_overflow_stack_init(void);
52efa0
 static void arm64_stackframe_init(void);
52efa0
 static int arm64_eframe_search(struct bt_info *);
52efa0
 static int arm64_is_kernel_exception_frame(struct bt_info *, ulong);
52efa0
@@ -63,6 +64,7 @@ static int arm64_get_dumpfile_stackframe(struct bt_info *, struct arm64_stackfra
52efa0
 static int arm64_in_kdump_text(struct bt_info *, struct arm64_stackframe *);
52efa0
 static int arm64_in_kdump_text_on_irq_stack(struct bt_info *);
52efa0
 static int arm64_switch_stack(struct bt_info *, struct arm64_stackframe *, FILE *);
52efa0
+static int arm64_switch_stack_from_overflow(struct bt_info *, struct arm64_stackframe *, FILE *);
52efa0
 static int arm64_get_stackframe(struct bt_info *, struct arm64_stackframe *);
52efa0
 static void arm64_get_stack_frame(struct bt_info *, ulong *, ulong *);
52efa0
 static void arm64_gen_hidden_frame(struct bt_info *bt, ulong, struct arm64_stackframe *);
52efa0
@@ -78,8 +80,11 @@ static int arm64_get_smp_cpus(void);
52efa0
 static void arm64_clear_machdep_cache(void);
52efa0
 static int arm64_on_process_stack(struct bt_info *, ulong);
52efa0
 static int arm64_in_alternate_stack(int, ulong);
52efa0
+static int arm64_in_alternate_stackv(int cpu, ulong stkptr, ulong *stacks, ulong stack_size);
52efa0
 static int arm64_on_irq_stack(int, ulong);
52efa0
+static int arm64_on_overflow_stack(int, ulong);
52efa0
 static void arm64_set_irq_stack(struct bt_info *);
52efa0
+static void arm64_set_overflow_stack(struct bt_info *);
52efa0
 static void arm64_set_process_stack(struct bt_info *);
52efa0
 static int arm64_get_kvaddr_ranges(struct vaddr_range *);
52efa0
 static void arm64_get_crash_notes(void);
52efa0
@@ -463,6 +468,7 @@ arm64_init(int when)
52efa0
 			machdep->hz = 100;
52efa0
 
52efa0
 		arm64_irq_stack_init();
52efa0
+		arm64_overflow_stack_init();
52efa0
 		arm64_stackframe_init();
52efa0
 		break;
52efa0
 
52efa0
@@ -1715,6 +1721,49 @@ arm64_irq_stack_init(void)
52efa0
 	} 
52efa0
 }
52efa0
 
52efa0
+/*
52efa0
+ *  Gather Overflow stack values.
52efa0
+ *
52efa0
+ *  Overflow stack supported since 4.14, in commit 872d8327c
52efa0
+ */
52efa0
+static void
52efa0
+arm64_overflow_stack_init(void)
52efa0
+{
52efa0
+	int i;
52efa0
+	struct syment *sp;
52efa0
+	struct gnu_request request, *req;
52efa0
+	struct machine_specific *ms = machdep->machspec;
52efa0
+	req = &request;
52efa0
+
52efa0
+	if (symbol_exists("overflow_stack") &&
52efa0
+	    (sp = per_cpu_symbol_search("overflow_stack")) &&
52efa0
+	    get_symbol_type("overflow_stack", NULL, req)) {
52efa0
+		if (CRASHDEBUG(1)) {
52efa0
+			fprintf(fp, "overflow_stack: \n");
52efa0
+			fprintf(fp, "  type: %x, %s\n",
52efa0
+				(int)req->typecode,
52efa0
+				(req->typecode == TYPE_CODE_ARRAY) ?
52efa0
+						"TYPE_CODE_ARRAY" : "other");
52efa0
+			fprintf(fp, "  target_typecode: %x, %s\n",
52efa0
+				(int)req->target_typecode,
52efa0
+				req->target_typecode == TYPE_CODE_INT ?
52efa0
+						"TYPE_CODE_INT" : "other");
52efa0
+			fprintf(fp, "  target_length: %ld\n",
52efa0
+						req->target_length);
52efa0
+			fprintf(fp, "  length: %ld\n", req->length);
52efa0
+		}
52efa0
+
52efa0
+		if (!(ms->overflow_stacks = (ulong *)malloc((size_t)(kt->cpus * sizeof(ulong)))))
52efa0
+			error(FATAL, "cannot malloc overflow_stack addresses\n");
52efa0
+
52efa0
+		ms->overflow_stack_size = ARM64_OVERFLOW_STACK_SIZE;
52efa0
+		machdep->flags |= OVERFLOW_STACKS;
52efa0
+
52efa0
+		for (i = 0; i < kt->cpus; i++)
52efa0
+			ms->overflow_stacks[i] = kt->__per_cpu_offset[i] + sp->value;
52efa0
+	}
52efa0
+}
52efa0
+
52efa0
 /*
52efa0
  *  Gather and verify all of the backtrace requirements.
52efa0
  */
52efa0
@@ -1960,6 +2009,7 @@ static char *arm64_exception_functions[] = {
52efa0
         "do_mem_abort",
52efa0
         "do_el0_irq_bp_hardening",
52efa0
         "do_sp_pc_abort",
52efa0
+        "handle_bad_stack",
52efa0
         NULL
52efa0
 };
52efa0
 
52efa0
@@ -1978,7 +2028,10 @@ arm64_in_exception_text(ulong ptr)
52efa0
 		if ((ptr >= ms->__exception_text_start) &&
52efa0
 		    (ptr < ms->__exception_text_end))
52efa0
 			return TRUE;
52efa0
-	} else if ((name = closest_symbol(ptr))) {  /* Linux 5.5 and later */
52efa0
+	}
52efa0
+
52efa0
+	name = closest_symbol(ptr);
52efa0
+	if (name != NULL) { /* Linux 5.5 and later */
52efa0
 		for (func = &arm64_exception_functions[0]; *func; func++) {
52efa0
 			if (STREQ(name, *func))
52efa0
 				return TRUE;
52efa0
@@ -2252,15 +2305,14 @@ arm64_unwind_frame(struct bt_info *bt, struct arm64_stackframe *frame)
52efa0
 	if ((frame->fp == 0) && (frame->pc == 0))
52efa0
 		return FALSE;
52efa0
 
52efa0
-	if (!(machdep->flags & IRQ_STACKS))
52efa0
-		return TRUE;
52efa0
-
52efa0
-	if (!(machdep->flags & IRQ_STACKS))
52efa0
+	if (!(machdep->flags & (IRQ_STACKS | OVERFLOW_STACKS)))
52efa0
 		return TRUE;
52efa0
 
52efa0
 	if (machdep->flags & UNW_4_14) {
52efa0
-		if ((bt->flags & BT_IRQSTACK) &&
52efa0
-		    !arm64_on_irq_stack(bt->tc->processor, frame->fp)) {
52efa0
+		if (((bt->flags & BT_IRQSTACK) &&
52efa0
+		     !arm64_on_irq_stack(bt->tc->processor, frame->fp)) ||
52efa0
+		    ((bt->flags & BT_OVERFLOW_STACK) &&
52efa0
+		     !arm64_on_overflow_stack(bt->tc->processor, frame->fp))) {
52efa0
 			if (arm64_on_process_stack(bt, frame->fp)) {
52efa0
 				arm64_set_process_stack(bt);
52efa0
 
52efa0
@@ -2677,6 +2729,9 @@ arm64_back_trace_cmd(struct bt_info *bt)
52efa0
 		if (arm64_on_irq_stack(bt->tc->processor, bt->frameptr)) {
52efa0
 			arm64_set_irq_stack(bt);
52efa0
 			bt->flags |= BT_IRQSTACK;
52efa0
+		} else if (arm64_on_overflow_stack(bt->tc->processor, bt->frameptr)) {
52efa0
+			arm64_set_overflow_stack(bt);
52efa0
+			bt->flags |= BT_OVERFLOW_STACK;
52efa0
 		}
52efa0
 		stackframe.sp = bt->stkptr;
52efa0
 		stackframe.pc = bt->instptr;
52efa0
@@ -2731,7 +2786,9 @@ arm64_back_trace_cmd(struct bt_info *bt)
52efa0
 			break;
52efa0
 
52efa0
 		if (arm64_in_exception_text(bt->instptr) && INSTACK(stackframe.fp, bt)) {
52efa0
-			if (!(bt->flags & BT_IRQSTACK) ||
52efa0
+			if (bt->flags & BT_OVERFLOW_STACK) {
52efa0
+				exception_frame = stackframe.fp - KERN_EFRAME_OFFSET;
52efa0
+			} else if (!(bt->flags & BT_IRQSTACK) ||
52efa0
 			    ((stackframe.sp + SIZE(pt_regs)) < bt->stacktop)) {
52efa0
 				if (arm64_is_kernel_exception_frame(bt, stackframe.fp - KERN_EFRAME_OFFSET))
52efa0
 					exception_frame = stackframe.fp - KERN_EFRAME_OFFSET;
52efa0
@@ -2745,6 +2802,12 @@ arm64_back_trace_cmd(struct bt_info *bt)
52efa0
 				break;
52efa0
 		}
52efa0
 
52efa0
+		if ((bt->flags & BT_OVERFLOW_STACK) &&
52efa0
+		    !arm64_on_overflow_stack(bt->tc->processor, stackframe.fp)) {
52efa0
+			bt->flags &= ~BT_OVERFLOW_STACK;
52efa0
+			if (arm64_switch_stack_from_overflow(bt, &stackframe, ofp) == USER_MODE)
52efa0
+				break;
52efa0
+		}
52efa0
 
52efa0
 		level++;
52efa0
 	}
52efa0
@@ -3131,6 +3194,43 @@ arm64_switch_stack(struct bt_info *bt, struct arm64_stackframe *frame, FILE *ofp
52efa0
 	return KERNEL_MODE;
52efa0
 }
52efa0
 
52efa0
+static int
52efa0
+arm64_switch_stack_from_overflow(struct bt_info *bt, struct arm64_stackframe *frame, FILE *ofp)
52efa0
+{
52efa0
+	int i;
52efa0
+	ulong stacktop, words, addr;
52efa0
+	ulong *stackbuf;
52efa0
+	char buf[BUFSIZE];
52efa0
+	struct machine_specific *ms = machdep->machspec;
52efa0
+
52efa0
+	if (bt->flags & BT_FULL) {
52efa0
+		stacktop = ms->overflow_stacks[bt->tc->processor] + ms->overflow_stack_size;
52efa0
+		words = (stacktop - bt->bptr) / sizeof(ulong);
52efa0
+		stackbuf = (ulong *)GETBUF(words * sizeof(ulong));
52efa0
+		readmem(bt->bptr, KVADDR, stackbuf, words * sizeof(long),
52efa0
+			"top of overflow stack", FAULT_ON_ERROR);
52efa0
+
52efa0
+		addr = bt->bptr;
52efa0
+		for (i = 0; i < words; i++) {
52efa0
+			if (!(i & 1))
52efa0
+				fprintf(ofp, "%s    %lx: ", i ? "\n" : "", addr);
52efa0
+			fprintf(ofp, "%s ", format_stack_entry(bt, buf, stackbuf[i], 0));
52efa0
+			addr += sizeof(ulong);
52efa0
+		}
52efa0
+		fprintf(ofp, "\n");
52efa0
+		FREEBUF(stackbuf);
52efa0
+	}
52efa0
+	fprintf(ofp, "--- <Overflow stack> ---\n");
52efa0
+
52efa0
+	if (frame->fp == 0)
52efa0
+		return USER_MODE;
52efa0
+
52efa0
+	if (!(machdep->flags & UNW_4_14))
52efa0
+		arm64_print_exception_frame(bt, frame->sp, KERNEL_MODE, ofp);
52efa0
+
52efa0
+	return KERNEL_MODE;
52efa0
+}
52efa0
+
52efa0
 static int
52efa0
 arm64_get_dumpfile_stackframe(struct bt_info *bt, struct arm64_stackframe *frame)
52efa0
 {
52efa0
@@ -3682,6 +3782,16 @@ arm64_display_machine_stats(void)
52efa0
 				machdep->machspec->irq_stacks[i]);
52efa0
 		}
52efa0
 	}
52efa0
+	if (machdep->machspec->overflow_stack_size) {
52efa0
+		fprintf(fp, "OVERFLOW STACK SIZE: %ld\n",
52efa0
+			machdep->machspec->overflow_stack_size);
52efa0
+		fprintf(fp, "    OVERFLOW STACKS:\n");
52efa0
+		for (i = 0; i < kt->cpus; i++) {
52efa0
+			pad = (i < 10) ? 3 : (i < 100) ? 2 : (i < 1000) ? 1 : 0;
52efa0
+			fprintf(fp, "%s           CPU %d: %lx\n", space(pad), i,
52efa0
+				machdep->machspec->overflow_stacks[i]);
52efa0
+		}
52efa0
+	}
52efa0
 }
52efa0
 
52efa0
 static int
52efa0
@@ -3875,24 +3985,41 @@ arm64_on_process_stack(struct bt_info *bt, ulong stkptr)
52efa0
 }
52efa0
 
52efa0
 static int
52efa0
-arm64_on_irq_stack(int cpu, ulong stkptr)
52efa0
+arm64_in_alternate_stackv(int cpu, ulong stkptr, ulong *stacks, ulong stack_size)
52efa0
 {
52efa0
-	return arm64_in_alternate_stack(cpu, stkptr);
52efa0
+	if ((cpu >= kt->cpus) || (stacks == NULL) || !stack_size)
52efa0
+		return FALSE;
52efa0
+
52efa0
+	if ((stkptr >= stacks[cpu]) &&
52efa0
+	    (stkptr < (stacks[cpu] + stack_size)))
52efa0
+		return TRUE;
52efa0
+
52efa0
+	return FALSE;
52efa0
 }
52efa0
 
52efa0
 static int
52efa0
 arm64_in_alternate_stack(int cpu, ulong stkptr)
52efa0
+{
52efa0
+	return (arm64_on_irq_stack(cpu, stkptr) ||
52efa0
+		arm64_on_overflow_stack(cpu, stkptr));
52efa0
+}
52efa0
+
52efa0
+static int
52efa0
+arm64_on_irq_stack(int cpu, ulong stkptr)
52efa0
 {
52efa0
 	struct machine_specific *ms = machdep->machspec;
52efa0
 
52efa0
-	if (!ms->irq_stack_size || (cpu >= kt->cpus))
52efa0
-		return FALSE;
52efa0
+	return arm64_in_alternate_stackv(cpu, stkptr,
52efa0
+			ms->irq_stacks, ms->irq_stack_size);
52efa0
+}
52efa0
 
52efa0
-	if ((stkptr >= ms->irq_stacks[cpu]) &&
52efa0
-	    (stkptr < (ms->irq_stacks[cpu] + ms->irq_stack_size)))
52efa0
-		return TRUE;
52efa0
+static int
52efa0
+arm64_on_overflow_stack(int cpu, ulong stkptr)
52efa0
+{
52efa0
+	struct machine_specific *ms = machdep->machspec;
52efa0
 
52efa0
-	return FALSE;
52efa0
+	return arm64_in_alternate_stackv(cpu, stkptr,
52efa0
+			ms->overflow_stacks, ms->overflow_stack_size);
52efa0
 }
52efa0
 
52efa0
 static void
52efa0
@@ -3905,6 +4032,16 @@ arm64_set_irq_stack(struct bt_info *bt)
52efa0
 	alter_stackbuf(bt);
52efa0
 }
52efa0
 
52efa0
+static void
52efa0
+arm64_set_overflow_stack(struct bt_info *bt)
52efa0
+{
52efa0
+	struct machine_specific *ms = machdep->machspec;
52efa0
+
52efa0
+	bt->stackbase = ms->overflow_stacks[bt->tc->processor];
52efa0
+	bt->stacktop = bt->stackbase + ms->overflow_stack_size;
52efa0
+	alter_stackbuf(bt);
52efa0
+}
52efa0
+
52efa0
 static void
52efa0
 arm64_set_process_stack(struct bt_info *bt)
52efa0
 {
52efa0
diff --git a/defs.h b/defs.h
52efa0
index a2f30853a4b1..7e2a16e34a59 100644
52efa0
--- a/defs.h
52efa0
+++ b/defs.h
52efa0
@@ -3218,6 +3218,7 @@ typedef signed int s32;
52efa0
 #define UNW_4_14      (0x200)
52efa0
 #define FLIPPED_VM    (0x400)
52efa0
 #define HAS_PHYSVIRT_OFFSET (0x800)
52efa0
+#define OVERFLOW_STACKS     (0x1000)
52efa0
 
52efa0
 /*
52efa0
  * Get kimage_voffset from /dev/crash
52efa0
@@ -3260,6 +3261,7 @@ typedef signed int s32;
52efa0
 
52efa0
 #define ARM64_STACK_SIZE   (16384)
52efa0
 #define ARM64_IRQ_STACK_SIZE   ARM64_STACK_SIZE
52efa0
+#define ARM64_OVERFLOW_STACK_SIZE   (4096)
52efa0
 
52efa0
 #define _SECTION_SIZE_BITS           30
52efa0
 #define _SECTION_SIZE_BITS_5_12      27
52efa0
@@ -3332,6 +3334,9 @@ struct machine_specific {
52efa0
 	char  *irq_stackbuf;
52efa0
 	ulong __irqentry_text_start;
52efa0
 	ulong __irqentry_text_end;
52efa0
+	ulong overflow_stack_size;
52efa0
+	ulong *overflow_stacks;
52efa0
+	char  *overflow_stackbuf;
52efa0
 	/* for exception vector code */
52efa0
 	ulong exp_entry1_start;
52efa0
 	ulong exp_entry1_end;
52efa0
@@ -5770,6 +5775,7 @@ ulong cpu_map_addr(const char *type);
52efa0
 #define BT_CPUMASK        (0x1000000000000ULL)
52efa0
 #define BT_SHOW_ALL_REGS  (0x2000000000000ULL)
52efa0
 #define BT_REGS_NOT_FOUND (0x4000000000000ULL)
52efa0
+#define BT_OVERFLOW_STACK (0x8000000000000ULL)
52efa0
 #define BT_SYMBOL_OFFSET   (BT_SYMBOLIC_ARGS)
52efa0
 
52efa0
 #define BT_REF_HEXVAL         (0x1)
52efa0
-- 
52efa0
2.30.2
52efa0