Skip to contentSkip to conversation

Enable livepatching for powerpc

RFC PATCH Superseded Balbir Singh 2 participants 2 messages Mar 3, 2016

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 Enable livepatching for powerpc

Changelog:
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. Removes the extra strict check in gcc-profile-kernel-notrace.sh and adds logic for checking offsets in livepatch. The patch for HAVE_C_RECORDMCOUNT is not required and not used here.

Depending on whether or not a TOC is generated, the offset for _mcount can be +16,+12,+8,+4. The changes are such that the offset checks are specific to powerpc.

TODOs
1. Build a version with offsets removed and rebuild ftrace_location() sort of functionality
2. Make livepatching experimental on powerpc

Comments? Testing? I tested the sample in the livepatch directory

References

1. https://patchwork.ozlabs.org/patch/581521/
2. https://patchwork.ozlabs.org/patch/587464/

---
 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/kgdb.c           |  4 +++
 arch/powerpc/kernel/livepatch.c      | 54 ++++++++++++++++++++++++++++++++++++
 include/linux/livepatch.h            |  2 ++
 kernel/livepatch/core.c              | 12 ++++++--
 8 files changed, 164 insertions(+), 3 deletions(-)
 create mode 100644 arch/powerpc/include/asm/livepatch.h
 create mode 100644 arch/powerpc/kernel/livepatch.c
arch/powerpc/Kconfig +3 -0
index 792e169..8278e5e 100644
@@ -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
@@ -1109,3 +1110,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
index 2da380f..b767e14 100644
@@ -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/kgdb.c +4 -0
index e77c3cc..dbf0981 100644
@@ -445,7 +445,11 @@ int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  * Global data
  */
 struct kgdb_arch arch_kgdb_ops = {
+#ifdef __LITTLE_ENDIAN__
+	.gdb_bpt_instr = {0x08, 0x10, 0x82, 0x7d},
+#else
 	.gdb_bpt_instr = {0x7d, 0x82, 0x10, 0x08},
+#endif
 };
 
 static int kgdb_not_implemented(struct pt_regs *regs)
arch/powerpc/kernel/livepatch.c new file +54 -0

54 lines not on this page · raw message

include/linux/livepatch.h +2 -0
index a882865..29cea3a 100644
@@ -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
index bc2c85c..c484a4b 100644
@@ -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;
 		}
 
Signature · 1 line
2.5.0
Tested-by on v2 1/1
#1 lore raw
Quoted 26 lines · Balbir Singh

Changelog:
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. Removes the extra strict check in gcc-profile-kernel-notrace.sh and adds logic for checking offsets in livepatch. The patch for HAVE_C_RECORDMCOUNT is not required and not used here.

Depending on whether or not a TOC is generated, the offset for _mcount can be +16,+12,+8,+4. The changes are such that the offset checks are specific to powerpc.

TODOs
1. Build a version with offsets removed and rebuild ftrace_location() sort of functionality
2. Make livepatching experimental on powerpc

Comments? Testing? I tested the sample in the livepatch directory

Quoted 7 lines

References

1. https://patchwork.ozlabs.org/patch/581521/
2. https://patchwork.ozlabs.org/patch/587464/