2024-04-26 14:45:51

by Matthew Wilcox

[permalink] [raw]
Subject: [PATCH 4/4] mm: Optimise vmf_anon_prepare() for VMAs without an anon_vma

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) <[email protected]>
Reviewed-by: Jann Horn <[email protected]>
Reviewed-by: Suren Baghdasaryan <[email protected]>
---
mm/memory.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 7dc112d3a7e4..b5453b86ec4b 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3232,16 +3232,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;
}

/*
--
2.43.0



2024-04-26 17:37:59

by David Hildenbrand

[permalink] [raw]
Subject: Re: [PATCH 4/4] mm: Optimise vmf_anon_prepare() for VMAs without an anon_vma

On 26.04.24 16:45, Matthew Wilcox (Oracle) wrote:
> 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) <[email protected]>
> Reviewed-by: Jann Horn <[email protected]>
> Reviewed-by: Suren Baghdasaryan <[email protected]>
> ---
> mm/memory.c | 13 +++++++++----
> 1 file changed, 9 insertions(+), 4 deletions(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index 7dc112d3a7e4..b5453b86ec4b 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -3232,16 +3232,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;
> }
>
> /*

Fancy

Reviewed-by: David Hildenbrand <[email protected]>

--
Cheers,

David / dhildenb