Skip to contentSkip to conversation

x86/cea: Clean up the exception stack names and declarations

PATCH Applied Sohil Mehta 4 participants 8 messages Nov 20 – Nov 23

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover x86/cea: Clean up the exception stack names and declarations

Background
----------
This is a simple cleanup of how the x86-64 exception stacks are named and reached. These patches started out as part [1][2] of the FRED KVM series. They were prerequisites for the KVM enabling because the original proposal [3] was to call __this_cpu_ist_top_va() directly from KVM. So the macro had to be converted to a function and then be exported.

However, KVM is now planning to use a FRED-specific helper [4] which is compiled into vmlinux so the macro directly works inside it. Therefore, FRED KVM no longer needs any of this. Though the cleanup itself is still valuable and makes the code clearer and the comments more accurate.

Motivation
----------
The exception stacks (DF, NMI, DB, MCE, VC) carry names too short for a header that almost the whole x86 build pulls in, and the stacks themselves are both declared and looked up by pasting those names into structure member names.

The first patch adds the ESTACK_ prefix to the names and the second patch indexes the stacks by the enum that callers already pass. This removes most of the pasting, and leaves the layout, the addresses and the guard pages exactly as they were.

There is no change in behavior expected.

Changes from the FRED KVM v10
-----------------------------
Patch 1:
- Minor commit message improvements
- Pick up Binbin's review tag

Patch 2:
- Drop the conversion of __this_cpu_ist_{bottom,top}_va() from macros to functions.
- Expand struct exception_stacks explicitly and drop ESTACKS_MEMBERS()
- Rewrite most of the commit message and drop the prior tags.

Links
-----
[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/
[4]: https://lore.kernel.org/lkml/[email protected]/

Sohil Mehta (1):
x86/cea: Index exception stacks by the ordering enum

Xin Li (Intel) (1):
x86/cea: Prefix exception stack names with ESTACK_

 arch/x86/coco/sev/noinstr.c           |  5 +--
 arch/x86/coco/sev/vc-handle.c         |  3 +-
 arch/x86/include/asm/cpu_entry_area.h | 52 +++++++++++----------------
 arch/x86/kernel/cpu/common.c          | 10 +++---
 arch/x86/kernel/dumpstack_64.c        | 18 +++++-----
 arch/x86/kernel/fred.c                |  6 ++--
 arch/x86/kernel/traps.c               |  2 +-
 arch/x86/mm/cpu_entry_area.c          | 14 ++++----
 arch/x86/mm/fault.c                   |  2 +-
 9 files changed, 52 insertions(+), 60 deletions(-)

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 x86/cea: Prefix exception stack names with ESTACK_

From: "Xin Li (Intel)" <[email protected]>

Add the ESTACK_ prefix to the exception stack names to improve clarity and readability. Without the prefix, names like DF, NMI, and DB are too brief and potentially ambiguous.

This renaming also prepares for reaching the stacks by their enum exception_stack_ordering value instead of by name.

---
v1:
- Say "exception stack" instead of "event stack" in the commit message.
- Pick up Binbin's Reviewed-by.

FRED KVM v10:
- https://lore.kernel.org/lkml/[email protected]/

---
 arch/x86/coco/sev/noinstr.c           |  5 +++--
 arch/x86/coco/sev/vc-handle.c         |  3 ++-
 arch/x86/include/asm/cpu_entry_area.h | 26 +++++++++++++-------------
 arch/x86/kernel/cpu/common.c          | 10 +++++-----
 arch/x86/kernel/dumpstack_64.c        | 14 +++++++-------
 arch/x86/kernel/fred.c                |  6 +++---
 arch/x86/kernel/traps.c               |  2 +-
 arch/x86/mm/cpu_entry_area.c          | 12 ++++++------
 arch/x86/mm/fault.c                   |  2 +-
 9 files changed, 41 insertions(+), 39 deletions(-)
arch/x86/coco/sev/noinstr.c +3 -2
index e1e03f12fc7b..8ff556d28780 100644
@@ -31,7 +31,8 @@ static __always_inline bool on_vc_stack(struct pt_regs *regs)
 	if (ip_within_syscall_gap(regs))
 		return false;
 
-	return ((sp >= __this_cpu_ist_bottom_va(VC)) && (sp < __this_cpu_ist_top_va(VC)));
+	return ((sp >= __this_cpu_ist_bottom_va(ESTACK_VC)) &&
+		(sp <  __this_cpu_ist_top_va(ESTACK_VC)));
 }
 
 /*
@@ -83,7 +84,7 @@ void noinstr __sev_es_ist_exit(void)
 	/* Read IST entry */
 	ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]);
 
-	if (WARN_ON(ist == __this_cpu_ist_top_va(VC)))
+	if (WARN_ON(ist == __this_cpu_ist_top_va(ESTACK_VC)))
 		return;
 
 	/* Read back old IST entry and write it to the TSS */
