Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pratik R. Sampat" <prsampat@amd.com>, kvm@vger.kernel.org
Subject: Re: [Patch v6 2/3] KVM: SVM: Add host support for SMT Protection
Date: Sat, 05 Dec 2026 23:03:33 +0000	[thread overview]
Message-ID: <sashiko-outbox-251977@kernel.org> (raw)
In-Reply-To: <06a05ba32a3e94f24fdf18a67be390f4f05b0582.1796503783.git.prsampat@amd.com>

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

  reply	other threads:[~2026-12-05 23:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

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=sashiko-outbox-251977@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=prsampat@amd.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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