Skip to contentSkip to conversation

Implement inline static calls on PPC32 - v4

PATCH Applied Christophe Leroy 4 participants 9 messages Dec 3, 2024 – Mar 2, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v4 cover Implement inline static calls on PPC32 - v4

This series implements inline static calls on PPC32.

First patch adds to static_call core the ability to pass the trampoline address at the same time as the site address to arch_static_call_transform() so that it can fallback on branching to the trampoline when the site is too far (Max distance for direct branch is 32 Mbytes on powerpc).

Second patch adds support for decoding all types of uncond branches.

Third patch rearranges function arch_static_call_transform() to ease implementation of inline static call in following patch.

Last patch implements inline static calls on PPC32: This is done by:
- 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 thanks to patch 1.

Christophe Leroy (4):
static_call_inline: Provide trampoline address when updating sites objtool/powerpc: Add support for decoding all types of uncond branches
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 +-
 kernel/static_call_inline.c            |  2 +-
 tools/objtool/arch/powerpc/decode.c    |  8 +++-
 6 files changed, 55 insertions(+), 18 deletions(-)
Signature · 1 line
2.47.0
#1 lore raw

PATCH v4 1/4 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
index 4eefaac64c6c..00b2ea40cbef 100644
@@ -158,7 +158,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
index 5259cda486d0..7fefbb3d8074 100644
@@ -206,7 +206,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.47.0
#2 lore raw

PATCH v4 2/4 objtool/powerpc: Add support for decoding all types of uncond branches

In reply to Christophe Leroy

Add support for 'bla' instruction.

This is done by 'flagging' the address as an absolute address so that arch_jump_destination() can calculate it as expected. Because code is _always_ 4 bytes aligned, use bit 30 as flag.

Also add support for 'b' and 'ba' instructions. Objtool call them jumps.

---
 tools/objtool/arch/powerpc/decode.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/objtool/arch/powerpc/decode.c +7 -1
index 53b55690f320..c1228fef3dec 100644
@@ -55,12 +55,15 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
 
 	switch (opcode) {
 	case 18: /* b[l][a] */
-		if ((ins & 3) == 1) /* bl */
+		if (ins & 1)	/* bl[a] */
 			typ = INSN_CALL;
+		else		/* b[a] */
+			typ = INSN_JUMP_UNCONDITIONAL;
 
 		imm = ins & 0x3fffffc;
 		if (imm & 0x2000000)
 			imm -= 0x4000000;
+		imm |= ins & 2;	/* AA flag */
 		break;
 	}
 
@@ -77,6 +80,9 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
 
 unsigned long arch_jump_destination(struct instruction *insn)
 {
+	if (insn->immediate & 2)
+		return insn->immediate & ~2;
+
 	return insn->offset + insn->immediate;
 }
 
Signature · 1 line
2.47.0
#5 lore raw

PATCH v4 3/4 powerpc: Prepare arch_static_call_transform() for supporting inline static calls

In reply to Christophe Leroy

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

---
 arch/powerpc/kernel/static_call.c | 36 ++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 15 deletions(-)
