From patchwork Mon Oct 19 10:37:40 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kaixu Xia X-Patchwork-Id: 55220 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lf0-f69.google.com (mail-lf0-f69.google.com [209.85.215.69]) by patches.linaro.org (Postfix) with ESMTPS id D70B822F11 for ; Mon, 19 Oct 2015 10:38:19 +0000 (UTC) Received: by lffv3 with SMTP id v3sf38789197lff.1 for ; Mon, 19 Oct 2015 03:38:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-type:sender:precedence :list-id:x-original-sender:x-original-authentication-results :mailing-list:list-post:list-help:list-archive:list-unsubscribe; bh=fYE9x/scgewWYZmXCzWHgcn8DcIKMF4JW6pO1rh4cHo=; b=VWkJYtrov9wdQhFsdfMM/YdVhGe5r/oh1AZHaKlYGWdNbAMuiW7fG4BE6i8vHwpA/Q Ef11HV7yMTVhck2HM9OlgZ1mNuQiu0cq4mdbA24MHNKFf2+fo+CkCxpd6MZ2J9dyAfCB ZHdp2v1XITwP3b8s2PLry2X/0qUE0onOww4EwkFEd6FnqtTdq8V6PFUBBEzQ5bXph+Ls KF/rOcWxaQkAzKijBrcQo4pNcbSXWFZWYye8FwvAY7QSWOZA20lPHehWd+NBDx4ZMbb2 Up+dV9R81ypJD/5w3Sl7e8nudiPE0DCAv0TDV0bOJO7+EcUyRkWj6xtuK6UIyDPJqN5Q QkVg== X-Gm-Message-State: ALoCoQmSIunmCdD/dRCONM2m5gB+YA1WNuIRgT7DPFy3CvLRFkdTuCAuJMVV/jxOHqrqyiztiXh4 X-Received: by 10.112.158.202 with SMTP id ww10mr6684065lbb.13.1445251098698; Mon, 19 Oct 2015 03:38:18 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.23.201 with SMTP id 70ls597897lfx.49.gmail; Mon, 19 Oct 2015 03:38:18 -0700 (PDT) X-Received: by 10.112.209.71 with SMTP id mk7mr14316948lbc.46.1445251098526; Mon, 19 Oct 2015 03:38:18 -0700 (PDT) Received: from mail-lb0-f179.google.com (mail-lb0-f179.google.com. [209.85.217.179]) by mx.google.com with ESMTPS id w204si22350773lfd.39.2015.10.19.03.38.18 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Oct 2015 03:38:18 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.179 as permitted sender) client-ip=209.85.217.179; Received: by lbbes7 with SMTP id es7so50388155lbb.2 for ; Mon, 19 Oct 2015 03:38:18 -0700 (PDT) X-Received: by 10.112.202.35 with SMTP id kf3mr14261145lbc.19.1445251098186; Mon, 19 Oct 2015 03:38:18 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp1387755lbq; Mon, 19 Oct 2015 03:38:17 -0700 (PDT) X-Received: by 10.50.122.68 with SMTP id lq4mr18887052igb.87.1445251096925; Mon, 19 Oct 2015 03:38:16 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w15si15398886ioi.162.2015.10.19.03.38.16; Mon, 19 Oct 2015 03:38:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753219AbbJSKiO (ORCPT + 28 others); Mon, 19 Oct 2015 06:38:14 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:37234 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750904AbbJSKiN (ORCPT ); Mon, 19 Oct 2015 06:38:13 -0400 Received: from 172.24.1.47 (EHLO szxeml434-hub.china.huawei.com) ([172.24.1.47]) by szxrg03-dlp.huawei.com (MOS 4.4.3-GA FastPath queued) with ESMTP id BPE25516; Mon, 19 Oct 2015 18:38:03 +0800 (CST) Received: from euler.hulk-profiling (10.107.193.250) by szxeml434-hub.china.huawei.com (10.82.67.225) with Microsoft SMTP Server id 14.3.235.1; Mon, 19 Oct 2015 18:37:49 +0800 From: Kaixu Xia To: , , , , , , , CC: , , , , , Subject: [PATCH V4 1/1] bpf: control events stored in PERF_EVENT_ARRAY maps trace data output when perf sampling Date: Mon, 19 Oct 2015 10:37:40 +0000 Message-ID: <1445251060-114617-2-git-send-email-xiakaixu@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1445251060-114617-1-git-send-email-xiakaixu@huawei.com> References: <1445251060-114617-1-git-send-email-xiakaixu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.250] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090205.5624C80E.002A, 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: a490e1f159cbf3b4a7e73eda7204ffd4 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: xiakaixu@huawei.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.179 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , This patch adds the flag soft_enable to control the trace data output process when perf sampling. By setting this flag and integrating with ebpf, we can control the data output process and get the samples we are most interested in. The bpf helper bpf_perf_event_control() can control either the perf event on current cpu or all the perf events stored in the maps by checking the third parameter 'flag'. Signed-off-by: Kaixu Xia --- include/linux/perf_event.h | 1 + include/uapi/linux/bpf.h | 19 +++++++++++++++ include/uapi/linux/perf_event.h | 3 ++- kernel/bpf/verifier.c | 3 ++- kernel/events/core.c | 13 +++++++++++ kernel/trace/bpf_trace.c | 51 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 2 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 092a0e8..bb3bf87 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -472,6 +472,7 @@ struct perf_event { struct irq_work pending; atomic_t event_limit; + atomic_t soft_enable; void (*destroy)(struct perf_event *); struct rcu_head rcu_head; diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 564f1f0..a2b0d9d 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -132,6 +132,20 @@ enum bpf_prog_type { #define BPF_NOEXIST 1 /* create new element if it didn't exist */ #define BPF_EXIST 2 /* update existing element */ +/* flags for PERF_EVENT_ARRAY maps*/ +enum { + BPF_EVENT_CTL_BIT_CUR = 0, + BPF_EVENT_CTL_BIT_ALL = 1, + __NR_BPF_EVENT_CTL_BITS, +}; + +#define BPF_CTL_BIT_FLAG_MASK \ + ((1ULL << __NR_BPF_EVENT_CTL_BITS) - 1) +#define BPF_CTL_BIT_DUMP_CUR \ + (1ULL << BPF_EVENT_CTL_BIT_CUR) +#define BPF_CTL_BIT_DUMP_ALL \ + (1ULL << BPF_EVENT_CTL_BIT_ALL) + union bpf_attr { struct { /* anonymous struct used by BPF_MAP_CREATE command */ __u32 map_type; /* one of enum bpf_map_type */ @@ -287,6 +301,11 @@ enum bpf_func_id { * Return: realm if != 0 */ BPF_FUNC_get_route_realm, + + /** + * u64 bpf_perf_event_control(&map, index, flag) + */ + BPF_FUNC_perf_event_control, __BPF_FUNC_MAX_ID, }; diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index 2881145..a791b03 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -331,7 +331,8 @@ struct perf_event_attr { comm_exec : 1, /* flag comm events that are due to an exec */ use_clockid : 1, /* use @clockid for time fields */ context_switch : 1, /* context switch data */ - __reserved_1 : 37; + soft_disable : 1, /* output data on samples by default */ + __reserved_1 : 36; union { __u32 wakeup_events; /* wakeup every n events */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 1d6b97b..ffec14b 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -245,6 +245,7 @@ static const struct { } func_limit[] = { {BPF_MAP_TYPE_PROG_ARRAY, BPF_FUNC_tail_call}, {BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_read}, + {BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_control}, }; static void print_verifier_state(struct verifier_env *env) @@ -910,7 +911,7 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id) * don't allow any other map type to be passed into * the special func; */ - if (bool_map != bool_func) + if (bool_func && bool_map != bool_func) return -EINVAL; } diff --git a/kernel/events/core.c b/kernel/events/core.c index b11756f..5219635 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6337,6 +6337,9 @@ static int __perf_event_overflow(struct perf_event *event, irq_work_queue(&event->pending); } + if (unlikely(!atomic_read(&event->soft_enable))) + return 0; + if (event->overflow_handler) event->overflow_handler(event, data, regs); else @@ -7709,6 +7712,14 @@ static void account_event(struct perf_event *event) account_event_cpu(event, event->cpu); } +static void perf_event_check_dump_flag(struct perf_event *event) +{ + if (event->attr.soft_disable == 1) + atomic_set(&event->soft_enable, 0); + else + atomic_set(&event->soft_enable, 1); +} + /* * Allocate and initialize a event structure */ @@ -7840,6 +7851,8 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, } } + perf_event_check_dump_flag(event); + return event; err_per_task: diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 0fe96c7..d26f3d4 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -215,6 +215,55 @@ const struct bpf_func_proto bpf_perf_event_read_proto = { .arg2_type = ARG_ANYTHING, }; +static u64 bpf_perf_event_control(u64 r1, u64 index, u64 flag, 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; + int i; + + if (unlikely(index >= array->map.max_entries)) + return -E2BIG; + + if (flag & (~BPF_CTL_BIT_FLAG_MASK)) + return -EINVAL; + + if (flag & BPF_CTL_BIT_DUMP_ALL) { + bool dump_control = flag & BPF_CTL_BIT_DUMP_CUR; + + for (i = 0; i < array->map.max_entries; i++) { + event = (struct perf_event *)array->ptrs[i]; + if (!event) + continue; + + if (dump_control) + atomic_dec_if_positive(&event->soft_enable); + else + atomic_inc_unless_negative(&event->soft_enable); + } + return 0; + } + + event = (struct perf_event *)array->ptrs[index]; + if (!event) + return -ENOENT; + + if (flag & BPF_CTL_BIT_DUMP_CUR) + atomic_dec_if_positive(&event->soft_enable); + else + atomic_inc_unless_negative(&event->soft_enable); + return 0; +} + +static const struct bpf_func_proto bpf_perf_event_control_proto = { + .func = bpf_perf_event_control, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_CONST_MAP_PTR, + .arg2_type = ARG_ANYTHING, + .arg3_type = ARG_ANYTHING, +}; + static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id) { switch (func_id) { @@ -242,6 +291,8 @@ static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func return &bpf_get_smp_processor_id_proto; case BPF_FUNC_perf_event_read: return &bpf_perf_event_read_proto; + case BPF_FUNC_perf_event_control: + return &bpf_perf_event_control_proto; default: return NULL; }