Skip to content

Commit

Permalink
mm/fault, arch: Use pagefault_disable() to check for disabled pagefau…
Browse files Browse the repository at this point in the history
…lts in the handler

Introduce faulthandler_disabled() and use it to check for irq context and
disabled pagefaults (via pagefault_disable()) in the pagefault handlers.

Please note that we keep the in_atomic() checks in place - to detect
whether in irq context (in which case preemption is always properly
disabled).

In contrast, preempt_disable() should never be used to disable pagefaults.
With !CONFIG_PREEMPT_COUNT, preempt_disable() doesn't modify the preempt
counter, and therefore the result of in_atomic() differs.
We validate that condition by using might_fault() checks when calling
might_sleep().

Therefore, add a comment to faulthandler_disabled(), describing why this
is needed.

faulthandler_disabled() and pagefault_disable() are defined in
linux/uaccess.h, so let's properly add that include to all relevant files.

This patch is based on a patch from Thomas Gleixner.

Reviewed-and-tested-by: Thomas Gleixner <[email protected]>
Signed-off-by: David Hildenbrand <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Cc: [email protected]
Cc: Linus Torvalds <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
David Hildenbrand authored and Ingo Molnar committed May 19, 2015
1 parent ce01948 commit 70ffdb9
Show file tree
Hide file tree
Showing 30 changed files with 72 additions and 57 deletions.
5 changes: 2 additions & 3 deletions arch/alpha/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
#include <linux/smp.h>
#include <linux/interrupt.h>
#include <linux/module.h>

#include <asm/uaccess.h>
#include <linux/uaccess.h>

extern void die_if_kernel(char *,struct pt_regs *,long, unsigned long *);

Expand Down Expand Up @@ -107,7 +106,7 @@ do_page_fault(unsigned long address, unsigned long mmcsr,

/* If we're in an interrupt context, or have no user context,
we must not take the fault. */
if (!mm || in_atomic())
if (!mm || faulthandler_disabled())
goto no_context;

#ifdef CONFIG_ALPHA_LARGE_VMALLOC
Expand Down
2 changes: 1 addition & 1 deletion arch/arc/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto no_context;

if (user_mode(regs))
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto no_context;

if (user_mode(regs))
Expand Down
2 changes: 1 addition & 1 deletion arch/arm64/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
* If we're in an interrupt or have no user context, we must not take
* the fault.
*/
if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto no_context;

if (user_mode(regs))
Expand Down
4 changes: 2 additions & 2 deletions arch/avr32/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
#include <linux/pagemap.h>
#include <linux/kdebug.h>
#include <linux/kprobes.h>
#include <linux/uaccess.h>

#include <asm/mmu_context.h>
#include <asm/sysreg.h>
#include <asm/tlb.h>
#include <asm/uaccess.h>

#ifdef CONFIG_KPROBES
static inline int notify_page_fault(struct pt_regs *regs, int trap)
Expand Down Expand Up @@ -81,7 +81,7 @@ asmlinkage void do_page_fault(unsigned long ecr, struct pt_regs *regs)
* If we're in an interrupt or have no user context, we must
* not take the fault...
*/
if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM))
if (faulthandler_disabled() || !mm || regs->sr & SYSREG_BIT(GM))
goto no_context;

local_irq_enable();
Expand Down
6 changes: 3 additions & 3 deletions arch/cris/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/wait.h>
#include <asm/uaccess.h>
#include <linux/uaccess.h>
#include <arch/system.h>

