Skip to contentSkip to conversation

Implement inline static calls on PPC32

RFC PATCH Superseded Christophe Leroy 3 participants 8 messages May 25, 2022

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Implement inline static calls on PPC32

This is first draft for implementing inline static calls on PPC32.

This series applies on top of the series v2 "objtool: Enable and implement --mcount option on powerpc"

For the time being only the case where functions are within 'bl' reach is supported. Otherwise panic() is invoked.

For the other case, we'll need to use the trampoline we have at startup before initialising inline static calls. But it seems that at the time being once inline static calls are initialised we don't know anymore where the trampoline was.
We'd need to keep the information somewhere (is the static_call_key ?) We may also need to keep the information for when the trampoline itself is out of 'bl' reach, in that case there is a trampoline setup by the compiler and we'll need to remind the location of that trampoline. Guess it should get saved somewhere when we initialise inline static calls ?

Christophe Leroy (4):
Revert "objtool: Enable objtool to run only on files with ftrace
enabled"
objtool: Add R_REL32 macro
static_call: Call static_call_init() from start_kernel() 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             | 41 ++++++++++++-------
 init/main.c                                   |  1 +
 scripts/Makefile.build                        |  4 +-
 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 +-
 9 files changed, 41 insertions(+), 22 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

RFC PATCH 1/4 Revert "objtool: Enable objtool to run only on files with ftrace enabled"

This reverts commit cf3013dfad89ad5ac7d16d56dced72d7c138a20e.

That commit is problematic as we miss some static calls.

---
 scripts/Makefile.build | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/Makefile.build +2 -2
index 06ceffd92921..2e0c3f9c1459 100644
@@ -258,8 +258,8 @@ else
 # 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
 # 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
 
-$(obj)/%.o: objtool-enabled = $(and $(if $(filter-out y%, $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n),y),        \
-        $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)),y),y)
+$(obj)/%.o: objtool-enabled = $(if $(filter-out y%, \
+	$(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n),y)
 
 endif
 
Signature · 1 line
2.35.3
maintainer
#2 lore raw

Re: [RFC PATCH v1 1/4] Revert "objtool: Enable objtool to run only on files with ftrace enabled"

Quoted 3 lines · Christophe Leroy

This reverts commit cf3013dfad89ad5ac7d16d56dced72d7c138a20e.

That commit is problematic as we miss some static calls.

Revert ?!?! who comitted this. And there's a ton more broken than just static calls. This must absolutely not be.

#3 lore raw

Re: [RFC PATCH v1 1/4] Revert "objtool: Enable objtool to run only on files with ftrace enabled"

Le 25/05/2022 à 18:34, Peter Zijlstra a écrit :

Quoted 7 lines · Christophe Leroy
Quoted 3 lines

This reverts commit cf3013dfad89ad5ac7d16d56dced72d7c138a20e.

That commit is problematic as we miss some static calls.

Revert ?!?! who comitted this. And there's a ton more broken than just static calls. This must absolutely not be.

No worry, it is just a follow-up of my previous series which includes it.

#4 lore raw

RFC PATCH 2/4 objtool: Add R_REL32 macro

In reply to Christophe Leroy

In order to allow other architectures than x86 to use 32 bits relative relocations, define a R_REL32 macro that each architecture will define, in the same way as already done for R_NONE.

---
 tools/objtool/arch/x86/include/arch/elf.h |  1 +
 tools/objtool/check.c                     | 10 +++++-----
 tools/objtool/orc_gen.c                   |  2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
tools/objtool/arch/x86/include/arch/elf.h +1 -0
index 69cc4264b28a..8aa8c29607da 100644
@@ -2,5 +2,6 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_X86_64_NONE
+#define R_REL32	R_X86_64_PC32
 
 #endif /* _OBJTOOL_ARCH_ELF */
tools/objtool/check.c +5 -5
index 70be5a72e838..1627d14a01c9 100644
@@ -650,7 +650,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;
 
@@ -691,7 +691,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;
 
@@ -735,7 +735,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;
@@ -787,7 +787,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;
@@ -3716,7 +3716,7 @@ static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn
 			continue;
 
 		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
index 1f22b7ebae58..49a877b9c879 100644
@@ -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.35.3
#5 lore raw

Re: [RFC PATCH v1 2/4] objtool: Add R_REL32 macro

Hi!

Quoted 3 lines · Christophe Leroy

In order to allow other architectures than x86 to use 32 bits relative relocations, define a R_REL32 macro that each architecture will define, in the same way as already done for R_NONE.

What are the expected semantics of this relocation? It is PC-relative, sure, but what is the destination? S+A-P always? That works for both x86-64 and for PowerPC, but it should be written doen somewhere :-)

Segher

#6 lore raw

RFC PATCH 3/4 static_call: Call static_call_init() from start_kernel()

In reply to Christophe Leroy

Call 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
index 98182c3c2c4b..b6c49c18ec5d 100644
@@ -962,6 +962,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.35.3
#7 lore raw

RFC PATCH 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
- Put a 'nop' when the destination function is NULL
- Put a 'li r3,0' when the destination is the RET0 function

For the time being it only works if the destination is within 32Mb from the caller.

---
 arch/powerpc/Kconfig                          |  1 +
 arch/powerpc/include/asm/static_call.h        |  2 +
 arch/powerpc/kernel/static_call.c             | 41 ++++++++++++-------
 tools/objtool/arch/powerpc/include/arch/elf.h |  1 +
 4 files changed, 31 insertions(+), 14 deletions(-)
arch/powerpc/Kconfig +1 -0
index 5ef8bf8eb202..3257a1c258d8 100644
@@ -246,6 +246,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
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 +27 -14

52 lines not on this page · raw message

tools/objtool/arch/powerpc/include/arch/elf.h +1 -0
index 3c8ebb7d2a6b..18784c764c14 100644
@@ -4,5 +4,6 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_PPC_NONE
+#define R_REL32	R_PPC_REL32
 
 #endif /* _OBJTOOL_ARCH_ELF */
Signature · 1 line
2.35.3