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 3/3] KVM: SVM: Add host support for Enhanced SMT Protection
Date: Sat, 05 Dec 2026 23:00:49 +0000	[thread overview]
Message-ID: <sashiko-outbox-251974@kernel.org> (raw)
In-Reply-To: <faf50c7c33e58394b725c5528961dae8335c9536.1796503783.git.prsampat@amd.com>

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

      reply	other threads:[~2026-12-05 23:00 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
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 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=sashiko-outbox-251974@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