Skip to contentSkip to conversation

libbpf: export bpf_object__reuse_map() to libbpf api

PATCH In review Hangbin Liu 2 participants 8 messages Sep 29, 2020 – Oct 1, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: export bpf_object__reuse_map() to libbpf api

Besides bpf_map__reuse_fd(), which could let us reuse existing map fd. bpf_object__reuse_map() could let us reuse existing pinned maps, which is helpful.

This functions could also be used when we add iproute2 libbpf support, so we don't need to re-use or re-implement new functions like bpf_obj_get()/bpf_map_selfcheck_pinned() in iproute2.

---
 tools/lib/bpf/libbpf.c | 3 +--
 tools/lib/bpf/libbpf.h | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +1 -2
index 32dc444224d8..e835d7a3437f 100644
@@ -4033,8 +4033,7 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
 		map_info.map_flags == map->def.map_flags);
 }
 
-static int
-bpf_object__reuse_map(struct bpf_map *map)
+int bpf_object__reuse_map(struct bpf_map *map)
 {
 	char *cp, errmsg[STRERR_BUFSIZE];
 	int err, pin_fd;
tools/lib/bpf/libbpf.h +1 -0
index a750f67a23f6..4b9e615eb393 100644
@@ -431,6 +431,7 @@ bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
 /* get/set map FD */
 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
+LIBBPF_API int bpf_object__reuse_map(struct bpf_map *map);
 /* get map definition */
 LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
 /* get map name */
Signature · 1 line
2.25.4
#1 lore raw
Quoted 34 lines · Hangbin Liu

Besides bpf_map__reuse_fd(), which could let us reuse existing map fd. bpf_object__reuse_map() could let us reuse existing pinned maps, which is helpful.

This functions could also be used when we add iproute2 libbpf support, so we don't need to re-use or re-implement new functions like bpf_obj_get()/bpf_map_selfcheck_pinned() in iproute2.

---
 tools/lib/bpf/libbpf.c | 3 +--
 tools/lib/bpf/libbpf.h | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +1 -2
index 32dc444224d8..e835d7a3437f 100644
@@ -4033,8 +4033,7 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
                map_info.map_flags == map->def.map_flags);
 }
 
-static int
-bpf_object__reuse_map(struct bpf_map *map)
+int bpf_object__reuse_map(struct bpf_map *map)
 {
        char *cp, errmsg[STRERR_BUFSIZE];
        int err, pin_fd;
tools/lib/bpf/libbpf.h +0 -0
index a750f67a23f6..4b9e615eb393 100644

tools/lib/bpf/libbpf.h bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj); · line 434

Quoted 4 lines
tools/lib/bpf/libbpf.h +1 -0
@@ -431,6 +431,7 @@ bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
 /* get/set map FD */
 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
+LIBBPF_API int bpf_object__reuse_map(struct bpf_map *map);

It's internal function, which doesn't check that map->pin_path is set, for one thing. It shouldn't be exposed. libbpf exposes bpf_map__set_pin_path() to set pin_path for any map, and then during load time libbpf with "reuse map", if pin_path is not NULL. Doesn't that work for you?

Quoted 6 lines
tools/lib/bpf/libbpf.h +0 -1
 /* get map definition */
 LIBBPF_API const struct bpf_map_def *bpf_map__def(const struct bpf_map *map);
 /* get map name */
--

2.25.4

#2 lore raw
Quoted 7 lines · Andrii Nakryiko

tools/lib/bpf/libbpf.h bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj); · line 434

Quoted 5 lines
tools/lib/bpf/libbpf.h +1 -0
@@ -431,6 +431,7 @@ bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
 /* get/set map FD */
 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
+LIBBPF_API int bpf_object__reuse_map(struct bpf_map *map);

It's internal function, which doesn't check that map->pin_path is set,

How about add a path check in bpf_object__reuse_map()?

And off course users who use it should call bpf_map__set_pin_path() first.

Quoted 4 lines

for one thing. It shouldn't be exposed. libbpf exposes bpf_map__set_pin_path() to set pin_path for any map, and then during load time libbpf with "reuse map", if pin_path is not NULL. Doesn't that work for you?

Long story...

When trying to add iproute2 libbpf support that I'm working on. We need to create iproute2 legacy map-in-map manually before libbpf load objects, because libbpf only support BTF type map-in-map(unless I missed something.).

After creating legacy map-in-map and reuse the fd, if the map has legacy pining defined, only set the pin path would not enough as libbpf will skip pinning map if map->fd > 0 in bpf_object__create_maps(). See the following code bellow.

bpf_map__set_pin_path()
bpf_create_map_in_map() <- create inner or outer map
bpf_map__reuse_fd(map, inner/outer_fd)
bpf_object__load(obj)
- bpf_object__load_xattr()
- bpf_object__create_maps()
- if (map->fd >= 0)
continue <- this will skip pinning map

So when handle legacy map-in-map + pin map, we need to create the map and pin maps manually at the same time. The code would looks like (err handler skipped).

