Skip to contentSkip to conversation

KVM: Guest Memory Pre-Population API

PATCH In review Paolo Bonzini 4 participants 13 messages Jul 10, 2024 – Jul 11, 2024

Subsystems (5)
Documentation x86 KVM Kselftest +1 more subsystems
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

v5 cover KVM: Guest Memory Pre-Population API

Pre-population has been requested several times to mitigate KVM page faults during guest boot or after live migration. It is also required by TDX before filling in the initial guest memory with measured contents. Introduce it as a generic API.

Paolo

v4->v5:
- add EIO as possible error
- do not introduce __kvm_mmu_do_page_fault(), instead update stats in callers
- remove goto
- fix commit message to not mention weak symbol
- return error if range->size is 0
- fix comment to explain TDP restriction
- fix guest_test_phys_mem > guest_test_virt_mem case in test

Isaku Yamahata (3):
KVM: Document KVM_PRE_FAULT_MEMORY ioctl
KVM: Add KVM_PRE_FAULT_MEMORY vcpu ioctl to pre-populate guest memory
KVM: selftests: x86: Add test for KVM_PRE_FAULT_MEMORY

Paolo Bonzini (2):
KVM: x86/mmu: Make kvm_mmu_do_page_fault() return mapped level
KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory()

Sean Christopherson (2):
KVM: x86/mmu: Bump pf_taken stat only in the "real" page fault handler
KVM: x86/mmu: Account pf_{fixed,emulate,spurious} in callers of "do
page fault"

 Documentation/virt/kvm/api.rst                |  55 +++++++
 arch/x86/kvm/Kconfig                          |   1 +
 arch/x86/kvm/mmu/mmu.c                        |  96 +++++++++++-
 arch/x86/kvm/mmu/mmu_internal.h               |  26 +---
 arch/x86/kvm/x86.c                            |   3 +
 include/linux/kvm_host.h                      |   5 +
 include/uapi/linux/kvm.h                      |  10 ++
 tools/include/uapi/linux/kvm.h                |  14 +-
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/pre_fault_memory_test.c     | 146 ++++++++++++++++++
 virt/kvm/Kconfig                              |   3 +
 virt/kvm/kvm_main.c                           |  60 +++++++
 12 files changed, 394 insertions(+), 26 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/pre_fault_memory_test.c
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v5 1/7 KVM: Document KVM_PRE_FAULT_MEMORY ioctl

From: Isaku Yamahata <[email protected]>

Adds documentation of KVM_PRE_FAULT_MEMORY ioctl. [1]

It populates guest memory. It doesn't do extra operations on the underlying technology-specific initialization [2]. For example, CoCo-related operations won't be performed. Concretely for TDX, this API won't invoke TDH.MEM.PAGE.ADD() or TDH.MR.EXTEND(). Vendor-specific APIs are required for such operations.

The key point is to adapt of vcpu ioctl instead of VM ioctl. First, populating guest memory requires vcpu. If it is VM ioctl, we need to pick one vcpu somehow. Secondly, vcpu ioctl allows each vcpu to invoke this ioctl in parallel. It helps to scale regarding guest memory size, e.g., hundreds of GB.

[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/

Message-ID: <9a060293c9ad9a78f1d8994cfe1311e818e99257.1712785629.git.isaku.yamahata@intel.com>

---
 Documentation/virt/kvm/api.rst | 55 ++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
Documentation/virt/kvm/api.rst +55 -0
index a71d91978d9e..d543a5b71d1a 100644
@@ -6352,6 +6352,61 @@ a single guest_memfd file, but the bound ranges must not overlap).
 
 See KVM_SET_USER_MEMORY_REGION2 for additional details.
 
