]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm: introduce lock_vma_under_rcu to be used from arch-specific code
authorSuren Baghdasaryan <surenb@google.com>
Thu, 12 May 2022 22:03:53 +0000 (15:03 -0700)
committerSuren Baghdasaryan <surenb@google.com>
Wed, 23 Nov 2022 02:09:46 +0000 (02:09 +0000)
Introduce lock_vma_under_rcu function to lookup and lock a VMA during
page fault handling. When VMA is not found, can't be locked or changes
after being locked, the function returns NULL. The lookup is performed
under RCU protection to prevent the found VMA from being destroyed before
the VMA lock is acquired. VMA lock statistics are updated according to
the results.
For now only anonymous VMAs can be searched this way. In other cases the
function returns NULL.

Signed-off-by: Suren Baghdasaryan <surenb@google.com>
include/linux/mm.h
mm/memory.c

index 3851f786c95aff13094cda771acf99b25d47f53a..d2c1e03c8088ad1e6d6ce19e449feb7628a875fc 100644 (file)
@@ -684,6 +684,9 @@ static inline void vma_assert_no_reader(struct vm_area_struct *vma)
                      vma);
 }
 
+struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,
+                                         unsigned long address);
+
 #else /* CONFIG_PER_VMA_LOCK */
 
 static inline void vma_init_lock(struct vm_area_struct *vma) {}
index a5a33d88f3c0379d6c9728ac04ef7abfa7a845b9..6640b8cba622075395871b0a70a076a2c30326fd 100644 (file)
@@ -5241,6 +5241,50 @@ vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
 }
 EXPORT_SYMBOL_GPL(handle_mm_fault);
 
+#ifdef CONFIG_PER_VMA_LOCK
+/*
+ * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be
+ * stable and not isolated. If the VMA is not found or is being modified the
+ * function returns NULL.
+ */
+struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,
+                                         unsigned long address)
+{
+       MA_STATE(mas, &mm->mm_mt, address, address);
+       struct vm_area_struct *vma, *validate;
+
+       rcu_read_lock();
+       vma = mas_walk(&mas);
+retry:
+       if (!vma)
+               goto inval;
+
+       if (!vma_is_anonymous(vma))
+               goto inval;
+
+       if (!vma_read_trylock(vma)) {
+               count_vm_vma_lock_event(VMA_LOCK_ABORT);
+               goto inval;
+       }
+
+       /* Check if the VMA got isolated after we found it */
+       validate = mas_walk(&mas);
+       if (validate != vma) {
+               vma_read_unlock(vma);
+               count_vm_vma_lock_event(VMA_LOCK_MISS);
+               /* The area was replaced with another one. */
+               vma = validate;
+               goto retry;
+       }
+
+       rcu_read_unlock();
+       return vma;
+inval:
+       rcu_read_unlock();
+       return NULL;
+}
+#endif /* CONFIG_PER_VMA_LOCK */
+
 #ifndef __PAGETABLE_P4D_FOLDED
 /*
  * Allocate p4d page table.