netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/2] bpf: add support for bpf program to read perf event sample address
@ 2018-03-06 18:55 Teng Qin
  2018-03-06 18:55 ` [PATCH bpf-next 1/2] bpf: add support to read sample address in bpf program Teng Qin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Teng Qin @ 2018-03-06 18:55 UTC (permalink / raw)
  To: ast, daniel, netdev; +Cc: yhs, qinteng, kernel-team

These patches add support that allows bpf programs attached to perf events to
read the address values recorded with the perf events. These values are
requested by specifying sample_type with PERF_SAMPLE_ADDR when calling
perf_event_open().

The main motivation for these changes is to support building memory or lock
access profiling and tracing tools. For example on Intel CPUs, the recorded
address values for supported memory or lock access perf events would be
the access or lock target addresses from PEBS buffer. Such information would
be very valuable for building tools that help understand memory access or
lock acquire pattern.

Teng Qin (2):
  bpf: add support to read sample address in bpf program
  samples/bpf: add example to test reading address

 include/uapi/linux/bpf_perf_event.h |  1 +
 kernel/trace/bpf_trace.c            | 20 ++++++++++++++++----
 samples/bpf/trace_event_kern.c      |  4 ++++
 samples/bpf/trace_event_user.c      | 15 +++++++++++++++
 4 files changed, 36 insertions(+), 4 deletions(-)

-- 
2.9.5

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next 1/2] bpf: add support to read sample address in bpf program
  2018-03-06 18:55 [PATCH bpf-next 0/2] bpf: add support for bpf program to read perf event sample address Teng Qin
@ 2018-03-06 18:55 ` Teng Qin
  2018-03-06 18:55 ` [PATCH bpf-next 2/2] samples/bpf: add example to test reading address Teng Qin
  2018-03-08  1:52 ` [PATCH bpf-next 0/2] bpf: add support for bpf program to read perf event sample address Daniel Borkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Teng Qin @ 2018-03-06 18:55 UTC (permalink / raw)
  To: ast, daniel, netdev; +Cc: yhs, qinteng, kernel-team

This commit adds new field "addr" to bpf_perf_event_data which could be
read and used by bpf programs attached to perf events. The value of the
field is copied from bpf_perf_event_data_kern.addr and contains the
address value recorded by specifying sample_type with PERF_SAMPLE_ADDR
when calling perf_event_open.

Signed-off-by: Teng Qin <qinteng@fb.com>
---
 include/uapi/linux/bpf_perf_event.h |  1 +
 kernel/trace/bpf_trace.c            | 20 ++++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/include/uapi/linux/bpf_perf_event.h b/include/uapi/linux/bpf_perf_event.h
index 8f95303..eb1b9d2 100644
--- a/include/uapi/linux/bpf_perf_event.h
+++ b/include/uapi/linux/bpf_perf_event.h
@@ -13,6 +13,7 @@
 struct bpf_perf_event_data {
 	bpf_user_pt_regs_t regs;
 	__u64 sample_period;
+	__u64 addr;
 };
 
 #endif /* _UAPI__LINUX_BPF_PERF_EVENT_H__ */
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index c0a9e31..c634e09 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -726,8 +726,7 @@ const struct bpf_prog_ops tracepoint_prog_ops = {
 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
 				    struct bpf_insn_access_aux *info)
 {
-	const int size_sp = FIELD_SIZEOF(struct bpf_perf_event_data,
-					 sample_period);
+	const int size_u64 = sizeof(u64);
 
 	if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
 		return false;
@@ -738,8 +737,13 @@ static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type
 
 	switch (off) {
 	case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
-		bpf_ctx_record_field_size(info, size_sp);
-		if (!bpf_ctx_narrow_access_ok(off, size, size_sp))
+		bpf_ctx_record_field_size(info, size_u64);
+		if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
+			return false;
+		break;
+	case bpf_ctx_range(struct bpf_perf_event_data, addr):
+		bpf_ctx_record_field_size(info, size_u64);
+		if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
 			return false;
 		break;
 	default:
@@ -766,6 +770,14 @@ static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
 				      bpf_target_off(struct perf_sample_data, period, 8,
 						     target_size));
 		break;
+	case offsetof(struct bpf_perf_event_data, addr):
+		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
+						       data), si->dst_reg, si->src_reg,
+				      offsetof(struct bpf_perf_event_data_kern, data));
+		*insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
+				      bpf_target_off(struct perf_sample_data, addr, 8,
+						     target_size));
+		break;
 	default:
 		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
 						       regs), si->dst_reg, si->src_reg,
