BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/4] libbpf: Fixes for light skeletons
@ 2026-12-05  1:05 Alexei Starovoitov
  2026-12-05  1:05 ` [PATCH bpf-next 1/4] libbpf, bpftool: Keep places of programs and maps in " Alexei Starovoitov
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-05  1:05 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor

From: Alexei Starovoitov <ast@kernel.org>

A light skeleton is the way to build BPF programs into the kernel and to
sign them. Fix what it gets wrong:

  object has                        light skeleton today
  SEC("?...") program               fds of the next programs are shifted
  SEC("?.struct_ops.link") map      the generation fails
  arena with variables              bpftool gets SIGBUS
  jump table                        the loader has an fd of another process
  signature                         rejected with lockdown=confidentiality

Jump tables and an arena with variables or without map_extra are refused.
The rest works.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Alexei Starovoitov (4):
  libbpf, bpftool: Keep places of programs and maps in light skeletons
  libbpf: Do not map an arena in gen_loader
  libbpf: Clear the stack of a loader without a helper
  selftests/bpf: Test places of programs and maps in a light skeleton

 tools/bpf/bpftool/gen.c                       |  7 ++-
 tools/lib/bpf/bpf_gen_internal.h              |  4 +-
 tools/lib/bpf/gen_loader.c                    | 36 ++++-------
 tools/lib/bpf/libbpf.c                        | 46 +++++++++++---
 tools/testing/selftests/bpf/Makefile.skel     |  3 +-
 .../bpf/prog_tests/struct_ops_lskel.c         | 32 ++++++++++
 .../selftests/bpf/progs/struct_ops_places.c   | 63 +++++++++++++++++++
 7 files changed, 152 insertions(+), 39 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_places.c


base-commit: 988a2b6378e65021d5d1e4a001f5005f5bfbaa54
-- 
2.55.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 1/4] libbpf, bpftool: Keep places of programs and maps in light skeletons
  2026-12-05  1:05 [PATCH bpf-next 0/4] libbpf: Fixes for light skeletons Alexei Starovoitov
@ 2026-12-05  1:05 ` Alexei Starovoitov
  2026-12-05  1:25   ` sashiko-bot
  2026-12-05  1:05 ` [PATCH bpf-next 2/4] libbpf: Do not map an arena in gen_loader Alexei Starovoitov
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-05  1:05 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor

From: Alexei Starovoitov <ast@kernel.org>

A light skeleton has a field for each program. The loader stores the fds
in the order of loads, so a program that is not loaded shifts all that
are behind it:

  SEC("?raw_tp/sys_enter") int a(void *ctx)  progs.a.prog_fd = fd of b
  SEC("syscall")           int b(void *ctx)  progs.b.prog_fd = 0

A map that is not created, SEC("?.struct_ops.link"), fails the generation
with "nr_progs nr_maps mismatch". SEC("?raw_tp") crashes bpftool, retsnoop
and wprof have it.

Index programs and maps by their places in the object. The fd of what is
not loaded stays 0 and the skeleton doesn't attach it. An insn that refers
to such a map is poisoned, as it is without the loader.

The count of maps was also what refused reused maps. Check it directly.
It fails earlier now, so reset the fake fd of BTF. Otherwise close of
the object closes fd 0.

