[PATCH] kernel/module_64.c: Add REL24 relocation support of livepatch symbols

Naveen N . Rao naveen.n.rao at linux.vnet.ibm.com
Tue Oct 3 20:30:29 AEDT 2017


Hi Kamalesh,

On 2017/10/03 05:36AM, Kamalesh Babulal wrote:
> With commit 425595a7fc20 ("livepatch: reuse module loader code to
> write relocations") livepatch uses module loader to write relocations
> of livepatch symbols, instead of managing them by arch-dependent
> klp_write_module_reloc() function.
> 
> livepatch module managed relocation entries are written to sections
> marked with SHF_RELA_LIVEPATCH flag and livepatch symbols within the
> section are marked with SHN_LIVEPATCH symbol section index. When the
> livepatching module is loaded, the livepatch symbols are resolved
> before calling apply_relocate_add() to apply the relocations.
> 
> R_PPC64_REL24 relocation type resolves to a function address, those may
> be local to the livepatch module or available in kernel/other modules.
> For every such non-local function, apply_relocate_add() constructs a stub
> (a.k.a trampoline) to branch to a function. Stub code is responsible
> to save toc onto the stack, before calling the function via the global
> entry point. A NOP instruction is expected after every non local function
> branch, i.e. after the REL24 relocation. Which in-turn is replaced by
> toc restore instruction by apply_relocate_add().
> 
> livepatch symbols with R_PPC64_REL24 relocation type, may not be
> reachable within current TOC range or might not have a NOP instruction
> following a branch. Latter symbols are the local symbols, whose became
> global to the livepatched function. As per ABIv2, local functions are
> accessed via local entry point, which is relative to current module's
> toc value.
> 
> For example, consider the following livepatch relocations (the example is
> from livepatch module generated by kpatch tool):
> 
> Relocation section '.klp.rela.vmlinux..text.meminfo_proc_show' at offset 0x84530 contains 44 entries:
>     Offset             Info             Type      Symbol's Value   Symbol's Name + Addend
> 0000000000000054  000000560000000a R_PPC64_REL24  0000000000000000 .klp.sym.vmlinux.si_swapinfo,0 + 0
> 000000000000007c  000000570000000a R_PPC64_REL24  0000000000000000 .klp.sym.vmlinux.total_swapcache_pages,0 + 0
> 00000000000000e8  000000580000000a R_PPC64_REL24  0000000000000000 .klp.sym.vmlinux.show_val_kb,1 + 0
> [...]
> 
> 1. .klp.sym.vmlinux.si_swapinfo and .klp.sym.vmlinux.total_swapcache_pages
>    are not available within the livepatch module TOC range.
> 
> 2. .klp.sym.vmlinux.show_val_kb livepatch symbol was previously local
>    but now global to fs/proc/meminfo.c::meminfo_proc_show().
> 
> While the livepatch module is loaded the livepatch symbols mentioned in
> case 1 will fail with an error:
> [   74.485405] module_64: kpatch_meminfo_string: REL24 -1152921504751525976 out of range!
> 
> and livepatch symbols mentioned in case 2 with fail with an error:
> [   24.568425] module_64: kpatch_meminfo_string: Expect noop after relocate, got 3d220000
> 
> Both the failures with REL24 livepatch symbols relocation, can be resolved
> by constructing a new livepatch stub. The newly setup klp_stub mimics the
> functionality of entry_64.S::livepatch_handler introduced by commit
> 85baa095497f ("powerpc/livepatch: Add live patching support on ppc64le").
> 
> Which introduces a "livepatch stack" growing upwards from the base of
> the regular stack and is used to store/restore TOC/LR values, other than
> the stub setup and branch. The additional instructions sequences to handle
> klp_stub increases the stub size and current ppc64_stub_insn[] is not
> sufficient to hold them. This patch also introduces new
> ppc64le_klp_stub_entry[], along with the helpers to find/allocate
> livepatch stub.
> 
> Signed-off-by: Kamalesh Babulal <kamalesh at linux.vnet.ibm.com>
> Cc: Balbir Singh <bsingharora at gmail.com>
> Cc: Naveen N. Rao <naveen.n.rao at linux.vnet.ibm.com>
> Cc: Josh Poimboeuf <jpoimboe at redhat.com>
> Cc: Jessica Yu <jeyu at kernel.org>
> Cc: Ananth N Mavinakayanahalli <ananth at linux.vnet.ibm.com>
> Cc: Aravinda Prasad <aravinda at linux.vnet.ibm.com>
> Cc: linuxppc-dev at lists.ozlabs.org
> Cc: live-patching at vger.kernel.org
> ---
>  arch/powerpc/include/asm/module.h              |   4 +
>  arch/powerpc/kernel/module_64.c                | 119 ++++++++++++++++++++++++-
>  arch/powerpc/kernel/trace/ftrace_64_mprofile.S |  77 ++++++++++++++++
>  3 files changed, 197 insertions(+), 3 deletions(-)
> 
 
[snip]

A few small nits focusing on just the trampoline...

> diff --git a/arch/powerpc/kernel/trace/ftrace_64_mprofile.S b/arch/powerpc/kernel/trace/ftrace_64_mprofile.S
> index c98e90b..708a96d 100644
> --- a/arch/powerpc/kernel/trace/ftrace_64_mprofile.S
> +++ b/arch/powerpc/kernel/trace/ftrace_64_mprofile.S
> @@ -249,6 +249,83 @@ livepatch_handler:
> 
>  	/* Return to original caller of live patched function */
>  	blr
> +
> +	/*
> +	 * This livepatch stub code, called from livepatch module to jump into
> +	 * kernel or other modules. It replicates the livepatch_handler code,
> +	 * with an expection of jumping to the trampoline instead of patched
> +	 * function.
> +	 */
> +	.global klp_stub_insn
> +klp_stub_insn:
> +	CURRENT_THREAD_INFO(r12, r1)
> +
> +	/* Allocate 3 x 8 bytes */
> +	ld      r11, TI_livepatch_sp(r12)
> +	addi    r11, r11, 24
> +	std     r11, TI_livepatch_sp(r12)
> +
> +	/* Save toc & real LR on livepatch stack */
> +	std     r2,  -24(r11)
> +	mflr    r12
> +	std     r12, -16(r11)
> +
> +	/* Store stack end marker */
> +	lis     r12, STACK_END_MAGIC at h
> +	ori     r12, r12, STACK_END_MAGIC at l
> +	std     r12, -8(r11)

Seeing as this is the same as livepatch_handler() except for this part 
in the middle, does it make sense to reuse livepatch_handler() with 
appropriate labels added for your use?  You could patch in the below 5 
instructions using the macros from ppc-opcode.h...

> +
> +	/*
> +	 * Stub memory is allocated dynamically, during the module load.
> +	 * Load TOC relative address into r11. module_64.c::klp_stub_for_addr()
> +	 * identifies the available free stub slot and loads the address into
> +	 * r11 with two instructions.
> +	 *
> +	 * addis r11, r2, stub_address at ha
> +	 * addi  r11, r11, stub_address at l
> +	 */
> +	.global klp_stub_entry
> +klp_stub_entry:
> +	addis   r11, r2, 0
> +	addi    r11, r11, 0
> +
> +	/* Load the r12 with called function address from entry->funcdata */
> +	ld      r12, 128(r11)
> +
> +	/* Move r12 into ctr for global entry and branch there */
> +	mtctr   r12
> +	bctrl
> +
> +	/*
> +	 * Now we are returning to the patched function. We are free to
> +	 * use r11, r12 and we can use r2 until we restore it.
> +	 */
> +	CURRENT_THREAD_INFO(r12, r1)
> +
> +	ld      r11, TI_livepatch_sp(r12)
> +
> +	/* Check stack marker hasn't been trashed */
> +	lis     r2,  STACK_END_MAGIC at h
> +	ori     r2,  r2, STACK_END_MAGIC at l
> +	ld      r12, -8(r11)
> +2:	tdne    r12, r2
> +	EMIT_BUG_ENTRY 2b, __FILE__, __LINE__ - 1, 0

If you plan to keep this trampoline separate from livepatch_handler(), 
note that the above bug entry is not required since you copy only the 
text of this trampoline elsewhere and you won't have an associated bug 
entry for that new stub address.

- Naveen



More information about the Linuxppc-dev mailing list