arch/x86/coco/sev/vc-handle.c +2 -1
index f25d302efceb..954df836ac65 100644
@@ -854,7 +854,8 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 
 static __always_inline bool is_vc2_stack(unsigned long sp)
 {
-	return (sp >= __this_cpu_ist_bottom_va(VC2) && sp < __this_cpu_ist_top_va(VC2));
+	return (sp >= __this_cpu_ist_bottom_va(ESTACK_VC2) &&
+		sp <  __this_cpu_ist_top_va(ESTACK_VC2));
 }
 
 static __always_inline bool vc_from_invalid_context(struct pt_regs *regs)
arch/x86/include/asm/cpu_entry_area.h +13 -13
index 462fc34f1317..d0f884c28178 100644
@@ -18,19 +18,19 @@
 
 /* Macro to enforce the same ordering and stack sizes */
 #define ESTACKS_MEMBERS(guardsize, optional_stack_size)		\
-	char	DF_stack_guard[guardsize];			\
-	char	DF_stack[EXCEPTION_STKSZ];			\
-	char	NMI_stack_guard[guardsize];			\
-	char	NMI_stack[EXCEPTION_STKSZ];			\
-	char	DB_stack_guard[guardsize];			\
-	char	DB_stack[EXCEPTION_STKSZ];			\
-	char	MCE_stack_guard[guardsize];			\
-	char	MCE_stack[EXCEPTION_STKSZ];			\
-	char	VC_stack_guard[guardsize];			\
-	char	VC_stack[optional_stack_size];			\
-	char	VC2_stack_guard[guardsize];			\
-	char	VC2_stack[optional_stack_size];			\
-	char	IST_top_guard[guardsize];			\
+	char	ESTACK_DF_stack_guard[guardsize];		\
+	char	ESTACK_DF_stack[EXCEPTION_STKSZ];		\
+	char	ESTACK_NMI_stack_guard[guardsize];		\
+	char	ESTACK_NMI_stack[EXCEPTION_STKSZ];		\
+	char	ESTACK_DB_stack_guard[guardsize];		\
+	char	ESTACK_DB_stack[EXCEPTION_STKSZ];		\
+	char	ESTACK_MCE_stack_guard[guardsize];		\
+	char	ESTACK_MCE_stack[EXCEPTION_STKSZ];		\
+	char	ESTACK_VC_stack_guard[guardsize];		\
+	char	ESTACK_VC_stack[optional_stack_size];		\
+	char	ESTACK_VC2_stack_guard[guardsize];		\
+	char	ESTACK_VC2_stack[optional_stack_size];		\
+	char	ESTACK_IST_top_guard[guardsize];		\
 
 /* The exception stacks' physical storage. No guard pages required */
 struct exception_stacks {
arch/x86/kernel/cpu/common.c +5 -5
index 9bd50c68cd7e..5c24f32c0558 100644
@@ -2379,12 +2379,12 @@ static inline void setup_getcpu(int cpu)
 static inline void tss_setup_ist(struct tss_struct *tss)
 {
 	/* Set up the per-CPU TSS IST stacks */
-	tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
-	tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
-	tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
-	tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
+	tss->x86_tss.ist[IST_INDEX_DF]	= __this_cpu_ist_top_va(ESTACK_DF);
+	tss->x86_tss.ist[IST_INDEX_NMI]	= __this_cpu_ist_top_va(ESTACK_NMI);
+	tss->x86_tss.ist[IST_INDEX_DB]	= __this_cpu_ist_top_va(ESTACK_DB);
+	tss->x86_tss.ist[IST_INDEX_MCE]	= __this_cpu_ist_top_va(ESTACK_MCE);
 	/* Only mapped when SEV-ES is active */
-	tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC);
+	tss->x86_tss.ist[IST_INDEX_VC]	= __this_cpu_ist_top_va(ESTACK_VC);
 }
 #else /* CONFIG_X86_64 */
 static inline void tss_setup_ist(struct tss_struct *tss) { }
arch/x86/kernel/dumpstack_64.c +7 -7
index 6c5defd6569a..40f51e278171 100644
@@ -73,7 +73,7 @@ struct estack_pages {
 	 PFN_DOWN(CEA_ESTACK_OFFS(st) + CEA_ESTACK_SIZE(st) - 1)] = {	\
 		.offs	= CEA_ESTACK_OFFS(st),				\
 		.size	= CEA_ESTACK_SIZE(st),				\
-		.type	= STACK_TYPE_EXCEPTION + ESTACK_ ##st, }
+		.type	= STACK_TYPE_EXCEPTION + st, }
 
 /*
  * Array of exception stack page descriptors. If the stack is larger than
@@ -83,12 +83,12 @@ struct estack_pages {
  */
 static const
 struct estack_pages estack_pages[CEA_ESTACK_PAGES] ____cacheline_aligned = {
-	EPAGERANGE(DF),
-	EPAGERANGE(NMI),
-	EPAGERANGE(DB),
-	EPAGERANGE(MCE),
-	EPAGERANGE(VC),
-	EPAGERANGE(VC2),
+	EPAGERANGE(ESTACK_DF),
+	EPAGERANGE(ESTACK_NMI),
+	EPAGERANGE(ESTACK_DB),
+	EPAGERANGE(ESTACK_MCE),
+	EPAGERANGE(ESTACK_VC),
+	EPAGERANGE(ESTACK_VC2),
 };
 
 static __always_inline bool in_exception_stack(unsigned long *stack, struct stack_info *info)
