MIPS: tracing: Optimize the implementation
PATCH 1/1 MIPS: tracing: Optimize the implementation
From: Wu Zhangjin <[email protected]>
Hi, Ralf
This patch not only fixes the function graph tracing support of 32bit kernel and module compiled with/without mmcount-ra-address, but also speeds up the dynamic function tracer and function graph tracer.
Just tested it with the latest gcc 4.5 and added necessary changes to make the kernel and module support work well with mmcount-ra-address. I think it is applicable now.
Regards,
Wu Zhangjin
--------------
Changes:
v4 -> v5:
* All of the these changes have been tested in 32bit & 64bit kernel compiled with gcc 4.4 & 4.5, including kernel & module Ftrace support.
+ Fixup of the module support broken by the change of v1->v2.
As the source code(and also the objdump result of the kernel image) of -mmcount-ra-address shows, the change of v1->v2 is wrong and has broken the module support:
For 64bit kernel with -mmcount-ra-address, the return address saving operation is put in the delay slot of the jump instruction(calling site of _mcount), so, the offset of "b 1f" is the same to the -mno-mcount-ra-address: 16.
but for 32bit kernel, since there is already an instruction put in the delay slot, the return address operation must be put before the jump instruciton, so, the offset is bigger than the one with -mno-mcount-ra-address: 16+4=20.
+ Fixup of the 32bit support with gcc 4.5
As the doc[1] of gcc-4.5 shows, the -mmcount-ra-address uses the $12 to transfer the stack offset of the return address to the _mcount function. in 64bit kernel, $12 is t0, but in 32bit kernel, it is t4, so, we need to use $12 instead of t0 here to cover the 64bit and 32bit support.
[1] Gcc doc: MIPS Options
http://gcc.gnu.org/onlinedocs/gcc/MIPS-Options.html
+ Cleanup of mcount.S
o merges two continuous "#ifdef CONFIG_64BIT".
o removes the instruction "move a0, t0" via using a0 directly.
o share the common "PTR_LA a0, PT_R1(sp)"
v3 -> v4:
+ Fixup of 'undefined ftrace_graph_caller'
ftrace_graph_caller variable is only needed by CONFIG_FUNCTION_GRAPH_TRACER, so, wrap it with #ifdef.
v2 -> v3:
+ Fixes the buidling error of the changes from v2.
v1 -> v2:
+ Fixes the support of -mmcount-ra-address
The offset of "b 1f" instruction should be 20 for leaf-function and 24 for non-leaf function when -mmcount-ra-address is enabled. This patch adds a new get_insn_b_1f() function to get the different "b 1f" instruction.
(only test without -mmcount-ra-address.)
v1:
+ Reduce the runtime overhead
o Uses macros instead of variables for the fixed instructions to reduce memory access
o Moves the Initilization of the instructions which will be fixed after linking from ftrace_make_nop/ftrace_make_call to ftrace_dyn_arch_init() and encodes the instructions through uasm(arch/mips/include/asm/uasm.h).
o A common macro in_module() is defined to determine which space the instruction pointer stays in and several related conditional statements are converted to conditional operator(? :) statement.
+ Cleanup the whole stuff
Lots of comments/macros have been cleaned up to let it look better.
--- arch/mips/include/asm/ftrace.h | 12 ++- arch/mips/kernel/ftrace.c | 217 +++++++++++++++++++++++----------------- arch/mips/kernel/mcount.S | 20 ++-- 3 files changed, 141 insertions(+), 108 deletions(-)
arch/mips/include/asm/ftrace.h +10 -2
@@ -3,7 +3,7 @@* License. See the file "COPYING" in the main directory of this archive for * more details. * -* Copyright (C) 2009 DSLab, Lanzhou University, China +* Copyright (C) 2009, 2010 DSLab, Lanzhou University, China * Author: Wu Zhangjin <[email protected]> */
@@ -19,6 +19,14 @@extern void _mcount (void );#define mcount _mcount +/* +* If the Instruction Pointer is in module space (0xc0000000), return true; +* otherwise, it is in kernel space (0x80000000), return false. +* +* FIXME: This may not work in some cases. +*/ +#define in_module (ip) (unlikely((ip) & 0x40000000)) +#define safe_load (load, src, dst, error) \ do { \ asm volatile ( \
@@ -83,8 +91,8 @@ static inline unsigned long ftrace_call_adjust(unsigned long addr)struct dyn_arch_ftrace { }; -#endif /* CONFIG_DYNAMIC_FTRACE */ +#endif /* __ASSEMBLY__ */ #endif /* CONFIG_FUNCTION_TRACER */ #endif /* _ASM_MIPS_FTRACE_H */
arch/mips/kernel/ftrace.c +123 -94
337 lines not on this page · raw message
arch/mips/kernel/mcount.S +8 -12
@@ -6,6 +6,7 @@ * more details. * * Copyright (C) 2009 Lemote Inc. & DSLab, Lanzhou University, China + * Copyright (C) 2010 DSLab, Lanzhou University, China * Author: Wu Zhangjin <[email protected]> */
@@ -45,8 +46,6 @@ PTR_L a5, PT_R9(sp) PTR_L a6, PT_R10(sp) PTR_L a7, PT_R11(sp) -#endif -#ifdef CONFIG_64BIT PTR_ADDIU sp, PT_SIZE #else PTR_ADDIU sp, (PT_SIZE + 8)
@@ -71,7 +70,7 @@ _mcount: MCOUNT_SAVE_REGS #ifdef KBUILD_MCOUNT_RA_ADDRESS - PTR_S t0, PT_R12(sp) /* t0 saved the location of the return address(at) by -mmcount-ra-address */ + PTR_S $12, PT_R12(sp) /* $12 saved the location of the return address(at) */ #endif move a0, ra /* arg1: next ip, selfaddr */
@@ -137,25 +136,22 @@ NESTED(ftrace_graph_caller, PT_SIZE, ra) #ifdef CONFIG_DYNAMIC_FTRACE PTR_L a1, PT_R31(sp) /* load the original ra from the stack */ #ifdef KBUILD_MCOUNT_RA_ADDRESS - PTR_L t0, PT_R12(sp) /* load the original t0 from the stack */ + PTR_L a0, PT_R12(sp) /* arg1: the location of the return address */ #endif -#else +#else /* ! CONFIG_DYNAMIC_FTRACE */ MCOUNT_SAVE_REGS move a1, ra /* arg2: next ip, selfaddr */ #endif #ifdef KBUILD_MCOUNT_RA_ADDRESS - bnez t0, 1f /* non-leaf func: t0 saved the location of the return address */ + bnez a0, 1f /* non-leaf func: get the location of the return address from saved $12 */ nop - PTR_LA t0, PT_R1(sp) /* leaf func: get the location of at(old ra) from our own stack */ -1: move a0, t0 /* arg1: the location of the return address */ -#else - PTR_LA a0, PT_R1(sp) /* arg1: &AT -> a0 */ #endif - jal prepare_ftrace_return + PTR_LA a0, PT_R1(sp) /* leaf func: get the location of at(old ra) from our own stack */ +1: jal prepare_ftrace_return #ifdef CONFIG_FRAME_POINTER move a2, fp /* arg3: frame pointer */ -#else +#else /* ! CONFIG_FRAME_POINTER */ #ifdef CONFIG_64BIT PTR_LA a2, PT_SIZE(sp) #else
Signature · 1 line
1.7.0
On Sun, 2010-04-18 at 04:51 +0800, Wu Zhangjin wrote:
[...]
Quoted 1 line
arch/mips/kernel/mcount.S +0
-0
[...]
Quoted 32 lines
arch/mips/kernel/mcount.S +3
-5@@ -45,8 +46,6 @@ PTR_L a5, PT_R9(sp) PTR_L a6, PT_R10(sp) PTR_L a7, PT_R11(sp) -#endif -#ifdef CONFIG_64BIT PTR_ADDIU sp, PT_SIZE #else PTR_ADDIU sp, (PT_SIZE + 8)@@ -71,7 +70,7 @@ _mcount: MCOUNT_SAVE_REGS #ifdef KBUILD_MCOUNT_RA_ADDRESS - PTR_S t0, PT_R12(sp) /* t0 saved the location of the return address(at) by -mmcount-ra-address */ + PTR_S $12, PT_R12(sp) /* $12 saved the location of the return address(at) */ #endif move a0, ra /* arg1: next ip, selfaddr */@@ -137,25 +136,22 @@ NESTED(ftrace_graph_caller, PT_SIZE, ra) #ifdef CONFIG_DYNAMIC_FTRACE PTR_L a1, PT_R31(sp) /* load the original ra from the stack */ #ifdef KBUILD_MCOUNT_RA_ADDRESS - PTR_L t0, PT_R12(sp) /* load the original t0 from the stack */ + PTR_L a0, PT_R12(sp) /* arg1: the location of the return address */ #endif -#else +#else /* ! CONFIG_DYNAMIC_FTRACE */ MCOUNT_SAVE_REGS move a1, ra /* arg2: next ip, selfaddr */ #endif
arch/mips/kernel/mcount.S NESTED(ftrace_graph_caller, PT_SIZE, ra) · line 155
Quoted 10 lines
arch/mips/kernel/mcount.S +1
-6#ifdef KBUILD_MCOUNT_RA_ADDRESS - bnez t0, 1f /* non-leaf func: t0 saved the location of the return address */ + bnez a0, 1f /* non-leaf func: get the location of the return address from saved $12 */ nop - PTR_LA t0, PT_R1(sp) /* leaf func: get the location of at(old ra) from our own stack */ -1: move a0, t0 /* arg1: the location of the return address */ -#else - PTR_LA a0, PT_R1(sp) /* arg1: &AT -> a0 */ #endif - jal prepare_ftrace_return
The above change has broken the static function graph tracer with -mmcount-ra-address for it did not consider the argument 1 for static function graph tracer, a new revision of this patch will be sent out as "MIPS: tracing: adds misc cleanups and fixups".
Regards,
Wu Zhangjin