From: Sohil Mehta <sohil.mehta@intel.com>
To: Dave Hansen <dave.hansen@intel.com>,
Dave Hansen <dave.hansen@linux.intel.com>,
Borislav Petkov <bp@alien8.de>
Cc: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
<x86@kernel.org>, "H . Peter Anvin" <hpa@zytor.com>,
Xin Li <xin@zytor.com>, Andy Lutomirski <luto@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Binbin Wu <binbin.wu@linux.intel.com>,
Tom Lendacky <thomas.lendacky@amd.com>,
"Chang S . Bae" <chang.seok.bae@intel.com>,
"Chao Gao" <chao.gao@intel.com>,
Sean Christopherson <seanjc@google.com>,
"Paolo Bonzini" <pbonzini@redhat.com>, <kvm@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2] x86/fred: Consolidate the FRED stack pointer assignments
Date: Mon, 30 Nov 2026 15:37:26 -0800 [thread overview]
Message-ID: <7e5b90b6-8490-4672-8129-ee25a2457636@intel.com> (raw)
In-Reply-To: <8525439b-5b3e-4eed-a09f-f387d8377013@intel.com>
On 11/30/2026 2:43 PM, Dave Hansen wrote:
> On 11/24/26 12:06, Sohil Mehta wrote:
>> +static unsigned long this_cpu_fred_sp(u32 msr)
>> +{
>
> I don't super like where this is going.
>
Changing the order of your questions to make my reply easier:
> BTW, does the KVM code need the MSR value or does it also need to do a
> wrmsr()?
The KVM code only needs the MSR values. It does not do an MSR write.
Every time a vCPU is loaded on a CPU it needs to program it in the VMCS
field HOST_IA32_FRED_RSPX:
vmcs_write64(HOST_IA32_FRED_RSP1, this_cpu_fred_sp(MSR_IA32_FRED_RSP1));
vmcs_write64(HOST_IA32_FRED_RSP2, this_cpu_fred_sp(MSR_IA32_FRED_RSP2));
vmcs_write64(HOST_IA32_FRED_RSP3, this_cpu_fred_sp(MSR_IA32_FRED_RSP3));
KVM could directly read the value from the real MSR but since the kernel
already has this cached per-cpu, it makes sense to read from that
instead. The interface can be keyed on the MSR itself or on a new enum
described below.
> The previous code kinda showed in one place how the FRED stack levels
> map to the exception stacks:
So we have three existing things here that create and use the mapping:
One:
#define FRED_DB_STACK_LEVEL 1UL
#define FRED_NMI_STACK_LEVEL 2UL
#define FRED_MC_STACK_LEVEL 2UL
#define FRED_DF_STACK_LEVEL 3UL
Two:
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));
Three:
wrmsrq(MSR_IA32_FRED_STKLVLS,
FRED_STKLVL(X86_TRAP_DB, FRED_DB_STACK_LEVEL) |
FRED_STKLVL(X86_TRAP_NMI, FRED_NMI_STACK_LEVEL) |
FRED_STKLVL(X86_TRAP_MC, FRED_MC_STACK_LEVEL) |
FRED_STKLVL(X86_TRAP_DF, FRED_DF_STACK_LEVEL));
The KVM side understandably wants to avoid knowing the details of which
exact stack is mapped to which level. We could introduce a new
abstraction enum (which Xin's v10 code did) to build the above mapping:
enum fred_stack_level {
FRED_STACK_LEVEL_0,
FRED_STACK_LEVEL_1,
FRED_STACK_LEVEL_2,
FRED_STACK_LEVEL_3
};
Then the new helper function could be keyed on that:
static unsigned long this_cpu_fred_rsp(enum fred_stack_level)
But then KVM would need to know and pass this new enum. The
FRED_*_STACK_LEVEL defines seem unnecessary at this point.
>
>> /* The FRED equivalents to IST stacks... */
>> - 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));
>
> But the new abstraction hides it completely:
>
>> + wrmsrq(MSR_IA32_FRED_RSP1, this_cpu_fred_sp(MSR_IA32_FRED_RSP1));
>> + wrmsrq(MSR_IA32_FRED_RSP2, this_cpu_fred_sp(MSR_IA32_FRED_RSP2));
>> + wrmsrq(MSR_IA32_FRED_RSP3, this_cpu_fred_sp(MSR_IA32_FRED_RSP3));
>> }
>
> It also says MSR_IA32_FRED_RSPX in a redundant way.
>
> Can this be done without the redundancy? Could it also be done with a
> build warning instead of a runtime one?
I am not sure. Let me try to figure out if we can do it more
efficiently. I was trying to keep this easily expandable so that kernel
shadow stacks don't need to redo all of this.
prev parent reply other threads:[~2026-11-30 23:37 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 20:06 Sohil Mehta
2026-11-30 22:43 ` Dave Hansen
2026-11-30 23:18 ` H. Peter Anvin
2026-11-30 23:38 ` Dave Hansen
2026-11-30 23:43 ` Sohil Mehta
2026-11-30 23:48 ` Dave Hansen
2026-12-01 0:02 ` Sohil Mehta
2026-12-01 0:06 ` H. Peter Anvin
2026-11-30 23:37 ` Sohil Mehta [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7e5b90b6-8490-4672-8129-ee25a2457636@intel.com \
--to=sohil.mehta@intel.com \
--cc=binbin.wu@linux.intel.com \
--cc=bp@alien8.de \
--cc=chang.seok.bae@intel.com \
--cc=chao.gao@intel.com \
--cc=dave.hansen@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=seanjc@google.com \
--cc=tglx@kernel.org \
--cc=thomas.lendacky@amd.com \
--cc=x86@kernel.org \
--cc=xin@zytor.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®