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.

Signed-off-by: Suren Baghdasaryan <[email protected]>
---
 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(-)

diff --git a/arch/arm/mm/fault.c b/arch/arm/mm/fault.c
index 0a09d4ff7718..48e2aa4dbb63 100644
--- a/arch/arm/mm/fault.c
+++ b/arch/arm/mm/fault.c
@@ -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;
        }
diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index b22736853fcd..41165beccab0 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -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;
        }
diff --git a/arch/loongarch/mm/fault.c b/arch/loongarch/mm/fault.c
index 2c93d33356e5..edbf700af18e 100644
--- a/arch/loongarch/mm/fault.c
+++ b/arch/loongarch/mm/fault.c
@@ -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;
        }
diff --git a/arch/powerpc/mm/fault.c b/arch/powerpc/mm/fault.c
index 806c74e0d5ab..eaadb8023eb2 100644
--- a/arch/powerpc/mm/fault.c
+++ b/arch/powerpc/mm/fault.c
@@ -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;
        }
diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c
index 04ed6f8acae4..742b2cc3b495 100644
--- a/arch/riscv/mm/fault.c
+++ b/arch/riscv/mm/fault.c
@@ -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;
        }
diff --git a/arch/s390/mm/fault.c b/arch/s390/mm/fault.c
index 46d828926009..3056b33496ec 100644
--- a/arch/s390/mm/fault.c
+++ b/arch/s390/mm/fault.c
@@ -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;
        }
diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
index aa88370ce739..23edca8f0555 100644
--- a/arch/x86/mm/fault.c
+++ b/arch/x86/mm/fault.c
@@ -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;
        }
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 4c576e24ce42..7a80c904eab9 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -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" },        \
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index 0adb51ab1542..9e1334379296 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -448,7 +448,7 @@ static inline int signal_pending_state(unsigned int state, 
struct task_struct *p
 static 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))));
 }
diff --git a/mm/memory.c b/mm/memory.c
index 90fc7c395124..e0f1aeed713c 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -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;
 
        /*
-- 
2.56.1.839.g22a8290db9-goog


Reply via email to