A struct_ops map turns on its SEC("?struct_ops/...") programs. Count them
when fd_array is sized for maps with pointers to functions.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 tools/bpf/bpftool/gen.c          |  7 +++++--
 tools/lib/bpf/bpf_gen_internal.h |  4 ++--
 tools/lib/bpf/gen_loader.c       | 29 +++++++++--------------------
 tools/lib/bpf/libbpf.c           | 28 ++++++++++++++++++++--------
 4 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index da5f7c9d99ab..97b2a06f6ca0 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -573,6 +573,7 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
 	char ident[256];
 
 	bpf_object__for_each_program(prog, obj) {
+		enum bpf_prog_type type = BPF_PROG_TYPE_UNSPEC;
 		const char *tp_name;
 
 		codegen("\
@@ -584,7 +585,9 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
 				int prog_fd = skel->progs.%2$s.prog_fd;		    \n\
 			", obj_name, bpf_program__name(prog));
 
-		switch (bpf_program__type(prog)) {
+		if (bpf_program__autoload(prog))
+			type = bpf_program__type(prog);
+		switch (type) {
 		case BPF_PROG_TYPE_RAW_TRACEPOINT:
 			tp_name = strchr(bpf_program__section_name(prog), '/') + 1;
 			printf("\tint fd = skel_raw_tracepoint_open(\"%s\", prog_fd);\n", tp_name);
@@ -656,7 +659,7 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
 			continue;
 		if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
 			continue;
-		if (!(bpf_map__map_flags(map) & BPF_F_LINK))
+		if (!(bpf_map__map_flags(map) & BPF_F_LINK) || !bpf_map__autocreate(map))
 			continue;
 
 		codegen("\
diff --git a/tools/lib/bpf/bpf_gen_internal.h b/tools/lib/bpf/bpf_gen_internal.h
index 50e260b17651..e8b5d74a0759 100644
--- a/tools/lib/bpf/bpf_gen_internal.h
+++ b/tools/lib/bpf/bpf_gen_internal.h
@@ -38,7 +38,7 @@ struct bpf_gen {
 	bool swapped_endian;
 	ssize_t cleanup_label;
 	__u32 nr_progs;
-	__u32 nr_maps;
+	__u32 prog_idx; /* of the next load */
 	int log_level;
 	int error;
 	struct ksym_relo_desc *relos;
@@ -64,7 +64,7 @@ struct bpf_gen {
 
 void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps,
 		   int max_func_ptr_maps, int nr_kfuncs);
-int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps);
+int bpf_gen__finish(struct bpf_gen *gen);
 void bpf_gen__free(struct bpf_gen *gen);
 void bpf_gen__load_btf(struct bpf_gen *gen, const void *raw_data, __u32 raw_size);
 void bpf_gen__map_create(struct bpf_gen *gen,
diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
index a031e97c7b4c..9d0009ac998f 100644
--- a/tools/lib/bpf/gen_loader.c
+++ b/tools/lib/bpf/gen_loader.c
@@ -127,6 +127,7 @@ void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps
 				 (nr_maps + max_func_ptr_maps + 1 + nr_kfuncs) * sizeof(int));
 	gen->prog_fds = add_data(gen, NULL, nr_progs * sizeof(int));
 	gen->log_level = log_level;
+	gen->nr_progs = nr_progs;
 	gen->nr_obj_maps = nr_maps;
 	gen->max_func_ptr_maps = max_func_ptr_maps;
 	/* their fds are closed like the fds of the maps of the object when loading fails */
@@ -371,16 +372,10 @@ static void emit_sys_close_blob(struct bpf_gen *gen, int blob_off)
 	__emit_sys_close(gen);
 }
 
-int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps)
+int bpf_gen__finish(struct bpf_gen *gen)
 {
 	int i;
 
-	if (nr_progs < gen->nr_progs || nr_maps != gen->nr_maps) {
-		pr_warn("nr_progs %d/%u nr_maps %d/%u mismatch\n",
-			nr_progs, gen->nr_progs, nr_maps, gen->nr_maps);
-		gen->error = -EFAULT;
-		return gen->error;
-	}
 	emit_sys_close_stack(gen, stack_off(btf_fd));
 	/* programs hold their maps with pointers to functions, nothing else needs them */
 	for (i = 0; i < gen->nr_func_ptr_maps; i++)
@@ -388,11 +383,11 @@ int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps)
 	for (i = 0; i < gen->nr_progs; i++)
 		move_blob2ctx(gen,
 			      sizeof(struct bpf_loader_ctx) +
-			      sizeof(struct bpf_map_desc) * gen->nr_maps +
+			      sizeof(struct bpf_map_desc) * gen->nr_obj_maps +
 			      sizeof(struct bpf_prog_desc) * i +
 			      offsetof(struct bpf_prog_desc, prog_fd), 4,
 			      blob_prog_fd_off(gen, i));
-	for (i = 0; i < gen->nr_maps; i++)
+	for (i = 0; i < gen->nr_obj_maps; i++)
 		move_blob2ctx(gen,
 			      sizeof(struct bpf_loader_ctx) +
 			      sizeof(struct bpf_map_desc) * i +
@@ -492,7 +487,7 @@ void bpf_gen__map_create(struct bpf_gen *gen,
 {
 	int attr_size = offsetofend(union bpf_attr, map_extra);
 	bool close_inner_map_fd = false;
-	int map_create_attr, idx;
+	int map_create_attr;
 	union bpf_attr attr;
 
 	memset(&attr, 0, attr_size);
@@ -559,13 +554,9 @@ void bpf_gen__map_create(struct bpf_gen *gen,
 		 */
 		emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_7,
 				      stack_off(inner_map_fd)));
-	} else if (map_idx != gen->nr_maps) {
-		gen->error = -EDOM; /* internal bug */
-		return;
 	} else {
-		idx = gen->nr_maps++;
 		emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
-						 0, 0, 0, blob_fd_array_off(gen, idx)));
+						 0, 0, 0, blob_fd_array_off(gen, map_idx)));
 		emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_7, 0));
 	}
 	if (close_inner_map_fd)