+4.143 KVM_PRE_FAULT_MEMORY
+------------------------
+
+:Capability: KVM_CAP_PRE_FAULT_MEMORY
+:Architectures: none
+:Type: vcpu ioctl
+:Parameters: struct kvm_pre_fault_memory (in/out)
+:Returns: 0 if at least one page is processed, < 0 on error
+
+Errors:
+
+  ========== ===============================================================
+  EINVAL     The specified `gpa` and `size` were invalid (e.g. not
+             page aligned, causes an overflow, or size is zero).
+  ENOENT     The specified `gpa` is outside defined memslots.
+  EINTR      An unmasked signal is pending and no page was processed.
+  EFAULT     The parameter address was invalid.
+  EOPNOTSUPP Mapping memory for a GPA is unsupported by the
+             hypervisor, and/or for the current vCPU state/mode.
+  EIO        unexpected error conditions (also causes a WARN)
+  ========== ===============================================================
+
+::
+
+  struct kvm_pre_fault_memory {
+	/* in/out */
+	__u64 gpa;
+	__u64 size;
+	/* in */
+	__u64 flags;
+	__u64 padding[5];
+  };
+
+KVM_PRE_FAULT_MEMORY populates KVM's stage-2 page tables used to map memory
+for the current vCPU state.  KVM maps memory as if the vCPU generated a
+stage-2 read page fault, e.g. faults in memory as needed, but doesn't break
+CoW.  However, KVM does not mark any newly created stage-2 PTE as Accessed.
+
+In some cases, multiple vCPUs might share the page tables.  In this
+case, the ioctl can be called in parallel.
+
+When the ioctl returns, the input values are updated to point to the
+remaining range.  If `size` > 0 on return, the caller can just issue
+the ioctl again with the same `struct kvm_map_memory` argument.
+
+Shadow page tables cannot support this ioctl because they
+are indexed by virtual address or nested guest physical address.
+Calling this ioctl when the guest is using shadow page tables (for
+example because it is running a nested guest with nested page tables)
+will fail with `EOPNOTSUPP` even if `KVM_CHECK_EXTENSION` reports
+the capability to be present.
+
+`flags` must currently be zero.
+
+
 5. The kvm_run structure
 ========================
 
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v5 2/7 KVM: Add KVM_PRE_FAULT_MEMORY vcpu ioctl to pre-populate guest memory

In reply to Paolo Bonzini

From: Isaku Yamahata <[email protected]>

Add a new ioctl KVM_PRE_FAULT_MEMORY in the KVM common code. It iterates on the memory range and calls the arch-specific function. The implementation is optional and enabled by a Kconfig symbol.

Message-ID: <819322b8f25971f2b9933bfa4506e618508ad782.1712785629.git.isaku.yamahata@intel.com>

---
 include/linux/kvm_host.h |  5 ++++
 include/uapi/linux/kvm.h | 10 +++++++
 virt/kvm/Kconfig         |  3 ++
 virt/kvm/kvm_main.c      | 60 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 78 insertions(+)
include/linux/kvm_host.h +5 -0
index 7b57878c8c18..c3c922bf077f 100644
@@ -2477,4 +2477,9 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t gfn, void __user *src, long npages
 void kvm_arch_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end);
 #endif
 
+#ifdef CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY
+long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
+				    struct kvm_pre_fault_memory *range);
+#endif
+
 #endif
include/uapi/linux/kvm.h +10 -0
index d03842abae57..e5af8c692dc0 100644
@@ -917,6 +917,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_MEMORY_ATTRIBUTES 233
 #define KVM_CAP_GUEST_MEMFD 234
 #define KVM_CAP_VM_TYPES 235
+#define KVM_CAP_PRE_FAULT_MEMORY 236
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
@@ -1548,4 +1549,13 @@ struct kvm_create_guest_memfd {
 	__u64 reserved[6];
 };
 
+#define KVM_PRE_FAULT_MEMORY	_IOWR(KVMIO, 0xd5, struct kvm_pre_fault_memory)
+
+struct kvm_pre_fault_memory {
+	__u64 gpa;
+	__u64 size;
+	__u64 flags;
+	__u64 padding[5];
+};
+
 #endif /* __LINUX_KVM_H */
