KVM: guest_memfd: Add a common framework for device memory providers
Subsystems (5)
v5 cover KVM: guest_memfd: Add a common framework for device memory providers
Confidential device assignment needs guest_memfd to manage device MMIO as well as RAM. Device resources such as PCI BARs supply PFNs directly, and their protected mappings need architecture-specific map and unmap operations.
This series adds the common guest_memfd support needed by device memory providers. It builds on the resource-provider API [1] and the existing support for folios and RAM PFNs.
The VFIO provider, IOMMUFD integration and Arm CCA implementation are available together at:
https://git.gitlab.arm.com/linux-arm/linux-cca.git cca/topics/cca-da-gmem-backend-v5
This series depends on the resource-provider groundwork from:
[1] https://lore.kernel.org/all/[email protected]
Notes:
* Codex assisted with implementation, review and series organization.
Changes from v4:
https://lore.kernel.org/all/[email protected]
* Leave VFIO changes out of this series to agree on guest_memfd changes that work with all providers.
* Replace the separate folio, RAM PFN and device operation tables with provider callbacks. Add callbacks for attribute validation, allocation, release, host mmap and host faults, with core helpers for providers that omit them.
* Use get_pfn() for both folios and PFNs through guest_memfd_handle. The handle carries the PFN, its attributes, contiguous range order and an optional folio reference. The core manages folio locking and references.
* Add nonallocating folio lookup through lookup_pfn_range() and require PFN providers to supply it alongside get_pfn(). Report PFN attributes separately from KVM shared/private memory attributes, including an MMIO flag.
* Remove convert_attributes() and prepare_pfn(). Make existing pages shared through provider range lookup, independently of VM bindings, before publishing shared attributes. Leave private conversion to guest faults or prefaults
* Pass KVM explicitly to attach() for use during attachment only.
* Use the provider mmap and fault callbacks for shared device mappings. Set noncached, decrypted protections in the VMA
Changes from v3:
https://lore.kernel.org/all/[email protected]
* Include the VFIO PCI provider and IOMMUFD vDEVICE integration alongside the common guest_memfd framework; v3 posted only the common framework.
* Replace make_private()/make_shared() with an optional convert_attributes() callback, and prepare_private_pfn() with prepare_pfn(). Pass attributes to both to handle shared and private memory at attribute-update or PFN-lookup time.
* Pass the device context directly to provider attach(), removing kvm_gmem_get_device() and avoiding a VFIO dependency on KVM symbols.
* Store has_private_ranges in guest_memfd_device so providers can read the core-maintained summary directly instead of calling has_private().
* Serialize VFIO file operations with provider setup and release using gmem_lock. Reject existing BAR mappings, dma-buf exports and ioeventfd handlers, and block direct VFIO BAR access while guest_memfd uses the device. Shared userspace mappings go through guest_memfd.
* Require D0 and hold a runtime-PM reference while guest_memfd uses the device. Block VFIO reset, power-state changes and userspace changes to Memory Enable or Bus Master Enable during that time.
Changes from v2:
https://lore.kernel.org/all/[email protected]
* Rebase on the v2 alternative-provider series and separate the common guest_memfd framework from the VFIO, IOMMUFD and Arm CCA integration.
* Separate folio, RAM PFN and device MMIO handling, with common locking, invalidation and attribute updates.
* Replace prepare_conversion()/finish_conversion() with non-failing lock()/unlock() callbacks.
* Convert device memory through ordinary file-offset attribute updates. Derive GPA from memslot bindings and PA from provider PFNs, removing the device request object.
* Allow shared and private device PFN lookup in the common layer. Include both mapping types in generic invalidation and leave architecture mapping policy to the backend.
* Support device range revocation through the common revoke path without reclaiming MMIO PFNs or changing attributes.
* Add shared device mmap through the existing get_pfn() fault path. Keep VMA protections and flags in the core
* Require only get_pfn() for device providers; lookup_pfn_range() remains required for RAM PFN providers.
Changes from v1:
https://lore.kernel.org/all/[email protected]
* Require GUEST_MEMFD_FLAG_MMAP to allow VFIO BAR mmap while a provider is attached; disable it by default.
* Use the common guest_memfd_provider_operations for folio and PFN providers. Select operations through the resource mapping superblock and forward VFIO operations to the device driver.
Aneesh Kumar K.V (Arm) (16):
KVM: guest_memfd: Add a handle for provider PFNs
KVM: guest_memfd: Get PFNs through common provider operations
KVM: guest_memfd: Let providers coordinate mapping changes
KVM: guest_memfd: Add provider PFN range lookup
KVM: guest_memfd: Let providers validate attribute changes
KVM: guest_memfd: Share memory through provider range lookup
KVM: guest_memfd: Release and revoke memory through provider
operations
KVM: guest_memfd: Use provider operations for fallocate allocation
KVM: guest_memfd: Use provider operations for host mappings and faults
KVM: guest_memfd: Let providers prepare memory regions
KVM: guest_memfd: Pass KVM explicitly to provider attach
KVM: guest_memfd: Support devices through common provider operations
KVM: guest_memfd: Notify providers after private attribute updates
KVM: guest_memfd: Enable shared host mappings for device providers
KVM: guest_memfd: Notify providers during file release
KVM: guest_memfd: Invalidate mappings before memslot unbind
Documentation/virt/kvm/api.rst | 6 +- include/linux/guest_memfd.h | 152 ++++- include/linux/kvm_host.h | 5 + mm/shmem.c | 3 +- samples/kvm/mem_provider_sample.c | 22 +- virt/kvm/guest_memfd.c | 996 +++++++++++++++++++++--------- 6 files changed, 868 insertions(+), 316 deletions(-)
Signature · 1 line
2.43.0
RFC PATCH v5 1/16 KVM: guest_memfd: Add a handle for provider PFNs
Add an MMIO PFN flag, separate from KVM memory attributes.
Add struct guest_memfd_handle to hold a PFN, its attributes, the order of the contiguous range and an optional folio reference. Later patches use this handle for both folio and PFN providers.
--- include/linux/guest_memfd.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
include/linux/guest_memfd.h +17 -0
@@ -2,13 +2,30 @@#ifndef _LINUX_GUEST_MEMFD_H#define _LINUX_GUEST_MEMFD_H +#include <linux/bits.h> #include <linux/types.h> +#define GUEST_MEMFD_PFN_MMIO BIT_ULL(0) +struct file ;struct folio ;struct inode ;struct mempolicy ; +/** +* struct guest_memfd_handle - PFN information and an optional folio reference +* @pfn: Physical frame number +* @pfn_attrs: GUEST_MEMFD_PFN_* memory type and permissions +* @max_order: Largest order of a contiguous range with the same PFN attributes +* @folio: Optional locked folio reference, managed by the core +*/ +struct guest_memfd_handle { +unsigned long pfn; +u64 pfn_attrs; +int max_order; +struct folio *folio; +}; +/** * struct guest_memfd_provider_operations - Operations for external memory providers * @attach: Attach to a resource file, perform filesystem-specific validation,
Signature · 1 line
2.43.0
RFC PATCH v5 2/16 KVM: guest_memfd: Get PFNs through common provider operations
In reply to Aneesh Kumar K.V (Arm)Use get_pfn() for both folios and external PFNs. Folio helpers return a locked folio reference in the handle. The core unlocks and releases that reference. PFN providers keep memory stable under the inode invalidate lock.
Declare native provider operations in their static table. Install the folio get_pfn() helper inside KVM for external folio providers, so built-in shmem does not depend on modular KVM.
--- include/linux/guest_memfd.h | 23 +-- samples/kvm/mem_provider_sample.c | 10 +- virt/kvm/guest_memfd.c | 245 +++++++++++++++++++----------- 3 files changed, 176 insertions(+), 102 deletions(-)
include/linux/guest_memfd.h +14 -9
@@ -18,6 +18,11 @@ struct mempolicy;* @pfn_attrs: GUEST_MEMFD_PFN_* memory type and permissions * @max_order: Largest order of a contiguous range with the same PFN attributes * @folio: Optional locked folio reference, managed by the core +* +* The core unlocks the folio before dropping its reference. Population keeps +* the reference while its callback runs without the folio lock. A host folio +* fault instead transfers the locked reference to the MM. PFN providers leave +* @folio NULL and keep their PFNs stable under the inode invalidate lock. */ struct guest_memfd_handle {unsigned long pfn;
@@ -35,13 +40,13 @@ struct guest_memfd_handle {* Must return a pointer to a struct folio on success, or an * ERR_PTR() error code on failure. Must never return NULL. * @invalidate_folio: Notify provider that a folio has been invalidated. -* @get_pfn: Obtain a PFN for a given index. The provider must ensure backing -* memory is present and is responsible for its contents, such as -* zeroing if necessary. On success, return 0 with the physical frame -* number in @pfn and, if @max_order is non-NULL, the maximum page -* order of contiguous backing memory containing @pfn. Returns a -* negative error code on failure. The provider must define -* @lookup_pfn_range when @get_pfn is defined. +* @get_pfn: Get the PFN for initialized memory at an index. Return zero on +* success or a negative error code. Called with the inode +* invalidate lock held. Fill the handle with its PFN, attributes +* and order. Folio helpers also return a locked folio reference in +* guest_memfd_handle.folio for the core to unlock and release. +* PFN providers leave folio NULL and keep their PFNs stable until +* the caller releases the inode invalidate lock. * @lookup_pfn_range: Look up the PFN for a given index. Returns 0 on success * with the physical frame number in @pfn and the number of * contiguous pages the mapping is valid for in @nr_pages.
@@ -64,8 +69,8 @@ struct guest_memfd_provider_operations {struct folio *(*alloc_folio)(void *provider,pgoff_t index,struct mempolicy *mpol);void (*invalidate_folio)(void *provider,struct folio *folio); -int (*get_pfn)(void *provider,pgoff_t index,unsigned long *pfn, -int *max_order); +int (*get_pfn)(struct inode *inode,void *provider,pgoff_t index, +struct guest_memfd_handle *handle);int (*lookup_pfn_range)(void *provider,pgoff_t index,unsigned long *pfn,size_t *nr_pages);void (*put_pfn_range)(void *provider,pgoff_t index,size_t nr_pages);
samples/kvm/mem_provider_sample.c +5 -5
@@ -178,8 +178,8 @@ static void mps_gmem_release(void *provider)fput (resource_file); } -static int mps_gmem_get_pfn (void *provider,pgoff_t index,unsigned long *pfn, -int *max_order) +static int mps_gmem_get_pfn (struct inode *inode,void *provider, +pgoff_t index,struct guest_memfd_handle *handle) {struct file *resource_file = provider;struct mps_info *info = resource_file->private_data;
@@ -190,9 +190,9 @@ static int mps_gmem_get_pfn(void *provider, pgoff_t index, unsigned long *pfn,if (test_bit (index, info->absent))return -EFAULT ; - *pfn = info->base_pfn + index; -if (max_order) - *max_order =0 ; + handle->pfn = info->base_pfn + index; + handle->max_order =0 ; + handle->pfn_attrs =0 ;return 0 ; }
virt/kvm/guest_memfd.c +157 -88
380 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v5 3/16 KVM: guest_memfd: Let providers coordinate mapping changes
In reply to Aneesh Kumar K.V (Arm)Add optional paired lock() and unlock() callbacks so providers can prevent memory changes during guest invalidation and attribute conversion. Reject providers that supply only one callback.
Take the provider lock with the inode invalidate lock held for write, before guest invalidation. Release it after invalidation and any successful attribute publication. Use the same locking for revocation and final inode cleanup.
This lock is separate from the lock taken by get_pfn() for an individual PFN. Existing providers can omit both callbacks.
--- include/linux/guest_memfd.h | 8 ++++++ virt/kvm/guest_memfd.c | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+)
include/linux/guest_memfd.h +8 -0
@@ -36,6 +36,12 @@ struct guest_memfd_handle {* @attach: Attach to a resource file, perform filesystem-specific validation, * and return an opaque provider context. * @release: Release provider context and unpin any resources. +* @lock: Optional provider lock for mapping and attribute changes. Called +* with the inode invalidate lock held for write, before guest +* invalidation starts. +* @unlock: Release the provider lock taken by lock(). Called after guest +* invalidation ends and any successful attribute publication, with +* the inode invalidate lock still held. * @alloc_folio: Allocate an uninserted folio for a given index and NUMA policy. * Must return a pointer to a struct folio on success, or an * ERR_PTR() error code on failure. Must never return NULL.
@@ -66,6 +72,8 @@ struct guest_memfd_handle {struct guest_memfd_provider_operations {void *(*attach)(struct inode *gmem_inode,struct file *resource_file);void (*release)(void *provider); +void (*lock)(void *provider); +void (*unlock)(void *provider);struct folio *(*alloc_folio)(void *provider,pgoff_t index,struct mempolicy *mpol);void (*invalidate_folio)(void *provider,struct folio *folio);
virt/kvm/guest_memfd.c +50 -0
100 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v5 4/16 KVM: guest_memfd: Add provider PFN range lookup
In reply to Aneesh Kumar K.V (Arm)Provide nonallocating folio range lookup and carry PFN attributes separately from requested KVM memory attributes
--- include/linux/guest_memfd.h | 10 ++++-- samples/kvm/mem_provider_sample.c | 6 ++-- virt/kvm/guest_memfd.c | 60 ++++++++++++++++++++++++++----- 3 files changed, 63 insertions(+), 13 deletions(-)
include/linux/guest_memfd.h +7 -3
@@ -60,7 +60,10 @@ struct guest_memfd_handle {* is present. If backing memory is not available, return * -ENOENT. Return a negative error code on other failure. * The provider must define @get_pfn when @lookup_pfn_range -* is defined. +* is defined, or omit both to use the folio helper. +* Lookup must not allocate memory or prepare it for access. +* PFNs remain stable under the inode invalidate lock and +* any provider lock held by conversion/reclaim callers. * @put_pfn_range: Release a range of PFNs previously obtained for a given * index. The provider does not need to ensure that backing * memory is present. If backing memory is not available, the
@@ -79,8 +82,9 @@ struct guest_memfd_provider_operations {void (*invalidate_folio)(void *provider,struct folio *folio);int (*get_pfn)(struct inode *inode,void *provider,pgoff_t index,struct guest_memfd_handle *handle); -int (*lookup_pfn_range)(void *provider,pgoff_t index, -unsigned long *pfn,size_t *nr_pages); +int (*lookup_pfn_range)(struct inode *inode,void *provider,pgoff_t index, +unsigned long *pfn,size_t *nr_pages, +u64 *pfn_attrs);void (*put_pfn_range)(void *provider,pgoff_t index,size_t nr_pages); };
samples/kvm/mem_provider_sample.c +4 -2
@@ -197,8 +197,9 @@ static int mps_gmem_get_pfn(struct inode *inode, void *provider,return 0 ; } -static int mps_gmem_lookup_pfn_range (void *provider,pgoff_t index, -unsigned long *pfn,size_t *nr_pages) +static int mps_gmem_lookup_pfn_range (struct inode *inode,void *provider, +pgoff_t index,unsigned long *pfn, +size_t *nr_pages,u64 *pfn_attrs) {struct file *resource_file = provider;struct mps_info *info = resource_file->private_data;
@@ -211,6 +212,7 @@ static int mps_gmem_lookup_pfn_range(void *provider, pgoff_t index, *pfn = info->base_pfn + index; *nr_pages =1 ; + *pfn_attrs =0 ;return 0 ; }
virt/kvm/guest_memfd.c +52 -8
110 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v5 5/16 KVM: guest_memfd: Let providers validate attribute changes
In reply to Aneesh Kumar K.V (Arm)Call prepare_attributes() before invalidating guest mappings or changing file attributes. Remove host mappings first when converting to private, so folio validation can check for outstanding references.
--- include/linux/guest_memfd.h | 9 ++++++++ virt/kvm/guest_memfd.c | 43 ++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 15 deletions(-)
include/linux/guest_memfd.h +9 -0
@@ -42,6 +42,13 @@ struct guest_memfd_handle {* @unlock: Release the provider lock taken by lock(). Called after guest * invalidation ends and any successful attribute publication, with * the inode invalidate lock still held. +* @prepare_attributes: Optional memory validation before conversion, under +* the inode invalidate lock for write, before taking +* the provider lock or starting guest invalidation. Host mappings have +* been removed for private conversion. Set @err_index on +* failure; it initially points to the range start. NULL +* selects folio validation for folio providers; for +* other providers, NULL skips this validation. * @alloc_folio: Allocate an uninserted folio for a given index and NUMA policy. * Must return a pointer to a struct folio on success, or an * ERR_PTR() error code on failure. Must never return NULL.
@@ -77,6 +84,8 @@ struct guest_memfd_provider_operations {void (*release)(void *provider);void (*lock)(void *provider);void (*unlock)(void *provider); +int (*prepare_attributes)(struct inode *inode,void *provider,pgoff_t start, +size_t nr_pages,u64 memory_attrs,pgoff_t *err_index);struct folio *(*alloc_folio)(void *provider,pgoff_t index,struct mempolicy *mpol);void (*invalidate_folio)(void *provider,struct folio *folio);
virt/kvm/guest_memfd.c +28 -15
84 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v5 6/16 KVM: guest_memfd: Share memory through provider range lookup
In reply to Aneesh Kumar K.V (Arm)Use provider range lookup for both folios and PFNs when converting memory to shared. Skip holes without allocating pages and return lookup errors.
Invalidate mappings before conversion and publish shared attributes only after conversion succeeds.
--- virt/kvm/guest_memfd.c | 126 ++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 64 deletions(-)
virt/kvm/guest_memfd.c +62 -64
@@ -1110,87 +1110,79 @@ static int gmem_folio_prepare_attributes(struct inode *inode, void *provider,return 0 ; } -#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_CONVERT -static void kvm_gmem_make_shared (struct inode *inode,pgoff_t start,pgoff_t end) +static int gmem_provider_make_shared (struct inode *inode,pgoff_t start, +pgoff_t end) { +#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_CONVERT struct gmem_inode *gi =GMEM_I (inode); -struct folio_batch fbatch; -pgoff_t next = start; -int i; +void *entry; -if (kvm_gmem_is_pfn_mode (gi)) { -pgoff_t index; +MA_STATE (mas, &gi->attributes, start, start); -for (index = start; index < end; ) { -size_t step =1 ; +if (!kvm_arch_has_gmem_convert ()) +return 0 ; + +mas_for_each (&mas, entry, end -1 ) { +pgoff_t first =max (start, mas.index); +pgoff_t last =min (end, mas.last +1 ); + +if (!(kvm_gmem_get_attributes (inode, entry) &KVM_MEMORY_ATTRIBUTE_PRIVATE )) +continue ; + +while (first < last) {size_t nr_pages; -kvm_pfn_t pfn;u64 pfn_attrs; - -if (!gmem_provider_lookup_pfn_range (gi, index, &pfn, &nr_pages, - &pfn_attrs)) { - step =min_t (size_t, nr_pages, end - index); -kvm_arch_gmem_make_shared (pfn, step); +kvm_pfn_t pfn; +int ret; + + ret =gmem_provider_lookup_pfn_range (gi, first, &pfn, &nr_pages, + &pfn_attrs); +if (ret == -ENOENT ) { +/* +* Skip the reported hole, advancing at least one page +* even if nr_pages is zero, but stop at the end of this +* attribute range. +*/ + first +=min_t (size_t,max_t (size_t, nr_pages,1 ), + last - first); +cond_resched (); +continue ; } +if (ret) +return ret; - index += step; -cond_resched (); - } -return ; - } + nr_pages =min_t (size_t, nr_pages, last - first); +kvm_arch_gmem_make_shared (pfn, nr_pages); + ret =0 ; +if (ret) +return ret; -folio_batch_init (&fbatch); -while (filemap_get_folios (inode->i_mapping, &next, end -1 , &fbatch)) { -for (i =0 ; i <folio_batch_count (&fbatch); ++i) { -struct folio *folio = fbatch.folios[i]; -pgoff_t start_index, end_index; -kvm_pfn_t start_pfn; -kvm_pfn_t nr_pages; - - start_index =max (start, folio->index); - end_index =min (end,folio_next_index (folio)); -/* -* end_index is either in folio or points to -* the first page of the next folio. Hence, -* all pages in range [start_index, end_index) -* are contiguous. -*/ - start_pfn =folio_file_pfn (folio, start_index); - nr_pages = end_index - start_index; - -kvm_arch_gmem_make_shared (start_pfn, nr_pages); + first += nr_pages; +cond_resched (); } - -folio_batch_release (&fbatch); -cond_resched (); } -} -#else -static void kvm_gmem_make_shared (struct inode *inode,pgoff_t start,pgoff_t end) {}#endif +return 0 ; +}static int __kvm_gmem_set_attributes (struct inode *inode,pgoff_t start, -size_t nr_pages,uint64_t attrs, -pgoff_t *err_index) +size_t nr_pages,u64 attrs,pgoff_t *err_index) {bool to_private = attrs &KVM_MEMORY_ATTRIBUTE_PRIVATE ;struct address_space *mapping = inode->i_mapping;struct gmem_inode *gi =GMEM_I (inode);enum kvm_gfn_range_filter filter;pgoff_t end = start + nr_pages; -struct maple_tree *mt;struct ma_state mas;int r =0 ; - mt = &gi->attributes; - *err_index = start;filemap_invalidate_lock (mapping);if (kvm_gmem_range_has_attributes (inode, start, nr_pages, attrs)) goto out; -mas_init (&mas, mt, start); +mas_init (&mas, &gi->attributes, start); r =kvm_gmem_mas_preallocate (&mas, attrs, start, nr_pages);if (r) goto out;
@@ -1207,29 +1199,35 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start,if (gi->provider_ops->prepare_attributes) { r = gi->provider_ops->prepare_attributes (inode, gi->provider, start, nr_pages, attrs, err_index); -if (r) { -mas_destroy (&mas); - goto out; - } +if (r) + goto destroy; } -/* -* From this point on guest_memfd has performed necessary -* checks and can proceed to do guest-breaking changes. -*/ -gmem_provider_lock_updates (gi); filter = to_private ?KVM_FILTER_SHARED :KVM_FILTER_PRIVATE ;kvm_gmem_invalidate_start (inode, start, end, filter); -if (!to_private &&kvm_arch_has_gmem_convert ()) -kvm_gmem_make_shared (inode, start, end); +/* +* Make existing pages host-accessible before publishing shared attributes. +* Host faults map shared pages without performing architecture conversion. +* Private conversion revokes host mappings here, but can defer making +* pages private until a guest fault or prefault. +*/ +if (!to_private) + r =gmem_provider_make_shared (inode, start, end); -mas_store_prealloc (&mas,xa_mk_value (attrs)); +if (!r) +mas_store_prealloc (&mas,xa_mk_value (attrs)); +if (r) +mas_destroy (&mas);kvm_gmem_invalidate_end (inode, start, end);gmem_provider_unlock_updates (gi); + goto out; + +destroy: +mas_destroy (&mas); out:filemap_invalidate_unlock (mapping);return r;
Signature · 1 line
2.43.0
RFC PATCH v5 7/16 KVM: guest_memfd: Release and revoke memory through provider operations
In reply to Aneesh Kumar K.V (Arm)Add release_range() helpers for folios and PFNs. Keep guest invalidation in the core. Folio release truncates memory; ->free_folio() calls kvm_arch_gmem_reclaim() when the folio is removed from the page cache. PFN release removes host mappings and calls kvm_arch_gmem_reclaim() before put_pfn_range() releases the memory.
Use these operations for hole punching, provider revocation and final inode cleanup.
--- include/linux/guest_memfd.h | 13 +++- samples/kvm/mem_provider_sample.c | 3 +- virt/kvm/guest_memfd.c | 106 +++++++++++++++++++++--------- 3 files changed, 88 insertions(+), 34 deletions(-)
include/linux/guest_memfd.h +11 -2
@@ -49,6 +49,12 @@ struct guest_memfd_handle {* failure; it initially points to the range start. NULL * selects folio validation for folio providers; for * other providers, NULL skips this validation. +* @release_range: Remove host mappings and release memory in [start, end), +* after core guest invalidation, under the inode invalidate +* write lock and any provider lock. Also called at final +* inode cleanup, when no bindings remain. The optional pfn +* identifies memory revoked by the provider. release_backing +* is false for provider-initiated revocation, true otherwise. * @alloc_folio: Allocate an uninserted folio for a given index and NUMA policy. * Must return a pointer to a struct folio on success, or an * ERR_PTR() error code on failure. Must never return NULL.
@@ -86,6 +92,9 @@ struct guest_memfd_provider_operations {void (*unlock)(void *provider);int (*prepare_attributes)(struct inode *inode,void *provider,pgoff_t start,size_t nr_pages,u64 memory_attrs,pgoff_t *err_index); +int (*release_range)(struct inode *inode,void *provider, +pgoff_t start,pgoff_t end,const unsigned long *pfn, +u64 pfn_attrs,bool release_backing);struct folio *(*alloc_folio)(void *provider,pgoff_t index,struct mempolicy *mpol);void (*invalidate_folio)(void *provider,struct folio *folio);
@@ -99,10 +108,10 @@ struct guest_memfd_provider_operations {#ifdef CONFIG_KVM_GUEST_MEMFD int kvm_gmem_revoke (struct inode *inode,pgoff_t start,size_t nr_pages, -const unsigned long *pfn); +const unsigned long *pfn,u64 pfn_attrs);#else static inline int kvm_gmem_revoke (struct inode *inode,pgoff_t start,size_t nr_pages, -const unsigned long *pfn) +const unsigned long *pfn,u64 pfn_attrs) {return -EOPNOTSUPP ; }
samples/kvm/mem_provider_sample.c +2 -1
@@ -88,7 +88,8 @@ static void mps_set_present(struct mps_info *info, unsigned long start,if (info->gmem_inode) {unsigned long pfn = info->base_pfn + start; -kvm_gmem_revoke (info->gmem_inode, start, end - start, &pfn); +kvm_gmem_revoke (info->gmem_inode, start, end - start, + &pfn,0 );mps_zero_range (info, start, end - start); } }
virt/kvm/guest_memfd.c +75 -31
184 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v5 8/16 KVM: guest_memfd: Use provider operations for fallocate allocation
In reply to Aneesh Kumar K.V (Arm)Use allocate_range() for fallocate allocation so providers can supply allocation and release callback. When a provider supplies neither callback, guest_memfd handles allocation and release for it. Providers that supply only release_range() cannot use fallocate(), including hole punching.
--- include/linux/guest_memfd.h | 8 +++ virt/kvm/guest_memfd.c | 122 +++++++++++++++++++++++------------- 2 files changed, 87 insertions(+), 43 deletions(-)
include/linux/guest_memfd.h +8 -0
@@ -49,12 +49,18 @@ struct guest_memfd_handle {* failure; it initially points to the range start. NULL * selects folio validation for folio providers; for * other providers, NULL skips this validation. +* @allocate_range: Preallocate [start, end), without architecture private +* preparation. Called with the inode invalidate lock held +* for read. * @release_range: Remove host mappings and release memory in [start, end), * after core guest invalidation, under the inode invalidate * write lock and any provider lock. Also called at final * inode cleanup, when no bindings remain. The optional pfn * identifies memory revoked by the provider. release_backing * is false for provider-initiated revocation, true otherwise. +* Supplying this without allocate_range() +* supports a fixed memory range and disables fallocate(), +* including hole punching. * @alloc_folio: Allocate an uninserted folio for a given index and NUMA policy. * Must return a pointer to a struct folio on success, or an * ERR_PTR() error code on failure. Must never return NULL.
@@ -92,6 +98,8 @@ struct guest_memfd_provider_operations {void (*unlock)(void *provider);int (*prepare_attributes)(struct inode *inode,void *provider,pgoff_t start,size_t nr_pages,u64 memory_attrs,pgoff_t *err_index); +int (*allocate_range)(struct inode *inode,void *provider, +pgoff_t start,pgoff_t end);int (*release_range)(struct inode *inode,void *provider,pgoff_t start,pgoff_t end,const unsigned long *pfn,u64 pfn_attrs,bool release_backing);
virt/kvm/guest_memfd.c +79 -43
168 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v5 9/16 KVM: guest_memfd: Use provider operations for host mappings and faults
In reply to Aneesh Kumar K.V (Arm)Add mmap() and fault() callbacks so providers can set up host mappings and handle faults. Keep common permission checks, private/shared checks and inode locking in guest_memfd. Require providers to supply both callbacks together, or let guest_memfd handle both.
--- include/linux/guest_memfd.h | 14 ++++ virt/kvm/guest_memfd.c | 146 +++++++++++++++++++++++------------- 2 files changed, 106 insertions(+), 54 deletions(-)
include/linux/guest_memfd.h +14 -0
@@ -3,6 +3,7 @@#define _LINUX_GUEST_MEMFD_H#include <linux/bits.h> +#include <linux/mm_types.h> #include <linux/types.h> #define GUEST_MEMFD_PFN_MMIO BIT_ULL(0)
@@ -61,6 +62,14 @@ struct guest_memfd_handle {* Supplying this without allocate_range() * supports a fixed memory range and disables fallocate(), * including hole punching. +* @mmap: Configure provider-specific VMA flags and operations after common +* mapping permission checks. Supply with fault(), or omit both to +* select folio/PFN host-mapping defaults. +* @fault: Resolve a shared host fault under the inode invalidate read lock. +* Default folio helpers transfer a locked page reference to the MM +* and return VM_FAULT_LOCKED; default PFN helpers insert the PFN with +* its memory type and permissions, then release their temporary +* reference. * @alloc_folio: Allocate an uninserted folio for a given index and NUMA policy. * Must return a pointer to a struct folio on success, or an * ERR_PTR() error code on failure. Must never return NULL.
@@ -72,6 +81,9 @@ struct guest_memfd_handle {* guest_memfd_handle.folio for the core to unlock and release. * PFN providers leave folio NULL and keep their PFNs stable until * the caller releases the inode invalidate lock. +* Folio providers supplying get_pfn() must also supply mmap() and +* fault() when host mmap is enabled. The default fault expects a +* locked folio reference in guest_memfd_handle.folio. * @lookup_pfn_range: Look up the PFN for a given index. Returns 0 on success * with the physical frame number in @pfn and the number of * contiguous pages the mapping is valid for in @nr_pages.
@@ -103,6 +115,8 @@ struct guest_memfd_provider_operations {int (*release_range)(struct inode *inode,void *provider,pgoff_t start,pgoff_t end,const unsigned long *pfn,u64 pfn_attrs,bool release_backing); +int (*mmap)(struct inode *inode,void *provider,struct vm_area_struct *vma); +vm_fault_t (*fault)(struct inode *inode,void *provider,struct vm_fault *vmf);struct folio *(*alloc_folio)(void *provider,pgoff_t index,struct mempolicy *mpol);void (*invalidate_folio)(void *provider,struct folio *folio);
virt/kvm/guest_memfd.c +92 -54
209 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v5 10/16 KVM: guest_memfd: Let providers prepare memory regions
In reply to Aneesh Kumar K.V (Arm)Memory providers may need to check memslot flags and memory ranges before KVM installs a memslot. Add an optional prepare_memory_region() callback to the common provider operations
Pass the byte offset, size and requested dirty-logging and read-only flags, and return any error before publishing the slot. Providers do not need access to KVM's memslot structure or internal flags. Existing providers that do not implement the callback keep their current behavior.
--- include/linux/guest_memfd.h | 8 ++++++++ virt/kvm/guest_memfd.c | 14 ++++++++++++++ 2 files changed, 22 insertions(+)
include/linux/guest_memfd.h +8 -0
@@ -37,6 +37,12 @@ struct guest_memfd_handle {* @attach: Attach to a resource file, perform filesystem-specific validation, * and return an opaque provider context. * @release: Release provider context and unpin any resources. +* @prepare_memory_region: Optional check of a memory range and preparation +* for access. Called after common file checks and +* before publishing the memslot. Offset and size are +* in bytes. The flags contain only +* KVM_MEM_LOG_DIRTY_PAGES and KVM_MEM_READONLY bits. +* Return 0 on success or a negative error code. * @lock: Optional provider lock for mapping and attribute changes. Called * with the inode invalidate lock held for write, before guest * invalidation starts.
@@ -106,6 +112,8 @@ struct guest_memfd_handle {struct guest_memfd_provider_operations {void *(*attach)(struct inode *gmem_inode,struct file *resource_file);void (*release)(void *provider); +int (*prepare_memory_region)(void *provider,u64 offset,u64 size, +u32 flags);void (*lock)(void *provider);void (*unlock)(void *provider); int (*prepare_attributes)(struct inode *inode, void *provider, pgoff_t start,
virt/kvm/guest_memfd.c +14 -0
@@ -1671,9 +1671,11 @@ int kvm_gmem_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *slot unsigned int fd, uoff_t offset) {uoff_t size = slot->npages <<PAGE_SHIFT ; +struct gmem_inode *gi;struct gmem_file *f;struct inode *inode;struct file *file; +int ret;BUILD_BUG_ON (sizeof (gpa_t) !=sizeof (offset));
@@ -1694,10 +1696,22 @@ int kvm_gmem_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *slot goto err; inode =file_inode (file); + gi =GMEM_I (inode);if (!PAGE_ALIGNED (offset) || offset + size >i_size_read (inode)) goto err; +if (gi->provider_ops->prepare_memory_region) { +u32 flags = slot->flags & (KVM_MEM_LOG_DIRTY_PAGES |KVM_MEM_READONLY ); + + ret = gi->provider_ops->prepare_memory_region (gi->provider, offset, + size, flags); +if (ret) { +fput (file); +return ret; + } + } +/* * memslots of flag KVM_MEM_GUEST_MEMFD are immutable to change, so * kvm_gmem_bind() must occur on a new memslot. Because the memslot
Signature · 1 line
2.43.0
RFC PATCH v5 11/16 KVM: guest_memfd: Pass KVM explicitly to provider attach
In reply to Aneesh Kumar K.V (Arm)Pass the attaching VM to the resource-provider attach callbacks. The argument is valid during attachment and must not be retained. This avoids storing the VM in the device binding context introduced next.
--- include/linux/guest_memfd.h | 3 ++- mm/shmem.c | 2 +- samples/kvm/mem_provider_sample.c | 2 +- virt/kvm/guest_memfd.c | 9 +++++---- 4 files changed, 9 insertions(+), 7 deletions(-)
include/linux/guest_memfd.h +2 -1
@@ -12,6 +12,7 @@ struct file;struct folio ;struct inode ;struct mempolicy ; +struct kvm ;/** * struct guest_memfd_handle - PFN information and an optional folio reference
@@ -110,7 +111,7 @@ struct guest_memfd_handle {* Used by filesystems and device drivers that provide memory for guest_memfd. */ struct guest_memfd_provider_operations { -void *(*attach)(struct inode *gmem_inode,struct file *resource_file); +void *(*attach)(struct inode *gmem_inode,struct file *resource_file,struct kvm *kvm);void (*release)(void *provider);int (*prepare_memory_region)(void *provider,u64 offset,u64 size,u32 flags);
mm/shmem.c +1 -1
@@ -5462,7 +5462,7 @@ static const struct inode_operations shmem_special_inode_operations = {#ifdef CONFIG_KVM_GUEST_MEMFD static void *shmem_gmem_attach (struct inode *gmem_inode, -struct file *resource_file) +struct file *resource_file,struct kvm *kvm) {struct inode *inode =file_inode (resource_file);struct shmem_sb_info *sbinfo;
samples/kvm/mem_provider_sample.c +1 -1
@@ -152,7 +152,7 @@ static long mps_file_ioctl(struct file *file, unsigned int cmd, }static void *mps_gmem_attach (struct inode *gmem_inode, -struct file *resource_file) +struct file *resource_file,struct kvm *kvm) {struct mps_info *info = resource_file->private_data;
virt/kvm/guest_memfd.c +5 -4
@@ -86,7 +86,7 @@ static bool kvm_gmem_is_pfn_mode(struct gmem_inode *gi) }static void *gmem_native_attach (struct inode *gmem_inode, -struct file *resource_file) +struct file *resource_file,struct kvm *kvm) {return GMEM_I (gmem_inode); }
@@ -193,7 +193,8 @@ static void gmem_provider_init_ops(struct gmem_inode *gi,#define GMEM_RESOURCE_FD_NATIVE (-1) -static int kvm_gmem_attach_resource (struct inode *inode,int resource_fd) +static int kvm_gmem_attach_resource (struct kvm *kvm, +struct inode *inode,int resource_fd) {struct gmem_inode *gi =GMEM_I (inode);const struct guest_memfd_provider_operations *ops =NULL ;
@@ -212,7 +213,7 @@ static int kvm_gmem_attach_resource(struct inode *inode, int resource_fd) }if (gmem_provider_ops_valid (gi, ops)) - provider = ops->attach (inode, resource_file); + provider = ops->attach (inode, resource_file, kvm);if (resource_file)fput (resource_file);
@@ -1619,7 +1620,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags, int resour*/ smp_wmb (); - err =kvm_gmem_attach_resource (inode, resource_fd); + err =kvm_gmem_attach_resource (kvm, inode, resource_fd);if (err) {fput (file);put_unused_fd (fd);
Signature · 1 line
2.43.0
RFC PATCH v5 12/16 KVM: guest_memfd: Support devices through common provider operations
In reply to Aneesh Kumar K.V (Arm)Allow device MMIO to use the PFN provider interface on architectures that support protected device mappings. Pass device information during attachment. Invalidate both shared and private device mappings during revocation and file cleanup.
--- Documentation/virt/kvm/api.rst | 6 ++- include/linux/guest_memfd.h | 34 ++++++++++++++-- include/linux/kvm_host.h | 5 +++ mm/shmem.c | 3 +- samples/kvm/mem_provider_sample.c | 3 +- virt/kvm/guest_memfd.c | 68 +++++++++++++++++++++++++++---- 6 files changed, 104 insertions(+), 15 deletions(-)
Documentation/virt/kvm/api.rst +4 -2
@@ -6478,8 +6478,10 @@ that refers to it. guest_memfd files are roughly analogous to files created via memfd_create(), e.g. guest_memfd files live in RAM, have volatile storage, and are automatically released when the last reference is dropped. Unlike "regular" memfd_create() files, guest_memfd files are bound to their owning -virtual machine (see below), cannot be mapped, read, or written by userspace, -and cannot be resized (guest_memfd files do however support PUNCH_HOLE). +virtual machine (see below), cannot be read or written by userspace, and +cannot be resized. Userspace mappings depend on the creation flags and memory +resource. Ordinary guest_memfd files support PUNCH_HOLE; files using device +memory reject fallocate(). ::
include/linux/guest_memfd.h +31 -3
@@ -14,6 +14,26 @@ struct inode;struct mempolicy ;struct kvm ; +/* Binding data stays valid only while the provider is active. */ +struct guest_memfd_device_binding { +/* Architecture-specific mapping owner, e.g. an RMM vDEVICE handle. */ +unsigned long owner; +u64 vdev_id; +}; + +/** +* struct guest_memfd_device - Device memory context supplied during attach() +* @size: File size in bytes +* @flags: GUEST_MEMFD_FLAG_* creation flags +* @binding: Mapping owner and device ID set by attach(), with the underlying +* resources pinned until release() +*/ +struct guest_memfd_device { +u64 size; +u64 flags; +struct guest_memfd_device_binding binding; +}; +/** * struct guest_memfd_handle - PFN information and an optional folio reference * @pfn: Physical frame number
@@ -36,8 +56,14 @@ struct guest_memfd_handle {/** * struct guest_memfd_provider_operations - Operations for external memory providers * @attach: Attach to a resource file, perform filesystem-specific validation, -* and return an opaque provider context. -* @release: Release provider context and unpin any resources. +* and return an opaque provider context. Pin resources and callback +* code until release(). Device providers receive their core context +* in the device argument, which is NULL for other providers. +* The kvm argument identifies the VM and is valid during attach(); +* providers must not retain it. +* @release: Release provider context and unpin any resources. Must not access +* the VM here; its file reference may be gone. +* @device_memory: Memory is device MMIO rather than RAM. * @prepare_memory_region: Optional check of a memory range and preparation * for access. Called after common file checks and * before publishing the memslot. Offset and size are
@@ -111,7 +137,8 @@ struct guest_memfd_handle {* Used by filesystems and device drivers that provide memory for guest_memfd. */ struct guest_memfd_provider_operations { -void *(*attach)(struct inode *gmem_inode,struct file *resource_file,struct kvm *kvm); +void *(*attach)(struct inode *gmem_inode,struct file *resource_file, +struct kvm *kvm,struct guest_memfd_device *device);void (*release)(void *provider);int (*prepare_memory_region)(void *provider,u64 offset,u64 size,u32 flags);
@@ -135,6 +162,7 @@ struct guest_memfd_provider_operations { unsigned long *pfn, size_t *nr_pages, u64 *pfn_attrs);void (*put_pfn_range)(void *provider,pgoff_t index,size_t nr_pages); +bool device_memory; };#ifdef CONFIG_KVM_GUEST_MEMFD
include/linux/kvm_host.h +5 -0
@@ -60,6 +60,7 @@*/ #define KVM_MEMSLOT_INVALID (1UL << 16)#define KVM_MEMSLOT_GMEM_ONLY (1UL << 17) +#define KVM_MEMSLOT_GMEM_DEVICE (1UL << 18)/* * Bit 63 of the memslot generation number is an "update in-progress flag",
@@ -2613,6 +2614,10 @@ static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)#endif /* kvm_arch_has_private_mem */ #ifdef CONFIG_KVM_GUEST_MEMFD +struct guest_memfd_device_binding ; +bool kvm_arch_gmem_device_supported (struct kvm *kvm); +void kvm_arch_gmem_device_bind (struct kvm_memory_slot *slot, +const struct guest_memfd_device_binding *binding);bool kvm_gmem_is_private_gfn (struct kvm *kvm,gfn_t gfn);bool kvm_arch_supports_gmem_init_shared (struct kvm *kvm);
mm/shmem.c +2 -1
@@ -5462,7 +5462,8 @@ static const struct inode_operations shmem_special_inode_operations = {#ifdef CONFIG_KVM_GUEST_MEMFD static void *shmem_gmem_attach (struct inode *gmem_inode, -struct file *resource_file,struct kvm *kvm) +struct file *resource_file,struct kvm *kvm, +struct guest_memfd_device *device) {struct inode *inode =file_inode (resource_file);struct shmem_sb_info *sbinfo;
samples/kvm/mem_provider_sample.c +2 -1
@@ -152,7 +152,8 @@ static long mps_file_ioctl(struct file *file, unsigned int cmd, }static void *mps_gmem_attach (struct inode *gmem_inode, -struct file *resource_file,struct kvm *kvm) +struct file *resource_file,struct kvm *kvm, +struct guest_memfd_device *device) {struct mps_info *info = resource_file->private_data;
virt/kvm/guest_memfd.c +60 -8
149 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v5 13/16 KVM: guest_memfd: Notify providers after private attribute updates
In reply to Aneesh Kumar K.V (Arm)Publish whether committed private ranges remain through an optional update_private_ranges() provider operation. Scan the attribute tree after successful publication while holding the inode invalidate lock and any provider lock.
Keep the cached summary in the provider that uses it. Providers initialize their summary during attach() before exposing access checks.
--- include/linux/guest_memfd.h | 5 +++++ virt/kvm/guest_memfd.c | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-)
include/linux/guest_memfd.h +5 -0
@@ -76,6 +76,10 @@ struct guest_memfd_handle {* @unlock: Release the provider lock taken by lock(). Called after guest * invalidation ends and any successful attribute publication, with * the inode invalidate lock still held. +* @update_private_ranges: Optional notification of whether committed private +* ranges remain. Called after attribute publication, +* and with false after final file cleanup, under the +* inode invalidate lock and any provider lock. * @prepare_attributes: Optional memory validation before conversion, under * the inode invalidate lock for write, before taking * the provider lock or starting guest invalidation. Host mappings have
@@ -144,6 +148,7 @@ struct guest_memfd_provider_operations { u32 flags);void (*lock)(void *provider);void (*unlock)(void *provider); +void (*update_private_ranges)(void *provider,bool has_private_ranges);int (*prepare_attributes)(struct inode *inode,void *provider,pgoff_t start,size_t nr_pages,u64 memory_attrs,pgoff_t *err_index); int (*allocate_range)(struct inode *inode, void *provider,
virt/kvm/guest_memfd.c +24 -1
@@ -1317,6 +1317,27 @@ static int gmem_provider_make_shared(struct inode *inode, pgoff_t start,return 0 ; } +static void gmem_provider_update_attributes (struct inode *inode) +{ +struct gmem_inode *gi =GMEM_I (inode); +void *entry; +bool private = false; + +MA_STATE (mas, &gi->attributes,0 ,0 ); + +if (!gi->provider_ops->update_private_ranges) +return ; + +mas_for_each (&mas, entry,ULONG_MAX ) { +if (kvm_gmem_get_attributes (&gi->vfs_inode, entry) & +KVM_MEMORY_ATTRIBUTE_PRIVATE ) { + private = true; +break ; + } + } + gi->provider_ops->update_private_ranges (gi->provider, private); +} +static int __kvm_gmem_set_attributes (struct inode *inode,pgoff_t start,size_t nr_pages,u64 attrs,pgoff_t *err_index) {
@@ -1369,8 +1390,10 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start,if (!to_private) r =gmem_provider_make_shared (inode, start, end); -if (!r) +if (!r) {mas_store_prealloc (&mas,xa_mk_value (attrs)); +gmem_provider_update_attributes (inode); + }if (r)mas_destroy (&mas);
Signature · 1 line
2.43.0
RFC PATCH v5 14/16 KVM: guest_memfd: Enable shared host mappings for device providers
In reply to Aneesh Kumar K.V (Arm)Use the PFN provider mmap and fault handlers for shared device memory. Apply device VMA flags and noncached, decrypted protections, retaining the inode invalidate lock across PFN lookup and PTE insertion.
Private ranges remain inaccessible to host faults. Attribute conversion revokes host mappings before making memory private.
--- include/linux/guest_memfd.h | 4 +++- virt/kvm/guest_memfd.c | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-)
include/linux/guest_memfd.h +3 -1
@@ -63,7 +63,9 @@ struct guest_memfd_handle {* providers must not retain it. * @release: Release provider context and unpin any resources. Must not access * the VM here; its file reference may be gone. -* @device_memory: Memory is device MMIO rather than RAM. +* @device_memory: Memory is device MMIO rather than RAM. The core maps its +* userspace VMAs noncached and decrypted, with device flags. +* Shared userspace faults are resolved through @get_pfn * @prepare_memory_region: Optional check of a memory range and preparation * for access. Called after common file checks and * before publishing the memslot. Offset and size are
virt/kvm/guest_memfd.c +16 -2
@@ -1053,6 +1053,8 @@ static vm_fault_t gmem_pfn_fault(struct inode *inode, void *provider,if (r)return gmem_fault_error (r); +if (gmem_provider_is_device (gi)) + prot =pgprot_decrypted (pgprot_noncached (prot)); ret =vmf_insert_pfn_prot (vmf->vma, vmf->address, handle.pfn, prot);gmem_unlock_handle (&handle);gmem_put_handle (&handle);
@@ -1114,6 +1116,11 @@ static const struct vm_operations_struct kvm_gmem_vm_ops = {#endif }; +/* Device PFNs have fixed addresses and do not use NUMA allocation policy. */ +static const struct vm_operations_struct kvm_gmem_device_vm_ops = { + .fault = kvm_gmem_fault_user_mapping, +}; +static int gmem_folio_mmap (struct inode *inode,void *provider,struct vm_area_struct *vma) {
@@ -1124,8 +1131,15 @@ static int gmem_folio_mmap(struct inode *inode, void *provider,static int gmem_pfn_mmap (struct inode *inode,void *provider,struct vm_area_struct *vma) { -vm_flags_set (vma,VM_PFNMAP ); - vma->vm_ops = &kvm_gmem_vm_ops; +struct gmem_inode *gi =GMEM_I (inode); + +/* Keep device memory protections in the VMA for mprotect(). */ +if (gmem_provider_is_device (gi)) + vma->vm_page_prot =pgprot_decrypted (pgprot_noncached (vma->vm_page_prot)); + +vm_flags_set (vma,VM_IO |VM_PFNMAP |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops =gmem_provider_is_device (gi) ? &kvm_gmem_device_vm_ops : + &kvm_gmem_vm_ops;return 0 ; }
Signature · 1 line
2.43.0
RFC PATCH v5 15/16 KVM: guest_memfd: Notify providers during file release
In reply to Aneesh Kumar K.V (Arm)When releasing the last file associated with an inode, invalidate its mappings and notify the provider that no private ranges remain. Hold the provider lock across both operations so device unlock waits for cleanup. Skip notification if provider attachment failed.
--- virt/kvm/guest_memfd.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-)
virt/kvm/guest_memfd.c +21 -3
@@ -87,7 +87,6 @@ static bool gmem_provider_is_device(struct gmem_inode *gi)return gi->provider_ops && gi->provider_ops->device_memory; } -static bool kvm_gmem_is_pfn_mode (struct gmem_inode *gi) {return gi->flags &GUEST_MEMFD_FLAG_PFN_MODE ;
@@ -904,12 +903,28 @@ static long kvm_gmem_fallocate(struct file *file, int mode, loff_t offset,return ret; } +/* No file users remain, so private attributes cannot create new mappings. */ +static void gmem_provider_release_views (struct inode *inode) +{ +struct gmem_inode *gi =GMEM_I (inode); + +/* File release also runs after a failed provider attachment. */ +if (!gi->provider_ops) +return ; + +if (gi->provider_ops->update_private_ranges && +list_is_singular (&gi->gmem_file_list)) + gi->provider_ops->update_private_ranges (gi->provider, false); +} +static int kvm_gmem_release (struct inode *inode,struct file *file) {struct gmem_file *f = file->private_data; +struct gmem_inode *gi =GMEM_I (inode);struct kvm_memory_slot *slot;struct kvm *kvm = f->kvm;unsigned long index; +enum kvm_gfn_range_filter filter =kvm_gmem_get_all_gfns_filter (inode);/* * Prevent concurrent attempts to *unbind* a memslot. This is the last
@@ -922,6 +937,8 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)filemap_invalidate_lock (inode->i_mapping); +gmem_provider_lock_updates (gi); +/* * Note! synchronize_srcu() is _not_ needed after nullifying memslot * bindings as slot->gmem.file cannot be set back to a non-null value
@@ -975,9 +992,10 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* Zap all SPTEs pointed at by this file. Do not free the backing * memory, as its lifetime is associated with the inode, not the file. */ -__kvm_gmem_invalidate_start (f,0 ,-1ul , -kvm_gmem_get_all_gfns_filter (inode)); +__kvm_gmem_invalidate_start (f,0 ,-1ul , filter);__kvm_gmem_invalidate_end (f,0 ,-1ul ); +gmem_provider_release_views (inode); +gmem_provider_unlock_updates (gi);list_del (&f->entry);
Signature · 1 line
2.43.0
RFC PATCH v5 16/16 KVM: guest_memfd: Invalidate mappings before memslot unbind
In reply to Aneesh Kumar K.V (Arm)Use the generic invalidate_start/end helpers to remove shared and private device mappings before removing the memslot's guest_memfd binding.
Hold gmem_provider_lock_updates() across invalidation and binding removal to prevent device unlock from racing with teardown.
Keep the file attributes and has_private_ranges unchanged. Deleting a memslot removes mappings but does not share the memory range. Device unlock remains blocked until userspace shares the private ranges or closes the last guest_memfd reference.
--- virt/kvm/guest_memfd.c | 8 ++++++++ 1 file changed, 8 insertions(+)
virt/kvm/guest_memfd.c +8 -0
@@ -1877,6 +1877,8 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot)unsigned long start = slot->gmem.pgoff;unsigned long end = start + slot->npages;struct gmem_file *f; +struct gmem_inode *gi; +enum kvm_gfn_range_filter filter;/* * Nothing to do if the underlying file was _already_ closed, as
@@ -1896,8 +1898,13 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot)* drops slots_lock, so there's no need to verify the file is live. */ f = file->private_data; + gi =GMEM_I (file_inode (file)); + filter =kvm_gmem_get_all_gfns_filter (file_inode (file));filemap_invalidate_lock (file->f_mapping); +gmem_provider_lock_updates (gi); +__kvm_gmem_invalidate_start (f, start, end, filter); +__kvm_gmem_invalidate_end (f, start, end);xa_store_range (&f->bindings, start, end -1 ,NULL ,GFP_KERNEL );/*
@@ -1906,6 +1913,7 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot)* to guarantee kvm_gmem_get_pfn() can't see the slot+file. */ WRITE_ONCE (slot->gmem.file,NULL ); +gmem_provider_unlock_updates (gi);filemap_invalidate_unlock (file->f_mapping); }
Signature · 1 line
2.43.0
Quoted 9 lines · Aneesh Kumar K.V (Arm)
Confidential device assignment needs
guest_memfdto manage device MMIO as well as RAM. Device resources such as PCI BARs supply PFNs directly, and their protected mappings need architecture-specific map and unmap operations.This series adds the common
guest_memfdsupport needed by device memory providers. It builds on the resource-provider API [1] and the existing support for folios and RAM PFNs.
Quoted 8 lines
The VFIO provider, IOMMUFD integration and Arm CCA implementation are available together at:
https://git.gitlab.arm.com/linux-arm/linux-cca.git cca/topics/cca-da-gmem-backend-v5
This series depends on the resource-provider groundwork from:
[1] https://lore.kernel.org/all/[email protected]
Just a quick update. I'm still interested, just tied up with internal work. I'll get to this as soon as I can.
Quoted 3 lines
[...snip...]