Resource providers may need to serialize host access with guest invalidation, architecture conversion and publication of new attributes. Add optional paired lock() and unlock() callbacks without exposing KVM memslots or architecture requests through the provider interface. Reject providers that supply only one callback. Acquire provider exclusion after the inode invalidate lock and before guest invalidation starts. Release it after invalidation ends and any successful attribute publication, including when conversion fails. These hooks only provide exclusion and cannot reject cleanup. Subsequent revocation, memslot unbind and final file cleanup use the same pair without changing attributes. Existing providers omit both callbacks and retain their current behavior. Cc: Paolo Bonzini Cc: Sean Christopherson Cc: David Hildenbrand Cc: kvm@vger.kernel.org Cc: patches@lists.linux.dev Signed-off-by: Aneesh Kumar K.V (Arm) --- include/linux/guest_memfd.h | 11 +++++++++++ virt/kvm/guest_memfd.c | 22 ++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/include/linux/guest_memfd.h b/include/linux/guest_memfd.h index 17407504307b..9fc04f1188c1 100644 --- a/include/linux/guest_memfd.h +++ b/include/linux/guest_memfd.h @@ -20,6 +20,15 @@ struct mempolicy; * 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 exclusion for mapping and attribute changes. Called + * with the inode invalidate lock held for write, before guest + * invalidation starts. May sleep and must return with exclusion held. + * Supply both lock and unlock, or neither. + * @unlock: Release exclusion acquired by lock(). Called after guest + * invalidation ends and any successful attribute publication, with + * the inode invalidate lock still held. The pair also covers range + * revocation, memslot unbind and final file cleanup; those operations + * do not change attributes. * @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. @@ -52,6 +61,8 @@ struct guest_memfd_provider_operations { void (*release)(void *provider); int (*prepare_memory_region)(void *provider, u64 offset, u64 size, u32 flags); + 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); diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index bd8324b4baba..d1060b4c785f 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -107,6 +107,9 @@ static bool gmem_provider_ops_valid(struct gmem_inode *gi, if (!ops || !ops->attach) return false; + if (!!ops->lock != !!ops->unlock) + return false; + if (kvm_gmem_is_pfn_mode(gi)) return ops->get_pfn && ops->lookup_pfn_range; @@ -208,6 +211,18 @@ static void gmem_provider_put_pfn_range(struct gmem_inode *gi, pgoff_t index, gi->provider_ops->put_pfn_range(gi->provider, index, nr_pages); } +static void gmem_provider_lock(struct gmem_inode *gi) +{ + if (gi->provider_ops && gi->provider_ops->lock) + gi->provider_ops->lock(gi->provider); +} + +static void gmem_provider_unlock(struct gmem_inode *gi) +{ + if (gi->provider_ops && gi->provider_ops->unlock) + gi->provider_ops->unlock(gi->provider); +} + static void gmem_provider_release(struct gmem_inode *gi) { if (gi->provider_ops && gi->provider_ops->release) @@ -1092,8 +1107,7 @@ static int kvm_gmem_convert_attributes(struct inode *inode, pgoff_t start, } 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; @@ -1119,6 +1133,9 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start, if (r) goto destroy; + /* Provider exclusion must cover invalidation as well as conversion. */ + gmem_provider_lock(gi); + filter = to_private ? KVM_FILTER_SHARED : KVM_FILTER_PRIVATE; kvm_gmem_invalidate_start(inode, start, end, filter); @@ -1129,6 +1146,7 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start, if (r) mas_destroy(&mas); kvm_gmem_invalidate_end(inode, start, end); + gmem_provider_unlock(gi); goto out; destroy: -- 2.43.0