virt/kvm/Kconfig +3 -0
index 754c6c923427..b14e14cdbfb9 100644
@@ -67,6 +67,9 @@ config HAVE_KVM_INVALID_WAKEUPS
 config KVM_GENERIC_DIRTYLOG_READ_PROTECT
        bool
 
+config KVM_GENERIC_PRE_FAULT_MEMORY
+       bool
+
 config KVM_COMPAT
        def_bool y
        depends on KVM && COMPAT && !(S390 || ARM64 || RISCV)
virt/kvm/kvm_main.c +60 -0

72 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v5 2/7] KVM: Add KVM_PRE_FAULT_MEMORY vcpu ioctl to pre-populate guest memory

virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu) · line 4390

Quoted 5 lines · Paolo Bonzini
virt/kvm/kvm_main.c +3 -0
+       if (!PAGE_ALIGNED(range->gpa) ||
+           !PAGE_ALIGNED(range->size) ||
...
+               return -EINVAL;

If 'gpa' and 'size' must be page-aligned anyways, doesn't it make sense to just take a 'gfn' and 'num_pages' and eliminate this error condition?

#4 lore raw

Re: [PATCH v5 2/7] KVM: Add KVM_PRE_FAULT_MEMORY vcpu ioctl to pre-populate guest memory

Quoted 10 lines · Anish Moorthy

virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu) · line 4390

Quoted 5 lines · Paolo Bonzini
virt/kvm/kvm_main.c +3 -0
+       if (!PAGE_ALIGNED(range->gpa) ||
+           !PAGE_ALIGNED(range->size) ||
...
+               return -EINVAL;

If 'gpa' and 'size' must be page-aligned anyways, doesn't it make sense to just take a 'gfn' and 'num_pages' and eliminate this error condition?

The downside is that taking gfn+num_pages prevents supporting sub-page pre-faulting in the future. I highly doubt that sub-page mappings will ever be a thing in KVM, but two PAGE_ALIGNED() checks is super cheap, so it's soft of a "why not?" scenario.

author maintainer
#5 lore raw

Re: [PATCH v5 2/7] KVM: Add KVM_PRE_FAULT_MEMORY vcpu ioctl to pre-populate guest memory

Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Anish Moorthy
Quoted 5 lines · Paolo Bonzini
virt/kvm/kvm_main.c +3 -0
+       if (!PAGE_ALIGNED(range->gpa) ||
+           !PAGE_ALIGNED(range->size) ||
...
+               return -EINVAL;
Quoted 7 lines
Quoted 3 lines

If 'gpa' and 'size' must be page-aligned anyways, doesn't it make sense to just take a 'gfn' and 'num_pages' and eliminate this error condition?

The downside is that taking gfn+num_pages prevents supporting sub-page pre-faulting in the future. I highly doubt that sub-page mappings will ever be a thing in KVM, but two PAGE_ALIGNED() checks is super cheap, so it's soft of a "why not?" scenario.

With ARM having multiple page sizes, and not necessarily the same size in host and guest, using a gfn argument is also unnecessarily confusing.

Paolo

author maintainer
#6 lore raw

PATCH v5 3/7 KVM: x86/mmu: Bump pf_taken stat only in the "real" page fault handler

In reply to Paolo Bonzini

From: Sean Christopherson <[email protected]>

Account stat.pf_taken in kvm_mmu_page_fault(), i.e. the actual page fault handler, instead of conditionally bumping it in kvm_mmu_do_page_fault(). The "real" page fault handler is the only path that should ever increment the number of taken page faults, as all other paths that "do page fault" are by definition not handling faults that occurred in the guest.

---
 arch/x86/kvm/mmu/mmu.c          | 2 ++
 arch/x86/kvm/mmu/mmu_internal.h | 8 --------
 2 files changed, 2 insertions(+), 8 deletions(-)
arch/x86/kvm/mmu/mmu.c +2 -0
index 8b64f31e13be..aa437aacf55f 100644
@@ -5925,6 +5925,8 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err
 	}
 
 	if (r == RET_PF_INVALID) {
+		vcpu->stat.pf_taken++;
+
 		r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa, error_code, false,
 					  &emulation_type);
 		if (KVM_BUG_ON(r == RET_PF_INVALID, vcpu->kvm))
arch/x86/kvm/mmu/mmu_internal.h +0 -8
index ce2fcd19ba6b..8efd31b3856b 100644
@@ -318,14 +318,6 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
 		fault.slot = kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
 	}
 
