2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995 Linus Torvalds
|
|
|
|
* Copyright (C) 2001,2002 Andi Kleen, SuSE Labs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/signal.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/ptrace.h>
|
x86: mmiotrace full patch, preview 1
kmmio.c handles the list of mmio probes with callbacks, list of traced
pages, and attaching into the page fault handler and die notifier. It
arms, traps and disarms the given pages, this is the core of mmiotrace.
mmio-mod.c is a user interface, hooking into ioremap functions and
registering the mmio probes. It also decodes the required information
from trapped mmio accesses via the pre and post callbacks in each probe.
Currently, hooking into ioremap functions works by redefining the symbols
of the target (binary) kernel module, so that it calls the traced
versions of the functions.
The most notable changes done since the last discussion are:
- kmmio.c is a built-in, not part of the module
- direct call from fault.c to kmmio.c, removing all dynamic hooks
- prepare for unregistering probes at any time
- make kmmio re-initializable and accessible to more than one user
- rewrite kmmio locking to remove all spinlocks from page fault path
Can I abuse call_rcu() like I do in kmmio.c:unregister_kmmio_probe()
or is there a better way?
The function called via call_rcu() itself calls call_rcu() again,
will this work or break? There I need a second grace period for RCU
after the first grace period for page faults.
Mmiotrace itself (mmio-mod.c) is still a module, I am going to attack
that next. At some point I will start looking into how to make mmiotrace
a tracer component of ftrace (thanks for the hint, Ingo). Ftrace should
make the user space part of mmiotracing as simple as
'cat /debug/trace/mmio > dump.txt'.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:57 +00:00
|
|
|
#include <linux/mmiotrace.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/mman.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/smp.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/tty.h>
|
|
|
|
#include <linux/vt_kern.h> /* For unblank_screen() */
|
|
|
|
#include <linux/compiler.h>
|
2008-01-30 12:34:11 +00:00
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <linux/bootmem.h> /* for max_low_pfn */
|
2007-05-08 07:27:03 +00:00
|
|
|
#include <linux/vmalloc.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/module.h>
|
2005-09-06 22:19:28 +00:00
|
|
|
#include <linux/kprobes.h>
|
2006-12-07 01:14:06 +00:00
|
|
|
#include <linux/uaccess.h>
|
2007-05-08 07:27:03 +00:00
|
|
|
#include <linux/kdebug.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#include <asm/system.h>
|
2008-01-30 12:34:11 +00:00
|
|
|
#include <asm/desc.h>
|
|
|
|
#include <asm/segment.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
#include <asm/smp.h>
|
|
|
|
#include <asm/tlbflush.h>
|
|
|
|
#include <asm/proto.h>
|
|
|
|
#include <asm-generic/sections.h>
|
2008-07-23 12:06:37 +00:00
|
|
|
#include <asm/traps.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-30 12:32:19 +00:00
|
|
|
/*
|
|
|
|
* Page fault error code bits
|
|
|
|
* bit 0 == 0 means no page found, 1 means protection fault
|
|
|
|
* bit 1 == 0 means read, 1 means write
|
|
|
|
* bit 2 == 0 means kernel, 1 means user-mode
|
|
|
|
* bit 3 == 1 means use of reserved bit detected
|
|
|
|
* bit 4 == 1 means fault was an instruction fetch
|
|
|
|
*/
|
2008-01-30 12:32:53 +00:00
|
|
|
#define PF_PROT (1<<0)
|
2006-01-11 21:44:09 +00:00
|
|
|
#define PF_WRITE (1<<1)
|
2008-01-30 12:32:53 +00:00
|
|
|
#define PF_USER (1<<2)
|
|
|
|
#define PF_RSVD (1<<3)
|
2006-01-11 21:44:09 +00:00
|
|
|
#define PF_INSTR (1<<4)
|
|
|
|
|
x86: mmiotrace full patch, preview 1
kmmio.c handles the list of mmio probes with callbacks, list of traced
pages, and attaching into the page fault handler and die notifier. It
arms, traps and disarms the given pages, this is the core of mmiotrace.
mmio-mod.c is a user interface, hooking into ioremap functions and
registering the mmio probes. It also decodes the required information
from trapped mmio accesses via the pre and post callbacks in each probe.
Currently, hooking into ioremap functions works by redefining the symbols
of the target (binary) kernel module, so that it calls the traced
versions of the functions.
The most notable changes done since the last discussion are:
- kmmio.c is a built-in, not part of the module
- direct call from fault.c to kmmio.c, removing all dynamic hooks
- prepare for unregistering probes at any time
- make kmmio re-initializable and accessible to more than one user
- rewrite kmmio locking to remove all spinlocks from page fault path
Can I abuse call_rcu() like I do in kmmio.c:unregister_kmmio_probe()
or is there a better way?
The function called via call_rcu() itself calls call_rcu() again,
will this work or break? There I need a second grace period for RCU
after the first grace period for page faults.
Mmiotrace itself (mmio-mod.c) is still a module, I am going to attack
that next. At some point I will start looking into how to make mmiotrace
a tracer component of ftrace (thanks for the hint, Ingo). Ftrace should
make the user space part of mmiotracing as simple as
'cat /debug/trace/mmio > dump.txt'.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:57 +00:00
|
|
|
static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
|
2008-05-12 19:20:56 +00:00
|
|
|
{
|
2008-10-24 17:08:11 +00:00
|
|
|
#ifdef CONFIG_MMIOTRACE
|
x86: mmiotrace full patch, preview 1
kmmio.c handles the list of mmio probes with callbacks, list of traced
pages, and attaching into the page fault handler and die notifier. It
arms, traps and disarms the given pages, this is the core of mmiotrace.
mmio-mod.c is a user interface, hooking into ioremap functions and
registering the mmio probes. It also decodes the required information
from trapped mmio accesses via the pre and post callbacks in each probe.
Currently, hooking into ioremap functions works by redefining the symbols
of the target (binary) kernel module, so that it calls the traced
versions of the functions.
The most notable changes done since the last discussion are:
- kmmio.c is a built-in, not part of the module
- direct call from fault.c to kmmio.c, removing all dynamic hooks
- prepare for unregistering probes at any time
- make kmmio re-initializable and accessible to more than one user
- rewrite kmmio locking to remove all spinlocks from page fault path
Can I abuse call_rcu() like I do in kmmio.c:unregister_kmmio_probe()
or is there a better way?
The function called via call_rcu() itself calls call_rcu() again,
will this work or break? There I need a second grace period for RCU
after the first grace period for page faults.
Mmiotrace itself (mmio-mod.c) is still a module, I am going to attack
that next. At some point I will start looking into how to make mmiotrace
a tracer component of ftrace (thanks for the hint, Ingo). Ftrace should
make the user space part of mmiotracing as simple as
'cat /debug/trace/mmio > dump.txt'.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:57 +00:00
|
|
|
if (unlikely(is_kmmio_active()))
|
|
|
|
if (kmmio_handler(regs, addr) == 1)
|
|
|
|
return -1;
|
2008-05-12 19:20:56 +00:00
|
|
|
#endif
|
x86: mmiotrace full patch, preview 1
kmmio.c handles the list of mmio probes with callbacks, list of traced
pages, and attaching into the page fault handler and die notifier. It
arms, traps and disarms the given pages, this is the core of mmiotrace.
mmio-mod.c is a user interface, hooking into ioremap functions and
registering the mmio probes. It also decodes the required information
from trapped mmio accesses via the pre and post callbacks in each probe.
Currently, hooking into ioremap functions works by redefining the symbols
of the target (binary) kernel module, so that it calls the traced
versions of the functions.
The most notable changes done since the last discussion are:
- kmmio.c is a built-in, not part of the module
- direct call from fault.c to kmmio.c, removing all dynamic hooks
- prepare for unregistering probes at any time
- make kmmio re-initializable and accessible to more than one user
- rewrite kmmio locking to remove all spinlocks from page fault path
Can I abuse call_rcu() like I do in kmmio.c:unregister_kmmio_probe()
or is there a better way?
The function called via call_rcu() itself calls call_rcu() again,
will this work or break? There I need a second grace period for RCU
after the first grace period for page faults.
Mmiotrace itself (mmio-mod.c) is still a module, I am going to attack
that next. At some point I will start looking into how to make mmiotrace
a tracer component of ftrace (thanks for the hint, Ingo). Ftrace should
make the user space part of mmiotracing as simple as
'cat /debug/trace/mmio > dump.txt'.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:57 +00:00
|
|
|
return 0;
|
2008-05-12 19:20:56 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 08:24:07 +00:00
|
|
|
static inline int notify_page_fault(struct pt_regs *regs)
|
2006-06-26 07:25:25 +00:00
|
|
|
{
|
2008-01-30 12:32:19 +00:00
|
|
|
#ifdef CONFIG_KPROBES
|
2007-10-16 08:24:07 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* kprobe_running() needs smp_processor_id() */
|
2008-01-30 12:34:10 +00:00
|
|
|
if (!user_mode_vm(regs)) {
|
2007-10-16 08:24:07 +00:00
|
|
|
preempt_disable();
|
|
|
|
if (kprobe_running() && kprobe_fault_handler(regs, 14))
|
|
|
|
ret = 1;
|
|
|
|
preempt_enable();
|
|
|
|
}
|
2006-06-26 07:25:25 +00:00
|
|
|
|
2007-10-16 08:24:07 +00:00
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2008-01-30 12:32:19 +00:00
|
|
|
}
|
2006-06-26 07:25:25 +00:00
|
|
|
|
2008-01-30 12:32:35 +00:00
|
|
|
/*
|
|
|
|
* X86_32
|
|
|
|
* Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
|
|
|
|
* Check that here and ignore it.
|
|
|
|
*
|
|
|
|
* X86_64
|
|
|
|
* Sometimes the CPU reports invalid exceptions on prefetch.
|
|
|
|
* Check that here and ignore it.
|
|
|
|
*
|
|
|
|
* Opcode checker based on code by Richard Brunner
|
|
|
|
*/
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
static int is_prefetch(struct pt_regs *regs, unsigned long error_code,
|
|
|
|
unsigned long addr)
|
2008-01-30 12:32:19 +00:00
|
|
|
{
|
2006-12-07 01:14:06 +00:00
|
|
|
unsigned char *instr;
|
2005-04-16 22:20:36 +00:00
|
|
|
int scan_more = 1;
|
2008-01-30 12:32:19 +00:00
|
|
|
int prefetch = 0;
|
2005-04-16 22:24:59 +00:00
|
|
|
unsigned char *max_instr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-03-27 20:29:09 +00:00
|
|
|
/*
|
|
|
|
* If it was a exec (instruction fetch) fault on NX page, then
|
|
|
|
* do not ignore the fault:
|
|
|
|
*/
|
2006-01-11 21:44:09 +00:00
|
|
|
if (error_code & PF_INSTR)
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
2008-01-30 12:32:35 +00:00
|
|
|
|
2008-01-30 12:33:12 +00:00
|
|
|
instr = (unsigned char *)convert_ip_to_linear(current, regs);
|
2005-04-16 22:24:59 +00:00
|
|
|
max_instr = instr + 15;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-06-23 07:08:46 +00:00
|
|
|
if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
|
2008-01-30 12:32:19 +00:00
|
|
|
while (scan_more && instr < max_instr) {
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned char opcode;
|
|
|
|
unsigned char instr_hi;
|
|
|
|
unsigned char instr_lo;
|
|
|
|
|
2006-12-07 01:14:06 +00:00
|
|
|
if (probe_kernel_address(instr, opcode))
|
2008-01-30 12:32:19 +00:00
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-30 12:32:19 +00:00
|
|
|
instr_hi = opcode & 0xf0;
|
|
|
|
instr_lo = opcode & 0x0f;
|
2005-04-16 22:20:36 +00:00
|
|
|
instr++;
|
|
|
|
|
2008-01-30 12:32:19 +00:00
|
|
|
switch (instr_hi) {
|
2005-04-16 22:20:36 +00:00
|
|
|
case 0x20:
|
|
|
|
case 0x30:
|
2008-01-30 12:32:19 +00:00
|
|
|
/*
|
|
|
|
* Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
|
|
|
|
* In X86_64 long mode, the CPU will signal invalid
|
|
|
|
* opcode if some of these prefixes are present so
|
|
|
|
* X86_64 will never get here anyway
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
scan_more = ((instr_lo & 7) == 0x6);
|
|
|
|
break;
|
2008-01-30 12:32:19 +00:00
|
|
|
#ifdef CONFIG_X86_64
|
2005-04-16 22:20:36 +00:00
|
|
|
case 0x40:
|
2008-01-30 12:32:19 +00:00
|
|
|
/*
|
|
|
|
* In AMD64 long mode 0x40..0x4F are valid REX prefixes
|
|
|
|
* Need to figure out under what instruction mode the
|
|
|
|
* instruction was issued. Could check the LDT for lm,
|
|
|
|
* but for now it's good enough to assume that long
|
|
|
|
* mode only uses well known segments or kernel.
|
|
|
|
*/
|
2005-06-23 07:08:46 +00:00
|
|
|
scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
2008-01-30 12:32:19 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
case 0x60:
|
|
|
|
/* 0x64 thru 0x67 are valid prefixes in all modes. */
|
|
|
|
scan_more = (instr_lo & 0xC) == 0x4;
|
2008-01-30 12:32:19 +00:00
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
case 0xF0:
|
2008-01-30 12:32:35 +00:00
|
|
|
/* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
|
2005-04-16 22:20:36 +00:00
|
|
|
scan_more = !instr_lo || (instr_lo>>1) == 1;
|
2008-01-30 12:32:19 +00:00
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
case 0x00:
|
|
|
|
/* Prefetch instruction is 0x0F0D or 0x0F18 */
|
|
|
|
scan_more = 0;
|
2008-01-30 12:33:12 +00:00
|
|
|
|
2006-12-07 01:14:06 +00:00
|
|
|
if (probe_kernel_address(instr, opcode))
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
prefetch = (instr_lo == 0xF) &&
|
|
|
|
(opcode == 0x0D || opcode == 0x18);
|
2008-01-30 12:32:19 +00:00
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
default:
|
|
|
|
scan_more = 0;
|
|
|
|
break;
|
2008-01-30 12:32:19 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return prefetch;
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:32:35 +00:00
|
|
|
static void force_sig_info_fault(int si_signo, int si_code,
|
|
|
|
unsigned long address, struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
siginfo_t info;
|
|
|
|
|
|
|
|
info.si_signo = si_signo;
|
|
|
|
info.si_errno = 0;
|
|
|
|
info.si_code = si_code;
|
|
|
|
info.si_addr = (void __user *)address;
|
|
|
|
force_sig_info(si_signo, &info, tsk);
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:34:10 +00:00
|
|
|
#ifdef CONFIG_X86_64
|
2008-01-30 12:32:19 +00:00
|
|
|
static int bad_address(void *p)
|
|
|
|
{
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned long dummy;
|
2006-12-07 01:14:06 +00:00
|
|
|
return probe_kernel_address((unsigned long *)p, dummy);
|
2008-01-30 12:32:19 +00:00
|
|
|
}
|
2008-01-30 12:34:10 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-13 21:31:31 +00:00
|
|
|
static void dump_pagetable(unsigned long address)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-01-30 12:34:10 +00:00
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
__typeof__(pte_val(__pte(0))) page;
|
|
|
|
|
|
|
|
page = read_cr3();
|
|
|
|
page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
|
|
|
|
#ifdef CONFIG_X86_PAE
|
|
|
|
printk("*pdpt = %016Lx ", page);
|
|
|
|
if ((page >> PAGE_SHIFT) < max_low_pfn
|
|
|
|
&& page & _PAGE_PRESENT) {
|
|
|
|
page &= PAGE_MASK;
|
|
|
|
page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
|
|
|
|
& (PTRS_PER_PMD - 1)];
|
|
|
|
printk(KERN_CONT "*pde = %016Lx ", page);
|
|
|
|
page &= ~_PAGE_NX;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
printk("*pde = %08lx ", page);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We must not directly access the pte in the highpte
|
|
|
|
* case if the page table is located in highmem.
|
|
|
|
* And let's rather not kmap-atomic the pte, just in case
|
|
|
|
* it's allocated already.
|
|
|
|
*/
|
|
|
|
if ((page >> PAGE_SHIFT) < max_low_pfn
|
|
|
|
&& (page & _PAGE_PRESENT)
|
|
|
|
&& !(page & _PAGE_PSE)) {
|
|
|
|
page &= PAGE_MASK;
|
|
|
|
page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
|
|
|
|
& (PTRS_PER_PTE - 1)];
|
|
|
|
printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
|
|
|
|
}
|
|
|
|
|
|
|
|
printk("\n");
|
|
|
|
#else /* CONFIG_X86_64 */
|
2005-04-16 22:20:36 +00:00
|
|
|
pgd_t *pgd;
|
|
|
|
pud_t *pud;
|
|
|
|
pmd_t *pmd;
|
|
|
|
pte_t *pte;
|
|
|
|
|
2007-07-22 09:12:29 +00:00
|
|
|
pgd = (pgd_t *)read_cr3();
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-30 12:32:19 +00:00
|
|
|
pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
|
2005-04-16 22:20:36 +00:00
|
|
|
pgd += pgd_index(address);
|
|
|
|
if (bad_address(pgd)) goto bad;
|
2006-02-03 20:51:47 +00:00
|
|
|
printk("PGD %lx ", pgd_val(*pgd));
|
2008-01-30 12:32:19 +00:00
|
|
|
if (!pgd_present(*pgd)) goto ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-06-26 11:57:56 +00:00
|
|
|
pud = pud_offset(pgd, address);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (bad_address(pud)) goto bad;
|
|
|
|
printk("PUD %lx ", pud_val(*pud));
|
2008-02-04 15:48:09 +00:00
|
|
|
if (!pud_present(*pud) || pud_large(*pud))
|
|
|
|
goto ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
pmd = pmd_offset(pud, address);
|
|
|
|
if (bad_address(pmd)) goto bad;
|
|
|
|
printk("PMD %lx ", pmd_val(*pmd));
|
2007-10-19 18:35:03 +00:00
|
|
|
if (!pmd_present(*pmd) || pmd_large(*pmd)) goto ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
pte = pte_offset_kernel(pmd, address);
|
|
|
|
if (bad_address(pte)) goto bad;
|
2008-01-30 12:32:19 +00:00
|
|
|
printk("PTE %lx", pte_val(*pte));
|
2005-04-16 22:20:36 +00:00
|
|
|
ret:
|
|
|
|
printk("\n");
|
|
|
|
return;
|
|
|
|
bad:
|
|
|
|
printk("BAD\n");
|
2008-01-30 12:34:10 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
|
|
|
|
{
|
|
|
|
unsigned index = pgd_index(address);
|
|
|
|
pgd_t *pgd_k;
|
|
|
|
pud_t *pud, *pud_k;
|
|
|
|
pmd_t *pmd, *pmd_k;
|
|
|
|
|
|
|
|
pgd += index;
|
|
|
|
pgd_k = init_mm.pgd + index;
|
|
|
|
|
|
|
|
if (!pgd_present(*pgd_k))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set_pgd(pgd, *pgd_k); here would be useless on PAE
|
|
|
|
* and redundant with the set_pmd() on non-PAE. As would
|
|
|
|
* set_pud.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pud = pud_offset(pgd, address);
|
|
|
|
pud_k = pud_offset(pgd_k, address);
|
|
|
|
if (!pud_present(*pud_k))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pmd = pmd_offset(pud, address);
|
|
|
|
pmd_k = pmd_offset(pud_k, address);
|
|
|
|
if (!pmd_present(*pmd_k))
|
|
|
|
return NULL;
|
|
|
|
if (!pmd_present(*pmd)) {
|
|
|
|
set_pmd(pmd, *pmd_k);
|
|
|
|
arch_flush_lazy_mmu_mode();
|
|
|
|
} else
|
|
|
|
BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
|
|
|
|
return pmd_k;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-01-30 12:34:10 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-30 12:32:35 +00:00
|
|
|
#ifdef CONFIG_X86_64
|
2008-01-30 12:32:19 +00:00
|
|
|
static const char errata93_warning[] =
|
2005-04-16 22:20:36 +00:00
|
|
|
KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
|
|
|
|
KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
|
|
|
|
KERN_ERR "******* Please consider a BIOS update.\n"
|
|
|
|
KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
|
2008-01-30 12:33:13 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Workaround for K8 erratum #93 & buggy BIOS.
|
|
|
|
BIOS SMM functions are required to use a specific workaround
|
2008-01-30 12:32:19 +00:00
|
|
|
to avoid corruption of the 64bit RIP register on C stepping K8.
|
|
|
|
A lot of BIOS that didn't get tested properly miss this.
|
2005-04-16 22:20:36 +00:00
|
|
|
The OS sees this as a page fault with the upper 32bits of RIP cleared.
|
|
|
|
Try to work around it here.
|
2008-01-30 12:33:13 +00:00
|
|
|
Note we only handle faults in kernel here.
|
|
|
|
Does nothing for X86_32
|
|
|
|
*/
|
2008-01-30 12:32:19 +00:00
|
|
|
static int is_errata93(struct pt_regs *regs, unsigned long address)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-01-30 12:33:13 +00:00
|
|
|
#ifdef CONFIG_X86_64
|
2005-04-16 22:20:36 +00:00
|
|
|
static int warned;
|
2008-01-30 12:30:56 +00:00
|
|
|
if (address != regs->ip)
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
2008-01-30 12:32:19 +00:00
|
|
|
if ((address >> 32) != 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
address |= 0xffffffffUL << 32;
|
2008-01-30 12:32:19 +00:00
|
|
|
if ((address >= (u64)_stext && address <= (u64)_etext) ||
|
|
|
|
(address >= MODULES_VADDR && address <= MODULES_END)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!warned) {
|
2008-01-30 12:32:19 +00:00
|
|
|
printk(errata93_warning);
|
2005-04-16 22:20:36 +00:00
|
|
|
warned = 1;
|
|
|
|
}
|
2008-01-30 12:30:56 +00:00
|
|
|
regs->ip = address;
|
2005-04-16 22:20:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2008-01-30 12:33:13 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
2008-01-30 12:32:19 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-30 12:34:09 +00:00
|
|
|
/*
|
|
|
|
* Work around K8 erratum #100 K8 in compat mode occasionally jumps to illegal
|
|
|
|
* addresses >4GB. We catch this in the page fault handler because these
|
|
|
|
* addresses are not reachable. Just detect this case and return. Any code
|
|
|
|
* segment in LDT is compatibility mode.
|
|
|
|
*/
|
|
|
|
static int is_errata100(struct pt_regs *regs, unsigned long address)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) &&
|
|
|
|
(address >> 32))
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:34:09 +00:00
|
|
|
static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_X86_F00F_BUG
|
|
|
|
unsigned long nr;
|
|
|
|
/*
|
|
|
|
* Pentium F0 0F C7 C8 bug workaround.
|
|
|
|
*/
|
|
|
|
if (boot_cpu_data.f00f_bug) {
|
|
|
|
nr = (address - idt_descr.address) >> 3;
|
|
|
|
|
|
|
|
if (nr == 6) {
|
|
|
|
do_invalid_op(regs, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:34:10 +00:00
|
|
|
static void show_fault_oops(struct pt_regs *regs, unsigned long error_code,
|
|
|
|
unsigned long address)
|
|
|
|
{
|
2008-01-30 12:34:10 +00:00
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
if (!oops_may_print())
|
|
|
|
return;
|
2008-01-30 12:34:11 +00:00
|
|
|
#endif
|
2008-01-30 12:34:10 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_X86_PAE
|
|
|
|
if (error_code & PF_INSTR) {
|
2008-02-01 16:49:43 +00:00
|
|
|
unsigned int level;
|
2008-01-30 12:34:10 +00:00
|
|
|
pte_t *pte = lookup_address(address, &level);
|
|
|
|
|
|
|
|
if (pte && pte_present(*pte) && !pte_exec(*pte))
|
|
|
|
printk(KERN_CRIT "kernel tried to execute "
|
|
|
|
"NX-protected page - exploit attempt? "
|
2008-11-13 23:38:40 +00:00
|
|
|
"(uid: %d)\n", current_uid());
|
2008-01-30 12:34:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-30 12:34:10 +00:00
|
|
|
printk(KERN_ALERT "BUG: unable to handle kernel ");
|
2008-01-30 12:34:10 +00:00
|
|
|
if (address < PAGE_SIZE)
|
2008-01-30 12:34:10 +00:00
|
|
|
printk(KERN_CONT "NULL pointer dereference");
|
2008-01-30 12:34:10 +00:00
|
|
|
else
|
2008-01-30 12:34:10 +00:00
|
|
|
printk(KERN_CONT "paging request");
|
2008-07-01 13:38:13 +00:00
|
|
|
printk(KERN_CONT " at %p\n", (void *) address);
|
2008-01-30 12:34:10 +00:00
|
|
|
printk(KERN_ALERT "IP:");
|
2008-01-30 12:34:10 +00:00
|
|
|
printk_address(regs->ip, 1);
|
|
|
|
dump_pagetable(address);
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:34:10 +00:00
|
|
|
#ifdef CONFIG_X86_64
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
static noinline void pgtable_bad(struct pt_regs *regs,
|
|
|
|
unsigned long error_code, unsigned long address)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-09-12 16:49:24 +00:00
|
|
|
unsigned long flags = oops_begin();
|
2008-10-22 10:00:09 +00:00
|
|
|
int sig = SIGKILL;
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
struct task_struct *tsk = current;
|
2005-09-12 16:49:24 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
tsk->comm, address);
|
2005-04-16 22:20:36 +00:00
|
|
|
dump_pagetable(address);
|
2006-01-11 21:42:14 +00:00
|
|
|
tsk->thread.cr2 = address;
|
|
|
|
tsk->thread.trap_no = 14;
|
|
|
|
tsk->thread.error_code = error_code;
|
2008-01-30 12:31:23 +00:00
|
|
|
if (__die("Bad pagetable", regs, error_code))
|
2008-10-22 10:00:09 +00:00
|
|
|
sig = 0;
|
|
|
|
oops_end(flags, regs, sig);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-01-30 12:34:10 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
static noinline void no_context(struct pt_regs *regs,
|
|
|
|
unsigned long error_code, unsigned long address)
|
|
|
|
{
|
|
|
|
struct task_struct *tsk = current;
|
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
unsigned long flags;
|
|
|
|
int sig;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Are we prepared to handle this kernel fault? */
|
|
|
|
if (fixup_exception(regs))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X86_32
|
|
|
|
* Valid to do another page fault here, because if this fault
|
|
|
|
* had been triggered by is_prefetch fixup_exception would have
|
|
|
|
* handled it.
|
|
|
|
*
|
|
|
|
* X86_64
|
|
|
|
* Hall of shame of CPU/BIOS bugs.
|
|
|
|
*/
|
|
|
|
if (is_prefetch(regs, error_code, address))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (is_errata93(regs, address))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
/* Executive summary in case the body of the oops scrolled away */
|
|
|
|
printk(KERN_EMERG "CR2: %016lx\n", address);
|
|
|
|
oops_end(flags, regs, sig);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __bad_area_nosemaphore(struct pt_regs *regs,
|
|
|
|
unsigned long error_code, unsigned long address,
|
|
|
|
int si_code)
|
|
|
|
{
|
|
|
|
struct task_struct *tsk = current;
|
|
|
|
|
|
|
|
/* User mode accesses just cause a SIGSEGV */
|
|
|
|
if (error_code & PF_USER) {
|
|
|
|
/*
|
|
|
|
* It's possible to have interrupts off here.
|
|
|
|
*/
|
|
|
|
local_irq_enable();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Valid to do another page fault here because this one came
|
|
|
|
* from user space.
|
|
|
|
*/
|
|
|
|
if (is_prefetch(regs, error_code, address))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (is_errata100(regs, address))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
|
|
|
|
printk_ratelimit()) {
|
|
|
|
printk(
|
|
|
|
"%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
|
|
|
|
task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
|
|
|
|
tsk->comm, task_pid_nr(tsk), address,
|
|
|
|
(void *) regs->ip, (void *) regs->sp, error_code);
|
|
|
|
print_vma_addr(" in ", regs->ip);
|
|
|
|
printk("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
tsk->thread.cr2 = address;
|
|
|
|
/* Kernel addresses are always protection faults */
|
|
|
|
tsk->thread.error_code = error_code | (address >= TASK_SIZE);
|
|
|
|
tsk->thread.trap_no = 14;
|
|
|
|
force_sig_info_fault(SIGSEGV, si_code, address, tsk);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_f00f_bug(regs, address))
|
|
|
|
return;
|
|
|
|
|
|
|
|
no_context(regs, error_code, address);
|
|
|
|
}
|
|
|
|
|
|
|
|
static noinline void bad_area_nosemaphore(struct pt_regs *regs,
|
|
|
|
unsigned long error_code, unsigned long address)
|
|
|
|
{
|
|
|
|
__bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __bad_area(struct pt_regs *regs,
|
|
|
|
unsigned long error_code, unsigned long address,
|
|
|
|
int si_code)
|
|
|
|
{
|
|
|
|
struct mm_struct *mm = current->mm;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Something tried to access memory that isn't in our memory map..
|
|
|
|
* Fix it, but check if it's kernel or user first..
|
|
|
|
*/
|
|
|
|
up_read(&mm->mmap_sem);
|
|
|
|
|
|
|
|
__bad_area_nosemaphore(regs, error_code, address, si_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
static noinline void bad_area(struct pt_regs *regs,
|
|
|
|
unsigned long error_code, unsigned long address)
|
|
|
|
{
|
|
|
|
__bad_area(regs, error_code, address, SEGV_MAPERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static noinline void bad_area_access_error(struct pt_regs *regs,
|
|
|
|
unsigned long error_code, unsigned long address)
|
|
|
|
{
|
|
|
|
__bad_area(regs, error_code, address, SEGV_ACCERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
|
|
|
|
static void out_of_memory(struct pt_regs *regs,
|
|
|
|
unsigned long error_code, unsigned long address)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We ran out of memory, call the OOM killer, and return the userspace
|
|
|
|
* (which will retry the fault, or kill us if we got oom-killed).
|
|
|
|
*/
|
|
|
|
up_read(¤t->mm->mmap_sem);
|
|
|
|
pagefault_out_of_memory();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_sigbus(struct pt_regs *regs,
|
|
|
|
unsigned long error_code, unsigned long address)
|
|
|
|
{
|
|
|
|
struct task_struct *tsk = current;
|
|
|
|
struct mm_struct *mm = tsk->mm;
|
|
|
|
|
|
|
|
up_read(&mm->mmap_sem);
|
|
|
|
|
|
|
|
/* Kernel mode? Handle exceptions or die */
|
|
|
|
if (!(error_code & PF_USER))
|
|
|
|
no_context(regs, error_code, address);
|
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
/* 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;
|
|
|
|
tsk->thread.trap_no = 14;
|
|
|
|
force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static noinline void mm_fault_error(struct pt_regs *regs,
|
|
|
|
unsigned long error_code, unsigned long address, unsigned int fault)
|
|
|
|
{
|
|
|
|
if (fault & VM_FAULT_OOM)
|
|
|
|
out_of_memory(regs, error_code, address);
|
|
|
|
else if (fault & VM_FAULT_SIGBUS)
|
|
|
|
do_sigbus(regs, error_code, address);
|
|
|
|
else
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2008-02-06 21:39:43 +00:00
|
|
|
static int spurious_fault_check(unsigned long error_code, pte_t *pte)
|
|
|
|
{
|
|
|
|
if ((error_code & PF_WRITE) && !pte_write(*pte))
|
|
|
|
return 0;
|
|
|
|
if ((error_code & PF_INSTR) && !pte_exec(*pte))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:34:11 +00:00
|
|
|
/*
|
|
|
|
* Handle a spurious fault caused by a stale TLB entry. This allows
|
|
|
|
* us to lazily refresh the TLB when increasing the permissions of a
|
|
|
|
* kernel page (RO -> RW or NX -> X). Doing it eagerly is very
|
|
|
|
* expensive since that implies doing a full cross-processor TLB
|
|
|
|
* flush, even if no stale TLB entries exist on other processors.
|
|
|
|
* There are no security implications to leaving a stale TLB when
|
|
|
|
* increasing the permissions on a page.
|
|
|
|
*/
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
static noinline int spurious_fault(unsigned long error_code,
|
|
|
|
unsigned long address)
|
2008-01-30 12:34:11 +00:00
|
|
|
{
|
|
|
|
pgd_t *pgd;
|
|
|
|
pud_t *pud;
|
|
|
|
pmd_t *pmd;
|
|
|
|
pte_t *pte;
|
|
|
|
|
|
|
|
/* Reserved-bit violation or user access to kernel space? */
|
|
|
|
if (error_code & (PF_USER | PF_RSVD))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pgd = init_mm.pgd + pgd_index(address);
|
|
|
|
if (!pgd_present(*pgd))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pud = pud_offset(pgd, address);
|
|
|
|
if (!pud_present(*pud))
|
|
|
|
return 0;
|
|
|
|
|
2008-02-06 21:39:43 +00:00
|
|
|
if (pud_large(*pud))
|
|
|
|
return spurious_fault_check(error_code, (pte_t *) pud);
|
|
|
|
|
2008-01-30 12:34:11 +00:00
|
|
|
pmd = pmd_offset(pud, address);
|
|
|
|
if (!pmd_present(*pmd))
|
|
|
|
return 0;
|
|
|
|
|
2008-02-06 21:39:43 +00:00
|
|
|
if (pmd_large(*pmd))
|
|
|
|
return spurious_fault_check(error_code, (pte_t *) pmd);
|
|
|
|
|
2008-01-30 12:34:11 +00:00
|
|
|
pte = pte_offset_kernel(pmd, address);
|
|
|
|
if (!pte_present(*pte))
|
|
|
|
return 0;
|
|
|
|
|
2008-02-06 21:39:43 +00:00
|
|
|
return spurious_fault_check(error_code, pte);
|
2008-01-30 12:34:11 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2008-01-30 12:34:10 +00:00
|
|
|
* X86_32
|
|
|
|
* Handle a fault on the vmalloc or module mapping area
|
|
|
|
*
|
|
|
|
* X86_64
|
2006-01-11 21:44:00 +00:00
|
|
|
* Handle a fault on the vmalloc area
|
2005-05-17 04:53:31 +00:00
|
|
|
*
|
|
|
|
* This assumes no large pages in there.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
static noinline int vmalloc_fault(unsigned long address)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-01-30 12:33:13 +00:00
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
unsigned long pgd_paddr;
|
|
|
|
pmd_t *pmd_k;
|
|
|
|
pte_t *pte_k;
|
x86: fix endless page faults in mount_block_root for Linux 2.6
Page faults in kernel address space between PAGE_OFFSET up to
VMALLOC_START should not try to map as vmalloc.
Fix rarely endless page faults inside mount_block_root for root
filesystem at boot time.
All 32bit kernels up to 2.6.25 can fail into this hole.
I can not present this under native linux kernel. I see, that the 64bit
has fixed the problem. I copied the same lines into 32bit part.
Recorded debugs are from coLinux kernel 2.6.22.18 (virtualisation):
http://www.henrynestler.com/colinux/testing/pfn-check-0.7.3/20080410-antinx/bug16-recursive-page-fault-endless.txt
The physicaly memory was trimmed down to 192MB to better catch the bug.
More memory gets the bug more rarely.
Details, how every x86 32bit system can fail:
Start from "mount_block_root",
http://lxr.linux.no/linux/init/do_mounts.c#L297
There the variable "fs_names" got one memory page with 4096 bytes.
Variable "p" walks through the existing file system types. The first
string is no problem.
But, with the second loop in mount_block_root the offset of "p" is not
at beginning of page, the offset is for example +9, if "reiserfs" is the
first in list.
Than calls do_mount_root, and lands in sys_mount.
Remember: Variable "type_page" contains now "fs_type+9" and not contains
a full page.
The sys_mount copies 4096 bytes with function "exact_copy_from_user()":
http://lxr.linux.no/linux/fs/namespace.c#L1540
Mostly exist pages after the buffer "fs_names+4096+9" and the page fault
handler was not called. No problem.
In the case, if the page after "fs_names+4096" is not mapped, the page
fault handler was called from http://lxr.linux.no/linux/fs/namespace.c#L1320
The do_page_fault gots an address 0xc03b4000.
It's kernel address, address >= TASK_SIZE, but not from vmalloc! It's
from "__getname()" alias "kmem_cache_alloc".
The "error_code" is 0. "vmalloc_fault" will be call:
http://lxr.linux.no/linux/arch/i386/mm/fault.c#L332
"vmalloc_fault" tryed to find the physical page for a non existing
virtual memory area. The macro "pte_present" in vmalloc_fault()
got a next page fault for 0xc0000ed0 at:
http://lxr.linux.no/linux/arch/i386/mm/fault.c#L282
No PTE exist for such virtual address. The page fault handler was trying
to sync the physical page for the PTE lockup.
This called vmalloc_fault() again for address 0xc000000, and that also
was not existing. The endless began...
In normal case the cpu would still loop with disabled interrrupts. Under
coLinux this was catched by a stack overflow inside printk debugs.
Signed-off-by: Henry Nestler <henry.nestler@gmail.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 13:44:39 +00:00
|
|
|
|
|
|
|
/* Make sure we are in vmalloc area */
|
|
|
|
if (!(address >= VMALLOC_START && address < VMALLOC_END))
|
|
|
|
return -1;
|
|
|
|
|
2008-01-30 12:33:13 +00:00
|
|
|
/*
|
|
|
|
* Synchronize this task's top level page-table
|
|
|
|
* with the 'reference' page table.
|
|
|
|
*
|
|
|
|
* Do _not_ use "current" here. We might be inside
|
|
|
|
* an interrupt in the middle of a task switch..
|
|
|
|
*/
|
|
|
|
pgd_paddr = read_cr3();
|
|
|
|
pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
|
|
|
|
if (!pmd_k)
|
|
|
|
return -1;
|
|
|
|
pte_k = pte_offset_kernel(pmd_k, address);
|
|
|
|
if (!pte_present(*pte_k))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
#else
|
2005-04-16 22:20:36 +00:00
|
|
|
pgd_t *pgd, *pgd_ref;
|
|
|
|
pud_t *pud, *pud_ref;
|
|
|
|
pmd_t *pmd, *pmd_ref;
|
|
|
|
pte_t *pte, *pte_ref;
|
|
|
|
|
2008-02-04 15:47:56 +00:00
|
|
|
/* Make sure we are in vmalloc area */
|
|
|
|
if (!(address >= VMALLOC_START && address < VMALLOC_END))
|
|
|
|
return -1;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Copy kernel mappings over when needed. This can also
|
|
|
|
happen within a race in page table update. In the later
|
|
|
|
case just flush. */
|
|
|
|
|
2009-01-09 20:17:43 +00:00
|
|
|
pgd = pgd_offset(current->active_mm, address);
|
2005-04-16 22:20:36 +00:00
|
|
|
pgd_ref = pgd_offset_k(address);
|
|
|
|
if (pgd_none(*pgd_ref))
|
|
|
|
return -1;
|
|
|
|
if (pgd_none(*pgd))
|
|
|
|
set_pgd(pgd, *pgd_ref);
|
2006-03-25 15:29:40 +00:00
|
|
|
else
|
2006-09-26 06:31:48 +00:00
|
|
|
BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Below here mismatches are bugs because these lower tables
|
|
|
|
are shared */
|
|
|
|
|
|
|
|
pud = pud_offset(pgd, address);
|
|
|
|
pud_ref = pud_offset(pgd_ref, address);
|
|
|
|
if (pud_none(*pud_ref))
|
|
|
|
return -1;
|
2006-09-26 06:31:48 +00:00
|
|
|
if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
|
2005-04-16 22:20:36 +00:00
|
|
|
BUG();
|
|
|
|
pmd = pmd_offset(pud, address);
|
|
|
|
pmd_ref = pmd_offset(pud_ref, address);
|
|
|
|
if (pmd_none(*pmd_ref))
|
|
|
|
return -1;
|
|
|
|
if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
|
|
|
|
BUG();
|
|
|
|
pte_ref = pte_offset_kernel(pmd_ref, address);
|
|
|
|
if (!pte_present(*pte_ref))
|
|
|
|
return -1;
|
|
|
|
pte = pte_offset_kernel(pmd, address);
|
2005-05-17 04:53:31 +00:00
|
|
|
/* Don't use pte_page here, because the mappings can point
|
|
|
|
outside mem_map, and the NUMA hash lookup cannot handle
|
|
|
|
that. */
|
|
|
|
if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
|
2005-04-16 22:20:36 +00:00
|
|
|
BUG();
|
|
|
|
return 0;
|
2008-01-30 12:33:13 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-22 09:12:28 +00:00
|
|
|
int show_unhandled_signals = 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
static inline int access_error(unsigned long error_code, int write,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
if (write) {
|
|
|
|
/* write, present and write, not present */
|
|
|
|
if (unlikely(!(vma->vm_flags & VM_WRITE)))
|
|
|
|
return 1;
|
|
|
|
} else if (unlikely(error_code & PF_PROT)) {
|
|
|
|
/* read, present */
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
/* read, not present */
|
|
|
|
if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* This routine handles page faults. It determines the address,
|
|
|
|
* and the problem, and then passes it off to one of the appropriate
|
|
|
|
* routines.
|
|
|
|
*/
|
2008-01-30 12:34:10 +00:00
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
asmlinkage
|
|
|
|
#endif
|
|
|
|
void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
unsigned long address;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct task_struct *tsk;
|
|
|
|
struct mm_struct *mm;
|
2008-01-30 12:32:19 +00:00
|
|
|
struct vm_area_struct *vma;
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
int write;
|
2008-01-30 12:34:10 +00:00
|
|
|
int fault;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-03-25 15:30:10 +00:00
|
|
|
tsk = current;
|
|
|
|
mm = tsk->mm;
|
|
|
|
prefetchw(&mm->mmap_sem);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* get the address */
|
2007-07-22 09:12:29 +00:00
|
|
|
address = read_cr2();
|
2005-04-16 22:20:36 +00:00
|
|
|
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
if (unlikely(notify_page_fault(regs)))
|
2008-01-30 12:33:12 +00:00
|
|
|
return;
|
x86: mmiotrace full patch, preview 1
kmmio.c handles the list of mmio probes with callbacks, list of traced
pages, and attaching into the page fault handler and die notifier. It
arms, traps and disarms the given pages, this is the core of mmiotrace.
mmio-mod.c is a user interface, hooking into ioremap functions and
registering the mmio probes. It also decodes the required information
from trapped mmio accesses via the pre and post callbacks in each probe.
Currently, hooking into ioremap functions works by redefining the symbols
of the target (binary) kernel module, so that it calls the traced
versions of the functions.
The most notable changes done since the last discussion are:
- kmmio.c is a built-in, not part of the module
- direct call from fault.c to kmmio.c, removing all dynamic hooks
- prepare for unregistering probes at any time
- make kmmio re-initializable and accessible to more than one user
- rewrite kmmio locking to remove all spinlocks from page fault path
Can I abuse call_rcu() like I do in kmmio.c:unregister_kmmio_probe()
or is there a better way?
The function called via call_rcu() itself calls call_rcu() again,
will this work or break? There I need a second grace period for RCU
after the first grace period for page faults.
Mmiotrace itself (mmio-mod.c) is still a module, I am going to attack
that next. At some point I will start looking into how to make mmiotrace
a tracer component of ftrace (thanks for the hint, Ingo). Ftrace should
make the user space part of mmiotracing as simple as
'cat /debug/trace/mmio > dump.txt'.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:57 +00:00
|
|
|
if (unlikely(kmmio_fault(regs, address)))
|
2008-05-12 19:20:56 +00:00
|
|
|
return;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We fault-in kernel-space virtual memory on-demand. The
|
|
|
|
* 'reference' page table is init_mm.pgd.
|
|
|
|
*
|
|
|
|
* NOTE! We MUST NOT take any locks for this case. We may
|
|
|
|
* be in an interrupt or a critical region, and should
|
|
|
|
* only copy the information from the master page table,
|
|
|
|
* nothing more.
|
|
|
|
*
|
|
|
|
* This verifies that the fault happens in kernel space
|
|
|
|
* (error_code & 4) == 0, and that the fault was not a
|
2006-01-11 21:42:23 +00:00
|
|
|
* protection error (error_code & 9) == 0.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-30 12:34:10 +00:00
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
if (unlikely(address >= TASK_SIZE)) {
|
2008-02-04 15:47:56 +00:00
|
|
|
#else
|
|
|
|
if (unlikely(address >= TASK_SIZE64)) {
|
|
|
|
#endif
|
2008-01-30 12:34:10 +00:00
|
|
|
if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
|
|
|
|
vmalloc_fault(address) >= 0)
|
|
|
|
return;
|
2008-01-30 12:34:11 +00:00
|
|
|
|
|
|
|
/* Can handle a stale RO->RW TLB */
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
if (spurious_fault(error_code, address))
|
2008-01-30 12:34:11 +00:00
|
|
|
return;
|
|
|
|
|
2008-01-30 12:34:10 +00:00
|
|
|
/*
|
|
|
|
* Don't take the mm semaphore here. If we fixup a prefetch
|
|
|
|
* fault we could otherwise deadlock.
|
|
|
|
*/
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
bad_area_nosemaphore(regs, error_code, address);
|
|
|
|
return;
|
2008-01-30 12:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-10-12 20:16:12 +00:00
|
|
|
* It's safe to allow irq's after cr2 has been saved and the
|
|
|
|
* vmalloc fault has been handled.
|
|
|
|
*
|
|
|
|
* User-mode registers count as a user access even for any
|
|
|
|
* potential system fault or CPU buglet.
|
2008-01-30 12:34:10 +00:00
|
|
|
*/
|
2008-10-12 20:16:12 +00:00
|
|
|
if (user_mode_vm(regs)) {
|
|
|
|
local_irq_enable();
|
|
|
|
error_code |= PF_USER;
|
|
|
|
} else if (regs->flags & X86_EFLAGS_IF)
|
2006-03-25 15:29:40 +00:00
|
|
|
local_irq_enable();
|
|
|
|
|
2008-10-12 20:16:12 +00:00
|
|
|
#ifdef CONFIG_X86_64
|
2006-01-11 21:44:09 +00:00
|
|
|
if (unlikely(error_code & PF_RSVD))
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
pgtable_bad(regs, error_code, address);
|
2008-10-12 20:16:12 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
2008-01-30 12:32:19 +00:00
|
|
|
* If we're in an interrupt, have no user context or are running in an
|
|
|
|
* atomic region then we must not take the fault.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
if (unlikely(in_atomic() || !mm)) {
|
|
|
|
bad_area_nosemaphore(regs, error_code, address);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-10-13 15:49:02 +00:00
|
|
|
/*
|
|
|
|
* When running in the kernel we expect faults to occur only to
|
2005-04-16 22:20:36 +00:00
|
|
|
* addresses in user space. All other faults represent errors in the
|
2007-10-19 23:25:36 +00:00
|
|
|
* kernel and should generate an OOPS. Unfortunately, in the case of an
|
2006-06-30 16:27:16 +00:00
|
|
|
* erroneous fault occurring in a code path which already holds mmap_sem
|
2005-04-16 22:20:36 +00:00
|
|
|
* we will deadlock attempting to validate the fault against the
|
|
|
|
* address space. Luckily the kernel only validly references user
|
|
|
|
* space from well defined areas of code, which are listed in the
|
|
|
|
* exceptions table.
|
|
|
|
*
|
|
|
|
* As the vast majority of faults will be valid we will only perform
|
2007-10-19 23:25:36 +00:00
|
|
|
* the source reference check when there is a possibility of a deadlock.
|
2005-04-16 22:20:36 +00:00
|
|
|
* Attempt to lock the address space, if we cannot we then validate the
|
|
|
|
* source. If this is invalid we can skip the address space check,
|
|
|
|
* thus avoiding the deadlock.
|
|
|
|
*/
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
|
2006-01-11 21:44:09 +00:00
|
|
|
if ((error_code & PF_USER) == 0 &&
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
!search_exception_tables(regs->ip)) {
|
|
|
|
bad_area_nosemaphore(regs, error_code, address);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
down_read(&mm->mmap_sem);
|
2009-01-29 15:02:12 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The above down_read_trylock() might have succeeded in which
|
|
|
|
* case we'll have missed the might_sleep() from down_read().
|
|
|
|
*/
|
|
|
|
might_sleep();
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vma = find_vma(mm, address);
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
if (unlikely(!vma)) {
|
|
|
|
bad_area(regs, error_code, address);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (likely(vma->vm_start <= address))
|
2005-04-16 22:20:36 +00:00
|
|
|
goto good_area;
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
|
|
|
|
bad_area(regs, error_code, address);
|
|
|
|
return;
|
|
|
|
}
|
2008-01-30 12:32:19 +00:00
|
|
|
if (error_code & PF_USER) {
|
2008-01-30 12:33:13 +00:00
|
|
|
/*
|
|
|
|
* Accessing the stack below %sp is always a bug.
|
|
|
|
* The large cushion allows instructions like enter
|
|
|
|
* and pusha to work. ("enter $65535,$31" pushes
|
|
|
|
* 32 pointers and then decrements %sp by 65535.)
|
2006-06-26 11:59:50 +00:00
|
|
|
*/
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
|
|
|
|
bad_area(regs, error_code, address);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
if (unlikely(expand_stack(vma, address))) {
|
|
|
|
bad_area(regs, error_code, address);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, we have a good vm_area for this memory access, so
|
|
|
|
* we can handle it..
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
good_area:
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
write = error_code & PF_WRITE;
|
|
|
|
if (unlikely(access_error(error_code, write, vma))) {
|
|
|
|
bad_area_access_error(regs, error_code, address);
|
|
|
|
return;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If for any reason at all we couldn't handle the fault,
|
|
|
|
* make sure we exit gracefully rather than endlessly redo
|
|
|
|
* the fault.
|
|
|
|
*/
|
2007-07-19 08:47:05 +00:00
|
|
|
fault = handle_mm_fault(mm, vma, address, write);
|
|
|
|
if (unlikely(fault & VM_FAULT_ERROR)) {
|
x86: optimise x86's do_page_fault (C entry point for the page fault path)
Impact: cleanup, restructure code to improve assembly
gcc isn't _all_ that smart about spilling registers to stack or reusing
stack slots, even with branch annotations. do_page_fault contained a lot
of functionality, so split unlikely paths into their own functions, and
mark them as noinline just to be sure. I consider this actually to be
somewhat of a cleanup too: the main function now contains about half
the number of lines so the normal path is easier to read, while the error
cases are also nicely split away.
Also, ensure the order of arguments to functions is always the same: regs,
addr, error_code. This can reduce code size a tiny bit, and just looks neater
too.
And add a couple of branch annotations.
Before:
do_page_fault:
subq $360, %rsp #,
After:
do_page_fault:
subq $56, %rsp #,
bloat-o-meter:
add/remove: 8/0 grow/shrink: 0/1 up/down: 2222/-1680 (542)
function old new delta
__bad_area_nosemaphore - 506 +506
no_context - 474 +474
vmalloc_fault - 424 +424
spurious_fault - 358 +358
mm_fault_error - 272 +272
bad_area_access_error - 89 +89
bad_area - 89 +89
bad_area_nosemaphore - 10 +10
do_page_fault 2464 784 -1680
Yes, the total size increases by 542 bytes, due to the extra function calls.
But these will very rarely be called (except for vmalloc_fault) in a normal
workload. Importantly, do_page_fault is less than 1/3rd it's original size,
and touches far less stack.
Existing gotos and branch hints did move a lot of the infrequently used text
out of the fastpath, but that's even further improved after this patch.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-01-20 03:24:26 +00:00
|
|
|
mm_fault_error(regs, error_code, address, fault);
|
|
|
|
return;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-07-19 08:47:05 +00:00
|
|
|
if (fault & VM_FAULT_MAJOR)
|
|
|
|
tsk->maj_flt++;
|
|
|
|
else
|
|
|
|
tsk->min_flt++;
|
2008-01-30 12:33:23 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
/*
|
|
|
|
* Did it hit the DOS screen memory VA from vm86 mode?
|
|
|
|
*/
|
|
|
|
if (v8086_mode(regs)) {
|
|
|
|
unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
|
|
|
|
if (bit < 32)
|
|
|
|
tsk->thread.screen_bitmap |= 1 << bit;
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
up_read(&mm->mmap_sem);
|
|
|
|
}
|
2005-11-05 16:25:54 +00:00
|
|
|
|
2006-03-25 15:29:40 +00:00
|
|
|
DEFINE_SPINLOCK(pgd_lock);
|
2007-05-02 17:27:10 +00:00
|
|
|
LIST_HEAD(pgd_list);
|
2006-03-25 15:29:40 +00:00
|
|
|
|
|
|
|
void vmalloc_sync_all(void)
|
|
|
|
{
|
2008-01-30 12:34:10 +00:00
|
|
|
unsigned long address;
|
|
|
|
|
2008-08-29 11:53:45 +00:00
|
|
|
#ifdef CONFIG_X86_32
|
2008-01-30 12:34:10 +00:00
|
|
|
if (SHARED_KERNEL_PMD)
|
|
|
|
return;
|
|
|
|
|
2008-08-29 11:53:45 +00:00
|
|
|
for (address = VMALLOC_START & PMD_MASK;
|
|
|
|
address >= TASK_SIZE && address < FIXADDR_TOP;
|
|
|
|
address += PMD_SIZE) {
|
2008-06-25 04:19:11 +00:00
|
|
|
unsigned long flags;
|
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&pgd_lock, flags);
|
|
|
|
list_for_each_entry(page, &pgd_list, lru) {
|
|
|
|
if (!vmalloc_sync_one(page_address(page),
|
|
|
|
address))
|
|
|
|
break;
|
2008-01-30 12:34:10 +00:00
|
|
|
}
|
2008-06-25 04:19:11 +00:00
|
|
|
spin_unlock_irqrestore(&pgd_lock, flags);
|
2008-01-30 12:34:10 +00:00
|
|
|
}
|
|
|
|
#else /* CONFIG_X86_64 */
|
2008-08-29 11:53:45 +00:00
|
|
|
for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
|
|
|
|
address += PGDIR_SIZE) {
|
2008-06-25 04:19:11 +00:00
|
|
|
const pgd_t *pgd_ref = pgd_offset_k(address);
|
|
|
|
unsigned long flags;
|
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
if (pgd_none(*pgd_ref))
|
|
|
|
continue;
|
|
|
|
spin_lock_irqsave(&pgd_lock, flags);
|
|
|
|
list_for_each_entry(page, &pgd_list, lru) {
|
|
|
|
pgd_t *pgd;
|
|
|
|
pgd = (pgd_t *)page_address(page) + pgd_index(address);
|
|
|
|
if (pgd_none(*pgd))
|
|
|
|
set_pgd(pgd, *pgd_ref);
|
|
|
|
else
|
|
|
|
BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
|
2006-03-25 15:29:40 +00:00
|
|
|
}
|
2008-06-25 04:19:11 +00:00
|
|
|
spin_unlock_irqrestore(&pgd_lock, flags);
|
2006-03-25 15:29:40 +00:00
|
|
|
}
|
2008-01-30 12:34:10 +00:00
|
|
|
#endif
|
2006-03-25 15:29:40 +00:00
|
|
|
}
|