Enable livepatching on powerpc
Subsystems (4)
PATCH v3 1/1 Enable livepatching on powerpc
Changelog v3:
1. Moved -ENOSYS to -EINVAL in klp_write_module_reloc
2. Moved klp_matchaddr to use ftrace_location_range
Changelog v2:
1. Implement review comments by Michael
2. The previous version compared _NIP from the wrong location to check for whether we are going to a patched location
This applies on top of the patches posted by Michael
https://patchwork.ozlabs.org/patch/589791/
It enables livepatching. This takes patch 6/8 and 7/8 of v8 as the base. (See the reference [1] below) and adds logic for checking offset ranges in livepatch with ftrace_location_range.
I tested the sample in the livepatch
References
1. https://patchwork.ozlabs.org/patch/581521/
--- arch/powerpc/Kconfig | 3 ++ arch/powerpc/include/asm/livepatch.h | 41 +++++++++++++++++++++++++++ arch/powerpc/kernel/Makefile | 1 + arch/powerpc/kernel/entry_64.S | 50 +++++++++++++++++++++++++++++++++ arch/powerpc/kernel/livepatch.c | 54 ++++++++++++++++++++++++++++++++++++ include/linux/ftrace.h | 1 + include/linux/livepatch.h | 2 ++ kernel/livepatch/core.c | 12 ++++++-- kernel/trace/ftrace.c | 2 +- 9 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 arch/powerpc/include/asm/livepatch.h create mode 100644 arch/powerpc/kernel/livepatch.c
arch/powerpc/Kconfig +3 -0
@@ -159,6 +159,7 @@ config PPC select ARCH_HAS_DEVMEM_IS_ALLOWED select HAVE_ARCH_SECCOMP_FILTER select ARCH_HAS_UBSAN_SANITIZE_ALL + select HAVE_LIVEPATCH if HAVE_DYNAMIC_FTRACE_WITH_REGS config GENERIC_CSUM def_bool CPU_LITTLE_ENDIAN
@@ -1110,3 +1111,5 @@ config PPC_LIB_RHEAP bool source "arch/powerpc/kvm/Kconfig" + +source "kernel/livepatch/Kconfig"
arch/powerpc/include/asm/livepatch.h new file +41 -0
41 lines not on this page · raw message
arch/powerpc/kernel/Makefile +1 -0
@@ -119,6 +119,7 @@ obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o obj-$(CONFIG_TRACING) += trace_clock.o +obj-$(CONFIG_LIVEPATCH) += livepatch.o ifneq ($(CONFIG_PPC_INDIRECT_PIO),y) obj-y += iomap.o
arch/powerpc/kernel/entry_64.S +50 -0
74 lines not on this page · raw message
arch/powerpc/kernel/livepatch.c new file +54 -0
54 lines not on this page · raw message
include/linux/ftrace.h +1 -0
@@ -455,6 +455,7 @@ int ftrace_update_record(struct dyn_ftrace *rec, int enable);int ftrace_test_record (struct dyn_ftrace *rec,int enable);void ftrace_run_stop_machine (int command);unsigned long ftrace_location (unsigned long ip); +unsigned long ftrace_location_range (unsigned long start,unsigned long end);unsigned long ftrace_get_addr_new (struct dyn_ftrace *rec);unsigned long ftrace_get_addr_curr (struct dyn_ftrace *rec);
include/linux/livepatch.h +2 -0
@@ -134,6 +134,8 @@ int klp_unregister_patch(struct klp_patch *);int klp_enable_patch (struct klp_patch *);int klp_disable_patch (struct klp_patch *); +int klp_matchaddr (struct ftrace_ops *ops,unsigned long ip, +int remove,int reset);#endif /* CONFIG_LIVEPATCH */ #endif /* _LINUX_LIVEPATCH_H_ */
kernel/livepatch/core.c +9 -3
@@ -298,6 +298,12 @@ unlock:rcu_read_unlock (); } +int __weak klp_matchaddr (struct ftrace_ops *ops,unsigned long ip, +int remove,int reset) +{ +return ftrace_set_filter_ip (ops, ip, remove, reset); +} +static void klp_disable_func (struct klp_func *func) {struct klp_ops *ops;
@@ -313,7 +319,7 @@ static void klp_disable_func(struct klp_func *func)if (list_is_singular (&ops->func_stack)) {WARN_ON (unregister_ftrace_function (&ops->fops)); -WARN_ON (ftrace_set_filter_ip (&ops->fops, func->old_addr,1 ,0 )); +WARN_ON (klp_matchaddr (&ops->fops, func->old_addr,1 ,0 ));list_del_rcu (&func->stack_node);list_del (&ops->node);
@@ -352,7 +358,7 @@ static int klp_enable_func(struct klp_func *func)INIT_LIST_HEAD (&ops->func_stack);list_add_rcu (&func->stack_node, &ops->func_stack); - ret =ftrace_set_filter_ip (&ops->fops, func->old_addr,0 ,0 ); + ret =klp_matchaddr (&ops->fops, func->old_addr,0 ,0 );if (ret) {pr_err ("failed to set ftrace filter for function '%s' (%d)\n" , func->old_name, ret);
@@ -363,7 +369,7 @@ static int klp_enable_func(struct klp_func *func)if (ret) {pr_err ("failed to register ftrace handler for function '%s' (%d)\n" , func->old_name, ret); -ftrace_set_filter_ip (&ops->fops, func->old_addr,1 ,0 ); +klp_matchaddr (&ops->fops, func->old_addr,1 ,0 ); goto err; }
kernel/trace/ftrace.c +1 -1
@@ -1533,7 +1533,7 @@ static int ftrace_cmp_recs(const void *a, const void *b)return 0 ; } -static unsigned long ftrace_location_range (unsigned long start,unsigned long end) +unsigned long ftrace_location_range (unsigned long start,unsigned long end) {struct ftrace_page *pg;struct dyn_ftrace *rec;
Signature · 1 line
2.5.0
Petr Mladek
maintainerQuoted 4 lines · Balbir Singh
Changelog v3:
1. Moved-ENOSYSto-EINVALinklp_write_module_reloc
2. Movedklp_matchaddrto useftrace_location_range
Ah, I have been working on it in parallel. It took me longer because I was busy with some other stuff previous days.
I am going to send slightly updated version in a bit. I will address there some comments, see below.
arch/powerpc/include/asm/livepatch.h line 3
Quoted 9 lines
arch/powerpc/include/asm/livepatch.h new file +3
-0@@ -0,0 +1,41 @@ + +extern int klp_write_module_reloc (struct module *mod,unsigned long type, +unsigned long loc,unsigned long value);
Nit, we do not use "extern" in the livepatch-related headers.
arch/powerpc/include/asm/livepatch.h line 40
Quoted 7 lines
arch/powerpc/include/asm/livepatch.h new file +7
-0+ +static inline void klp_arch_set_pc (struct pt_regs *regs,unsigned long ip) +{ + regs->nip = ip; +} + +#endif /* CONFIG_LIVEPATCH */
As Mirek already pointed out. We would like to have here:
#else /* CONFIG_LIVEPATCH */
#error Include linux/livepatch.h, not asm/livepatch.h #endif /* CONFIG_LIVEPATCH */
Quoted 15 lines
arch/powerpc/include/asm/livepatch.h new file +1
-0+#endif /* _ASM_POWERPC64_LIVEPATCH_H */ arch/powerpc/kernel/livepatch.c new file +6
-0@@ -0,0 +1,54 @@ +/** +* klp_write_module_reloc() - write a relocation in a module +* @mod: module in which the section to be modified is found +* @type: ELF relocation type (see asm/elf.h) +* @loc: address that the relocation should be written to +* @value: relocation value (sym address + addend)
arch/powerpc/kernel/livepatch.c line 16
Quoted 10 lines
arch/powerpc/kernel/livepatch.c new file +10
-0+* +* This function writes a relocation to the specified location for +* a particular module. +*/ +int klp_write_module_reloc (struct module *mod,unsigned long type, +unsigned long loc,unsigned long value) +{ +/* This requires infrastructure changes; we need the loadinfos. */ +pr_err ("klp_write_module_reloc not yet supported\n" ); +return -EINVAL ;
I agree that -ENOSYS is a bit misleading. But -EINVAL is not ideal either. Anyway, we should stay compatible with the other architectures in this patch.
arch/powerpc/kernel/livepatch.c line 46
Quoted 9 lines
arch/powerpc/kernel/livepatch.c new file +9
-0+} + +int klp_matchaddr (struct ftrace_ops *ops,unsigned long ip, +int remove,int reset) +{ +int ret =1 ; +unsigned long correct_ip; + + correct_ip =ftrace_location_range (ip, ip +16 );
I really like that it is so easy in the end.
arch/powerpc/kernel/livepatch.c line 50
Quoted 4 lines
arch/powerpc/kernel/livepatch.c new file +4
-0+if (!correct_ip) + goto done; + + ret =ftrace_set_filter_ip (ops, correct_ip, remove, reset);
I do not like that we hide this important operation under a function called klp_matchaddr(). I am going to rename this function to klp_get_ftrace_location() and just return the address found by ftrace_location_range(). Another advantage is that it will make one-liner from this arch-specific function.
arch/powerpc/kernel/livepatch.c line 54
Quoted 4 lines
arch/powerpc/kernel/livepatch.c new file +4
-0+done: +return ret; +} +
Thanks a lot for pushing this forward,
Petr