On Thu, Oct 1, 2020 at 4:34 AM Hangbin Liu <liuhangbin@xxxxxxxxx> wrote: > > 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? Because goto shouldn't be a default way of altering the control flow. > 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 I mean something like: if (map->pin_path) { ... } if (map fd < 0) { bpf_object__create_map(..); if (bpf_map__is_internal(..)) { ... } if (map->init_slot_sz) { ...} } if (map->pin_path && !map->pinned) { ... } > > 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