git: 6ef5bf8c21aa - stable/12 - arm64, riscv: size boot stacks appropriately

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Sun, 18 Sep 2022 06:27:17 UTC
The branch stable/12 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=6ef5bf8c21aa06b451422c1ba94aa8c8ae186c56

commit 6ef5bf8c21aa06b451422c1ba94aa8c8ae186c56
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2022-09-07 02:11:30 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2022-09-17 19:32:46 +0000

    arm64, riscv: size boot stacks appropriately
    
    In 8db2e8fd16c4 ("Remove the secondary_stacks array in arm64 [...]"),
    bootstacks was setup to be allocated dynamically.  While this is
    generally how x86 does it, it inadvertently shrunk each boot stack from
    KSTACK_PAGES pages to a single page.
    
    Resize these back up to the expected size using the kstack_pages
    tunable, as we'll need larger stacks with upcoming sanitizer work.
    
    Reviewed by:    andrew, imp, markj
    Fixes:  8db2e8fd16c4 ("Remove the secondary_stacks array [...]")
    Sponsored by:   Juniper Networks, Inc.
    Sponsored by:   Klara, Inc.
    
    (cherry picked from commit bab32a8029c3f9339acbd786ffe8f27ad9cfd288)
---
 sys/arm64/arm64/mp_machdep.c | 12 ++++++++----
 sys/riscv/riscv/mp_machdep.c | 10 +++++++---
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/sys/arm64/arm64/mp_machdep.c b/sys/arm64/arm64/mp_machdep.c
index 8e57fc2eac5f..85a22b01ff81 100644
--- a/sys/arm64/arm64/mp_machdep.c
+++ b/sys/arm64/arm64/mp_machdep.c
@@ -76,6 +76,8 @@ __FBSDID("$FreeBSD$");
 
 #include "pic_if.h"
 
+#define	MP_BOOTSTACK_SIZE	(kstack_pages * PAGE_SIZE)
+
 #define	MP_QUIRK_CPULIST	0x01	/* The list of cpus may be wrong, */
 					/* don't panic if one fails to start */
 static uint32_t mp_quirks;
@@ -344,7 +346,8 @@ smp_after_idle_runnable(void *arg __unused)
 			pc = pcpu_find(cpu);
 			while ((void *)atomic_load_ptr(&pc->pc_curpcb) == NULL)
 				cpu_spinwait();
-			kmem_free((vm_offset_t)bootstacks[cpu], PAGE_SIZE);
+			kmem_free((vm_offset_t)bootstacks[cpu],
+			    MP_BOOTSTACK_SIZE);
 		}
 	}
 }
@@ -510,10 +513,11 @@ start_cpu(u_int id, uint64_t target_cpu)
 	dpcpu[cpuid - 1] = (void *)kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO);
 	dpcpu_init(dpcpu[cpuid - 1], cpuid);
 
-	bootstacks[cpuid] = (void *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
+	bootstacks[cpuid] = (void *)kmem_malloc(MP_BOOTSTACK_SIZE,
+	    M_WAITOK | M_ZERO);
 
 	naps = atomic_load_int(&aps_started);
-	bootstack = (char *)bootstacks[cpuid] + PAGE_SIZE;
+	bootstack = (char *)bootstacks[cpuid] + MP_BOOTSTACK_SIZE;
 
 	printf("Starting CPU %u (%lx)\n", cpuid, target_cpu);
 	pa = pmap_extract(kernel_pmap, (vm_offset_t)mpentry);
@@ -532,7 +536,7 @@ start_cpu(u_int id, uint64_t target_cpu)
 		pcpu_destroy(pcpup);
 		kmem_free((vm_offset_t)dpcpu[cpuid - 1], DPCPU_SIZE);
 		dpcpu[cpuid - 1] = NULL;
-		kmem_free((vm_offset_t)bootstacks[cpuid], PAGE_SIZE);
+		kmem_free((vm_offset_t)bootstacks[cpuid], MP_BOOTSTACK_SIZE);
 		bootstacks[cpuid] = NULL;
 		mp_ncpus--;
 
diff --git a/sys/riscv/riscv/mp_machdep.c b/sys/riscv/riscv/mp_machdep.c
index 2cc1e5f5ca61..97d604bb832d 100644
--- a/sys/riscv/riscv/mp_machdep.c
+++ b/sys/riscv/riscv/mp_machdep.c
@@ -69,6 +69,8 @@ __FBSDID("$FreeBSD$");
 #include <dev/ofw/ofw_cpu.h>
 #endif
 
+#define	MP_BOOTSTACK_SIZE	(kstack_pages * PAGE_SIZE)
+
 boolean_t ofw_cpu_reg(phandle_t node, u_int, cell_t *);
 
 uint32_t __riscv_boot_ap[MAXCPU];
@@ -307,7 +309,8 @@ smp_after_idle_runnable(void *arg __unused)
 			pc = pcpu_find(cpu);
 			while ((void *)atomic_load_ptr(&pc->pc_curpcb) == NULL)
 				cpu_spinwait();
-			kmem_free((vm_offset_t)bootstacks[cpu], PAGE_SIZE);
+			kmem_free((vm_offset_t)bootstacks[cpu],
+			    MP_BOOTSTACK_SIZE);
 		}
 	}
 }
@@ -472,10 +475,11 @@ cpu_init_fdt(u_int id, phandle_t node, u_int addr_size, pcell_t *reg)
 	dpcpu[cpuid - 1] = (void *)kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO);
 	dpcpu_init(dpcpu[cpuid - 1], cpuid);
 
-	bootstacks[cpuid] = (void *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
+	bootstacks[cpuid] = (void *)kmem_malloc(MP_BOOTSTACK_SIZE,
+	    M_WAITOK | M_ZERO);
 
 	naps = atomic_load_int(&aps_started);
-	bootstack = (char *)bootstacks[cpuid] + PAGE_SIZE;
+	bootstack = (char *)bootstacks[cpuid] + MP_BOOTSTACK_SIZE;
 
 	printf("Starting CPU %u (hart %lx)\n", cpuid, hart);
 	atomic_store_32(&__riscv_boot_ap[hart], 1);