libbpf: Sync progs autoload with maps autocreate for struct_ops maps
authorEduard Zingerman <eddyz87@gmail.com>
Wed, 6 Mar 2024 10:45:22 +0000 (12:45 +0200)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 6 Mar 2024 23:18:15 +0000 (15:18 -0800)
commitfe9d049c3da06373a1a35914b7f695509e4cb1fe
tree6c12581c804da865f4d0f7bf2b224e1d0543f90a
parent1863acccdf936c6917398165ca97e252d02fa6dd
libbpf: Sync progs autoload with maps autocreate for struct_ops maps

Automatically select which struct_ops programs to load depending on
which struct_ops maps are selected for automatic creation.
E.g. for the BPF code below:

    SEC("struct_ops/test_1") int BPF_PROG(foo) { ... }
    SEC("struct_ops/test_2") int BPF_PROG(bar) { ... }

    SEC(".struct_ops.link")
    struct test_ops___v1 A = {
        .foo = (void *)foo
    };

    SEC(".struct_ops.link")
    struct test_ops___v2 B = {
        .foo = (void *)foo,
        .bar = (void *)bar,
    };

And the following libbpf API calls:

    bpf_map__set_autocreate(skel->maps.A, true);
    bpf_map__set_autocreate(skel->maps.B, false);

The autoload would be enabled for program 'foo' and disabled for
program 'bar'.

During load, for each struct_ops program P, referenced from some
struct_ops map M:
- set P.autoload = true if M.autocreate is true for some M;
- set P.autoload = false if M.autocreate is false for all M;
- don't change P.autoload, if P is not referenced from any map.

Do this after bpf_object__init_kern_struct_ops_maps()
to make sure that shadow vars assignment is done.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20240306104529.6453-9-eddyz87@gmail.com
tools/lib/bpf/libbpf.c