kmemcheck: remove useless check
[safe/jmp/linux-2.6] / arch / x86 / mm / fault.c
index ebfaca3..78a5fff 100644 (file)
@@ -1,41 +1,20 @@
 /*
  *  Copyright (C) 1995  Linus Torvalds
  *  Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
+ *  Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
  */
-#include <linux/interrupt.h>
-#include <linux/mmiotrace.h>
-#include <linux/bootmem.h>
-#include <linux/compiler.h>
-#include <linux/highmem.h>
-#include <linux/kprobes.h>
-#include <linux/uaccess.h>
-#include <linux/vmalloc.h>
-#include <linux/vt_kern.h>
-#include <linux/signal.h>
-#include <linux/kernel.h>
-#include <linux/ptrace.h>
-#include <linux/string.h>
-#include <linux/module.h>
-#include <linux/kdebug.h>
-#include <linux/errno.h>
-#include <linux/magic.h>
-#include <linux/sched.h>
-#include <linux/types.h>
-#include <linux/init.h>
-#include <linux/mman.h>
-#include <linux/tty.h>
-#include <linux/smp.h>
-#include <linux/mm.h>
-
-#include <asm-generic/sections.h>
-
-#include <asm/tlbflush.h>
-#include <asm/pgalloc.h>
-#include <asm/segment.h>
-#include <asm/system.h>
-#include <asm/proto.h>
-#include <asm/traps.h>
-#include <asm/desc.h>
+#include <linux/magic.h>               /* STACK_END_MAGIC              */
+#include <linux/sched.h>               /* test_thread_flag(), ...      */
+#include <linux/kdebug.h>              /* oops_begin/end, ...          */
+#include <linux/module.h>              /* search_exception_table       */
+#include <linux/bootmem.h>             /* max_low_pfn                  */
+#include <linux/kprobes.h>             /* __kprobes, ...               */
+#include <linux/mmiotrace.h>           /* kmmio_handler, ...           */
+#include <linux/perf_counter.h>                /* perf_swcounter_event         */
+
+#include <asm/traps.h>                 /* dotraplinkage, ...           */
+#include <asm/pgalloc.h>               /* pgd_*(), ...                 */
+#include <asm/kmemcheck.h>             /* kmemcheck_*(), ...           */
 
 /*
  * Page fault error code bits:
@@ -56,7 +35,8 @@ enum x86_pf_error_code {
 };
 
 /*
- * (returns 0 if mmiotrace is disabled)
+ * Returns 0 if mmiotrace is disabled, or if the fault is not
+ * handled by mmiotrace:
  */
 static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
 {
@@ -223,12 +203,10 @@ static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
        if (!pmd_present(*pmd_k))
                return NULL;
 
-       if (!pmd_present(*pmd)) {
+       if (!pmd_present(*pmd))
                set_pmd(pmd, *pmd_k);
-               arch_flush_lazy_mmu_mode();
-       } else {
+       else
                BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
-       }
 
        return pmd_k;
 }
