arm64: implement FTRACE_WITH_REGS
Subsystems (4)
PATCH 1/1 arm64: implement FTRACE_WITH_REGS
Hi all,
as promised on <gcc-patches>, here's the working prototype.
Once gcc is enhanced to optionally generate NOPs at the beginning of each function, like the concept proven in
https://gcc.gnu.org/ml/gcc-patches/2016-04/msg01671.html
(sans the "fprintf (... pad_size);", which spoils the data structure for kernel use), the generated pads can nicely be used to reroute function calls for tracing/profiling, or live patching in the near future.
The pads look likefffffc00081335f0 <hrtimer_init>:
fffffc00081335f0: d503201f nop fffffc00081335f4: d503201f nop fffffc00081335f8: a9bd7bfd stp x29, x30, [sp,#-48]! fffffc00081335fc: 910003fd mov x29, sp
[...]
This patch gets the pad locations from the compiler-generated __prolog_pads_loc into the _mcount_loc array, and provides the code patching functions to turn the pads at runtime into
fffffc00081335f0 mov x9, x30 fffffc00081335f4 bl 0xfffffc00080a08c0 <ftrace_caller> fffffc00081335f8 stp x29, x30, [sp,#-48]! fffffc00081335fc mov x29, sp
as well as an ftrace_caller that can handle these call sites. Now ARCH_SUPPORTS_FTRACE_OPS as a benefit, and the graph caller still works, too.
I originally started with Li Bin's
[PATCH 1/3] livepatch: ftrace: arm64: Add support for DYNAMIC_FTRACE_WITH_REGS dated Sat, 29 Aug 2015, although not much is left of it because of the different assumptions about compiler support.
IMHO the prolog-pad should be used more widely, but that's a different story.
--- arch/arm64/Kconfig | 1 + arch/arm64/Makefile | 4 ++ arch/arm64/include/asm/ftrace.h | 8 ++++ arch/arm64/kernel/Makefile | 6 +-- arch/arm64/kernel/entry-ftrace.S | 89 +++++++++++++++++++++++++++++++++++++++ arch/arm64/kernel/ftrace.c | 40 ++++++++++++++++-- include/asm-generic/vmlinux.lds.h | 2 +- include/linux/compiler.h | 5 +++ 8 files changed, 148 insertions(+), 7 deletions(-)
arch/arm64/Kconfig +1 -0
@@ -68,6 +68,7 @@ config ARM64 select HAVE_DMA_API_DEBUG select HAVE_DMA_CONTIGUOUS select HAVE_DYNAMIC_FTRACE + select HAVE_DYNAMIC_FTRACE_WITH_REGS select HAVE_EFFICIENT_UNALIGNED_ACCESS select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_TRACER
arch/arm64/Makefile +4 -0
@@ -35,6 +35,10 @@ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables KBUILD_CFLAGS += $(call cc-option, -mpc-relative-literal-loads) KBUILD_AFLAGS += $(lseinstr) +ifeq ($(CONFIG_DYNAMIC_FTRACE_WITH_REGS), y) +CC_FLAGS_FTRACE := -fprolog-pad=2 -fno-ipa-ra +endif + ifeq ($(CONFIG_CPU_BIG_ENDIAN), y) KBUILD_CPPFLAGS += -mbig-endian AS += -EB
arch/arm64/include/asm/ftrace.h +8 -0
@@ -16,6 +16,14 @@#define MCOUNT_ADDR ((unsigned long)_mcount)#define MCOUNT_INSN_SIZE AARCH64_INSN_SIZE +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS +#define ARCH_SUPPORTS_FTRACE_OPS 1 +#define REC_IP_BRANCH_OFFSET 4 +#define FTRACE_REGS_ADDR FTRACE_ADDR +#else +#define REC_IP_BRANCH_OFFSET 0 +#endif +#ifndef __ASSEMBLY__#include <linux/compat.h>
arch/arm64/kernel/Makefile +3 -3
@@ -6,9 +6,9 @@ CPPFLAGS_vmlinux.lds := -DTEXT_OFFSET=$(TEXT_OFFSET) AFLAGS_head.o := -DTEXT_OFFSET=$(TEXT_OFFSET) CFLAGS_armv8_deprecated.o := -I$(src) -CFLAGS_REMOVE_ftrace.o = -pg -CFLAGS_REMOVE_insn.o = -pg -CFLAGS_REMOVE_return_address.o = -pg +CFLAGS_REMOVE_ftrace.o = -pg $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_insn.o = -pg $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = -pg $(CC_FLAGS_FTRACE) # Object file lists. arm64-obj-y := debug-monitors.o entry.o irq.o fpsimd.o \
arch/arm64/kernel/entry-ftrace.S +89 -0
119 lines not on this page · raw message
arch/arm64/kernel/ftrace.c +37 -3
@@ -39,6 +39,9 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,if (aarch64_insn_read ((void *)pc, &replaced))return -EFAULT ; +if (replaced == new) +return 0 ; +if (replaced != old)return -EINVAL ; }
@@ -68,28 +71,59 @@ int ftrace_update_ftrace_func(ftrace_func_t func)*/ int ftrace_make_call (struct dyn_ftrace *rec,unsigned long addr) { -unsigned long pc = rec->ip; +unsigned long pc = rec->ip+REC_IP_BRANCH_OFFSET ; +int ret;u32 old, new; +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS old =aarch64_insn_gen_nop (); + new =0xaa1e03e9 ;/* mov x9,x30 */ + ret =ftrace_modify_code (pc-REC_IP_BRANCH_OFFSET , old, new, true); +if (ret) +return ret; +smp_wmb (); +#endif new =aarch64_insn_gen_branch_imm (pc, addr,AARCH64_INSN_BRANCH_LINK );return ftrace_modify_code (pc, old, new, true); } +int ftrace_modify_call (struct dyn_ftrace *rec,unsigned long old_addr, +unsigned long addr) +{ +unsigned long pc = rec->ip+REC_IP_BRANCH_OFFSET ; +u32 old, new; + + old =aarch64_insn_gen_branch_imm (pc, old_addr, true); + new =aarch64_insn_gen_branch_imm (pc, addr, true); + +return ftrace_modify_code (pc, old, new, true); +} +/* * Turn off the call to ftrace_caller() in instrumented function */ int ftrace_make_nop (struct module *mod,struct dyn_ftrace *rec,unsigned long addr) { -unsigned long pc = rec->ip; +unsigned long pc = rec->ip+REC_IP_BRANCH_OFFSET ;u32 old, new; +int ret; + old =aarch64_insn_gen_branch_imm (pc, addr,AARCH64_INSN_BRANCH_LINK ); new =aarch64_insn_gen_nop (); -return ftrace_modify_code (pc, old, new, true); + ret =ftrace_modify_code (pc, old, new, true); +if (ret) +return ret; +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS +smp_wmb (); + old =0xaa1e03e9 ;/* mov x9,x30 */ + new =aarch64_insn_gen_nop (); + ret =ftrace_modify_code (pc-REC_IP_BRANCH_OFFSET , old, new, true); +#endif +return ret; }void arch_ftrace_update_code (int command)
include/asm-generic/vmlinux.lds.h +1 -1
@@ -89,7 +89,7 @@#ifdef CONFIG_FTRACE_MCOUNT_RECORD #define MCOUNT_REC () . = ALIGN(8); \ VMLINUX_SYMBOL(__start_mcount_loc) = .; \ - *(__mcount_loc) \ + *(__mcount_loc) *(__prolog_pads_loc) \ VMLINUX_SYMBOL(__stop_mcount_loc) = .;#else #define MCOUNT_REC ()
include/linux/compiler.h +5 -0
@@ -63,8 +63,13 @@ extern void __chk_io_ptr(const volatile void __iomem *);#if defined(CC_USING_HOTPATCH ) && !defined(__CHECKER__)#define notrace __attribute__((hotpatch(0,0)))#else +/* Should really read "#ifdef CC_USING_PROLOG_PAD" : */ +#if defined(__aarch64__) && defined(CONFIG_DYNAMIC_FTRACE_WITH_REGS ) +#define notrace __attribute__((prolog_pad(0))) +#else #define notrace __attribute__((no_instrument_function))#endif +#endif /* Intel compiler defines __GNUC__. So we will overwrite implementations * coming from above header files here
Signature · 1 line
2.6.6