Skip to contentSkip to conversation

perf: Fix bpf prologue generation, uaccess

PATCH In review Sumanth Korikkar 2 participants 6 messages Jun 9, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover perf: Fix bpf prologue generation, uaccess

Patches:
1. Fix bpf prologue generation. bpf_probe_read is unavailable for arch with overlapping address space. If uaccess, use bpf_probe_read_user. Otherwise, use bpf_probe_read_kernel.
2. Fix uaccess in kprobes. Now perf probe add with @user attribute works

v1->v2:
- Split the patches and add Acked-By

Thank you.

Sumanth Korikkar (2):
perf: Fix user attribute access in kprobes
perf: Fix bpf prologue generation

 tools/perf/util/bpf-prologue.c | 14 ++++++++++----
 tools/perf/util/probe-event.c  |  7 +++++--
 tools/perf/util/probe-file.c   |  2 +-
 3 files changed, 16 insertions(+), 7 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/2 perf: Fix user attribute access in kprobes

Issue:
perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user' did not work before.

Fix:
Make (perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user') output equivalent to ftrace ('p:probe/do_sched_setscheduler _text+517384 pid=%r2:s32 policy=%r3:s32 sched_priority=+u0(%r4):s32' > kprobe_events)

Other:
1. Right now, __match_glob() does not handle [u]<offset>. For now, use *u]<offset>.
2. @user attribute was introduced in commit 1e032f7cfa14 ("perf-probe:
Add user memory access attribute support")

Test:
1. perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user'

2 ./perf script
sched 305669 [000] 1614458.838675: perf_bpf_probe:func: (2904e508) pid=261614 policy=2 sched_priority=1

3. cat /sys/kernel/debug/tracing/trace <...>-309956 [006] .... 1616098.093957: 0: prio: 1

---
 tools/perf/util/probe-event.c | 7 +++++--
 tools/perf/util/probe-file.c  | 2 +-
 2 files changed, 6 insertions(+), 3 deletions(-)
tools/perf/util/probe-event.c +5 -2
index a08f373d3305..df713a5d1e26 100644
@@ -1575,7 +1575,7 @@ static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
 	}
 
 	tmp = strchr(str, '@');
-	if (tmp && tmp != str && strcmp(tmp + 1, "user")) { /* user attr */
+	if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */
 		if (!user_access_is_supported()) {
 			semantic_error("ftrace does not support user access\n");
 			return -EINVAL;
@@ -1995,7 +1995,10 @@ static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
 		if (depth < 0)
 			return depth;
 	}
-	err = strbuf_addf(buf, "%+ld(", ref->offset);
+	if (ref->user_access)
+		err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset);
+	else
+		err = strbuf_addf(buf, "%+ld(", ref->offset);
 	return (err < 0) ? err : depth;
 }
 
tools/perf/util/probe-file.c +1 -1
index 8c852948513e..064b63a6a3f3 100644
@@ -1044,7 +1044,7 @@ static struct {
 	DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X, "*type: * x8/16/32/64,*"),
 	DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET, "*place (kretprobe): *"),
 	DEFINE_TYPE(FTRACE_README_UPROBE_REF_CTR, "*ref_ctr_offset*"),
-	DEFINE_TYPE(FTRACE_README_USER_ACCESS, "*[u]<offset>*"),
+	DEFINE_TYPE(FTRACE_README_USER_ACCESS, "*u]<offset>*"),
 	DEFINE_TYPE(FTRACE_README_MULTIPROBE_EVENT, "*Create/append/*"),
 	DEFINE_TYPE(FTRACE_README_IMMEDIATE_VALUE, "*\\imm-value,*"),
 };
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v2 1/2] perf: Fix user attribute access in kprobes

Em Tue, Jun 09, 2020 at 10:10:18AM +0200, Sumanth Korikkar escreveu:

Quoted 20 lines

Issue:
perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user' did not work before.

Fix:
Make (perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user') output equivalent to ftrace ('p:probe/do_sched_setscheduler _text+517384 pid=%r2:s32 policy=%r3:s32 sched_priority=+u0(%r4):s32' > kprobe_events)

Other:
1. Right now, __match_glob() does not handle [u]<offset>. For now, use *u]<offset>.
2. @user attribute was introduced in commit 1e032f7cfa14 ("perf-probe:
Add user memory access attribute support")

Test:
1. perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user'

Quoted 6 lines

2 ./perf script
sched 305669 [000] 1614458.838675: perf_bpf_probe:func: (2904e508) pid=261614 policy=2 sched_priority=1

3. cat /sys/kernel/debug/tracing/trace <...>-309956 [006] .... 1616098.093957: 0: prio: 1

Thanks, I'm adding this:

Fixes 1e032f7cfa14 ("perf-probe: Add user memory access attribute support")
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt (VMware) <[email protected]>

So that the stable guys pick this up eventually,

That first hunk with the strcmp() return check could have gone into a separate patch, but I'll process it as-is for expediency,

- Arnaldo

Quoted 49 lines
---
 tools/perf/util/probe-event.c | 7 +++++--
 tools/perf/util/probe-file.c  | 2 +-
 2 files changed, 6 insertions(+), 3 deletions(-)
tools/perf/util/probe-event.c +5 -2
index a08f373d3305..df713a5d1e26 100644
@@ -1575,7 +1575,7 @@ static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
 	}
 
 	tmp = strchr(str, '@');
