2022-02-17 14:09:47

by Jiri Olsa

[permalink] [raw]
Subject: [PATCH 2/3] perf tools: Remove bpf_map__set_priv/bpf_map__priv usage

Both bpf_map__set_priv/bpf_map__priv are deprecated
and will be eventually removed.

Using hashmap to replace that functionality.

Suggested-by: Andrii Nakryiko <[email protected]>
Signed-off-by: Jiri Olsa <[email protected]>
---
tools/perf/util/bpf-loader.c | 62 ++++++++++++++++++++++++++++++++----
1 file changed, 55 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index ec27aab2bd36..6f87729817ad 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -57,6 +57,7 @@ struct bpf_perf_object {

static LIST_HEAD(bpf_objects_list);
static struct hashmap *bpf_program_hash;
+static struct hashmap *bpf_map_hash;

static struct bpf_perf_object *
bpf_perf_object__next(struct bpf_perf_object *prev)
@@ -204,6 +205,8 @@ static void bpf_program_hash_free(void)
bpf_program_hash = NULL;
}

+static void bpf_map_hash_free(void);
+
void bpf__clear(void)
{
struct bpf_perf_object *perf_obj, *tmp;
@@ -214,6 +217,7 @@ void bpf__clear(void)
}

bpf_program_hash_free();
+ bpf_map_hash_free();
}

static size_t ptr_hash(const void *__key, void *ctx __maybe_unused)
@@ -969,7 +973,7 @@ bpf_map_priv__purge(struct bpf_map_priv *priv)
}

