Skip to content

Commit

Permalink
fprobe: Pass return address to the handlers
Browse files Browse the repository at this point in the history
Pass return address as 'ret_ip' to the fprobe entry and return handlers
so that the fprobe user handler can get the reutrn address without
analyzing arch-dependent pt_regs.

Link: https://lore.kernel.org/all/168507467664.913472.11642316698862778600.stgit@mhiramat.roam.corp.google.com/

Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
  • Loading branch information
Masami Hiramatsu (Google) committed Jun 6, 2023
1 parent 9561de3 commit cb16330
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 14 deletions.
6 changes: 4 additions & 2 deletions include/linux/fprobe.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ struct fprobe {
int nr_maxactive;

int (*entry_handler)(struct fprobe *fp, unsigned long entry_ip,
struct pt_regs *regs, void *entry_data);
unsigned long ret_ip, struct pt_regs *regs,
void *entry_data);
void (*exit_handler)(struct fprobe *fp, unsigned long entry_ip,
struct pt_regs *regs, void *entry_data);
unsigned long ret_ip, struct pt_regs *regs,
void *entry_data);
};

/* This fprobe is soft-disabled. */
Expand Down
2 changes: 1 addition & 1 deletion include/linux/rethook.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

struct rethook_node;

typedef void (*rethook_handler_t) (struct rethook_node *, void *, struct pt_regs *);
typedef void (*rethook_handler_t) (struct rethook_node *, void *, unsigned long, struct pt_regs *);

/**
* struct rethook - The rethook management data structure.
Expand Down
1 change: 1 addition & 0 deletions kernel/kprobes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2127,6 +2127,7 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
NOKPROBE_SYMBOL(pre_handler_kretprobe);

static void kretprobe_rethook_handler(struct rethook_node *rh, void *data,
unsigned long ret_addr,
struct pt_regs *regs)
{
struct kretprobe *rp = (struct kretprobe *)data;
Expand Down
6 changes: 4 additions & 2 deletions kernel/trace/bpf_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -2642,7 +2642,8 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,

static int
kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
struct pt_regs *regs, void *data)
unsigned long ret_ip, struct pt_regs *regs,
void *data)
{
struct bpf_kprobe_multi_link *link;

Expand All @@ -2653,7 +2654,8 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,

static void
kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,
struct pt_regs *regs, void *data)
unsigned long ret_ip, struct pt_regs *regs,
void *data)
{
struct bpf_kprobe_multi_link *link;

Expand Down
6 changes: 3 additions & 3 deletions kernel/trace/fprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static inline void __fprobe_handler(unsigned long ip, unsigned long parent_ip,
}

if (fp->entry_handler)
ret = fp->entry_handler(fp, ip, ftrace_get_regs(fregs), entry_data);
ret = fp->entry_handler(fp, ip, parent_ip, ftrace_get_regs(fregs), entry_data);

/* If entry_handler returns !0, nmissed is not counted. */
if (rh) {
Expand Down Expand Up @@ -112,7 +112,7 @@ static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
}

static void fprobe_exit_handler(struct rethook_node *rh, void *data,
struct pt_regs *regs)
unsigned long ret_ip, struct pt_regs *regs)
{
struct fprobe *fp = (struct fprobe *)data;
struct fprobe_rethook_node *fpr;
Expand All @@ -133,7 +133,7 @@ static void fprobe_exit_handler(struct rethook_node *rh, void *data,
return;
}

fp->exit_handler(fp, fpr->entry_ip, regs,
fp->exit_handler(fp, fpr->entry_ip, ret_ip, regs,
fp->entry_data_size ? (void *)fpr->data : NULL);
ftrace_test_recursion_unlock(bit);
}
Expand Down
3 changes: 2 additions & 1 deletion kernel/trace/rethook.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ unsigned long rethook_trampoline_handler(struct pt_regs *regs,
break;
handler = READ_ONCE(rhn->rethook->handler);
if (handler)
handler(rhn, rhn->rethook->data, regs);
handler(rhn, rhn->rethook->data,
correct_ret_addr, regs);

if (first == node)
break;
Expand Down
10 changes: 7 additions & 3 deletions lib/test_fprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ static noinline u32 fprobe_selftest_nest_target(u32 value, u32 (*nest)(u32))
}

static notrace int fp_entry_handler(struct fprobe *fp, unsigned long ip,
struct pt_regs *regs, void *data)
unsigned long ret_ip,
struct pt_regs *regs, void *data)
{
KUNIT_EXPECT_FALSE(current_test, preemptible());
/* This can be called on the fprobe_selftest_target and the fprobe_selftest_target2 */
Expand All @@ -57,6 +58,7 @@ static notrace int fp_entry_handler(struct fprobe *fp, unsigned long ip,
}

static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,
unsigned long ret_ip,
struct pt_regs *regs, void *data)
{
unsigned long ret = regs_return_value(regs);
Expand All @@ -78,14 +80,16 @@ static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,
}

static notrace int nest_entry_handler(struct fprobe *fp, unsigned long ip,
struct pt_regs *regs, void *data)
unsigned long ret_ip,
struct pt_regs *regs, void *data)
{
KUNIT_EXPECT_FALSE(current_test, preemptible());
return 0;
}

static notrace void nest_exit_handler(struct fprobe *fp, unsigned long ip,
struct pt_regs *regs, void *data)
unsigned long ret_ip,
struct pt_regs *regs, void *data)
{
KUNIT_EXPECT_FALSE(current_test, preemptible());
KUNIT_EXPECT_EQ(current_test, ip, target_nest_ip);
Expand Down
6 changes: 4 additions & 2 deletions samples/fprobe/fprobe_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static void show_backtrace(void)
}

static int sample_entry_handler(struct fprobe *fp, unsigned long ip,
unsigned long ret_ip,
struct pt_regs *regs, void *data)
{
if (use_trace)
Expand All @@ -65,10 +66,11 @@ static int sample_entry_handler(struct fprobe *fp, unsigned long ip,
return 0;
}

static void sample_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs,
static void sample_exit_handler(struct fprobe *fp, unsigned long ip,
unsigned long ret_ip, struct pt_regs *regs,
void *data)
{
unsigned long rip = instruction_pointer(regs);
unsigned long rip = ret_ip;

if (use_trace)
/*
Expand Down

0 comments on commit cb16330

Please sign in to comment.