Skip to content

Commit 26178ec

Browse files
committed
x86: mm: consolidate VM_FAULT_RETRY handling
The VM_FAULT_RETRY handling was confusing and incorrect for the case of returning to kernel mode. We need to handle the exception table fixup if we return to kernel mode due to a fatal signal - it will basically look to the kernel user mode access like the access failed due to the VM going away from udner it. Which is correct - the process is dying - and avoids the whole "repeat endless kernel page faults" case. Handling the VM_FAULT_RETRY early and in just one place also simplifies the mmap_sem handling, since once we've taken care of VM_FAULT_RETRY we know that we can just drop the lock. The remaining accounting and possible error handling is thread-local and does not need the mmap_sem. Signed-off-by: Linus Torvalds <[email protected]>
1 parent 7fb08ec commit 26178ec

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

arch/x86/mm/fault.c

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,
10551055
struct vm_area_struct *vma;
10561056
struct task_struct *tsk;
10571057
struct mm_struct *mm;
1058-
int fault;
1058+
int fault, major = 0;
10591059
unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
10601060

10611061
tsk = current;
@@ -1230,48 +1230,50 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,
12301230
* we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
12311231
*/
12321232
fault = handle_mm_fault(mm, vma, address, flags);
1233+
major |= fault & VM_FAULT_MAJOR;
12331234

12341235
/*
1235-
* If we need to retry but a fatal signal is pending, handle the
1236-
* signal first. We do not need to release the mmap_sem because it
1237-
* would already be released in __lock_page_or_retry in mm/filemap.c.
1236+
* If we need to retry the mmap_sem has already been released,
1237+
* and if there is a fatal signal pending there is no guarantee
1238+
* that we made any progress. Handle this case first.
12381239
*/
1239-
if (unlikely((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)))
1240+
if (unlikely(fault & VM_FAULT_RETRY)) {
1241+
/* Retry at most once */
1242+
if (flags & FAULT_FLAG_ALLOW_RETRY) {
1243+
flags &= ~FAULT_FLAG_ALLOW_RETRY;
1244+
flags |= FAULT_FLAG_TRIED;
1245+
if (!fatal_signal_pending(tsk))
1246+
goto retry;
1247+
}
1248+
1249+
/* User mode? Just return to handle the fatal exception */
1250+
if (fault & FAULT_FLAG_USER)
1251+
return;
1252+
1253+
/* Not returning to user mode? Handle exceptions or die: */
1254+
no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
12401255
return;
1256+
}
12411257

1258+
up_read(&mm->mmap_sem);
12421259
if (unlikely(fault & VM_FAULT_ERROR)) {
1243-
up_read(&mm->mmap_sem);
12441260
mm_fault_error(regs, error_code, address, fault);
12451261
return;
12461262
}
12471263

12481264
/*
1249-
* Major/minor page fault accounting is only done on the
1250-
* initial attempt. If we go through a retry, it is extremely
1251-
* likely that the page will be found in page cache at that point.
1265+
* Major/minor page fault accounting. If any of the events
1266+
* returned VM_FAULT_MAJOR, we account it as a major fault.
12521267
*/
1253-
if (flags & FAULT_FLAG_ALLOW_RETRY) {
1254-
if (fault & VM_FAULT_MAJOR) {
1255-
tsk->maj_flt++;
1256-
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
1257-
regs, address);
1258-
} else {
1259-
tsk->min_flt++;
1260-
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
1261-
regs, address);
1262-
}
1263-
if (fault & VM_FAULT_RETRY) {
1264-
/* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
1265-
* of starvation. */
1266-
flags &= ~FAULT_FLAG_ALLOW_RETRY;
1267-
flags |= FAULT_FLAG_TRIED;
1268-
goto retry;
1269-
}
1268+
if (major) {
1269+
tsk->maj_flt++;
1270+
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
1271+
} else {
1272+
tsk->min_flt++;
1273+
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
12701274
}
12711275

12721276
check_v8086_mode(regs, address, tsk);
1273-
1274-
up_read(&mm->mmap_sem);
12751277
}
12761278
NOKPROBE_SYMBOL(__do_page_fault);
12771279

0 commit comments

Comments
 (0)