-	if (tmp && tmp != str && strcmp(tmp + 1, "user")) { /* user attr */
+	if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */
 		if (!user_access_is_supported()) {
 			semantic_error("ftrace does not support user access\n");
 			return -EINVAL;
@@ -1995,7 +1995,10 @@ static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
 		if (depth < 0)
 			return depth;
 	}
-	err = strbuf_addf(buf, "%+ld(", ref->offset);
+	if (ref->user_access)
+		err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset);
+	else
+		err = strbuf_addf(buf, "%+ld(", ref->offset);
 	return (err < 0) ? err : depth;
 }
 
tools/perf/util/probe-file.c +1 -1
index 8c852948513e..064b63a6a3f3 100644
@@ -1044,7 +1044,7 @@ static struct {
 	DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X, "*type: * x8/16/32/64,*"),
 	DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET, "*place (kretprobe): *"),
 	DEFINE_TYPE(FTRACE_README_UPROBE_REF_CTR, "*ref_ctr_offset*"),
-	DEFINE_TYPE(FTRACE_README_USER_ACCESS, "*[u]<offset>*"),
+	DEFINE_TYPE(FTRACE_README_USER_ACCESS, "*u]<offset>*"),
 	DEFINE_TYPE(FTRACE_README_MULTIPROBE_EVENT, "*Create/append/*"),
 	DEFINE_TYPE(FTRACE_README_IMMEDIATE_VALUE, "*\\imm-value,*"),
 };
Signature · 1 line
2.17.1
Signature · 1 line
- Arnaldo
#3 lore raw

Re: [PATCH v2 1/2] perf: Fix user attribute access in kprobes

Em Tue, Jun 09, 2020 at 12:09:31PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 30 lines

Em Tue, Jun 09, 2020 at 10:10:18AM +0200, Sumanth Korikkar escreveu:

Quoted 20 lines

Issue:
perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user' did not work before.

Fix:
Make (perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user') output equivalent to ftrace ('p:probe/do_sched_setscheduler _text+517384 pid=%r2:s32 policy=%r3:s32 sched_priority=+u0(%r4):s32' > kprobe_events)

Other:
1. Right now, __match_glob() does not handle [u]<offset>. For now, use *u]<offset>.
2. @user attribute was introduced in commit 1e032f7cfa14 ("perf-probe:
Add user memory access attribute support")

Test:
1. perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user'

Quoted 6 lines

2 ./perf script
sched 305669 [000] 1614458.838675: perf_bpf_probe:func: (2904e508) pid=261614 policy=2 sched_priority=1

3. cat /sys/kernel/debug/tracing/trace <...>-309956 [006] .... 1616098.093957: 0: prio: 1

Thanks, I'm adding this:

Quoted 8 lines
Fixes 1e032f7cfa14 ("perf-probe: Add user memory access attribute support")
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt (VMware) <[email protected]>

So that the stable guys pick this up eventually,

That first hunk with the strcmp() return check could have gone into a separate patch, but I'll process it as-is for expediency,

I also added this:

ommitter testing:

Before:

  # perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user'
  param(type:sched_param) has no member sched_priority@user.
    Error: Failed to add events.
  # pahole sched_param
  struct sched_param {
        int                        sched_priority;       /*     0     4 */
        /* size: 4, cachelines: 1, members: 1 */
        /* last cacheline: 4 bytes */

};
#

After:

  # perf probe -a 'do_sched_setscheduler pid policy param->sched_priority@user'
  Added new event:
    probe:do_sched_setscheduler (on do_sched_setscheduler with pid policy sched_priority=param->sched_priority)

You can now use it in all perf tools, such as:

perf record -e probe:do_sched_setscheduler -aR sleep 1

  # cat /sys/kernel/debug/tracing/kprobe_events
  p:probe/do_sched_setscheduler _text+1113792 pid=%di:s32 policy=%si:s32 sched_priority=+u0(%dx):s32
  #
Quoted 55 lines

- Arnaldo

Quoted 49 lines
---
 tools/perf/util/probe-event.c | 7 +++++--
 tools/perf/util/probe-file.c  | 2 +-
 2 files changed, 6 insertions(+), 3 deletions(-)
tools/perf/util/probe-event.c +5 -2
index a08f373d3305..df713a5d1e26 100644
@@ -1575,7 +1575,7 @@ static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
 	}
 
 	tmp = strchr(str, '@');
