Skip to content

Commit fd08f94

Browse files
author
Alexei Starovoitov
committed
Merge branch 'Fix pining maps after reuse map fd'
Hangbin Liu says: ==================== When a user reuse map fd after creating a map manually and set the pin_path, then load the object via libbpf. bpf_object__create_maps() will skip pinning map if map fd exist. Fix it by add moving bpf creation to else condition and go on checking map pin_path after that. v3: for selftest: use CHECK() for bpf_object__open_file() and close map fd on error v2: a) close map fd if init map slots failed b) add bpf selftest for this scenario ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents dca4121 + 44c4aa2 commit fd08f94

File tree

2 files changed

+94
-35
lines changed

2 files changed

+94
-35
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4203,6 +4203,36 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map)
42034203
return 0;
42044204
}
42054205

4206+
static int init_map_slots(struct bpf_map *map)
4207+
{
4208+
const struct bpf_map *targ_map;
4209+
unsigned int i;
4210+
int fd, err;
4211+
4212+
for (i = 0; i < map->init_slots_sz; i++) {
4213+
if (!map->init_slots[i])
4214+
continue;
4215+
4216+
targ_map = map->init_slots[i];
4217+
fd = bpf_map__fd(targ_map);
4218+
err = bpf_map_update_elem(map->fd, &i, &fd, 0);
4219+
if (err) {
4220+
err = -errno;
4221+
pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
4222+
map->name, i, targ_map->name,
4223+
fd, err);
4224+
return err;
4225+
}
4226+
pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
4227+
map->name, i, targ_map->name, fd);
4228+
}
4229+
4230+
zfree(&map->init_slots);
4231+
map->init_slots_sz = 0;
4232+
4233+
return 0;
4234+
}
4235+
42064236
static int
42074237
bpf_object__create_maps(struct bpf_object *obj)
42084238
{
@@ -4226,47 +4256,29 @@ bpf_object__create_maps(struct bpf_object *obj)
42264256
if (map->fd >= 0) {
42274257
pr_debug("map '%s': skipping creation (preset fd=%d)\n",
42284258
map->name, map->fd);
4229-
continue;
4230-
}
4231-
4232-
err = bpf_object__create_map(obj, map);
4233-
if (err)
4234-
goto err_out;
4235-
4236-
pr_debug("map '%s': created successfully, fd=%d\n", map->name,
4237-
map->fd);
4238-
4239-
if (bpf_map__is_internal(map)) {
4240-
err = bpf_object__populate_internal_map(obj, map);
4241-
if (err < 0) {
4242-
zclose(map->fd);
4259+
} else {
4260+
err = bpf_object__create_map(obj, map);
4261+
if (err)
42434262
goto err_out;
4244-
}
4245-
}
42464263

4247-
if (map->init_slots_sz) {
4248-
for (j = 0; j < map->init_slots_sz; j++) {
4249-
const struct bpf_map *targ_map;
4250-
int fd;
4264+
pr_debug("map '%s': created successfully, fd=%d\n",
4265+
map->name, map->fd);
42514266

4252-
if (!map->init_slots[j])
4253-
continue;
4267+
if (bpf_map__is_internal(map)) {
4268+
err = bpf_object__populate_internal_map(obj, map);
4269+
if (err < 0) {
4270+
zclose(map->fd);
4271+
goto err_out;
4272+
}
4273+
}
42544274

4255-
targ_map = map->init_slots[j];
4256-
fd = bpf_map__fd(targ_map);
4257-
err = bpf_map_update_elem(map->fd, &j, &fd, 0);
4258-
if (err) {
4259-
err = -errno;
4260-
pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
4261-
map->name, j, targ_map->name,
4262-
fd, err);
4275+
if (map->init_slots_sz) {
4276+
err = init_map_slots(map);
4277+
if (err < 0) {
4278+
zclose(map->fd);
42634279
goto err_out;
42644280
}
4265-
pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
4266-
map->name, j, targ_map->name, fd);
42674281
}
4268-
zfree(&map->init_slots);
4269-
map->init_slots_sz = 0;
42704282
}
42714283

42724284
if (map->pin_path && !map->pinned) {

tools/testing/selftests/bpf/prog_tests/pinning.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void test_pinning(void)
3737
struct stat statbuf = {};
3838
struct bpf_object *obj;
3939
struct bpf_map *map;
40-
int err;
40+
int err, map_fd;
4141
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
4242
.pin_root_path = custpath,
4343
);
@@ -213,6 +213,53 @@ void test_pinning(void)
213213
if (CHECK(err, "stat custpinpath", "err %d errno %d\n", err, errno))
214214
goto out;
215215

216+
/* remove the custom pin path to re-test it with reuse fd below */
217+
err = unlink(custpinpath);
218+
if (CHECK(err, "unlink custpinpath", "err %d errno %d\n", err, errno))
219+
goto out;
220+
221+
err = rmdir(custpath);
222+
if (CHECK(err, "rmdir custpindir", "err %d errno %d\n", err, errno))
223+
goto out;
224+
225+
bpf_object__close(obj);
226+
227+
/* test pinning at custom path with reuse fd */
228+
obj = bpf_object__open_file(file, NULL);
229+
err = libbpf_get_error(obj);
230+
if (CHECK(err, "default open", "err %d errno %d\n", err, errno)) {
231+
obj = NULL;
232+
goto out;
233+
}
234+
235+
map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(__u32),
236+
sizeof(__u64), 1, 0);
237+
if (CHECK(map_fd < 0, "create pinmap manually", "fd %d\n", map_fd))
238+
goto out;
239+
240+
map = bpf_object__find_map_by_name(obj, "pinmap");
241+
if (CHECK(!map, "find map", "NULL map"))
242+
goto close_map_fd;
243+
244+
err = bpf_map__reuse_fd(map, map_fd);
245+
if (CHECK(err, "reuse pinmap fd", "err %d errno %d\n", err, errno))
246+
goto close_map_fd;
247+
248+
err = bpf_map__set_pin_path(map, custpinpath);
249+
if (CHECK(err, "set pin path", "err %d errno %d\n", err, errno))
250+
goto close_map_fd;
251+
252+
err = bpf_object__load(obj);
253+
if (CHECK(err, "custom load", "err %d errno %d\n", err, errno))
254+
goto close_map_fd;
255+
256+
/* check that pinmap was pinned at the custom path */
257+
err = stat(custpinpath, &statbuf);
258+
if (CHECK(err, "stat custpinpath", "err %d errno %d\n", err, errno))
259+
goto close_map_fd;
260+
261+
close_map_fd:
262+
close(map_fd);
216263
out:
217264
unlink(pinpath);
218265
unlink(nopinpath);

0 commit comments

Comments
 (0)