Live Patching
 help / color / mirror / Atom feed
From: Dylan Hatch <dylanbhatch@google.com>
To: Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	 Mark Rutland <mark.rutland@arm.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
	"David S . Miller" <davem@davemloft.net>,
	 Nathan Chancellor <nathan@kernel.org>,
	Nicolas Schier <nsc@kernel.org>,
	 Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	 Justin Stitt <justinstitt@google.com>,
	Weinan Liu <wnliu@google.com>,  Jens Remus <jremus@linux.ibm.com>,
	linux-arm-kernel@lists.infradead.org,
	 linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org,
	 linux-crypto@vger.kernel.org, llvm@lists.linux.dev,
	 Josh Poimboeuf <jpoimboe@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	 Ard Biesheuvel <ardb@kernel.org>,
	Indu Bhagat <ibhagatgnu@gmail.com>,
	 Prasanna Kumar T S M <ptsm@linux.microsoft.com>,
	live-patching@vger.kernel.org,
	 Dylan Hatch <dylanbhatch@google.com>
Subject: [RFC PATCH 00/12] arm64, unwind: add .lr_unwind unwinder for reliable stacktrace
Date: Tue, 17 Nov 2026 04:34:52 +0000	[thread overview]
Message-ID: <20261117043504.1021063-1-dylanbhatch@google.com> (raw)

Implement a build-time .eh_frame-to-.lr_unwind table generator and
in-kernel unwinder for arm64. The primary goal is to support reliable
stacktrace (arch_stack_walk_reliable()) across in-kernel exception
boundaries (pt_regs).

