From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752721AbbGWJnN (ORCPT ); Thu, 23 Jul 2015 05:43:13 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:9381 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751173AbbGWJnD (ORCPT ); Thu, 23 Jul 2015 05:43:03 -0400 From: Kaixu Xia To: , , , , , , CC: , , , , Subject: [PATCH v3 2/3] bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter Date: Thu, 23 Jul 2015 09:42:41 +0000 Message-ID: <1437644562-84431-3-git-send-email-xiakaixu@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1437644562-84431-1-git-send-email-xiakaixu@huawei.com> References: <1437644562-84431-1-git-send-email-xiakaixu@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.250] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020206.55B0B723.018A,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 737909b8092bd2ee1e9a7e5a07db1958 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org According to the perf_event_map_fd and index, the function bpf_perf_event_read() can convert the corresponding map value to the pointer to struct perf_event and return the Hardware PMU counter value. Signed-off-by: Kaixu Xia --- include/linux/bpf.h | 1 + include/linux/perf_event.h | 3 ++- include/uapi/linux/bpf.h | 1 + kernel/bpf/helpers.c | 36 ++++++++++++++++++++++++++++++++++++ kernel/events/core.c | 4 ++-- kernel/trace/bpf_trace.c | 2 ++ 6 files changed, 44 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 9cf74c0..0954b8f 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -187,6 +187,7 @@ extern const struct bpf_func_proto bpf_map_lookup_elem_proto; extern const struct bpf_func_proto bpf_map_update_elem_proto; extern const struct bpf_func_proto bpf_map_delete_elem_proto; +extern const struct bpf_func_proto bpf_perf_event_read_proto; extern const struct bpf_func_proto bpf_get_prandom_u32_proto; extern const struct bpf_func_proto bpf_get_smp_processor_id_proto; extern const struct bpf_func_proto bpf_tail_call_proto; diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 2ea4067..899abcb 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -662,7 +662,8 @@ extern void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu); extern u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running); - +extern void __perf_event_read(void *info); +extern u64 perf_event_count(struct perf_event *event); struct perf_sample_data { /* diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 69a1f6b..b9b13ce 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -250,6 +250,7 @@ enum bpf_func_id { * Return: 0 on success */ BPF_FUNC_get_current_comm, + BPF_FUNC_perf_event_read, /* u64 bpf_perf_event_read(&map, index) */ __BPF_FUNC_MAX_ID, }; diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 1447ec0..aab219d 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -182,3 +182,39 @@ const struct bpf_func_proto bpf_get_current_comm_proto = { .arg1_type = ARG_PTR_TO_STACK, .arg2_type = ARG_CONST_STACK_SIZE, }; + +static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5) +{ + struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; + struct bpf_array *array = container_of(map, struct bpf_array, map); + struct perf_event *event; + + if (index >= array->map.max_entries) + return -E2BIG; + + event = array->events[index]; + if (!event) + return -EBADF; + + if (event->state != PERF_EVENT_STATE_ACTIVE) + return -ENOENT; + + if (event->oncpu != raw_smp_processor_id() && + event->ctx->task != current) + return -EINVAL; + + if (event->attr.inherit) + return -EINVAL; + + __perf_event_read(event); + + return perf_event_count(event); +} + +const struct bpf_func_proto bpf_perf_event_read_proto = { + .func = bpf_perf_event_read, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_CONST_MAP_PTR, + .arg2_type = ARG_ANYTHING, +}; diff --git a/kernel/events/core.c b/kernel/events/core.c index 08cb467..c59d9c6 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -3177,7 +3177,7 @@ void perf_event_exec(void) /* * Cross CPU call to read the hardware event */ -static void __perf_event_read(void *info) +void __perf_event_read(void *info) { struct perf_event *event = info; struct perf_event_context *ctx = event->ctx; @@ -3204,7 +3204,7 @@ static void __perf_event_read(void *info) raw_spin_unlock(&ctx->lock); } -static inline u64 perf_event_count(struct perf_event *event) +u64 perf_event_count(struct perf_event *event) { if (event->pmu->count) return event->pmu->count(event); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 88a041a..9cf094f 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -183,6 +183,8 @@ static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func return bpf_get_trace_printk_proto(); case BPF_FUNC_get_smp_processor_id: return &bpf_get_smp_processor_id_proto; + case BPF_FUNC_perf_event_read: + return &bpf_perf_event_read_proto; default: return NULL; } -- 1.8.3.4