On Thu, 7 May 2026 at 21:22, Ackerley Tng via B4 Relay <[email protected]> wrote: > > From: Ackerley Tng <[email protected]> > > Extract a helper out of kvm_gmem_range_is_private() that checks that a > range has given attributes. > > Optimize setting memory attributes by returning early if all pages in the > requested range already has the requested attributes. > > Signed-off-by: Ackerley Tng <[email protected]>
Reviewed-by: Fuad Tabba <[email protected]> Cheers, /fuad > --- > virt/kvm/guest_memfd.c | 33 +++++++++++++++++++++++---------- > 1 file changed, 23 insertions(+), 10 deletions(-) > > diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c > index baf4b88dead1f..034b72b4947fb 100644 > --- a/virt/kvm/guest_memfd.c > +++ b/virt/kvm/guest_memfd.c > @@ -86,6 +86,23 @@ 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 maple_tree *mt, > + pgoff_t index, size_t nr_pages, > + u64 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 (xa_to_value(entry) != attributes) > + return false; > + } > + > + return true; > +} > + > static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot > *slot, > pgoff_t index, struct folio *folio) > { > @@ -649,12 +666,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(mt, start, nr_pages, attrs)) > + goto out; > + > mas_init(&mas, mt, start); > r = kvm_gmem_mas_preallocate(&mas, attrs, start, nr_pages); > if (r) { > @@ -1140,20 +1160,13 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_gmem_get_pfn); > static bool kvm_gmem_range_is_private(struct gmem_inode *gi, pgoff_t index, > size_t nr_pages, struct kvm *kvm, gfn_t > gfn) > { > - pgoff_t end = index + nr_pages - 1; > - void *entry; > - > if (vm_memory_attributes) > return kvm_range_has_vm_memory_attributes(kvm, gfn, gfn + > nr_pages, > > KVM_MEMORY_ATTRIBUTE_PRIVATE, > > KVM_MEMORY_ATTRIBUTE_PRIVATE); > > - mt_for_each(&gi->attributes, entry, index, end) { > - if (xa_to_value(entry) != KVM_MEMORY_ATTRIBUTE_PRIVATE) > - return false; > - } > - > - return true; > + return kvm_gmem_range_has_attributes(&gi->attributes, index, nr_pages, > + KVM_MEMORY_ATTRIBUTE_PRIVATE); > } > > static long __kvm_gmem_populate(struct kvm *kvm, struct kvm_memory_slot > *slot, > > -- > 2.54.0.563.g4f69b47b94-goog > >
