Inserts a direct branch to the stub target when possible, replacing the
mtctr/btctr sequence.

The load into r12 could potentially be skipped too, but that change
would need to refactor the arguments to indicate that the address
does not have a separate local entry point.

This helps the static call implementation, where modules calling their
own trampolines are called through this stub and the trampoline is
easily within range of a direct branch.

Signed-off-by: Benjamin Gray <bg...@linux.ibm.com>
---
 arch/powerpc/kernel/module_64.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_64.c
index 3656476097c2..03ab28d86008 100644
--- a/arch/powerpc/kernel/module_64.c
+++ b/arch/powerpc/kernel/module_64.c
@@ -432,8 +432,17 @@ static inline int create_stub(const Elf64_Shdr *sechdrs,
                return create_ftrace_stub(entry, addr, me);
 
        for (i = 0; i < ARRAY_SIZE(ppc64_stub_insns); i++) {
-               if (patch_instruction(&entry->jump[i],
-                                     ppc_inst(ppc64_stub_insns[i])))
+               ppc_inst_t inst = ppc_inst(ppc64_stub_insns[i]);
+
+               // Replace the indirect branch with a direct branch where 
possible
+               if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2) && i == 4) {
+                       ppc_inst_t direct;
+                       if (create_branch(&direct, (void*) entry + (i * 4), 
addr, 0) == 0) {
+                               inst = direct;
+                       }
+               }
+
+               if (patch_instruction(&entry->jump[i], inst))
                        return 0;
        }
 
-- 
2.37.3

Reply via email to