arch/x86/kernel/fred.c +3 -3
index 117aa06d25ca..1c452a389a70 100644
@@ -84,7 +84,7 @@ void cpu_init_fred_rsps(void)
 	       FRED_STKLVL(X86_TRAP_DF,  FRED_DF_STACK_LEVEL));
 
 	/* The FRED equivalents to IST stacks... */
-	wrmsrq(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB));
-	wrmsrq(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI));
-	wrmsrq(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF));
+	wrmsrq(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(ESTACK_DB));
+	wrmsrq(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(ESTACK_NMI));
+	wrmsrq(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(ESTACK_DF));
 }
arch/x86/kernel/traps.c +1 -1
index 30aa8369957e..8afd607a5873 100644
@@ -1075,7 +1075,7 @@ asmlinkage __visible noinstr struct pt_regs *vc_switch_off_ist(struct pt_regs *r
 
 	if (!get_stack_info_noinstr(stack, current, &info) || info.type == STACK_TYPE_ENTRY ||
 	    info.type > STACK_TYPE_EXCEPTION_LAST)
-		sp = __this_cpu_ist_top_va(VC2);
+		sp = __this_cpu_ist_top_va(ESTACK_VC2);
 
 sync:
 	/*
arch/x86/mm/cpu_entry_area.c +6 -6
index 575f863f3c75..9fa371af8abc 100644
@@ -151,15 +151,15 @@ static void __init percpu_setup_exception_stacks(unsigned int cpu)
 	 * by guard pages so each stack must be mapped separately. DB2 is
 	 * not mapped; it just exists to catch triple nesting of #DB.
 	 */
-	cea_map_stack(DF);
-	cea_map_stack(NMI);
-	cea_map_stack(DB);
-	cea_map_stack(MCE);
+	cea_map_stack(ESTACK_DF);
+	cea_map_stack(ESTACK_NMI);
+	cea_map_stack(ESTACK_DB);
+	cea_map_stack(ESTACK_MCE);
 
 	if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
 		if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT)) {
-			cea_map_stack(VC);
-			cea_map_stack(VC2);
+			cea_map_stack(ESTACK_VC);
+			cea_map_stack(ESTACK_VC2);
 		}
 	}
 }
arch/x86/mm/fault.c +1 -1
index aa88370ce739..37f972913928 100644
@@ -677,7 +677,7 @@ page_fault_oops(struct pt_regs *regs, unsigned long error_code,
 		 * and then double-fault, though, because we're likely to
 		 * break the console driver and lose most of the stack dump.
 		 */
-		call_on_stack(__this_cpu_ist_top_va(DF) - sizeof(void*),
+		call_on_stack(__this_cpu_ist_top_va(ESTACK_DF) - sizeof(void *),
 			      handle_stack_overflow,
 			      ASM_CALL_ARG3,
 			      , [arg1] "r" (regs), [arg2] "r" (address), [arg3] "r" (&info));
Signature · 1 line
2.43.0
merge bot Applied
#2 lore raw

[tip: x86/entry] x86/cea: Prefix exception stack names with ESTACK_

The following commit has been merged into the x86/entry branch of tip:

The following commit has been merged into the x86/entry branch of tip:

Commit-ID:     135dbd0519cf7f72af120801ec7dbb79275a362d
Gitweb:        https://git.kernel.org/tip/135dbd0519cf7f72af120801ec7dbb79275a362d
Author:        Xin Li (Intel) <[email protected]>
AuthorDate:    Fri, 20 Nov 2026 15:50:24 -08:00
Committer:     Dave Hansen <[email protected]>
CommitterDate: Mon, 23 Nov 2026 08:23:40 -08:00

x86/cea: Prefix exception stack names with ESTACK_

Add the ESTACK_ prefix to the exception stack names to improve clarity and readability. Without the prefix, names like DF, NMI, and DB are too brief and potentially ambiguous.

This renaming also prepares for reaching the stacks by their enum exception_stack_ordering value instead of by name.

---
 arch/x86/coco/sev/noinstr.c           |  5 +++--
 arch/x86/coco/sev/vc-handle.c         |  3 ++-
 arch/x86/include/asm/cpu_entry_area.h | 26 +++++++++++++-------------
 arch/x86/kernel/cpu/common.c          | 10 +++++-----
 arch/x86/kernel/dumpstack_64.c        | 14 +++++++-------
 arch/x86/kernel/fred.c                |  6 +++---
 arch/x86/kernel/traps.c               |  2 +-
 arch/x86/mm/cpu_entry_area.c          | 12 ++++++------
 arch/x86/mm/fault.c                   |  2 +-
 9 files changed, 41 insertions(+), 39 deletions(-)
arch/x86/coco/sev/noinstr.c +3 -2
index e1e03f1..8ff556d 100644
@@ -31,7 +31,8 @@ static __always_inline bool on_vc_stack(struct pt_regs *regs)
 	if (ip_within_syscall_gap(regs))
 		return false;
 
-	return ((sp >= __this_cpu_ist_bottom_va(VC)) && (sp < __this_cpu_ist_top_va(VC)));
+	return ((sp >= __this_cpu_ist_bottom_va(ESTACK_VC)) &&
+		(sp <  __this_cpu_ist_top_va(ESTACK_VC)));
 }
 
 /*
@@ -83,7 +84,7 @@ void noinstr __sev_es_ist_exit(void)
 	/* Read IST entry */
 	ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]);
 