-	if (tmp && tmp != str && strcmp(tmp + 1, "user")) { /* user attr */
+	if (tmp && tmp != str && !strcmp(tmp + 1, "user")) { /* user attr */
 		if (!user_access_is_supported()) {
 			semantic_error("ftrace does not support user access\n");
 			return -EINVAL;
@@ -1995,7 +1995,10 @@ static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
 		if (depth < 0)
 			return depth;
 	}
-	err = strbuf_addf(buf, "%+ld(", ref->offset);
+	if (ref->user_access)
+		err = strbuf_addf(buf, "%s%ld(", "+u", ref->offset);
+	else
+		err = strbuf_addf(buf, "%+ld(", ref->offset);
 	return (err < 0) ? err : depth;
 }
 
tools/perf/util/probe-file.c +1 -1
index 8c852948513e..064b63a6a3f3 100644
@@ -1044,7 +1044,7 @@ static struct {
 	DEFINE_TYPE(FTRACE_README_PROBE_TYPE_X, "*type: * x8/16/32/64,*"),
 	DEFINE_TYPE(FTRACE_README_KRETPROBE_OFFSET, "*place (kretprobe): *"),
 	DEFINE_TYPE(FTRACE_README_UPROBE_REF_CTR, "*ref_ctr_offset*"),
-	DEFINE_TYPE(FTRACE_README_USER_ACCESS, "*[u]<offset>*"),
+	DEFINE_TYPE(FTRACE_README_USER_ACCESS, "*u]<offset>*"),
 	DEFINE_TYPE(FTRACE_README_MULTIPROBE_EVENT, "*Create/append/*"),
 	DEFINE_TYPE(FTRACE_README_IMMEDIATE_VALUE, "*\\imm-value,*"),
 };
Signature · 1 line
2.17.1
Signature · 1 line
- Arnaldo
Signature · 1 line
- Arnaldo
#4 lore raw

PATCH v2 2/2 perf: Fix bpf prologue generation

In reply to Sumanth Korikkar

Issue:
bpf_probe_read is no longer available for architecture which has overlapping address space. Hence bpf prologue generation fails

Fix:
Use bpf_probe_read_kernel for kernel member access. For user attribute access in kprobes, use bpf_probe_read_user.

Other:
@user attribute was introduced in commit 1e032f7cfa14 ("perf-probe:
Add user memory access attribute support")

Test:
1. ulimit -l 128 ; ./perf record -e tests/bpf_sched_setscheduler.c
2. cat tests/bpf_sched_setscheduler.c

static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =

        (void *) 6;
static int (*bpf_probe_read_user)(void *dst, __u32 size,
                                  const void *unsafe_ptr) = (void *) 112;
static int (*bpf_probe_read_kernel)(void *dst, __u32 size,
        const void *unsafe_ptr) = (void *) 113;

SEC("func=do_sched_setscheduler pid policy param->sched_priority@user") int bpf_func__setscheduler(void *ctx, int err, pid_t pid, int policy,
int param)

{
        char fmt[] = "prio: %ld";
        bpf_trace_printk(fmt, sizeof(fmt), param);
        return 1;
}

char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;

3. ./perf script
sched 305669 [000] 1614458.838675: perf_bpf_probe:func: (2904e508) pid=261614 policy=2 sched_priority=1

4. cat /sys/kernel/debug/tracing/trace <...>-309956 [006] .... 1616098.093957: 0: prio: 1

---
 tools/perf/util/bpf-prologue.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
tools/perf/util/bpf-prologue.c +10 -4
index b020a8678eb9..9887ae09242d 100644
@@ -142,7 +142,8 @@ static int
 gen_read_mem(struct bpf_insn_pos *pos,
 	     int src_base_addr_reg,
 	     int dst_addr_reg,
-	     long offset)
+	     long offset,
+	     int probeid)
 {
 	/* mov arg3, src_base_addr_reg */
 	if (src_base_addr_reg != BPF_REG_ARG3)
@@ -159,7 +160,7 @@ gen_read_mem(struct bpf_insn_pos *pos,
 		ins(BPF_MOV64_REG(BPF_REG_ARG1, dst_addr_reg), pos);
 
 	/* Call probe_read  */
-	ins(BPF_EMIT_CALL(BPF_FUNC_probe_read), pos);
+	ins(BPF_EMIT_CALL(probeid), pos);
 	/*
 	 * Error processing: if read fail, goto error code,
 	 * will be relocated. Target should be the start of
@@ -241,7 +242,7 @@ static int
 gen_prologue_slowpath(struct bpf_insn_pos *pos,
 		      struct probe_trace_arg *args, int nargs)
 {
-	int err, i;
+	int err, i, probeid;
 
 	for (i = 0; i < nargs; i++) {
 		struct probe_trace_arg *arg = &args[i];
@@ -276,11 +277,16 @@ gen_prologue_slowpath(struct bpf_insn_pos *pos,
 				stack_offset), pos);
 
 		ref = arg->ref;
+		probeid = BPF_FUNC_probe_read_kernel;
 		while (ref) {
 			pr_debug("prologue: arg %d: offset %ld\n",
 				 i, ref->offset);
+
+			if (ref->user_access)
+				probeid = BPF_FUNC_probe_read_user;
+
 			err = gen_read_mem(pos, BPF_REG_3, BPF_REG_7,
-					   ref->offset);
+					   ref->offset, probeid);
 			if (err) {
 				pr_err("prologue: failed to generate probe_read function call\n");
 				goto errout;
Signature · 1 line
2.17.1
#5 lore raw

Re: [PATCH v2 2/2] perf: Fix bpf prologue generation

Em Tue, Jun 09, 2020 at 10:10:19AM +0200, Sumanth Korikkar escreveu:

Quoted 35 lines

Issue:
bpf_probe_read is no longer available for architecture which has overlapping address space. Hence bpf prologue generation fails

Fix:
Use bpf_probe_read_kernel for kernel member access. For user attribute access in kprobes, use bpf_probe_read_user.

Other:
@user attribute was introduced in commit 1e032f7cfa14 ("perf-probe:
Add user memory access attribute support")

Test:
1. ulimit -l 128 ; ./perf record -e tests/bpf_sched_setscheduler.c
2. cat tests/bpf_sched_setscheduler.c

static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =

        (void *) 6;
static int (*bpf_probe_read_user)(void *dst, __u32 size,
                                  const void *unsafe_ptr) = (void *) 112;
static int (*bpf_probe_read_kernel)(void *dst, __u32 size,
        const void *unsafe_ptr) = (void *) 113;

SEC("func=do_sched_setscheduler pid policy param->sched_priority@user") int bpf_func__setscheduler(void *ctx, int err, pid_t pid, int policy,
int param)

{
        char fmt[] = "prio: %ld";
        bpf_trace_printk(fmt, sizeof(fmt), param);
        return 1;
}

char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;

Quoted 6 lines

3. ./perf script
sched 305669 [000] 1614458.838675: perf_bpf_probe:func: (2904e508) pid=261614 policy=2 sched_priority=1

4. cat /sys/kernel/debug/tracing/trace <...>-309956 [006] .... 1616098.093957: 0: prio: 1

Thanks for providing a detailed set of steps to test your patch, that is great!

I added this, an alterenative way to test it, combining all the aspects in one 'perf trace' call:

Committer testing:

I had to add some missing headers in the bpf_sched_setscheduler.c test proggie, then instead of using record+script I used 'perf trace' to drive everything in one go:

  # cat bpf_sched_setscheduler.c
  #include <linux/types.h>
  #include <bpf.h>
  static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = (void *) 6;
  static int (*bpf_probe_read_user)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 112;
  static int (*bpf_probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 113;

  SEC("func=do_sched_setscheduler  pid policy param->sched_priority@user")
  int bpf_func__setscheduler(void *ctx, int err, pid_t pid, int policy, int param)
  {
          char fmt[] = "prio: %ld";
          bpf_trace_printk(fmt, sizeof(fmt), param);
          return 1;
  }

  char _license[] SEC("license") = "GPL";
  int _version SEC("version") = LINUX_VERSION_CODE;
  #
  #
  # perf trace -e bpf_sched_setscheduler.c chrt -f 42 sleep 1
     0.000 chrt/80125 perf_bpf_probe:func(__probe_ip: -1676607808, policy: 1, sched_priority: 42)
  #

And even with backtraces :-)

  # perf trace -e bpf_sched_setscheduler.c/max-stack=8/ chrt -f 42 sleep 1
       0.000 chrt/79805 perf_bpf_probe:func(__probe_ip: -1676607808, policy: 1, sched_priority: 42)
                                         do_sched_setscheduler ([kernel.kallsyms])
                                         __x64_sys_sched_setscheduler ([kernel.kallsyms])
                                         do_syscall_64 ([kernel.kallsyms])
                                         entry_SYSCALL_64 ([kernel.kallsyms])
                                         __GI___sched_setscheduler (/usr/lib64/libc-2.30.so)
  # 

- Arnaldo