Skip to content

Commit c2c6b27

Browse files
mrutland-armctmarinas
authored andcommitted
arm64: stacktrace: unwind exception boundaries
When arm64's stack unwinder encounters an exception boundary, it uses the pt_regs::stackframe created by the entry code, which has a copy of the PC and FP at the time the exception was taken. The unwinder doesn't know anything about pt_regs, and reports the PC from the stackframe, but does not report the LR. The LR is only guaranteed to contain the return address at function call boundaries, and can be used as a scratch register at other times, so the LR at an exception boundary may or may not be a legitimate return address. It would be useful to report the LR value regardless, as it can be helpful when debugging, and in future it will be helpful for reliable stacktrace support. This patch changes the way we unwind across exception boundaries, allowing both the PC and LR to be reported. The entry code creates a frame_record_meta structure embedded within pt_regs, which the unwinder uses to find the pt_regs. The unwinder can then extract pt_regs::pc and pt_regs::lr as two separate unwind steps before continuing with a regular walk of frame records. When a PC is unwound from pt_regs::lr, dump_backtrace() will log this with an "L" marker so that it can be identified easily. For example, an unwind across an exception boundary will appear as follows: | el1h_64_irq+0x6c/0x70 | _raw_spin_unlock_irqrestore+0x10/0x60 (P) | __aarch64_insn_write+0x6c/0x90 (L) | aarch64_insn_patch_text_nosync+0x28/0x80 ... with a (P) entry for pt_regs::pc, and an (L) entry for pt_regs:lr. Note that the LR may be stale at the point of the exception, for example, shortly after a return: | el1h_64_irq+0x6c/0x70 | default_idle_call+0x34/0x180 (P) | default_idle_call+0x28/0x180 (L) | do_idle+0x204/0x268 ... where the LR points a few instructions before the current PC. This plays nicely with all the other unwind metadata tracking. With the ftrace_graph profiler enabled globally, and kretprobes installed on generic_handle_domain_irq() and do_interrupt_handler(), a backtrace triggered by magic-sysrq + L reports: | Call trace: | show_stack+0x20/0x40 (CF) | dump_stack_lvl+0x60/0x80 (F) | dump_stack+0x18/0x28 | nmi_cpu_backtrace+0xfc/0x140 | nmi_trigger_cpumask_backtrace+0x1c8/0x200 | arch_trigger_cpumask_backtrace+0x20/0x40 | sysrq_handle_showallcpus+0x24/0x38 (F) | __handle_sysrq+0xa8/0x1b0 (F) | handle_sysrq+0x38/0x50 (F) | pl011_int+0x460/0x5a8 (F) | __handle_irq_event_percpu+0x60/0x220 (F) | handle_irq_event+0x54/0xc0 (F) | handle_fasteoi_irq+0xa8/0x1d0 (F) | generic_handle_domain_irq+0x34/0x58 (F) | gic_handle_irq+0x54/0x140 (FK) | call_on_irq_stack+0x24/0x58 (F) | do_interrupt_handler+0x88/0xa0 | el1_interrupt+0x34/0x68 (FK) | el1h_64_irq_handler+0x18/0x28 | el1h_64_irq+0x6c/0x70 | default_idle_call+0x34/0x180 (P) | default_idle_call+0x28/0x180 (L) | do_idle+0x204/0x268 | cpu_startup_entry+0x3c/0x50 (F) | rest_init+0xe4/0xf0 | start_kernel+0x744/0x750 | __primary_switched+0x88/0x98 Signed-off-by: Mark Rutland <mark.rutland@arm.com> Reviewed-by: Mark Brown <broonie@kernel.org> Reviewed-by: Miroslav Benes <mbenes@suse.cz> Reviewed-by: Puranjay Mohan <puranjay12@gmail.com> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Josh Poimboeuf <jpoimboe@kernel.org> Cc: Kalesh Singh <kaleshsingh@google.com> Cc: Madhavan T. Venkataraman <madvenka@linux.microsoft.com> Cc: Marc Zyngier <maz@kernel.org> Cc: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20241017092538.1859841-11-mark.rutland@arm.com Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent f05a4a4 commit c2c6b27

