BACKPORT: mm: call wp_page_copy() under the VMA lock
It is usually safe to call wp_page_copy() under the VMA lock. The only unsafe situation is when no anon_vma has been allocated for this VMA, and we have to look at adjacent VMAs to determine if their anon_vma can be shared. Since this happens only for the first COW of a page in this VMA, the majority of calls to wp_page_copy() do not need to fall back to the mmap_sem. Add vmf_anon_prepare() as an alternative to anon_vma_prepare() which will return RETRY if we currently hold the VMA lock and need to allocate an anon_vma. This lets us drop the check in do_wp_page(). Link: https://lkml.kernel.org/r/20231006195318.4087158-3-willy@infradead.org Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Reviewed-by: Suren Baghdasaryan <surenb@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> (cherry picked from commit 164b06f238b986317131e6b61b2f22aabcbc2cc0) [surenb: resolved merge conflicts due to folio/page differences] Bug: 293665307 Change-Id: I39bdc247b375bd3dae8078b52c60fd4ce12e1850 Signed-off-by: Suren Baghdasaryan <surenb@google.com>
This commit is contained in:
parent
b43b26b4cd
commit
95af8a80bb
1 changed files with 26 additions and 13 deletions
39
mm/memory.c
39
mm/memory.c
|
|
@ -3099,6 +3099,21 @@ static inline void wp_page_reuse(struct vm_fault *vmf)
|
|||
count_vm_event(PGREUSE);
|
||||
}
|
||||
|
||||
static vm_fault_t vmf_anon_prepare(struct vm_fault *vmf)
|
||||
{
|
||||
struct vm_area_struct *vma = vmf->vma;
|
||||
|
||||
if (likely(vma->anon_vma))
|
||||
return 0;
|
||||
if (vmf->flags & FAULT_FLAG_VMA_LOCK) {
|
||||
vma_end_read(vma);
|
||||
return VM_FAULT_RETRY;
|
||||
}
|
||||
if (__anon_vma_prepare(vma))
|
||||
return VM_FAULT_OOM;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle the case of a page which we actually need to copy to a new page,
|
||||
* either due to COW or unsharing.
|
||||
|
|
@ -3126,12 +3141,13 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
|
|||
pte_t entry;
|
||||
int page_copied = 0;
|
||||
struct mmu_notifier_range range;
|
||||
int ret;
|
||||
vm_fault_t ret;
|
||||
|
||||
delayacct_wpcopy_start();
|
||||
|
||||
if (unlikely(anon_vma_prepare(vma)))
|
||||
goto oom;
|
||||
ret = vmf_anon_prepare(vmf);
|
||||
if (unlikely(ret))
|
||||
goto out;
|
||||
|
||||
if (is_zero_pfn(pte_pfn(vmf->orig_pte))) {
|
||||
new_page = alloc_zeroed_user_highpage_movable(vma,
|
||||
|
|
@ -3139,13 +3155,14 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
|
|||
if (!new_page)
|
||||
goto oom;
|
||||
} else {
|
||||
int err;
|
||||
new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
|
||||
vmf->address);
|
||||
if (!new_page)
|
||||
goto oom;
|
||||
|
||||
ret = __wp_page_copy_user(new_page, old_page, vmf);
|
||||
if (ret) {
|
||||
err = __wp_page_copy_user(new_page, old_page, vmf);
|
||||
if (err) {
|
||||
/*
|
||||
* COW failed, if the fault was solved by other,
|
||||
* it's fine. If not, userspace would re-fault on
|
||||
|
|
@ -3158,7 +3175,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
|
|||
put_page(old_page);
|
||||
|
||||
delayacct_wpcopy_end();
|
||||
return ret == -EHWPOISON ? VM_FAULT_HWPOISON : 0;
|
||||
return err == -EHWPOISON ? VM_FAULT_HWPOISON : 0;
|
||||
}
|
||||
kmsan_copy_page_meta(new_page, old_page);
|
||||
}
|
||||
|
|
@ -3271,11 +3288,13 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
|
|||
oom_free_new:
|
||||
put_page(new_page);
|
||||
oom:
|
||||
ret = VM_FAULT_OOM;
|
||||
out:
|
||||
if (old_page)
|
||||
put_page(old_page);
|
||||
|
||||
delayacct_wpcopy_end();
|
||||
return VM_FAULT_OOM;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -3510,12 +3529,6 @@ reuse:
|
|||
return wp_page_shared(vmf);
|
||||
}
|
||||
copy:
|
||||
if ((vmf->flags & FAULT_FLAG_VMA_LOCK) && !vma->anon_vma) {
|
||||
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
||||
vma_end_read(vmf->vma);
|
||||
return VM_FAULT_RETRY;
|
||||
}
|
||||
|
||||
/*
|
||||
* Ok, we need to copy. Oh, well..
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue