linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/7] arm: Add livepatch support
@ 2016-12-06 17:06 Abel Vesa
  2016-12-06 17:06 ` [PATCH 1/7] arm: Add livepatch arch specific code Abel Vesa
                   ` (8 more replies)
  0 siblings, 9 replies; 30+ messages in thread
From: Abel Vesa @ 2016-12-06 17:06 UTC (permalink / raw)
  To: linux, jpoimboe, jeyu, jikos, mbenes, pmladek
  Cc: rostedt, mingo, gregkh, geert+renesas, davem, akpm,
	emil.l.velikov, mchehab, linux, ard.biesheuvel, jens.wiklander,
	jean-philippe.brucker, viro, stefano.stabellini, chris.brandt,
	linux-kernel, linux-arm-kernel, live-patching, Abel Vesa

This is just an idea I've been trying out for a while now. 

Just in case somebody wants to play with it, this applies to linux-arm/for-next.

Also please note that this was only tested in qemu, but I will do some testing 
on some real hardware in the following days.

FWICT, on this arch the compiler always generates a function prologue somewhere
between these lines:

e1a0c00d        mov     ip, sp
e92ddff0        push    {r4-r9, sl, fp, ip, lr, pc}
e24cb004        sub     fp, ip, #4
e24dd064        sub     sp, sp, #100    ; 0x64 <--- local variables
e52de004        push    {lr}            ; (str lr, [sp, #-4]!)
ebf9c2c9        bl      80110364 <__gnu_mcount_nc>
....

Every function that follows this pattern (the number of registers pushed and the
sp subtraction for the local variables being the only acceptable exception) can
be patched with this mechanism. IIRC, only the inline functions and notrace 
functions do not follow this pattern.

Considering that the function is livepatchable, when the time comes to call
ftrace_call, the ftrace_regs_caller is called instead.

Because this arch didn't have a ftrace with regs implementation, the
ftrace_regs_caller was added.

This new function adds the regs saving/restoring part, plus the part necessary
for the livepatch mechanism to work. After the regs are saved and the r3 is set
to contain the sp's value, we're keeping the old pc into r10 in order to be
checked later against the new pc.

Next, the r1 and r0 are set for the ftrace_func, then, the ftrace_stub is called
and the klp_ftrace_handler overwrites the old pc with the new one.

Here comes the tricky part. We're checking if the pc is still the old one, if it
is we jump the whole livepatching and go ahead with restoring the saved regs.

If the pc is modified, it means we're livepatching current function and we need
to pop all regs from r1 through r12, jump over the next two regs saved on stack
(we're not interested in those since we're trying to get the same regs context
as it was at the point the function-to-be-patched was called) and put the new pc
into r11.

Since r12 contains the sp from when the function just got branched to, we need
to set the sp back to that.

Then we need to put the new pc on stack so that when we're popping r11 through 
pc, we will actually jump to the first instruction from the new function.

We don't need to worry about the returning phase since the epilogue of the new
function will take care of that and from there on everything goes back to 
normal.

The whole advantage of this over adding compiler support is that we're not
introducing nops at the beginning of the function. As a matter of fact, we're
not changing anything between an image with livepatch and an image without it
(except the ftrace_regs_call addition and the livepatch necessary code).

As for the implementation of the ftrace_regs_caller, I still think there might
be some unsafe stack handling since I'm getting some build warnings. Those are
due to pushing/popping of a list of regs in which the sp resides. I'll try to 
get around those in a next iteration (if necessary), but first I would like to
hear some opinions about this work and if it's worth going forward.

Everything else should be pretty straightforward, so I'll skip explaining that.

Abel Vesa (7):
  arm: Add livepatch arch specific code
  arm: ftrace: Add call modify mechanism
  arm: module: Add apply_relocate_add
  arm: Add ftrace with regs support
  arm: ftrace: Add ARCH_SUPPORTS_FTRACE_OPS for ftrace with regs
  arm: Add livepatch to build if CONFIG_LIVEPATCH
  arm: Add livepatch necessary arch selects into Kconfig

 MAINTAINERS                      |  3 +++
 arch/arm/Kconfig                 |  4 ++++
 arch/arm/include/asm/ftrace.h    |  4 ++++
 arch/arm/include/asm/livepatch.h | 46 +++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/Makefile         |  1 +
 arch/arm/kernel/entry-ftrace.S   | 49 ++++++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/ftrace.c         | 21 +++++++++++++++++
 arch/arm/kernel/livepatch.c      | 43 +++++++++++++++++++++++++++++++++++
 arch/arm/kernel/module.c         |  9 ++++++++
 9 files changed, 180 insertions(+)
 create mode 100644 arch/arm/include/asm/livepatch.h
 create mode 100644 arch/arm/kernel/livepatch.c

-- 
2.7.4

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2017-01-18 13:54 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-06 17:06 [PATCH 0/7] arm: Add livepatch support Abel Vesa
2016-12-06 17:06 ` [PATCH 1/7] arm: Add livepatch arch specific code Abel Vesa
2017-01-16 16:47   ` Miroslav Benes
2017-01-17  0:22     ` Jessica Yu
2017-01-17  2:27       ` Jessica Yu
2017-01-17 13:53       ` Miroslav Benes
2016-12-06 17:06 ` [PATCH 2/7] arm: ftrace: Add call modify mechanism Abel Vesa
2016-12-07 10:37   ` kbuild test robot
2016-12-06 17:06 ` [PATCH 3/7] arm: module: Add apply_relocate_add Abel Vesa
2016-12-07  2:08   ` kbuild test robot
2017-01-17  4:49   ` Jessica Yu
2017-01-18 10:37     ` Miroslav Benes
2016-12-06 17:06 ` [PATCH 4/7] arm: Add ftrace with regs support Abel Vesa
2016-12-07  2:43   ` Steven Rostedt
2016-12-07 10:57   ` Russell King - ARM Linux
2016-12-07 11:58   ` Robin Murphy
2016-12-07 12:10     ` Abel Vesa
2016-12-06 17:06 ` [PATCH 5/7] arm: ftrace: Add ARCH_SUPPORTS_FTRACE_OPS for ftrace with regs Abel Vesa
2016-12-06 17:06 ` [PATCH 6/7] arm: Add livepatch to build if CONFIG_LIVEPATCH Abel Vesa
2016-12-07 15:05   ` Petr Mladek
2016-12-07 16:11     ` Abel Vesa
2017-01-18 12:36   ` Miroslav Benes
2016-12-06 17:06 ` [PATCH 7/7] arm: Add livepatch necessary arch selects into Kconfig Abel Vesa
2016-12-07  2:45   ` Steven Rostedt
2016-12-07  6:48   ` kbuild test robot
2017-01-18 12:40   ` Miroslav Benes
2017-01-18 13:35     ` Russell King - ARM Linux
2016-12-07  1:38 ` [PATCH 0/7] arm: Add livepatch support zhouchengming
2016-12-07 11:39   ` Abel Vesa
2016-12-07 15:19 ` Petr Mladek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).