mm: separate the fallback to mmap_lock from VM_FAULT_RETRY
Subsystems (14)
Suren Baghdasaryan
reviewercover mm: separate the fallback to mmap_lock from VM_FAULT_RETRY
VM_FAULT_RETRY is returned from the page fault path for two different reasons. Usually it means that the fault handler had to drop its lock, typically to wait for something, and the fault has to be handled again. With per-VMA locks it is also returned when the fault can not be handled under the per-VMA lock at all and has to be handled under mmap_lock. The architecture fault handlers can not tell these two cases apart, so every VM_FAULT_RETRY from a fault handled under the per-VMA lock results in a retry under mmap_lock.
This series separates the two cases:
Patch 1 introduces should_retry_mm_fault() and uses it in the per-VMA lock path of the architecture fault handlers, in fault_signal_pending() and in mm_account_fault() instead of testing VM_FAULT_RETRY directly.
Patch 2 introduces VM_FAULT_RETRY_LOCK_MM and returns it from the places that fall back from the per-VMA lock to mmap_lock: vmf_can_call_fault(), __vmf_anon_prepare() and the device private paths in do_swap_page() and do_huge_pmd_device_private(). VM_FAULT_RETRY keeps being returned in all the other cases.
should_retry_mm_fault() accepts both codes and the architecture fault handlers keep retrying under mmap_lock in both cases, so no functional change is intended. The series only makes the reason for the retry visible to the callers of handle_mm_fault().
Only the checks in the per-VMA lock path are converted. The checks done after handling a fault under mmap_lock still test VM_FAULT_RETRY, since VM_FAULT_RETRY_LOCK_MM is never returned there.
Drivers and filesystems that return VM_FAULT_RETRY are left unchanged. panthor_gem_any_fault() might return it after dropping the per-VMA lock and waiting, which is treated as a generic retry.
The series is based on mm-unstable.
Suren Baghdasaryan (2):
mm: introduce should_retry_mm_fault() helper
mm: split VM_FAULT_RETRY_LOCK_MM out of VM_FAULT_RETRY
Documentation/filesystems/porting.rst | 4 ++-- arch/arm/mm/fault.c | 4 ++-- arch/arm64/mm/fault.c | 4 ++-- arch/loongarch/mm/fault.c | 4 ++-- arch/powerpc/mm/fault.c | 4 ++-- arch/riscv/mm/fault.c | 4 ++-- arch/s390/mm/fault.c | 4 ++-- arch/x86/mm/fault.c | 4 ++-- include/linux/mm_types.h | 23 ++++++++++++++++++++++- include/linux/sched/signal.h | 2 +- mm/huge_memory.c | 2 +- mm/hugetlb.c | 9 +++++---- mm/internal.h | 2 +- mm/memory.c | 20 +++++++++++--------- 14 files changed, 57 insertions(+), 33 deletions(-)
base-commit: 890dc639d8a33e674caa7ea9993dbd2416660447
Signature · 1 line
2.56.1.839.g22a8290db9-goog
Suren Baghdasaryan
reviewerRFC PATCH 1/2 mm: introduce should_retry_mm_fault() helper
Page fault handlers test VM_FAULT_RETRY in the result of handle_mm_fault() to decide whether the fault has to be retried. For a fault that was handled under the per-VMA lock, this means falling back to handling it under mmap_lock.
Introduce should_retry_mm_fault() and use it for these checks in the per-VMA lock path of the architecture fault handlers, in fault_signal_pending() and in mm_account_fault(). This keeps the set of fault codes that request a retry in one place, in preparation for splitting VM_FAULT_RETRY into separate codes.
The checks done after handling the fault under mmap_lock are left unchanged.
No functional change intended.
--- arch/arm/mm/fault.c | 4 ++-- arch/arm64/mm/fault.c | 4 ++-- arch/loongarch/mm/fault.c | 4 ++-- arch/powerpc/mm/fault.c | 4 ++-- arch/riscv/mm/fault.c | 4 ++-- arch/s390/mm/fault.c | 4 ++-- arch/x86/mm/fault.c | 4 ++-- include/linux/mm_types.h | 12 ++++++++++++ include/linux/sched/signal.h | 2 +- mm/memory.c | 2 +- 10 files changed, 28 insertions(+), 16 deletions(-)
arch/arm/mm/fault.c +2 -2
@@ -407,10 +407,10 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs) goto bad_area; } fault =handle_mm_fault (vma, addr, flags |FAULT_FLAG_VMA_LOCK , regs); -if (!(fault & (VM_FAULT_RETRY |VM_FAULT_COMPLETED ))) +if (!should_retry_mm_fault (fault) && !(fault &VM_FAULT_COMPLETED ))vma_end_read (vma); -if (!(fault &VM_FAULT_RETRY )) { +if (!should_retry_mm_fault (fault)) {count_vm_vma_lock_event (VMA_LOCK_SUCCESS ); goto done; }
arch/arm64/mm/fault.c +2 -2
@@ -718,10 +718,10 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, } fault =handle_mm_fault (vma, addr, mm_flags |FAULT_FLAG_VMA_LOCK , regs); -if (!(fault & (VM_FAULT_RETRY |VM_FAULT_COMPLETED ))) +if (!should_retry_mm_fault (fault) && !(fault &VM_FAULT_COMPLETED ))vma_end_read (vma); -if (!(fault &VM_FAULT_RETRY )) { +if (!should_retry_mm_fault (fault)) {count_vm_vma_lock_event (VMA_LOCK_SUCCESS ); goto done; }
arch/loongarch/mm/fault.c +2 -2
@@ -247,10 +247,10 @@ static void __kprobes __do_page_fault(struct pt_regs *regs, } fault =handle_mm_fault (vma, address, flags |FAULT_FLAG_VMA_LOCK , regs); -if (!(fault & (VM_FAULT_RETRY |VM_FAULT_COMPLETED ))) +if (!should_retry_mm_fault (fault) && !(fault &VM_FAULT_COMPLETED ))vma_end_read (vma); -if (!(fault &VM_FAULT_RETRY )) { +if (!should_retry_mm_fault (fault)) {count_vm_vma_lock_event (VMA_LOCK_SUCCESS ); goto done; }
arch/powerpc/mm/fault.c +2 -2
@@ -503,10 +503,10 @@ static int ___do_page_fault(struct pt_regs *regs, unsigned long address, } fault =handle_mm_fault (vma, address, flags |FAULT_FLAG_VMA_LOCK , regs); -if (!(fault & (VM_FAULT_RETRY |VM_FAULT_COMPLETED ))) +if (!should_retry_mm_fault (fault) && !(fault &VM_FAULT_COMPLETED ))vma_end_read (vma); -if (!(fault &VM_FAULT_RETRY )) { +if (!should_retry_mm_fault (fault)) {count_vm_vma_lock_event (VMA_LOCK_SUCCESS ); goto done; }
arch/riscv/mm/fault.c +2 -2
@@ -360,10 +360,10 @@ void handle_page_fault(struct pt_regs *regs) } fault =handle_mm_fault (vma, addr, flags |FAULT_FLAG_VMA_LOCK , regs); -if (!(fault & (VM_FAULT_RETRY |VM_FAULT_COMPLETED ))) +if (!should_retry_mm_fault (fault) && !(fault &VM_FAULT_COMPLETED ))vma_end_read (vma); -if (!(fault &VM_FAULT_RETRY )) { +if (!should_retry_mm_fault (fault)) {count_vm_vma_lock_event (VMA_LOCK_SUCCESS ); goto done; }
arch/s390/mm/fault.c +2 -2
@@ -303,9 +303,9 @@ static void do_exception(struct pt_regs *regs, int access)return handle_fault_error_nolock (regs,SEGV_ACCERR ); } fault =handle_mm_fault (vma, address, flags |FAULT_FLAG_VMA_LOCK , regs); -if (!(fault & (VM_FAULT_RETRY |VM_FAULT_COMPLETED ))) +if (!should_retry_mm_fault (fault) && !(fault &VM_FAULT_COMPLETED ))vma_end_read (vma); -if (!(fault &VM_FAULT_RETRY )) { +if (!should_retry_mm_fault (fault)) {count_vm_vma_lock_event (VMA_LOCK_SUCCESS ); goto done; }
arch/x86/mm/fault.c +2 -2
@@ -1341,10 +1341,10 @@ void do_user_addr_fault(struct pt_regs *regs,return ; } fault =handle_mm_fault (vma, address, flags |FAULT_FLAG_VMA_LOCK , regs); -if (!(fault & (VM_FAULT_RETRY |VM_FAULT_COMPLETED ))) +if (!should_retry_mm_fault (fault) && !(fault &VM_FAULT_COMPLETED ))vma_end_read (vma); -if (!(fault &VM_FAULT_RETRY )) { +if (!should_retry_mm_fault (fault)) {count_vm_vma_lock_event (VMA_LOCK_SUCCESS ); goto done; }
include/linux/mm_types.h +12 -0
@@ -1739,6 +1739,18 @@ enum vm_fault_reason { VM_FAULT_SIGSEGV | VM_FAULT_HWPOISON | \ VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK) +/** +* should_retry_mm_fault - Check whether a page fault has to be retried. +* @fault: The fault result returned by handle_mm_fault(). +* +* Return: true if the fault was not handled and the caller has to retry it, +* false otherwise. +*/ +static inline bool should_retry_mm_fault (vm_fault_t fault) +{ +return !!(fault &VM_FAULT_RETRY ); +} +#define VM_FAULT_RESULT_TRACE \ { (__force u32)VM_FAULT_OOM, "OOM" }, \ { (__force u32)VM_FAULT_SIGBUS, "SIGBUS" }, \
include/linux/sched/signal.h +1 -1
@@ -448,7 +448,7 @@ static inline int signal_pending_state(unsigned int state, struct task_struct *pstatic inline bool fault_signal_pending (vm_fault_t fault_flags,struct pt_regs *regs) { -return unlikely ((fault_flags &VM_FAULT_RETRY ) && +return unlikely (should_retry_mm_fault (fault_flags) && (fatal_signal_pending (current) || (user_mode (regs) &&signal_pending (current)))); }
mm/memory.c +1 -1
@@ -6876,7 +6876,7 @@ static inline void mm_account_fault(struct mm_struct *mm, struct pt_regs *regs,bool major;/* Incomplete faults will be accounted upon completion. */ -if (ret &VM_FAULT_RETRY ) +if (should_retry_mm_fault (ret))return ;/*
Signature · 1 line
2.56.1.839.g22a8290db9-goog
Suren Baghdasaryan
reviewerRFC PATCH 2/2 mm: split VM_FAULT_RETRY_LOCK_MM out of VM_FAULT_RETRY
In reply to Suren BaghdasaryanVM_FAULT_RETRY is returned for two different reasons. It tells the caller that the fault handler had to drop its lock, usually to wait for something, so the fault has to be handled again. With per-VMA locks it is also returned when the fault can not be handled under the per-VMA lock at all and has to be handled under mmap_lock. The caller can not tell these two cases apart.
Introduce VM_FAULT_RETRY_LOCK_MM for the second case and return it where the fault handler falls back from the per-VMA lock to mmap_lock. Those cases are:
- vmf_can_call_fault(), when the VMA has ->fault but no ->map_pages;
- __vmf_anon_prepare(), when the VMA has no anon_vma yet and mmap_lock can not be taken without blocking;
- do_swap_page() and do_huge_pmd_device_private(), because migrate_to_ram() is not ready to operate under the per-VMA lock.
VM_FAULT_RETRY keeps being returned in all the other cases.
vmf_anon_prepare() and hugetlb release the per-VMA lock based on the result of __vmf_anon_prepare(), so make them check the new code.
should_retry_mm_fault() accepts both codes, so page fault handlers keep retrying under mmap_lock in both cases. VM_FAULT_RETRY_LOCK_MM is never returned for a fault that is handled under mmap_lock, so the checks done after such a fault are left unchanged.
No functional change intended.
--- Documentation/filesystems/porting.rst | 4 ++-- include/linux/mm_types.h | 13 +++++++++++-- mm/huge_memory.c | 2 +- mm/hugetlb.c | 9 +++++---- mm/internal.h | 2 +- mm/memory.c | 18 ++++++++++-------- 6 files changed, 30 insertions(+), 18 deletions(-)
Documentation/filesystems/porting.rst +2 -2
@@ -944,8 +944,8 @@ Calling convention for ->huge_fault has changed. It now takes a page order instead of an enum page_entry_size, and it may be called without the mmap_lock held. All in-tree users have been audited and do not seem to depend on the mmap_lock being held, but out of tree users should verify -for themselves. If they do need it, they can return VM_FAULT_RETRY to -be called with the mmap_lock held. +for themselves. If they do need it, they can drop the per-VMA lock and +return VM_FAULT_RETRY_LOCK_MM to be called with the mmap_lock held. ---
include/linux/mm_types.h +11 -2
@@ -1711,6 +1711,8 @@ typedef __bitwise unsigned int vm_fault_t;* fsync() to complete (for synchronous page faults * in DAX) * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released +* @VM_FAULT_RETRY_LOCK_MM: fault can not be handled under per-VMA lock, +* must retry under mmap_lock * @VM_FAULT_HINDEX_MASK: mask HINDEX value * */
@@ -1728,6 +1730,7 @@ enum vm_fault_reason {VM_FAULT_DONE_COW = (__force vm_fault_t )0x001000 ,VM_FAULT_NEEDDSYNC = (__force vm_fault_t )0x002000 ,VM_FAULT_COMPLETED = (__force vm_fault_t )0x004000 , +VM_FAULT_RETRY_LOCK_MM = (__force vm_fault_t )0x008000 , VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000, };
@@ -1743,12 +1746,17 @@ enum vm_fault_reason {* should_retry_mm_fault - Check whether a page fault has to be retried. * @fault: The fault result returned by handle_mm_fault(). * +* A fault handled under the per-VMA lock can fail with either +* VM_FAULT_RETRY_LOCK_MM, when it has to be handled under mmap_lock, or with +* VM_FAULT_RETRY for all the other reasons to retry. A fault handled under +* mmap_lock can only fail with the latter. +* * Return: true if the fault was not handled and the caller has to retry it, * false otherwise. */ static inline bool should_retry_mm_fault (vm_fault_t fault) { -return !!(fault &VM_FAULT_RETRY ); +return !!(fault & (VM_FAULT_RETRY |VM_FAULT_RETRY_LOCK_MM )); }#define VM_FAULT_RESULT_TRACE \
@@ -1764,7 +1772,8 @@ static inline bool should_retry_mm_fault(vm_fault_t fault) { (__force u32)VM_FAULT_FALLBACK, "FALLBACK" }, \ { (__force u32)VM_FAULT_DONE_COW, "DONE_COW" }, \ { (__force u32)VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ - { (__force u32)VM_FAULT_COMPLETED, "COMPLETED" } + { (__force u32)VM_FAULT_COMPLETED, "COMPLETED" }, \ + { (__force u32)VM_FAULT_RETRY_LOCK_MM, "RETRY_LOCK_MM" } struct vm_special_mapping {const char *name;/* The name, e.g. "[vdso]". */
mm/huge_memory.c +1 -1
@@ -1479,7 +1479,7 @@ vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf)if (vmf->flags &FAULT_FLAG_VMA_LOCK ) {vma_end_read (vma); -return VM_FAULT_RETRY ; +return VM_FAULT_RETRY_LOCK_MM ; } ptl =pmd_lock (vma->vm_mm, vmf->pmd);
mm/hugetlb.c +5 -4
@@ -6130,9 +6130,9 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,/* * We must check to release the per-VMA lock. __vmf_anon_prepare() is -* the only way ret can be set to VM_FAULT_RETRY. +* the only way ret can be set to VM_FAULT_RETRY_LOCK_MM. */ -if (unlikely (ret &VM_FAULT_RETRY )) +if (unlikely (ret &VM_FAULT_RETRY_LOCK_MM ))vma_end_read (vma);mutex_unlock (&hugetlb_fault_mutex_table[hash]);
@@ -6391,9 +6391,10 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,/* * We must check to release the per-VMA lock. __vmf_anon_prepare() in -* hugetlb_wp() is the only way ret can be set to VM_FAULT_RETRY. +* hugetlb_wp() is the only way ret can be set to +* VM_FAULT_RETRY_LOCK_MM. */ -if (unlikely (ret &VM_FAULT_RETRY )) +if (unlikely (ret &VM_FAULT_RETRY_LOCK_MM ))vma_end_read (vma);mutex_unlock (&hugetlb_fault_mutex_table[hash]);
mm/internal.h +1 -1
@@ -574,7 +574,7 @@ static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) {vm_fault_t ret =__vmf_anon_prepare (vmf); -if (unlikely (ret &VM_FAULT_RETRY )) +if (unlikely (ret &VM_FAULT_RETRY_LOCK_MM ))vma_end_read (vmf->vma);return ret; }
mm/memory.c +10 -8
@@ -4021,7 +4021,7 @@ static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf)if (vma->vm_ops->map_pages || !(vmf->flags &FAULT_FLAG_VMA_LOCK ))return 0 ;vma_end_read (vma); -return VM_FAULT_RETRY ; +return VM_FAULT_RETRY_LOCK_MM ; }/**
@@ -4037,7 +4037,8 @@ static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf)* do with only the per-VMA lock held for this VMA. * * Return: 0 if fault handling can proceed. Any other value should be -* returned to the caller. +* returned to the caller. VM_FAULT_RETRY_LOCK_MM is returned with the +* per-VMA lock still held if the fault has to be retried under mmap_lock. */ vm_fault_t __vmf_anon_prepare (struct vm_fault *vmf) {
@@ -4048,7 +4049,7 @@ vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf)return 0 ;if (vmf->flags &FAULT_FLAG_VMA_LOCK ) {if (!mmap_read_trylock (vma->vm_mm)) -return VM_FAULT_RETRY ; +return VM_FAULT_RETRY_LOCK_MM ; }if (__anon_vma_prepare (vma)) ret =VM_FAULT_OOM ;
@@ -5046,7 +5047,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)* under VMA lock. */ vma_end_read (vma); - ret =VM_FAULT_RETRY ; + ret =VM_FAULT_RETRY_LOCK_MM ; goto out; }
@@ -6744,9 +6745,9 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)/* * On entry, we hold either the VMA lock or the mmap_lock -* (see FAULT_FLAG_VMA_LOCK). If VM_FAULT_RETRY is set in -* the result, the lock is not held on exit. See filemap_fault() -* and __folio_lock_or_retry(). +* (see FAULT_FLAG_VMA_LOCK). If VM_FAULT_RETRY or +* VM_FAULT_RETRY_LOCK_MM is set in the result, the lock is not held +* on exit. See filemap_fault() and __folio_lock_or_retry(). */ static vm_fault_t __handle_mm_fault (struct vm_area_struct *vma,unsigned long address,unsigned int flags)
@@ -6966,7 +6967,8 @@ static vm_fault_t sanitize_fault_flags(struct vm_area_struct *vma, }/* * Per-VMA locks can't be used with FAULT_FLAG_RETRY_NOWAIT because of -* the assumption that lock is dropped on VM_FAULT_RETRY. +* the assumption that lock is dropped on VM_FAULT_RETRY and +* VM_FAULT_RETRY_LOCK_MM. */ if (WARN_ON_ONCE((*flags & (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT)) ==
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]