-	if (WARN_ON(ist == __this_cpu_ist_top_va(VC)))
+	if (WARN_ON(ist == __this_cpu_ist_top_va(ESTACK_VC)))
 		return;
 
 	/* Read back old IST entry and write it to the TSS */
arch/x86/coco/sev/vc-handle.c +2 -1
index f25d302..954df83 100644
@@ -854,7 +854,8 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 
 static __always_inline bool is_vc2_stack(unsigned long sp)
 {
-	return (sp >= __this_cpu_ist_bottom_va(VC2) && sp < __this_cpu_ist_top_va(VC2));
+	return (sp >= __this_cpu_ist_bottom_va(ESTACK_VC2) &&
+		sp <  __this_cpu_ist_top_va(ESTACK_VC2));
 }
 
 static __always_inline bool vc_from_invalid_context(struct pt_regs *regs)
arch/x86/include/asm/cpu_entry_area.h +13 -13
index 462fc34..d0f884c 100644
@@ -18,19 +18,19 @@
 
 /* Macro to enforce the same ordering and stack sizes */
 #define ESTACKS_MEMBERS(guardsize, optional_stack_size)		\
-	char	DF_stack_guard[guardsize];			\
-	char	DF_stack[EXCEPTION_STKSZ];			\
-	char	NMI_stack_guard[guardsize];			\
-	char	NMI_stack[EXCEPTION_STKSZ];			\
-	char	DB_stack_guard[guardsize];			\
-	char	DB_stack[EXCEPTION_STKSZ];			\
-	char	MCE_stack_guard[guardsize];			\
-	char	MCE_stack[EXCEPTION_STKSZ];			\
-	char	VC_stack_guard[guardsize];			\
-	char	VC_stack[optional_stack_size];			\
-	char	VC2_stack_guard[guardsize];			\
-	char	VC2_stack[optional_stack_size];			\
-	char	IST_top_guard[guardsize];			\
+	char	ESTACK_DF_stack_guard[guardsize];		\
+	char	ESTACK_DF_stack[EXCEPTION_STKSZ];		\
+	char	ESTACK_NMI_stack_guard[guardsize];		\
+	char	ESTACK_NMI_stack[EXCEPTION_STKSZ];		\
+	char	ESTACK_DB_stack_guard[guardsize];		\
+	char	ESTACK_DB_stack[EXCEPTION_STKSZ];		\
+	char	ESTACK_MCE_stack_guard[guardsize];		\
+	char	ESTACK_MCE_stack[EXCEPTION_STKSZ];		\
+	char	ESTACK_VC_stack_guard[guardsize];		\
+	char	ESTACK_VC_stack[optional_stack_size];		\
+	char	ESTACK_VC2_stack_guard[guardsize];		\
+	char	ESTACK_VC2_stack[optional_stack_size];		\
+	char	ESTACK_IST_top_guard[guardsize];		\
 
 /* The exception stacks' physical storage. No guard pages required */
 struct exception_stacks {
arch/x86/kernel/cpu/common.c +5 -5
index 7e4eb70..b2a4138 100644
@@ -2380,12 +2380,12 @@ static inline void setup_getcpu(int cpu)
 static inline void tss_setup_ist(struct tss_struct *tss)
 {
 	/* Set up the per-CPU TSS IST stacks */
-	tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
-	tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
-	tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
-	tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
+	tss->x86_tss.ist[IST_INDEX_DF]	= __this_cpu_ist_top_va(ESTACK_DF);
+	tss->x86_tss.ist[IST_INDEX_NMI]	= __this_cpu_ist_top_va(ESTACK_NMI);
+	tss->x86_tss.ist[IST_INDEX_DB]	= __this_cpu_ist_top_va(ESTACK_DB);
+	tss->x86_tss.ist[IST_INDEX_MCE]	= __this_cpu_ist_top_va(ESTACK_MCE);
 	/* Only mapped when SEV-ES is active */
-	tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC);
+	tss->x86_tss.ist[IST_INDEX_VC]	= __this_cpu_ist_top_va(ESTACK_VC);
 }
 #else /* CONFIG_X86_64 */
 static inline void tss_setup_ist(struct tss_struct *tss) { }