@@ -536,8 +514,6 @@ bad:
 static int is_errata93(struct pt_regs *regs, unsigned long address)
 {
 #ifdef CONFIG_X86_64
-       static int once;
-
        if (address != regs->ip)
                return 0;
 
@@ -547,10 +523,7 @@ static int is_errata93(struct pt_regs *regs, unsigned long address)
        address |= 0xffffffffUL << 32;
        if ((address >= (u64)_stext && address <= (u64)_etext) ||
            (address >= MODULES_VADDR && address <= MODULES_END)) {
-               if (!once) {
-                       printk(errata93_warning);
-                       once = 1;
-               }
+               printk_once(errata93_warning);
                regs->ip = address;
                return 1;
        }
@@ -659,11 +632,8 @@ no_context(struct pt_regs *regs, unsigned long error_code,
 {
        struct task_struct *tsk = current;
        unsigned long *stackend;
-
-#ifdef CONFIG_X86_64
        unsigned long flags;
        int sig;
-#endif
 
        /* Are we prepared to handle this kernel fault? */
        if (fixup_exception(regs))
@@ -690,11 +660,7 @@ no_context(struct pt_regs *regs, unsigned long error_code,
         * Oops. The kernel tried to access some bad page. We'll have to
         * terminate things with extreme prejudice:
         */
-#ifdef CONFIG_X86_32
-       bust_spinlocks(1);
-#else
        flags = oops_begin();
-#endif
 
        show_fault_oops(regs, error_code, address);
 
@@ -702,15 +668,10 @@ no_context(struct pt_regs *regs, unsigned long error_code,
        if (*stackend != STACK_END_MAGIC)
                printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
 
-       tsk->thread.cr2 = address;
-       tsk->thread.trap_no = 14;
-       tsk->thread.error_code = error_code;
+       tsk->thread.cr2         = address;
+       tsk->thread.trap_no     = 14;
+       tsk->thread.error_code  = error_code;
 
-#ifdef CONFIG_X86_32
-       die("Oops", regs, error_code);
-       bust_spinlocks(0);
-       do_exit(SIGKILL);
-#else
        sig = SIGKILL;
        if (__die("Oops", regs, error_code))
                sig = 0;
@@ -719,7 +680,6 @@ no_context(struct pt_regs *regs, unsigned long error_code,
        printk(KERN_EMERG "CR2: %016lx\n", address);
 
        oops_end(flags, regs, sig);
-#endif
 }
 
 /*
@@ -849,11 +809,9 @@ do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
        if (!(error_code & PF_USER))
                no_context(regs, error_code, address);
 
-#ifdef CONFIG_X86_32
-       /* User space => ok to do another page fault: */
+       /* User-space => ok to do another page fault: */
        if (is_prefetch(regs, error_code, address))
                return;
-#endif
 
        tsk->thread.cr2         = address;
        tsk->thread.error_code  = error_code;
@@ -973,11 +931,7 @@ access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
 
 static int fault_in_kernel_space(unsigned long address)
 {
-#ifdef CONFIG_X86_32
-       return address >= TASK_SIZE;
-#else
-       return address >= TASK_SIZE64;
-#endif
+       return address >= TASK_SIZE_MAX;
 }
 
 /*
@@ -985,10 +939,8 @@ static int fault_in_kernel_space(unsigned long address)
  * and the problem, and then passes it off to one of the appropriate
  * routines.
  */
-#ifdef CONFIG_X86_64
-asmlinkage
-#endif
-void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
+dotraplinkage void __kprobes
+do_page_fault(struct pt_regs *regs, unsigned long error_code)
 {
        struct vm_area_struct *vma;
        struct task_struct *tsk;
@@ -1000,11 +952,17 @@ void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
        tsk = current;
        mm = tsk->mm;
 
-       prefetchw(&mm->mmap_sem);
-
        /* Get the faulting address: */
        address = read_cr2();
 
+       /*
+        * Detect and handle instructions that would cause a page fault for
+        * both a tracked kernel page and a userspace page.
+        */
+       if (kmemcheck_active(regs))
+               kmemcheck_hide(regs);
+       prefetchw(&mm->mmap_sem);
+
        if (unlikely(kmmio_fault(regs, address)))
                return;
 
@@ -1022,9 +980,13 @@ void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
         * protection error (error_code & 9) == 0.
         */
        if (unlikely(fault_in_kernel_space(address))) {
-               if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
-                   vmalloc_fault(address) >= 0)
-                       return;
+               if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
+                       if (vmalloc_fault(address) >= 0)
+                               return;
+
+                       if (kmemcheck_fault(regs, address, error_code))
+                               return;
+               }
 
                /* Can handle a stale RO->RW TLB: */
                if (spurious_fault(error_code, address))
@@ -1063,6 +1025,8 @@ void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
        if (unlikely(error_code & PF_RSVD))
                pgtable_bad(regs, error_code, address);
 
+       perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
+
        /*
         * If we're in an interrupt, have no user context or are running
         * in an atomic region then we must not take the fault:
@@ -1149,17 +1113,22 @@ good_area:
         * make sure we exit gracefully rather than endlessly redo
         * the fault:
         */
-       fault = handle_mm_fault(mm, vma, address, write);
+       fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
 
        if (unlikely(fault & VM_FAULT_ERROR)) {
                mm_fault_error(regs, error_code, address, fault);
                return;
        }
 
-       if (fault & VM_FAULT_MAJOR)
+       if (fault & VM_FAULT_MAJOR) {
                tsk->maj_flt++;
-       else
+               perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
+                                    regs, address);
+       } else {
                tsk->min_flt++;
+               perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
+                                    regs, address);
+       }
 
        check_v8086_mode(regs, address, tsk);