@@ -1058,7 +1049,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
 		attr_size = offsetofend(union bpf_attr, entry_rec_size);
 	} else if (gen->has_entries) {
 		emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_0, BPF_PSEUDO_MAP_IDX_VALUE,
-						 0, 0, 0, blob_prog_fd_off(gen, gen->nr_progs)));
+						 0, 0, 0, blob_prog_fd_off(gen, gen->prog_idx)));
 		emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0));
 		out = emit_goto_unless(gen, BPF_JEQ, BPF_REG_0);
 	}
@@ -1069,7 +1060,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
 	/* add insns to blob of bytes */
 	insns_off = add_data(gen, insns, insn_cnt * sizeof(struct bpf_insn));
 	pr_debug("gen: prog_load: prog_idx %u type %u insn off %d insns_cnt %zu license off %d\n",
-		 gen->nr_progs, prog_type, insns_off, insn_cnt, license_off);
+		 gen->prog_idx, prog_type, insns_off, insn_cnt, license_off);
 
 	/* convert blob insns to target endianness */
 	if (gen->swapped_endian && !gen->error) {
@@ -1187,7 +1178,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
 		emit_check_err(gen);
 	/* remember prog_fd in the blob, if successful */
 	emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
-					 0, 0, 0, blob_prog_fd_off(gen, gen->nr_progs)));
+					 0, 0, 0, blob_prog_fd_off(gen, gen->prog_idx)));
 	emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_7, 0));
 	for (i = 0; i < entry_cnt; i++)
 		move_blob2blob(gen, blob_prog_fd_off(gen, load_attr->entries[i].prog_fd), 4,
@@ -1196,8 +1187,6 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
 		emit_label(gen, out);
 	if (entry_cnt)
 		gen->has_entries = true;
-	else
-		gen->nr_progs++;
 }
 
 /*
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 8ed4786e0e51..9ff6a580078d 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -5919,6 +5919,12 @@ bpf_object__create_maps(struct bpf_object *obj)
 			}
 		}
 
+		if (map->reused && obj->gen_loader) {
+			pr_warn("map '%s': gen_loader does not support reused maps\n", map->name);
+			err = -EOPNOTSUPP;
+			goto err_out;
+		}
+
 		if (map->reused) {
 			pr_debug("map '%s': skipping creation (preset fd=%d)\n",
 				 map->name, map->fd);
@@ -6881,7 +6887,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
 		switch (relo->type) {
 		case RELO_LD64:
 			map = &obj->maps[relo->map_idx];
-			if (obj->gen_loader) {
+			if (obj->gen_loader && map->autocreate) {
 				insn[0].src_reg = BPF_PSEUDO_MAP_IDX;
 				insn[0].imm = relo->map_idx;
 			} else if (map->autocreate) {
@@ -6912,7 +6918,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
 				insn[0].src_reg = obj->gen_loader ? BPF_PSEUDO_MAP_IDX_VALUE :
 								    BPF_PSEUDO_MAP_VALUE;
 				insn[0].imm = map_fd;
-			} else if (obj->gen_loader) {
+			} else if (obj->gen_loader && map->autocreate) {
 				insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
 				insn[0].imm = relo->map_idx;
 			} else if (map->autocreate) {
@@ -9140,16 +9146,14 @@ static int bpf_program_record_relos(struct bpf_program *prog)
 	return 0;
 }
 
-/* The loader keeps the fds of the programs in the order of bpf_object__load_progs() */
+/* The place of the program in a skeleton */
 static int bpf_program_gen_idx(const struct bpf_program *prog)
 {
 	const struct bpf_program *p;
 	int idx = 0;
 
 	for (p = prog->obj->programs; p != prog; p++)
-		if (!prog_is_subprog(prog->obj, p) &&
-		    p->load_strategy == BPF_PROG_LOAD_STRATEGY_AUTO)
-			idx++;
+		idx += !prog_is_subprog(prog->obj, p);
 	return idx;
 }
 
