Skip to contentSkip to conversation

riscv: Fix page fault accounting in do_page_fault()

PATCH New Pekka Enberg 1 participant 2 messages Jun 28, 2020 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 riscv: Fix page fault accounting in do_page_fault()

The do_page_fault() function accounts for page faults only if FAULT_FLAG_ALLOW_RETRY is set, which is incorrect. Fix up page fault accounting by separating fault retry and accounting logic.

---
 arch/riscv/mm/fault.c | 41 ++++++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 21 deletions(-)
arch/riscv/mm/fault.c +20 -21
index ae7b7fe24658..26f51cf99542 100644
@@ -32,7 +32,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs)
 	unsigned long addr, cause;
 	unsigned int flags = FAULT_FLAG_DEFAULT;
 	int code = SEGV_MAPERR;
-	vm_fault_t fault;
+	vm_fault_t fault, major = 0;
 
 	cause = regs->cause;
 	addr = regs->badaddr;
@@ -111,6 +111,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs)
 	 * the fault.
 	 */
 	fault = handle_mm_fault(vma, addr, flags);
+	major |= fault & VM_FAULT_MAJOR;
 
 	/*
 	 * If we need to retry but a fatal signal is pending, handle the
@@ -120,6 +121,18 @@ asmlinkage void do_page_fault(struct pt_regs *regs)
 	if (fault_signal_pending(fault, regs))
 		return;
 
+	if (unlikely((fault & VM_FAULT_RETRY) &&
+		      (flags & FAULT_FLAG_ALLOW_RETRY))) {
+		flags |= FAULT_FLAG_TRIED;
+
+		/*
+		 * No need to mmap_read_unlock(mm) as we would
+		 * have already released it in __lock_page_or_retry
+		 * in mm/filemap.c.
+		 */
+		goto retry;
+	}
+
 	if (unlikely(fault & VM_FAULT_ERROR)) {
 		if (fault & VM_FAULT_OOM)
 			goto out_of_memory;
@@ -133,26 +146,12 @@ asmlinkage void do_page_fault(struct pt_regs *regs)
 	 * initial attempt. If we go through a retry, it is extremely
 	 * likely that the page will be found in page cache at that point.
 	 */
-	if (flags & FAULT_FLAG_ALLOW_RETRY) {
-		if (fault & VM_FAULT_MAJOR) {
-			tsk->maj_flt++;
-			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
-				      1, regs, addr);
-		} else {
-			tsk->min_flt++;
-			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
-				      1, regs, addr);
-		}
-		if (fault & VM_FAULT_RETRY) {
-			flags |= FAULT_FLAG_TRIED;
-
-			/*
-			 * No need to mmap_read_unlock(mm) as we would
-			 * have already released it in __lock_page_or_retry
-			 * in mm/filemap.c.
-			 */
-			goto retry;
-		}
+	if (major) {
+		tsk->maj_flt++;
+		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, addr);
+	} else {
+		tsk->min_flt++;
+		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, addr);
 	}
 
 	mmap_read_unlock(mm);
Signature · 7 lines
2.25.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

PATCH 2/2 riscv: Drop mmap lock for page fault accounting

There's no need to hold on to the mmap lock while doing page fault accounting in do_page_fault().

---
 arch/riscv/mm/fault.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/riscv/mm/fault.c +2 -3
index 26f51cf99542..6256f25027af 100644
@@ -133,6 +133,8 @@ asmlinkage void do_page_fault(struct pt_regs *regs)
 		goto retry;
 	}
 
+	mmap_read_unlock(mm);
+
 	if (unlikely(fault & VM_FAULT_ERROR)) {
 		if (fault & VM_FAULT_OOM)
 			goto out_of_memory;
@@ -154,7 +156,6 @@ asmlinkage void do_page_fault(struct pt_regs *regs)
 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, addr);
 	}
 
-	mmap_read_unlock(mm);
 	return;
 
 	/*
@@ -190,14 +191,12 @@ asmlinkage void do_page_fault(struct pt_regs *regs)
 	 * (which will retry the fault, or kill us if we got oom-killed).
 	 */
 out_of_memory:
-	mmap_read_unlock(mm);
 	if (!user_mode(regs))
 		goto no_context;
 	pagefault_out_of_memory();
 	return;
 
 do_sigbus:
-	mmap_read_unlock(mm);
 	/* Kernel mode? Handle exceptions or die */
 	if (!user_mode(regs))
 		goto no_context;
Signature · 7 lines
2.25.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv