Implement inline static calls on PPC32 - v3
Subsystems (4)
v3 cover Implement inline static calls on PPC32 - v3
This series applies on top of the series v2 "objtool: Enable and implement --mcount option on powerpc" [1]
A few modifications are done to core parts to enable powerpc implementation:
- R_X86_64_PC32 is abstracted to R_REL32 so that it can then be redefined as R_PPC_REL32.
- A call to static_call_init() is added to start_kernel() to avoid every architecture to have to call it
- Trampoline address is provided to arch_static_call_transform() even when setting a site to fallback on a call to the trampoline when the target is too far.
[1] https://lore.kernel.org/all/[email protected]/T/
This version of the series includes a preliminary patch (patch 1) that should be squashed into Sathvika's series.
Christophe Leroy (6):
Fixup for "objtool/powerpc: Add --mcount specific implementation"
objtool: Add architecture specific R_REL32 macro
init: Call static_call_init() from start_kernel()
static_call_inline: Provide trampoline address when updating sites
powerpc: Prepare arch_static_call_transform() for supporting inline
static calls
powerpc/static_call: Implement inline static calls
arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/static_call.h | 2 + arch/powerpc/kernel/static_call.c | 58 ++++++++++++++----- arch/x86/kernel/static_call.c | 2 +- init/main.c | 1 + kernel/static_call_inline.c | 2 +- tools/objtool/arch/powerpc/decode.c | 39 +++++++++---- tools/objtool/arch/powerpc/include/arch/elf.h | 1 + tools/objtool/arch/x86/include/arch/elf.h | 1 + tools/objtool/check.c | 10 ++-- tools/objtool/orc_gen.c | 2 +- 11 files changed, 85 insertions(+), 34 deletions(-)
Signature · 1 line
2.37.1
PATCH v3 4/6 static_call_inline: Provide trampoline address when updating sites
In preparation of support of inline static calls on powerpc, provide trampoline address when updating sites, so that when the destination function is too far for a direct function call, the call site is patched with a call to the trampoline.
--- arch/x86/kernel/static_call.c | 2 +- kernel/static_call_inline.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/static_call.c +1 -1
@@ -113,7 +113,7 @@ void arch_static_call_transform(void *site, void *tramp, void *func, bool tail) {mutex_lock (&text_mutex); -if (tramp) { +if (tramp && !site) {__static_call_validate (tramp, true, true);__static_call_transform (tramp,__sc_insn (!func, true), func, false); }
kernel/static_call_inline.c +1 -1
@@ -195,7 +195,7 @@ void __static_call_update(struct static_call_key *key, void *tramp, void *func)continue ; } -arch_static_call_transform (site_addr,NULL , func, +arch_static_call_transform (site_addr, tramp, func,static_call_is_tail (site)); } }
Signature · 1 line
2.37.1
PATCH v3 3/6 init: Call static_call_init() from start_kernel()
In reply to Christophe LeroyCall static_call_init() just after jump_label_init().
x86 already called it from setup_arch(). This is not a problem as static_call_init() is guarded from double call.
--- init/main.c | 1 + 1 file changed, 1 insertion(+)
init/main.c +1 -0
@@ -963,6 +963,7 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)pr_notice ("Kernel command line: %s\n" , saved_command_line);/* parameters may set static keys */ jump_label_init (); +static_call_init ();parse_early_param (); after_dashes = parse_args("Booting kernel", static_command_line, __start___param,
Signature · 1 line
2.37.1
PATCH v3 1/6 Fixup for "objtool/powerpc: Add --mcount specific implementation"
In reply to Christophe LeroyMake arch_decode_instruction() more future proof and less error prone:
- Use local vars for type and imm so that GCC will detect when we forget to update them
- Handle imm outside the branch type check
- Adapt len for prefixed instructions
Handle the four branch types b, bl, ba, bla
Part of it should probably go into "objtool/powerpc: Enable objtool to be built on ppc" for instance the setup of the len. Maybe all the skeleton with the local vars, the switch with its default, etc ... Then following patches only have to add stuff inside the switch.
--- tools/objtool/arch/powerpc/decode.c | 39 +++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 11 deletions(-)
tools/objtool/arch/powerpc/decode.c +28 -11
@@ -50,31 +50,48 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec {u32 insn;unsigned int opcode; +unsigned long imm; +enum insn_type typ; - *immediate =0 ; insn =bswap_if_needed (file->elf, *(u32 *)(sec->data->d_buf + offset)); - *len =4 ; - *type =INSN_OTHER ; opcode = insn >>26 ;switch (opcode) { -case 18 :/* bl */ -if ((insn &3 ) ==1 ) { - *type =INSN_CALL ; - *immediate = insn &0x3fffffc ; -if (*immediate &0x2000000 ) - *immediate -=0x4000000 ; - } +case 18 :/* bl/b/bla/ba */ +if (insn &1 ) + typ =INSN_CALL ; +else + typ =INSN_JUMP_UNCONDITIONAL ; + + imm = insn &0x3fffffc ; +if (imm &0x2000000 ) + imm -=0x4000000 ; + imm |= insn &2 ;/* AA flag */ +break ; +default : + typ =INSN_OTHER ; + imm =0 ;break ; } +if (opcode ==1 ) + *len =8 ; +else + *len =4 ; + + *type = typ; + *immediate = imm; +return 0 ; }unsigned long arch_jump_destination (struct instruction *insn) { -return insn->offset + insn->immediate; +if (insn->immediate &2 ) +return insn->immediate & ~2 ; + +return insn->offset + insn->immediate; }void arch_initial_func_cfi_state (struct cfi_init_state *state)
Signature · 1 line
2.37.1
PATCH v3 2/6 objtool: Add architecture specific R_REL32 macro
In reply to Christophe LeroyIn order to allow other architectures than x86 to use 32 bits PC relative relocations (S+A-P), define a R_REL32 macro that each architecture will define, in the same way as already done for R_NONE, R_ABS32 and R_ABS64.
For x86 that corresponds to R_X86_64_PC32.
For powerpc it is R_PPC_REL32/R_PPC64_REL32.
---
v3: Added powerpc in the patch
v2: Improved commit message based on feedback from Segher
--- tools/objtool/arch/powerpc/include/arch/elf.h | 1 + tools/objtool/arch/x86/include/arch/elf.h | 1 + tools/objtool/check.c | 10 +++++----- tools/objtool/orc_gen.c | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-)
tools/objtool/arch/powerpc/include/arch/elf.h +1 -0
@@ -6,5 +6,6 @@#define R_NONE R_PPC_NONE#define R_ABS64 R_PPC64_ADDR64#define R_ABS32 R_PPC_ADDR32 +#define R_REL32 R_PPC_REL32/* R_PPC64_REL32 is identical */ #endif /* _OBJTOOL_ARCH_ELF */
tools/objtool/arch/x86/include/arch/elf.h +1 -0
@@ -4,5 +4,6 @@#define R_NONE R_X86_64_NONE#define R_ABS64 R_X86_64_64#define R_ABS32 R_X86_64_32 +#define R_REL32 R_X86_64_PC32#endif /* _OBJTOOL_ARCH_ELF */
tools/objtool/check.c +5 -5
@@ -655,7 +655,7 @@ static int create_static_call_sections(struct objtool_file *file)/* populate reloc for 'addr' */ if (elf_add_reloc_to_insn (file->elf, sec, idx *sizeof (struct static_call_site ), -R_X86_64_PC32 , +R_REL32 , insn->sec, insn->offset))return -1 ;
@@ -696,7 +696,7 @@ static int create_static_call_sections(struct objtool_file *file)/* populate reloc for 'key' */ if (elf_add_reloc (file->elf, sec, idx *sizeof (struct static_call_site ) +4 , -R_X86_64_PC32 , key_sym, +R_REL32 , key_sym,is_sibling_call (insn) *STATIC_CALL_SITE_TAIL ))return -1 ;
@@ -740,7 +740,7 @@ static int create_retpoline_sites_sections(struct objtool_file *file)if (elf_add_reloc_to_insn (file->elf, sec, idx *sizeof (int ), -R_X86_64_PC32 , +R_REL32 , insn->sec, insn->offset)) {WARN ("elf_add_reloc_to_insn: .retpoline_sites" );return -1 ;
@@ -838,7 +838,7 @@ static int create_ibt_endbr_seal_sections(struct objtool_file *file)if (elf_add_reloc_to_insn (file->elf, sec, idx *sizeof (int ), -R_X86_64_PC32 , +R_REL32 , insn->sec, insn->offset)) {WARN ("elf_add_reloc_to_insn: .ibt_endbr_seal" );return -1 ;
@@ -4004,7 +4004,7 @@ static int validate_ibt_insn(struct objtool_file *file, struct instruction *insncontinue ; off = reloc->sym->offset; -if (reloc->type ==R_X86_64_PC32 || reloc->type ==R_X86_64_PLT32 ) +if (reloc->type ==R_REL32 || reloc->type ==R_X86_64_PLT32 ) off +=arch_dest_reloc_offset (reloc->addend);else off += reloc->addend;
tools/objtool/orc_gen.c +1 -1
@@ -101,7 +101,7 @@ static int write_orc_entry(struct elf *elf, struct section *orc_sec, orc->bp_offset =bswap_if_needed (elf, orc->bp_offset);/* populate reloc for ip */ -if (elf_add_reloc_to_insn (elf, ip_sec, idx *sizeof (int ),R_X86_64_PC32 , +if (elf_add_reloc_to_insn (elf, ip_sec, idx *sizeof (int ),R_REL32 , insn_sec, insn_off))return -1 ;
Signature · 1 line
2.37.1
PATCH v3 6/6 powerpc/static_call: Implement inline static calls
In reply to Christophe LeroyImplement inline static calls:
- Put a 'bl' to the destination function ('b' if tail call)
- Put a 'nop' when the destination function is NULL ('blr' if tail call)
- Put a 'li r3,0' when the destination is the RET0 function and not a tail call.
If the destination is too far (over the 32Mb limit), go via the trampoline.
--- arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/static_call.h | 2 ++ arch/powerpc/kernel/static_call.c | 24 +++++++++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-)
arch/powerpc/Kconfig +1 -0
@@ -251,6 +251,7 @@ config PPC select HAVE_STACKPROTECTOR if PPC32 && $(cc-option,-mstack-protector-guard=tls -mstack-protector-guard-reg=r2) select HAVE_STACKPROTECTOR if PPC64 && $(cc-option,-mstack-protector-guard=tls -mstack-protector-guard-reg=r13) select HAVE_STATIC_CALL if PPC32 + select HAVE_STATIC_CALL_INLINE if PPC32 select HAVE_SYSCALL_TRACEPOINTS select HAVE_VIRT_CPU_ACCOUNTING select HUGETLB_PAGE_SIZE_VARIABLE if PPC_BOOK3S_64 && HUGETLB_PAGE
arch/powerpc/include/asm/static_call.h +2 -0
@@ -26,4 +26,6 @@#define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP (name) __PPC_SCT(name, "blr")#define ARCH_DEFINE_STATIC_CALL_RET0_TRAMP (name) __PPC_SCT(name, "b .+20") +#define CALL_INSN_SIZE 4 +#endif /* _ASM_POWERPC_STATIC_CALL_H */
arch/powerpc/kernel/static_call.c +23 -1
@@ -15,7 +15,29 @@ void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)mutex_lock (&text_mutex); -if (tramp) { +if (site && tail) { +if (!func) + err =patch_instruction (site,ppc_inst (PPC_RAW_BLR ())); +else if (is_ret0) + err =patch_branch (site, _ret0,0 ); +else if (is_short) + err =patch_branch (site, _func,0 ); +else if (tramp) + err =patch_branch (site, _tramp,0 ); +else + err =0 ; + }else if (site) { +if (!func) + err =patch_instruction (site,ppc_inst (PPC_RAW_NOP ())); +else if (is_ret0) + err =patch_instruction (site,ppc_inst (PPC_RAW_LI (_R3,0 ))); +else if (is_short) + err =patch_branch (site, _func,BRANCH_SET_LINK ); +else if (tramp) + err =patch_branch (site, _tramp,BRANCH_SET_LINK ); +else + err =0 ; + }else if (tramp) {if (func && !is_short) { err =patch_instruction (tramp +PPC_SCT_DATA ,ppc_inst (_func));if (err)
Signature · 1 line
2.37.1
PATCH v3 5/6 powerpc: Prepare arch_static_call_transform() for supporting inline static calls
In reply to Christophe LeroyReorganise arch_static_call_transform() in order to ease the support of inline static calls in following patch:
- remove 'target' to nhide whether it is a 'return 0' or not.
- Don't bail out if 'tramp' is NULL, just do nothing until next patch.
Note that 'target' was 'tramp + PPC_SCT_RET0', is_short was perforce true. So in the 'if (func && !is_short)' leg, target was perforce equal to 'func'.
---
v3: New. Split out of following patch.
--- arch/powerpc/kernel/static_call.c | 36 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-)
arch/powerpc/kernel/static_call.c +21 -15
@@ -8,26 +8,32 @@ void arch_static_call_transform(void *site, void *tramp, void *func, bool tail) {int err;bool is_ret0 = (func == __static_call_return0); -unsigned long target = (unsigned long )(is_ret0 ? tramp +PPC_SCT_RET0 : func); -bool is_short =is_offset_in_branch_range ((long )target - (long )tramp); - -if (!tramp) -return ; +unsigned long _tramp = (unsigned long )tramp; +unsigned long _func = (unsigned long )func; +unsigned long _ret0 = _tramp +PPC_SCT_RET0 ; +bool is_short =is_offset_in_branch_range ((long )func - (long )(site ? : tramp));mutex_lock (&text_mutex); -if (func && !is_short) { - err =patch_instruction (tramp +PPC_SCT_DATA ,ppc_inst (target)); -if (err) - goto out; +if (tramp) { +if (func && !is_short) { + err =patch_instruction (tramp +PPC_SCT_DATA ,ppc_inst (_func)); +if (err) + goto out; + } + +if (!func) + err =patch_instruction (tramp,ppc_inst (PPC_RAW_BLR ())); +else if (is_ret0) + err =patch_branch (tramp, _ret0,0 ); +else if (is_short) + err =patch_branch (tramp, _func,0 ); +else + err =patch_instruction (tramp,ppc_inst (PPC_RAW_NOP ())); + }else { + err =0 ; } -if (!func) - err =patch_instruction (tramp,ppc_inst (PPC_RAW_BLR ())); -else if (is_short) - err =patch_branch (tramp, target,0 ); -else - err =patch_instruction (tramp,ppc_inst (PPC_RAW_NOP ())); out:mutex_unlock (&text_mutex);
Signature · 1 line
2.37.1
Re: [PATCH v3 2/6] objtool: Add architecture specific R_REL32 macro
In reply to Christophe LeroyQuoted 9 lines · Christophe Leroy
In order to allow other architectures than x86 to use 32 bits PC relative relocations (S+A-P), define a
R_REL32macro that each architecture will define, in the same way as already done forR_NONE,R_ABS32andR_ABS64.For x86 that corresponds to
R_X86_64_PC32.
For powerpc it is R_PPC_REL32/R_PPC64_REL32.Signed-off-by Christophe Leroy <[email protected]>