libbpf: Check if pin_path was set even map fd exist
authorHangbin Liu <liuhangbin@gmail.com>
Tue, 6 Oct 2020 02:13:44 +0000 (10:13 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 6 Oct 2020 18:10:20 +0000 (11:10 -0700)
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>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20201006021345.3817033-3-liuhangbin@gmail.com
tools/lib/bpf/libbpf.c

index 7cec237d9d1ff633013ae7bc45af533adad16031..9f90d1a686fd1f02083af4d68c6c6f544c73a6b5 100644 (file)
@@ -4256,29 +4256,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;
+                               }
                        }
                }