map_fd = bpf_obj_get(pathname);

if (map_fd > 0) {
	bpf_map__set_pin_path(map, pathname);
	return bpf_object__reuse_map(map);   <- here we need the reuse_map
}

bpf_create_map_in_map()
bpf_map__reuse_fd(map, map_fd);
bpf_map__pin(map, pathname);

So I think this function is needed, what do you think?

Thanks
Hangbin

#3 lore raw
Quoted 30 lines · Hangbin Liu
Quoted 7 lines · Andrii Nakryiko

tools/lib/bpf/libbpf.h bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj); · line 434

Quoted 5 lines
tools/lib/bpf/libbpf.h +1 -0
@@ -431,6 +431,7 @@ bpf_map__prev(const struct bpf_map *map, const struct bpf_object *obj);
 /* get/set map FD */
 LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
+LIBBPF_API int bpf_object__reuse_map(struct bpf_map *map);

It's internal function, which doesn't check that map->pin_path is set,

How about add a path check in bpf_object__reuse_map()?

And off course users who use it should call bpf_map__set_pin_path() first.

Quoted 4 lines

for one thing. It shouldn't be exposed. libbpf exposes bpf_map__set_pin_path() to set pin_path for any map, and then during load time libbpf with "reuse map", if pin_path is not NULL. Doesn't that work for you?

Long story...

When trying to add iproute2 libbpf support that I'm working on. We need to create iproute2 legacy map-in-map manually before libbpf load objects, because libbpf only support BTF type map-in-map(unless I missed something.).

After creating legacy map-in-map and reuse the fd, if the map has legacy pining defined, only set the pin path would not enough as libbpf will skip pinning map if map->fd > 0 in bpf_object__create_maps(). See the following code bellow.

Quoted 8 lines

bpf_map__set_pin_path()
bpf_create_map_in_map() <- create inner or outer map
bpf_map__reuse_fd(map, inner/outer_fd)
bpf_object__load(obj)
- bpf_object__load_xattr()
- bpf_object__create_maps()
- if (map->fd >= 0)
continue <- this will skip pinning map

so maybe that's the part that needs to be fixed?..

Quoted 14 lines

So when handle legacy map-in-map + pin map, we need to create the map and pin maps manually at the same time. The code would looks like (err handler skipped).

map_fd = bpf_obj_get(pathname);
if (map_fd > 0) {
bpf_map__set_pin_path(map, pathname);
return bpf_object__reuse_map(map); <- here we need the reuse_map
}
bpf_create_map_in_map()
bpf_map__reuse_fd(map, map_fd);
bpf_map__pin(map, pathname);

Quoted 1 line

So I think this function is needed, what do you think?

I'm still not sure. And to be honest your examples are still a bit too succinct for me to follow where the problem is exactly. Can you please elaborate a bit more?

It might very well be that map pinning and FD reuse have buggy and convoluted logic, but let's try to fix that first, before we expose new APIs.

Quoted 3 lines

Thanks
Hangbin

#4 lore raw
Quoted 10 lines · Andrii Nakryiko
Quoted 8 lines

bpf_map__set_pin_path()
bpf_create_map_in_map() <- create inner or outer map
bpf_map__reuse_fd(map, inner/outer_fd)
bpf_object__load(obj)
- bpf_object__load_xattr()
- bpf_object__create_maps()
- if (map->fd >= 0)
continue <- this will skip pinning map

so maybe that's the part that needs to be fixed?..

Hmm...maybe, let me see

Quoted 4 lines

I'm still not sure. And to be honest your examples are still a bit too succinct for me to follow where the problem is exactly. Can you please elaborate a bit more?

Let's take iproute2 legacy map for example, if it's a map-in-map type with pin path defined. In user space we could do like:

if (bpf_obj_get(pathname) < 0) {
	bpf_create_map_in_map();
	bpf_map__reuse_fd(map, map_fd);
}

bpf_map__set_pin_path(map, pathname);
bpf_object__load(obj)

So in libbpf we need

tools/lib/bpf/libbpf.c +2 -1
index 32dc444224d8..5412aa7169db 100644
@@ -4215,7 +4215,7 @@ 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;
+                       goto check_pin_path;
                }
 
                err = bpf_object__create_map(obj, map);
@@ -4258,6 +4258,7 @@ bpf_object__create_maps(struct bpf_object *obj)
                        map->init_slots_sz = 0;
                }
 
