Skip to content

Commit d31a580

Browse files
jpoimboeingomolnar
authored andcommitted
x86/unwind/orc: Detect the end of the stack
The existing UNWIND_HINT_EMPTY annotations happen to be good indicators of where entry code calls into C code for the first time. So also use them to mark the end of the stack for the ORC unwinder. Use that information to set unwind->error if the ORC unwinder doesn't unwind all the way to the end. This will be needed for enabling HAVE_RELIABLE_STACKTRACE for the ORC unwinder so we can use it with the livepatch consistency model. Thanks to Jiri Slaby for teaching the ORCs about the unwind hints. Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com> Signed-off-by: Jiri Slaby <jslaby@suse.cz> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: https://lkml.kernel.org/lkml/20180518064713.26440-5-jslaby@suse.cz Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 0c41436 commit d31a580

9 files changed

Lines changed: 52 additions & 29 deletions

File tree

‎arch/x86/entry/entry_64.S‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ ENTRY(ret_from_fork)
408408

409409
1:
410410
/* kernel thread */
411+
UNWIND_HINT_EMPTY
411412
movq %r12, %rdi
412413
CALL_NOSPEC %rbx
413414
/*

‎arch/x86/include/asm/orc_types.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ struct orc_entry {
8888
unsigned sp_reg:4;
8989
unsigned bp_reg:4;
9090
unsigned type:2;
91+
unsigned end:1;
9192
} __packed;
9293

9394
/*
@@ -101,6 +102,7 @@ struct unwind_hint {
101102
s16 sp_offset;
102103
u8 sp_reg;
103104
u8 type;
105+
u8 end;
104106
};
105107
#endif /* __ASSEMBLY__ */
106108

‎arch/x86/include/asm/unwind_hints.h‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* the debuginfo as necessary. It will also warn if it sees any
2727
* inconsistencies.
2828
*/
29-
.macro UNWIND_HINT sp_reg=ORC_REG_SP sp_offset=0 type=ORC_TYPE_CALL
29+
.macro UNWIND_HINT sp_reg=ORC_REG_SP sp_offset=0 type=ORC_TYPE_CALL end=0
3030
#ifdef CONFIG_STACK_VALIDATION
3131
.Lunwind_hint_ip_\@:
3232
.pushsection .discard.unwind_hints
@@ -35,12 +35,14 @@
3535
.short \sp_offset
3636
.byte \sp_reg
3737
.byte \type
38+
.byte \end
39+
.balign 4
3840
.popsection
3941
#endif
4042
.endm
4143

4244
.macro UNWIND_HINT_EMPTY
43-
UNWIND_HINT sp_reg=ORC_REG_UNDEFINED
45+
UNWIND_HINT sp_reg=ORC_REG_UNDEFINED end=1
4446
.endm
4547

4648
.macro UNWIND_HINT_REGS base=%rsp offset=0 indirect=0 extra=1 iret=0
@@ -86,19 +88,21 @@
8688

8789
#else /* !__ASSEMBLY__ */
8890

89-
#define UNWIND_HINT(sp_reg, sp_offset, type) \
91+
#define UNWIND_HINT(sp_reg, sp_offset, type, end) \
9092
"987: \n\t" \
9193
".pushsection .discard.unwind_hints\n\t" \
9294
/* struct unwind_hint */ \
9395
".long 987b - .\n\t" \
94-
".short " __stringify(sp_offset) "\n\t" \
96+
".short " __stringify(sp_offset) "\n\t" \
9597
".byte " __stringify(sp_reg) "\n\t" \
9698
".byte " __stringify(type) "\n\t" \
99+
".byte " __stringify(end) "\n\t" \
100+
".balign 4 \n\t" \
97101
".popsection\n\t"
98102

99-
#define UNWIND_HINT_SAVE UNWIND_HINT(0, 0, UNWIND_HINT_TYPE_SAVE)
103+
#define UNWIND_HINT_SAVE UNWIND_HINT(0, 0, UNWIND_HINT_TYPE_SAVE, 0)
100104

101-
#define UNWIND_HINT_RESTORE UNWIND_HINT(0, 0, UNWIND_HINT_TYPE_RESTORE)
105+
#define UNWIND_HINT_RESTORE UNWIND_HINT(0, 0, UNWIND_HINT_TYPE_RESTORE, 0)
102106

