RFC Optimize nSVM TLB flushes
Get series with b4
b4 am [email protected]Versions newest first
- v2 RFC Latest25 patches 64 repliesIn review
Changes in v2 from the cover letter
- Changed from a static ASID per VM to a static ASID per vCPU, dropping the needed per-CPU ASID -> vCPU tracking and dropping all SEV changes.
- Fixed a missing flush in the current code when forcefully leaving nested (patch 1).
- Expose more ASIDs to L1 (patch 2), otherwise the usefulness of this series is limited when L1 runs a bunch of L2s.
- Drop passing is_guest_mode around (past me, EW!).
- Reuse more of kvm_mmu_invlpg() for INVLPGA emulation.
- Fix INVLPGA emulation in case L1 runs on a different CPU than L2 (but L2 never switches its CPU).
- Resync the nested NPT when L1 changes L2's ASID (missed in RFC v1).
- Shuffled some patches around, combined/split patches differently, and rewrote some changlogs.
Patches in v2 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/25 | KVM: nSVM: Flush the TLB after forcefully leaving nestedarch/ | +2 | 0 replies | |
| 2/25 | KVM: SVM: Passthrough the number of supported ASIDsarch/ | +0 | 23 replies | |
| 3/25 | KVM: VMX: Generalize VPID allocation to be vendor-neutralarch/ arch/ arch/ +1 more | +105 | 3 replies | |
| 4/25 | KVM: x86/mmu: Support specifying a minimum TLB tagarch/ arch/ arch/ | +26 | 3 replies | |
| 5/25 | KVM: SVM: Add helpers to set/clear ASID flush in VMCBarch/ arch/ arch/ +1 more | +14 | 0 replies | |
| 6/25 | KVM: SVM: Fallback to flush everything if FLUSHBYASID is not availablearch/ arch/ | +5 | 1 reply | |
| 7/25 | KVM: SVM: Duplicate pre-run ASID check for SEV and non-SEV guestsarch/ arch/ | +10 | 0 replies | |
| 8/25 | KVM: SEV: Stop using per-vCPU ASID for SEV VMsarch/ | +4 | 4 replies | |
| 9/25 | KVM: SVM: Use a static ASID per vCPUarch/ arch/ arch/ | +53 | 2 replies | |
| 10/25 | KVM: nSVM: Add a placeholder ASID for L2arch/ arch/ | +5 | 0 replies | |
| 11/25 | KVM: x86: hyper-v: Rename kvm_hv_vcpu_purge_flush_tlb()arch/ arch/ arch/ | +4 | 0 replies | |
| 12/25 | KVM: x86: hyper-v: Allow puring all TLB flush FIFOsarch/ | +20 | 0 replies | |
| 13/25 | KVM: nSVM: Flush both L1 and L2 ASIDs on KVM_REQ_TLB_FLUSHarch/ arch/ arch/ | +13 | 2 replies | |
| 14/25 | KVM: nSVM: Move svm_switch_vmcb() to nested.carch/ arch/ arch/ | +9 | 0 replies | |
| 15/25 | KVM: nSVM: Call nested_svm_transition_tlb_flush() on every VMCB switcharch/ | +4 | 2 replies | |
| 16/25 | KVM: nSVM: Split nested_svm_transition_tlb_flush() into entry/exit fnsarch/ | +13 | 0 replies | |
| 17/25 | KVM: nSVM: Service local TLB flushes before nested transitionsarch/ | +7 | 6 replies | |
| 18/25 | KVM: nSVM: Handle nested TLB flush requests through TLB_CONTROLarch/ arch/ | +27 | 0 replies | |
| 19/25 | KVM: nSVM: Flush the TLB if L1 changes L2's ASID in vmcb12arch/ arch/ | +18 | 0 replies | |
| 20/25 | KVM: nSVM: Do not reset TLB_CONTROL in vmcb02 on nested VM-Enterarch/ | +1 | 0 replies | |
| 21/25 | KVM: x86/mmu: rename __kvm_mmu_invalidate_addr()arch/ | +5 | 0 replies | |
| 22/25 | KVM: x86/mmu: Refactor kvm_mmu_invlpg() to allow skipping the gva flusharch/ | +17 | 12 replies | |
| 23/25 | KVM: nSVM: Flush L2's ASID when emulating INVLPGAarch/ arch/ arch/ | +44 | 0 replies | |
| 24/25 | KVM: nSVM: Use different ASIDs for L1 and L2arch/ | +3 | 6 replies | |
| 25/25 | DO NOT MERGE: Add nested_tlb_force_flusharch/ | +15 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.
Cover letter [RFC PATCH v2 00/25] Optimize nSVM TLB flushes
A second RFC series (or third) for optimizing TLB flushes for nested SVM, over a year later. This series optimizes TLB flushes 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 for vCPU (or two if using nested), brining it further closer to how VMX handles VPIDs. Modern CPUs (checked Milan, Genoa, and Turin) advertise 32K ASIDs, so there is no point.
The series is mostly in a good shape, but it is an RFC because I only did basic testing (booted an L2 and an L3, run netperf in L2). I am mainly sending this out to get some feedback on the code while I do more extensive testing. Ideally I would test this with a memory overcommitted VM in L0 (to trigger KVM unmapping and flushing guest memory) and a CPU overcommitted VM in L1 (to trigger L1 KVM multiplexing multiple L2 ASIDs on the same L1 vCPU) -- let's see if AI can help with this.
Admittedly, the SEV and HV bits only got build tested, and I doubt that I would be able to do more than this going forward.
Since over a year passed and the code changed non-trivially, I dropped all Reviewed-by tags (sorry Maxim!), even on patches that are relatively the same. I won't go too much into the details for what changed because over a year has passed, but I will describe the main changes.
Breakdown:
- Patch 1 is a bug fix that can be taken separately.
- Patches 2-9 implement the change to use static ASIDs per vCPU for SVM, and can also be taken separately if needed.
- The rest of the patches (up to patch 24) add an L2 ASID and proper handling for it, leading to ultimately dropping the unconditional flushes on nested transitions.
- Patch 25 is purely for testing convenience. It adds a module param that adds back the unconditional flushes. Although if we are really paranoid we can keep it for a while? Idk if we usually do that.
RFC v1 -> RFC v2:
- Changed from a static ASID per VM to a static ASID per vCPU, dropping the needed per-CPU ASID -> vCPU tracking and dropping all SEV changes.
- Fixed a missing flush in the current code when forcefully leaving nested (patch 1).
- Expose more ASIDs to L1 (patch 2), otherwise the usefulness of this series is limited when L1 runs a bunch of L2s.
- Drop passing is_guest_mode around (past me, EW!).
- Reuse more of kvm_mmu_invlpg() for INVLPGA emulation.
- Fix INVLPGA emulation in case L1 runs on a different CPU than L2 (but L2 never switches its CPU).
- Resync the nested NPT when L1 changes L2's ASID (missed in RFC v1).
- Shuffled some patches around, combined/split patches differently, and rewrote some changlogs.
RFC v1: https://lore.kernel.org/lkml/[email protected]/
Yosry Ahmed (25):
KVM: nSVM: Flush the TLB after forcefully leaving nested
KVM: SVM: Passthrough the number of supported ASIDs
KVM: VMX: Generalize VPID allocation to be vendor-neutral
KVM: x86/mmu: Support specifying a minimum TLB tag
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: Stop using per-vCPU ASID for SEV VMs
KVM: SVM: Use a static ASID per 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: Flush both L1 and L2 ASIDs on KVM_REQ_TLB_FLUSH
KVM: nSVM: Move svm_switch_vmcb() to nested.c
KVM: nSVM: Call nested_svm_transition_tlb_flush() on every VMCB switch
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()
KVM: x86/mmu: Refactor kvm_mmu_invlpg() to allow skipping the gva
flush
KVM: nSVM: Flush L2's ASID when emulating INVLPGA
KVM: nSVM: Use different ASIDs for L1 and L2
DO NOT MERGE: Add nested_tlb_force_flush
arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/cpuid.c | 2 - arch/x86/kvm/hyperv.h | 33 ++++++-- arch/x86/kvm/mmu.h | 8 ++ arch/x86/kvm/mmu/mmu.c | 106 +++++++++++++++++++++--- arch/x86/kvm/svm/nested.c | 106 ++++++++++++++++++------ arch/x86/kvm/svm/sev.c | 12 ++- arch/x86/kvm/svm/svm.c | 137 ++++++++++++++++++++------------ arch/x86/kvm/svm/svm.h | 41 +++++++--- arch/x86/kvm/vmx/vmx.c | 40 +++------- arch/x86/kvm/vmx/vmx.h | 29 ++++++- arch/x86/kvm/x86.c | 2 +- 12 files changed, 377 insertions(+), 142 deletions(-)
base-commit: c1f7303302927f9cbf4efedf70f0512cde168c65
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog