summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAckerley Tng <ackerleytng@google.com>2026-09-10 16:55:43 -0700
committerSean Christopherson <seanjc@google.com>2026-09-22 07:28:30 -0700
commit856852ef2108fd4e70f29d450487fe15324c647c (patch)
tree11f3e5586b41558ad4b76a33b2d4c44f51530c92
parent6fa1dab756fd49d4b3162a68775de3aeec78646e (diff)
KVM: guest_memfd: Return early if range already has requested attributes
Provide a function to check that a range has given attributes. Optimize setting memory attributes by returning early if all pages in the requested range already have the requested attributes. Signed-off-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Shivank Garg <shivankg@amd.com> Reviewed-by: Fuad Tabba <tabba@google.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-17-dd6fbf94f4e1@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
-rw-r--r--virt/kvm/guest_memfd.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 5b6b58ea2acb..8eedff42a96f 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -99,6 +99,24 @@ static bool kvm_gmem_is_shared_mem(struct inode *inode, pgoff_t index)
return !kvm_gmem_is_private_mem(inode, index);
}
+static bool kvm_gmem_range_has_attributes(struct inode *inode,
+ pgoff_t index, size_t nr_pages,
+ u64 attributes)
+{
+ struct maple_tree *mt = &GMEM_I(inode)->attributes;
+ pgoff_t end = index + nr_pages - 1;
+ void *entry;
+
+ lockdep_assert(mt_lock_is_held(mt));
+
+ mt_for_each(mt, entry, index, end) {
+ if (kvm_gmem_get_attributes(inode, entry) != attributes)
+ return false;
+ }
+
+ return true;
+}
+
/*
* Returns a locked folio on success. The caller is responsible for
* setting the up-to-date flag before the memory is mapped into the guest.
@@ -675,12 +693,15 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start,
pgoff_t end = start + nr_pages;
struct maple_tree *mt;
struct ma_state mas;
- int r;
+ int r = 0;
mt = &gi->attributes;
filemap_invalidate_lock(mapping);
+ if (kvm_gmem_range_has_attributes(inode, start, nr_pages, attrs))
+ goto out;
+
mas_init(&mas, mt, start);
r = kvm_gmem_mas_preallocate(&mas, attrs, start, nr_pages);
if (r) {