[PATCH RFC] MIPS: livepatch: Add LIVEPATCH basic code

From: Jinyang He
Date: Mon Mar 01 2021 - 06:33:00 EST


Add the basic code of livepatch. livepatch is temporarily unavailable.
Two core functions are missing, one is DYNAMIC_FTRACE_WITH_REGS, and
another is save_stack_trace_tsk_reliable().
`Huang Pei <huangpei@xxxxxxxxxxx>` is doing for ftrace. He will use
`-fpatchable-function-entry` to achieve more complete ftrace.
save_stack_trace_tsk_reliable() currently has difficulties. This function
may be improved in the future, but that seems to be a long time away.
This is also the reason for delivering this RFC. Hope to get any help.

Signed-off-by: Jinyang He <hejinyang@xxxxxxxxxxx>
---
arch/mips/Kconfig | 1 +
arch/mips/include/asm/livepatch.h | 28 ++++++++++++++++++++++++++++
arch/mips/include/asm/thread_info.h | 1 +
arch/mips/kernel/mcount.S | 9 +++++++++
4 files changed, 39 insertions(+)
create mode 100644 arch/mips/include/asm/livepatch.h

diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
index 47715cb..8ef92dd 100644
--- a/arch/mips/Kconfig
+++ b/arch/mips/Kconfig
@@ -478,6 +478,7 @@ config MACH_LOONGSON64
select CEVT_R4K
select CPU_HAS_WB
select FORCE_PCI
+ select HAVE_LIVEPATCH
select ISA
select I8259
select IRQ_MIPS_CPU
diff --git a/arch/mips/include/asm/livepatch.h b/arch/mips/include/asm/livepatch.h
new file mode 100644
index 0000000..26e1212
--- /dev/null
+++ b/arch/mips/include/asm/livepatch.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * livepatch.h - mips-specific Kernel Live Patching Core
+ */
+
+#ifndef _ASM_MIPS_LIVEPATCH_H
+#define _ASM_MIPS_LIVEPATCH_H
+
+#include <linux/ftrace.h>
+#include <linux/kallsyms.h>
+
+static inline void klp_arch_set_pc(struct ftrace_regs *fregs, unsigned long ip)
+{
+ struct pt_regs *regs = ftrace_get_regs(fregs);
+
+ regs->regs[31] = ip;
+}
+
+#define klp_get_ftrace_location klp_get_ftrace_location
+static inline unsigned long klp_get_ftrace_location(unsigned long faddr)
+{
+ unsigned long func_size, offset;
+
+ kallsyms_lookup_size_offset(faddr, &func_size, &offset);
+ return ftrace_location_range(faddr, faddr + func_size);
+}
+
+#endif
diff --git a/arch/mips/include/asm/thread_info.h b/arch/mips/include/asm/thread_info.h
index e2c352d..1e78359 100644
--- a/arch/mips/include/asm/thread_info.h
+++ b/arch/mips/include/asm/thread_info.h
@@ -117,6 +117,7 @@ static inline struct thread_info *current_thread_info(void)
#define TIF_UPROBE 6 /* breakpointed or singlestepping */
#define TIF_NOTIFY_SIGNAL 7 /* signal notifications exist */
#define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal() */
+#define TIF_PATCH_PENDING 10 /* pending live patching update */
#define TIF_USEDFPU 16 /* FPU was used by this task this quantum (SMP) */
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
#define TIF_NOHZ 19 /* in adaptive nohz mode */
diff --git a/arch/mips/kernel/mcount.S b/arch/mips/kernel/mcount.S
index cff52b2..8bf4c6a 100644
--- a/arch/mips/kernel/mcount.S
+++ b/arch/mips/kernel/mcount.S
@@ -113,6 +113,15 @@ ftrace_stub:
RETURN_BACK
END(ftrace_caller)

+#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
+NESTED(ftrace_regs_caller, PT_SIZE, ra)
+/* ftrace_regs_caller body */
+#ifdef CONFIG_LIVEPATCH
+/* Restore sp register */
+#endif
+ RETURN_BACK
+ END(ftrace_regs_caller)
+#endif
#else /* ! CONFIG_DYNAMIC_FTRACE */

NESTED(_mcount, PT_SIZE, ra)
--
2.1.0