mirror of https://lore.kernel.org/lkml/
 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 00/11] objtool/powerpc: Fix up feature section alternatives at build time
Date: Wed,  2 Dec 2026 13:10:47 +0530	[thread overview]
Message-ID: <20261202074059.899491-1-shivangu@linux.ibm.com> (raw)

The replacement code of a powerpc feature section (BEGIN_FTR_SECTION /
FTR_SECTION_ELSE / ALT_FTR_SECTION_END macros) is emitted into a __ftr_alt_*
section.  At boot, apply_feature_fixups() copies the replacement code over it.
Since the copy lands at a different address than the one it was linked for, the
PC-relative branches in it have to be translated, which is currently done at
runtime by patch_feature_section().

This has two drawbacks:

 - __ftr_alt_* sections are allocatable but not executable, so the
   linker cannot insert branch stubs for them.  They therefore have to
   be placed close to .text, and every branch in them must reach its
   target directly.  This can break the build as the kernel grows.

 - The branches are translated on every boot, although everything
   needed to do it is known at link time.

This series moves the translation to build time.  After vmlinux is
linked, objtool is run on it with the new --alt-fixup option.  It parses
the __ftr_fixup, __mmu_ftr_fixup and __fw_ftr_fixup tables, and for each
PC-relative relocation in the replacement code, asks the arch to rewrite
the instruction so that it is valid at the place it will be copied to.
With that, the __ftr_alt_* sections are marked executable and moved out
of .text into their own output section, so the linker can insert stubs
where needed.

Objtool does not process modules or the VDSO, so the runtime translation
is kept for those and only skipped for the core kernel.

Previously, Nicholas Piggin proposed a build-time solution using a custom
powerpc tool [1].  The approach here builds on that idea, but uses
objtool's existing ELF parsing and instruction patching infrastructure
instead of a separate tool.

Patch overview:

  1-3   Teach objtool about linked objects (sections with a non-zero
        address): make symbol and relocation offsets section relative,
        and keep the file layout when writing it back.
  4-6   Add --alt-fixup, built on top of the existing alternatives
        handling, and describe the powerpc feature fixup tables to it.
  7-8   Share the powerpc branch helpers between the kernel and
        objtool, and implement arch_support_alt_relocation() for
        powerpc.
  9-11  Make __ftr_alt_* executable, always build objtool for powerpc,
        run it post-link, and stop translating branches at boot for the
        core kernel.

This patchset applies atop upstream master branch.

Testing:
  verfied build and boot on ppc64le, ppc64, ppc32be.

[1] Original powerpc tool approach:
    http://patchwork.ozlabs.org/project/linuxppc-dev/patch/20170521010130.13552-1-npiggin@gmail.com/

---
===== Changelog ======

Changes since v2: https://lore.kernel.org/all/20260929055138.43489-1-shivangu@linux.ibm.com/
- Reworked after Peter Zijlstra's review: objtool now understands linked
  objects (sections with a non-zero address) instead of special casing
  --ftr-fixup.  Symbol and relocation offsets are made section relative
  (1-2/11), which replaces the find_insn() offset hack.
- ELF_F_LAYOUT is now set for every non-ET_REL file instead of only with
  --ftr-fixup (3/11).
- --ftr-fixup and its powerpc specific parsing are replaced
  by --alt-fixup, which reuses the generic alternatives code and
  arch_support_alt_relocation() (4-6/11, 8/11).  The hacks that skipped
  add_jump_destinations() and silenced intra-function call warnings are
  gone, objtool just stops after processing the alternatives.
- The powerpc branch helpers are shared between the kernel and objtool
  through code-patching-branch.c rather than duplicated (7/11).
- Dropped HAVE_OBJTOOL_FTR_FIXUP and the top level kbuild changes;
  ARCH_VMLINUX_NEEDS_RELOCS and a vmlinux.unstripped rule in
  Makefile.postlink are used instead (10/11).  The runtime branch
  translation is kept for modules and the VDSO (11/11).


v1: https://lore.kernel.org/all/20260505084628.17940-1-sv@linux.ibm.com/

=======================

Shivang Upadhyay (11):
  objtool: Make symbol offsets section relative in linked objects
  objtool: Make relocation offsets section relative in linked objects
  objtool: Keep the layout of linked objects when writing them back
  objtool: Pass the objtool_file to arch_support_alt_relocation()
  objtool: Add --alt-fixup
  objtool: Describe special_entry parsing for --alt-fixup
  powerpc/lib: Extract branch patching helpers to code-patching-branch.c
  objtool/powerpc: Implement arch_support_alt_relocation()
  powerpc: Mark feature section alternatives as executable
  powerpc: Always build objtool for powerpc
  powerpc: Remove runtime checking of jumps in core kernel feature-fixup

 arch/powerpc/Kconfig                          |   1 +
 arch/powerpc/Makefile                         |   2 +-
 arch/powerpc/Makefile.postlink                |   4 +
 arch/powerpc/include/asm/feature-fixups.h     |   2 +-
 arch/powerpc/include/asm/text-patching.h      |   3 +
 arch/powerpc/kernel/vmlinux.lds.S             |   7 +-
 arch/powerpc/lib/code-patching-branch.c       | 128 ++++++++++++++++++
 arch/powerpc/lib/code-patching.c              | 110 +--------------
 arch/powerpc/lib/feature-fixups.c             |  46 ++++---
 tools/arch/powerpc/include/asm/inst.h         |  41 ++++++
 .../arch/powerpc/include/asm/text-patching.h  |  67 +++++++++
 tools/objtool/arch/loongarch/special.c        |   3 +-
 tools/objtool/arch/powerpc/decode.c           |   6 +-
 .../arch/powerpc/include/arch/special.h       |  16 +++
 tools/objtool/arch/powerpc/special.c          |  42 +++++-
 tools/objtool/arch/x86/special.c              |   3 +-
 tools/objtool/builtin-check.c                 |   4 +-
 tools/objtool/check.c                         |   7 +-
 tools/objtool/elf.c                           |  13 ++
 tools/objtool/include/objtool/builtin.h       |   1 +
 tools/objtool/include/objtool/elf.h           |  14 +-
 tools/objtool/include/objtool/special.h       |   3 +-
 tools/objtool/special.c                       |  80 ++++++++++-
 23 files changed, 456 insertions(+), 147 deletions(-)
 create mode 100644 arch/powerpc/lib/code-patching-branch.c
 create mode 100644 tools/arch/powerpc/include/asm/inst.h
 create mode 100644 tools/arch/powerpc/include/asm/text-patching.h

-- 
2.56.1


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

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  7:40 Shivang Upadhyay [this message]
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 ` [PATCH v3 11/11] powerpc: Remove runtime checking of jumps in core kernel feature-fixup Shivang Upadhyay

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-1-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®