From patchwork Sat Oct 17 10:48:25 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 55159 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lf0-f71.google.com (mail-lf0-f71.google.com [209.85.215.71]) by patches.linaro.org (Postfix) with ESMTPS id 322FF22FFA for ; Sat, 17 Oct 2015 10:51:22 +0000 (UTC) Received: by lffy185 with SMTP id y185sf25788049lff.2 for ; Sat, 17 Oct 2015 03:51:20 -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=RWN6CnU5TRhfbwKh6ptjDnmlnXEwq8/PCD12DbMNEYw=; b=Qp1gSoPqJyQl4Q+81U83m0hyps9INxw267Hcddx3X0qog0E5o8xF7c/5QpAgvvzroe vYROaU6TDEC+QBHr9jhbAO191DSpubo9RjuIJ8kmw23uWqUvxeDHXXs90Xd9IkyCkKM2 6SxDzBfdMig31bRkNtFJxZD1BsXYQ/1CDzbv6NUd0xYvoQ+5je5d78885/3RRBJ2qrbZ hV7nta3/K1y60cMgAlPIYtQsUzE0RuXtJJQNEGAZ6JFe7VMEe1+b6jpY/h0aYJBKVmCW KuW8twDV7uTTYADHg6CZkyZiEPE85lT3UzvVKaNhZ37sW+Ibyah9z251LEq/OyIV+Yyt zkSQ== X-Gm-Message-State: ALoCoQlJPW6zxw12AjrI4w6U6wUpYu+1QKgsNnEIiplVRa+AUGvzIM3vsib3M1UTt3OnfnbtIR+E X-Received: by 10.194.156.193 with SMTP id wg1mr4785605wjb.3.1445079080770; Sat, 17 Oct 2015 03:51:20 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.152.7 with SMTP id a7ls419281lfe.100.gmail; Sat, 17 Oct 2015 03:51:20 -0700 (PDT) X-Received: by 10.112.168.10 with SMTP id zs10mr466220lbb.101.1445079080518; Sat, 17 Oct 2015 03:51:20 -0700 (PDT) Received: from mail-lb0-f176.google.com (mail-lb0-f176.google.com. [209.85.217.176]) by mx.google.com with ESMTPS id pr9si15811414lbb.6.2015.10.17.03.51.20 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Oct 2015 03:51:20 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.176 as permitted sender) client-ip=209.85.217.176; Received: by lbbpp2 with SMTP id pp2so86914805lbb.0 for ; Sat, 17 Oct 2015 03:51:20 -0700 (PDT) X-Received: by 10.112.146.104 with SMTP id tb8mr10492635lbb.35.1445079080030; Sat, 17 Oct 2015 03:51:20 -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 w3csp436970lbq; Sat, 17 Oct 2015 03:51:18 -0700 (PDT) X-Received: by 10.68.129.168 with SMTP id nx8mr21977322pbb.168.1445079078771; Sat, 17 Oct 2015 03:51:18 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ns1si36097282pbc.169.2015.10.17.03.51.18; Sat, 17 Oct 2015 03:51:18 -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 S1752734AbbJQKt4 (ORCPT + 30 others); Sat, 17 Oct 2015 06:49:56 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:7840 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751462AbbJQKtT (ORCPT ); Sat, 17 Oct 2015 06:49:19 -0400 Received: from 172.24.1.50 (EHLO szxeml428-hub.china.huawei.com) ([172.24.1.50]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id CWX99309; Sat, 17 Oct 2015 18:48:55 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml428-hub.china.huawei.com (10.82.67.183) with Microsoft SMTP Server id 14.3.235.1; Sat, 17 Oct 2015 18:48:42 +0800 From: Wang Nan To: , , CC: , , , , , , , , , , , , Wang Nan , Arnaldo Carvalho de Melo Subject: [RFC PATCH 2/7] perf tools: Add API to apply config to BPF map Date: Sat, 17 Oct 2015 10:48:25 +0000 Message-ID: <1445078910-73699-3-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1445078910-73699-1-git-send-email-wangnan0@huawei.com> References: <1445078910-73699-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected 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: wangnan0@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.176 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: , bpf__apply_config() is introduced as the core API to apply config options to all BPF objects. This patch also does the real work for setting values for BPF_MAP_TYPE_PERF_ARRAY maps by inserting value stored in map's private field into the BPF map. This patch is required because we are not always able to set all BPF config during parsing. Further patch will set events created by perf to BPF_MAP_TYPE_PERF_EVENT_ARRAY maps, which is not exist until perf_evsel__open(). bpf_map_foreach_key() is introduced to iterate over each key needs to be configured. This function would be extended to support more map types and different key settings. Signed-off-by: Wang Nan Signed-off-by: He Kuang Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Brendan Gregg Cc: Daniel Borkmann Cc: David Ahern Cc: He Kuang Cc: Jiri Olsa Cc: Kaixu Xia Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/n/ebpf-tmg65cm1zaf1zxs7zmvxmxp4@git.kernel.org --- tools/perf/util/bpf-loader.c | 163 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 15 ++++ 2 files changed, 178 insertions(+) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index a5a1c36..15cf27a 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -7,6 +7,7 @@ #include #include +#include #include #include "perf.h" #include "debug.h" @@ -666,6 +667,69 @@ bpf_map_priv__clear(struct bpf_map *map __maybe_unused, } static int +bpf_map_config_foreach_key(struct bpf_map *map, + int (*func)(const char *name, + int map_fd, + struct bpf_map_def *pdef, + struct bpf_map_priv *priv, + void *pkey, void *arg), + void *arg) +{ + unsigned int i; + int err, map_fd; + const char *name; + struct bpf_map_def def; + struct bpf_map_priv *priv; + + name = bpf_map__get_name(map); + + err = bpf_map__get_private(map, (void **)&priv); + if (err) { + pr_debug("ERROR: failed to get private from map %s\n", name); + return -EINVAL; + } + if (!priv) { + pr_debug("INFO: nothing to config for map %s\n", name); + return 0; + } + + err = bpf_map__get_def(map, &def); + if (err) { + pr_debug("ERROR: failed to get definition from map %s\n", name); + return -EINVAL; + } + map_fd = bpf_map__get_fd(map); + if (map_fd < 0) { + pr_debug("ERROR: failed to get fd from map %s\n", name); + return map_fd; + } + + switch (def.type) { + case BPF_MAP_TYPE_ARRAY: + switch (priv->key.type) { + case BPF_MAP_PRIV_KEY_ALL: + for (i = 0; i < def.max_entries; i++) { + err = (*func)(name, map_fd, &def, + priv, &i, arg); + if (err) { + pr_debug("ERROR: failed to insert value to %s[%u]\n", + name, i); + return err; + } + } + return 0; + default: + pr_debug("ERROR: keytype for map '%s' invalid\n", name); + return -EINVAL; + } + default: + pr_debug("ERROR: type of '%s' incorrect\n", name); + return -EINVAL; + } +} + + +static int bpf__config_obj_map_array_value(struct bpf_map *map, struct parse_events_term *term) { @@ -799,6 +863,97 @@ int bpf__config_obj(struct bpf_object *obj, return -ENODEV; } +static int +bpf__apply_config_value_for_key(int map_fd, void *pkey, + size_t val_size, u64 val) +{ + int err = 0; + + switch (val_size) { + case 1: { + u8 _val = (u8)(val); + err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY); + break; + } + case 2: { + u16 _val = (u16)(val); + err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY); + break; + } + case 4: { + u32 _val = (u32)(val); + err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY); + break; + } + case 8: { + err = bpf_map_update_elem(map_fd, pkey, &val, BPF_ANY); + break; + } + default: + pr_debug("ERROR: internal error: invalid value size\n"); + return -EINVAL; + } + if (err && errno) + err = -errno; + return err; +} + +static int +bpf__apply_config_map_for_key(const char *name, int map_fd, + struct bpf_map_def *pdef __maybe_unused, + struct bpf_map_priv *priv, + void *pkey, void *arg __maybe_unused) +{ + int err; + + switch (priv->value.type) { + case BPF_MAP_PRIV_VAL_VALUE: + err = bpf__apply_config_value_for_key(map_fd, pkey, + pdef->value_size, + priv->value.val); + break; + default: + pr_debug("ERROR: unknown value type for '%s'\n", name); + err = -EINVAL; + } + return err; +} + +static int +bpf__apply_config_map(struct bpf_map *map) +{ + return bpf_map_config_foreach_key(map, bpf__apply_config_map_for_key, + NULL); +} + +static int +bpf__apply_config_object(struct bpf_object *obj) +{ + struct bpf_map *map; + int err; + + bpf_map__for_each(map, obj) { + err = bpf__apply_config_map(map); + if (err) + return err; + } + return 0; +} + +int bpf__apply_config(void) +{ + struct bpf_object *obj, *tmp; + int err; + + bpf_object__for_each_safe(obj, tmp) { + err = bpf__apply_config_object(obj); + if (err) + return err; + } + + return 0; +} + #define bpf__strerror_head(err, buf, size) \ char sbuf[STRERR_BUFSIZE], *emsg;\ if (!size)\ @@ -855,3 +1010,11 @@ int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, bpf__strerror_end(buf, size); return 0; } + +int bpf__strerror_apply_config(int err, char *buf, size_t size) +{ + bpf__strerror_head(err, buf, size); + bpf__strerror_entry(EINVAL, "Invalid option for map, add -v to see detail"); + bpf__strerror_end(buf, size); + return 0; +} diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h index dfec9b8..c9c515e 100644 --- a/tools/perf/util/bpf-loader.h +++ b/tools/perf/util/bpf-loader.h @@ -43,6 +43,8 @@ int bpf__strerror_config_obj(struct bpf_object *obj, struct parse_events_term *term, struct perf_evlist *evlist, int err, char *buf, size_t size); +int bpf__apply_config(void); +int bpf__strerror_apply_config(int err, char *buf, size_t size); #else static inline struct bpf_object * bpf__prepare_load(const char *filename __maybe_unused, @@ -82,6 +84,12 @@ bpf__config_obj(struct bpf_object *obj __maybe_unused, } static inline int +bpf__apply_config(void) +{ + return 0; +} + +static inline int __bpf_strerror(char *buf, size_t size) { if (!size) @@ -117,5 +125,12 @@ bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, { return __bpf_strerror(buf, size); } + +static inline int +bpf__strerror_apply_config(int err __maybe_unused, + char *buf, size_t size) +{ + return __bpf_strerror(buf, size); +} #endif #endif