From: Yosry Ahmed <yosry@kernel.org>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Jim Mattson <jmattson@google.com>,
Maxim Levitsky <mlevitsk@redhat.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Tom Lendacky <thomas.lendacky@amd.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Yosry Ahmed <yosry@kernel.org>
Subject: [PATCH v1 00/28] KVM: nSVM: Optimize nSVM TLB flushes
Date: Tue, 28 Jul 2026 00:35:29 +0000 [thread overview]
Message-ID: <20260728003557.1136583-1-yosry@kernel.org> (raw)
This series optimizes TLB flushes for nested SVM by using and maintaining
a separate ASID for L2 for each vCPU (similar to VMX's handling of VPIDs),
rather than sharing the same ASID and flushing everything on every nested
transition.
The series also drops the dynamic ASID allocation scheme and uses a
static ASID per vCPU (or two if using nested), bringing it closer to how
VMX handles VPIDs. Modern CPUs (Rome, Milan, Genoa, Turin) advertise 32K
ASIDs, so there is no need for dynamic allocation.
The SEV and Hyper-V bits only got build tested.
Otherwise, this series was tested in two ways:
1. A new selftest:
- Tests most basic scenarios where L1 flushes the TLB for L2, as well
as when KVM triggers a TLB flush for both L1 and L2.
- Fails reliably when injecting manual TLB bugs (e.g. commenting out a
flush).
- Caught an actual bug when I was rebasing the series on top of
Paolo's MMU structures rework.
- Works for both SVM and VMX.
- Was used to repro a separate nVMX TLB bug [1].
2. An AI generated script (800 lines of bash that I don't dare to read):
- Runs 10 L1 VMs, each with 4 L2 VMs inside.
- Each L2 VM runs a workload that writes to all memory and then
validates its content.
- L2 VMs overcommit both memory and CPU in L1 (exercise L1-induced TLB
flushes as well as L2 ASID switching).
- L0 periodically reclaims memory from L1 VMs proactively (exercise
L0-induced TLB flushes).
- Fails semi-reliably when injecting manual TLB bugs.
- Caugh an actual bug when testing the fallback ASID logic with
synthetically reduced number of ASIDs.
Overall, I think I am happy with the testing I put this series through,
hence dropping the RFC tag at last.
Breakdown:
- Patch 1 is a bug fix that can be taken separately.
- Patches 2-11 implement the change to use static ASIDs per vCPU for
SVM (including SEV-specific handling and fallback ASID optimization),
and can also be taken separately if needed.
- Patches 12-27 add the L2 ASID and proper handling for it, leading to
dropping the unconditional flushes on nested transitions.
- Patch 28 adds the nested TLB flush selftest.
RFC v2 -> v1:
- Dropped the RFC tag.
- Document KVM's handling of number of ASIDs in CPUID rather than
increase the number of advertised ASIDs [Jim, Sean].
- Explicitly support reserved TLB tags rather than a minuimum TLB tag
[Sean].
- Set an upper bound on the number of TLB tags [Sean].
- Allow reserving all ASIDs to allow allocating all ASIDs to SEV (if at
all possible). [Sashiko].
- Actually handle TLB flushes for nested on SEV [Sean, Sashiko].
- Optimize fallback ASID flushes by tracking the last vCPU using it on
each pCPU instead of flushing it on every VMRUN [Sean].
- Drop svm->nested.initialized and properly NULLify the vmcb02 pointer
instead (and use it).
- Make sure servicing local TLB flushes is done when both the VMCB and
guest_mode are consistent [Sashiko].
- Always initialize last_asid to 0 to flush the TLB on the first use of
L2's ASID [Sashiko].
- Open-code TLB flush handling on nested VM-Enter and VM-Exit instead of
burying it in svm_switch_vmcb() [Sean].
- Always flush the hardware ASID on nested transitions if L1 and L2
share it (e.g. when using the fallback ASID, or for SEV guests) to
prevent TLB entry leakage between L1 and L2.
- Rename __kvm_mmu_invalidate_addr() to kvm_mmu_sync_addr() [Sean].
RFC v1: https://lore.kernel.org/lkml/20250326193619.3714986-1-yosry.ahmed@linux.dev/
RFC v2: https://lore.kernel.org/kvm/20260616004155.1435766-1-yosry@kernel.org/
[1]https://lore.kernel.org/kvm/20260722230128.1587363-1-yosry@kernel.org/
Yosry Ahmed (28):
KVM: nSVM: Flush the TLB after forcefully leaving nested
KVM: SVM: Document number of ASIDs CPUID setting
KVM: VMX: Generalize VPID allocation to be vendor-neutral
KVM: x86/mmu: Support specifying reserved TLB tags
KVM: SVM: Add helpers to set/clear ASID flush in VMCB
KVM: SVM: Fallback to flush everything if FLUSHBYASID is not available
KVM: SVM: Duplicate pre-run ASID check for SEV and non-SEV guests
KVM: SEV: Do ASID initialization at VMCB initialization
KVM: SEV: Expose sev_get_asid() outside of sev.c
KVM: SVM: Use a static ASID per vCPU
KVM: SVM: Only flush the fallback ASID when used by a different vCPU
KVM: nSVM: Add a placeholder ASID for L2
KVM: x86: hyper-v: Rename kvm_hv_vcpu_purge_flush_tlb()
KVM: x86: hyper-v: Allow puring all TLB flush FIFOs
KVM: nSVM: Drop svm->nested.initialized
KVM: nSVM: Flush both L1 and L2 ASIDs on KVM_REQ_TLB_FLUSH
KVM: nSVM: Always switch VMCB before leaving guest mode
KVM: nSVM: Split nested_svm_transition_tlb_flush() into entry/exit fns
KVM: nSVM: Service local TLB flushes before nested transitions
KVM: nSVM: Handle nested TLB flush requests through TLB_CONTROL
KVM: nSVM: Flush the TLB if L1 changes L2's ASID in vmcb12
KVM: nSVM: Do not reset TLB_CONTROL in vmcb02 on nested VM-Enter
KVM: x86/mmu: Rename __kvm_mmu_invalidate_addr() to
kvm_mmu_sync_addr()
KVM: x86/mmu: Refactor kvm_mmu_invlpg() to allow skipping the GVA
flush
KVM: nSVM: Flush L2's ASID when emulating INVLPGA
KVM: nSVM: Flush the ASID on nested transitions if shared by L1 and L2
KVM: nSVM: Use different ASIDs for L1 and L2
KVM: selftests: Add a test for nested TLB flushes
Documentation/virt/kvm/api.rst | 20 +
arch/x86/kvm/cpuid.c | 4 +-
arch/x86/kvm/hyperv.h | 33 +-
arch/x86/kvm/mmu.h | 11 +
arch/x86/kvm/mmu/mmu.c | 126 ++++-
arch/x86/kvm/svm/nested.c | 142 ++++--
arch/x86/kvm/svm/sev.c | 16 +-
arch/x86/kvm/svm/svm.c | 133 ++++--
arch/x86/kvm/svm/svm.h | 54 ++-
arch/x86/kvm/vmx/vmx.c | 40 +-
arch/x86/kvm/vmx/vmx.h | 28 +-
arch/x86/kvm/x86.c | 2 +-
tools/testing/selftests/kvm/Makefile.kvm | 1 +
tools/testing/selftests/kvm/include/x86/svm.h | 5 +
tools/testing/selftests/kvm/include/x86/vmx.h | 21 +
.../selftests/kvm/x86/nested_tlb_flush_test.c | 432 ++++++++++++++++++
16 files changed, 916 insertions(+), 152 deletions(-)
create mode 100644 tools/testing/selftests/kvm/x86/nested_tlb_flush_test.c
base-commit: 271255273d5ff348fe29d89fe4712b2f7f7907c3
--
2.55.0.229.g6434b31f56-goog
next reply other threads:[~2026-07-28 0:36 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-28 0:35 Yosry Ahmed [this message]
2026-07-28 0:35 ` [PATCH v1 01/28] KVM: nSVM: Flush the TLB after forcefully leaving nested Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 02/28] KVM: SVM: Document number of ASIDs CPUID setting Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 03/28] KVM: VMX: Generalize VPID allocation to be vendor-neutral Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 04/28] KVM: x86/mmu: Support specifying reserved TLB tags Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 05/28] KVM: SVM: Add helpers to set/clear ASID flush in VMCB Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 06/28] KVM: SVM: Fallback to flush everything if FLUSHBYASID is not available Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 07/28] KVM: SVM: Duplicate pre-run ASID check for SEV and non-SEV guests Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 08/28] KVM: SEV: Do ASID initialization at VMCB initialization Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 09/28] KVM: SEV: Expose sev_get_asid() outside of sev.c Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 10/28] KVM: SVM: Use a static ASID per vCPU Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 11/28] KVM: SVM: Only flush the fallback ASID when used by a different vCPU Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 12/28] KVM: nSVM: Add a placeholder ASID for L2 Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 13/28] KVM: x86: hyper-v: Rename kvm_hv_vcpu_purge_flush_tlb() Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 14/28] KVM: x86: hyper-v: Allow puring all TLB flush FIFOs Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 15/28] KVM: nSVM: Drop svm->nested.initialized Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 16/28] KVM: nSVM: Flush both L1 and L2 ASIDs on KVM_REQ_TLB_FLUSH Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 17/28] KVM: nSVM: Always switch VMCB before leaving guest mode Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 18/28] KVM: nSVM: Split nested_svm_transition_tlb_flush() into entry/exit fns Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 19/28] KVM: nSVM: Service local TLB flushes before nested transitions Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 20/28] KVM: nSVM: Handle nested TLB flush requests through TLB_CONTROL Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 21/28] KVM: nSVM: Flush the TLB if L1 changes L2's ASID in vmcb12 Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 22/28] KVM: nSVM: Do not reset TLB_CONTROL in vmcb02 on nested VM-Enter Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 23/28] KVM: x86/mmu: Rename __kvm_mmu_invalidate_addr() to kvm_mmu_sync_addr() Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 24/28] KVM: x86/mmu: Refactor kvm_mmu_invlpg() to allow skipping the GVA flush Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 25/28] KVM: nSVM: Flush L2's ASID when emulating INVLPGA Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 26/28] KVM: nSVM: Flush the ASID on nested transitions if shared by L1 and L2 Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 27/28] KVM: nSVM: Use different ASIDs for " Yosry Ahmed
2026-07-28 0:35 ` [PATCH v1 28/28] KVM: selftests: Add a test for nested TLB flushes Yosry Ahmed
2026-07-28 1:05 ` [PATCH v1 00/28] KVM: nSVM: Optimize nSVM " Yosry Ahmed
2026-07-28 1:18 ` Sean Christopherson
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=20260728003557.1136583-1-yosry@kernel.org \
--to=yosry@kernel.org \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mlevitsk@redhat.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=thomas.lendacky@amd.com \
--cc=vkuznets@redhat.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.