KVM: Restricted mapping of guest_memfd at the host and arm64 support
Subsystems (6)
v6 cover KVM: Restricted mapping of guest_memfd at the host and arm64 support
This series adds restricted mmap() support to guest_memfd, as well as support for guest_memfd on arm64. Please see v3 for the context [1].
Main changes since v5 [2]:
- Freeze folio refcounts when checking them to avoid races (Kirill, Vlastimili, Ackerley)
- Handle invalidation (e.g., on truncation) of potentially shared memory (Ackerley)
- Rebased on the KVM: Mapping guest_memfd backed memory at the host for software protected VMs series [3], which entails renaming of MAPPABLE to SHAREABLE and a rebase on Linux 6.14-rc7.
The state diagram that uses the new states in this patch series, and how they would interact with sharing/unsharing in pKVM [4].
Cheers,
/fuad
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lpc.events/event/18/contributions/1758/attachments/1457/3699/Guestmemfd%20folio%20state%20page_type.pdf
Ackerley Tng (2):
KVM: guest_memfd: Make guest mem use guest mem inodes instead of
anonymous inodes
KVM: guest_memfd: Track folio sharing within a struct kvm_gmem_private
Fuad Tabba (5):
KVM: guest_memfd: Introduce kvm_gmem_get_pfn_locked(), which retains
the folio lock
KVM: guest_memfd: Folio sharing states and functions that manage their
transition
KVM: guest_memfd: Restore folio state after final folio_put()
KVM: guest_memfd: Handle invalidation of shared memory
KVM: guest_memfd: Add a guest_memfd() flag to initialize it as shared
Documentation/virt/kvm/api.rst | 4 + include/linux/kvm_host.h | 56 +- include/uapi/linux/kvm.h | 1 + include/uapi/linux/magic.h | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 7 +- virt/kvm/guest_memfd.c | 589 ++++++++++++++++-- virt/kvm/kvm_main.c | 62 ++ 7 files changed, 682 insertions(+), 38 deletions(-)
base-commit: 1ea0414b447c8c96e6a6f6f953323c3df71b85a6
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
PATCH v6 1/7 KVM: guest_memfd: Make guest mem use guest mem inodes instead of anonymous inodes
From: Ackerley Tng <[email protected]>
Using guest mem inodes allows us to store metadata for the backing memory on the inode. Metadata will be added in a later patch to support HugeTLB pages.
Metadata about backing memory should not be stored on the file, since the file represents a guest_memfd's binding with a struct kvm, and metadata about backing memory is not unique to a specific binding and struct kvm.
--- include/uapi/linux/magic.h | 1 + virt/kvm/guest_memfd.c | 130 +++++++++++++++++++++++++++++++------ 2 files changed, 111 insertions(+), 20 deletions(-)
include/uapi/linux/magic.h +1 -0
@@ -103,5 +103,6 @@#define DEVMEM_MAGIC 0x454d444d/* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d/* "SECM" */ #define PID_FS_MAGIC 0x50494446/* "PIDF" */ +#define GUEST_MEMORY_MAGIC 0x474d454d/* "GMEM" */ #endif /* __LINUX_MAGIC_H__ */
virt/kvm/guest_memfd.c +110 -20
177 lines not on this page · raw message
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
PATCH v6 2/7 KVM: guest_memfd: Introduce kvm_gmem_get_pfn_locked(), which retains the folio lock
In reply to Fuad TabbaCreate a new variant of kvm_gmem_get_pfn(), which retains the folio lock if it returns successfully. This is needed in subsequent patches to protect against races when checking whether a folio can be shared with the host.
--- include/linux/kvm_host.h | 11 +++++++++++ virt/kvm/guest_memfd.c | 27 ++++++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-)
include/linux/kvm_host.h +11 -0
@@ -2535,6 +2535,9 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot,gfn_t gfn,kvm_pfn_t *pfn,struct page **page,int *max_order); +int kvm_gmem_get_pfn_locked (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,struct page **page, +int *max_order);#else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn,
@@ -2544,6 +2547,14 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,KVM_BUG_ON (1 , kvm); return -EIO; } +static inline int kvm_gmem_get_pfn_locked (struct kvm *kvm, +struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn, +struct page **page,int *max_order) +{ +KVM_BUG_ON (1 , kvm); +return -EIO ; +}#endif /* CONFIG_KVM_PRIVATE_MEM */ #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE
virt/kvm/guest_memfd.c +20 -7
@@ -802,9 +802,9 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,return folio; } -int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, -gfn_t gfn,kvm_pfn_t *pfn,struct page **page, -int *max_order) +int kvm_gmem_get_pfn_locked (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,struct page **page, +int *max_order) {pgoff_t index =kvm_gmem_get_index (slot, gfn);struct file *file =kvm_gmem_get_file (slot);
@@ -824,17 +824,30 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,if (!is_prepared) r =kvm_gmem_prepare_folio (kvm, slot, gfn, folio); -folio_unlock (folio); - -if (!r) +if (!r) { *page =folio_file_page (folio, index); -else + }else { +folio_unlock (folio);folio_put (folio); + } out:fput (file);return r; } +EXPORT_SYMBOL_GPL (kvm_gmem_get_pfn_locked); + +int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,struct page **page, +int *max_order) +{ +int r =kvm_gmem_get_pfn_locked (kvm, slot, gfn, pfn, page, max_order); + +if (!r) +unlock_page (*page); + +return r; +}EXPORT_SYMBOL_GPL (kvm_gmem_get_pfn);#ifdef CONFIG_KVM_GENERIC_PRIVATE_MEM
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
PATCH v6 3/7 KVM: guest_memfd: Track folio sharing within a struct kvm_gmem_private
In reply to Fuad TabbaFrom: Ackerley Tng <[email protected]>
Track guest_memfd folio sharing state within the inode, since it is a property of the guest_memfd's memory contents.
The guest_memfd PRIVATE memory attribute is not used for two reasons. It reflects the userspace expectation for the memory state, and therefore can be toggled by userspace. Also, although each guest_memfd file has a 1:1 binding with a KVM instance, the plan is to allow multiple files per inode, e.g. to allow intra-host migration to a new KVM instance, without destroying guest_memfd.
--- virt/kvm/guest_memfd.c | 56 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 5 deletions(-)
virt/kvm/guest_memfd.c +51 -5
@@ -17,6 +17,17 @@ struct kvm_gmem {struct list_head entry; }; +struct kvm_gmem_inode_private { +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +struct xarray shared_offsets; +#endif +}; + +static struct kvm_gmem_inode_private *kvm_gmem_private (struct inode *inode) +{ +return inode->i_mapping->i_private_data; +} +#ifdef CONFIG_KVM_GMEM_SHARED_MEM void kvm_gmem_handle_folio_put (struct folio *folio) {
@@ -324,8 +335,28 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)return gfn - slot->base_gfn + slot->gmem.pgoff; } +static void kvm_gmem_evict_inode (struct inode *inode) +{ +struct kvm_gmem_inode_private *private =kvm_gmem_private (inode); + +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +/* +* .evict_inode can be called before private data is set up if there are +* issues during inode creation. +*/ +if (private) +xa_destroy (&private->shared_offsets); +#endif + +truncate_inode_pages_final (inode->i_mapping); + +kfree (private); +clear_inode (inode); +} +static const struct super_operations kvm_gmem_super_operations = { - .statfs = simple_statfs, + .statfs = simple_statfs, + .evict_inode = kvm_gmem_evict_inode, };static int kvm_gmem_init_fs_context (struct fs_context *fc)
@@ -553,6 +584,7 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, loff_t size, u64 flags) {const struct qstr qname =QSTR_INIT (name,strlen (name)); +struct kvm_gmem_inode_private *private;struct inode *inode;int err;
@@ -561,10 +593,19 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name,return inode; err =security_inode_init_security_anon (inode, &qname,NULL ); -if (err) { -iput (inode); -return ERR_PTR (err); - } +if (err) + goto out; + + err = -ENOMEM ; + private =kzalloc (sizeof (*private),GFP_KERNEL ); +if (!private) + goto out; + +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +xa_init (&private->shared_offsets); +#endif + + inode->i_mapping->i_private_data = private; inode->i_private = (void *)(unsigned long )flags; inode->i_op = &kvm_gmem_iops;
@@ -577,6 +618,11 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name,WARN_ON_ONCE (!mapping_unevictable (inode->i_mapping));return inode; + +out: +iput (inode); + +return ERR_PTR (err); } static struct file *kvm_gmem_inode_create_getfile(void *priv, loff_t size,
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
PATCH v6 4/7 KVM: guest_memfd: Folio sharing states and functions that manage their transition
In reply to Fuad TabbaTo allow in-place sharing of guest_memfd folios with the host, guest_memfd needs to track their sharing state, because mapping of shared folios will only be allowed where it safe to access these folios. It is safe to map and access these folios when explicitly shared with the host, or potentially if not yet exposed to the guest (e.g., at initialization).
This patch introduces sharing states for guest_memfd folios as well as the functions that manage transitioning between those states.
--- include/linux/kvm_host.h | 39 +++++++- virt/kvm/guest_memfd.c | 188 ++++++++++++++++++++++++++++++++++++--- virt/kvm/kvm_main.c | 62 +++++++++++++ 3 files changed, 275 insertions(+), 14 deletions(-)
include/linux/kvm_host.h +38 -1
@@ -2600,7 +2600,44 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,#endif #ifdef CONFIG_KVM_GMEM_SHARED_MEM +int kvm_gmem_set_shared (struct kvm *kvm,gfn_t start,gfn_t end); +int kvm_gmem_clear_shared (struct kvm *kvm,gfn_t start,gfn_t end); +int kvm_gmem_slot_set_shared (struct kvm_memory_slot *slot,gfn_t start, +gfn_t end); +int kvm_gmem_slot_clear_shared (struct kvm_memory_slot *slot,gfn_t start, +gfn_t end); +bool kvm_gmem_slot_is_guest_shared (struct kvm_memory_slot *slot,gfn_t gfn);void kvm_gmem_handle_folio_put (struct folio *folio); -#endif +#else +static inline int kvm_gmem_set_shared (struct kvm *kvm,gfn_t start,gfn_t end) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} +static inline int kvm_gmem_clear_shared (struct kvm *kvm,gfn_t start, +gfn_t end) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} +static inline int kvm_gmem_slot_set_shared (struct kvm_memory_slot *slot, +gfn_t start,gfn_t end) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} +static inline int kvm_gmem_slot_clear_shared (struct kvm_memory_slot *slot, +gfn_t start,gfn_t end) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} +static inline bool kvm_gmem_slot_is_guest_shared (struct kvm_memory_slot *slot, +gfn_t gfn) +{ +WARN_ON_ONCE (1 ); +return false; +} +#endif /* CONFIG_KVM_GMEM_SHARED_MEM */ #endif
virt/kvm/guest_memfd.c +175 -13
209 lines not on this page · raw message
virt/kvm/kvm_main.c +62 -0
68 lines not on this page · raw message
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
PATCH v6 5/7 KVM: guest_memfd: Restore folio state after final folio_put()
In reply to Fuad TabbaBefore transitioning a guest_memfd folio to unshared, thereby disallowing access by the host and allowing the hypervisor to transition its view of the guest page as private, we need to be sure that the host doesn't have any references to the folio.
This patch uses the guest_memfd folio type to register a callback that informs the guest_memfd subsystem when the last reference is dropped, therefore knowing that the host doesn't have any remaining references.
---
The function kvm_slot_gmem_register_callback() isn't used in this series. It will be used later in code that performs unsharing of memory. I have tested it with pKVM, based on downstream code [*]. It's included in this RFC since it demonstrates the plan to handle unsharing of private folios.
[*] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/guestmem-6.13-v6-pkvm
--- include/linux/kvm_host.h | 6 ++ virt/kvm/guest_memfd.c | 142 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 147 insertions(+), 1 deletion(-)
include/linux/kvm_host.h +6 -0
@@ -2607,6 +2607,7 @@ int kvm_gmem_slot_set_shared(struct kvm_memory_slot *slot, gfn_t start,int kvm_gmem_slot_clear_shared (struct kvm_memory_slot *slot,gfn_t start,gfn_t end);bool kvm_gmem_slot_is_guest_shared (struct kvm_memory_slot *slot,gfn_t gfn); +int kvm_gmem_slot_register_callback (struct kvm_memory_slot *slot,gfn_t gfn);void kvm_gmem_handle_folio_put (struct folio *folio);#else static inline int kvm_gmem_set_shared (struct kvm *kvm,gfn_t start,gfn_t end)
@@ -2638,6 +2639,11 @@ static inline bool kvm_gmem_slot_is_guest_shared(struct kvm_memory_slot *slot,WARN_ON_ONCE (1 );return false; } +static inline int kvm_gmem_slot_register_callback (struct kvm_memory_slot *slot,gfn_t gfn) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +}#endif /* CONFIG_KVM_GMEM_SHARED_MEM */ #endif
virt/kvm/guest_memfd.c +141 -1
162 lines not on this page · raw message
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
Re: [PATCH v6 5/7] KVM: guest_memfd: Restore folio state after final folio_put()
Quoted 17 lines · Fuad Tabba
...
virt/kvm/guest_memfd.c +13
-1+/* +* Callback function for __folio_put(), i.e., called once all references by the +* host to the folio have been dropped. This allows gmem to transition the state +* of the folio to shared with the guest, and allows the hypervisor to continue +* transitioning its state to private, since the host cannot attempt to access +* it anymore. +*/ void kvm_gmem_handle_folio_put (struct folio *folio) { -WARN_ONCE (1 ,"A placeholder that shouldn't trigger. Work in progress." ); +struct address_space *mapping; +struct xarray *shared_offsets; +struct inode *inode; +pgoff_t index; +void *xval; +
virt/kvm/guest_memfd.c static int kvm_gmem_offset_range_clear_shared(struct inode *inode, · line 638
Quoted 10 lines
virt/kvm/guest_memfd.c +10
-0+ mapping = folio->mapping; +if (WARN_ON_ONCE (!mapping)) +return ; + + inode = mapping->host; + index = folio->index; + shared_offsets = &kvm_gmem_private (inode)->shared_offsets; + xval =xa_mk_value (KVM_GMEM_GUEST_SHARED ); + +filemap_invalidate_lock (inode->i_mapping);
As discussed in the guest_memfd upstream, folio_put can happen from atomic context [1], so we need a way to either defer the work outside kvm_gmem_handle_folio_put() (which is very likely needed to handle hugepages and merge operation) or ensure to execute the logic using synchronization primitives that will not sleep.
[1] https://elixir.bootlin.com/linux/v6.14-rc6/source/include/linux/mm.h#L1483
Quoted 11 lines
virt/kvm/guest_memfd.c +5
-1+folio_lock (folio); +kvm_gmem_restore_pending_folio (folio, inode); +folio_unlock (folio); +WARN_ON_ONCE (xa_err (xa_store (shared_offsets, index, xval,GFP_KERNEL ))); +filemap_invalidate_unlock (inode->i_mapping); }EXPORT_SYMBOL_GPL (kvm_gmem_handle_folio_put); --2.49.0.rc1.451.g8f38331e32-goog
Re: [PATCH v6 5/7] KVM: guest_memfd: Restore folio state after final folio_put()
Hi Vishal,
Quoted 27 lines · Vishal Annapurve
Quoted 25 lines · Fuad Tabba
...
virt/kvm/guest_memfd.c +21
-1+/* +* Callback function for __folio_put(), i.e., called once all references by the +* host to the folio have been dropped. This allows gmem to transition the state +* of the folio to shared with the guest, and allows the hypervisor to continue +* transitioning its state to private, since the host cannot attempt to access +* it anymore. +*/ void kvm_gmem_handle_folio_put (struct folio *folio) { -WARN_ONCE (1 ,"A placeholder that shouldn't trigger. Work in progress." ); +struct address_space *mapping; +struct xarray *shared_offsets; +struct inode *inode; +pgoff_t index; +void *xval; + + mapping = folio->mapping; +if (WARN_ON_ONCE (!mapping)) +return ; + + inode = mapping->host; + index = folio->index; + shared_offsets = &kvm_gmem_private (inode)->shared_offsets; + xval =xa_mk_value (KVM_GMEM_GUEST_SHARED );
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + filemap_invalidate_lock(inode->i_mapping);As discussed in the
guest_memfdupstream,folio_putcan happen from atomic context [1], so we need a way to either defer the work outsidekvm_gmem_handle_folio_put()(which is very likely needed to handle hugepages and merge operation) or ensure to execute the logic using synchronization primitives that will not sleep.
Thanks for pointing this out. For now, rather than deferring (which we'll come to when hugepages come into play), I think this would be possible to resolve by ensuring we have exclusive access* to the folio instead, and using that to ensure that we can access the shared_offsets maps.
* By exclusive access I mean either holding the folio lock, or knowing that no one else has references to the folio (which is the case when kvm_gmem_handle_folio_put() is called).
I'll try to respin something in time for folks to look at it before the next sync.
Cheers,
/fuad
Quoted 13 lines
[1] https://elixir.bootlin.com/linux/v6.14-rc6/source/include/linux/mm.h#L1483
Quoted 11 lines
virt/kvm/guest_memfd.c +5
-1+folio_lock (folio); +kvm_gmem_restore_pending_folio (folio, inode); +folio_unlock (folio); +WARN_ON_ONCE (xa_err (xa_store (shared_offsets, index, xval,GFP_KERNEL ))); +filemap_invalidate_unlock (inode->i_mapping); }EXPORT_SYMBOL_GPL (kvm_gmem_handle_folio_put); --2.49.0.rc1.451.g8f38331e32-goog
Re: [PATCH v6 5/7] KVM: guest_memfd: Restore folio state after final folio_put()
Quoted 40 lines · Fuad Tabba
Hi Vishal,
Quoted 27 lines · Vishal Annapurve
Quoted 25 lines · Fuad Tabba
...
virt/kvm/guest_memfd.c +21
-1+/* +* Callback function for __folio_put(), i.e., called once all references by the +* host to the folio have been dropped. This allows gmem to transition the state +* of the folio to shared with the guest, and allows the hypervisor to continue +* transitioning its state to private, since the host cannot attempt to access +* it anymore. +*/ void kvm_gmem_handle_folio_put (struct folio *folio) { -WARN_ONCE (1 ,"A placeholder that shouldn't trigger. Work in progress." ); +struct address_space *mapping; +struct xarray *shared_offsets; +struct inode *inode; +pgoff_t index; +void *xval; + + mapping = folio->mapping; +if (WARN_ON_ONCE (!mapping)) +return ; + + inode = mapping->host; + index = folio->index; + shared_offsets = &kvm_gmem_private (inode)->shared_offsets; + xval =xa_mk_value (KVM_GMEM_GUEST_SHARED );Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + filemap_invalidate_lock(inode->i_mapping);As discussed in the
guest_memfdupstream,folio_putcan happen from atomic context [1], so we need a way to either defer the work outsidekvm_gmem_handle_folio_put()(which is very likely needed to handle hugepages and merge operation) or ensure to execute the logic using synchronization primitives that will not sleep.
Quoted 2 lines
Thanks for pointing this out. For now, rather than deferring (which we'll come to when hugepages come into play), I think this would be
FWIW, with SNP, it's only possible to unsplit an RMP entry if the guest cooperates with re-validating/re-accepting the memory at a higher order. Currently, this guest support is not implemented in linux.
So, if we were to opportunistically unsplit hugepages, we'd zap the mappings in KVM, let it fault in at a higher order so we could reduce TLB misses, and then KVM would (via kvm_x86_call(private_max_mapping_level)(kvm, pfn) find that the RMP entry is still split to 4K, and remap everything right back to the 4K granularity it was already at to begin with.
TDX seems to have a bit more flexibility in being able to 'unsplit'/promote private ranges back up to higher orders, so it could potentially benefit from doing things opportunistically...
However, ideally...the guest would just avoid unecessarily carving up ranges to begin with and pack all it's shared mappings into smaller GPA ranges. Then, all this unsplitting of huge pages could be completely avoided until cleanup/truncate time. So maybe even for hugepages we should just plan to do things this way, at least as a start?
Quoted 10 lines
possible to resolve by ensuring we have exclusive access* to the folio instead, and using that to ensure that we can access the
shared_offsetsmaps.* By exclusive access I mean either holding the folio lock, or knowing that no one else has references to the folio (which is the case when
kvm_gmem_handle_folio_put()is called).I'll try to respin something in time for folks to look at it before the next sync.
Thanks for posting. I was looking at how to get rid of filemap_invalidate_lock() from conversion path, and having that separate rwlock seems to resolve a lot of the potential races I was looking at. I'm working on rebasing SNP 2MB support on top of your v7 series now.
-Mike
Quoted 18 lines
Cheers,
/fuadQuoted 13 lines
[1] https://elixir.bootlin.com/linux/v6.14-rc6/source/include/linux/mm.h#L1483
Quoted 11 lines
virt/kvm/guest_memfd.c +5
-1+folio_lock (folio); +kvm_gmem_restore_pending_folio (folio, inode); +folio_unlock (folio); +WARN_ON_ONCE (xa_err (xa_store (shared_offsets, index, xval,GFP_KERNEL ))); +filemap_invalidate_unlock (inode->i_mapping); }EXPORT_SYMBOL_GPL (kvm_gmem_handle_folio_put); --2.49.0.rc1.451.g8f38331e32-goog
PATCH v6 6/7 KVM: guest_memfd: Handle invalidation of shared memory
In reply to Fuad TabbaWhen guest_memfd backed memory is invalidated, e.g., on punching holes, releasing, ensure that the sharing states are updated and that any folios in a transient state are restored to an appropriate state.
--- virt/kvm/guest_memfd.c | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
virt/kvm/guest_memfd.c +56 -0
@@ -117,6 +117,16 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index)return filemap_grab_folio (inode->i_mapping, index); } +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +static void kvm_gmem_offset_range_invalidate_shared (struct inode *inode, +pgoff_t start,pgoff_t end); +#else +static inline void kvm_gmem_offset_range_invalidate_shared (struct inode *inode, +pgoff_t start,pgoff_t end) +{ +} +#endif +static void kvm_gmem_invalidate_begin (struct kvm_gmem *gmem,pgoff_t start,pgoff_t end) {
@@ -126,6 +136,7 @@ static void kvm_gmem_invalidate_begin(struct kvm_gmem *gmem, pgoff_t start,unsigned long index;xa_for_each_range (&gmem->bindings, index, slot, start, end -1 ) { +struct file *file =READ_ONCE (slot->gmem.file);pgoff_t pgoff = slot->gmem.pgoff;struct kvm_gfn_range gfn_range = {
@@ -145,6 +156,16 @@ static void kvm_gmem_invalidate_begin(struct kvm_gmem *gmem, pgoff_t start, } flush |=kvm_mmu_unmap_gfn_range (kvm, &gfn_range); + +/* +* If this gets called after kvm_gmem_unbind() it means that all +* in-flight operations are gone, and the file has been closed. +*/ +if (file) { +kvm_gmem_offset_range_invalidate_shared (file_inode (file), + gfn_range.start, + gfn_range.end); + } }if (flush)
@@ -509,6 +530,41 @@ static int kvm_gmem_offset_clear_shared(struct inode *inode, pgoff_t index)return r; } +/* +* Callback when invalidating memory that is potentially shared. +* +* Must be called with the filemap (inode->i_mapping) invalidate_lock held. +*/ +static void kvm_gmem_offset_range_invalidate_shared (struct inode *inode, +pgoff_t start,pgoff_t end) +{ +struct xarray *shared_offsets = &kvm_gmem_private (inode)->shared_offsets; +pgoff_t i; + +rwsem_assert_held_write_nolockdep (&inode->i_mapping->invalidate_lock); + +for (i = start; i < end; i++) { +/* +* If the folio is NONE_SHARED, it indicates that it is +* transitioning to private (GUEST_SHARED). Transition it to +* shared (ALL_SHARED) and remove the callback. +*/ +if (xa_to_value (xa_load (shared_offsets, i)) ==KVM_GMEM_NONE_SHARED ) { +struct folio *folio = folio =filemap_lock_folio (inode->i_mapping, i); + +if (!WARN_ON_ONCE (IS_ERR (folio))) { +if (folio_test_guestmem (folio)) +kvm_gmem_restore_pending_folio (folio, inode); + +folio_unlock (folio); +folio_put (folio); + } + } + +xa_erase (shared_offsets, i); + } +} +/* * Marks the range [start, end) as not shared with the host. If the host doesn't * have any references to a particular folio, then that folio is marked as
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
PATCH v6 7/7 KVM: guest_memfd: Add a guest_memfd() flag to initialize it as shared
In reply to Fuad TabbaNot all use cases require guest_memfd() to be shared with the host when first created. Add a new flag, GUEST_MEMFD_FLAG_INIT_SHARED, which when set on KVM_CREATE_GUEST_MEMFD initializes the memory as shared with the host, and therefore mappable by it. Otherwise, memory is private until explicitly shared by the guest with the host.
--- Documentation/virt/kvm/api.rst | 4 ++++ include/uapi/linux/kvm.h | 1 + tools/testing/selftests/kvm/guest_memfd_test.c | 7 +++++-- virt/kvm/guest_memfd.c | 12 ++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-)
Documentation/virt/kvm/api.rst +4 -0
@@ -6386,6 +6386,10 @@ most one mapping per page, i.e. binding multiple memory regions to a single guest_memfd range is not allowed (any number of memory regions can be bound to a single guest_memfd file, but the bound ranges must not overlap). +If the capability KVM_CAP_GMEM_SHARED_MEM is supported, then the flags field +supports GUEST_MEMFD_FLAG_INIT_SHARED, which initializes the memory as shared +with the host, and thereby, mappable by it. + See KVM_SET_USER_MEMORY_REGION2 for additional details. 4.143 KVM_PRE_FAULT_MEMORY
include/uapi/linux/kvm.h +1 -0
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3)#define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) +#define GUEST_MEMFD_FLAG_INIT_SHARED (1UL << 0)struct kvm_create_guest_memfd {__u64 size;
tools/testing/selftests/kvm/guest_memfd_test.c +5 -2
@@ -159,7 +159,7 @@ static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size)static void test_create_guest_memfd_invalid (struct kvm_vm *vm) {size_t page_size =getpagesize (); -uint64_t flag; +uint64_t flag =BIT (0 );size_t size;int fd;
@@ -170,7 +170,10 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm) size); } -for (flag =BIT (0 ); flag; flag <<=1 ) { +if (kvm_has_cap (KVM_CAP_GMEM_SHARED_MEM )) + flag =GUEST_MEMFD_FLAG_INIT_SHARED <<1 ; + +for (; flag; flag <<=1 ) { fd =__vm_create_guest_memfd (vm, page_size, flag); TEST_ASSERT(fd == -1 && errno == EINVAL, "guest_memfd() with flag '0x%lx' should fail with EINVAL",
virt/kvm/guest_memfd.c +12 -0
@@ -1045,6 +1045,15 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) goto err_gmem; } +if (IS_ENABLED (CONFIG_KVM_GMEM_SHARED_MEM ) && + (flags &GUEST_MEMFD_FLAG_INIT_SHARED )) { + err =kvm_gmem_offset_range_set_shared (file_inode (file),0 , size >>PAGE_SHIFT ); +if (err) { +fput (file); + goto err_gmem; + } + } +kvm_get_kvm (kvm); gmem->kvm = kvm;xa_init (&gmem->bindings);
@@ -1066,6 +1075,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)u64 flags = args->flags;u64 valid_flags =0 ; +if (IS_ENABLED (CONFIG_KVM_GMEM_SHARED_MEM )) + valid_flags |=GUEST_MEMFD_FLAG_INIT_SHARED ; +if (flags & ~valid_flags)return -EINVAL ;
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog