On Thu, 1 Oct 2020 at 02:30, Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote: > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > > index 32dc444224d8..5412aa7169db 100644 > > --- a/tools/lib/bpf/libbpf.c > > +++ b/tools/lib/bpf/libbpf.c > > @@ -4215,7 +4215,7 @@ bpf_object__create_maps(struct bpf_object *obj) > > if (map->fd >= 0) { > > pr_debug("map '%s': skipping creation (preset fd=%d)\n", > > map->name, map->fd); > > - continue; > > + goto check_pin_path; > > } > > > > err = bpf_object__create_map(obj, map); > > @@ -4258,6 +4258,7 @@ bpf_object__create_maps(struct bpf_object *obj) > > map->init_slots_sz = 0; > > } > > > > +check_pin_path: > > if (map->pin_path && !map->pinned) { > > err = bpf_map__pin(map, NULL); > > if (err) { > > > > > > Do you think if this change be better? > > Yes, of course. Just don't do it through use of goto. Guard map > creation with that if instead. Hi Andrii, Looks I missed something, Would you like to explain why we should not use goto? And for "guard map creation with the if", do you mean duplicate the if (map->pin_path && !map->pinned) in if (map->fd >= 0)? like diff --git a/src/libbpf.c b/src/libbpf.c index 3df1f4d..705abcb 100644 --- a/src/libbpf.c +++ b/src/libbpf.c @@ -4215,6 +4215,15 @@ bpf_object__create_maps(struct bpf_object *obj) if (map->fd >= 0) { pr_debug("map '%s': skipping creation (preset fd=%d)\n", map->name, map->fd); + if (map->pin_path && !map->pinned) { + err = bpf_map__pin(map, NULL); + if (err) { + pr_warn("map '%s': failed to auto-pin at '%s': %d\n", + map->name, map->pin_path, err); + zclose(map->fd); + goto err_out; + } + } continue; } (Sorry if the code format got corrupted as I replied in web gmail....) Thanks Hangbin