103107
#endif /* __ASSEMBLY__ */
104108

‎arch/x86/kernel/unwind_orc.c‎

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static int orc_sort_cmp(const void *_a, const void *_b)
198198
* whitelisted .o files which didn't get objtool generation.
199199
*/
200200
orc_a = cur_orc_table + (a - cur_orc_ip_table);
201-
return orc_a->sp_reg == ORC_REG_UNDEFINED ? -1 : 1;
201+
return orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end ? -1 : 1;
202202
}
203203

204204
#ifdef CONFIG_MODULES
@@ -352,7 +352,7 @@ static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr
352352

353353
bool unwind_next_frame(struct unwind_state *state)
354354
{
355-
unsigned long ip_p, sp, orig_ip, prev_sp = state->sp;
355+
unsigned long ip_p, sp, orig_ip = state->ip, prev_sp = state->sp;
356356
enum stack_type prev_type = state->stack_info.type;
357357
struct orc_entry *orc;
358358
bool indirect = false;
@@ -363,9 +363,9 @@ bool unwind_next_frame(struct unwind_state *state)
363363
/* Don't let modules unload while we're reading their ORC data. */
364364
preempt_disable();
365365

366-
/* Have we reached the end? */
366+
/* End-of-stack check for user tasks: */
367367
if (state->regs && user_mode(state->regs))
368-
goto done;
368+
goto the_end;
369369

370370
/*
371371
* Find the orc_entry associated with the text address.
@@ -374,9 +374,16 @@ bool unwind_next_frame(struct unwind_state *state)
374374
* calls and calls to noreturn functions.
375375
*/
376376
orc = orc_find(state->signal ? state->ip : state->ip - 1);
377-
if (!orc || orc->sp_reg == ORC_REG_UNDEFINED)
378-
goto done;
379-
orig_ip = state->ip;
377+
if (!orc)
378+
goto err;
379+
380+
/* End-of-stack check for kernel threads: */
381+
if (orc->sp_reg == ORC_REG_UNDEFINED) {
382+
if (!orc->end)
383+
goto err;
384+
385+
goto the_end;
386+
}
380387

381388
/* Find the previous frame's stack: */
382389
switch (orc->sp_reg) {
@@ -402,7 +409,7 @@ bool unwind_next_frame(struct unwind_state *state)
402409
if (!state->regs || !state->full_regs) {
403410
orc_warn("missing regs for base reg R10 at ip %pB\n",
404411
(void *)state->ip);
405-
goto done;
412+
goto err;
406413
}
407414
sp = state->regs->r10;
408415
break;
@@ -411,7 +418,7 @@ bool unwind_next_frame(struct unwind_state *state)
411418
if (!state->regs || !state->full_regs) {
412419
orc_warn("missing regs for base reg R13 at ip %pB\n",
413420
(void *)state->ip);
414-
goto done;
421+
goto err;
415422
}
416423
sp = state->regs->r13;
417424
break;
@@ -420,7 +427,7 @@ bool unwind_next_frame(struct unwind_state *state)
420427
if (!state->regs || !state->full_regs) {
421428
orc_warn("missing regs for base reg DI at ip %pB\n",
422429
(void *)state->ip);
423-
goto done;
430+
goto err;
424431
}
425432
sp = state->regs->di;
426433
break;
@@ -429,20 +436,20 @@ bool unwind_next_frame(struct unwind_state *state)
429436
if (!state->regs || !state->full_regs) {
430437
orc_warn("missing regs for base reg DX at ip %pB\n",
431438
(void *)state->ip);
432-
goto done;
439+
goto err;
433440
}
434441
sp = state->regs->dx;
435442
break;
436443

437444
default:
438445
orc_warn("unknown SP base reg %d for ip %pB\n",
439446
orc->sp_reg, (void *)state->ip);
440-
goto done;
447+
goto err;
441448
}
442449

443450
if (indirect) {
444451
if (!deref_stack_reg(state, sp, &sp))
445-
goto done;
452+
goto err;
446453
}
447454

448455
/* Find IP, SP and possibly regs: */
@@ -451,7 +458,7 @@ bool unwind_next_frame(struct unwind_state *state)
451458
ip_p = sp - sizeof(long);
452459

453460
if (!deref_stack_reg(state, ip_p, &state->ip))
454-
goto done;
461+
goto err;
455462

456463
state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
457464
state->ip, (void *)ip_p);
@@ -465,7 +472,7 @@ bool unwind_next_frame(struct unwind_state *state)
465472
if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) {
466473
orc_warn("can't dereference registers at %p for ip %pB\n",
467474
(void *)sp, (void *)orig_ip);
468-
goto done;
475+
goto err;
469476
}
470477