-	/*
-	 * Async #PF "faults", a.k.a. prefetch faults, are not faults from the
-	 * guest perspective and have already been counted at the time of the
-	 * original fault.
-	 */
-	if (!prefetch)
-		vcpu->stat.pf_taken++;
-
 	if (IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) && fault.is_tdp)
 		r = kvm_tdp_page_fault(vcpu, &fault);
 	else
Signature · 1 line
2.43.0
author maintainer
#7 lore raw

PATCH v5 4/7 KVM: x86/mmu: Account pf_{fixed,emulate,spurious} in callers of "do page fault"

In reply to Paolo Bonzini

From: Sean Christopherson <[email protected]>

Move the accounting of the result of kvm_mmu_do_page_fault() to its callers, as only pf_fixed is common to guest page faults and async #PFs, and upcoming support KVM_PRE_FAULT_MEMORY won't bump _any_ stats.

---
 arch/x86/kvm/mmu/mmu.c          | 19 ++++++++++++++++++-
 arch/x86/kvm/mmu/mmu_internal.h | 13 -------------
 2 files changed, 18 insertions(+), 14 deletions(-)
arch/x86/kvm/mmu/mmu.c +18 -1
index aa437aacf55f..8c2c5c0afba1 100644
@@ -4291,7 +4291,16 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
 	      work->arch.cr3 != kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu))
 		return;
 
-	kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, work->arch.error_code, true, NULL);
+	r = kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, work->arch.error_code,
+				  true, NULL);
+
+	/*
+	 * Account fixed page faults, otherwise they'll never be counted, but
+	 * ignore stats for all other return times.  Page-ready "faults" aren't
+	 * truly spurious and never trigger emulation
+	 */
+	if (r == RET_PF_FIXED)
+		vcpu->stat.pf_fixed++;
 }
 
 static inline u8 kvm_max_level_for_order(int order)
@@ -5935,6 +5944,14 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err
 
 	if (r < 0)
 		return r;
+
+	if (r == RET_PF_FIXED)
+		vcpu->stat.pf_fixed++;
+	else if (r == RET_PF_EMULATE)
+		vcpu->stat.pf_emulate++;
+	else if (r == RET_PF_SPURIOUS)
+		vcpu->stat.pf_spurious++;
+
 	if (r != RET_PF_EMULATE)
 		return 1;
 
arch/x86/kvm/mmu/mmu_internal.h +0 -13
index 8efd31b3856b..444f55a5eed7 100644
@@ -337,19 +337,6 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
 	if (fault.write_fault_to_shadow_pgtable && emulation_type)
 		*emulation_type |= EMULTYPE_WRITE_PF_TO_SP;
 
-	/*
-	 * Similar to above, prefetch faults aren't truly spurious, and the
-	 * async #PF path doesn't do emulation.  Do count faults that are fixed
-	 * by the async #PF handler though, otherwise they'll never be counted.
-	 */
-	if (r == RET_PF_FIXED)
-		vcpu->stat.pf_fixed++;
-	else if (prefetch)
-		;
-	else if (r == RET_PF_EMULATE)
-		vcpu->stat.pf_emulate++;
-	else if (r == RET_PF_SPURIOUS)
-		vcpu->stat.pf_spurious++;
 	return r;
 }
 
Signature · 1 line
2.43.0
author maintainer
#8 lore raw

PATCH v5 5/7 KVM: x86/mmu: Make kvm_mmu_do_page_fault() return mapped level

In reply to Paolo Bonzini

The guest memory population logic will need to know what page size or level (4K, 2M, ...) is mapped.

