All of lore.kernel.org
 help / color / mirror / Atom feed
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
To: Hangbin Liu <liuhangbin@gmail.com>
Cc: 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>,
	netdev@vger.kernel.org,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Andrii Nakryiko" <andrii.nakryiko@gmail.com>
Subject: Re: [PATCH libbpf] libbpf: check if pin_path was set even map fd exist
Date: Fri, 2 Oct 2020 13:49:36 +0200	[thread overview]
Message-ID: <20201002114936.GA20275@ranger.igk.intel.com> (raw)
In-Reply-To: <20201002075750.1978298-1-liuhangbin@gmail.com>

On Fri, Oct 02, 2020 at 03:57:50PM +0800, Hangbin Liu 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(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index e493d6048143..d4149585a76c 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -3861,50 +3861,49 @@ 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;
> -		}
> -
> -		err = bpf_object__create_map(obj, map);
> -		if (err)
> -			goto err_out;
> -
> -		pr_debug("map '%s': created successfully, fd=%d\n", map->name,
> -			 map->fd);
> -
> -		if (bpf_map__is_internal(map)) {
> -			err = bpf_object__populate_internal_map(obj, map);
> -			if (err < 0) {
> -				zclose(map->fd);
> +		if (map->fd < 0) {
> +			err = bpf_object__create_map(obj, map);
> +			if (err)
>  				goto err_out;
> -			}
> -		}
> -
> -		if (map->init_slots_sz) {
> -			for (j = 0; j < map->init_slots_sz; j++) {
> -				const struct bpf_map *targ_map;
> -				int fd;
>  
> -				if (!map->init_slots[j])
> -					continue;
> +			pr_debug("map '%s': created successfully, fd=%d\n", map->name,
> +				 map->fd);
>  
> -				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);
> +			if (bpf_map__is_internal(map)) {
> +				err = bpf_object__populate_internal_map(obj, map);
> +				if (err < 0) {
> +					zclose(map->fd);
>  					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;
> +
> +			if (map->init_slots_sz) {

Couldn't we flatten the code by inverting the logic here and using goto?

	if (!map->init_slot_sz) {
		pr_debug("map '%s': skipping creation (preset fd=%d)\n",
			 map->name, map->fd);
		goto map_pin;
	}

	(...)
map_pin:
	if (map->pin_path && !map->pinned) {

If I'm reading this right.

> +				for (j = 0; j < map->init_slots_sz; j++) {
> +					const struct bpf_map *targ_map;
> +					int fd;
> +
> +					if (!map->init_slots[j])
> +						continue;
> +
> +					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);
> +						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;
> +			}
> +		} else {
> +			pr_debug("map '%s': skipping creation (preset fd=%d)\n",
> +				 map->name, map->fd);
>  		}
>  
>  		if (map->pin_path && !map->pinned) {
> -- 
> 2.25.4
> 

  reply	other threads:[~2020-10-02 11:56 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 [this message]
2020-10-02 18:13   ` Andrii Nakryiko
2020-10-02 18:11 ` Andrii Nakryiko
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=20201002114936.GA20275@ranger.igk.intel.com \
    --to=maciej.fijalkowski@intel.com \
    --cc=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.