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. 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 | 8 ++++++++ virt/kvm/guest_memfd.c | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/linux/guest_memfd.h b/include/linux/guest_memfd.h index 777507191603..79e2983c3b26 100644 --- a/include/linux/guest_memfd.h +++ b/include/linux/guest_memfd.h @@ -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, diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 3a92f2453e8b..ffecc4b19a17 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -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 -- 2.43.0