Message-ID: <eabc3f3e5eb03b370cadf6e1901ea34d7a020adc.1712785629.git.isaku.yamahata@intel.com>

---
 arch/x86/kvm/mmu/mmu.c          | 4 ++--
 arch/x86/kvm/mmu/mmu_internal.h | 5 ++++-
 2 files changed, 6 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu/mmu.c +2 -2
index 8c2c5c0afba1..152b30fa22ad 100644
@@ -4292,7 +4292,7 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
 		return;
 
 	r = kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, work->arch.error_code,
-				  true, NULL);
+				  true, NULL, NULL);
 
 	/*
 	 * Account fixed page faults, otherwise they'll never be counted, but
@@ -5937,7 +5937,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err
 		vcpu->stat.pf_taken++;
 
 		r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa, error_code, false,
-					  &emulation_type);
+					  &emulation_type, NULL);
 		if (KVM_BUG_ON(r == RET_PF_INVALID, vcpu->kvm))
 			return -EIO;
 	}
arch/x86/kvm/mmu/mmu_internal.h +4 -1
index 444f55a5eed7..1721d97743e9 100644
@@ -288,7 +288,8 @@ static inline void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
 }
 
 static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
-					u64 err, bool prefetch, int *emulation_type)
+					u64 err, bool prefetch,
+					int *emulation_type, u8 *level)
 {
 	struct kvm_page_fault fault = {
 		.addr = cr2_or_gpa,
@@ -336,6 +337,8 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
 
 	if (fault.write_fault_to_shadow_pgtable && emulation_type)
 		*emulation_type |= EMULTYPE_WRITE_PF_TO_SP;
+	if (level)
+		*level = fault.goal_level;
 
 	return r;
 }
Signature · 1 line
2.43.0
author maintainer
#9 lore raw

PATCH v5 6/7 KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory()

In reply to Paolo Bonzini

Wire KVM_PRE_FAULT_MEMORY ioctl to __kvm_mmu_do_page_fault() to populate guest memory. It can be called right after KVM_CREATE_VCPU creates a vCPU, since at that point kvm_mmu_create() and kvm_init_mmu() are called and the vCPU is ready to invoke the KVM page fault handler.

The helper function kvm_mmu_map_tdp_page take care of the logic to process RET_PF_* return values and convert them to success or errno.

Message-ID: <9b866a0ae7147f96571c439e75429a03dcb659b6.1712785629.git.isaku.yamahata@intel.com>

---
 arch/x86/kvm/Kconfig   |  1 +
 arch/x86/kvm/mmu/mmu.c | 73 ++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c     |  3 ++
 3 files changed, 77 insertions(+)
arch/x86/kvm/Kconfig +1 -0
index 80e5afde69f4..4287a8071a3a 100644
@@ -44,6 +44,7 @@ config KVM
 	select KVM_VFIO
 	select HAVE_KVM_PM_NOTIFIER if PM
 	select KVM_GENERIC_HARDWARE_ENABLING
+	select KVM_GENERIC_PRE_FAULT_MEMORY
 	select KVM_WERROR if WERROR
 	help
 	  Support hosting fully virtualized guest machines using hardware
arch/x86/kvm/mmu/mmu.c +73 -0

79 lines not on this page · raw message

arch/x86/kvm/x86.c +3 -0
index ba0ad76f53bc..a6968eadd418 100644
@@ -4705,6 +4705,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_MEMORY_FAULT_INFO:
 		r = 1;
 		break;
+	case KVM_CAP_PRE_FAULT_MEMORY:
+		r = tdp_enabled;
+		break;
 	case KVM_CAP_EXIT_HYPERCALL:
 		r = KVM_EXIT_HYPERCALL_VALID_MASK;
 		break;
Signature · 1 line
2.43.0
#10 lore raw

Re: [PATCH v5 6/7] KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory()

Quoted 1 line · Paolo Bonzini

Wire KVM_PRE_FAULT_MEMORY ioctl to __kvm_mmu_do_page_fault() to populate guest

__kvm_mmu_do_page_fault() -> kvm_mmu_do_page_fault()

Quoted 5 lines

memory. It can be called right after KVM_CREATE_VCPU creates a vCPU, since at that point kvm_mmu_create() and kvm_init_mmu() are called and the vCPU is ready to invoke the KVM page fault handler.

The helper function kvm_mmu_map_tdp_page take care of the logic to

kvm_mmu_map_tdp_page -> kvm_tdp_map_page()?

Quoted 23 lines

process RET_PF_* return values and convert them to success or errno.

Message-ID: <9b866a0ae7147f96571c439e75429a03dcb659b6.1712785629.git.isaku.yamahata@intel.com>

---
  arch/x86/kvm/Kconfig   |  1 +
  arch/x86/kvm/mmu/mmu.c | 73 ++++++++++++++++++++++++++++++++++++++++++
  arch/x86/kvm/x86.c     |  3 ++
  3 files changed, 77 insertions(+)
arch/x86/kvm/Kconfig +1 -0
index 80e5afde69f4..4287a8071a3a 100644
@@ -44,6 +44,7 @@ config KVM
  	select KVM_VFIO
  	select HAVE_KVM_PM_NOTIFIER if PM
  	select KVM_GENERIC_HARDWARE_ENABLING
+	select KVM_GENERIC_PRE_FAULT_MEMORY
  	select KVM_WERROR if WERROR
  	help
  	  Support hosting fully virtualized guest machines using hardware

[...]

arch/x86/kvm/x86.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 4710

Quoted 10 lines

index ba0ad76f53bc..a6968eadd418 100644

arch/x86/kvm/x86.c +3 -0
@@ -4705,6 +4705,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  	case KVM_CAP_MEMORY_FAULT_INFO:
  		r = 1;
  		break;
+	case KVM_CAP_PRE_FAULT_MEMORY:
+		r = tdp_enabled;
+		break;

If !CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY, this should return 0.

Quoted 3 lines
arch/x86/kvm/x86.c +0 -0
  	case KVM_CAP_EXIT_HYPERCALL:
  		r = KVM_EXIT_HYPERCALL_VALID_MASK;
  		break;
author maintainer
#11 lore raw

Re: [PATCH v5 6/7] KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory()

Quoted 5 lines · Binbin Wu
Quoted 1 line · Paolo Bonzini

Wire KVM_PRE_FAULT_MEMORY ioctl to __kvm_mmu_do_page_fault() to populate guest

__kvm_mmu_do_page_fault() -> kvm_mmu_do_page_fault()

Quoted 7 lines
Quoted 5 lines

memory. It can be called right after KVM_CREATE_VCPU creates a vCPU, since at that point kvm_mmu_create() and kvm_init_mmu() are called and the vCPU is ready to invoke the KVM page fault handler.

The helper function kvm_mmu_map_tdp_page take care of the logic to

kvm_mmu_map_tdp_page -> kvm_tdp_map_page()?

Yes, will fix.

Quoted 13 lines
Quoted 12 lines
arch/x86/kvm/Kconfig +1 -0
index 80e5afde69f4..4287a8071a3a 100644
@@ -44,6 +44,7 @@ config KVM
      select KVM_VFIO
      select HAVE_KVM_PM_NOTIFIER if PM
      select KVM_GENERIC_HARDWARE_ENABLING
+     select KVM_GENERIC_PRE_FAULT_MEMORY
      select KVM_WERROR if WERROR
      help
        Support hosting fully virtualized guest machines using hardware

[...]

Quoted 11 lines

arch/x86/kvm/x86.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 4710

Quoted 10 lines

index ba0ad76f53bc..a6968eadd418 100644

arch/x86/kvm/x86.c +3 -0
@@ -4705,6 +4705,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
      case KVM_CAP_MEMORY_FAULT_INFO:
              r = 1;
              break;
+     case KVM_CAP_PRE_FAULT_MEMORY:
+             r = tdp_enabled;
+             break;

If !CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY, this should return 0.

This is x86-specific code and it CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY is always selected by CONFIG_KVM on x86 (that is, it does not depend on TDX or anything else).

Paolo

author maintainer
#12 lore raw

PATCH v5 7/7 KVM: selftests: x86: Add test for KVM_PRE_FAULT_MEMORY

In reply to Paolo Bonzini

From: Isaku Yamahata <[email protected]>

Add a test case to exercise KVM_PRE_FAULT_MEMORY and run the guest to access the pre-populated area. It tests KVM_PRE_FAULT_MEMORY ioctl for KVM_X86_DEFAULT_VM and KVM_X86_SW_PROTECTED_VM.

Message-ID: <32427791ef42e5efaafb05d2ac37fa4372715f47.1712785629.git.isaku.yamahata@intel.com>

---
 tools/include/uapi/linux/kvm.h                |  14 +-
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/pre_fault_memory_test.c     | 146 ++++++++++++++++++
 3 files changed, 159 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/pre_fault_memory_test.c
tools/include/uapi/linux/kvm.h +12 -2
index ea32b101b999..e5af8c692dc0 100644
@@ -917,6 +917,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_MEMORY_ATTRIBUTES 233
 #define KVM_CAP_GUEST_MEMFD 234
 #define KVM_CAP_VM_TYPES 235
+#define KVM_CAP_PRE_FAULT_MEMORY 236
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
@@ -1221,9 +1222,9 @@ struct kvm_vfio_spapr_tce {
 /* Available with KVM_CAP_SPAPR_RESIZE_HPT */
 #define KVM_PPC_RESIZE_HPT_PREPARE _IOR(KVMIO, 0xad, struct kvm_ppc_resize_hpt)
 #define KVM_PPC_RESIZE_HPT_COMMIT  _IOR(KVMIO, 0xae, struct kvm_ppc_resize_hpt)