-- 
2.9.5

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf-next 2/2] samples/bpf: add example to test reading address
  2018-03-06 18:55 [PATCH bpf-next 0/2] bpf: add support for bpf program to read perf event sample address Teng Qin
  2018-03-06 18:55 ` [PATCH bpf-next 1/2] bpf: add support to read sample address in bpf program Teng Qin
@ 2018-03-06 18:55 ` Teng Qin
  2018-03-08  1:52 ` [PATCH bpf-next 0/2] bpf: add support for bpf program to read perf event sample address Daniel Borkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Teng Qin @ 2018-03-06 18:55 UTC (permalink / raw)
  To: ast, daniel, netdev; +Cc: yhs, qinteng, kernel-team

This commit adds additional test in the trace_event example, by
attaching the bpf program to MEM_UOPS_RETIRED.LOCK_LOADS event with
PERF_SAMPLE_ADDR requested, and print the lock address value read from
the bpf program to trace_pipe.

Signed-off-by: Teng Qin <qinteng@fb.com>
---
 samples/bpf/trace_event_kern.c |  4 ++++
 samples/bpf/trace_event_user.c | 15 +++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/samples/bpf/trace_event_kern.c b/samples/bpf/trace_event_kern.c
index a77a583d..7068fbd 100644
--- a/samples/bpf/trace_event_kern.c
+++ b/samples/bpf/trace_event_kern.c
@@ -39,6 +39,7 @@ int bpf_prog1(struct bpf_perf_event_data *ctx)
 {
 	char time_fmt1[] = "Time Enabled: %llu, Time Running: %llu";
 	char time_fmt2[] = "Get Time Failed, ErrCode: %d";
+	char addr_fmt[] = "Address recorded on event: %llx";
 	char fmt[] = "CPU-%d period %lld ip %llx";
 	u32 cpu = bpf_get_smp_processor_id();
 	struct bpf_perf_event_value value_buf;
@@ -64,6 +65,9 @@ int bpf_prog1(struct bpf_perf_event_data *ctx)
 	else
 	  bpf_trace_printk(time_fmt2, sizeof(time_fmt2), ret);
 
+	if (ctx->addr != 0)
+	  bpf_trace_printk(addr_fmt, sizeof(addr_fmt), ctx->addr);
+
 	val = bpf_map_lookup_elem(&counts, &key);
 	if (val)
 		(*val)++;
diff --git a/samples/bpf/trace_event_user.c b/samples/bpf/trace_event_user.c
index bf4f1b6..56f7a25 100644
--- a/samples/bpf/trace_event_user.c
+++ b/samples/bpf/trace_event_user.c
@@ -215,6 +215,17 @@ static void test_bpf_perf_event(void)
 		/* Intel Instruction Retired */
 		.config = 0xc0,
 	};
+	struct perf_event_attr attr_type_raw_lock_load = {
+		.sample_freq = SAMPLE_FREQ,
+		.freq = 1,
+		.type = PERF_TYPE_RAW,
+		/* Intel MEM_UOPS_RETIRED.LOCK_LOADS */
+		.config = 0x21d0,
+		/* Request to record lock address from PEBS */
+		.sample_type = PERF_SAMPLE_ADDR,
+		/* Record address value requires precise event */
+		.precise_ip = 2,
+	};
 
 	printf("Test HW_CPU_CYCLES\n");
 	test_perf_event_all_cpu(&attr_type_hw);
@@ -236,6 +247,10 @@ static void test_bpf_perf_event(void)
 	test_perf_event_all_cpu(&attr_type_raw);
 	test_perf_event_task(&attr_type_raw);
 
+	printf("Test Lock Load\n");
+	test_perf_event_all_cpu(&attr_type_raw_lock_load);
+	test_perf_event_task(&attr_type_raw_lock_load);
+
 	printf("*** PASS ***\n");
 }
 
-- 
2.9.5

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next 0/2] bpf: add support for bpf program to read perf event sample address
  2018-03-06 18:55 [PATCH bpf-next 0/2] bpf: add support for bpf program to read perf event sample address Teng Qin
  2018-03-06 18:55 ` [PATCH bpf-next 1/2] bpf: add support to read sample address in bpf program Teng Qin
  2018-03-06 18:55 ` [PATCH bpf-next 2/2] samples/bpf: add example to test reading address Teng Qin
@ 2018-03-08  1:52 ` Daniel Borkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Borkmann @ 2018-03-08  1:52 UTC (permalink / raw)
  To: Teng Qin, ast, netdev; +Cc: yhs, kernel-team

On 03/06/2018 07:55 PM, Teng Qin wrote:
> These patches add support that allows bpf programs attached to perf events to
> read the address values recorded with the perf events. These values are
> requested by specifying sample_type with PERF_SAMPLE_ADDR when calling
> perf_event_open().
> 
> The main motivation for these changes is to support building memory or lock
> access profiling and tracing tools. For example on Intel CPUs, the recorded
> address values for supported memory or lock access perf events would be
> the access or lock target addresses from PEBS buffer. Such information would
> be very valuable for building tools that help understand memory access or
> lock acquire pattern.

Series applied to bpf-next, thanks Teng!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-03-08  1:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-06 18:55 [PATCH bpf-next 0/2] bpf: add support for bpf program to read perf event sample address Teng Qin
2018-03-06 18:55 ` [PATCH bpf-next 1/2] bpf: add support to read sample address in bpf program Teng Qin
2018-03-06 18:55 ` [PATCH bpf-next 2/2] samples/bpf: add example to test reading address Teng Qin
2018-03-08  1:52 ` [PATCH bpf-next 0/2] bpf: add support for bpf program to read perf event sample address Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).