ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
/*
|
|
|
|
* Code for replacing ftrace calls with jumps.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
|
|
|
|
*
|
|
|
|
* Thanks goes to Ingo Molnar, for suggesting the idea.
|
|
|
|
* Mathieu Desnoyers, for suggesting postponing the modifications.
|
|
|
|
* Arjan van de Ven, for keeping me straight, and explaining to me
|
|
|
|
* the dangers of modifying code on the run.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/hardirq.h>
|
2008-08-20 16:55:07 +00:00
|
|
|
#include <linux/uaccess.h>
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
#include <linux/ftrace.h>
|
|
|
|
#include <linux/percpu.h>
|
2008-11-11 10:57:02 +00:00
|
|
|
#include <linux/sched.h>
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
|
2008-06-21 18:17:27 +00:00
|
|
|
#include <asm/ftrace.h>
|
2008-11-11 06:03:45 +00:00
|
|
|
#include <linux/ftrace.h>
|
ftrace: use only 5 byte nops for x86
Mathieu Desnoyers revealed a bug in the original code. The nop that is
used to relpace the mcount caller can be a two part nop. This runs the
risk where a process can be preempted after executing the first nop, but
before the second part of the nop.
The ftrace code calls kstop_machine to keep multiple CPUs from executing
code that is being modified, but it does not protect against a task preempting
in the middle of a two part nop.
If the above preemption happens and the tracer is enabled, after the
kstop_machine runs, all those nops will be calls to the trace function.
If the preempted process that was preempted between the two nops is executed
again, it will execute half of the call to the trace function, and this
might crash the system.
This patch instead uses what both the latest Intel and AMD spec suggests.
That is the P6_NOP5 sequence of "0x0f 0x1f 0x44 0x00 0x00".
Note, some older CPUs and QEMU might fault on this nop, so this nop
is executed with fault handling first. If it detects a fault, it will then
use the code "0x66 0x66 0x66 0x66 0x90". If that faults, it will then
default to a simple "jmp 1f; .byte 0x00 0x00 0x00; 1:". The jmp is
not optimal but will do if the first two can not be executed.
TODO: Examine the cpuid to determine the nop to use.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-08-14 22:05:05 +00:00
|
|
|
#include <asm/nops.h>
|
2008-11-11 06:03:45 +00:00
|
|
|
#include <asm/nmi.h>
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
|
|
|
|
|
2008-11-11 06:03:45 +00:00
|
|
|
#ifdef CONFIG_DYNAMIC_FTRACE
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
|
|
|
|
union ftrace_code_union {
|
2008-06-21 18:17:27 +00:00
|
|
|
char code[MCOUNT_INSN_SIZE];
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
struct {
|
|
|
|
char e8;
|
|
|
|
int offset;
|
|
|
|
} __attribute__((packed));
|
|
|
|
};
|
|
|
|
|
2008-10-23 13:33:08 +00:00
|
|
|
static int ftrace_calc_offset(long ip, long addr)
|
2008-05-12 19:20:43 +00:00
|
|
|
{
|
|
|
|
return (int)(addr - ip);
|
|
|
|
}
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
|
2008-11-15 00:21:19 +00:00
|
|
|
static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
|
2008-05-12 19:20:43 +00:00
|
|
|
{
|
|
|
|
static union ftrace_code_union calc;
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
|
2008-05-12 19:20:43 +00:00
|
|
|
calc.e8 = 0xe8;
|
2008-06-21 18:17:27 +00:00
|
|
|
calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
|
2008-05-12 19:20:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No locking needed, this must be called via kstop_machine
|
|
|
|
* which in essence is like running on a uniprocessor machine.
|
|
|
|
*/
|
|
|
|
return calc.code;
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
}
|
|
|
|
|
2008-10-30 20:08:32 +00:00
|
|
|
/*
|
|
|
|
* Modifying code must take extra care. On an SMP machine, if
|
|
|
|
* the code being modified is also being executed on another CPU
|
|
|
|
* that CPU will have undefined results and possibly take a GPF.
|
|
|
|
* We use kstop_machine to stop other CPUS from exectuing code.
|
|
|
|
* But this does not stop NMIs from happening. We still need
|
|
|
|
* to protect against that. We separate out the modification of
|
|
|
|
* the code to take care of this.
|
|
|
|
*
|
|
|
|
* Two buffers are added: An IP buffer and a "code" buffer.
|
|
|
|
*
|
2008-10-31 04:03:22 +00:00
|
|
|
* 1) Put the instruction pointer into the IP buffer
|
2008-10-30 20:08:32 +00:00
|
|
|
* and the new code into the "code" buffer.
|
|
|
|
* 2) Set a flag that says we are modifying code
|
|
|
|
* 3) Wait for any running NMIs to finish.
|
|
|
|
* 4) Write the code
|
|
|
|
* 5) clear the flag.
|
|
|
|
* 6) Wait for any running NMIs to finish.
|
|
|
|
*
|
|
|
|
* If an NMI is executed, the first thing it does is to call
|
|
|
|
* "ftrace_nmi_enter". This will check if the flag is set to write
|
|
|
|
* and if it is, it will write what is in the IP and "code" buffers.
|
|
|
|
*
|
|
|
|
* The trick is, it does not matter if everyone is writing the same
|
|
|
|
* content to the code location. Also, if a CPU is executing code
|
|
|
|
* it is OK to write to that code location if the contents being written
|
|
|
|
* are the same as what exists.
|
|
|
|
*/
|
|
|
|
|
2009-02-06 03:30:07 +00:00
|
|
|
static atomic_t nmi_running = ATOMIC_INIT(0);
|
2008-10-31 04:03:22 +00:00
|
|
|
static int mod_code_status; /* holds return value of text write */
|
|
|
|
static int mod_code_write; /* set when NMI should do the write */
|
|
|
|
static void *mod_code_ip; /* holds the IP to write to */
|
|
|
|
static void *mod_code_newcode; /* holds the text to write to the IP */
|
2008-10-30 20:08:32 +00:00
|
|
|
|
2008-10-31 04:03:22 +00:00
|
|
|
static unsigned nmi_wait_count;
|
|
|
|
static atomic_t nmi_update_count = ATOMIC_INIT(0);
|
2008-10-30 20:08:33 +00:00
|
|
|
|
|
|
|
int ftrace_arch_read_dyn_info(char *buf, int size)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = snprintf(buf, size, "%u %u",
|
|
|
|
nmi_wait_count,
|
|
|
|
atomic_read(&nmi_update_count));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-10-30 20:08:32 +00:00
|
|
|
static void ftrace_mod_code(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Yes, more than one CPU process can be writing to mod_code_status.
|
|
|
|
* (and the code itself)
|
|
|
|
* But if one were to fail, then they all should, and if one were
|
|
|
|
* to succeed, then they all should.
|
|
|
|
*/
|
|
|
|
mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
|
|
|
|
MCOUNT_INSN_SIZE);
|
|
|
|
}
|
|
|
|
|
2009-02-06 06:45:16 +00:00
|
|
|
void ftrace_nmi_enter(void)
|
2008-10-30 20:08:32 +00:00
|
|
|
{
|
2009-02-06 03:30:07 +00:00
|
|
|
atomic_inc(&nmi_running);
|
|
|
|
/* Must have nmi_running seen before reading write flag */
|
2008-10-30 20:08:32 +00:00
|
|
|
smp_mb();
|
2008-10-30 20:08:33 +00:00
|
|
|
if (mod_code_write) {
|
2008-10-30 20:08:32 +00:00
|
|
|
ftrace_mod_code();
|
2008-10-30 20:08:33 +00:00
|
|
|
atomic_inc(&nmi_update_count);
|
|
|
|
}
|
2008-10-30 20:08:32 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 06:45:16 +00:00
|
|
|
void ftrace_nmi_exit(void)
|
2008-10-30 20:08:32 +00:00
|
|
|
{
|
2009-02-06 03:30:07 +00:00
|
|
|
/* Finish all executions before clearing nmi_running */
|
2008-10-30 20:08:32 +00:00
|
|
|
smp_wmb();
|
2009-02-06 03:30:07 +00:00
|
|
|
atomic_dec(&nmi_running);
|
2008-10-30 20:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wait_for_nmi(void)
|
|
|
|
{
|
2009-02-06 03:30:07 +00:00
|
|
|
if (!atomic_read(&nmi_running))
|
2009-01-26 15:28:02 +00:00
|
|
|
return;
|
2008-10-30 20:08:33 +00:00
|
|
|
|
2009-01-26 15:28:02 +00:00
|
|
|
do {
|
2008-10-30 20:08:32 +00:00
|
|
|
cpu_relax();
|
2009-02-06 03:30:07 +00:00
|
|
|
} while (atomic_read(&nmi_running));
|
2008-10-30 20:08:33 +00:00
|
|
|
|
2009-01-26 15:28:02 +00:00
|
|
|
nmi_wait_count++;
|
2008-10-30 20:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_ftrace_mod_code(unsigned long ip, void *new_code)
|
|
|
|
{
|
|
|
|
mod_code_ip = (void *)ip;
|
|
|
|
mod_code_newcode = new_code;
|
|
|
|
|
|
|
|
/* The buffers need to be visible before we let NMIs write them */
|
|
|
|
smp_wmb();
|
|
|
|
|
|
|
|
mod_code_write = 1;
|
|
|
|
|
|
|
|
/* Make sure write bit is visible before we wait on NMIs */
|
|
|
|
smp_mb();
|
|
|
|
|
|
|
|
wait_for_nmi();
|
|
|
|
|
|
|
|
/* Make sure all running NMIs have finished before we write the code */
|
|
|
|
smp_mb();
|
|
|
|
|
|
|
|
ftrace_mod_code();
|
|
|
|
|
|
|
|
/* Make sure the write happens before clearing the bit */
|
|
|
|
smp_wmb();
|
|
|
|
|
|
|
|
mod_code_write = 0;
|
|
|
|
|
|
|
|
/* make sure NMIs see the cleared bit */
|
|
|
|
smp_mb();
|
|
|
|
|
|
|
|
wait_for_nmi();
|
|
|
|
|
|
|
|
return mod_code_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-11 06:03:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
|
|
|
|
|
2008-11-15 00:21:19 +00:00
|
|
|
static unsigned char *ftrace_nop_replace(void)
|
2008-11-11 06:03:45 +00:00
|
|
|
{
|
|
|
|
return ftrace_nop;
|
|
|
|
}
|
|
|
|
|
2008-11-15 00:21:19 +00:00
|
|
|
static int
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
ftrace_modify_code(unsigned long ip, unsigned char *old_code,
|
|
|
|
unsigned char *new_code)
|
|
|
|
{
|
2008-08-20 16:55:07 +00:00
|
|
|
unsigned char replaced[MCOUNT_INSN_SIZE];
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: Due to modules and __init, code can
|
|
|
|
* disappear and change, we need to protect against faulting
|
2008-10-23 13:33:00 +00:00
|
|
|
* as well as code changing. We do this by using the
|
2008-10-23 13:33:01 +00:00
|
|
|
* probe_kernel_* functions.
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
*
|
|
|
|
* No real locking needed, this code is run through
|
2008-08-20 16:55:07 +00:00
|
|
|
* kstop_machine, or before SMP starts.
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
*/
|
2008-10-23 13:33:00 +00:00
|
|
|
|
|
|
|
/* read the text we want to modify */
|
2008-10-23 13:33:01 +00:00
|
|
|
if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
|
2008-10-23 13:32:59 +00:00
|
|
|
return -EFAULT;
|
2008-08-20 16:55:07 +00:00
|
|
|
|
2008-10-23 13:33:00 +00:00
|
|
|
/* Make sure it is what we expect it to be */
|
2008-08-20 16:55:07 +00:00
|
|
|
if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
|
2008-10-23 13:32:59 +00:00
|
|
|
return -EINVAL;
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
|
2008-10-23 13:33:00 +00:00
|
|
|
/* replace the text with the new text */
|
2008-10-30 20:08:32 +00:00
|
|
|
if (do_ftrace_mod_code(ip, new_code))
|
2008-10-23 13:32:59 +00:00
|
|
|
return -EPERM;
|
2008-08-20 16:55:07 +00:00
|
|
|
|
|
|
|
sync_core();
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
|
2008-08-20 16:55:07 +00:00
|
|
|
return 0;
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
}
|
|
|
|
|
2008-11-15 00:21:19 +00:00
|
|
|
int ftrace_make_nop(struct module *mod,
|
|
|
|
struct dyn_ftrace *rec, unsigned long addr)
|
|
|
|
{
|
|
|
|
unsigned char *new, *old;
|
|
|
|
unsigned long ip = rec->ip;
|
|
|
|
|
|
|
|
old = ftrace_call_replace(ip, addr);
|
|
|
|
new = ftrace_nop_replace();
|
|
|
|
|
|
|
|
return ftrace_modify_code(rec->ip, old, new);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
|
|
|
|
{
|
|
|
|
unsigned char *new, *old;
|
|
|
|
unsigned long ip = rec->ip;
|
|
|
|
|
|
|
|
old = ftrace_nop_replace();
|
|
|
|
new = ftrace_call_replace(ip, addr);
|
|
|
|
|
|
|
|
return ftrace_modify_code(rec->ip, old, new);
|
|
|
|
}
|
|
|
|
|
2008-10-23 13:33:08 +00:00
|
|
|
int ftrace_update_ftrace_func(ftrace_func_t func)
|
2008-05-12 19:20:43 +00:00
|
|
|
{
|
|
|
|
unsigned long ip = (unsigned long)(&ftrace_call);
|
2008-06-21 18:17:27 +00:00
|
|
|
unsigned char old[MCOUNT_INSN_SIZE], *new;
|
2008-05-12 19:20:43 +00:00
|
|
|
int ret;
|
|
|
|
|
2008-06-21 18:17:27 +00:00
|
|
|
memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
|
2008-05-12 19:20:43 +00:00
|
|
|
new = ftrace_call_replace(ip, (unsigned long)func);
|
|
|
|
ret = ftrace_modify_code(ip, old, new);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __init ftrace_dyn_arch_init(void *data)
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
{
|
ftrace: use only 5 byte nops for x86
Mathieu Desnoyers revealed a bug in the original code. The nop that is
used to relpace the mcount caller can be a two part nop. This runs the
risk where a process can be preempted after executing the first nop, but
before the second part of the nop.
The ftrace code calls kstop_machine to keep multiple CPUs from executing
code that is being modified, but it does not protect against a task preempting
in the middle of a two part nop.
If the above preemption happens and the tracer is enabled, after the
kstop_machine runs, all those nops will be calls to the trace function.
If the preempted process that was preempted between the two nops is executed
again, it will execute half of the call to the trace function, and this
might crash the system.
This patch instead uses what both the latest Intel and AMD spec suggests.
That is the P6_NOP5 sequence of "0x0f 0x1f 0x44 0x00 0x00".
Note, some older CPUs and QEMU might fault on this nop, so this nop
is executed with fault handling first. If it detects a fault, it will then
use the code "0x66 0x66 0x66 0x66 0x90". If that faults, it will then
default to a simple "jmp 1f; .byte 0x00 0x00 0x00; 1:". The jmp is
not optimal but will do if the first two can not be executed.
TODO: Examine the cpuid to determine the nop to use.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-08-14 22:05:05 +00:00
|
|
|
extern const unsigned char ftrace_test_p6nop[];
|
|
|
|
extern const unsigned char ftrace_test_nop5[];
|
|
|
|
extern const unsigned char ftrace_test_jmp[];
|
|
|
|
int faulted = 0;
|
2008-05-12 19:20:43 +00:00
|
|
|
|
ftrace: use only 5 byte nops for x86
Mathieu Desnoyers revealed a bug in the original code. The nop that is
used to relpace the mcount caller can be a two part nop. This runs the
risk where a process can be preempted after executing the first nop, but
before the second part of the nop.
The ftrace code calls kstop_machine to keep multiple CPUs from executing
code that is being modified, but it does not protect against a task preempting
in the middle of a two part nop.
If the above preemption happens and the tracer is enabled, after the
kstop_machine runs, all those nops will be calls to the trace function.
If the preempted process that was preempted between the two nops is executed
again, it will execute half of the call to the trace function, and this
might crash the system.
This patch instead uses what both the latest Intel and AMD spec suggests.
That is the P6_NOP5 sequence of "0x0f 0x1f 0x44 0x00 0x00".
Note, some older CPUs and QEMU might fault on this nop, so this nop
is executed with fault handling first. If it detects a fault, it will then
use the code "0x66 0x66 0x66 0x66 0x90". If that faults, it will then
default to a simple "jmp 1f; .byte 0x00 0x00 0x00; 1:". The jmp is
not optimal but will do if the first two can not be executed.
TODO: Examine the cpuid to determine the nop to use.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-08-14 22:05:05 +00:00
|
|
|
/*
|
|
|
|
* There is no good nop for all x86 archs.
|
|
|
|
* We will default to using the P6_NOP5, but first we
|
|
|
|
* will test to make sure that the nop will actually
|
|
|
|
* work on this CPU. If it faults, we will then
|
|
|
|
* go to a lesser efficient 5 byte nop. If that fails
|
|
|
|
* we then just use a jmp as our nop. This isn't the most
|
|
|
|
* efficient nop, but we can not use a multi part nop
|
|
|
|
* since we would then risk being preempted in the middle
|
|
|
|
* of that nop, and if we enabled tracing then, it might
|
|
|
|
* cause a system crash.
|
|
|
|
*
|
|
|
|
* TODO: check the cpuid to determine the best nop.
|
|
|
|
*/
|
|
|
|
asm volatile (
|
|
|
|
"ftrace_test_jmp:"
|
|
|
|
"jmp ftrace_test_p6nop\n"
|
2008-10-10 02:19:08 +00:00
|
|
|
"nop\n"
|
|
|
|
"nop\n"
|
|
|
|
"nop\n" /* 2 byte jmp + 3 bytes */
|
ftrace: use only 5 byte nops for x86
Mathieu Desnoyers revealed a bug in the original code. The nop that is
used to relpace the mcount caller can be a two part nop. This runs the
risk where a process can be preempted after executing the first nop, but
before the second part of the nop.
The ftrace code calls kstop_machine to keep multiple CPUs from executing
code that is being modified, but it does not protect against a task preempting
in the middle of a two part nop.
If the above preemption happens and the tracer is enabled, after the
kstop_machine runs, all those nops will be calls to the trace function.
If the preempted process that was preempted between the two nops is executed
again, it will execute half of the call to the trace function, and this
might crash the system.
This patch instead uses what both the latest Intel and AMD spec suggests.
That is the P6_NOP5 sequence of "0x0f 0x1f 0x44 0x00 0x00".
Note, some older CPUs and QEMU might fault on this nop, so this nop
is executed with fault handling first. If it detects a fault, it will then
use the code "0x66 0x66 0x66 0x66 0x90". If that faults, it will then
default to a simple "jmp 1f; .byte 0x00 0x00 0x00; 1:". The jmp is
not optimal but will do if the first two can not be executed.
TODO: Examine the cpuid to determine the nop to use.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-08-14 22:05:05 +00:00
|
|
|
"ftrace_test_p6nop:"
|
|
|
|
P6_NOP5
|
|
|
|
"jmp 1f\n"
|
|
|
|
"ftrace_test_nop5:"
|
|
|
|
".byte 0x66,0x66,0x66,0x66,0x90\n"
|
|
|
|
"1:"
|
|
|
|
".section .fixup, \"ax\"\n"
|
|
|
|
"2: movl $1, %0\n"
|
|
|
|
" jmp ftrace_test_nop5\n"
|
|
|
|
"3: movl $2, %0\n"
|
|
|
|
" jmp 1b\n"
|
|
|
|
".previous\n"
|
|
|
|
_ASM_EXTABLE(ftrace_test_p6nop, 2b)
|
|
|
|
_ASM_EXTABLE(ftrace_test_nop5, 3b)
|
|
|
|
: "=r"(faulted) : "0" (faulted));
|
|
|
|
|
|
|
|
switch (faulted) {
|
|
|
|
case 0:
|
|
|
|
pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
|
ftrace: use a real variable for ftrace_nop in x86
Impact: avoid section mismatch warning, clean up
The dynamic ftrace determines which nop is safe to use at start up.
When it finds a safe nop for patching, it sets a pointer called ftrace_nop
to point to the code. All call sites are then patched to this nop.
Later, when tracing is turned on, this ftrace_nop variable is again used
to compare the location to make sure it is a nop before we update it to
an mcount call. If this fails just once, a warning is printed and ftrace
is disabled.
Rakib Mullick noted that the code that sets up the nop is a .init section
where as the nop itself is in the .text section. This is needed because
the nop is used later on after boot up. The problem is that the test of the
nop jumps back to the setup code and causes a "section mismatch" warning.
Rakib first recommended to convert the nop to .init.text, but as stated
above, this would fail since that text is used later.
The real solution is to extend Rabik's patch, and to make the ftrace_nop
into an array, and just save the code from the assembly to this array.
Now the section can stay as an init section, and we have a nop to use
later on.
Reported-by: Rakib Mullick <rakib.mullick@gmail.com>
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-10-24 13:12:17 +00:00
|
|
|
memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
|
ftrace: use only 5 byte nops for x86
Mathieu Desnoyers revealed a bug in the original code. The nop that is
used to relpace the mcount caller can be a two part nop. This runs the
risk where a process can be preempted after executing the first nop, but
before the second part of the nop.
The ftrace code calls kstop_machine to keep multiple CPUs from executing
code that is being modified, but it does not protect against a task preempting
in the middle of a two part nop.
If the above preemption happens and the tracer is enabled, after the
kstop_machine runs, all those nops will be calls to the trace function.
If the preempted process that was preempted between the two nops is executed
again, it will execute half of the call to the trace function, and this
might crash the system.
This patch instead uses what both the latest Intel and AMD spec suggests.
That is the P6_NOP5 sequence of "0x0f 0x1f 0x44 0x00 0x00".
Note, some older CPUs and QEMU might fault on this nop, so this nop
is executed with fault handling first. If it detects a fault, it will then
use the code "0x66 0x66 0x66 0x66 0x90". If that faults, it will then
default to a simple "jmp 1f; .byte 0x00 0x00 0x00; 1:". The jmp is
not optimal but will do if the first two can not be executed.
TODO: Examine the cpuid to determine the nop to use.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-08-14 22:05:05 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
|
ftrace: use a real variable for ftrace_nop in x86
Impact: avoid section mismatch warning, clean up
The dynamic ftrace determines which nop is safe to use at start up.
When it finds a safe nop for patching, it sets a pointer called ftrace_nop
to point to the code. All call sites are then patched to this nop.
Later, when tracing is turned on, this ftrace_nop variable is again used
to compare the location to make sure it is a nop before we update it to
an mcount call. If this fails just once, a warning is printed and ftrace
is disabled.
Rakib Mullick noted that the code that sets up the nop is a .init section
where as the nop itself is in the .text section. This is needed because
the nop is used later on after boot up. The problem is that the test of the
nop jumps back to the setup code and causes a "section mismatch" warning.
Rakib first recommended to convert the nop to .init.text, but as stated
above, this would fail since that text is used later.
The real solution is to extend Rabik's patch, and to make the ftrace_nop
into an array, and just save the code from the assembly to this array.
Now the section can stay as an init section, and we have a nop to use
later on.
Reported-by: Rakib Mullick <rakib.mullick@gmail.com>
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-10-24 13:12:17 +00:00
|
|
|
memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
|
ftrace: use only 5 byte nops for x86
Mathieu Desnoyers revealed a bug in the original code. The nop that is
used to relpace the mcount caller can be a two part nop. This runs the
risk where a process can be preempted after executing the first nop, but
before the second part of the nop.
The ftrace code calls kstop_machine to keep multiple CPUs from executing
code that is being modified, but it does not protect against a task preempting
in the middle of a two part nop.
If the above preemption happens and the tracer is enabled, after the
kstop_machine runs, all those nops will be calls to the trace function.
If the preempted process that was preempted between the two nops is executed
again, it will execute half of the call to the trace function, and this
might crash the system.
This patch instead uses what both the latest Intel and AMD spec suggests.
That is the P6_NOP5 sequence of "0x0f 0x1f 0x44 0x00 0x00".
Note, some older CPUs and QEMU might fault on this nop, so this nop
is executed with fault handling first. If it detects a fault, it will then
use the code "0x66 0x66 0x66 0x66 0x90". If that faults, it will then
default to a simple "jmp 1f; .byte 0x00 0x00 0x00; 1:". The jmp is
not optimal but will do if the first two can not be executed.
TODO: Examine the cpuid to determine the nop to use.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-08-14 22:05:05 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2008-10-10 02:19:08 +00:00
|
|
|
pr_info("ftrace: converting mcount calls to jmp . + 5\n");
|
ftrace: use a real variable for ftrace_nop in x86
Impact: avoid section mismatch warning, clean up
The dynamic ftrace determines which nop is safe to use at start up.
When it finds a safe nop for patching, it sets a pointer called ftrace_nop
to point to the code. All call sites are then patched to this nop.
Later, when tracing is turned on, this ftrace_nop variable is again used
to compare the location to make sure it is a nop before we update it to
an mcount call. If this fails just once, a warning is printed and ftrace
is disabled.
Rakib Mullick noted that the code that sets up the nop is a .init section
where as the nop itself is in the .text section. This is needed because
the nop is used later on after boot up. The problem is that the test of the
nop jumps back to the setup code and causes a "section mismatch" warning.
Rakib first recommended to convert the nop to .init.text, but as stated
above, this would fail since that text is used later.
The real solution is to extend Rabik's patch, and to make the ftrace_nop
into an array, and just save the code from the assembly to this array.
Now the section can stay as an init section, and we have a nop to use
later on.
Reported-by: Rakib Mullick <rakib.mullick@gmail.com>
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-10-24 13:12:17 +00:00
|
|
|
memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
|
ftrace: use only 5 byte nops for x86
Mathieu Desnoyers revealed a bug in the original code. The nop that is
used to relpace the mcount caller can be a two part nop. This runs the
risk where a process can be preempted after executing the first nop, but
before the second part of the nop.
The ftrace code calls kstop_machine to keep multiple CPUs from executing
code that is being modified, but it does not protect against a task preempting
in the middle of a two part nop.
If the above preemption happens and the tracer is enabled, after the
kstop_machine runs, all those nops will be calls to the trace function.
If the preempted process that was preempted between the two nops is executed
again, it will execute half of the call to the trace function, and this
might crash the system.
This patch instead uses what both the latest Intel and AMD spec suggests.
That is the P6_NOP5 sequence of "0x0f 0x1f 0x44 0x00 0x00".
Note, some older CPUs and QEMU might fault on this nop, so this nop
is executed with fault handling first. If it detects a fault, it will then
use the code "0x66 0x66 0x66 0x66 0x90". If that faults, it will then
default to a simple "jmp 1f; .byte 0x00 0x00 0x00; 1:". The jmp is
not optimal but will do if the first two can not be executed.
TODO: Examine the cpuid to determine the nop to use.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-08-14 22:05:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The return code is retured via data */
|
|
|
|
*(unsigned long *)data = 0;
|
2008-05-12 19:20:43 +00:00
|
|
|
|
ftrace: dynamic enabling/disabling of function calls
This patch adds a feature to dynamically replace the ftrace code
with the jmps to allow a kernel with ftrace configured to run
as fast as it can without it configured.
The way this works, is on bootup (if ftrace is enabled), a ftrace
function is registered to record the instruction pointer of all
places that call the function.
Later, if there's still any code to patch, a kthread is awoken
(rate limited to at most once a second) that performs a stop_machine,
and replaces all the code that was called with a jmp over the call
to ftrace. It only replaces what was found the previous time. Typically
the system reaches equilibrium quickly after bootup and there's no code
patching needed at all.
e.g.
call ftrace /* 5 bytes */
is replaced with
jmp 3f /* jmp is 2 bytes and we jump 3 forward */
3:
When we want to enable ftrace for function tracing, the IP recording
is removed, and stop_machine is called again to replace all the locations
of that were recorded back to the call of ftrace. When it is disabled,
we replace the code back to the jmp.
Allocation is done by the kthread. If the ftrace recording function is
called, and we don't have any record slots available, then we simply
skip that call. Once a second a new page (if needed) is allocated for
recording new ftrace function calls. A large batch is allocated at
boot up to get most of the calls there.
Because we do this via stop_machine, we don't have to worry about another
CPU executing a ftrace call as we modify it. But we do need to worry
about NMI's so all functions that might be called via nmi must be
annotated with notrace_nmi. When this code is configured in, the NMI code
will not call notrace.
Signed-off-by: Steven Rostedt <srostedt@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2008-05-12 19:20:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-11-11 06:03:45 +00:00
|
|
|
#endif
|
2008-11-16 05:02:06 +00:00
|
|
|
|
2008-11-25 20:07:04 +00:00
|
|
|
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
|
2008-11-16 05:02:06 +00:00
|
|
|
|
2008-11-26 05:16:24 +00:00
|
|
|
#ifdef CONFIG_DYNAMIC_FTRACE
|
|
|
|
extern void ftrace_graph_call(void);
|
|
|
|
|
|
|
|
static int ftrace_mod_jmp(unsigned long ip,
|
|
|
|
int old_offset, int new_offset)
|
|
|
|
{
|
|
|
|
unsigned char code[MCOUNT_INSN_SIZE];
|
|
|
|
|
|
|
|
if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*(int *)(&code[1]) = new_offset;
|
|
|
|
|
|
|
|
if (do_ftrace_mod_code(ip, &code))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ftrace_enable_ftrace_graph_caller(void)
|
|
|
|
{
|
|
|
|
unsigned long ip = (unsigned long)(&ftrace_graph_call);
|
|
|
|
int old_offset, new_offset;
|
|
|
|
|
|
|
|
old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
|
|
|
|
new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
|
|
|
|
|
|
|
|
return ftrace_mod_jmp(ip, old_offset, new_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ftrace_disable_ftrace_graph_caller(void)
|
|
|
|
{
|
|
|
|
unsigned long ip = (unsigned long)(&ftrace_graph_call);
|
|
|
|
int old_offset, new_offset;
|
|
|
|
|
|
|
|
old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
|
|
|
|
new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
|
|
|
|
|
|
|
|
return ftrace_mod_jmp(ip, old_offset, new_offset);
|
|
|
|
}
|
|
|
|
|
2008-11-16 05:02:06 +00:00
|
|
|
#endif /* !CONFIG_DYNAMIC_FTRACE */
|
|
|
|
|
|
|
|
/* Add a function return address to the trace stack on thread info.*/
|
|
|
|
static int push_return_trace(unsigned long ret, unsigned long long time,
|
2008-11-25 23:57:25 +00:00
|
|
|
unsigned long func, int *depth)
|
2008-11-16 05:02:06 +00:00
|
|
|
{
|
|
|
|
int index;
|
2008-11-23 05:22:56 +00:00
|
|
|
|
|
|
|
if (!current->ret_stack)
|
|
|
|
return -EBUSY;
|
2008-11-16 05:02:06 +00:00
|
|
|
|
|
|
|
/* The return trace stack is full */
|
2008-11-23 05:22:56 +00:00
|
|
|
if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
|
|
|
|
atomic_inc(¤t->trace_overrun);
|
2008-11-16 05:02:06 +00:00
|
|
|
return -EBUSY;
|
2008-11-17 02:22:41 +00:00
|
|
|
}
|
2008-11-16 05:02:06 +00:00
|
|
|
|
2008-11-23 05:22:56 +00:00
|
|
|
index = ++current->curr_ret_stack;
|
2008-11-16 05:02:06 +00:00
|
|
|
barrier();
|
2008-11-23 05:22:56 +00:00
|
|
|
current->ret_stack[index].ret = ret;
|
|
|
|
current->ret_stack[index].func = func;
|
|
|
|
current->ret_stack[index].calltime = time;
|
2008-11-25 23:57:25 +00:00
|
|
|
*depth = index;
|
2008-11-16 05:02:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieve a function return address to the trace stack on thread info.*/
|
2008-11-25 23:57:25 +00:00
|
|
|
static void pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret)
|
2008-11-16 05:02:06 +00:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
2008-11-23 05:22:56 +00:00
|
|
|
index = current->curr_ret_stack;
|
2008-12-03 04:50:06 +00:00
|
|
|
|
|
|
|
if (unlikely(index < 0)) {
|
|
|
|
ftrace_graph_stop();
|
|
|
|
WARN_ON(1);
|
|
|
|
/* Might as well panic, otherwise we have no where to go */
|
|
|
|
*ret = (unsigned long)panic;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-23 05:22:56 +00:00
|
|
|
*ret = current->ret_stack[index].ret;
|
2008-11-25 23:57:25 +00:00
|
|
|
trace->func = current->ret_stack[index].func;
|
|
|
|
trace->calltime = current->ret_stack[index].calltime;
|
|
|
|
trace->overrun = atomic_read(¤t->trace_overrun);
|
|
|
|
trace->depth = index;
|
2008-12-03 04:50:05 +00:00
|
|
|
barrier();
|
2008-11-23 05:22:56 +00:00
|
|
|
current->curr_ret_stack--;
|
2008-12-03 04:50:06 +00:00
|
|
|
|
2008-11-16 05:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send the trace to the ring-buffer.
|
|
|
|
* @return the original return address.
|
|
|
|
*/
|
|
|
|
unsigned long ftrace_return_to_handler(void)
|
|
|
|
{
|
2008-11-25 20:07:04 +00:00
|
|
|
struct ftrace_graph_ret trace;
|
2008-11-25 23:57:25 +00:00
|
|
|
unsigned long ret;
|
|
|
|
|
|
|
|
pop_return_trace(&trace, &ret);
|
2008-11-16 05:02:06 +00:00
|
|
|
trace.rettime = cpu_clock(raw_smp_processor_id());
|
2008-11-25 23:57:25 +00:00
|
|
|
ftrace_graph_return(&trace);
|
2008-11-16 05:02:06 +00:00
|
|
|
|
2008-12-03 04:50:06 +00:00
|
|
|
if (unlikely(!ret)) {
|
|
|
|
ftrace_graph_stop();
|
|
|
|
WARN_ON(1);
|
|
|
|
/* Might as well panic. What else to do? */
|
|
|
|
ret = (unsigned long)panic;
|
|
|
|
}
|
|
|
|
|
2008-11-25 23:57:25 +00:00
|
|
|
return ret;
|
2008-11-16 05:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hook the return address and push it in the stack of return addrs
|
|
|
|
* in current thread info.
|
|
|
|
*/
|
|
|
|
void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
|
|
|
|
{
|
|
|
|
unsigned long old;
|
|
|
|
unsigned long long calltime;
|
|
|
|
int faulted;
|
2008-11-25 23:57:25 +00:00
|
|
|
struct ftrace_graph_ent trace;
|
2008-11-16 05:02:06 +00:00
|
|
|
unsigned long return_hooker = (unsigned long)
|
|
|
|
&return_to_handler;
|
|
|
|
|
|
|
|
/* Nmi's are currently unsupported */
|
2009-02-06 06:14:26 +00:00
|
|
|
if (unlikely(in_nmi()))
|
2008-12-06 02:43:41 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (unlikely(atomic_read(¤t->tracing_graph_pause)))
|
2008-11-16 05:02:06 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Protect against fault, even if it shouldn't
|
|
|
|
* happen. This tool is too much intrusive to
|
|
|
|
* ignore such a protection.
|
|
|
|
*/
|
|
|
|
asm volatile(
|
2008-12-02 20:34:08 +00:00
|
|
|
"1: " _ASM_MOV " (%[parent_old]), %[old]\n"
|
|
|
|
"2: " _ASM_MOV " %[return_hooker], (%[parent_replaced])\n"
|
2008-11-16 05:02:06 +00:00
|
|
|
" movl $0, %[faulted]\n"
|
|
|
|
|
|
|
|
".section .fixup, \"ax\"\n"
|
|
|
|
"3: movl $1, %[faulted]\n"
|
|
|
|
".previous\n"
|
|
|
|
|
2008-12-02 20:34:08 +00:00
|
|
|
_ASM_EXTABLE(1b, 3b)
|
|
|
|
_ASM_EXTABLE(2b, 3b)
|
2008-11-16 05:02:06 +00:00
|
|
|
|
|
|
|
: [parent_replaced] "=r" (parent), [old] "=r" (old),
|
|
|
|
[faulted] "=r" (faulted)
|
|
|
|
: [parent_old] "0" (parent), [return_hooker] "r" (return_hooker)
|
|
|
|
: "memory"
|
|
|
|
);
|
|
|
|
|
2008-12-03 04:50:02 +00:00
|
|
|
if (unlikely(faulted)) {
|
|
|
|
ftrace_graph_stop();
|
|
|
|
WARN_ON(1);
|
2008-11-16 05:02:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
calltime = cpu_clock(raw_smp_processor_id());
|
|
|
|
|
2008-11-25 23:57:25 +00:00
|
|
|
if (push_return_trace(old, calltime,
|
|
|
|
self_addr, &trace.depth) == -EBUSY) {
|
2008-11-16 05:02:06 +00:00
|
|
|
*parent = old;
|
2008-11-25 23:57:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace.func = self_addr;
|
|
|
|
|
2008-12-03 04:50:05 +00:00
|
|
|
/* Only trace if the calling function expects to */
|
|
|
|
if (!ftrace_graph_entry(&trace)) {
|
|
|
|
current->curr_ret_stack--;
|
|
|
|
*parent = old;
|
|
|
|
}
|
2008-11-16 05:02:06 +00:00
|
|
|
}
|
2008-11-25 20:07:04 +00:00
|
|
|
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
|