All of lore.kernel.org
 help / color / mirror / Atom feed
From: Shivang Upadhyay <shivangu@linux.ibm.com>
To: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org
Cc: maddy@linux.ibm.com, mpe@ellerman.id.au, npiggin@gmail.com,
	chleroy@kernel.org, ritesh.list@gmail.com, sshegde@linux.ibm.com,
	jpoimboe@kernel.org, peterz@infradead.org,
	shivangu@linux.ibm.com, ardb@kernel.org, will@kernel.org,
	sv@linux.ibm.com, joe.lawrence@redhat.com, song@kernel.org,
	mingo@kernel.org, mbenes@suse.cz
Subject: [PATCH v3 11/11] powerpc: Remove runtime checking of jumps in core kernel feature-fixup
Date: Wed,  2 Dec 2026 13:10:58 +0530	[thread overview]
Message-ID: <20261202074059.899491-12-shivangu@linux.ibm.com> (raw)
In-Reply-To: <20261202074059.899491-1-shivangu@linux.ibm.com>

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@gmail.com>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Co-developed-by: Sathvika Vasireddy <sv@linux.ibm.com>
Signed-off-by: Sathvika Vasireddy <sv@linux.ibm.com>
Signed-off-by: Shivang Upadhyay <shivangu@linux.ibm.com>
---
 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


      parent reply	other threads:[~2026-12-02  7:43 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 01/11] objtool: Make symbol offsets section relative in linked objects Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 02/11] objtool: Make relocation " Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 03/11] objtool: Keep the layout of linked objects when writing them back Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 04/11] objtool: Pass the objtool_file to arch_support_alt_relocation() Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 05/11] objtool: Add --alt-fixup Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 06/11] objtool: Describe special_entry parsing for --alt-fixup Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 07/11] powerpc/lib: Extract branch patching helpers to code-patching-branch.c Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 08/11] objtool/powerpc: Implement arch_support_alt_relocation() Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 09/11] powerpc: Mark feature section alternatives as executable Shivang Upadhyay
2026-12-02  7:40 ` [PATCH v3 10/11] powerpc: Always build objtool for powerpc Shivang Upadhyay
2026-12-02  7:40 ` Shivang Upadhyay [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202074059.899491-12-shivangu@linux.ibm.com \
    --to=shivangu@linux.ibm.com \
    --cc=ardb@kernel.org \
    --cc=chleroy@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mbenes@suse.cz \
    --cc=mingo@kernel.org \
    --cc=mpe@ellerman.id.au \
    --cc=npiggin@gmail.com \
    --cc=peterz@infradead.org \
    --cc=ritesh.list@gmail.com \
    --cc=song@kernel.org \
    --cc=sshegde@linux.ibm.com \
    --cc=sv@linux.ibm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.