netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hangbin Liu <liuhangbin@gmail.com>
To: bpf@vger.kernel.org
Cc: "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>,
	"Andrii Nakryiko" <andrii.nakryiko@gmail.com>,
	netdev@vger.kernel.org,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Hangbin Liu" <liuhangbin@gmail.com>
Subject: [PATCHv2 bpf 2/3] libbpf: check if pin_path was set even map fd exist
Date: Sat,  3 Oct 2020 16:55:04 +0800	[thread overview]
Message-ID: <20201003085505.3388332-3-liuhangbin@gmail.com> (raw)
In-Reply-To: <20201003085505.3388332-1-liuhangbin@gmail.com>

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.

v2: keep if condition with existing order

Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
 tools/lib/bpf/libbpf.c | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index c8b5fe45682d..1fb3fd733b23 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4245,29 +4245,28 @@ 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;
+		} else {
+			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);
+			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);
-				goto err_out;
+			if (bpf_map__is_internal(map)) {
+				err = bpf_object__populate_internal_map(obj, map);
+				if (err < 0) {
+					zclose(map->fd);
+					goto err_out;
+				}
 			}
-		}
 
-		if (map->init_slots_sz) {
-			err = init_map_slots(map);
-			if (err < 0) {
-				zclose(map->fd);
-				goto err_out;
+			if (map->init_slots_sz) {
+				err = init_map_slots(map);
+				if (err < 0) {
+					zclose(map->fd);
+					goto err_out;
+				}
 			}
 		}
 
-- 
2.25.4


  parent reply	other threads:[~2020-10-03  8:55 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
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   ` Hangbin Liu [this message]
2020-10-05 21:22     ` [PATCHv2 bpf 2/3] libbpf: check if pin_path was set even map fd exist 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=20201003085505.3388332-3-liuhangbin@gmail.com \
    --to=liuhangbin@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).