mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs.git
synced 2026-10-30 13:52:50 +01:00
Introduce a generic API for unwinding user stacks. In order to expand user space unwinding to be able to handle more complex scenarios, such as deferred unwinding and reading user space information, create a generic interface that all architectures can use that support the various unwinding methods. This is an alternative method for handling user space stack traces from the simple stack_trace_save_user() API. This does not replace that interface, but this interface will be used to expand the functionality of user space stack walking. None of the structures introduced will be exposed to user space tooling. Support for frame pointer unwinding is added. For an architecture to support frame pointer unwinding it needs to enable CONFIG_HAVE_UNWIND_USER_FP and define ARCH_INIT_USER_FP_FRAME. By encoding the frame offsets in struct unwind_user_frame, much of this code can also be reused for future unwinder implementations like sframe. Cc: Masami Hiramatsu <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Indu Bhagat <[email protected]> Cc: "Jose E. Marchesi" <[email protected]> Cc: Beau Belgrave <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Florian Weimer <[email protected]> Cc: Sam James <[email protected]> Link: https://lore.kernel.org/[email protected] Reviewed-by: Jens Remus <[email protected]> Signed-off-by: Josh Poimboeuf <[email protected]> Co-developed-by: Mathieu Desnoyers <[email protected]> Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Mathieu Desnoyers <[email protected]> Co-developed-by: Steven Rostedt (Google) <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
129 lines
2.7 KiB
C
129 lines
2.7 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Generic interfaces for unwinding user space
|
|
*/
|
|
#include <linux/kernel.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/sched/task_stack.h>
|
|
#include <linux/unwind_user.h>
|
|
#include <linux/uaccess.h>
|
|
|
|
static const struct unwind_user_frame fp_frame = {
|
|
ARCH_INIT_USER_FP_FRAME
|
|
};
|
|
|
|
#define for_each_user_frame(state) \
|
|
for (unwind_user_start(state); !(state)->done; unwind_user_next(state))
|
|
|
|
static int unwind_user_next_fp(struct unwind_user_state *state)
|
|
{
|
|
const struct unwind_user_frame *frame = &fp_frame;
|
|
unsigned long cfa, fp, ra;
|
|
unsigned int shift;
|
|
|
|
if (frame->use_fp) {
|
|
if (state->fp < state->sp)
|
|
return -EINVAL;
|
|
cfa = state->fp;
|
|
} else {
|
|
cfa = state->sp;
|
|
}
|
|
|
|
/* Get the Canonical Frame Address (CFA) */
|
|
cfa += frame->cfa_off;
|
|
|
|
/* stack going in wrong direction? */
|
|
if (cfa <= state->sp)
|
|
return -EINVAL;
|
|
|
|
/* Make sure that the address is word aligned */
|
|
shift = sizeof(long) == 4 ? 2 : 3;
|
|
if (cfa & ((1 << shift) - 1))
|
|
return -EINVAL;
|
|
|
|
/* Find the Return Address (RA) */
|
|
if (get_user(ra, (unsigned long *)(cfa + frame->ra_off)))
|
|
return -EINVAL;
|
|
|
|
if (frame->fp_off && get_user(fp, (unsigned long __user *)(cfa + frame->fp_off)))
|
|
return -EINVAL;
|
|
|
|
state->ip = ra;
|
|
state->sp = cfa;
|
|
if (frame->fp_off)
|
|
state->fp = fp;
|
|
return 0;
|
|
}
|
|
|
|
static int unwind_user_next(struct unwind_user_state *state)
|
|
{
|
|
unsigned long iter_mask = state->available_types;
|
|
unsigned int bit;
|
|
|
|
if (state->done)
|
|
return -EINVAL;
|
|
|
|
for_each_set_bit(bit, &iter_mask, NR_UNWIND_USER_TYPE_BITS) {
|
|
enum unwind_user_type type = BIT(bit);
|
|
|
|
state->current_type = type;
|
|
switch (type) {
|
|
case UNWIND_USER_TYPE_FP:
|
|
if (!unwind_user_next_fp(state))
|
|
return 0;
|
|
continue;
|
|
default:
|
|
WARN_ONCE(1, "Undefined unwind bit %d", bit);
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
/* No successful unwind method. */
|
|
state->current_type = UNWIND_USER_TYPE_NONE;
|
|
state->done = true;
|
|
return -EINVAL;
|
|
}
|
|
|
|
static int unwind_user_start(struct unwind_user_state *state)
|
|
{
|
|
struct pt_regs *regs = task_pt_regs(current);
|
|
|
|
memset(state, 0, sizeof(*state));
|
|
|
|
if ((current->flags & PF_KTHREAD) || !user_mode(regs)) {
|
|
state->done = true;
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP))
|
|
state->available_types |= UNWIND_USER_TYPE_FP;
|
|
|
|
state->ip = instruction_pointer(regs);
|
|
state->sp = user_stack_pointer(regs);
|
|
state->fp = frame_pointer(regs);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries)
|
|
{
|
|
struct unwind_user_state state;
|
|
|
|
trace->nr = 0;
|
|
|
|
if (!max_entries)
|
|
return -EINVAL;
|
|
|
|
if (current->flags & PF_KTHREAD)
|
|
return 0;
|
|
|
|
for_each_user_frame(&state) {
|
|
trace->entries[trace->nr++] = state.ip;
|
|
if (trace->nr >= max_entries)
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|