471478
state->regs = (struct pt_regs *)sp;
@@ -477,7 +484,7 @@ bool unwind_next_frame(struct unwind_state *state)
477484
if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) {
478485
orc_warn("can't dereference iret registers at %p for ip %pB\n",
479486
(void *)sp, (void *)orig_ip);
480-
goto done;
487+
goto err;
481488
}
482489

483490
state->regs = (void *)sp - IRET_FRAME_OFFSET;
@@ -500,18 +507,18 @@ bool unwind_next_frame(struct unwind_state *state)
500507

501508
case ORC_REG_PREV_SP:
502509
if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp))
503-
goto done;
510+
goto err;
504511
break;
505512

506513
case ORC_REG_BP:
507514
if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp))
508-
goto done;
515+
goto err;
509516
break;
510517

511518
default:
512519
orc_warn("unknown BP base reg %d for ip %pB\n",
513520
orc->bp_reg, (void *)orig_ip);
514-
goto done;
521+
goto err;
515522
}
516523

517524
/* Prevent a recursive loop due to bad ORC data: */
@@ -520,13 +527,16 @@ bool unwind_next_frame(struct unwind_state *state)
520527
state->sp <= prev_sp) {
521528
orc_warn("stack going in the wrong direction? ip=%pB\n",
522529
(void *)orig_ip);
523-
goto done;
530+
goto err;
524531
}
525532

526533
preempt_enable();
527534
return true;
528535

529-
done:
536+
err:
537+
state->error = true;
538+
539+
the_end:
530540
preempt_enable();
531541
state->stack_info.type = STACK_TYPE_UNKNOWN;
532542
return false;

‎tools/objtool/arch/x86/include/asm/orc_types.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ struct orc_entry {
8888
unsigned sp_reg:4;
8989
unsigned bp_reg:4;
9090
unsigned type:2;
91+
unsigned end:1;
9192
} __packed;
9293

9394
/*
@@ -101,6 +102,7 @@ struct unwind_hint {
101102
s16 sp_offset;
102103
u8 sp_reg;
103104
u8 type;
105+
u8 end;
104106
};
105107
#endif /* __ASSEMBLY__ */
106108

‎tools/objtool/check.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,7 @@ static int read_unwind_hints(struct objtool_file *file)
11561156

11571157
cfa->offset = hint->sp_offset;
11581158
insn->state.type = hint->type;
1159+
insn->state.end = hint->end;
11591160
}
11601161

11611162
return 0;

‎tools/objtool/check.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct insn_state {
3131
int stack_size;
3232
unsigned char type;
3333
bool bp_scratch;
34-
bool drap;
34+
bool drap, end;
3535
int drap_reg, drap_offset;
3636
struct cfi_reg vals[CFI_NUM_REGS];
3737
};

‎tools/objtool/orc_dump.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ int orc_dump(const char *_objname)
203203

204204
print_reg(orc[i].bp_reg, orc[i].bp_offset);
205205

206-
printf(" type:%s\n", orc_type_name(orc[i].type));
206+
printf(" type:%s end:%d\n",
207+
orc_type_name(orc[i].type), orc[i].end);
207208
}
208209

209210
elf_end(elf);

‎tools/objtool/orc_gen.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ int create_orc(struct objtool_file *file)
3131
struct cfi_reg *cfa = &insn->state.cfa;
3232
struct cfi_reg *bp = &insn->state.regs[CFI_BP];
3333

34+
orc->end = insn->state.end;
35+
3436
if (cfa->base == CFI_UNDEFINED) {
3537
orc->sp_reg = ORC_REG_UNDEFINED;
3638
continue;

0 commit comments

Comments
 (0)