Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753519AbbGAC6l (ORCPT ); Tue, 30 Jun 2015 22:58:41 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:32949 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752729AbbGAC6U (ORCPT ); Tue, 30 Jun 2015 22:58:20 -0400 From: He Kuang To: , , , , , , , CC: , Subject: [RFC PATCH 4/5] bpf: Introduce function for outputing sample data to perf event Date: Wed, 1 Jul 2015 02:57:34 +0000 Message-ID: <1435719455-91155-5-git-send-email-hekuang@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1435719455-91155-1-git-send-email-hekuang@huawei.com> References: <1435719455-91155-1-git-send-email-hekuang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.210] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 6193 Lines: 171 Add function to receive data from eBPF programs and fill that into perf extra trace buffer of the current context. In previous patch we make sure that the recursion flag protecting perf trace buffer is checked when bpf_prog is executed, so here we can safely fill the trace buffer. In order to get the corresponding trace buffer of the context, new function perf_swevent_current_context_type() is added, this function only gets the current context type but does not increase the recursion count. Signed-off-by: He Kuang --- include/linux/perf_event.h | 2 ++ include/uapi/linux/bpf.h | 1 + kernel/events/core.c | 6 ++++++ kernel/events/internal.h | 17 ++++++++++------- kernel/trace/bpf_trace.c | 29 +++++++++++++++++++++++++++++ samples/bpf/bpf_helpers.h | 2 ++ 6 files changed, 50 insertions(+), 7 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index a204d52..984c89c 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -939,6 +939,7 @@ extern unsigned int perf_output_copy(struct perf_output_handle *handle, const void *buf, unsigned int len); extern unsigned int perf_output_skip(struct perf_output_handle *handle, unsigned int len); +extern int perf_swevent_current_context_type(void); extern int perf_swevent_get_recursion_context(void); extern void perf_swevent_put_recursion_context(int rctx); extern u64 perf_swevent_set_period(struct perf_event *event); @@ -995,6 +996,7 @@ static inline void perf_event_exec(void) { } static inline void perf_event_comm(struct task_struct *tsk, bool exec) { } static inline void perf_event_fork(struct task_struct *tsk) { } static inline void perf_event_init(void) { } +static inline int perf_swevent_current_context_type(void); { return -1; } static inline int perf_swevent_get_recursion_context(void) { return -1; } static inline void perf_swevent_put_recursion_context(int rctx) { } static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; } diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a9ebdf5..8a7015d 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -169,6 +169,7 @@ enum bpf_func_id { BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */ BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */ BPF_FUNC_probe_read, /* int bpf_probe_read(void *dst, int size, void *src) */ + BPF_FUNC_output_sample, /* int bpf_output_sample(void *src, int size) */ BPF_FUNC_ktime_get_ns, /* u64 bpf_ktime_get_ns(void) */ BPF_FUNC_trace_printk, /* int bpf_trace_printk(const char *fmt, int fmt_size, ...) */ BPF_FUNC_get_prandom_u32, /* u32 prandom_u32(void) */ diff --git a/kernel/events/core.c b/kernel/events/core.c index 9e0773d..0224d5b 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6376,6 +6376,12 @@ end: DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); +int perf_swevent_current_context_type(void) +{ + return current_context_type(); +} +EXPORT_SYMBOL_GPL(perf_swevent_current_context_type); + int perf_swevent_get_recursion_context(void) { struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); diff --git a/kernel/events/internal.h b/kernel/events/internal.h index 2deb24c..5cabce5 100644 --- a/kernel/events/internal.h +++ b/kernel/events/internal.h @@ -175,18 +175,21 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs); extern int get_callchain_buffers(void); extern void put_callchain_buffers(void); -static inline int get_recursion_context(int *recursion) +static inline int current_context_type(void) { - int rctx; - if (in_nmi()) - rctx = 3; + return 3; else if (in_irq()) - rctx = 2; + return 2; else if (in_softirq()) - rctx = 1; + return 1; else - rctx = 0; + return 0; +} + +static inline int get_recursion_context(int *recursion) +{ + int rctx = current_context_type(); if (recursion[rctx]) return -1; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 2d56ce5..b2f363d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -79,6 +79,33 @@ static const struct bpf_func_proto bpf_probe_read_proto = { .arg3_type = ARG_ANYTHING, }; +static u64 bpf_output_sample(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) +{ + void *src = (void *) (long) r1; + int size = (int) r2; + void *buf; + int rctx = perf_swevent_current_context_type(); + + if (rctx < 0) + return -EINVAL; + + buf = perf_extra_trace_buf_prepare(size, rctx); + if (!buf) + return -ENOMEM; + + memcpy(buf, src, size); + + return 0; +} + +static const struct bpf_func_proto bpf_output_sample_proto = { + .func = bpf_output_sample, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_STACK, + .arg2_type = ARG_CONST_STACK_SIZE, +}; + static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) { /* NMI safe access to clock monotonic */ @@ -170,6 +197,8 @@ static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func return &bpf_map_delete_elem_proto; case BPF_FUNC_probe_read: return &bpf_probe_read_proto; + case BPF_FUNC_output_sample: + return &bpf_output_sample_proto; case BPF_FUNC_ktime_get_ns: return &bpf_ktime_get_ns_proto; diff --git a/samples/bpf/bpf_helpers.h b/samples/bpf/bpf_helpers.h index f960b5f..105510e 100644 --- a/samples/bpf/bpf_helpers.h +++ b/samples/bpf/bpf_helpers.h @@ -17,6 +17,8 @@ static int (*bpf_map_delete_elem)(void *map, void *key) = (void *) BPF_FUNC_map_delete_elem; static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) = (void *) BPF_FUNC_probe_read; +static int (*bpf_output_sample)(void *src, int size) = + (void *) BPF_FUNC_output_sample; static unsigned long long (*bpf_ktime_get_ns)(void) = (void *) BPF_FUNC_ktime_get_ns; static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = -- 1.8.5.2 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/