static void
-bpf_map_priv__clear(struct bpf_map *map __maybe_unused,
+bpf_map_priv__clear(const struct bpf_map *map __maybe_unused,
void *_priv)
{
struct bpf_map_priv *priv = _priv;
@@ -978,6 +982,50 @@ bpf_map_priv__clear(struct bpf_map *map __maybe_unused,
free(priv);
}

+static void *map_priv(const struct bpf_map *map)
+{
+ void *priv;
+
+ if (!bpf_map_hash)
+ return NULL;
+ if (!hashmap__find(bpf_map_hash, map, &priv))
+ return NULL;
+ return priv;
+}
+
+static void bpf_map_hash_free(void)
+{
+ struct hashmap_entry *cur;
+ size_t bkt;
+
+ if (!bpf_map_hash)
+ return;
+
+ hashmap__for_each_entry(bpf_map_hash, cur, bkt)
+ bpf_map_priv__clear(cur->key, cur->value);
+
+ hashmap__free(bpf_map_hash);
+ bpf_map_hash = NULL;
+}
+
+static int map_set_priv(struct bpf_map *map, void *priv)
+{
+ void *old_priv;
+
+ if (!bpf_map_hash) {
+ bpf_map_hash = hashmap__new(ptr_hash, ptr_equal, NULL);
+ if (!bpf_map_hash)
+ return -ENOMEM;
+ }
+
+ old_priv = map_priv(map);
+ if (old_priv) {
+ bpf_map_priv__clear(map, old_priv);
+ return hashmap__set(bpf_map_hash, map, priv, NULL, NULL);
+ }
+ return hashmap__add(bpf_map_hash, map, priv);
+}
+
static int
bpf_map_op_setkey(struct bpf_map_op *op, struct parse_events_term *term)
{
@@ -1077,7 +1125,7 @@ static int
bpf_map__add_op(struct bpf_map *map, struct bpf_map_op *op)
{
const char *map_name = bpf_map__name(map);
- struct bpf_map_priv *priv = bpf_map__priv(map);
+ struct bpf_map_priv *priv = map_priv(map);

if (IS_ERR(priv)) {
pr_debug("Failed to get private from map %s\n", map_name);
@@ -1092,7 +1140,7 @@ bpf_map__add_op(struct bpf_map *map, struct bpf_map_op *op)
}
INIT_LIST_HEAD(&priv->ops_list);

- if (bpf_map__set_priv(map, priv, bpf_map_priv__clear)) {
+ if (map_set_priv(map, priv)) {
free(priv);
return -BPF_LOADER_ERRNO__INTERNAL;
}
@@ -1432,7 +1480,7 @@ bpf_map_config_foreach_key(struct bpf_map *map,
struct bpf_map_op *op;
const struct bpf_map_def *def;
const char *name = bpf_map__name(map);
- struct bpf_map_priv *priv = bpf_map__priv(map);
+ struct bpf_map_priv *priv = map_priv(map);

if (IS_ERR(priv)) {
pr_debug("ERROR: failed to get private from map %s\n", name);
@@ -1652,7 +1700,7 @@ struct evsel *bpf__setup_output_event(struct evlist *evlist, const char *name)
bool need_init = false;

bpf__perf_for_each_map_named(map, perf_obj, tmp, name) {
- struct bpf_map_priv *priv = bpf_map__priv(map);
+ struct bpf_map_priv *priv = map_priv(map);

if (IS_ERR(priv))
return ERR_PTR(-BPF_LOADER_ERRNO__INTERNAL);
@@ -1688,7 +1736,7 @@ struct evsel *bpf__setup_output_event(struct evlist *evlist, const char *name)
}

bpf__perf_for_each_map_named(map, perf_obj, tmp, name) {
- struct bpf_map_priv *priv = bpf_map__priv(map);
+ struct bpf_map_priv *priv = map_priv(map);

if (IS_ERR(priv))
return ERR_PTR(-BPF_LOADER_ERRNO__INTERNAL);
@@ -1700,7 +1748,7 @@ struct evsel *bpf__setup_output_event(struct evlist *evlist, const char *name)
if (!priv)
return ERR_PTR(-ENOMEM);

- err = bpf_map__set_priv(map, priv, bpf_map_priv__clear);
+ err = map_set_priv(map, priv);
if (err) {
bpf_map_priv__clear(map, priv);
return ERR_PTR(err);
--
2.35.1


2022-02-17 23:25:18

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH 2/3] perf tools: Remove bpf_map__set_priv/bpf_map__priv usage

On Thu, Feb 17, 2022 at 5:19 AM Jiri Olsa <[email protected]> wrote:
>
> Both bpf_map__set_priv/bpf_map__priv are deprecated
> and will be eventually removed.
>
> Using hashmap to replace that functionality.
>
> Suggested-by: Andrii Nakryiko <[email protected]>
> Signed-off-by: Jiri Olsa <[email protected]>
> ---
> tools/perf/util/bpf-loader.c | 62 ++++++++++++++++++++++++++++++++----
> 1 file changed, 55 insertions(+), 7 deletions(-)
>

[...]

> +static int map_set_priv(struct bpf_map *map, void *priv)
> +{
> + void *old_priv;
> +
> + if (!bpf_map_hash) {
> + bpf_map_hash = hashmap__new(ptr_hash, ptr_equal, NULL);
> + if (!bpf_map_hash)

same as in previous patch, on error this is not going to be NULL

> + return -ENOMEM;
> + }
> +
> + old_priv = map_priv(map);
> + if (old_priv) {
> + bpf_map_priv__clear(map, old_priv);
> + return hashmap__set(bpf_map_hash, map, priv, NULL, NULL);
> + }
> + return hashmap__add(bpf_map_hash, map, priv);
> +}
> +

[...]

2022-02-18 09:21:49

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH 2/3] perf tools: Remove bpf_map__set_priv/bpf_map__priv usage

On Thu, Feb 17, 2022 at 01:49:39PM -0800, Andrii Nakryiko wrote:
> On Thu, Feb 17, 2022 at 5:19 AM Jiri Olsa <[email protected]> wrote:
> >
> > Both bpf_map__set_priv/bpf_map__priv are deprecated
> > and will be eventually removed.
> >
> > Using hashmap to replace that functionality.
> >
> > Suggested-by: Andrii Nakryiko <[email protected]>
> > Signed-off-by: Jiri Olsa <[email protected]>
> > ---
> > tools/perf/util/bpf-loader.c | 62 ++++++++++++++++++++++++++++++++----
> > 1 file changed, 55 insertions(+), 7 deletions(-)
> >
>
> [...]
>
> > +static int map_set_priv(struct bpf_map *map, void *priv)
> > +{
> > + void *old_priv;
> > +
> > + if (!bpf_map_hash) {
> > + bpf_map_hash = hashmap__new(ptr_hash, ptr_equal, NULL);
> > + if (!bpf_map_hash)
>
> same as in previous patch, on error this is not going to be NULL

yes, will change

jirka

>
> > + return -ENOMEM;
> > + }
> > +
> > + old_priv = map_priv(map);
> > + if (old_priv) {
> > + bpf_map_priv__clear(map, old_priv);
> > + return hashmap__set(bpf_map_hash, map, priv, NULL, NULL);
> > + }
> > + return hashmap__add(bpf_map_hash, map, priv);
> > +}
> > +
>
> [...]