Skip to content

KVM: x86/mmu: TDX post-populate cleanups

Sean Christopherson kvm kvm-riscv kvmarm linux-arm-kernel linux-coco linux-mips linux-riscv linuxppc-dev lkml loongarch 4 versions – Applied

Status from this message: Applied to kvm-x86 tdx, with fixups for the various typos. Thanks for all the

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest28 patches 37 repliesApplied
  2. v325 patches 65 repliesSuperseded
  3. v2 RFC18 patches 44 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v1 RFC12 patches72 repliesSuperseded

Changes in v4 from the cover letter

- Collect reviews/acks.
- Add a lockdep assertion in kvm_tdp_mmu_map_private_pfn(). [Yan]
- Wrap kvm_tdp_mmu_map_private_pfn() with CONFIG_KVM_GUEST_MEMFD=y. [test bot]
- Improve (or add) comments. [Kai, and probably others]
- s/spte/mirror_spte to make it clear what's being passed in
- Update set_external_spte() to take @mirror_spte as well. [Yan]
- Move the KVM_BUG_ON() on tdh_mr_extend() failure to the end. [Rick]
- Take "all" the locks in tdx_vm_ioctl(). [Kai]
- WARN if KVM attempts to map SPTEs into an invalid root. [Yan]
- Use tdx_flush_vp_on_cpu() instead of tdx_disassociate_vp() when freeing a vCPU in VCPU_TD_STATE_UNINITIALIZED state. [Yan]

Patches in v4 Applied

#Subject and filesSizeRepliesApplied
1/28KVM: Make support for kvm_arch_vcpu_async_ioctl() mandatoryarch/arm64/kvm/arm.c arch/loongarch/kvm/Kconfig arch/mips/kvm/Kconfig +6 more+12 -180 replies
2/28KVM: Rename kvm_arch_vcpu_async_ioctl() to kvm_arch_vcpu_unlocked_ioctl()arch/arm64/kvm/arm.c arch/loongarch/kvm/vcpu.c arch/mips/kvm/mips.c +6 more+19 -190 replies
3/28KVM: TDX: Drop PROVE_MMU=y sanity check on to-be-populated mappingsarch/x86/kvm/vmx/tdx.c+0 -140 replies
4/28KVM: x86/mmu: Add dedicated API to map guest_memfd pfn into TDP MMUarch/x86/kvm/mmu.h arch/x86/kvm/mmu/mmu.c arch/x86/kvm/vmx/tdx.c+84 -81 reply
5/28KVM: x86/mmu: WARN if KVM attempts to map into an invalid TDP MMU rootarch/x86/kvm/mmu/tdp_mmu.c+2 -01 reply
6/28Revert "KVM: x86/tdp_mmu: Add a helper function to walk down the TDP MMU"arch/x86/kvm/mmu.h arch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/tdp_mmu.c+7 -360 replies
7/28KVM: x86/mmu: Rename kvm_tdp_map_page() to kvm_tdp_page_prefault()arch/x86/kvm/mmu/mmu.c+3 -30 replies
8/28KVM: TDX: Drop superfluous page pinning in S-EPT managementarch/x86/kvm/vmx/tdx.c+4 -242 replies
9/28KVM: TDX: Return -EIO, not -EINVAL, on a KVM_BUG_ON() conditionarch/x86/kvm/vmx/tdx.c+6 -61 reply
10/28KVM: TDX: Fold tdx_sept_drop_private_spte() into tdx_sept_remove_private_spte()arch/x86/kvm/vmx/tdx.c+40 -501 reply
11/28KVM: x86/mmu: Drop the return code from kvm_x86_ops.remove_external_spte()arch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu/tdp_mmu.c arch/x86/kvm/vmx/tdx.c+12 -171 reply
12/28KVM: TDX: WARN if mirror SPTE doesn't have full RWX when creating S-EPT mappingarch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu/tdp_mmu.c arch/x86/kvm/vmx/tdx.c+7 -44 replies
13/28KVM: TDX: Avoid a double-KVM_BUG_ON() in tdx_sept_zap_private_spte()arch/x86/kvm/vmx/tdx.c+4 -20 replies
14/28KVM: TDX: Use atomic64_dec_return() instead of a poor equivalentarch/x86/kvm/vmx/tdx.c+2 -40 replies
15/28KVM: TDX: Fold tdx_mem_page_record_premap_cnt() into its sole callerarch/x86/kvm/vmx/tdx.c+21 -280 replies
16/28KVM: TDX: ADD pages to the TD image while populating mirror EPT entriesarch/x86/kvm/vmx/tdx.c arch/x86/kvm/vmx/tdx.h+43 -711 reply
17/28KVM: TDX: Fold tdx_sept_zap_private_spte() into tdx_sept_remove_private_spte()arch/x86/kvm/vmx/tdx.c+11 -301 reply
18/28KVM: TDX: Combine KVM_BUG_ON + pr_tdx_error() into TDX_BUG_ON()arch/x86/kvm/vmx/tdx.c+46 -642 replies
19/28KVM: TDX: Derive error argument names from the local variable namesarch/x86/kvm/vmx/tdx.c+7 -61 reply
20/28KVM: TDX: Assert that mmu_lock is held for write when removing S-EPT entriesarch/x86/kvm/vmx/tdx.c+7 -02 replies
21/28KVM: TDX: Add macro to retry SEAMCALLs when forcing vCPUs out of guestarch/x86/kvm/vmx/tdx.c+33 -492 replies
22/28KVM: TDX: Add tdx_get_cmd() helper to get and validate sub-ioctl commandarch/x86/kvm/vmx/tdx.c+21 -141 reply
23/28KVM: TDX: Convert INIT_MEM_REGION and INIT_VCPU to "unlocked" vCPU ioctlarch/x86/include/asm/kvm-x86-ops.h arch/x86/include/asm/kvm_host.h arch/x86/kvm/vmx/main.c +3 more+55 -61 reply
24/28KVM: TDX: Use guard() to acquire kvm->lock in tdx_vm_ioctl()arch/x86/kvm/vmx/tdx.c+3 -61 reply
25/28KVM: TDX: Don't copy "cmd" back to userspace for KVM_TDX_CAPABILITIESarch/x86/kvm/vmx/tdx.c+3 -31 reply
26/28KVM: TDX: Guard VM state transitions with "all" the locksarch/x86/kvm/vmx/tdx.c+49 -104 replies
27/28KVM: TDX: Bug the VM if extending the initial measurement failsarch/x86/kvm/vmx/tdx.c+13 -113 replies
28/28KVM: TDX: Fix list_add corruption during vcpu_load()arch/x86/kvm/vmx/tdx.c+38 -51 reply

