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

From: Sohil Mehta

Date: Mon Nov 23 2026 - 13:35:10 EST


On 11/23/2026 8:19 AM, Dave Hansen wrote:
> On 11/20/26 15:50, Sohil Mehta wrote:
>> +/* 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: */
>> + 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.

> 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().

> 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.
>


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.

>> +/* 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/720bc7ac-7e81-4ad9-8cc5-29ac540be283@xxxxxxxxx/

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/ba8434fb-8bbd-4d4a-bd01-1bdb2219979b@xxxxxxxxx/

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?


> 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.