Skip to content

Commit ec35271

Browse files
amlutosuryasaimadhu
authored andcommitted
x86/fault: Fold mm_fault_error() into do_user_addr_fault()
mm_fault_error() is logically just the end of do_user_addr_fault(). Combine the functions. This makes the code easier to read. Most of the churn here is from renaming hw_error_code to error_code in do_user_addr_fault(). This makes no difference at all to the generated code (objdump -dr) as compared to changing noinline to __always_inline in the definition of mm_fault_error(). Signed-off-by: Andy Lutomirski <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/dedc4d9c9b047e51ce38b991bd23971a28af4e7b.1612924255.git.luto@kernel.org
1 parent d24df8e commit ec35271

File tree

1 file changed

+45
-52
lines changed

1 file changed

+45
-52
lines changed

arch/x86/mm/fault.c

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -981,40 +981,6 @@ do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
981981
force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
982982
}
983983

984-
static noinline void
985-
mm_fault_error(struct pt_regs *regs, unsigned long error_code,
986-
unsigned long address, vm_fault_t fault)
987-
{
988-
if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) {
989-
no_context(regs, error_code, address, 0, 0);
990-
return;
991-
}
992-
993-
if (fault & VM_FAULT_OOM) {
994-
/* Kernel mode? Handle exceptions or die: */
995-
if (!(error_code & X86_PF_USER)) {
996-
no_context(regs, error_code, address,
997-
SIGSEGV, SEGV_MAPERR);
998-
return;
999-
}
1000-
1001-
/*
1002-
* We ran out of memory, call the OOM killer, and return the
1003-
* userspace (which will retry the fault, or kill us if we got
1004-
* oom-killed):
1005-
*/
1006-
pagefault_out_of_memory();
1007-
} else {
1008-
if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
1009-
VM_FAULT_HWPOISON_LARGE))
1010-
do_sigbus(regs, error_code, address, fault);
1011-
else if (fault & VM_FAULT_SIGSEGV)
1012-
bad_area_nosemaphore(regs, error_code, address);
1013-
else
1014-
BUG();
1015-
}
1016-
}
1017-
1018984
static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte)
1019985
{
1020986
if ((error_code & X86_PF_WRITE) && !pte_write(*pte))
@@ -1252,7 +1218,7 @@ NOKPROBE_SYMBOL(do_kern_addr_fault);
12521218
/* Handle faults in the user portion of the address space */
12531219
static inline
12541220
void do_user_addr_fault(struct pt_regs *regs,
1255-
unsigned long hw_error_code,
1221+
unsigned long error_code,
12561222
unsigned long address)
12571223
{
12581224
struct vm_area_struct *vma;
@@ -1272,8 +1238,8 @@ void do_user_addr_fault(struct pt_regs *regs,
12721238
* Reserved bits are never expected to be set on
12731239
* entries in the user portion of the page tables.
12741240
*/
1275-
if (unlikely(hw_error_code & X86_PF_RSVD))
1276-
pgtable_bad(regs, hw_error_code, address);
1241+
if (unlikely(error_code & X86_PF_RSVD))
1242+
pgtable_bad(regs, error_code, address);
12771243

12781244
/*
12791245
* If SMAP is on, check for invalid kernel (supervisor) access to user
@@ -1283,10 +1249,10 @@ void do_user_addr_fault(struct pt_regs *regs,
12831249
* enforcement appears to be consistent with the USER bit.
12841250
*/
12851251
if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP) &&
1286-
!(hw_error_code & X86_PF_USER) &&
1252+
!(error_code & X86_PF_USER) &&
12871253
!(regs->flags & X86_EFLAGS_AC)))
12881254
{
1289-
bad_area_nosemaphore(regs, hw_error_code, address);
1255+
bad_area_nosemaphore(regs, error_code, address);
12901256
return;
12911257
}
12921258

