Skip to contentSkip to conversation

libbpf: add "bool skipped" to struct bpf_map

PATCH Superseded Shuyi Cheng 2 participants 2 messages Dec 10, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libbpf: add "bool skipped" to struct bpf_map

Fix error: "failed to pin map: Bad file descriptor, path: /sys/fs/bpf/_rodata_str1_1."

In the old kernel, the global data map will not be created, see [0]. So we should skip the pinning of the global data map to avoid bpf_object__pin_maps returning error. Therefore, when the map is not created, we mark “map->skipped" as true and then check during relocation and during pinning.

---
v1: https://lore.kernel.org/bpf/CAEf4BzbtQGnGZTLbTdy1GHK54f5S7YNFQak7BuEfaqGEwqNNJA@mail.gmail.com/T/#m80ec7f8bc69dbcf4a5945e2aa6f16145901afc40
v1->v2:
-- add "bool skipped" to struct bpf_map.
-- replace "bpf_map__is_internal(map) && !kernel_supports(obj, FEAT_GLOBAL_DATA))" with map->skipped

 tools/lib/bpf/libbpf.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +8 -3
index 18d95c6a89fe..a5bad6b43c15 100644
@@ -432,6 +432,7 @@ struct bpf_map {
 	bool pinned;
 	bool reused;
 	__u64 map_extra;
+	bool skipped;
 };
 
 enum extern_type {
@@ -5087,8 +5088,10 @@ bpf_object__create_maps(struct bpf_object *obj)
 		 * kernels.
 		 */
 		if (bpf_map__is_internal(map) &&
-		    !kernel_supports(obj, FEAT_GLOBAL_DATA))
+		    !kernel_supports(obj, FEAT_GLOBAL_DATA)) {
+			map->skipped = true;
 			continue;
+		}
 
 		retried = false;
 retry:
@@ -5717,8 +5720,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
 			} else {
 				const struct bpf_map *map = &obj->maps[relo->map_idx];
 
-				if (bpf_map__is_internal(map) &&
-				    !kernel_supports(obj, FEAT_GLOBAL_DATA)) {
+				if (map->skipped) {
 					pr_warn("prog '%s': relo #%d: kernel doesn't support global data\n",
 						prog->name, i);
 					return -ENOTSUP;
@@ -7926,6 +7928,9 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
 		char *pin_path = NULL;
 		char buf[PATH_MAX];
 
+		if (map->skipped)
+			continue;
+
 		if (path) {
 			int len;
 
Signature · 1 line
2.19.1.6.gb485710b
#1 lore raw
Quoted 4 lines · Shuyi Cheng

Fix error: "failed to pin map: Bad file descriptor, path:
/sys/fs/bpf/_rodata_str1_1."

Quoted 8 lines

In the old kernel, the global data map will not be created, see [0]. So we should skip the pinning of the global data map to avoid bpf_object__pin_maps returning error. Therefore, when the map is not created, we mark “map->skipped" as true and then check during relocation and during pinning.

---

This doesn't apply cleanly, can you please rebase on top of the latest bpf-next and resubmit? The changes themselves look good.

Quoted 15 lines

v1:
https://lore.kernel.org/bpf/CAEf4BzbtQGnGZTLbTdy1GHK54f5S7YNFQak7BuEfaqGEwqNNJA@mail.gmail.com/T/#m80ec7f8bc69dbcf4a5945e2aa6f16145901afc40
v1->v2:
-- add "bool skipped" to struct bpf_map.
-- replace "bpf_map__is_internal(map) && !kernel_supports(obj, FEAT_GLOBAL_DATA))" with map->skipped

  tools/lib/bpf/libbpf.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +0 -0
index 18d95c6a89fe..a5bad6b43c15 100644

tools/lib/bpf/libbpf.c struct bpf_map { · line 435

Quoted 4 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -432,6 +432,7 @@ struct bpf_map {
        bool pinned;
        bool reused;
        __u64 map_extra;
+       bool skipped;

please move up after pinned and reused to improve padding usage, thanks

Quoted 40 lines
tools/lib/bpf/libbpf.c +4 -3
  };
 
  enum extern_type {
@@ -5087,8 +5088,10 @@ bpf_object__create_maps(struct bpf_object *obj)
                 * kernels.
                 */
                if (bpf_map__is_internal(map) &&
-                   !kernel_supports(obj, FEAT_GLOBAL_DATA))
+                   !kernel_supports(obj, FEAT_GLOBAL_DATA)) {
+                       map->skipped = true;
                        continue;
+               }
 
                retried = false;
  retry:
@@ -5717,8 +5720,7 @@ bpf_object__relocate_data(struct bpf_object *obj,
 struct bpf_program *prog)
                        } else {
                                const struct bpf_map *map = &obj->maps[relo->map_idx];
 
-                               if (bpf_map__is_internal(map) &&
-                                   !kernel_supports(obj, FEAT_GLOBAL_DATA)) {
+                               if (map->skipped) {
                                        pr_warn("prog '%s': relo #%d: kernel doesn't support global data\n",
                                                prog->name, i);
                                        return -ENOTSUP;
tools/lib/bpf/libbpf.c +3 -1
@@ -7926,6 +7928,9 @@ int bpf_object__pin_maps(struct bpf_object *obj,
 const char *path)
                char *pin_path = NULL;
                char buf[PATH_MAX];
 
+               if (map->skipped)
+                       continue;
+
                if (path) {
                        int len;
 
--

2.19.1.6.gb485710b