@@ -9430,6 +9434,9 @@ bpf_object__load_progs(struct bpf_object *obj, int log_level)
 		}
 		prog->log_level |= log_level;
 
+		if (obj->gen_loader)
+			obj->gen_loader->prog_idx = bpf_program_gen_idx(prog);
+
 		bpf_object_load_shared(obj, prog);
 		if (prog->fd >= 0)
 			continue;
@@ -10275,6 +10282,9 @@ static int bpf_object_prepare(struct bpf_object *obj, const char *target_btf_pat
 	err = err ? : bpf_object_prepare_progs(obj);
 
 	if (err) {
+		/* it's a fake fd 0 */
+		if (obj->gen_loader && obj->btf)
+			btf__set_fd(obj->btf, -1);
 		bpf_object_unpin(obj);
 		bpf_object_unload(obj);
 		obj->state = OBJ_LOADED;
@@ -10308,6 +10318,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
 		 * Every program that has callx may get a copy of every map with
 		 * pointers to functions. Which functions a program calls is not
 		 * known yet. Any of them may call the ones that have callx.
+		 * Nor is it known which programs struct_ops maps turn on.
 		 */
 		for (i = 0; i < obj->nr_maps; i++)
 			if (obj->maps[i].autocreate && obj->maps[i].func_ptr_cnt)
@@ -10317,7 +10328,8 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
 			    prog_has_callx(&obj->programs[i]))
 				text_has_callx = true;
 		for (i = 0; i < obj->nr_programs; i++)
-			if (obj->programs[i].load_strategy != BPF_PROG_LOAD_STRATEGY_DISABLED &&
+			if ((obj->programs[i].load_strategy != BPF_PROG_LOAD_STRATEGY_DISABLED ||
+			     obj->programs[i].type == BPF_PROG_TYPE_STRUCT_OPS) &&
 			    !prog_is_subprog(obj, &obj->programs[i]) &&
 			    (text_has_callx || prog_has_callx(&obj->programs[i])))
 				nr_progs++;
@@ -10344,7 +10356,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
 		if (obj->btf)
 			btf__set_fd(obj->btf, -1);
 		if (!err)
-			err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps);
+			err = bpf_gen__finish(obj->gen_loader);
 	}
 
 	bpf_object_post_load_cleanup(obj, false /* !force */);
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 2/4] libbpf: Do not map an arena in gen_loader
  2026-12-05  1:05 [PATCH bpf-next 0/4] libbpf: Fixes for light skeletons Alexei Starovoitov
  2026-12-05  1:05 ` [PATCH bpf-next 1/4] libbpf, bpftool: Keep places of programs and maps in " Alexei Starovoitov
@ 2026-12-05  1:05 ` Alexei Starovoitov
  2026-12-05  1:05 ` [PATCH bpf-next 3/4] libbpf: Clear the stack of a loader without a helper Alexei Starovoitov
  2026-12-05  1:05 ` [PATCH bpf-next 4/4] selftests/bpf: Test places of programs and maps in a light skeleton Alexei Starovoitov
  3 siblings, 0 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-05  1:05 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor

From: Alexei Starovoitov <ast@kernel.org>

When the loader is generated there is no arena yet. libbpf maps
the placeholder fd at map_extra in the process that generates it.
"bpftool gen skeleton -L" dies with SIGBUS when the arena has variables.
Skip the mapping.

Refuse what the loader cannot do. Nobody maps the arena while the loader
runs, so its address has to be in map_extra and it cannot have variables.
The map of a jump table is created by the process that generates
the loader. Its fd is valid only there and a signature doesn't cover its
content.

