KVM: guest_memfd: New hooks and functionality for SEV-SNP and TDX
Subsystems (6)
Paolo Bonzini
maintainerv2 cover KVM: guest_memfd: New hooks and functionality for SEV-SNP and TDX
This is the hopefully final version of the gmem common API patches, adding target-independent functionality and hooks that are needed by SEV-SNP and TDX.
Changes from https://patchew.org/linux/[email protected]/:
- added acks and comments from Vlastimil to patches 1-2
- dropped FGP_CREAT_ONLY, which also means removing kvm_gmem_undo_get_pfn() in the last patch
- filemap_grab_folio() cannot return NULL
- dropped weak implementation of kvm_arch_gmem_prepare_needed()
- dropped __kvm_gmem_punch_hole()
- introduced kvm_gmem_populate_cb typedef
- renamed argument of kvm_gmem_populate from gfn to start_gfn, variable from this_gfn to gfn
- post_populate hook is mandatory
- replaced implementation of "KVM: x86: Add hook for determining max NPT mapping level" with suggestion from SNP series
Contrarily to my answer to Yilun, prepare does not need to test hwpoison. It's kvm_gmem_populate() that needs it, and in fact it already does via __kvm_gmem_get_pfn().
Paolo
Michael Roth (4):
mm: Introduce AS_INACCESSIBLE for encrypted/confidential memory
KVM: guest_memfd: Use AS_INACCESSIBLE when creating guest_memfd inode
KVM: guest_memfd: Add hook for invalidating memory
KVM: x86: Add hook for determining max NPT mapping level
Paolo Bonzini (5):
KVM: guest_memfd: pass error up from filemap_grab_folio
KVM: guest_memfd: limit overzealous WARN
KVM: guest_memfd: Add hook for initializing memory
KVM: guest_memfd: extract __kvm_gmem_get_pfn()
KVM: guest_memfd: Add interface for populating gmem pages with user
data
arch/x86/include/asm/kvm-x86-ops.h | 3 + arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/mmu/mmu.c | 23 +++- arch/x86/kvm/x86.c | 13 +++ include/linux/kvm_host.h | 36 +++++++ include/linux/pagemap.h | 1 + mm/truncate.c | 3 +- virt/kvm/Kconfig | 8 ++ virt/kvm/guest_memfd.c | 167 +++++++++++++++++++++++++---- 9 files changed, 231 insertions(+), 26 deletions(-)
Signature · 1 line
2.43.0
Paolo Bonzini
maintainerPATCH v2 1/9 mm: Introduce AS_INACCESSIBLE for encrypted/confidential memory
From: Michael Roth <[email protected]>
filemap users like guest_memfd may use page cache pages to allocate/manage memory that is only intended to be accessed by guests via hardware protections like encryption. Writes to memory of this sort in common paths like truncation may cause unexpected behavior such as writing garbage instead of zeros when attempting to zero pages, or worse, triggering hardware protections that are considered fatal as far as the kernel is concerned.
Introduce a new address_space flag, AS_INACCESSIBLE, and use this initially to prevent zero'ing of pages during truncation, with the understanding that it is up to the owner of the mapping to handle this specially if needed.
This is admittedly a rather blunt solution, but it seems like there are no other places that should take into account the flag to keep its promise.
Message-ID: <[email protected]>
--- include/linux/pagemap.h | 1 + mm/truncate.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/pagemap.h +1 -0
@@ -207,6 +207,7 @@ enum mapping_flags {AS_STABLE_WRITES ,/* must wait for writeback before modifying folio contents */ AS_UNMOVABLE ,/* The mapping cannot be moved, ever */ + AS_INACCESSIBLE, /* Do not attempt direct R/W access to the mapping */ };/**
mm/truncate.c +2 -1
@@ -233,7 +233,8 @@ bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end)* doing a complex calculation here, and then doing the zeroing * anyway if the page split fails. */ -folio_zero_range (folio, offset, length); +if (!(folio->mapping->flags &AS_INACCESSIBLE )) +folio_zero_range (folio, offset, length);if (folio_has_private (folio))folio_invalidate (folio, offset, length);
Signature · 1 line
2.43.0
Paolo Bonzini
maintainerPATCH v2 2/9 KVM: guest_memfd: Use AS_INACCESSIBLE when creating guest_memfd inode
In reply to Paolo BonziniFrom: Michael Roth <[email protected]>
truncate_inode_pages_range() may attempt to zero pages before truncating them, and this will occur before arch-specific invalidations can be triggered via .invalidate_folio/.free_folio hooks via kvm_gmem_aops. For AMD SEV-SNP this would result in an RMP #PF being generated by the hardware, which is currently treated as fatal (and even if specifically allowed for, would not result in anything other than garbage being written to guest pages due to encryption). On Intel TDX this would also result in undesirable behavior.
Set the AS_INACCESSIBLE flag to prevent the MM from attempting unexpected accesses of this sort during operations like truncation.
This may also in some cases yield a decent performance improvement for guest_memfd userspace implementations that hole-punch ranges immediately after private->shared conversions via KVM_SET_MEMORY_ATTRIBUTES, since the current implementation of truncate_inode_pages_range() always ends up zero'ing an entire 4K range if it is backing by a 2M folio.
Message-ID: <[email protected]>
--- virt/kvm/guest_memfd.c | 1 + 1 file changed, 1 insertion(+)
virt/kvm/guest_memfd.c +1 -0
@@ -357,6 +357,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) inode->i_private = (void *)(unsigned long )flags; inode->i_op = &kvm_gmem_iops; inode->i_mapping->a_ops = &kvm_gmem_aops; + inode->i_mapping->flags |=AS_INACCESSIBLE ; inode->i_mode |=S_IFREG ; inode->i_size = size;mapping_set_gfp_mask (inode->i_mapping,GFP_HIGHUSER );
Signature · 1 line
2.43.0
Paolo Bonzini
maintainerPATCH v2 3/9 KVM: guest_memfd: pass error up from filemap_grab_folio
In reply to Paolo BonziniSome SNP ioctls will require the page not to be in the pagecache, and as such they will want to return EEXIST to userspace. Start by passing the error up from filemap_grab_folio.
--- virt/kvm/guest_memfd.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
virt/kvm/guest_memfd.c +6 -6
@@ -19,8 +19,8 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index)/* TODO: Support huge pages. */ folio =filemap_grab_folio (inode->i_mapping, index); -if (IS_ERR_OR_NULL (folio)) -return NULL ; +if (IS_ERR (folio)) +return folio;/* * Use the up-to-date flag to track whether or not the memory has been
@@ -146,8 +146,8 @@ static long kvm_gmem_allocate(struct inode *inode, loff_t offset, loff_t len) } folio =kvm_gmem_get_folio (inode, index); -if (!folio) { - r = -ENOMEM ; +if (IS_ERR (folio)) { + r =PTR_ERR (folio);break ; }
@@ -505,8 +505,8 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, } folio =kvm_gmem_get_folio (file_inode (file), index); -if (!folio) { - r = -ENOMEM ; +if (IS_ERR (folio)) { + r =PTR_ERR (folio); goto out_fput; }
Signature · 1 line
2.43.0
Paolo Bonzini
maintainerPATCH v2 4/9 KVM: guest_memfd: limit overzealous WARN
In reply to Paolo BonziniBecause kvm_gmem_get_pfn() is called from the page fault path without any of the slots_lock, filemap lock or mmu_lock taken, it is possible for it to race with kvm_gmem_unbind(). This is not a problem, as any PTE that is installed temporarily will be zapped before the guest has the occasion to run.
However, it is not possible to have a complete unbind+bind racing with the page fault, because deleting the memslot will call synchronize_srcu_expedited() and wait for the page fault to be resolved. Thus, we can still warn if the file is there and is not the one we expect.
--- virt/kvm/guest_memfd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
virt/kvm/guest_memfd.c +2 -1
@@ -499,7 +499,8 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gmem = file->private_data; -if (WARN_ON_ONCE (xa_load (&gmem->bindings, index) != slot)) { +if (xa_load (&gmem->bindings, index) != slot) { +WARN_ON_ONCE (xa_load (&gmem->bindings, index)); r = -EIO ; goto out_fput; }
Signature · 1 line
2.43.0
Paolo Bonzini
maintainerPATCH v2 5/9 KVM: guest_memfd: Add hook for initializing memory
In reply to Paolo Bonziniguest_memfd pages are generally expected to be in some arch-defined initial state prior to using them for guest memory. For SEV-SNP this initial state is 'private', or 'guest-owned', and requires additional operations to move these pages into a 'private' state by updating the corresponding entries the RMP table.
Allow for an arch-defined hook to handle updates of this sort, and go ahead and implement one for x86 so KVM implementations like AMD SVM can register a kvm_x86_ops callback to handle these updates for SEV-SNP guests.
The preparation callback is always called when allocating/grabbing folios via gmem, and it is up to the architecture to keep track of whether or not the pages are already in the expected state (e.g. the RMP table in the case of SEV-SNP).
In some cases, it is necessary to defer the preparation of the pages to handle things like in-place encryption of initial guest memory payloads before marking these pages as 'private'/'guest-owned'. Add an argument (always true for now) to kvm_gmem_get_folio() that allows for the preparation callback to be bypassed. To detect possible issues in the way userspace initializes memory, it is only possible to add an unprepared page if it is not already included in the filemap.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 6 ++++ include/linux/kvm_host.h | 5 +++ virt/kvm/Kconfig | 4 +++ virt/kvm/guest_memfd.c | 51 ++++++++++++++++++++++++++++-- 6 files changed, 65 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -139,6 +139,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL(alloc_apic_backing_page) +KVM_X86_OP_OPTIONAL_RET0(gmem_prepare)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1814,6 +1814,7 @@ struct kvm_x86_ops {gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu,gva_t gva,unsigned int flags);void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); +int (*gmem_prepare)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order); };struct kvm_x86_nested_ops {
arch/x86/kvm/x86.c +6 -0
@@ -13598,6 +13598,12 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_arch_no_poll); +#ifdef CONFIG_HAVE_KVM_GMEM_PREPARE +int kvm_arch_gmem_prepare (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int max_order) +{ +return static_call (kvm_x86_gmem_prepare)(kvm, pfn, gfn, max_order); +} +#endif int kvm_spec_ctrl_test_value (u64 value) {
include/linux/kvm_host.h +5 -0
@@ -2443,4 +2443,9 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm, }#endif /* CONFIG_KVM_PRIVATE_MEM */ +#ifdef CONFIG_HAVE_KVM_GMEM_PREPARE +int kvm_arch_gmem_prepare (struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn,int max_order); +bool kvm_arch_gmem_prepare_needed (struct kvm *kvm); +#endif +#endif
virt/kvm/Kconfig +4 -0
@@ -109,3 +109,7 @@ config KVM_GENERIC_PRIVATE_MEM select KVM_GENERIC_MEMORY_ATTRIBUTES select KVM_PRIVATE_MEM bool + +config HAVE_KVM_GMEM_PREPARE + bool + depends on KVM_PRIVATE_MEM
virt/kvm/guest_memfd.c +48 -3
75 lines not on this page · raw message
Signature · 1 line
2.43.0
Paolo Bonzini
maintainerPATCH v2 6/9 KVM: guest_memfd: extract __kvm_gmem_get_pfn()
In reply to Paolo BonziniIn preparation for adding a function that walks a set of pages provided by userspace and populates them in a guest_memfd, add a version of kvm_gmem_get_pfn() that has a "bool prepare" argument and passes it down to kvm_gmem_get_folio().
Populating guest memory has to call repeatedly __kvm_gmem_get_pfn() on the same file, so make the new function take struct file*.
--- virt/kvm/guest_memfd.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-)
virt/kvm/guest_memfd.c +24 -16
@@ -528,33 +528,29 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot)fput (file); } -int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, -gfn_t gfn,kvm_pfn_t *pfn,int *max_order) +static int __kvm_gmem_get_pfn (struct file *file,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order,bool prepare) {pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; -struct kvm_gmem *gmem; +struct kvm_gmem *gmem = file->private_data;struct folio *folio;struct page *page; -struct file *file;int r; - file =kvm_gmem_get_file (slot); -if (!file) +if (file != slot->gmem.file) { +WARN_ON_ONCE (slot->gmem.file);return -EFAULT ; + } gmem = file->private_data; -if (xa_load (&gmem->bindings, index) != slot) {WARN_ON_ONCE (xa_load (&gmem->bindings, index)); - r = -EIO ; - goto out_fput; +return -EIO ; } - folio =kvm_gmem_get_folio (file_inode (file), index, true); -if (IS_ERR (folio)) { - r =PTR_ERR (folio); - goto out_fput; - } + folio =kvm_gmem_get_folio (file_inode (file), index, prepare); +if (IS_ERR (folio)) +return PTR_ERR (folio);if (folio_test_hwpoison (folio)) { r = -EHWPOISON ;
@@ -571,9 +567,21 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, out_unlock:folio_unlock (folio); -out_fput: -fput (file);return r; } + +int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order) +{ +struct file *file =kvm_gmem_get_file (slot); +int r; + +if (!file) +return -EFAULT ; + + r =__kvm_gmem_get_pfn (file, slot, gfn, pfn, max_order, true); +fput (file); +return r; +}EXPORT_SYMBOL_GPL (kvm_gmem_get_pfn);
Signature · 1 line
2.43.0
Paolo Bonzini
maintainerPATCH v2 7/9 KVM: guest_memfd: Add interface for populating gmem pages with user data
In reply to Paolo BonziniDuring guest run-time, kvm_arch_gmem_prepare() is issued as needed to prepare newly-allocated gmem pages prior to mapping them into the guest. In the case of SEV-SNP, this mainly involves setting the pages to private in the RMP table.
However, for the GPA ranges comprising the initial guest payload, which are encrypted/measured prior to starting the guest, the gmem pages need to be accessed prior to setting them to private in the RMP table so they can be initialized with the userspace-provided data. Additionally, an SNP firmware call is needed afterward to encrypt them in-place and measure the contents into the guest's launch digest.
While it is possible to bypass the kvm_arch_gmem_prepare() hooks so that this handling can be done in an open-coded/vendor-specific manner, this may expose more gmem-internal state/dependencies to external callers than necessary. Try to avoid this by implementing an interface that tries to handle as much of the common functionality inside gmem as possible, while also making it generic enough to potentially be usable/extensible for TDX as well.
--- include/linux/kvm_host.h | 27 +++++++++++++++++++++ virt/kvm/guest_memfd.c | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+)
include/linux/kvm_host.h +27 -0
@@ -2448,4 +2448,31 @@ int kvm_arch_gmem_prepare(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int max_ordbool kvm_arch_gmem_prepare_needed (struct kvm *kvm);#endif +/** +* kvm_gmem_populate() - Populate/prepare a GPA range with guest data +* +* @kvm: KVM instance +* @gfn: starting GFN to be populated +* @src: userspace-provided buffer containing data to copy into GFN range +* (passed to @post_populate, and incremented on each iteration +* if not NULL) +* @npages: number of pages to copy from userspace-buffer +* @post_populate: callback to issue for each gmem page that backs the GPA +* range +* @opaque: opaque data to pass to @post_populate callback +* +* This is primarily intended for cases where a gmem-backed GPA range needs +* to be initialized with userspace-provided data prior to being mapped into +* the guest as a private page. This should be called with the slots->lock +* held so that caller-enforced invariants regarding the expected memory +* attributes of the GPA range do not race with KVM_SET_MEMORY_ATTRIBUTES. +* +* Returns the number of pages that were populated. +*/ +typedef int (*kvm_gmem_populate_cb )(struct kvm *kvm,gfn_t gfn,kvm_pfn_t pfn, +void __user *src,int order,void *opaque); + +long kvm_gmem_populate (struct kvm *kvm,gfn_t gfn,void __user *src,long npages, +kvm_gmem_populate_cb post_populate,void *opaque); +#endif
virt/kvm/guest_memfd.c +52 -0
55 lines not on this page · raw message
Signature · 1 line
2.43.0
Paolo Bonzini
maintainerPATCH v2 8/9 KVM: guest_memfd: Add hook for invalidating memory
In reply to Paolo BonziniFrom: Michael Roth <[email protected]>
In some cases, like with SEV-SNP, guest memory needs to be updated in a platform-specific manner before it can be safely freed back to the host. Wire up arch-defined hooks to the .free_folio kvm_gmem_aops callback to allow for special handling of this sort when freeing memory in response to FALLOC_FL_PUNCH_HOLE operations and when releasing the inode, and go ahead and define an arch-specific hook for x86 since it will be needed for handling memory used for SEV-SNP guests.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 7 +++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/Kconfig | 4 ++++ virt/kvm/guest_memfd.c | 14 ++++++++++++++ 6 files changed, 31 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -140,6 +140,7 @@ KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL(alloc_apic_backing_page) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL(gmem_invalidate)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1815,6 +1815,7 @@ struct kvm_x86_ops {gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu,gva_t gva,unsigned int flags);void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu);int (*gmem_prepare)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order); +void (*gmem_invalidate)(kvm_pfn_t start,kvm_pfn_t end); };struct kvm_x86_nested_ops {
arch/x86/kvm/x86.c +7 -0
@@ -13605,6 +13605,13 @@ int kvm_arch_gmem_prepare(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int max_ord }#endif +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE +void kvm_arch_gmem_invalidate (kvm_pfn_t start,kvm_pfn_t end) +{ +static_call_cond (kvm_x86_gmem_invalidate)(start, end); +} +#endif +int kvm_spec_ctrl_test_value (u64 value) {/*
include/linux/kvm_host.h +4 -0
@@ -2475,4 +2475,8 @@ typedef int (*kvm_gmem_populate_cb)(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,long kvm_gmem_populate (struct kvm *kvm,gfn_t gfn,void __user *src,long npages,kvm_gmem_populate_cb post_populate,void *opaque); +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE +void kvm_arch_gmem_invalidate (kvm_pfn_t start,kvm_pfn_t end); +#endif +#endif
virt/kvm/Kconfig +4 -0
@@ -113,3 +113,7 @@ config KVM_GENERIC_PRIVATE_MEM config HAVE_KVM_GMEM_PREPARE bool depends on KVM_PRIVATE_MEM + +config HAVE_KVM_GMEM_INVALIDATE + bool + depends on KVM_PRIVATE_MEM
virt/kvm/guest_memfd.c +14 -0
@@ -343,10 +343,24 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *folreturn MF_DELAYED ; } +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE +static void kvm_gmem_free_folio (struct folio *folio) +{ +struct page *page =folio_page (folio,0 ); +kvm_pfn_t pfn =page_to_pfn (page); +int order =folio_order (folio); + +kvm_arch_gmem_invalidate (pfn, pfn + (1ul << order)); +} +#endif +static const struct address_space_operations kvm_gmem_aops = { .dirty_folio = noop_dirty_folio, .migrate_folio = kvm_gmem_migrate_folio, .error_remove_folio = kvm_gmem_error_folio, +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE + .free_folio = kvm_gmem_free_folio, +#endif }; static int kvm_gmem_getattr(struct mnt_idmap *idmap, const struct path *path,
Signature · 1 line
2.43.0
Paolo Bonzini
maintainerPATCH v2 9/9 KVM: x86: Add hook for determining max NPT mapping level
In reply to Paolo BonziniFrom: Michael Roth <[email protected]>
In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table. The existing mixed-attribute tracking in KVM is insufficient here, for instance:
- gmem allocates 2MB page
- guest issues PVALIDATE on 2MB page
- guest later converts a subpage to shared
- SNP host code issues PSMASH to split 2MB RMP mapping to 4K
- KVM MMU splits NPT mapping to 4K
- guest later converts that shared page back to private
At this point there are no mixed attributes, and KVM would normally allow for 2MB NPT mappings again, but this is actually not allowed because the RMP table mappings are 4K and cannot be promoted on the hypervisor side, so the NPT mappings must still be limited to 4K to match this.
Add a hook to determine the max NPT mapping size in situations like this.
Message-ID: <[email protected]>
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 23 +++++++++++++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -140,6 +140,7 @@ KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL(alloc_apic_backing_page) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL_RET0(private_max_mapping_level) KVM_X86_OP_OPTIONAL(gmem_invalidate) #undef KVM_X86_OP
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1816,6 +1816,7 @@ struct kvm_x86_ops {void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu);int (*gmem_prepare)(struct kvm *kvm,kvm_pfn_t pfn,gfn_t gfn,int max_order);void (*gmem_invalidate)(kvm_pfn_t start,kvm_pfn_t end); +int (*private_max_mapping_level)(struct kvm *kvm,kvm_pfn_t pfn); };struct kvm_x86_nested_ops {
arch/x86/kvm/mmu/mmu.c +21 -2
@@ -4271,6 +4271,25 @@ static inline u8 kvm_max_level_for_order(int order)return PG_LEVEL_4K ; } +static u8 kvm_max_private_mapping_level (struct kvm *kvm,kvm_pfn_t pfn, +u8 max_level,int gmem_order) +{ +u8 req_max_level; + +if (max_level ==PG_LEVEL_4K ) +return PG_LEVEL_4K ; + + max_level =min (kvm_max_level_for_order (gmem_order), max_level); +if (max_level ==PG_LEVEL_4K ) +return PG_LEVEL_4K ; + + req_max_level =static_call (kvm_x86_private_max_mapping_level)(kvm, pfn); +if (req_max_level) + max_level =min (max_level, req_max_level); + +return req_max_level; +} +static int kvm_faultin_pfn_private (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {
@@ -4288,9 +4307,9 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu,return r; } - fault->max_level =min (kvm_max_level_for_order (max_order), - fault->max_level); fault->map_writable = !(fault->slot->flags &KVM_MEM_READONLY ); + fault->max_level =kvm_max_private_mapping_level (vcpu->kvm, fault->pfn, + fault->max_level, max_order);return RET_PF_CONTINUE ; }
Signature · 1 line
2.43.0
Quoted 5 lines · Paolo Bonzini
This is the hopefully final version of the gmem common API patches, adding target-independent functionality and hooks that are needed by SEV-SNP and TDX.
Changes from https://patchew.org/linux/[email protected]/:
I tried those patches and confirmed that now kvm_gmem_populate() and private_max_mapping_level() hook work for TDX KVM without further change.
Thank you for updating them.
Signature · 1 line
Isaku Yamahata <[email protected]>