Reviews trailers per patch

All 28 patches have a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchClaudio ImbrendaIra WeinyKai HuangBinbin WuRick EdgecombeYan Zhao
SeriesT-b R-b T-b
1/28A-b
2/28A-b
3/28R-b R-b R-b
4/28R-b R-b R-b
5/28R-b
6/28R-b R-b R-b
7/28R-b R-b R-b
8/28R-b R-b R-b R-b
9/28R-b R-b R-b R-b
10/28R-b R-b
11/28R-b R-b
12/28R-b R-b
13/28R-b R-b R-b
14/28R-b R-b R-b
15/28R-b R-b R-b
16/28R-b R-b R-b
17/28R-b R-b R-b
18/28R-b R-b R-b
19/28R-b
20/28R-b R-b
21/28R-b R-b
22/28R-b R-b R-b
23/28R-b R-b
24/28R-b R-b R-b
25/28R-b
26/28R-b
27/28R-b R-b
28/28R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 6 reviewers.

Read the v4 thread

Cover letter [PATCH v4 00/28] KVM: x86/mmu: TDX post-populate cleanups

Non-x86 folks, as with v3, patches 1 and 2 are likely the only thing of interest here. They make kvm_arch_vcpu_async_ioctl() mandatory and then rename it to kvm_arch_vcpu_unlocked_ioctl().

As for the x86 side...

Clean up the TDX post-populate paths (and many tangentially related paths) to address locking issues between gmem and TDX's post-populate hook[*], and within KVM itself (KVM doesn't ensure full mutual exclusivity between paths that for all intents and purposes the TDX-Module requires to be serialized).

I apologize if I missed any trailers or feedback, I think I got everything...

[*] http://lore.kernel.org/all/[email protected]

v4:
- Collect reviews/acks.
- Add a lockdep assertion in kvm_tdp_mmu_map_private_pfn(). [Yan]
- Wrap kvm_tdp_mmu_map_private_pfn() with CONFIG_KVM_GUEST_MEMFD=y. [test bot]
- Improve (or add) comments. [Kai, and probably others]
- s/spte/mirror_spte to make it clear what's being passed in
- Update set_external_spte() to take @mirror_spte as well. [Yan]
- Move the KVM_BUG_ON() on tdh_mr_extend() failure to the end. [Rick]
- Take "all" the locks in tdx_vm_ioctl(). [Kai]
- WARN if KVM attempts to map SPTEs into an invalid root. [Yan]
- Use tdx_flush_vp_on_cpu() instead of tdx_disassociate_vp() when freeing a vCPU in VCPU_TD_STATE_UNINITIALIZED state. [Yan]