+check_pin_path:
                if (map->pin_path && !map->pinned) {
                        err = bpf_map__pin(map, NULL);
                        if (err) {

Do you think if this change be better?

Thanks
Hangbin

#5 lore raw
Quoted 55 lines · Hangbin Liu
Quoted 10 lines · Andrii Nakryiko
Quoted 8 lines

bpf_map__set_pin_path()
bpf_create_map_in_map() <- create inner or outer map
bpf_map__reuse_fd(map, inner/outer_fd)
bpf_object__load(obj)
- bpf_object__load_xattr()
- bpf_object__create_maps()
- if (map->fd >= 0)
continue <- this will skip pinning map

so maybe that's the part that needs to be fixed?..

Hmm...maybe, let me see

Quoted 4 lines

I'm still not sure. And to be honest your examples are still a bit too succinct for me to follow where the problem is exactly. Can you please elaborate a bit more?

Let's take iproute2 legacy map for example, if it's a map-in-map type with pin path defined. In user space we could do like:

if (bpf_obj_get(pathname) < 0) {
        bpf_create_map_in_map();
        bpf_map__reuse_fd(map, map_fd);
}

bpf_map__set_pin_path(map, pathname);
bpf_object__load(obj)

So in libbpf we need

tools/lib/bpf/libbpf.c +2 -1
index 32dc444224d8..5412aa7169db 100644
@@ -4215,7 +4215,7 @@ 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;
+                       goto check_pin_path;
                }
 
                err = bpf_object__create_map(obj, map);
@@ -4258,6 +4258,7 @@ bpf_object__create_maps(struct bpf_object *obj)
                        map->init_slots_sz = 0;
                }
 
+check_pin_path:
                if (map->pin_path && !map->pinned) {
                        err = bpf_map__pin(map, NULL);
                        if (err) {
Quoted 1 line

Do you think if this change be better?

Yes, of course. Just don't do it through use of goto. Guard map creation with that if instead.

Quoted 3 lines

Thanks
Hangbin

#6 lore raw
Quoted 23 lines · Andrii Nakryiko
Quoted 23 lines
tools/lib/bpf/libbpf.c +2 -1
index 32dc444224d8..5412aa7169db 100644
@@ -4215,7 +4215,7 @@ 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;
+                       goto check_pin_path;
                }
 
                err = bpf_object__create_map(obj, map);
@@ -4258,6 +4258,7 @@ bpf_object__create_maps(struct bpf_object *obj)
                        map->init_slots_sz = 0;
                }
 
+check_pin_path:
                if (map->pin_path && !map->pinned) {
                        err = bpf_map__pin(map, NULL);
                        if (err) {
Quoted 4 lines
Quoted 1 line

Do you think if this change be better?

Yes, of course. Just don't do it through use of goto. Guard map creation with that if instead.

Hi Andrii,

Looks I missed something, Would you like to explain why we should not use goto? And for "guard map creation with the if", do you mean duplicate the if (map->pin_path && !map->pinned) in if (map->fd >= 0)? like

src/libbpf.c +4 -0
index 3df1f4d..705abcb 100644
@@ -4215,6 +4215,15 @@ 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);
+                       if (map->pin_path && !map->pinned) {
+                               err = bpf_map__pin(map, NULL);
+                               if (err) {
+                                       pr_warn("map '%s': failed to

auto-pin at '%s': %d\n",
+ map->name, map->pin_path, err);
+ zclose(map->fd);
+ goto err_out;
+ }
+ }

                        continue;
                }

(Sorry if the code format got corrupted as I replied in web gmail....)

Thanks
Hangbin

#7 lore raw
Quoted 25 lines · Hangbin Liu
Quoted 23 lines · Andrii Nakryiko
Quoted 23 lines
tools/lib/bpf/libbpf.c +2 -1
index 32dc444224d8..5412aa7169db 100644
@@ -4215,7 +4215,7 @@ 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;
+                       goto check_pin_path;
                }
 
                err = bpf_object__create_map(obj, map);
@@ -4258,6 +4258,7 @@ bpf_object__create_maps(struct bpf_object *obj)
                        map->init_slots_sz = 0;
                }
 
+check_pin_path:
                if (map->pin_path && !map->pinned) {
                        err = bpf_map__pin(map, NULL);
                        if (err) {
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Do you think if this change be better?

Yes, of course. Just don't do it through use of goto. Guard map creation with that if instead.

Hi Andrii,

Looks I missed something, Would you like to explain why we should not use goto?

Because goto shouldn't be a default way of altering the control flow.

Quoted 2 lines

And for "guard map creation with the if", do you mean duplicate the if (map->pin_path && !map->pinned) in if (map->fd >= 0)? like

I mean something like:

if (map->pin_path) { ... }

if (map fd < 0) {
bpf_object__create_map(..);
if (bpf_map__is_internal(..)) { ... }
if (map->init_slot_sz) { ...}
}

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

Quoted 26 lines
src/libbpf.c +9 -0
index 3df1f4d..705abcb 100644
@@ -4215,6 +4215,15 @@ 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);
+                       if (map->pin_path && !map->pinned) {
+                               err = bpf_map__pin(map, NULL);
+                               if (err) {
+                                       pr_warn("map '%s': failed to
 auto-pin at '%s': %d\n",
+                                               map->name, map->pin_path, err);
+                                       zclose(map->fd);
+                                       goto err_out;
+                               }
+                       }
                        continue;
                }

(Sorry if the code format got corrupted as I replied in web gmail....)

Thanks
Hangbin