Skip to contentSkip to conversation

x86: Bump per-CPU stack/data to 12KiB

PATCH Applied Sean Christopherson 1 participant 5 messages Feb 15, 2025 – Feb 24, 2025 KVM

lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

cover x86: Bump per-CPU stack/data to 12KiB

Make the stacks and per-CPU data page aligned, and bump the total size to 12KiB to reduce the probability having to debug stack overflow insanity in the future.

Sean Christopherson (3):
x86: Make per-CPU stacks page-aligned
x86: Add a macro for the size of the per-CPU stack/data area
x86: Increase per-CPU stack/data area to 12KiB

 lib/x86/apic-defs.h |  7 ++++---
 lib/x86/setup.c     |  2 +-
 lib/x86/smp.c       |  2 +-
 x86/cstart.S        | 10 +++++-----
 x86/cstart64.S      |  8 ++++----
 x86/trampolines.S   |  7 +++++--
 6 files changed, 20 insertions(+), 16 deletions(-)

base-commit: f77fb696cfd0e4a5562cdca189be557946bf522f

Signature · 1 line
2.48.1.601.g30ceb7b040-goog
author reviewer
#1 lore raw

PATCH 1/3 x86: Make per-CPU stacks page-aligned

Align the per-CPU stacks, and by extension, the per-CPU data area to page boundaries so that when things go sideways, it's at least somewhat obvious that a test overran its stack. E.g. as is, stacks often start at the *bottom* of a page, and so it looks like they're always broken because they immediately split a page.

---
 x86/cstart.S   | 3 +--
 x86/cstart64.S | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)
x86/cstart.S +1 -2
index ceee58f9..df3458fe 100644
@@ -8,9 +8,8 @@ ipi_vector = 0x20
 max_cpus = MAX_TEST_CPUS
 
 .bss
-
+.align 4096
 	. = . + 4096 * max_cpus
-	.align 16
 stacktop:
 
 .data
x86/cstart64.S +1 -2
index 4dff1102..bafb2017 100644
@@ -6,9 +6,8 @@ ipi_vector = 0x20
 max_cpus = MAX_TEST_CPUS
 
 .bss
-
+.align 4096
 	. = . + 4096 * max_cpus
-	.align 16
 stacktop:
 
 .data
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
author reviewer
#2 lore raw

PATCH 2/3 x86: Add a macro for the size of the per-CPU stack/data area

In reply to Sean Christopherson

Add a macro to define the size of the per-CPU stack/data area so that it's somewhat possible to make sense of the madness.

---
 lib/x86/apic-defs.h | 6 +++---
 lib/x86/setup.c     | 2 +-
 lib/x86/smp.c       | 2 +-
 x86/cstart.S        | 7 ++++---
 x86/cstart64.S      | 5 +++--
 x86/trampolines.S   | 7 +++++--
 6 files changed, 17 insertions(+), 12 deletions(-)
lib/x86/apic-defs.h +3 -3
index 4db73da2..fde1db38 100644
@@ -2,11 +2,11 @@
 #define _X86_APIC_DEFS_H_
 
 /*
- * Abuse this header file to hold the number of max-cpus, making it available
- * both in C and ASM
+ * Abuse this header file to hold the number of max-cpus and the size of the
+ * per-CPU stack/data area, making them available both in C and ASM.
  */
-
 #define MAX_TEST_CPUS (255)
+#define PER_CPU_SIZE  (4096)
 
 /*
  * Constants for various Intel APICs. (local APIC, IOAPIC, etc.)
lib/x86/setup.c +1 -1
index d509a248..b4b7fec0 100644
@@ -146,7 +146,7 @@ unsigned long setup_tss(u8 *stacktop)
 	set_gdt_entry(TSS_MAIN + id * 8,
 		      (unsigned long)tss_entry, 0xffff, 0x89, 0);
 	set_gdt_entry(TSS_MAIN + MAX_TEST_CPUS * 8 + id * 8,
-		      (unsigned long)stacktop - 4096, 0xfffff, 0x93, 0xc0);
+		      (unsigned long)stacktop - PER_CPU_SIZE, 0xfffff, 0x93, 0xc0);
 
 	return TSS_MAIN + id * 8;
 }
lib/x86/smp.c +1 -1
index e297016c..9706072a 100644
@@ -273,7 +273,7 @@ void bringup_aps(void)
 	setup_rm_gdt();
 
 #ifdef CONFIG_EFI
-	smp_stacktop = ((u64) (&stacktop)) - PAGE_SIZE;
+	smp_stacktop = ((u64) (&stacktop)) - PER_CPU_SIZE;
 #endif
 
 	/* INIT */
x86/cstart.S +4 -3
index df3458fe..2e396e52 100644
@@ -5,11 +5,12 @@
 
 ipi_vector = 0x20
 
+per_cpu_size = PER_CPU_SIZE
 max_cpus = MAX_TEST_CPUS
 
 .bss
 .align 4096
