arm64 live patching
Subsystems (4)
v3 cover arm64 live patching
V3, including suggestions from the replies and 2 fixes.
Reminder: make sure you have a prolog-pad gcc, and this in your top level Makefile:
ifdef CONFIG_LIVEPATCHKBUILD_CFLAGS += $(call cc-option,-fno-ipa-ra)
endif
Tested with v4.8-rc1 + gcc-6.1
Changes since v2:
* Do not skip initial NOP conversion for CC_USING_PROLOG_PAD, in other words: don't introduce new #ifdefs into generic code. (as requested by Steven Rostedt after my intermediate suggestion) Instead, check in arm64 whether a call to mcount is to be NOPed, and be strict again about the expected "old" instructions.
* issue a warning should the compiler not know about -fprolog-pad= (as suggested by Josh Poimboeuf)
Only strip the compiler flag proper, not the cpp flag, for critical files.
* fix thinko / bummer in live patch return address change detection. Skip the graph caller if the addresses are _not_ equal!
Changes since v1:
* instead of a comment "should be CC_USING_PROLOG_PAD":
do it. CC_FLAGS_FTRACE holds it now, and the IPA disabler has become a separate issue (see above).
Torsten Duwe (2):
arm64: implement FTRACE_WITH_REGS
arm64: implement live patching
arch/arm64/Kconfig | 4 ++ arch/arm64/Makefile | 9 ++++ arch/arm64/include/asm/ftrace.h | 8 +++ arch/arm64/include/asm/livepatch.h | 37 ++++++++++++++ arch/arm64/kernel/Makefile | 6 +-- arch/arm64/kernel/entry-ftrace.S | 102 +++++++++++++++++++++++++++++++++++++ arch/arm64/kernel/ftrace.c | 45 ++++++++++++++-- include/asm-generic/vmlinux.lds.h | 2 +- include/linux/compiler.h | 4 ++ 9 files changed, 209 insertions(+), 8 deletions(-) create mode 100644 arch/arm64/include/asm/livepatch.h
Signature · 1 line
2.6.2
PATCH v3 1/2 arm64: implement FTRACE_WITH_REGS
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.
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.
--- arch/arm64/Kconfig | 1 + arch/arm64/Makefile | 9 ++++ arch/arm64/include/asm/ftrace.h | 8 ++++ arch/arm64/kernel/Makefile | 6 +-- arch/arm64/kernel/entry-ftrace.S | 89 +++++++++++++++++++++++++++++++++++++++ arch/arm64/kernel/ftrace.c | 45 ++++++++++++++++++-- include/asm-generic/vmlinux.lds.h | 2 +- include/linux/compiler.h | 4 ++ 8 files changed, 156 insertions(+), 8 deletions(-)
arch/arm64/Kconfig +1 -0
@@ -72,6 +72,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 +9 -0
@@ -55,6 +55,15 @@ ifeq ($(CONFIG_ARM64_MODULE_PLTS),y) KBUILD_LDFLAGS_MODULE += -T $(srctree)/arch/arm64/kernel/module.lds endif +ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS + CC_FLAGS_FTRACE := -fprolog-pad=2 + KBUILD_CPPFLAGS += -DCC_USING_PROLOG_PAD + ifeq ($(call cc-option,-fprolog-pad=2),) + $(warning Cannot use CONFIG_DYNAMIC_FTRACE_WITH_REGS: \ + -fprolog-pad not supported by compiler) + endif +endif + # Default value head-y := arch/arm64/kernel/head.o
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 +41 -4
69 lines not on this page · raw message
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 +4 -0
@@ -63,8 +63,12 @@ extern void __chk_io_ptr(const volatile void __iomem *);#if defined(CC_USING_HOTPATCH ) && !defined(__CHECKER__)#define notrace __attribute__((hotpatch(0,0)))#else +#ifdef CC_USING_PROLOG_PAD +#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.2
kbuild test robot
build botRe: [PATCH v3 1/2] arm64: implement FTRACE_WITH_REGS
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…
Hi Torsten,
[auto build test ERROR on arm64/for-next/core]
[also build test ERROR on v4.8-rc1 next-20160811]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Torsten-Duwe/arm64-live-patching/20160812-004131 base: https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core config: arm64-allmodconfig (attached as .config) compiler: aarch64-linux-gnu-gcc (Debian 5.4.0-6) 5.4.0 20160609
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=arm64
All errors (new ones prefixed by >>):
arch/arm64/Makefile:61: Cannot use CONFIG_DYNAMIC_FTRACE_WITH_REGS: -fprolog-pad not supported by compiler Makefile:687: Cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler
>> aarch64-linux-gnu-gcc: error: unrecognized command line option '-fprolog-pad=2'
make[2]: *** [kernel/bounds.s] Error 1 make[2]: Target '__build' not remade because of errors. make[1]: *** [prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [sub-make] Error 2
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
PATCH v3 2/2 arm64: implement live patching
In reply to Torsten Duwestraightforward on top of FTRACE_WITH_REGS.
--- arch/arm64/Kconfig | 3 +++ arch/arm64/include/asm/livepatch.h | 37 +++++++++++++++++++++++++++++++++++++ arch/arm64/kernel/entry-ftrace.S | 13 +++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 arch/arm64/include/asm/livepatch.h
arch/arm64/Kconfig +3 -0
@@ -80,6 +80,7 @@ config ARM64 select HAVE_GENERIC_DMA_COHERENT select HAVE_HW_BREAKPOINT if PERF_EVENTS select HAVE_IRQ_TIME_ACCOUNTING + select HAVE_LIVEPATCH select HAVE_MEMBLOCK select HAVE_MEMBLOCK_NODE_MAP if NUMA select HAVE_PATA_PLATFORM
@@ -1042,4 +1043,6 @@ if CRYPTO source "arch/arm64/crypto/Kconfig" endif +source "kernel/livepatch/Kconfig" + source "lib/Kconfig"
arch/arm64/include/asm/livepatch.h new file +37 -0
@@ -0,0 +1,37 @@ +/* +* livepatch.h - arm64-specific Kernel Live Patching Core +* +* Copyright (C) 2016 SUSE +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef _ASM_ARM64_LIVEPATCH_H +#define _ASM_ARM64_LIVEPATCH_H + +#include <linux/module.h> +#include <linux/ftrace.h> + +#ifdef CONFIG_LIVEPATCH +static inline int klp_check_compiler_support (void ) +{ +return 0 ; +} + +static inline void klp_arch_set_pc (struct pt_regs *regs,unsigned long ip) +{ + regs->pc = ip; +} +#endif /* CONFIG_LIVEPATCH */ + +#endif /* _ASM_ARM64_LIVEPATCH_H */
arch/arm64/kernel/entry-ftrace.S +13 -0
@@ -204,6 +204,9 @@ ENTRY(ftrace_caller) str x9, [sp, #S_LR] /* The program counter just after the ftrace call site */ str lr, [sp, #S_PC] +#if defined(CONFIG_LIVEPATCH) && defined(CONFIG_FUNCTION_GRAPH_TRACER) + mov x19,lr /* remember old return address */ +#endif /* The stack pointer as it was on ftrace_caller entry... */ add x29, sp, #S_FRAME_SIZE+16 /* ...is also our new FP */ str x29, [sp, #S_SP]
@@ -219,6 +222,16 @@ ftrace_call: bl ftrace_stub +#if defined(CONFIG_LIVEPATCH) && defined(CONFIG_FUNCTION_GRAPH_TRACER) + /* Is the trace function a live patcher an has messed with + * the return address? + */ + ldr x9, [sp, #S_PC] + cmp x9, x19 /* compare with the value we remembered */ + /* to not call graph tracer's "call" mechanism twice! */ + b.ne ftrace_regs_return +#endif + #ifdef CONFIG_FUNCTION_GRAPH_TRACER .global ftrace_graph_call ftrace_graph_call: // ftrace_graph_caller();
Signature · 1 line
2.6.2