All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Hangbin Liu <liuhangbin@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, "Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
	"Andrii Nakryiko" <andriin@fb.com>,
	Networking <netdev@vger.kernel.org>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>
Subject: Re: [PATCH libbpf] libbpf: check if pin_path was set even map fd exist
Date: Fri, 2 Oct 2020 11:11:54 -0700	[thread overview]
Message-ID: <CAEf4BzZTBxCKp2JT0yuwBwWX-EuqdSAOM7Duz7ifkRzeYKbKJw@mail.gmail.com> (raw)
In-Reply-To: <20201002075750.1978298-1-liuhangbin@gmail.com>

On Fri, Oct 2, 2020 at 12:58 AM Hangbin Liu <liuhangbin@gmail.com> wrote:
>
> Say a user reuse map fd after creating a map manually and set the
> pin_path, then load the object via libbpf.
>
> In libbpf bpf_object__create_maps(), bpf_object__reuse_map() will
> return 0 if there is no pinned map in map->pin_path. Then after
> checking if map fd exist, we should also check if pin_path was set
> and do bpf_map__pin() instead of continue the loop.
>
> Fix it by creating map if fd not exist and continue checking pin_path
> after that.
>
> Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
> ---
>  tools/lib/bpf/libbpf.c | 75 +++++++++++++++++++++---------------------
>  1 file changed, 37 insertions(+), 38 deletions(-)
>

Please add a selftests that validates the logic you are going to rely on.

> +                                       targ_map = map->init_slots[j];
> +                                       fd = bpf_map__fd(targ_map);
> +                                       err = bpf_map_update_elem(map->fd, &j, &fd, 0);
> +                                       if (err) {
> +                                               err = -errno;
> +                                               pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
> +                                                       map->name, j, targ_map->name,
> +                                                       fd, err);

I just noticed that we don't zclose(map->fd) here, can you please fix
it with a separate patch along these changes? Thanks!

> +                                               goto err_out;
> +                                       }
> +                                       pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
> +                                               map->name, j, targ_map->name, fd);
> +                               }
> +                               zfree(&map->init_slots);
> +                               map->init_slots_sz = 0;

Let's move this slot initting logic into a helper function
(init_map_slots() or something like that? doesn't have to use
"bpf_object__" prefix as it is internal static function), that will
simplify overall flow.

> +                       }
> +               } else {
> +                       pr_debug("map '%s': skipping creation (preset fd=%d)\n",
> +                                map->name, map->fd);

to make diff a bit smaller, maybe let's keep the original order, but
do if/else instead of continuing:

if (map->fd >= 0) {
    pr_debug("skipping...");
} else {
   /* do the creation here */
}

/* pinning logic here */

>                 }
>
>                 if (map->pin_path && !map->pinned) {
> --
> 2.25.4
>

  parent reply	other threads:[~2020-10-02 18:12 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-02  7:57 [PATCH libbpf] libbpf: check if pin_path was set even map fd exist Hangbin Liu
2020-10-02 11:49 ` Maciej Fijalkowski
2020-10-02 18:13   ` Andrii Nakryiko
2020-10-02 18:11 ` Andrii Nakryiko [this message]
2020-10-03  8:55 ` [PATCHv2 bpf 0/3] Fix pining maps after reuse map fd Hangbin Liu
2020-10-03  8:55   ` [PATCHv2 bpf 1/3] libbpf: close map fd if init map slots failed Hangbin Liu
2020-10-05 21:21     ` Andrii Nakryiko
2020-10-03  8:55   ` [PATCHv2 bpf 2/3] libbpf: check if pin_path was set even map fd exist Hangbin Liu
2020-10-05 21:22     ` Andrii Nakryiko
2020-10-03  8:55   ` [PATCHv2 bpf 3/3] selftest/bpf: test pinning map with reused map fd Hangbin Liu
2020-10-05 21:28     ` Andrii Nakryiko
2020-10-06  2:13   ` [PATCHv3 bpf 0/3] Fix pining maps after reuse " Hangbin Liu
2020-10-06  2:13     ` [PATCHv3 bpf 1/3] libbpf: close map fd if init map slots failed Hangbin Liu
2020-10-06  2:13     ` [PATCHv3 bpf 2/3] libbpf: check if pin_path was set even map fd exist Hangbin Liu
2020-10-06  2:13     ` [PATCHv3 bpf 3/3] selftest/bpf: test pinning map with reused map fd Hangbin Liu
2020-10-06  3:26       ` Andrii Nakryiko
2020-10-06 18:40     ` [PATCHv3 bpf 0/3] Fix pining maps after reuse " patchwork-bot+bpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAEf4BzZTBxCKp2JT0yuwBwWX-EuqdSAOM7Duz7ifkRzeYKbKJw@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=liuhangbin@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=toke@redhat.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.