On Tue, Oct 29, 2019 at 12:39 PM Toke Høiland-Jørgensen <toke@xxxxxxxxxx> wrote: > > From: Toke Høiland-Jørgensen <toke@xxxxxxxxxx> > > This adds support to libbpf for setting map pinning information as part of > the BTF map declaration, to get automatic map pinning (and reuse) on load. > The pinning type currently only supports a single PIN_BY_NAME mode, where > each map will be pinned by its name in a path that can be overridden, but > defaults to /sys/fs/bpf. > > Since auto-pinning only does something if any maps actually have a > 'pinning' BTF attribute set, we default the new option to enabled, on the > assumption that seamless pinning is what most callers want. > > When a map has a pin_path set at load time, libbpf will compare the map > pinned at that location (if any), and if the attributes match, will re-use > that map instead of creating a new one. If no existing map is found, the > newly created map will instead be pinned at the location. > > Programs wanting to customise the pinning can override the pinning paths > using bpf_map__set_pin_path() before calling bpf_object__load() (including > setting it to NULL to disable pinning of a particular map). > > Signed-off-by: Toke Høiland-Jørgensen <toke@xxxxxxxxxx> > --- Please fix unconditional pin_path setting, with that: Acked-by: Andrii Nakryiko <andriin@xxxxxx> > tools/lib/bpf/bpf_helpers.h | 6 ++ > tools/lib/bpf/libbpf.c | 144 +++++++++++++++++++++++++++++++++++++++++-- > tools/lib/bpf/libbpf.h | 13 ++++ > 3 files changed, 154 insertions(+), 9 deletions(-) > > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h > index 2203595f38c3..0c7d28292898 100644 > --- a/tools/lib/bpf/bpf_helpers.h > +++ b/tools/lib/bpf/bpf_helpers.h > @@ -38,4 +38,10 @@ struct bpf_map_def { > unsigned int map_flags; > }; > [...] > @@ -1270,6 +1292,28 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj, > } > map->def.value_size = sz; > map->btf_value_type_id = t->type; > + } else if (strcmp(name, "pinning") == 0) { > + __u32 val; > + int err; > + > + if (!get_map_field_int(map_name, obj->btf, def, m, > + &val)) > + return -EINVAL; > + pr_debug("map '%s': found pinning = %u.\n", > + map_name, val); > + > + if (val != LIBBPF_PIN_NONE && > + val != LIBBPF_PIN_BY_NAME) { > + pr_warn("map '%s': invalid pinning value %u.\n", > + map_name, val); > + return -EINVAL; > + } > + err = build_map_pin_path(map, pin_root_path); uhm... only if (val == LIBBPF_PIN_BY_NAME)?.. maybe extend tests with a mix if auto-pinned and never pinned map to catch issue like this? > + if (err) { > + pr_warn("map '%s': couldn't build pin path.\n", > + map_name); > + return err; > + } > } else { > if (strict) { > pr_warn("map '%s': unknown field '%s'.\n", > @@ -1289,7 +1333,8 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj, > return 0; > } > [...]