[PATCH v3 11/11] powerpc: Remove runtime checking of jumps in core kernel feature-fixup
From: Shivang Upadhyay
Date: Wed Dec 02 2026 - 02:43:19 EST
Relative branches in the core kernel's alternative code are now fixed up
at build time by objtool (--alt-fixup), so skip the runtime translation
for them.
objtool does not process the VDSO or modules, so keep translating
branches for those. Add a 'translate' argument to the patching path:
do_feature_fixups() (used by modules and the VDSO) and the selftests
pass true, while the core kernel fixups pass false.
Now the fixup of alterate section is handled at boot time, We can remove
the runtime checking of jumps.
Co-developed-by: Nicholas Piggin <npiggin@xxxxxxxxx>
Signed-off-by: Nicholas Piggin <npiggin@xxxxxxxxx>
Co-developed-by: Sathvika Vasireddy <sv@xxxxxxxxxxxxx>
Signed-off-by: Sathvika Vasireddy <sv@xxxxxxxxxxxxx>
Signed-off-by: Shivang Upadhyay <shivangu@xxxxxxxxxxxxx>
---
arch/powerpc/lib/feature-fixups.c | 46 +++++++++++++++++++------------
1 file changed, 29 insertions(+), 17 deletions(-)
diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature-fixups.c
index 587c8cf1230f..f127f45d9331 100644
--- a/arch/powerpc/lib/feature-fixups.c
+++ b/arch/powerpc/lib/feature-fixups.c
@@ -51,14 +51,22 @@ static u32 *calc_addr(struct fixup_entry *fcur, long offset)
return (u32 *)((unsigned long)fcur + offset);
}
-static int patch_alt_instruction(u32 *src, u32 *dest, u32 *alt_start, u32 *alt_end)
+/*
+ * Relative branches in the alternative code of the core kernel have been
+ * fixed up for where they get copied to at build time, by objtool
+ * (--alt-fixup), so @translate is false for the core kernel. The VDSO and
+ * modules are not processed by objtool, so their relative branches still
+ * need to be translated at patch time.
+ */
+static int patch_alt_instruction(u32 *src, u32 *dest, u32 *alt_start,
+ u32 *alt_end, bool translate)
{
int err;
ppc_inst_t instr;
instr = ppc_inst_read(src);
- if (instr_is_relative_branch(ppc_inst_read(src))) {
+ if (translate && instr_is_relative_branch(ppc_inst_read(src))) {
u32 *target = (u32 *)branch_target(src);
/* Branch within the section doesn't need translating */
@@ -75,7 +83,7 @@ static int patch_alt_instruction(u32 *src, u32 *dest, u32 *alt_start, u32 *alt_e
}
static int patch_feature_section_mask(unsigned long value, unsigned long mask,
- struct fixup_entry *fcur)
+ struct fixup_entry *fcur, bool translate)
{
u32 *start, *end, *alt_start, *alt_end, *src, *dest;
@@ -95,7 +103,8 @@ static int patch_feature_section_mask(unsigned long value, unsigned long mask,
for (; src < alt_end; src = ppc_inst_next(src, src),
dest = ppc_inst_next(dest, dest)) {
- if (patch_alt_instruction(src, dest, alt_start, alt_end))
+ if (patch_alt_instruction(src, dest, alt_start, alt_end,
+ translate))
return 1;
}
@@ -106,7 +115,8 @@ static int patch_feature_section_mask(unsigned long value, unsigned long mask,
}
static void do_feature_fixups_mask(unsigned long value, unsigned long mask,
- void *fixup_start, void *fixup_end)
+ void *fixup_start, void *fixup_end,
+ bool translate)
{
struct fixup_entry *fcur, *fend;
@@ -114,7 +124,7 @@ static void do_feature_fixups_mask(unsigned long value, unsigned long mask,
fend = fixup_end;
for (; fcur < fend; fcur++) {
- if (patch_feature_section_mask(value, mask, fcur)) {
+ if (patch_feature_section_mask(value, mask, fcur, translate)) {
WARN_ON(1);
printk("Unable to patch feature section at %p - %p" \
" with %p - %p\n",
@@ -126,9 +136,10 @@ static void do_feature_fixups_mask(unsigned long value, unsigned long mask,
}
}
+/* Used for modules and the VDSO, whose branches must be translated. */
void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
{
- do_feature_fixups_mask(value, ~0, fixup_start, fixup_end);
+ do_feature_fixups_mask(value, ~0, fixup_start, fixup_end, true);
}
#ifdef CONFIG_PPC_BARRIER_NOSPEC
@@ -645,13 +656,13 @@ void __init apply_feature_fixups(void)
* Apply the CPU-specific and firmware specific fixups to kernel text
* (nop out sections not relevant to this CPU or this firmware).
*/
- do_feature_fixups(spec->cpu_features,
- PTRRELOC(&__start___ftr_fixup),
- PTRRELOC(&__stop___ftr_fixup));
+ do_feature_fixups_mask(spec->cpu_features, ~0,
+ PTRRELOC(&__start___ftr_fixup),
+ PTRRELOC(&__stop___ftr_fixup), false);
- do_feature_fixups(spec->mmu_features,
- PTRRELOC(&__start___mmu_ftr_fixup),
- PTRRELOC(&__stop___mmu_ftr_fixup));
+ do_feature_fixups_mask(spec->mmu_features, ~0,
+ PTRRELOC(&__start___mmu_ftr_fixup),
+ PTRRELOC(&__stop___mmu_ftr_fixup), false);
do_lwsync_fixups(spec->cpu_features,
PTRRELOC(&__start___lwsync_fixup),
@@ -659,8 +670,9 @@ void __init apply_feature_fixups(void)
#ifdef CONFIG_PPC64
saved_firmware_features = powerpc_firmware_features;
- do_feature_fixups(powerpc_firmware_features,
- &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
+ do_feature_fixups_mask(powerpc_firmware_features, ~0,
+ &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup,
+ false);
#endif
do_final_fixups();
}
@@ -672,7 +684,7 @@ void __init update_mmu_feature_fixups(unsigned long mask)
do_feature_fixups_mask(cur_cpu_spec->mmu_features, mask,
PTRRELOC(&__start___mmu_ftr_fixup),
- PTRRELOC(&__stop___mmu_ftr_fixup));
+ PTRRELOC(&__stop___mmu_ftr_fixup), false);
mmu_feature_keys_init();
}
@@ -711,7 +723,7 @@ late_initcall(check_features);
static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
{
- return patch_feature_section_mask(value, ~0, fcur);
+ return patch_feature_section_mask(value, ~0, fcur, true);
}
/* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
--
2.56.1