v3:
- https://lore.kernel.org/all/[email protected]
- Collect more reviews.
- Add the async_ioctl() => unlocked_ioctl() patches, and use the "unlocked" variant in the TDX vCPU sub-ioctls so they can take kvm->lock outside of vcpu->mutex.
- Add a patch to document that vcpu->mutex is taken *outside* kvm->slots_lock.
- Add the tdx_vm_state_guard CLASS() to take kvm->lock, all vcpu->mutex locks, and kvm->slots_lock, in order to make tdx_td_init(), tdx_td_finalize(), tdx_vcpu_init_mem_region(), and tdx_vcpu_init() mutually exclusive with each other, and mutually exclusvie with basically anything that can result in contending one of the TDX-Module locks (can't remember which one).
- Refine the changelog for the "Drop PROVE_MMU=y" patch. [Binbin]

v2:
- Collect a few reviews (and ignore some because the patches went away). [Rick, Kai, Ira]
- Move TDH_MEM_PAGE_ADD under mmu_lock and drop nr_premapped. [Yan, Rick]
- Force max_level = PG_LEVEL_4K straightaway. [Yan]
- s/kvm_tdp_prefault_page/kvm_tdp_page_prefault. [Rick]
- Use Yan's version of "Say no to pinning!". [Yan, Rick]
- Tidy up helpers and macros to reduce boilerplate and copy+pate code, and to eliminate redundant/dead code (e.g. KVM_BUG_ON() the same error multiple times).
- KVM_BUG_ON() if TDH_MR_EXTEND fails (I convinced myself it can't).

v1: https://lore.kernel.org/all/[email protected]

Sean Christopherson (26):
KVM: Make support for kvm_arch_vcpu_async_ioctl() mandatory
KVM: Rename kvm_arch_vcpu_async_ioctl() to
kvm_arch_vcpu_unlocked_ioctl()
KVM: TDX: Drop PROVE_MMU=y sanity check on to-be-populated mappings
KVM: x86/mmu: Add dedicated API to map guest_memfd pfn into TDP MMU
KVM: x86/mmu: WARN if KVM attempts to map into an invalid TDP MMU root Revert "KVM: x86/tdp_mmu: Add a helper function to walk down the TDP
MMU"
KVM: x86/mmu: Rename kvm_tdp_map_page() to kvm_tdp_page_prefault()
KVM: TDX: Return -EIO, not -EINVAL, on a KVM_BUG_ON() condition
KVM: TDX: Fold tdx_sept_drop_private_spte() into
tdx_sept_remove_private_spte()
KVM: x86/mmu: Drop the return code from

    kvm_x86_ops.remove_external_spte()

KVM: TDX: WARN if mirror SPTE doesn't have full RWX when creating
S-EPT mapping
KVM: TDX: Avoid a double-KVM_BUG_ON() in tdx_sept_zap_private_spte()
KVM: TDX: Use atomic64_dec_return() instead of a poor equivalent
KVM: TDX: Fold tdx_mem_page_record_premap_cnt() into its sole caller
KVM: TDX: ADD pages to the TD image while populating mirror EPT
entries
KVM: TDX: Fold tdx_sept_zap_private_spte() into
tdx_sept_remove_private_spte()
KVM: TDX: Combine KVM_BUG_ON + pr_tdx_error() into TDX_BUG_ON()
KVM: TDX: Derive error argument names from the local variable names
KVM: TDX: Assert that mmu_lock is held for write when removing S-EPT
entries
KVM: TDX: Add macro to retry SEAMCALLs when forcing vCPUs out of guest
KVM: TDX: Add tdx_get_cmd() helper to get and validate sub-ioctl
command
KVM: TDX: Convert INIT_MEM_REGION and INIT_VCPU to "unlocked" vCPU
ioctl
KVM: TDX: Use guard() to acquire kvm->lock in tdx_vm_ioctl()
KVM: TDX: Don't copy "cmd" back to userspace for KVM_TDX_CAPABILITIES
KVM: TDX: Guard VM state transitions with "all" the locks
KVM: TDX: Bug the VM if extending the initial measurement fails

Yan Zhao (2):
KVM: TDX: Drop superfluous page pinning in S-EPT management
KVM: TDX: Fix list_add corruption during vcpu_load()

 arch/arm64/kvm/arm.c               |   6 +
 arch/loongarch/kvm/Kconfig         |   1 -
 arch/loongarch/kvm/vcpu.c          |   4 +-
 arch/mips/kvm/Kconfig              |   1 -
 arch/mips/kvm/mips.c               |   4 +-
 arch/powerpc/kvm/Kconfig           |   1 -
 arch/powerpc/kvm/powerpc.c         |   4 +-
 arch/riscv/kvm/Kconfig             |   1 -
 arch/riscv/kvm/vcpu.c              |   4 +-
 arch/s390/kvm/Kconfig              |   1 -
 arch/s390/kvm/kvm-s390.c           |   4 +-
 arch/x86/include/asm/kvm-x86-ops.h |   1 +
 arch/x86/include/asm/kvm_host.h    |   7 +-
 arch/x86/kvm/mmu.h                 |   3 +-
 arch/x86/kvm/mmu/mmu.c             |  87 +++-
 arch/x86/kvm/mmu/tdp_mmu.c         |  50 +--
 arch/x86/kvm/vmx/main.c            |   9 +
 arch/x86/kvm/vmx/tdx.c             | 659 ++++++++++++++---------------
 arch/x86/kvm/vmx/tdx.h             |   8 +-
 arch/x86/kvm/vmx/x86_ops.h         |   1 +
 arch/x86/kvm/x86.c                 |  13 +
 include/linux/kvm_host.h           |  14 +-
 virt/kvm/Kconfig                   |   3 -
 virt/kvm/kvm_main.c                |   6 +-
 24 files changed, 468 insertions(+), 424 deletions(-)

base-commit: 4cc167c50eb19d44ac7e204938724e685e3d8057

Signature · 1 line
2.51.1.930.gacf6e81ea2-goog