arch/powerpc/kernel/static_call.c +21 -15
index 7cfd0710e757..1b106fbcc567 100644
@@ -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_ulong(tramp + PPC_SCT_DATA, target);
-		if (err)
-			goto out;
+	if (tramp) {
+		if (func && !is_short) {
+			err = patch_ulong(tramp + PPC_SCT_DATA, _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.47.0
#6 lore raw

PATCH v4 4/4 powerpc/static_call: Implement inline static calls

In reply to Christophe Leroy

Implement 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
index a0ce777f9706..285bc1ae17dd 100644
@@ -282,6 +282,7 @@ config PPC
 	select HAVE_STACKPROTECTOR		if PPC32 && $(cc-option,$(m32-flag) -mstack-protector-guard=tls -mstack-protector-guard-reg=r2 -mstack-protector-guard-offset=0)
 	select HAVE_STACKPROTECTOR		if PPC64 && $(cc-option,$(m64-flag) -mstack-protector-guard=tls -mstack-protector-guard-reg=r13 -mstack-protector-guard-offset=0)
 	select HAVE_STATIC_CALL			if PPC32
+	select HAVE_STATIC_CALL_INLINE		if PPC32
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_VIRT_CPU_ACCOUNTING
 	select HAVE_VIRT_CPU_ACCOUNTING_GEN
arch/powerpc/include/asm/static_call.h +2 -0
index de1018cc522b..e3d5d3823dac 100644
@@ -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
index 1b106fbcc567..ec3101f95e53 100644
@@ -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_ulong(tramp + PPC_SCT_DATA, _func);
 			if (err)
Signature · 1 line
2.47.0
Reviewed-by on v4 2/4
#3 lore raw

Re: [PATCH v4 2/4] objtool/powerpc: Add support for decoding all types of uncond branches

In reply to Christophe Leroy
Quoted 5 lines · Christophe Leroy

Add support for 'bla' instruction.

This is done by 'flagging' the address as an absolute address so that arch_jump_destination() can calculate it as expected. Because code is _always_ 4 bytes aligned, use bit 30 as flag.

The AA field already is there, so why not, eh :-)

Quoted 3 lines

Also add support for 'b' and 'ba' instructions. Objtool call them jumps.

Quoted 6 lines
tools/objtool/arch/powerpc/decode.c +0 -0
@@ -55,12 +55,15 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
 
 	switch (opcode) {
 	case 18: /* b[l][a] */

tools/objtool/arch/powerpc/decode.c int arch_decode_instruction(struct objtool_file *file, const struct section *sec · line 66

Quoted 10 lines
tools/objtool/arch/powerpc/decode.c +4 -1
-		if ((ins & 3) == 1) /* bl */
+		if (ins & 1)	/* bl[a] */
 			typ = INSN_CALL;
+		else		/* b[a] */
+			typ = INSN_JUMP_UNCONDITIONAL;
 
 		imm = ins & 0x3fffffc;
 		if (imm & 0x2000000)
 			imm -= 0x4000000;
+		imm |= ins & 2;	/* AA flag */

You could of course put that together with the 3fffffc thing, but you can leave that to the compiler as well :-)

Segher

#7 lore raw

Re: [PATCH v4 4/4] powerpc/static_call: Implement inline static calls

In reply to Christophe Leroy
kernel test robot noticed the following build warnings:

Hi Christophe,

kernel test robot noticed the following build warnings:

[auto build test WARNING on powerpc/next]
[also build test WARNING on powerpc/fixes linus/master v6.13-rc1 next-20241204] [cannot apply to tip/x86/core]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Christophe-Leroy/static_call_inline-Provide-trampoline-address-when-updating-sites/20241204-120612
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
patch link:    https://lore.kernel.org/r/3dbd0b2ba577c942729235d0211d04a406653d81.1733245362.git.christophe.leroy%40csgroup.eu
patch subject: [PATCH v4 4/4] powerpc/static_call: Implement inline static calls
config: powerpc-randconfig-r062-20241204 (https://download.01.org/0day-ci/archive/20241205/[email protected]/config)
compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project 592c0fe55f6d9a811028b5f3507be91458ab2713)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241205/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from arch/powerpc/platforms/powermac/bootx_init.c:18:
In file included from arch/powerpc/include/asm/io.h:24:
In file included from include/linux/mm.h:2223:
include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]

     518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   1 warning generated.
>> arch/powerpc/platforms/powermac/bootx_init.o: warning: objtool: bootx_init+0x28: unannotated intra-function call
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

PATCH v5 2/4 objtool/powerpc: Add support for decoding all types of uncond branches

In reply to Christophe Leroy

Add support for 'bla' instruction.

This is done by 'flagging' the address as an absolute address so that arch_jump_destination() can calculate it as expected. Because code is _always_ 4 bytes aligned, use bit 30 as flag.

Also add support for 'b' and 'ba' instructions. Objtool call them jumps.

And make sure the special 'bl .+4' used by clang in relocatable code is not seen as an 'unannotated intra-function call'. clang should use the special 'bcl 20,31,.+4' form like gcc but for the time being it does not so lets work around that.

---
v5: Add a special case for clang to ignore 'bl .+4' form.

---
 tools/objtool/arch/powerpc/decode.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
tools/objtool/arch/powerpc/decode.c +9 -1
index 53b55690f320..26d5050424a9 100644
@@ -55,12 +55,17 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
 
 	switch (opcode) {
 	case 18: /* b[l][a] */
-		if ((ins & 3) == 1) /* bl */
+		if (ins == 0x48000005)	/* bl .+4 */
+			typ = INSN_OTHER;
+		else if (ins & 1)	/* bl[a] */
 			typ = INSN_CALL;
+		else		/* b[a] */
+			typ = INSN_JUMP_UNCONDITIONAL;
 
 		imm = ins & 0x3fffffc;
 		if (imm & 0x2000000)
 			imm -= 0x4000000;
+		imm |= ins & 2;	/* AA flag */
 		break;
 	}
 
@@ -77,6 +82,9 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
 
 unsigned long arch_jump_destination(struct instruction *insn)
 {
+	if (insn->immediate & 2)
+		return insn->immediate & ~2;
+
 	return insn->offset + insn->immediate;
 }
 
Signature · 1 line
2.47.0
maintainer Applied
#8 lore raw
In reply to Christophe Leroy
Quoted 9 lines · Christophe Leroy

This series implements inline static calls on PPC32.

First patch adds to static_call core the ability to pass the trampoline address at the same time as the site address to arch_static_call_transform() so that it can fallback on branching to the trampoline when the site is too far (Max distance for direct branch is 32 Mbytes on powerpc).

[...]

Applied to powerpc/next.

[1/4] static_call_inline: Provide trampoline address when updating sites

      https://git.kernel.org/powerpc/c/d856bc3ac7d9ca88b3f52d8e08e58ce892dc3ce1
[2/4] objtool/powerpc: Add support for decoding all types of uncond branches
      https://git.kernel.org/powerpc/c/bb7f054f4de260dc14813230cfe4ca7299647b6e
[3/4] powerpc: Prepare arch_static_call_transform() for supporting inline static calls
      https://git.kernel.org/powerpc/c/6626f98ed55a7a20b1852e7d263a96d8f5a1b59f
[4/4] powerpc/static_call: Implement inline static calls
      https://git.kernel.org/powerpc/c/f50b45626e053dc10792c680cabbbadbf8c001e7

Thanks