-	. = . + 4096 * max_cpus
+	. = . + PER_CPU_SIZE * max_cpus
 stacktop:
 
 .data
@@ -81,7 +82,7 @@ prepare_32:
 	mov %eax, %cr0
 	ret
 
-smp_stacktop:	.long stacktop - 4096
+smp_stacktop:	.long stacktop - per_cpu_size
 
 save_id:
 	movl $(APIC_DEFAULT_PHYS_BASE + APIC_ID), %eax
@@ -92,7 +93,7 @@ save_id:
 
 ap_start32:
 	setup_segments
-	mov $-4096, %esp
+	mov $-per_cpu_size, %esp
 	lock xaddl %esp, smp_stacktop
 	setup_tr_and_percpu
 	call prepare_32
x86/cstart64.S +3 -2
index bafb2017..a9db65ce 100644
@@ -3,11 +3,12 @@
 
 ipi_vector = 0x20
 
+per_cpu_size = PER_CPU_SIZE
 max_cpus = MAX_TEST_CPUS
 
 .bss
 .align 4096
-	. = . + 4096 * max_cpus
+	. = . + PER_CPU_SIZE * max_cpus
 stacktop:
 
 .data
@@ -91,7 +92,7 @@ switch_to_5level:
 	call enter_long_mode
 	jmpl $8, $lvl5
 
-smp_stacktop:	.long stacktop - 4096
+smp_stacktop:	.long stacktop - per_cpu_size
 
 .align 16
 
x86/trampolines.S +5 -2
index 6a3df9c1..02713157 100644
@@ -2,6 +2,9 @@
  * Common bootstrapping code to transition from 16-bit to 32-bit code, and to
  * transition from 32-bit to 64-bit code (x86-64 only)
  */
+#include "apic-defs.h"
+
+per_cpu_size = PER_CPU_SIZE
 
  /* EFI provides it's own SIPI sequence to handle relocation. */
 #ifndef CONFIG_EFI
@@ -56,7 +59,7 @@ rm_trampoline_end:
 MSR_GS_BASE = 0xc0000101
 
 .macro setup_percpu_area
-	lea -4096(%esp), %eax
+	lea -per_cpu_size(%esp), %eax
 	mov $0, %edx
 	mov $MSR_GS_BASE, %ecx
 	wrmsr
@@ -116,7 +119,7 @@ ap_start32:
 	setup_segments
 
 	load_absolute_addr $smp_stacktop, %edx
-	mov $-4096, %esp
+	mov $-per_cpu_size, %esp
 	lock xaddl %esp, (%edx)
 
 	setup_percpu_area
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
author reviewer
#3 lore raw

PATCH 3/3 x86: Increase per-CPU stack/data area to 12KiB

In reply to Sean Christopherson

Increase the size of the per-CPU stack/data area from one page to three, i.e. from 4KiB to 12KiB. KVM-Unit-Tests currently places the per-CPU data at the bottom of the stack page, i.e. the stack "page" is actually a page minus the size of the per-CPU area. And of course there's no guard page or buffer in between the two, and so overflowing the stack clobbers per-CPU data and sends tests into the weeds in weird ways.

Punt on less awful infrastructure, and settle for fixing the most egregious problem of tests having less than 4KiB of stack to work with.

---
 lib/x86/apic-defs.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/x86/apic-defs.h +3 -2
index fde1db38..5a1dff3a 100644
@@ -3,10 +3,11 @@
 
 /*
  * Abuse this header file to hold the number of max-cpus and the size of the
- * per-CPU stack/data area, making them available both in C and ASM.
+ * per-CPU stack/data area, making them available both in C and ASM.  One page
+ * for per-CPU, and two pages for the stack (plus some buffer in-between).
  */
 #define MAX_TEST_CPUS (255)
-#define PER_CPU_SIZE  (4096)
+#define PER_CPU_SIZE  (3 * 4096)
 
 /*
  * Constants for various Intel APICs. (local APIC, IOAPIC, etc.)
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
author reviewer Applied
#4 lore raw
In reply to Sean Christopherson
Quoted 10 lines · Sean Christopherson

Make the stacks and per-CPU data page aligned, and bump the total size to 12KiB to reduce the probability having to debug stack overflow insanity in the future.

Sean Christopherson (3):
x86: Make per-CPU stacks page-aligned
x86: Add a macro for the size of the per-CPU stack/data area
x86: Increase per-CPU stack/data area to 12KiB

[...]

Applied to kvm-x86 next (and now pulled by Paolo), thanks!

[1/3] x86: Make per-CPU stacks page-aligned

      https://github.com/kvm-x86/kvm-unit-tests/commit/a95dd6beeeb4
[2/3] x86: Add a macro for the size of the per-CPU stack/data area
      https://github.com/kvm-x86/kvm-unit-tests/commit/2821b32d627d
[3/3] x86: Increase per-CPU stack/data area to 12KiB
      https://github.com/kvm-x86/kvm-unit-tests/commit/b94ace2edb58

--
https://github.com/kvm-x86/kvm-unit-tests/tree/next