KVM: SVM: Bump asid_generation on CPU online to avoid ASID collision after hotplug
PATCH 1/1 KVM: SVM: Bump asid_generation on CPU online to avoid ASID collision after hotplug
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.
--- arch/x86/kvm/svm/svm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +6 -1
@@ -571,7 +571,12 @@ static int svm_enable_virtualization_cpu(void)return r; sd =per_cpu_ptr (&svm_data, me); - sd->asid_generation =1 ; +/* +* Bump the current asid_generation value to ensure any vCPU that +* previously ran on this CPU sees a stale generation and is forced +* to acquire a new ASID, preventing a latent ASID collision. +*/ + sd->asid_generation++; sd->max_asid =cpuid_ebx (SVM_CPUID_FUNC ) -1 ; sd->next_asid = sd->max_asid +1 ; sd->min_asid = max_sev_asid +1 ;
Signature · 1 line
2.48.1
Quoted 54 lines · Nikunj A Dadhania
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()resetsasid_generationto 1 and setsnext_asidtomax_asid+ 1 on every CPU online event, including hotplug cycles. Becausenext_asidstarts beyond the pool boundary, the first call tonew_asid()after an online event always wraps the pool, incrementingasid_generationto 2 and assigning ASIDs starting frommin_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_generationresets 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 frommin_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->cpuis 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_generationinstead of resetting it to 1 insvm_enable_virtualization_cpu(). On module load,asid_generationstarts 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 inpre_svm_run()reliably forcesnew_asid()on every vCPU after every hotplug cycle.Fixes 774c47f1d78e ("[PATCH] KVM: cpu hotplug support")Reported-by Chandrakanth Silveru <[email protected]>Tested-by Srikanth Aithal <[email protected]>Reviewed-by K Prateek Nayak <[email protected]>Reviewed-by Tom Lendacky <[email protected]>Signed-off-by Nikunj A Dadhania <[email protected]>
Quoted 3 lines
--- arch/x86/kvm/svm/svm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
- Naveen
Quoted 60 lines · Naveen N Rao
Quoted 54 lines · Nikunj A Dadhania
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()resetsasid_generationto 1 and setsnext_asidtomax_asid+ 1 on every CPU online event, including hotplug cycles. Becausenext_asidstarts beyond the pool boundary, the first call tonew_asid()after an online event always wraps the pool, incrementingasid_generationto 2 and assigning ASIDs starting frommin_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_generationresets 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 frommin_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->cpuis 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_generationinstead of resetting it to 1 insvm_enable_virtualization_cpu(). On module load,asid_generationstarts 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 inpre_svm_run()reliably forcesnew_asid()on every vCPU after every hotplug cycle.Fixes 774c47f1d78e ("[PATCH] KVM: cpu hotplug support")Reported-by Chandrakanth Silveru <[email protected]>Tested-by Srikanth Aithal <[email protected]>Reviewed-by K Prateek Nayak <[email protected]>Reviewed-by Tom Lendacky <[email protected]>Signed-off-by Nikunj A Dadhania <[email protected]>Quoted 3 lines
--- arch/x86/kvm/svm/svm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 2 lines
Reviewed-by Naveen N Rao (AMD) <[email protected]>
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
Quoted 49 lines · Nikunj A Dadhania
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()resetsasid_generationto 1 and setsnext_asidtomax_asid+ 1 on every CPU online event, including hotplug cycles. Becausenext_asidstarts beyond the pool boundary, the first call tonew_asid()after an online event always wraps the pool, incrementingasid_generationto 2 and assigning ASIDs starting frommin_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_generationresets 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 frommin_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->cpuis 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_generationinstead of resetting it to 1 insvm_enable_virtualization_cpu(). On module load,asid_generationstarts 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 inpre_svm_run()reliably forcesnew_asid()on every vCPU after every hotplug cycle.
Quoted 6 lines
Fixes 774c47f1d78e ("[PATCH] KVM: cpu hotplug support")Reported-by Chandrakanth Silveru <[email protected]>Tested-by Srikanth Aithal <[email protected]>Reviewed-by K Prateek Nayak <[email protected]>Reviewed-by Tom Lendacky <[email protected]>Signed-off-by Nikunj A Dadhania <[email protected]>
Quoted 48 lines · Nikunj A Dadhania
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()resetsasid_generationto 1 and setsnext_asidtomax_asid+ 1 on every CPU online event, including hotplug cycles. Becausenext_asidstarts beyond the pool boundary, the first call tonew_asid()after an online event always wraps the pool, incrementingasid_generationto 2 and assigning ASIDs starting frommin_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_generationresets 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 frommin_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->cpuis 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_generationinstead of resetting it to 1 insvm_enable_virtualization_cpu(). On module load,asid_generationstarts 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 inpre_svm_run()reliably forcesnew_asid()on every vCPU after every hotplug cycle.
Quoted 6 lines
Fixes 774c47f1d78e ("[PATCH] KVM: cpu hotplug support")Reported-by Chandrakanth Silveru <[email protected]>Tested-by Srikanth Aithal <[email protected]>Reviewed-by K Prateek Nayak <[email protected]>Reviewed-by Tom Lendacky <[email protected]>Signed-off-by Nikunj A Dadhania <[email protected]>
Queued, thanks.
Paolo
Quoted 22 lines
--- arch/x86/kvm/svm/svm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)arch/x86/kvm/svm/svm.c +6
-1@@ -571,7 +571,12 @@ static int svm_enable_virtualization_cpu(void)return r; sd =per_cpu_ptr (&svm_data, me); - sd->asid_generation =1 ; +/* +* Bump the current asid_generation value to ensure any vCPU that +* previously ran on this CPU sees a stale generation and is forced +* to acquire a new ASID, preventing a latent ASID collision. +*/ + sd->asid_generation++; sd->max_asid =cpuid_ebx (SVM_CPUID_FUNC ) -1 ; sd->next_asid = sd->max_asid +1 ; sd->min_asid = max_sev_asid +1 ;