-/* Available with KVM_CAP_PPC_RADIX_MMU or KVM_CAP_PPC_MMU_HASH_V3 */
+/* Available with KVM_CAP_PPC_MMU_RADIX or KVM_CAP_PPC_MMU_HASH_V3 */
 #define KVM_PPC_CONFIGURE_V3_MMU  _IOW(KVMIO,  0xaf, struct kvm_ppc_mmuv3_cfg)
-/* Available with KVM_CAP_PPC_RADIX_MMU */
+/* Available with KVM_CAP_PPC_MMU_RADIX */
 #define KVM_PPC_GET_RMMU_INFO	  _IOW(KVMIO,  0xb0, struct kvm_ppc_rmmu_info)
 /* Available with KVM_CAP_PPC_GET_CPU_CHAR */
 #define KVM_PPC_GET_CPU_CHAR	  _IOR(KVMIO,  0xb1, struct kvm_ppc_cpu_char)
@@ -1548,4 +1549,13 @@ struct kvm_create_guest_memfd {
 	__u64 reserved[6];
 };
 
+#define KVM_PRE_FAULT_MEMORY	_IOWR(KVMIO, 0xd5, struct kvm_pre_fault_memory)
+
+struct kvm_pre_fault_memory {
+	__u64 gpa;
+	__u64 size;
+	__u64 flags;
+	__u64 padding[5];
+};
+
 #endif /* __LINUX_KVM_H */
tools/testing/selftests/kvm/Makefile +1 -0
index ce8ff8e8ce3a..e915d4ae1793 100644
@@ -145,6 +145,7 @@ TEST_GEN_PROGS_x86_64 += set_memory_region_test
 TEST_GEN_PROGS_x86_64 += steal_time
 TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test
 TEST_GEN_PROGS_x86_64 += system_counter_offset_test
+TEST_GEN_PROGS_x86_64 += pre_fault_memory_test
 
 # Compiled outputs used by test targets
 TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test
tools/testing/selftests/kvm/pre_fault_memory_test.c new file +146 -0

146 lines not on this page · raw message

Signature · 1 line
2.43.0