File tree

7 files changed

+158
-19
lines changed

7 files changed

+158
-19
lines changed

arch/arm64/include/asm/ptrace.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,7 @@ struct pt_regs {
168168
u32 pmr;
169169

170170
u64 sdei_ttbr1;
171-
u64 unused;
172-
173-
struct frame_record stackframe;
171+
struct frame_record_meta stackframe;
174172

175173
/* Only valid for some EL1 exceptions. */
176174
u64 lockdep_hardirqs;

arch/arm64/include/asm/stacktrace/frame.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,46 @@
33
#define __ASM_STACKTRACE_FRAME_H
44

55
/*
6+
* - FRAME_META_TYPE_NONE
7+
*
8+
* This value is reserved.
9+
*
10+
* - FRAME_META_TYPE_FINAL
11+
*
12+
* The record is the last entry on the stack.
13+
* Unwinding should terminate successfully.
14+
*
15+
* - FRAME_META_TYPE_PT_REGS
16+
*
17+
* The record is embedded within a struct pt_regs, recording the registers at
18+
* an arbitrary point in time.
19+
* Unwinding should consume pt_regs::pc, followed by pt_regs::lr.
20+
*
21+
* Note: all other values are reserved and should result in unwinding
22+
* terminating with an error.
23+
*/
24+
#define FRAME_META_TYPE_NONE 0
25+
#define FRAME_META_TYPE_FINAL 1
26+
#define FRAME_META_TYPE_PT_REGS 2
27+
28+
#ifndef __ASSEMBLY__
29+
/*
630
* A standard AAPCS64 frame record.
731
*/
832
struct frame_record {
933
u64 fp;
1034
u64 lr;
1135
};
1236

37+
/*
38+
* A metadata frame record indicating a special unwind.
39+
* The record::{fp,lr} fields must be zero to indicate the presence of
40+
* metadata.
41+
*/
42+
struct frame_record_meta {
43+
struct frame_record record;
44+
u64 type;
45+
};
46+
#endif /* __ASSEMBLY */
47+
1348
#endif /* __ASM_STACKTRACE_FRAME_H */

arch/arm64/kernel/asm-offsets.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ int main(void)
8181
DEFINE(S_SDEI_TTBR1, offsetof(struct pt_regs, sdei_ttbr1));
8282
DEFINE(S_PMR, offsetof(struct pt_regs, pmr));
8383
DEFINE(S_STACKFRAME, offsetof(struct pt_regs, stackframe));
84+
DEFINE(S_STACKFRAME_TYPE, offsetof(struct pt_regs, stackframe.type));
8485
DEFINE(PT_REGS_SIZE, sizeof(struct pt_regs));
8586
BLANK();
8687
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS

arch/arm64/kernel/entry.S

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <asm/processor.h>
2626
#include <asm/ptrace.h>
2727
#include <asm/scs.h>
28+
#include <asm/stacktrace/frame.h>
2829
#include <asm/thread_info.h>
2930
#include <asm/asm-uaccess.h>
3031
#include <asm/unistd.h>
@@ -284,15 +285,16 @@ alternative_else_nop_endif
284285
stp lr, x21, [sp, #S_LR]
285286

286287
/*
287-
* For exceptions from EL0, create a final frame record.
288-
* For exceptions from EL1, create a synthetic frame record so the
289-
* interrupted code shows up in the backtrace.
288+
* Create a metadata frame record. The unwinder will use this to
289+
* identify and unwind exception boundaries.
290290
*/
291-
.if \el == 0
292291
stp xzr, xzr, [sp, #S_STACKFRAME]
292+
.if \el == 0
293+
mov x0, #FRAME_META_TYPE_FINAL
293294
.else
294-
stp x29, x22, [sp, #S_STACKFRAME]
295+
mov x0, #FRAME_META_TYPE_PT_REGS
295296
.endif
297+
str x0, [sp, #S_STACKFRAME_TYPE]
296298
add x29, sp, #S_STACKFRAME
297299

298300
#ifdef CONFIG_ARM64_SW_TTBR0_PAN

arch/arm64/kernel/head.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <asm/scs.h>
3333
#include <asm/smp.h>
3434
#include <asm/sysreg.h>
35+
#include <asm/stacktrace/frame.h>
3536
#include <asm/thread_info.h>
3637
#include <asm/virt.h>
3738

@@ -199,6 +200,8 @@ SYM_CODE_END(preserve_boot_args)
199200
sub sp, sp, #PT_REGS_SIZE
200201

201202
stp xzr, xzr, [sp, #S_STACKFRAME]
203+
mov \tmp1, #FRAME_META_TYPE_FINAL
204+
str \tmp1, [sp, #S_STACKFRAME_TYPE]
202205
add x29, sp, #S_STACKFRAME
203206

204207
scs_load_current

arch/arm64/kernel/process.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
409409
*/
410410
memset(childregs, 0, sizeof(struct pt_regs));
411411
childregs->pstate = PSR_MODE_EL1h | PSR_IL_BIT;
412+
childregs->stackframe.type = FRAME_META_TYPE_FINAL;
412413

413414
p->thread.cpu_context.x19 = (unsigned long)args->fn;
414415
p->thread.cpu_context.x20 = (unsigned long)args->fn_arg;

arch/arm64/kernel/stacktrace.c

Lines changed: 110 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ enum kunwind_source {
2626
KUNWIND_SOURCE_CALLER,
2727
KUNWIND_SOURCE_TASK,
2828
KUNWIND_SOURCE_REGS_PC,
29+
KUNWIND_SOURCE_REGS_LR,
2930
};
3031

3132
union unwind_flags {
@@ -55,6 +56,7 @@ struct kunwind_state {
5556
#endif
5657
enum kunwind_source source;
5758
union unwind_flags flags;
59+
struct pt_regs *regs;
5860
};
5961

6062
static __always_inline void
@@ -65,6 +67,7 @@ kunwind_init(struct kunwind_state *state,
6567
state->task = task;
6668
state->source = KUNWIND_SOURCE_UNKNOWN;
6769
state->flags.all = 0;
70+
state->regs = NULL;
6871
}
6972

7073
/*
@@ -80,6 +83,7 @@ kunwind_init_from_regs(struct kunwind_state *state,
8083
{
8184
kunwind_init(state, current);
8285

86+
state->regs = regs;
8387
state->common.fp = regs->regs[29];
8488
state->common.pc = regs->pc;
8589
state->source = KUNWIND_SOURCE_REGS_PC;
@@ -155,6 +159,103 @@ kunwind_recover_return_address(struct kunwind_state *state)
155159
return 0;
156160
}
157161

162+
static __always_inline
163+
int kunwind_next_regs_pc(struct kunwind_state *state)
164+
{
165+
struct stack_info *info;
166+
unsigned long fp = state->common.fp;
167+
struct pt_regs *regs;
168+
169+
regs = container_of((u64 *)fp, struct pt_regs, stackframe.record.fp);
170+
171+
info = unwind_find_stack(&state->common, (unsigned long)regs, sizeof(*regs));
172+
if (!info)
173+
return -EINVAL;
174+
175+
unwind_consume_stack(&state->common, info, (unsigned long)regs,
176+
sizeof(*regs));
177+
178+
state->regs = regs;
179+
state->common.pc = regs->pc;
180+
state->common.fp = regs->regs[29];
181+
state->source = KUNWIND_SOURCE_REGS_PC;
182+
return 0;
183+
}
184+
185+
static __always_inline int
186+
kunwind_next_regs_lr(struct kunwind_state *state)
187+
{
188+
/*
189+
* The stack for the regs was consumed by kunwind_next_regs_pc(), so we
190+
* cannot consume that again here, but we know the regs are safe to
191+
* access.
192+
*/
193+
state->common.pc = state->regs->regs[30];
194+
state->common.fp = state->regs->regs[29];
195+
state->regs = NULL;
196+
state->source = KUNWIND_SOURCE_REGS_LR;
197+
198+
return 0;
199+
}
200+
201+
static __always_inline int
202+
kunwind_next_frame_record_meta(struct kunwind_state *state)
203+
{
204+
struct task_struct *tsk = state->task;
205+
unsigned long fp = state->common.fp;
206+
struct frame_record_meta *meta;
207+
struct stack_info *info;
208+
209+
info = unwind_find_stack(&state->common, fp, sizeof(*meta));
210+
if (!info)
211+
return -EINVAL;
212+
213+
meta = (struct frame_record_meta *)fp;
214+
switch (READ_ONCE(meta->type)) {
215+
case FRAME_META_TYPE_FINAL:
216+
if (meta == &task_pt_regs(tsk)->stackframe)
217+
return -ENOENT;
218+
WARN_ON_ONCE(1);
219+
return -EINVAL;
220+
case FRAME_META_TYPE_PT_REGS:
221+
return kunwind_next_regs_pc(state);
222+
default:
223+
WARN_ON_ONCE(1);
224+
return -EINVAL;
225+
}
226+
}
227+
228+
static __always_inline int
229+
kunwind_next_frame_record(struct kunwind_state *state)
230+
{
231+
unsigned long fp = state->common.fp;
232+
struct frame_record *record;
233+
struct stack_info *info;
234+
unsigned long new_fp, new_pc;
235+
236+
if (fp & 0x7)
237+
return -EINVAL;
238+
239+
info = unwind_find_stack(&state->common, fp, sizeof(*record));
240+
if (!info)
241+
return -EINVAL;
242+
243+
record = (struct frame_record *)fp;
244+
new_fp = READ_ONCE(record->fp);
245+
new_pc = READ_ONCE(record->lr);
246+
247+
if (!new_fp && !new_pc)
248+
return kunwind_next_frame_record_meta(state);
249+
250+
unwind_consume_stack(&state->common, info, fp, sizeof(*record));
251+
252+
state->common.fp = new_fp;
253+
state->common.pc = new_pc;
254+
state->source = KUNWIND_SOURCE_FRAME;
255+
256+
return 0;
257+
}
258+
158259
/*
159260
* Unwind from one frame record (A) to the next frame record (B).
160261
*
@@ -165,30 +266,27 @@ kunwind_recover_return_address(struct kunwind_state *state)
165266
static __always_inline int
166267
kunwind_next(struct kunwind_state *state)
167268
{
168-
struct task_struct *tsk = state->task;
169-
unsigned long fp = state->common.fp;
170269
int err;
171270

172271
state->flags.all = 0;
173272

174-
/* Final frame; nothing to unwind */
175-
if (fp == (unsigned long)&task_pt_regs(tsk)->stackframe)
176-
return -ENOENT;
177-
178273
switch (state->source) {
179274
case KUNWIND_SOURCE_FRAME:
180275
case KUNWIND_SOURCE_CALLER:
181276
case KUNWIND_SOURCE_TASK:
277+
case KUNWIND_SOURCE_REGS_LR:
278+
err = kunwind_next_frame_record(state);
279+
break;
182280
case KUNWIND_SOURCE_REGS_PC:
183-
err = unwind_next_frame_record(&state->common);
184-
if (err)
185-
return err;
186-
state->source = KUNWIND_SOURCE_FRAME;
281+
err = kunwind_next_regs_lr(state);
187282
break;
188283
default:
189-
return -EINVAL;
284+
err = -EINVAL;
190285
}
191286

287+
if (err)
288+
return err;
289+
192290
state->common.pc = ptrauth_strip_kernel_insn_pac(state->common.pc);
193291

194292
return kunwind_recover_return_address(state);
@@ -338,6 +436,7 @@ static const char *state_source_string(const struct kunwind_state *state)
338436
case KUNWIND_SOURCE_CALLER: return "C";
339437
case KUNWIND_SOURCE_TASK: return "T";
340438
case KUNWIND_SOURCE_REGS_PC: return "P";
439+
case KUNWIND_SOURCE_REGS_LR: return "L";
341440
default: return "U";
342441
}
343442
}

0 commit comments

Comments
 (0)