Give device backing a revoke operation that removes host and both shared and private guest translations under provider exclusion, without reclaiming MMIO PFNs or changing attributes. Keep fallocate and hole punching unsupported for device memory. Range revocation does not exclude new faults after returning. Providers must preserve the underlying resource and call without provider locks held, since inode exclusion precedes provider exclusion. Private attributes continue to describe ownership until explicitly changed or the file closes. Signed-off-by: Aneesh Kumar K.V (Arm) --- virt/kvm/guest_memfd.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 1322576350b8..cfb7f34c0180 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -559,6 +559,18 @@ static int kvm_gmem_ram_pfn_revoke(struct inode *inode, pgoff_t start, pgoff_t e return 0; } +static int kvm_gmem_device_revoke(struct inode *inode, pgoff_t start, pgoff_t end, + const unsigned long *pfn, bool put_pfn_range) +{ + /* + * Common invalidation handles shared and private guest mappings. Only + * host userspace mappings need to be unmapped here; MMIO has no RAM + * pages to reclaim or provider allocations to release. + */ + unmap_mapping_pages(inode->i_mapping, start, end - start, true); + return 0; +} + static int __kvm_gmem_revoke(struct inode *inode, pgoff_t start, size_t nr_pages, const unsigned long *pfn, bool put_pfn_range) @@ -622,7 +634,11 @@ static long kvm_gmem_punch_hole(struct inode *inode, loff_t offset, loff_t len) * * Must be called before accessing memory contents. Keeping the mapping from * @start to @pfn constant is the responsibility of the caller. - * .put_pfn_range() will not be called. + * .put_pfn_range() will not be called. For device memory, this revokes host + * and both shared and private guest mappings under provider exclusion, without + * changing attributes or releasing BAR PFNs. Call without provider locks held: + * inode exclusion precedes provider exclusion. This does not exclude new faults + * after returning; the provider must preserve the backing resource. * * Return: 0 on success, or a negative error code on failure. */ @@ -1185,6 +1201,7 @@ static const struct kvm_gmem_backing_ops kvm_gmem_ram_pfn_backing_ops = { }; static const struct kvm_gmem_backing_ops kvm_gmem_device_backing_ops = { + .revoke = kvm_gmem_device_revoke, .prepare_attributes = kvm_gmem_pfn_prepare_attributes, .make_private = kvm_gmem_device_make_private, .make_shared = kvm_gmem_noop_conversion, -- 2.43.0