* [Patch v6 0/3] Host support for SMT and Enhanced SMT Protection
@ 2026-12-05 22:34 Pratik R. Sampat
2026-12-05 22:34 ` [Patch v6 1/3] KVM: SVM: Re-queue events that were never injected Pratik R. Sampat
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Pratik R. Sampat @ 2026-12-05 22:34 UTC (permalink / raw)
To: x86, kvm, linux-doc, linux-kernel
Cc: tglx, mingo, bp, dave.hansen, haiyangz, wei.liu, seanjc, pbonzini,
thomas.lendacky, kim.phillips, nikunj, michael.roth, ashish.kalra,
prsampat
SMT Protection (SMTP) and Enhanced SMT Protection (ESMTP) let an
SEV-SNP VM control what may run on the SMT sibling of a thread that is
executing one of its vCPUs. This mitigates the side-channel risk of
sharing core resources with untrusted host threads or with another
guest.
SMTP (Zen 4 and later) requires every sibling thread on the physical
core to be idle in host mode while the vCPU is in guest mode, which
gives up half of every core the VM runs on.
ESMTP (Zen 5 and later) relaxes that: a sibling may also run a vCPU
the guest has declared a legal sibling. A legal sibling is another
ESMTP vCPU of the same guest (matching ASID) with the same
VCPU_SIBLING_MASK and the same VCPU_ID outside that mask.
Unlike core scheduling, where co-residency is a host kernel policy
expressed with cookies, both features are enforced by hardware. For
ESMTP the policy lives in the VMSA. The host is not trusted to keep
its own kernel, userspace, or interrupt-handling work off a sibling
thread while a protected vCPU is in guest mode.
Series
----------
Patch 1 is a prerequisite. KVM has assumed that a #VMEXIT with
EVENTINJ still valid only happens on VMEXIT_INVALID, after which the
guest is torn down. With SMTP and ESMTP, VMRUN can end with an
ordinary exit code without entering guest mode, so the staged event
was never delivered. Detect that in svm_complete_interrupts() and
requeue the event instead of dropping it.
Patch 2 adds SMTP: the CPUID feature bit, IDLE_WAKEUP_ICR, retrying
VMRUN on VMEXIT_IDLE_REQUIRED, and advertising SVM_SEV_FEAT_SMTP
through KVM_X86_SEV_VMSA_FEATURES so userspace can request it with
KVM_SEV_INIT2. IDLE_WAKEUP_ICR is programmed on each CPU when
virtualization is enabled on it.
Patch 3 adds ESMTP on top: the scattered CPUID bit, the VMCB timeout
field, retrying the three new exits, populating VCPU_ID and
VCPU_SIBLING_MASK in the launch VMSAs KVM builds, KVM fully sets
VCPU_SIBLING_MASK, as the guest does for its APs, so every vCPU of the
guest may be co-resident with any other.
Merging
-------
Following the discussion on v4, guest and host support are now split
into separate series to untangle x86 and KVM. Since the guest series
from v5 is now pushed to tip, this series adds the KVM host side.
8509996df261 ("x86/sev: Add Enhanced SMT Protection fields to the VMSA")
0d4aa2a4f846 ("x86/sev: Add guest support for Enhanced SMT Protection")
SMTP needs no guest changes.
Testing
-------
It is tested with the v5 guest patches and patched OVMF [1] and
QEMU [2] builds. Launch an SEV-SNP guest with ESMTP enabled on the
sev-snp-guest object:
For SMTP:
-object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,smtp=on
Or,
For ESMTP:
-object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,esmtp=on
SMTP and ESMTP are mutually exclusive.
The guest reports the feature in dmesg among the SNP feature names:
# dmesg | grep -i SEV
... SEV-SNP ... SMTProt ...
Or,
... SEV-SNP ... ESMTProt ...
To check the sibling behaviour on an SMT=2 host:
* Identify a sibling pair X and Y on the host:
cat /sys/devices/system/cpu/cpuX/topology/thread_siblings_list
* Pin two of the guest's vCPUs to X and Y.
* Load both vCPUs in the guest, e.g. stress-ng --cpu 2.
* With SMTP: host CPUs should show 50% guest utilization each, since
the two vCPUs cannot run together at once and one sibling thread
must idle when the other runs.
* With ESMTP: Both host CPUs should show 100% guest utilization, since
the two vCPUs are legal siblings.
* Start a host task on one of the pair, e.g. taskset -c X stress-ng
--cpu 1. CPU X's time should now be split between host and guest.
* With SMTP: guest utilization on CPU Y should remain relatively
unchanged since it already must remain idle regardless of what runs
on CPU X.
* With ESMTP: guest utilization on CPU Y should drop, since the vCPU
on Y cannot enter guest mode while the host task runs on X.
Based on tip/master.
v6:
* Host support only. Guest support was split out in v5 and is in tip.
* New patch 2: host support for regular SMT Protection. SMTP is less
flexible than ESMTP, since it keeps the sibling idle instead of
allowing a legal sibling vCPU, but ESMTP needs Zen 5 or later while
SMTP is available from Zen 4.
* Move IDLE_WAKEUP_ICR programming which is used by both SMTP and ESMTP
to the former patch. Since functionally the patch remains the,
retaining reviewed-by's.
* Patch 1: update the commit message and comment to cover SMTP as well
as ESMTP.
* Patch 3: reject KVM_SEV_INIT2 requests that set both SMTP and ESMTP,
now that KVM can advertise both.
v5: https://lore.kernel.org/kvm/cover.1794935627.git.prsampat@amd.com/
* Split guest and host support into separate series, per Sean and
Boris [4]. This series carries guest support only.
* New patch 1: move the VCPU_ID/VCPU_SIBLING_MASK VMSA definitions out
of the KVM host patch so that the guest side builds on its own.
Retaining Tom's reviewed-by since it is functionally unchanged.
* Drop the sibling-mask comments from patches 2 and 3 - Boris
* Rebase onto tip/master.
v4: https://lore.kernel.org/all/cover.1794244450.git.prsampat@amd.com/
* Fix conflict of reserved bits introduced by PML support in
vmcb_control_area
v3: https://lore.kernel.org/kvm/cover.1792184487.git.prsampat@amd.com
* Collected Reviewed-by tags - Tom
* Use sev_status to determine SNP support for guest as well - Tom
* Not syncing the tools/ copy of msr-index.h, since several bits apart
from ESMTP differ and the perf tooling maintainers usually sync it.
Happy to add a sync patch if needed.
v2: https://lore.kernel.org/kvm/cover.1791302373.git.prsampat@amd.com/
* Populate the ESMTP VMSA fields for Hyper-V AP bring-up - Sashiko
* Add ESMTP to SNP_FEATURES_IMPL and SNP_FEATURES_IMPL_REQ - local
Sashiko instance
v1: https://lore.kernel.org/kvm/cover.1789399214.git.prsampat@amd.com
[1]: https://github.com/tianocore/edk2/pull/13128
[2]: https://lists.nongnu.org/archive/html/qemu-devel/2026-12/msg01488.html
[3]: https://lore.kernel.org/all/avc-5lkQhoR6US9o@google.com/
Pratik R. Sampat (3):
KVM: SVM: Re-queue events that were never injected
KVM: SVM: Add host support for SMT Protection
KVM: SVM: Add host support for Enhanced SMT Protection
.../admin-guide/kernel-parameters.txt | 19 ++++++
arch/x86/include/asm/cpufeatures.h | 2 +
arch/x86/include/asm/msr-index.h | 1 +
arch/x86/include/asm/svm.h | 5 +-
arch/x86/include/uapi/asm/svm.h | 11 +++-
arch/x86/kernel/cpu/scattered.c | 1 +
arch/x86/kvm/svm/sev.c | 58 ++++++++++++++++++-
arch/x86/kvm/svm/svm.c | 42 ++++++++++++--
arch/x86/kvm/svm/svm.h | 2 +
9 files changed, 134 insertions(+), 7 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [Patch v6 1/3] KVM: SVM: Re-queue events that were never injected 2026-12-05 22:34 [Patch v6 0/3] Host support for SMT and Enhanced SMT Protection Pratik R. Sampat @ 2026-12-05 22:34 ` Pratik R. Sampat 2026-12-05 22:59 ` sashiko-bot 2026-12-05 22:34 ` [Patch v6 2/3] KVM: SVM: Add host support for SMT Protection Pratik R. Sampat 2026-12-05 22:35 ` [Patch v6 3/3] KVM: SVM: Add host support for Enhanced " Pratik R. Sampat 2 siblings, 1 reply; 7+ messages in thread From: Pratik R. Sampat @ 2026-12-05 22:34 UTC (permalink / raw) To: x86, kvm, linux-doc, linux-kernel Cc: tglx, mingo, bp, dave.hansen, haiyangz, wei.liu, seanjc, pbonzini, thomas.lendacky, kim.phillips, nikunj, michael.roth, ashish.kalra, prsampat When injecting an event into the guest via the event_inj field, a non-zero event_inj value on #VMEXIT means that the hardware was not able to inject the event into the guest. This used to only occur for the VMEXIT_INVALID intercept code, which was a fatal error and resulted in the guest being torn down. SMT protection (SMTP) and Enhanced SMT Protection (ESMTP) invalidates that assumption. When enabled, VMRUN doesn't enter guest mode immediately; it stalls at a synchronization point until every sibling thread is either idle or has executed VMRUN for a legal sibling vCPU. If a timeout / illegal sibling exit / interrupt arrives while VMRUN is stalled, VMRUN can now terminate with the corresponding #VMEXIT intercept code, without entering guest mode, and thus without injecting the event. For example, on a 2-way SMT core running vCPU0 on thread 0 and vCPU1 on thread 1: Thread 0 (vCPU0) Thread 1 (vCPU1) ---------------- ---------------- Interrupt A injected to the guest in host VMRUN | | | v | Stall waiting for sibling | | | | host INTR arrives on thread 0 | | while it waits | | | v | #VMEXIT v Interrupt B injected to the guest idle VMRUN In this case injecting interrupt B clobbers the last event. Interrupt A is never delivered and lost forever. Therefore, in preparation for [E]SMTP support, accommodate for potentially lost interrupts by detecting an undelivered injected event and re-queuing it so as not to lose the event. svm_cancel_injection() already recovers a staged event this way and becomes redundant, so fold it in. Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com> Signed-off-by: Pratik R. Sampat <prsampat@amd.com> --- v5..v6: * Massage commit message to include SMT protection case --- arch/x86/kvm/svm/svm.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 47987f5ce095..8dbbc6beb74a 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4482,6 +4482,7 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu) struct vcpu_svm *svm = to_svm(vcpu); u8 vector; int type; + struct vmcb_control_area *control = &svm->vmcb->control; u32 exitintinfo = svm->vmcb->control.exit_int_info; bool nmi_l1_to_l2 = svm->nmi_l1_to_l2; bool soft_int_injected = svm->soft_int_injected; @@ -4489,6 +4490,27 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu) svm->nmi_l1_to_l2 = false; svm->soft_int_injected = false; + /* + * Hardware clears EVENTINJ field when it injects an event. + * A non-empty EVENTINJ on #VMEXIT means the vCPU never entered guest + * mode, and thus that the event was never delivered. Migrate the event + * to EXITINTINFO so that it's requeued instead of being dropped. + * + * An undelivered event doesn't imply a fatal VMEXIT_INVALID. With + * SMT Protection and Enhanced SMT Protection, VMRUN may exit with an + * ordinary #VMEXIT without having injected that event into the guest. + * + * Clobbering EXITINTINFO is safe precisely because the vCPU never + * entered guest mode. + */ + if (control->event_inj) { + control->exit_int_info = control->event_inj; + control->exit_int_info_err = control->event_inj_err; + control->event_inj = 0; + + exitintinfo = control->exit_int_info; + } + /* * If we've made progress since setting awaiting_iret_completion, we've * executed an IRET and can allow NMI injection. @@ -4552,11 +4574,14 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu) static void svm_cancel_injection(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); - struct vmcb_control_area *control = &svm->vmcb->control; - control->exit_int_info = control->event_inj; - control->exit_int_info_err = control->event_inj_err; - control->event_inj = 0; + /* + * EXITINTINFO is stale as it holds the event from the previous #VMEXIT + * (or from the last time the current VMCB was run). Invalidate it so + * that svm_complete_interrupts() requeues if and only if KVM staged an + * event in EVENTINJ. + */ + svm->vmcb->control.exit_int_info = 0; svm_complete_interrupts(vcpu); } -- 2.43.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [Patch v6 1/3] KVM: SVM: Re-queue events that were never injected 2026-12-05 22:34 ` [Patch v6 1/3] KVM: SVM: Re-queue events that were never injected Pratik R. Sampat @ 2026-12-05 22:59 ` sashiko-bot 0 siblings, 0 replies; 7+ messages in thread From: sashiko-bot @ 2026-12-05 22:59 UTC (permalink / raw) To: Pratik R. Sampat, kvm Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] KVM: SVM: force_immediate_exit self-IPI aborts stalled ESMTP VMRUN in a loop - [Low] KVM: SVM: svm_complete_interrupts() clobbers VMCB exit_int_info and event_inj - [Low] KVM: SVM: inaccurate EVENTINJ clear comment in svm_complete_interrupts() -- commit 9b4d36c2e80540d699ee7d2ba230910d0fc05661 Author: Pratik R. Sampat <prsampat@amd.com> KVM: SVM: Re-queue events that were never injected This commit updates svm_complete_interrupts() to migrate a non-zero control->event_inj into control->exit_int_info so undelivered events are re-queued on pre-entry #VMEXITs, and folds svm_cancel_injection() into it. > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index 2dd05cd6b7571..1d45bcc04112c 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c [ ... ] > @@ -4489,6 +4490,27 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu) > svm->nmi_l1_to_l2 = false; > svm->soft_int_injected = false; > > + /* > + * Hardware clears EVENTINJ field when it injects an event. > + * A non-empty EVENTINJ on #VMEXIT means the vCPU never entered guest > + * mode, and thus that the event was never delivered. Migrate the event > + * to EXITINTINFO so that it's requeued instead of being dropped. [Severity: Low] As reported by Sean Christopherson on v1 [1], is this comment accurate about when hardware clears EVENTINJ? Per the AMD APM and commit 69b721a86d0d ("KVM: nSVM: Clear EVENTINJ fields in vmcb12 on nested #VMEXIT"), hardware clears EVENTINJ unconditionally on #VMEXIT out of guest mode, not when it injects an event; pre-entry exits leave EVENTINJ intact. > + * > + * An undelivered event doesn't imply a fatal VMEXIT_INVALID. With > + * SMT Protection and Enhanced SMT Protection, VMRUN may exit with an > + * ordinary #VMEXIT without having injected that event into the guest. > + * > + * Clobbering EXITINTINFO is safe precisely because the vCPU never > + * entered guest mode. > + */ > + if (control->event_inj) { > + control->exit_int_info = control->event_inj; > + control->exit_int_info_err = control->event_inj_err; > + control->event_inj = 0; > + > + exitintinfo = control->exit_int_info; > + } [Severity: Low] Does overwriting control->exit_int_info, control->exit_int_info_err, and control->event_inj in place corrupt the VMCB fields before svm_handle_exit() runs? On a failed VMRUN (SVM_EXIT_ERR), dump_vmcb() then logs event_inj as 0 and reports the staged event under exit_int_info, obscuring VMRUN failure diagnosis. svm_get_exit_info() also reports the synthetic exit_int_info to downstream callers such as trace_kvm_nested_vmexit(). Passing local exitintinfo and exitintinfo_err values to a helper, similar to __vmx_complete_interrupts(), would avoid mutating the VMCB fields. [Severity: Medium] As reported by Sean Christopherson on v1 [2], how is KVM_RUN_FORCE_IMMEDIATE_EXIT supposed to work with ESMTP? On an SEV-SNP guest (where AVIC is inhibited), two pending injectable events make kvm_check_and_inject_events() set *req_immediate_exit, so svm_vcpu_run() sends a physical self-IPI via smp_send_reschedule() before VMRUN. With ESMTP enabled, that pending self-IPI immediately aborts a stalled VMRUN with SVM_EXIT_INTR before guest entry. svm_complete_interrupts() then requeues control->event_inj and raises KVM_REQ_EVENT, so every retry queues another self-IPI. The vCPU can never wait at the ESMTP synchronization point for its sibling: it spins firing self-IPIs while the sibling is in host mode, and if both siblings have KVM_RUN_FORCE_IMMEDIATE_EXIT set, they livelock in repeated SVM_EXIT_INTR exits. [1] https://lore.kernel.org/r/arFYHRzNFT886BME@google.com [2] https://lore.kernel.org/r/arEuMojZa_kv9VXA@google.com -- Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796503783.git.prsampat@amd.com?part=1 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [Patch v6 2/3] KVM: SVM: Add host support for SMT Protection 2026-12-05 22:34 [Patch v6 0/3] Host support for SMT and Enhanced SMT Protection Pratik R. Sampat 2026-12-05 22:34 ` [Patch v6 1/3] KVM: SVM: Re-queue events that were never injected Pratik R. Sampat @ 2026-12-05 22:34 ` Pratik R. Sampat 2026-12-05 23:03 ` sashiko-bot 2026-12-05 22:35 ` [Patch v6 3/3] KVM: SVM: Add host support for Enhanced " Pratik R. Sampat 2 siblings, 1 reply; 7+ messages in thread From: Pratik R. Sampat @ 2026-12-05 22:34 UTC (permalink / raw) To: x86, kvm, linux-doc, linux-kernel Cc: tglx, mingo, bp, dave.hansen, haiyangz, wei.liu, seanjc, pbonzini, thomas.lendacky, kim.phillips, nikunj, michael.roth, ashish.kalra, prsampat SMT Protection (SMTP) protects an SEV-SNP guest for ZEN 4+ systems from SMT side channels by requiring that, while one of its vCPUs is in guest mode, every SMT sibling thread on that physical core is HLT idle in host mode. Hardware enforces this at VMRUN. If a sibling thread is not idle, VMRUN does not enter guest mode and exits with the new VMEXIT_IDLE_REQUIRED (exit code -3). The exit is not fatal: resume the vCPU and let VMRUN be retried. Any event staged in EVENTINJ was not delivered in that case, and svm_complete_interrupts() requeues it. While the vCPU runs, its sibling is held in the idle state. If the idle sibling receives an interrupt, hardware first kicks the vCPU's thread out of guest mode by writing the value of the IDLE_WAKEUP_ICR MSR to the APIC ICR, and only then lets the sibling leave the idle state to service the interrupt. Program IDLE_WAKEUP_ICR on each CPU with an IPI to its SMT sibling when virtualization is enabled on that CPU. Refer to the AMD64 APM Vol 2, section "Side-Channel Protection". Signed-off-by: Pratik R. Sampat <prsampat@amd.com> --- v5..v6: * New patch - Introduce SMT protection --- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/svm.h | 1 + arch/x86/include/uapi/asm/svm.h | 4 +++- arch/x86/kvm/svm/sev.c | 30 +++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.c | 5 +++++ arch/x86/kvm/svm/svm.h | 2 ++ 7 files changed, 42 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 303467a498b8..4840e90ba3c4 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -472,6 +472,7 @@ #define X86_FEATURE_DEBUG_SWAP (19*32+14) /* "debug_swap" SEV-ES full debug state swap support */ #define X86_FEATURE_RMPREAD (19*32+21) /* RMPREAD instruction */ #define X86_FEATURE_SEGMENTED_RMP (19*32+23) /* Segmented RMP support */ +#define X86_FEATURE_SMT_PROT (19*32+25) /* SMT Protection */ #define X86_FEATURE_ALLOWED_SEV_FEATURES (19*32+27) /* Allowed SEV Features */ #define X86_FEATURE_SVSM (19*32+28) /* "svsm" SVSM present */ #define X86_FEATURE_HV_INUSE_WR_ALLOWED (19*32+30) /* Allow Write to in-use hypervisor-owned pages */ diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index 82182af9140f..1980f6d29788 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -772,6 +772,7 @@ #define MSR_AMD64_SEG_RMP_ENABLED_BIT 0 #define MSR_AMD64_SEG_RMP_ENABLED BIT_ULL(MSR_AMD64_SEG_RMP_ENABLED_BIT) #define MSR_AMD64_RMP_SEGMENT_SHIFT(x) (((x) & GENMASK_ULL(13, 8)) >> 8) +#define MSR_AMD64_IDLE_WAKEUP_ICR 0xc0010137 #define MSR_AMD64_RMPOPT_BASE 0xc0010139 #define MSR_AMD64_RMPOPT_ENABLE_BIT 0 #define MSR_AMD64_RMPOPT_ENABLE BIT_ULL(MSR_AMD64_RMPOPT_ENABLE_BIT) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index aceb405449a5..a2331f5e533c 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -314,6 +314,7 @@ static_assert((X2AVIC_4K_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AV #define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4) #define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) #define SVM_SEV_FEAT_SECURE_TSC BIT(9) +#define SVM_SEV_FEAT_SMTP BIT(15) #define VMCB_ALLOWED_SEV_FEATURES_VALID BIT_ULL(63) diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index e80676185092..f9f972177b08 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -136,6 +136,7 @@ #define SVM_EXIT_SW 0xf0000000ull #define SVM_EXIT_ERR -1ull +#define SVM_EXIT_IDLE_REQUIRED -3ull /* SMT Protection: sibling not idle */ #define SVM_EXIT_REASONS \ { SVM_EXIT_READ_CR0, "read_cr0" }, \ @@ -248,7 +249,8 @@ { SVM_VMGEXIT_EXT_GUEST_REQUEST, "vmgexit_ext_guest_request" }, \ { SVM_VMGEXIT_AP_CREATION, "vmgexit_ap_creation" }, \ { SVM_VMGEXIT_HV_FEATURES, "vmgexit_hypervisor_feature" }, \ - { SVM_EXIT_ERR, "invalid_guest_state" } + { SVM_EXIT_ERR, "invalid_guest_state" }, \ + { SVM_EXIT_IDLE_REQUIRED, "idle_required" } #endif /* _UAPI__SVM_H */ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index b90c5f14c1ac..ad802211ec4f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -520,7 +520,7 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp, return -EINVAL; if (!snp_active) - valid_vmsa_features &= ~SVM_SEV_FEAT_SECURE_TSC; + valid_vmsa_features &= ~(SVM_SEV_FEAT_SECURE_TSC | SVM_SEV_FEAT_SMTP); if (data->vmsa_features & ~valid_vmsa_features) return -EINVAL; @@ -3283,6 +3283,9 @@ void __init sev_hardware_setup(void) if (sev_snp_enabled && tsc_khz && cpu_feature_enabled(X86_FEATURE_SNP_SECURE_TSC)) sev_supported_vmsa_features |= SVM_SEV_FEAT_SECURE_TSC; + + if (sev_snp_enabled && cpu_feature_enabled(X86_FEATURE_SMT_PROT)) + sev_supported_vmsa_features |= SVM_SEV_FEAT_SMTP; } void sev_hardware_unsetup(void) @@ -3314,6 +3317,31 @@ int sev_cpu_init(struct svm_cpu_data *sd) return 0; } +void sev_enable_idle_wakeup_icr(void) +{ + unsigned int cpu, sibling, shift; + u32 core; + + if (!(sev_supported_vmsa_features & SVM_SEV_FEAT_SMTP)) + return; + + cpu = raw_smp_processor_id(); + shift = topology_get_domain_shift(TOPO_CORE_DOMAIN); + core = per_cpu(x86_cpu_to_apicid, cpu) >> shift; + + for_each_present_cpu(sibling) { + u32 apicid = per_cpu(x86_cpu_to_apicid, sibling); + u64 icr; + + if (sibling == cpu || (apicid >> shift) != core) + continue; + + icr = (u64)apicid << 32; + icr |= LOCAL_TIMER_VECTOR; + WARN_ON_ONCE(wrmsrq_safe(MSR_AMD64_IDLE_WAKEUP_ICR, icr)); + } +} + /* * Pages used by hardware to hold guest encrypted state must be flushed before * returning them to the system. diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 8dbbc6beb74a..7183c33a51ed 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -603,6 +603,8 @@ static int svm_enable_virtualization_cpu(void) __svm_write_tsc_multiplier(SVM_TSC_RATIO_DEFAULT); } + sev_enable_idle_wakeup_icr(); + svm_init_os_visible_workarounds(); svm_init_erratum_383(); @@ -3870,6 +3872,9 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) return 0; } + if (svm->vmcb->control.exit_code == SVM_EXIT_IDLE_REQUIRED) + return 1; + if (exit_fastpath != EXIT_FASTPATH_NONE) return 1; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index cd3242811e43..62139f49c2d1 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -1021,6 +1021,7 @@ void __init sev_set_cpu_caps(void); void __init sev_hardware_setup(void); void sev_hardware_unsetup(void); int sev_cpu_init(struct svm_cpu_data *sd); +void sev_enable_idle_wakeup_icr(void); int sev_dev_get_attr(u32 group, u64 attr, u64 *val); extern unsigned int max_sev_asid; void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); @@ -1049,6 +1050,7 @@ static inline void __init sev_set_cpu_caps(void) {} static inline void __init sev_hardware_setup(void) {} static inline void sev_hardware_unsetup(void) {} static inline int sev_cpu_init(struct svm_cpu_data *sd) { return 0; } +static inline void sev_enable_idle_wakeup_icr(void) {} static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXIO; } #define max_sev_asid 0 static inline void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) {} -- 2.43.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [Patch v6 2/3] KVM: SVM: Add host support for SMT Protection 2026-12-05 22:34 ` [Patch v6 2/3] KVM: SVM: Add host support for SMT Protection Pratik R. Sampat @ 2026-12-05 23:03 ` sashiko-bot 0 siblings, 0 replies; 7+ messages in thread From: sashiko-bot @ 2026-12-05 23:03 UTC (permalink / raw) To: Pratik R. Sampat, kvm Thank you for your contribution! Sashiko AI review found 8 potential issues to consider: - [High] KVM: SVM: sev_enable_idle_wakeup_icr() uses x2APIC ICR encoding on xAPIC hosts - [High] KVM: SVM: svm_vcpu_run() drops TLB and VMCB state on SVM_EXIT_IDLE_REQUIRED - [Medium] KVM: SVM: spurious LAPIC timer IRQs from sev_enable_idle_wakeup_icr() - [Medium] KVM: SVM: unbounded retry on SVM_EXIT_IDLE_REQUIRED in svm_handle_exit() - [Medium] KVM: SVM: stale exit_int_info not cleared on SVM_EXIT_IDLE_REQUIRED - [Low] KVM: SVM: 32-bit exit_code truncation breaks SVM_EXIT_IDLE_REQUIRED tracing - [Low] kvm: svm: svm_complete_interrupts() clobbers VMCB exit_int_info and event_inj - [Low] kvm: svm: inaccurate EVENTINJ clear comment in svm_complete_interrupts() -- commit 1620d819054cdfe726666bc9ca1d59aff6c97c0e Author: Pratik R. Sampat <prsampat@amd.com> KVM: SVM: Add host support for SMT Protection This commit adds host KVM support for AMD SEV-SNP SMT Protection (SMTP), programming MSR_AMD64_IDLE_WAKEUP_ICR when virtualization is enabled on a CPU and retrying VMRUN on SVM_EXIT_IDLE_REQUIRED exits. [ ... ] > diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h > index e806761850921..f9f972177b082 100644 > --- a/arch/x86/include/uapi/asm/svm.h > +++ b/arch/x86/include/uapi/asm/svm.h > @@ -136,6 +136,7 @@ > #define SVM_EXIT_SW 0xf0000000ull > > #define SVM_EXIT_ERR -1ull > +#define SVM_EXIT_IDLE_REQUIRED -3ull /* SMT Protection: sibling not idle */ > > #define SVM_EXIT_REASONS \ > { SVM_EXIT_READ_CR0, "read_cr0" }, \ > @@ -248,7 +249,8 @@ > { SVM_VMGEXIT_EXT_GUEST_REQUEST, "vmgexit_ext_guest_request" }, \ > { SVM_VMGEXIT_AP_CREATION, "vmgexit_ap_creation" }, \ > { SVM_VMGEXIT_HV_FEATURES, "vmgexit_hypervisor_feature" }, \ > - { SVM_EXIT_ERR, "invalid_guest_state" } > + { SVM_EXIT_ERR, "invalid_guest_state" }, \ > + { SVM_EXIT_IDLE_REQUIRED, "idle_required" } [Severity: Low] Can trace_kvm_exit() ever match this SVM_EXIT_REASONS entry? svm_get_exit_info() truncates control->exit_code (-3ull) to a u32 (0xfffffffd), and TRACE_EVENT_KVM_EXIT stores exit_reason as an unsigned int. When passed to __print_symbolic_u64(), it zero-extends to 0x00000000fffffffdULL, which never matches 0xfffffffffffffffdULL, so traces always print raw hex instead of "idle_required". > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > index b90c5f14c1ac9..ad802211ec4f7 100644 > --- a/arch/x86/kvm/svm/sev.c > +++ b/arch/x86/kvm/svm/sev.c [ ... ] > @@ -3314,6 +3317,31 @@ int sev_cpu_init(struct svm_cpu_data *sd) > return 0; > } > > +void sev_enable_idle_wakeup_icr(void) > +{ > + unsigned int cpu, sibling, shift; > + u32 core; > + > + if (!(sev_supported_vmsa_features & SVM_SEV_FEAT_SMTP)) > + return; > + > + cpu = raw_smp_processor_id(); > + shift = topology_get_domain_shift(TOPO_CORE_DOMAIN); > + core = per_cpu(x86_cpu_to_apicid, cpu) >> shift; > + > + for_each_present_cpu(sibling) { > + u32 apicid = per_cpu(x86_cpu_to_apicid, sibling); > + u64 icr; > + > + if (sibling == cpu || (apicid >> shift) != core) > + continue; > + > + icr = (u64)apicid << 32; [Severity: High] Does sev_enable_idle_wakeup_icr() work on hosts running in legacy xAPIC mode? It unconditionally places apicid in bits 63:32 (x2APIC layout) without checking x2apic_mode, and sev_hardware_setup() advertises SVM_SEV_FEAT_SMTP regardless of APIC mode. In xAPIC mode, the 8-bit physical destination ID belongs in bits 63:56 (SET_XAPIC_DEST_FIELD(apicid) in ICR2) and bits 55:32 are reserved. On xAPIC hosts, the WRMSR either faults (leaving MSR_AMD64_IDLE_WAKEUP_ICR at 0 after WARN_ON_ONCE) or targets APIC ID 0 instead of the sibling, preventing hardware from kicking the guest vCPU out of guest mode and stalling the idle sibling from servicing interrupts. Format the destination according to x2apic_mode, or gate SVM_SEV_FEAT_SMTP on x2APIC mode. > + icr |= LOCAL_TIMER_VECTOR; > + WARN_ON_ONCE(wrmsrq_safe(MSR_AMD64_IDLE_WAKEUP_ICR, icr)); > + } > +} [Severity: Medium] Is LOCAL_TIMER_VECTOR safe to use as the wakeup IPI vector here? Every hardware idle-wakeup IPI will enter local_apic_timer_interrupt(), incrementing APIC_TIMER stats and invoking evt->event_handler(). In periodic or low-resolution tick modes (e.g. highres=off), tick_handle_periodic() unconditionally runs tick_periodic() without checking dev->next_event, prematurely advancing jiffies and charging bogus CPU ticks; even in high-resolution mode, every wakeup runs hrtimer_interrupt() and reprograms the LAPIC timer. Consider using a lightweight wakeup vector without timer side effects, such as RESCHEDULE_VECTOR or POSTED_INTR_VECTOR. > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index 1d45bcc04112c..6947447daa4bc 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c [ ... ] > @@ -3870,6 +3872,9 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) > return 0; > } > > + if (svm->vmcb->control.exit_code == SVM_EXIT_IDLE_REQUIRED) > + return 1; > + > if (exit_fastpath != EXIT_FASTPATH_NONE) > return 1; [Severity: High] Does svm_vcpu_run() drop pending TLB flushes, ERAPS clears, and dirty VMCB state when VMRUN aborts with SVM_EXIT_IDLE_REQUIRED? In svm_vcpu_run(), the post-VMRUN cleanup of flush_all_asids, vmcb->control.tlb_ctl, ERAP_CONTROL_CLEAR_RAP, and vmcb_mark_all_clean() is guarded only by !svm_is_vmrun_failure(svm->vmcb->control.exit_code), which only matches SVM_EXIT_ERR. Because SVM_EXIT_IDLE_REQUIRED aborts before entering guest mode, hardware is not guaranteed to have performed those VMCB control actions, as established by commits 60d93c27859f ("KVM: SVM: Preserve TLB control (i.e. pending TLB flush) on failed VMRUN") and e27fc915c8f5 ("KVM: SVM: Update control fields on #VMEXIT if and only if VMRUN succeeded"). On retry, pre_sev_run() and vcpu_enter_guest() have already updated sd->sev_vmcbs[asid] and last_vmentry_cpu, so cleared per-ASID and pCPU-wide TLB flushes are permanently lost, risking stale TLB translations across guests and host. [Severity: Medium] Can returning 1 unconditionally on SVM_EXIT_IDLE_REQUIRED cause a 100% CPU mutual livelock? If two SMTP-enabled vCPUs are scheduled on sibling threads of the same physical core, neither sibling is HLT-idle, so both vCPUs spin in vcpu_run() -> vcpu_enter_guest() -> VMRUN -> SVM_EXIT_IDLE_REQUIRED indefinitely with zero guest forward progress. Neither CFS SMT balancing (smt_balance() returns false when no idle core exists) nor standard per-VM SCHED_CORE (where vCPUs of the same VM share a cookie and are co-scheduled together in pick_next_task()) separates them. Consider adding backoff, yielding, or scheduler coordination on repeated SVM_EXIT_IDLE_REQUIRED exits. [Severity: Medium] Can svm_complete_interrupts() re-queue a stale event from vmcb->control.exit_int_info after SVM_EXIT_IDLE_REQUIRED? If hardware leaves exit_int_info untouched on a pre-entry abort like SVM_EXIT_IDLE_REQUIRED, svm_vcpu_run() calling svm_complete_interrupts() with control->event_inj == 0 will read the stale exit_int_info from a prior exit. For example, if an AP exits with SVM_EXITINTINFO_VALID set and is then reset via SVM_VMGEXIT_AP_CREATE (kvm_vcpu_reset() -> init_vmcb() does not clear exit_int_info), a subsequent SVM_EXIT_IDLE_REQUIRED with event_inj == 0 spuriously re-queues the pre-reset event into the freshly reset AP. Clearing vmcb->control.exit_int_info (or calling svm_cancel_injection()) on pre-entry VMRUN aborts would avoid reading stale EXITINTINFO. [Severity: Low] In commit "KVM: SVM: Re-queue events that were never injected", does svm_complete_interrupts() clobber control->exit_int_info and control->event_inj in the VMCB before svm_handle_exit() inspects them? svm_vcpu_run() calls svm_complete_interrupts() before svm_handle_exit(). When control->event_inj is non-zero, overwriting control->exit_int_info and zeroing control->event_inj in the VMCB causes dump_vmcb() on SVM_EXIT_ERR to report event_inj as 0 and misreport the staged event under exit_int_info (and trace_kvm_nested_vmexit() to log the overwritten value). Using local variables instead of mutating control->exit_int_info in the VMCB would preserve the diagnostic state. [Severity: Low] As reported by Sean Christopherson on v1 [1], the comment added to svm_complete_interrupts() in commit "KVM: SVM: Re-queue events that were never injected" inaccurately states that hardware clears EVENTINJ when it injects an event. Hardware actually clears EVENTINJ unconditionally on #VMEXIT when exiting guest mode (as documented in shutdown_interception()), whereas pre-entry exits abort VMRUN before entering guest mode and leave EVENTINJ intact. [1] https://lore.kernel.org/r/arFYHRzNFT886BME@google.com -- Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796503783.git.prsampat@amd.com?part=2 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [Patch v6 3/3] KVM: SVM: Add host support for Enhanced SMT Protection 2026-12-05 22:34 [Patch v6 0/3] Host support for SMT and Enhanced SMT Protection Pratik R. Sampat 2026-12-05 22:34 ` [Patch v6 1/3] KVM: SVM: Re-queue events that were never injected Pratik R. Sampat 2026-12-05 22:34 ` [Patch v6 2/3] KVM: SVM: Add host support for SMT Protection Pratik R. Sampat @ 2026-12-05 22:35 ` Pratik R. Sampat 2026-12-05 23:00 ` sashiko-bot 2 siblings, 1 reply; 7+ messages in thread From: Pratik R. Sampat @ 2026-12-05 22:35 UTC (permalink / raw) To: x86, kvm, linux-doc, linux-kernel Cc: tglx, mingo, bp, dave.hansen, haiyangz, wei.liu, seanjc, pbonzini, thomas.lendacky, kim.phillips, nikunj, michael.roth, ashish.kalra, prsampat Enhanced SMT Protection (ESMTP) protects an SEV-SNP guest for ZEN 5+ systems from SMT side channels by requiring that, while a vCPU is in guest mode, its SMT sibling thread is either idle in host mode or executing a vCPU the guest has declared to be a legal sibling. A legal sibling is another ESMTP vCPU of the same guest that carries the same VCPU_SIBLING_MASK and agrees on VCPU_ID outside that mask. Hardware enforces this at VMRUN, which no longer enters guest mode right away but stalls until the condition holds. Three new exits report that it could not be met, all of them non-fatal: ESMTP_ILLSIB: a sibling thread is running a vCPU that is not a legal sibling of this one. ESMTP_TIMEOUT: the stall exceeded the timeout programmed in the VMCB. The timer bounds how long VMRUN waits for the core to settle. A module parameter exposes an interface to program this timer. ESMTP_RETRY: If DBREQ is detected, VMRUN exits. ESMTP_RETRY indicates an internal event and the hypervisor should execute a VMRUN. ESMTP synchronizes exits as well: when one thread leaves guest mode, hardware kicks any sibling that is still running guest code by writing the IDLE_WAKEUP_ICR MSR value to the APIC ICR. Program IDLE_WAKEUP_ICR when ESMTP is supported, as is already done for SMT Protection. ESMTP and SMT Protection are mutually exclusive, and VMRUN fails with VMEXIT_INVALID if both are set, so reject KVM_SEV_INIT2 requests that ask for both. Refer to the AMD64 APM Vol 2, section "AMD64 Enhanced SMT Protection". Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com> Acked-by: Borislav Petkov (AMD) <bp@alien8.de> # non-virtualization changes Signed-off-by: Pratik R. Sampat <prsampat@amd.com> --- v5..v6: * Move IDLE_WAKEUP_ICR programming to patch 2 since SMT protection also depends on it --- .../admin-guide/kernel-parameters.txt | 19 +++++++++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/svm.h | 4 ++- arch/x86/include/uapi/asm/svm.h | 9 +++++- arch/x86/kernel/cpu/scattered.c | 1 + arch/x86/kvm/svm/sev.c | 32 +++++++++++++++++-- arch/x86/kvm/svm/svm.c | 6 +++- 7 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 515028e4ac46..2d9b3cbe32b9 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -3260,6 +3260,25 @@ Kernel parameters max_snp_asid == min_sev_asid-1, will effectively make SEV-ES unusable. + kvm-amd.esmtp_timeout_ns= + [KVM,AMD] Enhanced SMT Protection (ESMTP) VMRUN + timeout, in nanoseconds, for SEV-SNP guests that enable + ESMTP. When an ESMTP vCPU executes VMRUN, hardware + waits for every sibling thread to either be idle or + run a legal sibling vCPU of the same guest before + entering guest mode. If the wait exceeds this timeout, + VMRUN exits with VMEXIT_ESMTP_TIMEOUT and KVM retries + the VMRUN. + + The value is converted to TSC cycles and programmed + into the VMCB when a vCPU is initialized, so changing + it at runtime only affects vCPUs that are created or + reset afterwards. + + Default is 0, which disables the timeout, i.e. VMRUN + waits until the sibling condition is met or a physical + interrupt arrives. + kvm-arm.mode= [KVM,ARM,EARLY] Select one of KVM/arm64's modes of operation. diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 4840e90ba3c4..359bbcf3b61e 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -537,6 +537,7 @@ * and purposes if CLEAR_CPU_BUF_VM is set). */ #define X86_FEATURE_X2AVIC_EXT (21*32+20) /* AMD SVM x2AVIC support for 4k vCPUs */ +#define X86_FEATURE_AMD_ESMTP (21*32+21) /* AMD Enhanced SMT Protection */ /* * BUG word(s) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index a2331f5e533c..84122c7e7c50 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -165,7 +165,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area { u8 reserved_9[22]; u64 allowed_sev_features; /* Offset 0x138 */ u64 guest_sev_features; /* Offset 0x140 */ - u8 reserved_10[128]; + u64 esmtp_timeout; /* Offset 0x148 */ + u8 reserved_10[120]; u64 pml_addr; /* Offset 0x1c8 */ u16 pml_index; /* Offset 0x1d0 */ u8 reserved_11[526]; @@ -315,6 +316,7 @@ static_assert((X2AVIC_4K_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AV #define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) #define SVM_SEV_FEAT_SECURE_TSC BIT(9) #define SVM_SEV_FEAT_SMTP BIT(15) +#define SVM_SEV_FEAT_ESMTP BIT(17) #define VMCB_ALLOWED_SEV_FEATURES_VALID BIT_ULL(63) diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index f9f972177b08..66faf87fb5c2 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -137,6 +137,10 @@ #define SVM_EXIT_ERR -1ull #define SVM_EXIT_IDLE_REQUIRED -3ull /* SMT Protection: sibling not idle */ +/* Enhanced SMT Protection VM exits taken during VMRUN sibling sync */ +#define SVM_EXIT_ESMTP_ILLSIB -5ull /* Illegal ESMTP sibling */ +#define SVM_EXIT_ESMTP_TIMEOUT -6ull /* ESMTP_TIMEOUT expired */ +#define SVM_EXIT_ESMTP_RETRY -7ull /* Internal event; retry VMRUN */ #define SVM_EXIT_REASONS \ { SVM_EXIT_READ_CR0, "read_cr0" }, \ @@ -250,7 +254,10 @@ { SVM_VMGEXIT_AP_CREATION, "vmgexit_ap_creation" }, \ { SVM_VMGEXIT_HV_FEATURES, "vmgexit_hypervisor_feature" }, \ { SVM_EXIT_ERR, "invalid_guest_state" }, \ - { SVM_EXIT_IDLE_REQUIRED, "idle_required" } + { SVM_EXIT_IDLE_REQUIRED, "idle_required" }, \ + { SVM_EXIT_ESMTP_ILLSIB, "esmtp_illsib" }, \ + { SVM_EXIT_ESMTP_TIMEOUT, "esmtp_timeout" }, \ + { SVM_EXIT_ESMTP_RETRY, "esmtp_retry" } #endif /* _UAPI__SVM_H */ diff --git a/arch/x86/kernel/cpu/scattered.c b/arch/x86/kernel/cpu/scattered.c index c990245d29d6..6d0cf3bee31b 100644 --- a/arch/x86/kernel/cpu/scattered.c +++ b/arch/x86/kernel/cpu/scattered.c @@ -71,6 +71,7 @@ static const struct cpuid_bit cpuid_bits[] = { { X86_FEATURE_AMD_LBR_V2, CPUID_EAX, 1, 0x80000022, 0 }, { X86_FEATURE_AMD_LBR_PMC_FREEZE, CPUID_EAX, 2, 0x80000022, 0 }, { X86_FEATURE_RMPOPT, CPUID_EDX, 0, 0x80000025, 0 }, + { X86_FEATURE_AMD_ESMTP, CPUID_EDX, 1, 0x80000025, 0 }, { X86_FEATURE_AMD_HTR_CORES, CPUID_EAX, 30, 0x80000026, 0 }, { 0, 0, 0, 0, 0 } }; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index ad802211ec4f..915acb2c973e 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -67,6 +67,11 @@ module_param_named(sev_snp, sev_snp_enabled, bool, 0444); static unsigned int __ro_after_init nr_ciphertext_hiding_asids; module_param_named(ciphertext_hiding_asids, nr_ciphertext_hiding_asids, uint, 0444); +static unsigned int esmtp_timeout_ns; +module_param(esmtp_timeout_ns, uint, 0644); +MODULE_PARM_DESC(esmtp_timeout_ns, + "ESMTP VMRUN sibling-wait timeout in nanoseconds (default: 0, timer disabled)"); + #define AP_RESET_HOLD_NONE 0 #define AP_RESET_HOLD_NAE_EVENT 1 #define AP_RESET_HOLD_MSR_PROTO 2 @@ -520,11 +525,17 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp, return -EINVAL; if (!snp_active) - valid_vmsa_features &= ~(SVM_SEV_FEAT_SECURE_TSC | SVM_SEV_FEAT_SMTP); + valid_vmsa_features &= ~(SVM_SEV_FEAT_SECURE_TSC | + SVM_SEV_FEAT_ESMTP | SVM_SEV_FEAT_SMTP); if (data->vmsa_features & ~valid_vmsa_features) return -EINVAL; + /* VMRUN fails with VMEXIT_INVALID if both are set. */ + if ((data->vmsa_features & SVM_SEV_FEAT_SMTP) && + (data->vmsa_features & SVM_SEV_FEAT_ESMTP)) + return -EINVAL; + if (data->ghcb_version > GHCB_VERSION_MAX || (!es_active && data->ghcb_version)) return -EINVAL; @@ -1035,6 +1046,15 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->sev_features = sev->vmsa_features; + if (sev->vmsa_features & SVM_SEV_FEAT_ESMTP) { + save->vcpu_id = vcpu->vcpu_id; + /* + * All vCPUs of a guest are put into one group and can be run + * co-resident on the sibling thread of the same core. + */ + save->vcpu_sibling_mask = U32_MAX; + } + /* * Skip FPU and AVX setup with KVM_SEV_ES_INIT to avoid * breaking older measurements. @@ -3286,6 +3306,9 @@ void __init sev_hardware_setup(void) if (sev_snp_enabled && cpu_feature_enabled(X86_FEATURE_SMT_PROT)) sev_supported_vmsa_features |= SVM_SEV_FEAT_SMTP; + + if (sev_snp_enabled && cpu_feature_enabled(X86_FEATURE_AMD_ESMTP)) + sev_supported_vmsa_features |= SVM_SEV_FEAT_ESMTP; } void sev_hardware_unsetup(void) @@ -3322,7 +3345,8 @@ void sev_enable_idle_wakeup_icr(void) unsigned int cpu, sibling, shift; u32 core; - if (!(sev_supported_vmsa_features & SVM_SEV_FEAT_SMTP)) + if (!(sev_supported_vmsa_features & + (SVM_SEV_FEAT_SMTP | SVM_SEV_FEAT_ESMTP))) return; cpu = raw_smp_processor_id(); @@ -4865,6 +4889,10 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm, bool init_event) svm->vmcb->control.allowed_sev_features = sev->vmsa_features | VMCB_ALLOWED_SEV_FEATURES_VALID; + if (sev->vmsa_features & SVM_SEV_FEAT_ESMTP) + svm->vmcb->control.esmtp_timeout = mul_u64_u32_div(esmtp_timeout_ns, + tsc_khz, 1000000); + /* Can't intercept CR register access, HV can't modify CR registers */ svm_clr_intercept(svm, INTERCEPT_CR0_READ); svm_clr_intercept(svm, INTERCEPT_CR4_READ); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 7183c33a51ed..2556dd4b961f 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3872,8 +3872,12 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) return 0; } - if (svm->vmcb->control.exit_code == SVM_EXIT_IDLE_REQUIRED) + if (svm->vmcb->control.exit_code == SVM_EXIT_IDLE_REQUIRED || + svm->vmcb->control.exit_code == SVM_EXIT_ESMTP_RETRY || + svm->vmcb->control.exit_code == SVM_EXIT_ESMTP_ILLSIB || + svm->vmcb->control.exit_code == SVM_EXIT_ESMTP_TIMEOUT) { return 1; + } if (exit_fastpath != EXIT_FASTPATH_NONE) return 1; -- 2.43.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [Patch v6 3/3] KVM: SVM: Add host support for Enhanced SMT Protection 2026-12-05 22:35 ` [Patch v6 3/3] KVM: SVM: Add host support for Enhanced " Pratik R. Sampat @ 2026-12-05 23:00 ` sashiko-bot 0 siblings, 0 replies; 7+ messages in thread From: sashiko-bot @ 2026-12-05 23:00 UTC (permalink / raw) To: Pratik R. Sampat, kvm Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] KVM: SVM: sev_es_sync_vmsa() uses nonexistent sev_es_save_area fields - [High] KVM: SVM: svm_vcpu_run() clears TLB/VMCB state on ESMTP/SMTP aborts - [High] KVM: SVM: missing vcpu_id and vcpu_sibling_mask fields in sev_es_save_area - [Medium] KVM: SVM: sev_enable_idle_wakeup_icr() ignores xAPIC ICR destination format - [Medium] KVM: SVM: sev_enable_idle_wakeup_icr() misuses LOCAL_TIMER_VECTOR for kicks - [Medium] KVM: SVM: svm_complete_interrupts() may reuse stale exit_int_info on ESMTP - [Low] KVM: SVM: plain read of writable esmtp_timeout_ns in sev_es_init_vmcb() -- commit 44efccfc3cd4956b61f97b12386bb66eb255647a Author: Pratik R. Sampat <prsampat@amd.com> KVM: SVM: Add host support for Enhanced SMT Protection This commit adds host KVM/SVM support for AMD Enhanced SMT Protection (ESMTP) on SEV-SNP guests, including feature detection, VMCB timeout configuration via a module parameter, VMSA sibling metadata, IDLE_WAKEUP_ICR programming, and exit handling for the three new ESMTP VMRUN exits. > diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h > index 72466901d791a..e30eb48688ae1 100644 > --- a/arch/x86/include/asm/svm.h > +++ b/arch/x86/include/asm/svm.h > @@ -165,7 +165,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area { > u8 reserved_9[22]; > u64 allowed_sev_features; /* Offset 0x138 */ > u64 guest_sev_features; /* Offset 0x140 */ > - u8 reserved_10[128]; > + u64 esmtp_timeout; /* Offset 0x148 */ > + u8 reserved_10[120]; > u64 pml_addr; /* Offset 0x1c8 */ > u16 pml_index; /* Offset 0x1d0 */ > u8 reserved_11[526]; [Severity: High] Does struct sev_es_save_area in this header also need vcpu_id and vcpu_sibling_mask fields? Neither field is defined anywhere in struct sev_es_save_area, so compiling arch/x86/kvm/svm/sev.c with CONFIG_KVM_AMD_SEV=y fails when sev_es_sync_vmsa() assigns them. [ ... ] > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > index ad802211ec4f7..915acb2c973e5 100644 > --- a/arch/x86/kvm/svm/sev.c > +++ b/arch/x86/kvm/svm/sev.c [ ... ] > @@ -1035,6 +1046,15 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) > > save->sev_features = sev->vmsa_features; > > + if (sev->vmsa_features & SVM_SEV_FEAT_ESMTP) { > + save->vcpu_id = vcpu->vcpu_id; > + /* > + * All vCPUs of a guest are put into one group and can be run > + * co-resident on the sibling thread of the same core. > + */ > + save->vcpu_sibling_mask = U32_MAX; > + } > + > /* > * Skip FPU and AVX setup with KVM_SEV_ES_INIT to avoid > * breaking older measurements. [Severity: High] Will this build with CONFIG_KVM_AMD_SEV enabled? save is a struct sev_es_save_area *, which does not have vcpu_id or vcpu_sibling_mask members in arch/x86/include/asm/svm.h. Adding those fields at their architectural VMSA offsets will also require updating the reserved padding and the compile-time size and offset assertions in __unused_size_checks(). [ ... ] > @@ -3322,7 +3345,8 @@ void sev_enable_idle_wakeup_icr(void) > unsigned int cpu, sibling, shift; > u32 core; > > - if (!(sev_supported_vmsa_features & SVM_SEV_FEAT_SMTP)) > + if (!(sev_supported_vmsa_features & > + (SVM_SEV_FEAT_SMTP | SVM_SEV_FEAT_ESMTP))) > return; > > cpu = raw_smp_processor_id(); [Severity: Medium] Does sev_enable_idle_wakeup_icr() handle hosts running in legacy xAPIC mode (!x2apic_enabled())? Later in this function, the ICR value is built unconditionally in x2APIC format: arch/x86/kvm/svm/sev.c:sev_enable_idle_wakeup_icr() { ... icr = (u64)apicid << 32; icr |= LOCAL_TIMER_VECTOR; WARN_ON_ONCE(wrmsrq_safe(MSR_AMD64_IDLE_WAKEUP_ICR, icr)); } In xAPIC mode, the 8-bit physical destination APIC ID lives in bits 63:56 (SET_XAPIC_DEST_FIELD(apicid) in APIC_ICR2) while bits 55:32 are reserved. When hardware writes MSR_AMD64_IDLE_WAKEUP_ICR directly to the APIC ICR on a synchronized ESMTP exit, an xAPIC host will send the wakeup IPI to APIC ID 0 instead of the SMT sibling, breaking sibling kick synchronization and delivering spurious timer IPIs to CPU 0. Formatting the destination according to x2apic_enabled() (or gating SMTP/ESMTP support on x2apic_enabled() if x2APIC is required by hardware) would avoid this. [Severity: Medium] Is LOCAL_TIMER_VECTOR safe to program into MSR_AMD64_IDLE_WAKEUP_ICR for ESMTP sibling kicks? Every synchronized ESMTP sibling VM-exit will deliver a spurious local APIC timer interrupt to the sibling CPU via sysvec_apic_timer_interrupt() -> local_apic_timer_interrupt(). On hosts in periodic or low-resolution tick mode (highres=off or !CONFIG_HIGH_RES_TIMERS), tick_handle_periodic() and tick_nohz_lowres_handler() unconditionally advance jiffies and charge process time on each delivery. Even with high-resolution timers enabled, every sibling kick runs the full hrtimer_interrupt() path (acquiring cpu_base->lock, walking hrtimer queues, and reprogramming the hardware LAPIC timer) and increments APIC_TIMER stats. Can this use RESCHEDULE_VECTOR instead, which KVM already uses via sysvec_reschedule_ipi() to force a CPU out of guest mode without touching timers? [ ... ] > @@ -4865,6 +4889,10 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm, bool init_event) > svm->vmcb->control.allowed_sev_features = sev->vmsa_features | > VMCB_ALLOWED_SEV_FEATURES_VALID; > > + if (sev->vmsa_features & SVM_SEV_FEAT_ESMTP) > + svm->vmcb->control.esmtp_timeout = mul_u64_u32_div(esmtp_timeout_ns, > + tsc_khz, 1000000); > + > /* Can't intercept CR register access, HV can't modify CR registers */ > svm_clr_intercept(svm, INTERCEPT_CR0_READ); > svm_clr_intercept(svm, INTERCEPT_CR4_READ); [Severity: Low] Should this read use READ_ONCE(esmtp_timeout_ns)? esmtp_timeout_ns is a 0644 module parameter updated via sysfs under kernel_param_lock, so an unmarked plain read here during vCPU creation, reset, or migration races with sysfs writes and triggers a KCSAN data-race report. > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index 6947447daa4bc..affd0e7982d54 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c > @@ -3872,8 +3872,12 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) > return 0; > } > > - if (svm->vmcb->control.exit_code == SVM_EXIT_IDLE_REQUIRED) > + if (svm->vmcb->control.exit_code == SVM_EXIT_IDLE_REQUIRED || > + svm->vmcb->control.exit_code == SVM_EXIT_ESMTP_RETRY || > + svm->vmcb->control.exit_code == SVM_EXIT_ESMTP_ILLSIB || > + svm->vmcb->control.exit_code == SVM_EXIT_ESMTP_TIMEOUT) { > return 1; > + } > > if (exit_fastpath != EXIT_FASTPATH_NONE) > return 1; [Severity: High] Does svm_vcpu_run() drop pending TLB/ERAP flushes and VMCB dirty bits when VMRUN aborts with one of these non-entry ESMTP or SMTP exit codes? svm_vcpu_run() guards its post-VMRUN cleanup only with !svm_is_vmrun_failure(), which matches SVM_EXIT_ERR alone: arch/x86/kvm/svm/svm.c:svm_vcpu_run() { ... if (!svm_is_vmrun_failure(svm->vmcb->control.exit_code)) { this_cpu_ptr(&svm_data)->flush_all_asids = false; svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; if (cpu_feature_enabled(X86_FEATURE_ERAPS)) svm->vmcb->control.erap_ctl &= ~ERAP_CONTROL_CLEAR_RAP; vmcb_mark_all_clean(svm->vmcb); ... } Because VMRUN never entered guest mode on SVM_EXIT_IDLE_REQUIRED or SVM_EXIT_ESMTP_*, hardware does not guarantee that pending TLB flushes, RAP clears, or VMCB clean-bit updates were performed. When svm_handle_exit() returns 1 to retry VMRUN, those flushes are not re-armed (for example, pre_sev_run() skips TLB_CONTROL_FLUSH_ASID because sd->sev_vmcbs[asid] and last_vmentry_cpu were already updated), which can leave stale NPT/ASID TLB translations or stale cached VMCB state active on guest entry. Using a separate predicate in svm_vcpu_run() that covers all non-entry VMRUN exit codes would preserve pending flushes and VMCB dirty bits across retries. [Severity: Medium] Can svm_complete_interrupts() re-inject a stale event from control->exit_int_info on a non-entry ESMTP or SMTP VMRUN abort? If hardware leaves VMCB control.exit_int_info unmodified when VMRUN aborts before guest entry (as with SVM_EXIT_ERR), and control->event_inj is zero on that run (for instance after userspace cleared an injected event via KVM_SET_VCPU_EVENTS, or after a prior X86_TRAP_VC exit), svm_complete_interrupts() skips its event_inj overwrite and re-processes the stale SVM_EXITINTINFO_VALID entry from the previous VM-exit. svm_cancel_injection() already zeroes control->exit_int_info for this reason, so clearing it before VMRUN or on pre-entry aborts would prevent resurrecting old interrupts, NMIs, or exceptions. -- Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796503783.git.prsampat@amd.com?part=3 ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-05 23:03 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-05 22:34 [Patch v6 0/3] Host support for SMT and Enhanced SMT Protection Pratik R. Sampat 2026-12-05 22:34 ` [Patch v6 1/3] KVM: SVM: Re-queue events that were never injected Pratik R. Sampat 2026-12-05 22:59 ` sashiko-bot 2026-12-05 22:34 ` [Patch v6 2/3] KVM: SVM: Add host support for SMT Protection Pratik R. Sampat 2026-12-05 23:03 ` sashiko-bot 2026-12-05 22:35 ` [Patch v6 3/3] KVM: SVM: Add host support for Enhanced " Pratik R. Sampat 2026-12-05 23:00 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox