]> www.infradead.org Git - users/willy/pagecache.git/commitdiff
mm: Optimise vmf_anon_prepare() for VMAs without an anon_vma vma-lock
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Mon, 15 Apr 2024 15:13:41 +0000 (11:13 -0400)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Mon, 15 Apr 2024 18:15:52 +0000 (14:15 -0400)
If the mmap_lock can be taken for read, we can call __anon_vma_prepare()
while holding it, saving ourselves a trip back through the fault handler.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Jann Horn <jannh@google.com>
mm/memory.c

index 64d2986dd239278b107adfffeb75f5171b86412d..5624b881b662d0a3d33469e60c00a93d932bf417 100644 (file)
@@ -3224,16 +3224,21 @@ static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf)
 vm_fault_t vmf_anon_prepare(struct vm_fault *vmf)
 {
        struct vm_area_struct *vma = vmf->vma;
+       vm_fault_t ret = 0;
 
        if (likely(vma->anon_vma))
                return 0;
        if (vmf->flags & FAULT_FLAG_VMA_LOCK) {
-               vma_end_read(vma);
-               return VM_FAULT_RETRY;
+               if (!mmap_read_trylock(vma->vm_mm)) {
+                       vma_end_read(vma);
+                       return VM_FAULT_RETRY;
+               }
        }
        if (__anon_vma_prepare(vma))
-               return VM_FAULT_OOM;
-       return 0;
+               ret = VM_FAULT_OOM;
+       if (vmf->flags & FAULT_FLAG_VMA_LOCK)
+               mmap_read_unlock(vma->vm_mm);
+       return ret;
 }
 
 /*