@@ -1295,7 +1261,7 @@ void do_user_addr_fault(struct pt_regs *regs,
12951261
* in a region with pagefaults disabled then we must not take the fault
12961262
*/
12971263
if (unlikely(faulthandler_disabled() || !mm)) {
1298-
bad_area_nosemaphore(regs, hw_error_code, address);
1264+
bad_area_nosemaphore(regs, error_code, address);
12991265
return;
13001266
}
13011267

@@ -1316,9 +1282,9 @@ void do_user_addr_fault(struct pt_regs *regs,
13161282

13171283
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
13181284

1319-
if (hw_error_code & X86_PF_WRITE)
1285+
if (error_code & X86_PF_WRITE)
13201286
flags |= FAULT_FLAG_WRITE;
1321-
if (hw_error_code & X86_PF_INSTR)
1287+
if (error_code & X86_PF_INSTR)
13221288
flags |= FAULT_FLAG_INSTRUCTION;
13231289

13241290
#ifdef CONFIG_X86_64
@@ -1334,7 +1300,7 @@ void do_user_addr_fault(struct pt_regs *regs,
13341300
* to consider the PF_PK bit.
13351301
*/
13361302
if (is_vsyscall_vaddr(address)) {
1337-
if (emulate_vsyscall(hw_error_code, regs, address))
1303+
if (emulate_vsyscall(error_code, regs, address))
13381304
return;
13391305
}
13401306
#endif
@@ -1357,7 +1323,7 @@ void do_user_addr_fault(struct pt_regs *regs,
13571323
* Fault from code in kernel from
13581324
* which we do not expect faults.
13591325
*/
1360-
bad_area_nosemaphore(regs, hw_error_code, address);
1326+
bad_area_nosemaphore(regs, error_code, address);
13611327
return;
13621328
}
13631329
retry:
@@ -1373,17 +1339,17 @@ void do_user_addr_fault(struct pt_regs *regs,
13731339

13741340
vma = find_vma(mm, address);
13751341
if (unlikely(!vma)) {
1376-
bad_area(regs, hw_error_code, address);
1342+
bad_area(regs, error_code, address);
13771343
return;
13781344
}
13791345
if (likely(vma->vm_start <= address))
13801346
goto good_area;
13811347
if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1382-
bad_area(regs, hw_error_code, address);
1348+
bad_area(regs, error_code, address);
13831349
return;
13841350
}
13851351
if (unlikely(expand_stack(vma, address))) {
1386-
bad_area(regs, hw_error_code, address);
1352+
bad_area(regs, error_code, address);
13871353
return;
13881354
}
13891355

@@ -1392,8 +1358,8 @@ void do_user_addr_fault(struct pt_regs *regs,
13921358
* we can handle it..
13931359
*/
13941360
good_area:
1395-
if (unlikely(access_error(hw_error_code, vma))) {
1396-
bad_area_access_error(regs, hw_error_code, address, vma);
1361+
if (unlikely(access_error(error_code, vma))) {
1362+
bad_area_access_error(regs, error_code, address, vma);
13971363
return;
13981364
}
13991365

@@ -1415,7 +1381,7 @@ void do_user_addr_fault(struct pt_regs *regs,
14151381
/* Quick path to respond to signals */
14161382
if (fault_signal_pending(fault, regs)) {
14171383
if (!user_mode(regs))
1418-
no_context(regs, hw_error_code, address, SIGBUS,
1384+
no_context(regs, error_code, address, SIGBUS,
14191385
BUS_ADRERR);
14201386
return;
14211387
}
@@ -1432,11 +1398,38 @@ void do_user_addr_fault(struct pt_regs *regs,
14321398
}
14331399

14341400
mmap_read_unlock(mm);
1435-
if (unlikely(fault & VM_FAULT_ERROR)) {
1436-
mm_fault_error(regs, hw_error_code, address, fault);
1401+
if (likely(!(fault & VM_FAULT_ERROR)))
1402+
return;
1403+
1404+
if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) {
1405+
no_context(regs, error_code, address, 0, 0);
14371406
return;
14381407
}
14391408

1409+
if (fault & VM_FAULT_OOM) {
1410+
/* Kernel mode? Handle exceptions or die: */
1411+
if (!(error_code & X86_PF_USER)) {
1412+
no_context(regs, error_code, address,
1413+
SIGSEGV, SEGV_MAPERR);
1414+
return;
1415+
}
1416+
1417+
/*
1418+
* We ran out of memory, call the OOM killer, and return the
1419+
* userspace (which will retry the fault, or kill us if we got
1420+
* oom-killed):
1421+
*/
1422+
pagefault_out_of_memory();
1423+
} else {
1424+
if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
1425+
VM_FAULT_HWPOISON_LARGE))
1426+
do_sigbus(regs, error_code, address, fault);
1427+
else if (fault & VM_FAULT_SIGSEGV)
1428+
bad_area_nosemaphore(regs, error_code, address);
1429+
else
1430+
BUG();
1431+
}
1432+
14401433
check_v8086_mode(regs, address, tsk);
14411434
}
14421435
NOKPROBE_SYMBOL(do_user_addr_fault);

0 commit comments

Comments
 (0)