2021-03-01 11:34:00

by Jinyang He

[permalink] [raw]
Subject: [PATCH RFC] MIPS: livepatch: Add LIVEPATCH basic code

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 <[email protected]>` 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 <[email protected]>
---
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


2021-03-10 08:20:03

by Miroslav Benes

[permalink] [raw]
Subject: Re: [PATCH RFC] MIPS: livepatch: Add LIVEPATCH basic code

Hi,

I cannot really comment on mips arch specifics but few words from the live
patching perspective.

On Mon, 1 Mar 2021, Jinyang He wrote:

> 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 <[email protected]>` is doing for ftrace. He will use
> `-fpatchable-function-entry` to achieve more complete ftrace.

DYNAMIC_FTRACE_WITH_ARGS has been introduced recently, so you might also
look at that. As far as the live patching is concerned,
DYNAMIC_FTRACE_WITH_ARGS is sufficient.

> 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.

You may want to look at Documentation/livepatch/reliable-stacktrace.rst
which nicely describes the requirements for the reliable stacktraces.

Regards
Miroslav

2021-03-10 08:58:09

by Jinyang He

[permalink] [raw]
Subject: Re: [PATCH RFC] MIPS: livepatch: Add LIVEPATCH basic code

On 03/10/2021 04:18 PM, Miroslav Benes wrote:

> Hi,
>
> I cannot really comment on mips arch specifics but few words from the live
> patching perspective.
Thanks for your reply. :-)

>
> On Mon, 1 Mar 2021, Jinyang He wrote:
>
>> 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 <[email protected]>` is doing for ftrace. He will use
>> `-fpatchable-function-entry` to achieve more complete ftrace.
> DYNAMIC_FTRACE_WITH_ARGS has been introduced recently, so you might also
> look at that. As far as the live patching is concerned,
> DYNAMIC_FTRACE_WITH_ARGS is sufficient.
Huang Pei had told me, and the follow link explains it detaily.
He is doing this work on mips arch now.

http://mpe.github.io/posts/2016/05/23/kernel-live-patching-for-ppc64le/

>> 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.
> You may want to look at Documentation/livepatch/reliable-stacktrace.rst
> which nicely describes the requirements for the reliable stacktraces.
And Maciej answered many questions for me about reliable stacktrace.
I thought Documentation/livepatch/reliable-stacktrace.rst is important, too.
I noticed that arm64 has submitted objtool patches before, and it seems that
MIPS may use this method (ORC) to achieve reliable stack traceback. It looks
complicated to me. Drawf, compiler, abi and so on.

> Regards
> Miroslav
Thanks,
Jinyang