selftests/bpf: add BPF object fixup step to veristat
authorAndrii Nakryiko <andrii@kernel.org>
Wed, 5 Oct 2022 16:14:50 +0000 (09:14 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 5 Oct 2022 23:55:12 +0000 (16:55 -0700)
Add a step to attempt to "fix up" BPF object file to make it possible to
successfully load it. E.g., set non-zero size for BPF maps that expect
max_entries set, but BPF object file itself doesn't have declarative
max_entries values specified.

Another issue was with automatic map pinning. Pinning has no effect on
BPF verification process itself but can interfere when validating
multiple related programs and object files, so veristat disabled all the
pinning explicitly.

In the future more such fix up heuristics could be added to accommodate
common patterns encountered in practice.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20221005161450.1064469-3-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/veristat.c

index 38f6781..973cbf6 100644 (file)
@@ -509,6 +509,28 @@ static int parse_verif_log(char * const buf, size_t buf_sz, struct verif_stats *
        return 0;
 }
 
+static void fixup_obj(struct bpf_object *obj)
+{
+       struct bpf_map *map;
+
+       bpf_object__for_each_map(map, obj) {
+               /* disable pinning */
+               bpf_map__set_pin_path(map, NULL);
+
+               /* fix up map size, if necessary */
+               switch (bpf_map__type(map)) {
+               case BPF_MAP_TYPE_SK_STORAGE:
+               case BPF_MAP_TYPE_TASK_STORAGE:
+               case BPF_MAP_TYPE_INODE_STORAGE:
+               case BPF_MAP_TYPE_CGROUP_STORAGE:
+                       break;
+               default:
+                       if (bpf_map__max_entries(map) == 0)
+                               bpf_map__set_max_entries(map, 1);
+               }
+       }
+}
+
 static int process_prog(const char *filename, struct bpf_object *obj, struct bpf_program *prog)
 {
        const char *prog_name = bpf_program__name(prog);
@@ -543,6 +565,9 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
        }
        verif_log_buf[0] = '\0';
 
+       /* increase chances of successful BPF object loading */
+       fixup_obj(obj);
+
        err = bpf_object__load(obj);
        env.progs_processed++;