On arm64, normal stack unwinding follows frame pointer (x29) chains.
However, when an asynchronous exception or interrupt occurs inside the
kernel, the frame pointer chain alone is insufficient to unwind reliably
from the interrupted context (pt_regs):

  1. Before the prologue updates x29 (mov x29, sp), after the epilogue
     restores x29/x30, or throughout a leaf function, the interrupted
     function's return address is live in x30 (pt_regs->regs[30]) while
     x29 still points to the caller's frame record.
  2. Between saving the frame record on the stack (e.g.
     stp x29, x30, [sp, #-N]!) and updating x29 (mov x29, sp), or in
     functions where the compiler restores x29/x30 before adjusting sp,
     the interrupted function's frame record resides at [sp + sp_off]
     while x29 still points to the caller's frame record.
  3. Inside the function body after mov x29, sp, [x29] points to the
     interrupted function's own frame record and x30 may have been
     clobbered by nested calls.

Without knowing which state holds at the interrupted PC, the unwinder
cannot determine whether pt_regs->regs[30] is a valid frame or a
duplicate of [x29 + 8], nor can it recover a frame record at
[sp + sp_off] before x29 is updated, forcing arch_stack_walk_reliable()
to reject stack traces that cross an in-kernel exception boundary.

This RFC is proposed as an alternative to an SFrame [1] based solution:
[2]. While SFrame works well with recent GNU binutils (2.46+), it is not
yet supported by LLVM/Clang toolchains. Conversely, compiler-generated
.eh_frame tables (-fasynchronous-unwind-tables, enabled by
CONFIG_UNWIND_TABLES) are supported today by both GCC and Clang/LLVM,
and arm64 already includes a DWARF .eh_frame parser in
arch/arm64/kernel/pi/patch-scs.c for dynamic Shadow Call Stack patching.

Rather than retaining bulky raw .eh_frame sections in kernel memory and
interpreting variable-length DWARF CFI opcodes during stack unwinding,
this series introduces a build-time host tool (scripts/gen_lr_unwind)
that reuses and extends arm64's .eh_frame parser (<asm/eh_frame.h>) to
distill .eh_frame into compact, fixed-width .lr_unwind_fde and
.lr_unwind_ranges sections, and strips .eh_frame from the final vmlinux
and module binaries.

Each annotated function is described by an 8-byte struct lr_fde entry
(32-bit PC-relative start_offset + 32-bit range_desc), sorted at build
time by scripts/sorttable (and at module load time) for O(log N) lookup.
Each instruction range within a function is classified into one of three
exception-boundary unwind modes:
  - LR_UNWIND_LIVE: x30 holds the return address, x29 is the caller's FP
  - LR_UNWIND_FP:   [x29] holds the saved {x29, x30} frame record
  - LR_UNWIND_SP:   [sp + sp_off] holds the saved {x29, x30} frame record

Functions with non-standard x30 spills (such as frameless leaf functions
spilling x30 without x29) are omitted from .lr_unwind_fde so the
unwinder treats them as unreliable.

For functions following the canonical 4-phase LIVE -> FP/SP -> SP -> LIVE
layout (prologue <= 31 insns, body <= 4095 insns, epilogue/return <= 15
insns each, and sp_off <= 496 bytes), the ranges and sp_off are packed
directly into the 32-bit range_desc word of struct lr_fde; functions
with more ranges or larger lengths reference 2-byte lr_range_t entries
in .lr_unwind_ranges. Across vmlinux, .lr_unwind_fde and
.lr_unwind_ranges total ~800-935 KiB (~4x smaller than raw .eh_frame).

This series is available on github: [3].

Structure of the series:
  - Patches 1-6: Enable .eh_frame generation for CONFIG_ARM64_LR_UNWIND,
    factor the .eh_frame parser out of patch-scs.c into <asm/eh_frame.h>,
    extend it to track CFA and x29/x30 state, and implement
    scripts/gen_lr_unwind (including --dump) to emit .lr_unwind sections.
  - Patch 7: Teach scripts/sorttable to sort .lr_unwind_fde at build time.
  - Patches 8-9: Add in-kernel .lr_unwind lookup, optional boot/module
    validation (CONFIG_ARM64_LR_UNWIND_VALIDATION), and module support.
  - Patches 10-11: Annotate call_on_irq_stack() and arm64 leaf assembly
    functions (SYM_LEAF_FUNC_START / SYM_LEAF_FUNC_END) with CFI
    directives so they are covered by .lr_unwind.
  - Patch 12: Use .lr_unwind in arch/arm64/kernel/stacktrace.c to
    reliably unwind across in-kernel exception boundaries.

Ref:
[1]: https://sourceware.org/binutils/docs/sframe-spec.html
[2]: https://lore.kernel.org/all/20261117020648.323464-1-dylanbhatch@google.com/
[3]: https://github.com/dylanbhatch/linux/tree/eh_frame

Dylan Hatch (11):
  arm64, unwind: build with .eh_frame and add gen_lr_unwind skeleton
  arm64: factor .eh_frame parser out of patch-scs.c into
    <asm/eh_frame.h>
  arm64: eh_frame: track CFA and x29/x30 register state in CFI parser
  arm64, unwind: parse .eh_frame into .lr_unwind entries in
    gen_lr_unwind
  scripts/gen_lr_unwind: emit .lr_unwind_fde and .lr_unwind_ranges
  scripts/gen_lr_unwind: add --dump option to inspect .lr_unwind tables
  scripts/sorttable: sort .lr_unwind_fde by start_offset
  arm64, unwind: add in-kernel .lr_unwind lookup and validation
  arm64/module, unwind: add .lr_unwind support for modules
  arm64, crypto/lib: annotate leaf functions with CFI info
  unwind: arm64: use .lr_unwind to unwind interrupt frames

Weinan Liu (1):
  arm64: entry: add unwind info for call_on_irq_stack()

 MAINTAINERS                            |   1 +
 arch/arm64/Kconfig                     |  20 +
 arch/arm64/crypto/aes-ce-ccm-core.S    |  12 +-
 arch/arm64/crypto/aes-neonbs-core.S    |  16 +-
 arch/arm64/crypto/ghash-ce-core.S      |  20 +-
 arch/arm64/crypto/sm4-ce-ccm-core.S    |  16 +-
 arch/arm64/crypto/sm4-ce-cipher-core.S |   4 +-
 arch/arm64/crypto/sm4-ce-core.S        |  44 +-
 arch/arm64/crypto/sm4-ce-gcm-core.S    |  16 +-
 arch/arm64/crypto/sm4-neon-core.S      |  12 +-
 arch/arm64/include/asm/eh_frame.h      | 552 +++++++++++++++++
 arch/arm64/include/asm/linkage.h       |  33 +
 arch/arm64/include/asm/lr_unwind.h     | 149 +++++
 arch/arm64/include/asm/module.h        |   6 +
 arch/arm64/include/asm/module.lds.h    |   2 +-
 arch/arm64/include/asm/scs.h           |   7 -
 arch/arm64/include/asm/sections.h      |   3 +
 arch/arm64/kernel/Makefile             |   5 +
 arch/arm64/kernel/entry.S              |  12 +-
 arch/arm64/kernel/lr_unwind.c          | 395 ++++++++++++
 arch/arm64/kernel/module.c             |  11 +
 arch/arm64/kernel/pi/patch-scs.c       | 259 +-------
 arch/arm64/kernel/setup.c              |   3 +
 arch/arm64/kernel/stacktrace.c         |  85 ++-
 arch/arm64/kernel/vmlinux.lds.S        |  27 +-
 arch/arm64/lib/clear_user.S            |   4 +-
 arch/arm64/lib/copy_from_user.S        |   4 +-
 arch/arm64/lib/copy_page.S             |   4 +-
 arch/arm64/lib/copy_to_user.S          |   4 +-
 arch/arm64/lib/memchr.S                |   4 +-
 arch/arm64/lib/memcmp.S                |   4 +-
 arch/arm64/lib/memcpy.S                |   8 +-
 arch/arm64/lib/memset.S                |   8 +-
 arch/arm64/lib/mte.S                   |  28 +-
 arch/arm64/lib/strchr.S                |   4 +-
 arch/arm64/lib/strcmp.S                |   4 +-
 arch/arm64/lib/strlen.S                |   4 +-
 arch/arm64/lib/strncmp.S               |   4 +-
 arch/arm64/lib/strnlen.S               |   4 +-
 arch/arm64/lib/strrchr.S               |   4 +-
 arch/arm64/lib/tishift.S               |  12 +-
 scripts/.gitignore                     |   1 +
 scripts/Makefile                       |   2 +
 scripts/Makefile.modfinal              |  10 +-
 scripts/Makefile.vmlinux_o             |   8 +-
 scripts/gen_lr_unwind.c                | 821 +++++++++++++++++++++++++
 scripts/link-vmlinux.sh                |   2 +-
 scripts/sorttable.c                    |  50 +-
 48 files changed, 2325 insertions(+), 383 deletions(-)
 create mode 100644 arch/arm64/include/asm/eh_frame.h
 create mode 100644 arch/arm64/include/asm/lr_unwind.h
 create mode 100644 arch/arm64/kernel/lr_unwind.c
 create mode 100644 scripts/gen_lr_unwind.c


base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.56.0.679.gc527c65306-goog


             reply	other threads:[~2026-11-17  4:35 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  4:34 Dylan Hatch [this message]
2026-11-17  4:34 ` [RFC PATCH 01/12] arm64, unwind: build with .eh_frame and add gen_lr_unwind skeleton Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 02/12] arm64: factor .eh_frame parser out of patch-scs.c into <asm/eh_frame.h> Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 03/12] arm64: eh_frame: track CFA and x29/x30 register state in CFI parser Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 04/12] arm64, unwind: parse .eh_frame into .lr_unwind entries in gen_lr_unwind Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 05/12] scripts/gen_lr_unwind: emit .lr_unwind_fde and .lr_unwind_ranges Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 06/12] scripts/gen_lr_unwind: add --dump option to inspect .lr_unwind tables Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 07/12] scripts/sorttable: sort .lr_unwind_fde by start_offset Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 08/12] arm64, unwind: add in-kernel .lr_unwind lookup and validation Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 09/12] arm64/module, unwind: add .lr_unwind support for modules Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 10/12] arm64: entry: add unwind info for call_on_irq_stack() Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 11/12] arm64, crypto/lib: annotate leaf functions with CFI info Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 12/12] unwind: arm64: use .lr_unwind to unwind interrupt frames Dylan Hatch

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=20261117043504.1021063-1-dylanbhatch@google.com \
    --to=dylanbhatch@google.com \
    --cc=ardb@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=ibhagatgnu@gmail.com \
    --cc=jpoimboe@kernel.org \
    --cc=jremus@linux.ibm.com \
    --cc=justinstitt@google.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nsc@kernel.org \
    --cc=ptsm@linux.microsoft.com \
    --cc=rostedt@goodmis.org \
    --cc=will@kernel.org \
    --cc=wnliu@google.com \
    /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