extern int find_fixup_code(struct pt_regs *);
Expand Down Expand Up @@ -109,11 +109,11 @@ do_page_fault(unsigned long address, struct pt_regs *regs,
info.si_code = SEGV_MAPERR;

/*
* If we're in an interrupt or "atomic" operation or have no
* If we're in an interrupt, have pagefaults disabled or have no
* user context, we must not take the fault.
*/

if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto no_context;

if (user_mode(regs))
Expand Down
4 changes: 2 additions & 2 deletions arch/frv/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include <linux/kernel.h>
#include <linux/ptrace.h>
#include <linux/hardirq.h>
#include <linux/uaccess.h>

#include <asm/pgtable.h>
#include <asm/uaccess.h>
#include <asm/gdb-stub.h>

/*****************************************************************************/
Expand Down Expand Up @@ -78,7 +78,7 @@ asmlinkage void do_page_fault(int datammu, unsigned long esr0, unsigned long ear
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto no_context;

if (user_mode(__frame))
Expand Down
4 changes: 2 additions & 2 deletions arch/ia64/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#include <linux/kprobes.h>
#include <linux/kdebug.h>
#include <linux/prefetch.h>
#include <linux/uaccess.h>

#include <asm/pgtable.h>
#include <asm/processor.h>
#include <asm/uaccess.h>

extern int die(char *, struct pt_regs *, long);

Expand Down Expand Up @@ -96,7 +96,7 @@ ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *re
/*
* If we're in an interrupt or have no user context, we must not take the fault..
*/
if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto no_context;

#ifdef CONFIG_VIRTUAL_MEM_MAP
Expand Down
8 changes: 4 additions & 4 deletions arch/m32r/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include <linux/vt_kern.h> /* For unblank_screen() */
#include <linux/highmem.h>
#include <linux/module.h>
#include <linux/uaccess.h>

#include <asm/m32r.h>
#include <asm/uaccess.h>
#include <asm/hardirq.h>
#include <asm/mmu_context.h>
#include <asm/tlbflush.h>
Expand Down Expand Up @@ -111,10 +111,10 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code,
mm = tsk->mm;

/*
* If we're in an interrupt or have no user context or are running in an
* atomic region then we must not take the fault..
* If we're in an interrupt or have no user context or have pagefaults
* disabled then we must not take the fault.
*/
if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto bad_area_nosemaphore;

if (error_code & ACE_USERMODE)
Expand Down
4 changes: 2 additions & 2 deletions arch/m68k/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#include <linux/ptrace.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/uaccess.h>

#include <asm/setup.h>
#include <asm/traps.h>
#include <asm/uaccess.h>
#include <asm/pgalloc.h>

extern void die_if_kernel(char *, struct pt_regs *, long);
Expand Down Expand Up @@ -81,7 +81,7 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto no_context;

if (user_mode(regs))
Expand Down
2 changes: 1 addition & 1 deletion arch/metag/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,

mm = tsk->mm;

if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto no_context;

if (user_mode(regs))
Expand Down
8 changes: 4 additions & 4 deletions arch/microblaze/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ void do_page_fault(struct pt_regs *regs, unsigned long address,
if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)
is_write = 0;

if (unlikely(in_atomic() || !mm)) {
if (unlikely(faulthandler_disabled() || !mm)) {
if (kernel_mode(regs))
goto bad_area_nosemaphore;

/* in_atomic() in user mode is really bad,
/* faulthandler_disabled() in user mode is really bad,
as is current->mm == NULL. */
pr_emerg("Page fault in user mode with in_atomic(), mm = %p\n",
mm);
pr_emerg("Page fault in user mode with faulthandler_disabled(), mm = %p\n",
mm);
pr_emerg("r15 = %lx MSR = %lx\n",
regs->r15, regs->msr);
die("Weird page fault", regs, SIGSEGV);
Expand Down
4 changes: 2 additions & 2 deletions arch/mips/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/perf_event.h>
#include <linux/uaccess.h>

#include <asm/branch.h>
#include <asm/mmu_context.h>
#include <asm/uaccess.h>
#include <asm/ptrace.h>
#include <asm/highmem.h> /* For VMALLOC_END */
#include <linux/kdebug.h>
Expand Down Expand Up @@ -94,7 +94,7 @@ static void __kprobes __do_page_fault(struct pt_regs *regs, unsigned long write,
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto bad_area_nosemaphore;

if (user_mode(regs))
Expand Down
4 changes: 2 additions & 2 deletions arch/mn10300/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/vt_kern.h> /* For unblank_screen() */
#include <linux/uaccess.h>

#include <asm/uaccess.h>
#include <asm/pgalloc.h>
#include <asm/hardirq.h>
#include <asm/cpu-regs.h>
Expand Down Expand Up @@ -168,7 +168,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long fault_code,
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto no_context;

if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR)
Expand Down
2 changes: 1 addition & 1 deletion arch/nios2/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (in_atomic() || !mm)
if (faulthandler_disabled() || !mm)
goto bad_area_nosemaphore;

if (user_mode(regs))
Expand Down
4 changes: 2 additions & 2 deletions arch/parisc/kernel/traps.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#include <linux/console.h>
#include <linux/bug.h>
#include <linux/ratelimit.h>
#include <linux/uaccess.h>

#include <asm/assembly.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/traps.h>
Expand Down Expand Up @@ -800,7 +800,7 @@ void notrace handle_interruption(int code, struct pt_regs *regs)
* unless pagefault_disable() was called before.
*/

if (fault_space == 0 && !in_atomic())
if (fault_space == 0 && !faulthandler_disabled())
{
pdc_chassis_send_status(PDC_CHASSIS_DIRECT_PANIC);
parisc_terminate("Kernel Fault", regs, code, fault_address);
Expand Down
4 changes: 2 additions & 2 deletions arch/parisc/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/uaccess.h>

#include <asm/uaccess.h>
#include <asm/traps.h>

/* Various important other fields */
Expand Down Expand Up @@ -207,7 +207,7 @@ void do_page_fault(struct pt_regs *regs, unsigned long code,
int fault;
unsigned int flags;

if (in_atomic())
if (pagefault_disabled())
goto no_context;

tsk = current;
Expand Down
9 changes: 5 additions & 4 deletions arch/powerpc/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
#include <linux/ratelimit.h>
#include <linux/context_tracking.h>
#include <linux/hugetlb.h>
#include <linux/uaccess.h>

#include <asm/firmware.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm/mmu.h>
#include <asm/mmu_context.h>
#include <asm/uaccess.h>
#include <asm/tlbflush.h>
#include <asm/siginfo.h>
#include <asm/debug.h>
Expand Down Expand Up @@ -272,15 +272,16 @@ int __kprobes do_page_fault(struct pt_regs *regs, unsigned long address,
if (!arch_irq_disabled_regs(regs))
local_irq_enable();

if (in_atomic() || mm == NULL) {
if (faulthandler_disabled() || mm == NULL) {
if (!user_mode(regs)) {
rc = SIGSEGV;
goto bail;
}
/* in_atomic() in user mode is really bad,
/* faulthandler_disabled() in user mode is really bad,
as is current->mm == NULL. */
printk(KERN_EMERG "Page fault in user mode with "
"in_atomic() = %d mm = %p\n", in_atomic(), mm);
"faulthandler_disabled() = %d mm = %p\n",
faulthandler_disabled(), mm);
printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
regs->nip, regs->msr);
die("Weird page fault", regs, SIGSEGV);
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ static inline int do_exception(struct pt_regs *regs, int access)
* user context.
*/
fault = VM_FAULT_BADCONTEXT;
if (unlikely(!user_space_fault(regs) || in_atomic() || !mm))
if (unlikely(!user_space_fault(regs) || faulthandler_disabled() || !mm))
goto out;

address = trans_exc_code & __FAIL_ADDR_MASK;
Expand Down
3 changes: 2 additions & 1 deletion arch/score/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <linux/string.h>
#include <linux/types.h>
#include <linux/ptrace.h>
#include <linux/uaccess.h>

/*
* This routine handles page faults. It determines the address,
Expand Down Expand Up @@ -73,7 +74,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (in_atomic() || !mm)
if (pagefault_disabled() || !mm)
goto bad_area_nosemaphore;

if (user_mode(regs))
Expand Down
5 changes: 3 additions & 2 deletions arch/sh/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <linux/kprobes.h>
#include <linux/perf_event.h>
#include <linux/kdebug.h>
#include <linux/uaccess.h>
#include <asm/io_trapped.h>
#include <asm/mmu_context.h>
#include <asm/tlbflush.h>
Expand Down Expand Up @@ -438,9 +439,9 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,

/*
* If we're in an interrupt, have no user context or are running
* in an atomic region then we must not take the fault:
* with pagefaults disabled then we must not take the fault:
*/
if (unlikely(in_atomic() || !mm)) {
if (unlikely(faulthandler_disabled() || !mm)) {
bad_area_nosemaphore(regs, error_code, address);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions arch/sparc/mm/fault_32.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <linux/perf_event.h>
#include <linux/interrupt.h>
#include <linux/kdebug.h>
#include <linux/uaccess.h>

#include <asm/page.h>
#include <asm/pgtable.h>
Expand All @@ -29,7 +30,6 @@
#include <asm/setup.h>
#include <asm/smp.h>
#include <asm/traps.h>
#include <asm/uaccess.h>

#include "mm_32.h"

Expand Down Expand Up @@ -196,7 +196,7 @@ asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (in_atomic() || !mm)
if (pagefault_disabled() || !mm)
goto no_context;

perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
Expand Down
Loading

0 comments on commit 70ffdb9

Please sign in to comment.