arch/x86/kernel/dumpstack_64.c +7 -7
index 6c5defd..40f51e2 100644
@@ -73,7 +73,7 @@ struct estack_pages {
 	 PFN_DOWN(CEA_ESTACK_OFFS(st) + CEA_ESTACK_SIZE(st) - 1)] = {	\
 		.offs	= CEA_ESTACK_OFFS(st),				\
 		.size	= CEA_ESTACK_SIZE(st),				\
-		.type	= STACK_TYPE_EXCEPTION + ESTACK_ ##st, }
+		.type	= STACK_TYPE_EXCEPTION + st, }
 
 /*
  * Array of exception stack page descriptors. If the stack is larger than
@@ -83,12 +83,12 @@ struct estack_pages {
  */
 static const
 struct estack_pages estack_pages[CEA_ESTACK_PAGES] ____cacheline_aligned = {
-	EPAGERANGE(DF),
-	EPAGERANGE(NMI),
-	EPAGERANGE(DB),
-	EPAGERANGE(MCE),
-	EPAGERANGE(VC),
-	EPAGERANGE(VC2),
+	EPAGERANGE(ESTACK_DF),
+	EPAGERANGE(ESTACK_NMI),
+	EPAGERANGE(ESTACK_DB),
+	EPAGERANGE(ESTACK_MCE),
+	EPAGERANGE(ESTACK_VC),
+	EPAGERANGE(ESTACK_VC2),
 };
 
 static __always_inline bool in_exception_stack(unsigned long *stack, struct stack_info *info)
arch/x86/kernel/fred.c +3 -3
index 117aa06..1c452a3 100644
@@ -84,7 +84,7 @@ void cpu_init_fred_rsps(void)
 	       FRED_STKLVL(X86_TRAP_DF,  FRED_DF_STACK_LEVEL));
 
 	/* The FRED equivalents to IST stacks... */
-	wrmsrq(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB));
-	wrmsrq(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI));
-	wrmsrq(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF));
+	wrmsrq(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(ESTACK_DB));
+	wrmsrq(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(ESTACK_NMI));
+	wrmsrq(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(ESTACK_DF));
 }
arch/x86/kernel/traps.c +1 -1
index 30aa836..8afd607 100644
@@ -1075,7 +1075,7 @@ asmlinkage __visible noinstr struct pt_regs *vc_switch_off_ist(struct pt_regs *r
 
 	if (!get_stack_info_noinstr(stack, current, &info) || info.type == STACK_TYPE_ENTRY ||
 	    info.type > STACK_TYPE_EXCEPTION_LAST)
-		sp = __this_cpu_ist_top_va(VC2);
+		sp = __this_cpu_ist_top_va(ESTACK_VC2);
 
 sync:
 	/*
arch/x86/mm/cpu_entry_area.c +6 -6
index 575f863..9fa371a 100644
@@ -151,15 +151,15 @@ static void __init percpu_setup_exception_stacks(unsigned int cpu)
 	 * by guard pages so each stack must be mapped separately. DB2 is
 	 * not mapped; it just exists to catch triple nesting of #DB.
 	 */
-	cea_map_stack(DF);
-	cea_map_stack(NMI);
-	cea_map_stack(DB);
-	cea_map_stack(MCE);
+	cea_map_stack(ESTACK_DF);
+	cea_map_stack(ESTACK_NMI);
+	cea_map_stack(ESTACK_DB);
+	cea_map_stack(ESTACK_MCE);
 
 	if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
 		if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT)) {
-			cea_map_stack(VC);
-			cea_map_stack(VC2);
+			cea_map_stack(ESTACK_VC);
+			cea_map_stack(ESTACK_VC2);
 		}
 	}
 }
