libbpf: handle yet another corner case of nulling out struct_ops program
authorAndrii Nakryiko <andrii@kernel.org>
Tue, 7 May 2024 00:13:30 +0000 (17:13 -0700)
committerMartin KaFai Lau <martin.lau@kernel.org>
Tue, 7 May 2024 23:21:59 +0000 (16:21 -0700)
There is yet another corner case where user can set STRUCT_OPS program
reference in STRUCT_OPS map to NULL, but libbpf will fail to disable
autoload for such BPF program. This time it's the case of "new" kernel
which has type information about callback field, but user explicitly
nulled-out program reference from user-space after opening BPF object.

Fix, hopefully, the last remaining unhandled case.

Fixes: 0737df6de946 ("libbpf: better fix for handling nulled-out struct_ops program")
Fixes: f973fccd43d3 ("libbpf: handle nulled-out program in struct_ops correctly")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240507001335.1445325-3-andrii@kernel.org
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
tools/lib/bpf/libbpf.c

index 7d77a1b158ceb97906d852e308e6da99e7a60f52..04de4fb8178598d05a1a29bc22eb26c238c726d5 100644 (file)
@@ -1193,11 +1193,19 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
                }
 
                if (btf_is_ptr(mtype)) {
-                       /* Update the value from the shadow type */
                        prog = *(void **)mdata;
+                       /* just like for !kern_member case above, reset declaratively
+                        * set (at compile time) program's autload to false,
+                        * if user replaced it with another program or NULL
+                        */
+                       if (st_ops->progs[i] && st_ops->progs[i] != prog)
+                               st_ops->progs[i]->autoload = false;
+
+                       /* Update the value from the shadow type */
                        st_ops->progs[i] = prog;
                        if (!prog)
                                continue;
+
                        if (!is_valid_st_ops_program(obj, prog)) {
                                pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n",
                                        map->name, mname);