Trylock the mmap_lock in vmf_anon_prepare()
Subsystems (2)
RFC 1/1 Trylock the mmap_lock in vmf_anon_prepare()
It seems a shame to fallback if the mmap_lock is readily available. It doesn't blow up immediately in my testing ... thoughts?
From 70833af858ce41a80b5f0669648ee9f59b149a03 Mon Sep 17 00:00:00 2001
From: "Matthew Wilcox (Oracle)" <[email protected]>
Date: Mon, 15 Apr 2024 11:13:41 -0400
Subject: [PATCH] mm: Optimise vmf_anon_prepare() for VMAs without an anon_vma
If the mmap_lock can be taken for read, there's no reason not to take it so that we can call __anon_vma_prepare().
--- mm/memory.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
mm/memory.c +6 -2
@@ -3219,11 +3219,15 @@ vm_fault_t vmf_anon_prepare(struct vm_fault *vmf)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 ; +if (vmf->flags &FAULT_FLAG_VMA_LOCK ) +mmap_read_unlock (vma->vm_mm);return 0 ; }
Signature · 1 line
2.43.0
Suren Baghdasaryan
reviewerQuoted 3 lines · Matthew Wilcox
It seems a shame to fallback if the
mmap_lockis readily available. It doesn't blow up immediately in my testing ... thoughts?
I can't think of anything that would break...
Quoted 21 lines
From
70833af858ce41a80b5f0669648ee9f59b149a03Mon Sep 17 00:00:00 2001
From: "Matthew Wilcox (Oracle)" <[email protected]>
Date: Mon, 15 Apr 2024 11:13:41 -0400
Subject: [PATCH] mm: Optimisevmf_anon_prepare()for VMAs without ananon_vmaIf the
mmap_lockcan be taken for read, there's no reason not to take it so that we can call__anon_vma_prepare().Signed-off-by Matthew Wilcox (Oracle) <[email protected]>--- mm/memory.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)mm/memory.c +0
-0@@ -3219,11 +3219,15 @@ vm_fault_t vmf_anon_prepare(struct vm_fault *vmf)if (likely (vma->anon_vma))return 0 ;
mm/memory.c vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) · line 3228
Quoted 10 lines
mm/memory.c +4
-2if (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 ;
You should drop mmap_lock when returning VM_FAULT_OOM as well.
Quoted 9 lines
mm/memory.c +2
-1+if (vmf->flags &FAULT_FLAG_VMA_LOCK ) +mmap_read_unlock (vma->vm_mm);return 0 ; } --2.43.0
Quoted 11 lines · Suren Baghdasaryan
mm/memory.c vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) · line 3228
Quoted 10 lines
if (vmf->flags & FAULT_FLAG_VMA_LOCK) {mm/memory.c +4
-2-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 ;
Quoted 6 lines
You should drop
mmap_lockwhen returningVM_FAULT_OOMas well.Quoted 4 lines
mm/memory.c +2
-0+if (vmf->flags &FAULT_FLAG_VMA_LOCK ) +mmap_read_unlock (vma->vm_mm);return 0 ; }
Thanks. Fixed and pushed togit://git.infradead.org/users/willy/pagecache.git vma-lock
+++ b/mm/memory.c
@@ -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;
}
/*
Quoted 15 lines · Matthew Wilcox
Quoted 13 lines · Suren Baghdasaryan
mm/memory.c vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) · line 3228
Quoted 10 lines
if (vmf->flags & FAULT_FLAG_VMA_LOCK) {mm/memory.c +4
-2-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 ;You should drop
mmap_lockwhen returningVM_FAULT_OOMas well.
Quoted 7 lines
Quoted 4 lines
mm/memory.c vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) · line 3232
Quoted 4 lines
mm/memory.c +2
-0+if (vmf->flags &FAULT_FLAG_VMA_LOCK ) +mmap_read_unlock (vma->vm_mm);return 0 ; }Thanks. Fixed and pushed to
git://git.infradead.org/users/willy/pagecache.gitvma-lock
That looks correct now.
Quoted 29 lines
+++
b/mm/memory.cmm/memory.c +9
-4@@ -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; }/*
Quoted 2 lines · Matthew Wilcox
It seems a shame to fallback if the
mmap_lockis readily available. It doesn't blow up immediately in my testing ... thoughts?
Seems fine to me, since nothing below the trylocked mmap_lock does vma_start_*().
Quoted 9 lines
Quoted 1 line
From
70833af858ce41a80b5f0669648ee9f59b149a03Mon Sep 17 00:00:00 2001From: "Matthew Wilcox (Oracle)" <[email protected]>
Date: Mon, 15 Apr 2024 11:13:41 -0400
Subject: [PATCH] mm: Optimisevmf_anon_prepare()for VMAs without ananon_vmaIf the
mmap_lockcan be taken for read, there's no reason not to take it so that we can call__anon_vma_prepare().Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Quoted 28 lines
--- mm/memory.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)mm/memory.c +6
-3@@ -3219,11 +3219,15 @@ vm_fault_t vmf_anon_prepare(struct vm_fault *vmf)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 ; +if (vmf->flags &FAULT_FLAG_VMA_LOCK ) +mmap_read_unlock (vma->vm_mm);return 0 ; } --2.43.0