arch/x86/mm/fault.c +1 -1
index aa88370..37f9729 100644
@@ -677,7 +677,7 @@ page_fault_oops(struct pt_regs *regs, unsigned long error_code,
 		 * and then double-fault, though, because we're likely to
 		 * break the console driver and lose most of the stack dump.
 		 */
-		call_on_stack(__this_cpu_ist_top_va(DF) - sizeof(void*),
+		call_on_stack(__this_cpu_ist_top_va(ESTACK_DF) - sizeof(void *),
 			      handle_stack_overflow,
 			      ASM_CALL_ARG3,
 			      , [arg1] "r" (regs), [arg2] "r" (address), [arg3] "r" (&info));
#3 lore raw

PATCH 2/2 x86/cea: Index exception stacks by the ordering enum

In reply to Sohil Mehta

The exception stacks are generated by ESTACKS_MEMBERS(), which pastes each stack name into a member name, once for the physical storage and once for the CEA mapping.

The macro used to be the only thing keeping the two structures in the same order, but there is no ordering requirement left to enforce. The stacks are mapped one at a time and have been paired by name for a while. Also, the sizes stopped matching once the #VC stacks became config dependent.

So, spell out the physical storage and index the mapping by the stack ordering enum. Also drop CEA_ESTACK_SIZE, which just duplicated EXCEPTION_STKSZ.

The stack addresses and the guard pages are unchanged. No functional change intended.

---
v1:
- Drop the conversion of __this_cpu_ist_{bottom,top}_va() from macros to functions. KVM does not need the accessor exported any more.
- Expand struct exception_stacks explicitly and drop ESTACKS_MEMBERS() altogether.
- Name the new array stacks[] rather than event_stacks[].
- Switch authorship due to significant changes to Xin's original patch.
- Drop Binbin's and Dave's tags.

FRED KVM v10:
- https://lore.kernel.org/lkml/[email protected]/

---
 arch/x86/include/asm/cpu_entry_area.h | 52 +++++++++++----------------
 arch/x86/kernel/dumpstack_64.c        |  4 +--
 arch/x86/mm/cpu_entry_area.c          |  2 +-
 3 files changed, 24 insertions(+), 34 deletions(-)
arch/x86/include/asm/cpu_entry_area.h +21 -31
index d0f884c28178..ba6456a04180 100644
@@ -16,32 +16,6 @@
 #define VC_EXCEPTION_STKSZ	0
 #endif
 
-/* Macro to enforce the same ordering and stack sizes */
-#define ESTACKS_MEMBERS(guardsize, optional_stack_size)		\
-	char	ESTACK_DF_stack_guard[guardsize];		\
-	char	ESTACK_DF_stack[EXCEPTION_STKSZ];		\
-	char	ESTACK_NMI_stack_guard[guardsize];		\
-	char	ESTACK_NMI_stack[EXCEPTION_STKSZ];		\
-	char	ESTACK_DB_stack_guard[guardsize];		\
-	char	ESTACK_DB_stack[EXCEPTION_STKSZ];		\
-	char	ESTACK_MCE_stack_guard[guardsize];		\
-	char	ESTACK_MCE_stack[EXCEPTION_STKSZ];		\
-	char	ESTACK_VC_stack_guard[guardsize];		\
-	char	ESTACK_VC_stack[optional_stack_size];		\
-	char	ESTACK_VC2_stack_guard[guardsize];		\
-	char	ESTACK_VC2_stack[optional_stack_size];		\
-	char	ESTACK_IST_top_guard[guardsize];		\
-
-/* The exception stacks' physical storage. No guard pages required */
-struct exception_stacks {
-	ESTACKS_MEMBERS(0, VC_EXCEPTION_STKSZ)
-};
-
-/* The effective cpu entry area mapping with guard pages. */
-struct cea_exception_stacks {
-	ESTACKS_MEMBERS(PAGE_SIZE, EXCEPTION_STKSZ)
-};
-
 /*
  * The exception stack ordering in [cea_]exception_stacks
  */
@@ -55,17 +29,33 @@ enum exception_stack_ordering {
 	N_EXCEPTION_STACKS
 };
 
-#define CEA_ESTACK_SIZE(st)					\
-	sizeof(((struct cea_exception_stacks *)0)->st## _stack)
+/* The exception stacks' physical storage. No guard pages required */
+struct exception_stacks {
+	char ESTACK_DF_stack[EXCEPTION_STKSZ];
+	char ESTACK_NMI_stack[EXCEPTION_STKSZ];
+	char ESTACK_DB_stack[EXCEPTION_STKSZ];
+	char ESTACK_MCE_stack[EXCEPTION_STKSZ];
+	char ESTACK_VC_stack[VC_EXCEPTION_STKSZ];
+	char ESTACK_VC2_stack[VC_EXCEPTION_STKSZ];
+};
+
+/* The effective cpu entry area mapping with guard pages. */
+struct cea_exception_stacks {
+	struct {
+		char stack_guard[PAGE_SIZE];
+		char stack[EXCEPTION_STKSZ];
+	} stacks[N_EXCEPTION_STACKS];
+	char IST_top_guard[PAGE_SIZE];
+};
 
 #define CEA_ESTACK_BOT(ceastp, st)				\
-	((unsigned long)&(ceastp)->st## _stack)
+	((unsigned long)&(ceastp)->stacks[st].stack)
 
 #define CEA_ESTACK_TOP(ceastp, st)				\
-	(CEA_ESTACK_BOT(ceastp, st) + CEA_ESTACK_SIZE(st))
+	(CEA_ESTACK_BOT(ceastp, st) + EXCEPTION_STKSZ)
 
 #define CEA_ESTACK_OFFS(st)					\
-	offsetof(struct cea_exception_stacks, st## _stack)
+	offsetof(struct cea_exception_stacks, stacks[st].stack)
 
 #define CEA_ESTACK_PAGES					\
 	(sizeof(struct cea_exception_stacks) / PAGE_SIZE)
arch/x86/kernel/dumpstack_64.c +2 -2
index 40f51e278171..93b10b264e53 100644
@@ -70,9 +70,9 @@ struct estack_pages {
 
 #define EPAGERANGE(st)							\
 	[PFN_DOWN(CEA_ESTACK_OFFS(st)) ...				\
-	 PFN_DOWN(CEA_ESTACK_OFFS(st) + CEA_ESTACK_SIZE(st) - 1)] = {	\
+	 PFN_DOWN(CEA_ESTACK_OFFS(st) + EXCEPTION_STKSZ - 1)] = {	\
 		.offs	= CEA_ESTACK_OFFS(st),				\
-		.size	= CEA_ESTACK_SIZE(st),				\
+		.size	= EXCEPTION_STKSZ,				\
 		.type	= STACK_TYPE_EXCEPTION + st, }
 
 /*
arch/x86/mm/cpu_entry_area.c +1 -1
index 9fa371af8abc..570e60fde79c 100644
@@ -132,7 +132,7 @@ static void __init percpu_setup_debug_store(unsigned int cpu)
 
 #define cea_map_stack(name) do {					\
 	npages = sizeof(estacks->name## _stack) / PAGE_SIZE;		\
-	cea_map_percpu_pages(cea->estacks.name## _stack,		\
+	cea_map_percpu_pages(cea->estacks.stacks[name].stack,		\
 			estacks->name## _stack, npages, PAGE_KERNEL);	\
 	} while (0)
 
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] x86/cea: Index exception stacks by the ordering enum

Quoted 10 lines · Sohil Mehta

The exception stacks are generated by ESTACKS_MEMBERS(), which pastes each stack name into a member name, once for the physical storage and once for the CEA mapping.

The macro used to be the only thing keeping the two structures in the same order, but there is no ordering requirement left to enforce. The stacks are mapped one at a time and have been paired by name for a while. Also, the sizes stopped matching once the #VC stacks became config dependent.

Quoted 8 lines

So, spell out the physical storage and index the mapping by the stack ordering enum. Also drop CEA_ESTACK_SIZE, which just duplicated EXCEPTION_STKSZ.

The stack addresses and the guard pages are unchanged. No functional change intended.

#5 lore raw

Re: [PATCH 2/2] x86/cea: Index exception stacks by the ordering enum

In reply to Sohil Mehta

arch/x86/include/asm/cpu_entry_area.h enum exception_stack_ordering { · line 37

Quoted 6 lines · Sohil Mehta
arch/x86/include/asm/cpu_entry_area.h +6 -0
+/* The exception stacks' physical storage. No guard pages required */
+struct exception_stacks {
+	char ESTACK_DF_stack [EXCEPTION_STKSZ];
+	char ESTACK_NMI_stack[EXCEPTION_STKSZ];
+	char ESTACK_DB_stack [EXCEPTION_STKSZ];
+	char ESTACK_MCE_stack[EXCEPTION_STKSZ];
	/* The #VC stack storage is conditionally compiled out: */

arch/x86/include/asm/cpu_entry_area.h enum exception_stack_ordering { · line 40

Quoted 3 lines
arch/x86/include/asm/cpu_entry_area.h +3 -0
+	char ESTACK_VC_stack [VC_EXCEPTION_STKSZ];
+	char ESTACK_VC2_stack[VC_EXCEPTION_STKSZ];
+};

Please vertically align the [EXCEPTION_STKSZ] like I did ^. Also, please add some kind of comment about why the VC stacks need to be different.

Honestly, though, looking at this, two #ifdefs would be perfectly readable here.

struct exception_stacks {
	char ESTACK_DF_stack [EXCEPTION_STKSZ];
	char ESTACK_NMI_stack[EXCEPTION_STKSZ];
	char ESTACK_DB_stack [EXCEPTION_STKSZ];
	char ESTACK_MCE_stack[EXCEPTION_STKSZ];
#ifdef CONFIG_AMD_MEM_ENCRYPT
	char ESTACK_VC_stack [EXCEPTION_STKSZ];
	char ESTACK_VC2_stack[EXCEPTION_STKSZ];

#endif
};

Because, without it, we have to explain VC_EXCEPTION_STKSZ anyway.

arch/x86/include/asm/cpu_entry_area.h enum exception_stack_ordering { · line 49

Quoted 8 lines
arch/x86/include/asm/cpu_entry_area.h +8 -0
+/* The effective cpu entry area mapping with guard pages. */
+struct cea_exception_stacks {
+	struct {
+		char stack_guard[PAGE_SIZE];
+		char stack[EXCEPTION_STKSZ];
+	} stacks[N_EXCEPTION_STACKS];
+	char IST_top_guard[PAGE_SIZE];
+};

I dislike the asymmetry here. I'd rather just have two explicit lists of stacks. This also begs the question but doesn't offer an answer about why it's OK to have EXCEPTION_STKSZ vs. VC_EXCEPTION_STKSZ in the "exception_stacks" but have EXCEPTION_STKSZ for all in "cea_exception_stacks".

#6 lore raw

Re: [PATCH 2/2] x86/cea: Index exception stacks by the ordering enum

Quoted 8 lines · Dave Hansen

arch/x86/include/asm/cpu_entry_area.h enum exception_stack_ordering { · line 37

Quoted 6 lines · Sohil Mehta
arch/x86/include/asm/cpu_entry_area.h +6 -0
+/* The exception stacks' physical storage. No guard pages required */
+struct exception_stacks {
+	char ESTACK_DF_stack [EXCEPTION_STKSZ];
+	char ESTACK_NMI_stack[EXCEPTION_STKSZ];
+	char ESTACK_DB_stack [EXCEPTION_STKSZ];
+	char ESTACK_MCE_stack[EXCEPTION_STKSZ];
	/* The #VC stack storage is conditionally compiled out: */
Quoted 5 lines

arch/x86/include/asm/cpu_entry_area.h enum exception_stack_ordering { · line 40

Quoted 3 lines
arch/x86/include/asm/cpu_entry_area.h +3 -0
+	char ESTACK_VC_stack [VC_EXCEPTION_STKSZ];
+	char ESTACK_VC2_stack[VC_EXCEPTION_STKSZ];
+};

Please vertically align the [EXCEPTION_STKSZ] like I did ^.

Sure, will do.

Quoted 6 lines

Also, please add some kind of comment about why the VC stacks need to be different.

Honestly, though, looking at this, two #ifdefs would be perfectly readable here.

I think commit 541ac97186d9 ("x86/sev: Make the #VC exception stacks part of the default stacks storage") added the #ifdefs in the header file to avoid the #ifdefs in percpu_setup_exception_stacks().

Quoted 8 lines
struct exception_stacks {
	char ESTACK_DF_stack [EXCEPTION_STKSZ];
	char ESTACK_NMI_stack[EXCEPTION_STKSZ];
	char ESTACK_DB_stack [EXCEPTION_STKSZ];
	char ESTACK_MCE_stack[EXCEPTION_STKSZ];
#ifdef CONFIG_AMD_MEM_ENCRYPT
	char ESTACK_VC_stack [EXCEPTION_STKSZ];
	char ESTACK_VC2_stack[EXCEPTION_STKSZ];
Quoted 5 lines

#endif
};

Because, without it, we have to explain VC_EXCEPTION_STKSZ anyway.

I tried a few variations to get rid of VC_EXCEPTION_STKSZ but they all make the C files uglier. C code refers ESTACK_VC/VC2 unconditionally or under IS_ENABLED(). I am inclined to add the comment instead because it keeps those C functions cleaner.

Quoted 11 lines

arch/x86/include/asm/cpu_entry_area.h enum exception_stack_ordering { · line 49

Quoted 8 lines
arch/x86/include/asm/cpu_entry_area.h +8 -0
+/* The effective cpu entry area mapping with guard pages. */
+struct cea_exception_stacks {
+	struct {
+		char stack_guard[PAGE_SIZE];
+		char stack[EXCEPTION_STKSZ];
+	} stacks[N_EXCEPTION_STACKS];
+	char IST_top_guard[PAGE_SIZE];
+};

I dislike the asymmetry here. I'd rather just have two explicit lists of stacks.

This request seems to have originated from you to simplify the accessors :)

https://lore.kernel.org/all/[email protected]/

It would have been ideal if we could move everything over to the enum as you requested below. But I can't figure out a clean way to do it without making other code worse.

https://lore.kernel.org/all/[email protected]/

It is a trade-off between using name pasting vs array indexing. If we want to keep the symmetry then keeping the existing ESTACKS_MEMBERS() macro seems better. Should I drop this patch and create another one to fix the comments and add the VC_EXCEPTION_STKSZ explanation?

Quoted 4 lines

This also begs the question but doesn't offer an answer about why it's OK to have EXCEPTION_STKSZ vs. VC_EXCEPTION_STKSZ in the "exception_stacks" but have EXCEPTION_STKSZ for all in "cea_exception_stacks".

The exception_stacks tracks to real storage so it uses VC_EXCEPTION_STKSZ to save space in case CONFIG_AMD_MEM_ENCRYPT isn't enabled. The cea_exception_stacks only reserve a slot with no PTEs so they are using EXCEPTION_STKSZ for simplicity.

Though, I agree, this should be explained properly if we decide to go ahead with this patch.

#7 lore raw

Re: [PATCH 2/2] x86/cea: Index exception stacks by the ordering enum

Quoted 9 lines · Sohil Mehta
Quoted 6 lines

Also, please add some kind of comment about why the VC stacks need to be different.

Honestly, though, looking at this, two #ifdefs would be perfectly readable here.

I think commit 541ac97186d9 ("x86/sev: Make the #VC exception stacks part of the default stacks storage") added the #ifdefs in the header

I meant ^ VC_EXCEPTION_STKSZ

Quoted 17 lines

file to avoid the #ifdefs in percpu_setup_exception_stacks().

Quoted 13 lines
struct exception_stacks {
	char ESTACK_DF_stack [EXCEPTION_STKSZ];
	char ESTACK_NMI_stack[EXCEPTION_STKSZ];
	char ESTACK_DB_stack [EXCEPTION_STKSZ];
	char ESTACK_MCE_stack[EXCEPTION_STKSZ];
#ifdef CONFIG_AMD_MEM_ENCRYPT
	char ESTACK_VC_stack [EXCEPTION_STKSZ];
	char ESTACK_VC2_stack[EXCEPTION_STKSZ];

#endif
};

Because, without it, we have to explain VC_EXCEPTION_STKSZ anyway.

...

Quoted 11 lines
Quoted 4 lines

This also begs the question but doesn't offer an answer about why it's OK to have EXCEPTION_STKSZ vs. VC_EXCEPTION_STKSZ in the "exception_stacks" but have EXCEPTION_STKSZ for all in "cea_exception_stacks".

The exception_stacks tracks to real storage so it uses VC_EXCEPTION_STKSZ to save space in case CONFIG_AMD_MEM_ENCRYPT isn't enabled. The cea_exception_stacks only reserve a slot with no PTEs so they are using EXCEPTION_STKSZ for simplicity.

Ah! This isn't just for simplicity. The EPAGERANGE(ESTACK_VC/VC2) usage in dumpstack has a compile issue if the CEA_ESTACK_SIZE is zero.

Overall, Patch 1 was the most useful (Thanks for merging that). The name pasting macros have been working fine for a few years. I am not seeing a lot of value in disturbing the status quo with Patch 2 especially since we want to keep the two structs symmetric.

Maybe some clarifying code comments would be useful, but I can't think of anything beyond that. Please let me know if you feel otherwise.