As before, the loader binds no arena: an insn has to refer to it.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 tools/lib/bpf/libbpf.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 9ff6a580078d..2b188e64e356 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -5940,7 +5940,7 @@ bpf_object__create_maps(struct bpf_object *obj)
 				err = bpf_object__populate_internal_map(obj, map);
 				if (err < 0)
 					goto err_out;
-			} else if (map->def.type == BPF_MAP_TYPE_ARENA) {
+			} else if (map->def.type == BPF_MAP_TYPE_ARENA && !obj->gen_loader) {
 				map->mmaped = mmap((void *)(long)map->map_extra,
 						   bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
 						   map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
@@ -6625,6 +6625,11 @@ static int create_jt_map(struct bpf_object *obj, struct bpf_program *prog, struc
 	__u64 *jt;
 	__u32 i;
 
+	if (obj->gen_loader) {
+		pr_warn("prog '%s': gen_loader does not support jump tables\n", prog->name);
+		return -EOPNOTSUPP;
+	}
+
 	map_fd = find_jt_map(obj, prog, sym_off);
 	if (map_fd >= 0)
 		return map_fd;
@@ -10241,10 +10246,21 @@ static void bpf_object_post_load_cleanup(struct bpf_object *obj, bool force)
 static int bpf_object_check_gen_loader(struct bpf_object *obj)
 {
 	struct bpf_program *prog;
+	struct bpf_map *map;
 
 	if (!obj->gen_loader)
 		return 0;
 
+	/* nobody maps an arena before the programs are loaded */
+	bpf_object__for_each_map(map, obj) {
+		if (map->def.type != BPF_MAP_TYPE_ARENA || !map->autocreate ||
+		    (map->map_extra && !obj->arena_data))
+			continue;
+		pr_warn("map '%s': gen_loader needs an arena with map_extra and no variables\n",
+			map->name);
+		return -EOPNOTSUPP;
+	}
+
 	/*
 	 * gen_loader only records programs loaded by bpf_object__load_progs(),
 	 * which skips MANUAL ones.
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 3/4] libbpf: Clear the stack of a loader without a helper
  2026-12-05  1:05 [PATCH bpf-next 0/4] libbpf: Fixes for light skeletons Alexei Starovoitov
  2026-12-05  1:05 ` [PATCH bpf-next 1/4] libbpf, bpftool: Keep places of programs and maps in " Alexei Starovoitov
  2026-12-05  1:05 ` [PATCH bpf-next 2/4] libbpf: Do not map an arena in gen_loader Alexei Starovoitov
@ 2026-12-05  1:05 ` Alexei Starovoitov
  2026-12-05  1:05 ` [PATCH bpf-next 4/4] selftests/bpf: Test places of programs and maps in a light skeleton Alexei Starovoitov
  3 siblings, 0 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-05  1:05 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor

From: Alexei Starovoitov <ast@kernel.org>

bpf_probe_read_kernel() is not available with lockdown=confidentiality,
so the verifier rejects the loader. A signed loader calls it only to clear
8 bytes of its stack. Use two stores instead.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 tools/lib/bpf/gen_loader.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
index 9d0009ac998f..7b36a84ee3e2 100644
--- a/tools/lib/bpf/gen_loader.c
+++ b/tools/lib/bpf/gen_loader.c
@@ -136,11 +136,8 @@ void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps
 	emit(gen, BPF_MOV64_REG(BPF_REG_6, BPF_REG_1));
 
 	/* bzero stack */
-	emit(gen, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
-	emit(gen, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -stack_sz));
-	emit(gen, BPF_MOV64_IMM(BPF_REG_2, stack_sz));
-	emit(gen, BPF_MOV64_IMM(BPF_REG_3, 0));
-	emit(gen, BPF_EMIT_CALL(BPF_FUNC_probe_read_kernel));
+	for (i = 0; i < stack_sz; i += 4)
+		emit(gen, BPF_ST_MEM(BPF_W, BPF_REG_10, -stack_sz + i, 0));
 
 	/*
 	 * jump over cleanup code below:
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 4/4] selftests/bpf: Test places of programs and maps in a light skeleton
  2026-12-05  1:05 [PATCH bpf-next 0/4] libbpf: Fixes for light skeletons Alexei Starovoitov
                   ` (2 preceding siblings ...)
  2026-12-05  1:05 ` [PATCH bpf-next 3/4] libbpf: Clear the stack of a loader without a helper Alexei Starovoitov
@ 2026-12-05  1:05 ` Alexei Starovoitov
  3 siblings, 0 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-05  1:05 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, eddyz87, memxor

From: Alexei Starovoitov <ast@kernel.org>

Load a signed light skeleton with an arena, struct_ops, and programs and
maps that are not loaded.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 tools/testing/selftests/bpf/Makefile.skel     |  3 +-
 .../bpf/prog_tests/struct_ops_lskel.c         | 32 ++++++++++
 .../selftests/bpf/progs/struct_ops_places.c   | 63 +++++++++++++++++++
 3 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_places.c

diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel
index 2b8ceecacb29..f1708da95e74 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -38,7 +38,8 @@ LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c 	\
 	struct_ops_vmlinux.c struct_ops_mod.c				\
 	test_ksyms_strong_typeless.c
 
-LSKELS_SIGNED := fentry_test.c fexit_test.c share_subprogs_signed.c atomics.c
+LSKELS_SIGNED := fentry_test.c fexit_test.c share_subprogs_signed.c atomics.c \
+	struct_ops_places.c
 
 # Generate both light skeleton and libbpf skeleton for these
 LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \
diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c
index a1fe16af6934..f3eda59c1d1d 100644
--- a/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c
+++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c
@@ -1,11 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/btf.h>
+#include <linux/keyctl.h>
 #include <test_progs.h>
 #include "network_helpers.h"
 #include "test_btf.h"
 #include "struct_ops_vmlinux.lskel.h"
 #include "struct_ops_mod.lskel.h"
+#include "struct_ops_places.lskel.h"
 
 static int set_tcp_ca_cb(int fd, void *opts)
 {
@@ -115,6 +117,34 @@ static void test_module(void)
 	struct_ops_mod_lskel__destroy(skel);
 }
 
+static void test_places(void)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	struct struct_ops_places_lskel *skel;
+
+	if (!env.has_testmod) {
+		test__skip();
+		return;
+	}
+
+	skel = struct_ops_places_lskel__open();
+	if (!ASSERT_OK_PTR(skel, "open"))
+		return;
+	skel->keyring_id = KEY_SPEC_SESSION_KEYRING;
+	if (!ASSERT_OK(struct_ops_places_lskel__load(skel), "load"))
+		goto cleanup;
+
+	ASSERT_EQ(skel->maps.not_created.map_fd, 0, "not_created");
+	ASSERT_EQ(skel->progs.not_attached.prog_fd, 0, "not_attached");
+	ASSERT_EQ(skel->progs.not_loaded.prog_fd, 0, "not_loaded");
+	ASSERT_OK(struct_ops_places_lskel__attach(skel), "attach");
+	ASSERT_EQ(skel->bss->mul_result, 4 * 6, "mul_result");
+	ASSERT_OK(bpf_prog_test_run_opts(skel->progs.alloc_arena_page.prog_fd, &opts), "run");
+	ASSERT_EQ(opts.retval, 7, "alloc_arena_page");
+cleanup:
+	struct_ops_places_lskel__destroy(skel);
+}
+
 static void check_crafted_btf(const char *field_name, __u32 int_off,
 			      __u32 int_bits, __u32 int_sz, __u32 member_bit_off)
 {
@@ -195,6 +225,8 @@ void test_struct_ops_lskel(void)
 		test_vmlinux();
 	if (test__start_subtest("module"))
 		test_module();
+	if (test__start_subtest("places"))
+		test_places();
 	if (test__start_subtest("invalid_local_btf"))
 		test_invalid_local_btf();
 }
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_places.c b/tools/testing/selftests/bpf/progs/struct_ops_places.c
new file mode 100644
index 000000000000..7f0c9ab2ba88
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/struct_ops_places.c
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_experimental.h"
+#include <bpf_arena_common.h>
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARENA);
+	__uint(map_flags, BPF_F_MMAPABLE);
+	__uint(max_entries, 16);
+#ifdef __TARGET_ARCH_arm64
+	__ulong(map_extra, 0x1ull << 32);
+#else
+	__ulong(map_extra, 0x1ull << 44);
+#endif
+} arena SEC(".maps");
+
+int mul_result;
+
+SEC("?raw_tp/sys_enter")
+int not_attached(void *ctx)
+{
+	return 0;
+}
+
+SEC("struct_ops/none")
+int BPF_PROG(not_loaded)
+{
+	return 0;
+}
+
+/* The map turns it on */
+SEC("?struct_ops/test_2")
+void BPF_PROG(mul_args, int a, int b)
+{
+	mul_result = a * b;
+}
+
+SEC("syscall")
+int alloc_arena_page(void *ctx)
+{
+	return bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0) ? 7 : 0;
+}
+
+struct bpf_testmod_ops___places {
+	int data;
+	void (*test_2)(int a, int b);
+	int (*none)(void);
+};
+
+SEC("?.struct_ops.link")
+struct bpf_testmod_ops___places not_created = {
+	.none = (void *)not_loaded,
+};
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops___places mul_ops = {
+	.data = 6,
+	.test_2 = (void *)mul_args,
+};
+
+char _license[] SEC("license") = "GPL";
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next 1/4] libbpf, bpftool: Keep places of programs and maps in light skeletons
  2026-12-05  1:05 ` [PATCH bpf-next 1/4] libbpf, bpftool: Keep places of programs and maps in " Alexei Starovoitov
@ 2026-12-05  1:25   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-05  1:25 UTC (permalink / raw)
  To: Alexei Starovoitov, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] bpftool: codegen_attach_detach() emits attach helper for
  uncreated struct_ops
--

commit 5cc89494fb6061133b6673a4a512cf419242013b
Author: Alexei Starovoitov <ast@kernel.org>

libbpf, bpftool: Keep places of programs and maps in light skeletons

This commit indexes programs and maps in light skeletons by their
positions in the BPF object rather than load order, so non-autoloaded
programs and non-autocreated maps keep their slots with fd 0. It also
updates bpftool skeleton attach generation to skip non-autoloaded
programs and non-autocreated struct_ops maps.

> diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
> index da5f7c9d99abe..97b2a06f6ca0c 100644
> --- a/tools/bpf/bpftool/gen.c
> +++ b/tools/bpf/bpftool/gen.c

[ ... ]

> @@ -656,7 +659,7 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
>  			continue;
>  		if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
>  			continue;
> -		if (!(bpf_map__map_flags(map) & BPF_F_LINK))
> +		if (!(bpf_map__map_flags(map) & BPF_F_LINK) || !bpf_map__autocreate(map))
>  			continue;
>  
>  		codegen("\

[Severity: Low]
Should the earlier per-map helper loop in codegen_attach_detach() also
check !bpf_map__autocreate(map)?

The aggregate <obj>__attach() loop skips non-autocreated struct_ops maps,
but the earlier bpf_object__for_each_map() loop still emits
<obj>__<map>__attach() calling skel_link_create() unconditionally with
map_fd equal to 0.

Calling that per-map helper directly issues BPF_LINK_CREATE on fd 0,
failing with -EINVAL or -EBADF (or silently attaching an unrelated
struct_ops map if fd 0 was closed and reused).  Skipping the helper or
emitting a no-op returning 0 when !bpf_map__autocreate(map) avoids this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205010519.2565529-1-alexei.starovoitov@gmail.com?part=1

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-05  1:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  1:05 [PATCH bpf-next 0/4] libbpf: Fixes for light skeletons Alexei Starovoitov
2026-12-05  1:05 ` [PATCH bpf-next 1/4] libbpf, bpftool: Keep places of programs and maps in " Alexei Starovoitov
2026-12-05  1:25   ` sashiko-bot
2026-12-05  1:05 ` [PATCH bpf-next 2/4] libbpf: Do not map an arena in gen_loader Alexei Starovoitov
2026-12-05  1:05 ` [PATCH bpf-next 3/4] libbpf: Clear the stack of a loader without a helper Alexei Starovoitov
2026-12-05  1:05 ` [PATCH bpf-next 4/4] selftests/bpf: Test places of programs and maps in a light skeleton Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox