Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: "Nikunj A. Dadhania" <nikunj@amd.com>
To: Naveen N Rao <naveen@kernel.org>, <seanjc@google.com>
Cc: <kvm@vger.kernel.org>, <pbonzini@redhat.com>, <yosry@kernel.org>,
	Chandrakanth Silveru <Chandrakanth.Silveru@amd.com>,
	Srikanth Aithal <Srikanth.Aithal@amd.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	"Tom Lendacky" <thomas.lendacky@amd.com>
Subject: Re: [PATCH] KVM: SVM: Bump asid_generation on CPU online to avoid ASID collision after hotplug
Date: Thu, 16 Jul 2026 10:48:35 +0530	[thread overview]
Message-ID: <d43b969e-beca-4dc8-a9e6-d236294e734a@amd.com> (raw)
In-Reply-To: <aldBh9GummZYUayT@blrnaveerao1>



On 7/15/2026 1:46 PM, Naveen N Rao wrote:
> On Wed, Jul 15, 2026 at 06:35:06AM +0000, Nikunj A Dadhania wrote:
>> If a vCPU stays scheduled out (or blocked) while the last pCPU it ran
>> on goes through a hotplug cycle (online->offline->online), and the vCPU
>> then resumes execution on the same pCPU, then it is possible for it to
>> run with an ASID that has now been assigned to a different vCPU,
>> resulting in stale TLB translations being used.
>>
>> svm_enable_virtualization_cpu() resets asid_generation to 1 and sets
>> next_asid to max_asid + 1 on every CPU online event, including hotplug
>> cycles.  Because next_asid starts beyond the pool boundary, the first
>> call to new_asid() after an online event always wraps the pool,
>> incrementing asid_generation to 2 and assigning ASIDs starting from
>> min_asid.
>>
>> Consider two vCPUs from different VMs, vCPU-A pinned to CPU-X holding
>> asid_generation=2 and ASID=N from before the hotplug event:
>>
>>   1. CPU-X goes offline and back online: asid_generation resets to 1,
>>      next_asid = max_asid + 1.
>>
>>   2. One or more vCPUs migrate to CPU-X and call new_asid(), wrapping
>>      the pool and consuming ASIDs starting from min_asid.  Eventually
>>      vCPU-B from a different VM is assigned asid_generation=2, ASID=N
>>      — the same ASID that vCPU-A held before the hotplug.
>>
>>   3. vCPU-A enters pre_svm_run() on CPU-X: current_vmcb->cpu is
>>      unchanged so the migration branch is skipped.  Its saved
>>      asid_generation=2 matches sd->asid_generation=2, so the generation
>>      check silently passes and vCPU-A continues running with ASID=N —
>>      the same ASID just freshly assigned to vCPU-B.
>>
>> Both vCPUs from different VMs now run on CPU-X with the same ASID,
>> causing them to share NPT TLB entries and producing stale translations.
>>
>> The collision manifests as a KVM internal error (Suberror: 1, emulation
>> failure).  The NPT page fault reports a faulting GPA far outside the
>> VM's physical memory range — a sign of stale TLB translations being
>> used.  KVM falls back to instruction emulation, which fails on
>> FPU/XSave instructions (XRSTOR, STMXCSR) that the emulator does not
>> implement.
>>
>> Fix this by incrementing asid_generation instead of resetting it to 1
>> in svm_enable_virtualization_cpu().  On module load, asid_generation
>> starts at 0 (memset) and the increment produces 1, identical to the
>> old behaviour.  On subsequent hotplug cycles the generation advances
>> beyond any value a vCPU previously observed on this CPU, so the
>> generation check in pre_svm_run() reliably forces new_asid() on every
>> vCPU after every hotplug cycle.
>>
>> Fixes: 774c47f1d78e ("[PATCH] KVM: cpu hotplug support")
>> Reported-by: Chandrakanth Silveru <Chandrakanth.Silveru@amd.com>
>> Tested-by: Srikanth Aithal <Srikanth.Aithal@amd.com>
>> Reviewed-by: K Prateek Nayak <kprateek.nayak@amd.com>
>> Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
>> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
>> ---
>>  arch/x86/kvm/svm/svm.c | 7 ++++++-
>>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> 
> Cc: stable@vger.kernel.org
> Reviewed-by: Naveen N Rao (AMD) <naveen@kernel.org>

Thanks for the review!

On the Cc: stable@ — if a v2 becomes necessary I will add it.
Otherwise, Sean, could you please add it when applying?

Regards,
Nikunj

  reply	other threads:[~2026-07-16  5:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-15  6:35 [PATCH] KVM: SVM: Bump asid_generation on CPU online to avoid ASID collision after hotplug Nikunj A Dadhania
2026-07-15  8:16 ` Naveen N Rao
2026-07-16  5:18   ` Nikunj A. Dadhania [this message]
2026-07-15 19:01 ` Yosry Ahmed
2026-07-16  7:24 ` Paolo Bonzini

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=d43b969e-beca-4dc8-a9e6-d236294e734a@amd.com \
    --to=nikunj@amd.com \
    --cc=Chandrakanth.Silveru@amd.com \
    --cc=Srikanth.Aithal@amd.com \
    --cc=kprateek.nayak@amd.com \
    --cc=kvm@vger.kernel.org \
    --cc=naveen@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=thomas.lendacky@amd.com \
    --cc=yosry@kernel.org \
    /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