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>,
"Andrii Nakryiko" <andrii@kernel.org>
Subject: [PATCHv3 bpf 1/3] libbpf: close map fd if init map slots failed
Date: Tue, 6 Oct 2020 10:13:43 +0800 [thread overview]
Message-ID: <20201006021345.3817033-2-liuhangbin@gmail.com> (raw)
In-Reply-To: <20201006021345.3817033-1-liuhangbin@gmail.com>
Previously we forgot to close the map fd if bpf_map_update_elem()
failed during map slot init, which will leak map fd.
Let's move map slot initialization to new function init_map_slots() to
simplify the code. And close the map fd if init slot failed.
Reported-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
tools/lib/bpf/libbpf.c | 55 ++++++++++++++++++++++++++----------------
1 file changed, 34 insertions(+), 21 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 32dc444224d8..c8b5fe45682d 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4192,6 +4192,36 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map)
return 0;
}
+static int init_map_slots(struct bpf_map *map)
+{
+ const struct bpf_map *targ_map;
+ unsigned int i;
+ int fd, err;
+
+ for (i = 0; i < map->init_slots_sz; i++) {
+ if (!map->init_slots[i])
+ continue;
+
+ targ_map = map->init_slots[i];
+ fd = bpf_map__fd(targ_map);
+ err = bpf_map_update_elem(map->fd, &i, &fd, 0);
+ if (err) {
+ err = -errno;
+ pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
+ map->name, i, targ_map->name,
+ fd, err);
+ return err;
+ }
+ pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
+ map->name, i, targ_map->name, fd);
+ }
+
+ zfree(&map->init_slots);
+ map->init_slots_sz = 0;
+
+ return 0;
+}
+
static int
bpf_object__create_maps(struct bpf_object *obj)
{
@@ -4234,28 +4264,11 @@ bpf_object__create_maps(struct bpf_object *obj)
}
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;
-
- 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);
+ err = init_map_slots(map);
+ if (err < 0) {
+ zclose(map->fd);
+ goto err_out;
}
- zfree(&map->init_slots);
- map->init_slots_sz = 0;
}
if (map->pin_path && !map->pinned) {
--
2.25.4
next prev parent reply other threads:[~2020-10-06 2:15 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 ` [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 ` Hangbin Liu [this message]
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=20201006021345.3817033-2-liuhangbin@gmail.com \
--to=liuhangbin@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--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).