Skip to contentSkip to conversation

libbpf: Perform map fd cleanup for gen_loader in case of error

PATCH In review Kumar Kartikeya Dwivedi 2 participants 3 messages Nov 12, 2021 – Nov 13, 2021

Subsystems (2)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

PATCH v2 1/1 libbpf: Perform map fd cleanup for gen_loader in case of error

Alexei reported a fd leak issue in gen loader (when invoked from bpftool) [0]. When adding ksym support, map fd allocation was moved from stack to loader map, however I missed closing these fds (relevant when cleanup label is jumped to on error). For the success case, the allocated fd is returned in loader ctx, hence this problem is not noticed.

Make three changes, first MAX_USED_MAPS in MAX_FD_ARRAY_SZ instead of MAX_USED_PROGS, the braino was not a problem until now for this case as we didn't try to close map fds (otherwise use of it would have tried closing 32 additional fds in ksym btf fd range). Then, do a cleanup for all nr_maps fds in cleanup label code, so that in case of error all temporary map fds from bpf_gen__map_create are closed.

Then, adjust the cleanup label to only generate code for the required number of program and map fds. To trim code for remaining program fds, lay out prog_fd array in stack in the end, so that we can directly skip the remaining instances. Still stack size remains same, since changing that would require changes in a lot of places (including adjustment of stack_off macro), so nr_progs_sz variable is only used to track required number of iterations (and jump over cleanup size calculated from that), stack offset calculation remains unaffected.

The difference for test_ksyms_module.o is as follows:
libbpf: //prog cleanup iterations: before = 34, after = 5
libbpf: //maps cleanup iterations: before = 64, after = 2

Also, move allocation of gen->fd_array offset to bpf_gen__init. Since offset can now be 0, and we already continue even if add_data returns 0 in case of failure, we do not need to distinguish between 0 offset and failure case 0, as we rely on bpf_gen__finish to check errors. We can also skip check for gen->fd_array in add_*_fd functions, since bpf_gen__init will take care of it.

[0]: https://lore.kernel.org/bpf/CAADnVQJ6jSitKSNKyxOrUzwY2qDRX0sPkJ=VLGHuCLVJ=qOt9g@mail.gmail.com

Fixes 18f4fccbf314 ("libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations")

---
v1 -> v2
* Only generate cleanup code for nr_progs/nr_maps (Alexei)
* Reorder gen->fd_array init to start of bpf_gen__init (Alexei)
* Don't reorder add_data

---
 tools/lib/bpf/bpf_gen_internal.h |  4 +--
 tools/lib/bpf/gen_loader.c       | 47 ++++++++++++++++++++------------
 tools/lib/bpf/libbpf.c           |  4 +--
 3 files changed, 34 insertions(+), 21 deletions(-)
tools/lib/bpf/bpf_gen_internal.h +2 -2
index 75ca9fb857b2..cc486a77db65 100644
@@ -47,8 +47,8 @@ struct bpf_gen {
 	int nr_fd_array;
 };
 
-void bpf_gen__init(struct bpf_gen *gen, int log_level);
-int bpf_gen__finish(struct bpf_gen *gen);
+void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps);
+int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps);
 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, struct bpf_create_map_params *map_attr, int map_idx);
tools/lib/bpf/gen_loader.c +30 -17

118 lines not on this page · raw message

tools/lib/bpf/libbpf.c +2 -2
index de7e09a6b5ec..f6faa33c80fa 100644
@@ -7263,7 +7263,7 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
 	}
 
 	if (obj->gen_loader)
-		bpf_gen__init(obj->gen_loader, attr->log_level);
+		bpf_gen__init(obj->gen_loader, attr->log_level, obj->nr_programs, obj->nr_maps);
 
 	err = bpf_object__probe_loading(obj);
 	err = err ? : bpf_object__load_vmlinux_btf(obj, false);
@@ -7282,7 +7282,7 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
 		for (i = 0; i < obj->nr_maps; i++)
 			obj->maps[i].fd = -1;
 		if (!err)
-			err = bpf_gen__finish(obj->gen_loader);
+			err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps);
 	}
 
 	/* clean up fd_array */

--
2.33.1

#1 lore raw

tools/lib/bpf/gen_loader.c void bpf_gen__init(struct bpf_gen *gen, int log_level) · line 131

Quoted 3 lines · Kumar Kartikeya Dwivedi
tools/lib/bpf/gen_loader.c +2 -0
+	/* amount of stack actually used, only used to calculate iterations, not stack offset */
+	nr_progs_sz = offsetof(struct loader_stack, prog_fd[nr_progs + 1]);

I think '+ 1' would be one too many.
When nr_progs == 1 the offsetof(struct loader_stack, prog_fd[1]) would cover btf_fd, inner_map_fd, and prog_fd[0].

tools/lib/bpf/gen_loader.c void bpf_gen__init(struct bpf_gen *gen, int log_level) · line 139

Quoted 10 lines
tools/lib/bpf/gen_loader.c +6 -2
 	/* jump over cleanup code */
 	emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0,
-			      /* size of cleanup code below */
-			      (stack_sz / 4) * 3 + 2));
+			      /* size of cleanup code below (including map fd cleanup) */
+			      (nr_progs_sz / 4) * 3 + 2 +
+			      /* 6 insns for emit_sys_close_blob,
+			       * 6 insns for debug_regs in emit_sys_close_blob
+			       */
+			      (nr_maps * (6 + (gen->log_level ? 6 : 0)))));

I've removed the extra () in the above.

And pushed to bpf tree.
Please confirm that +1 removal was correct.

Thanks for the quick debugging and fix. Much appreciate it.

author maintainer
#2 lore raw
Quoted 9 lines · Alexei Starovoitov

tools/lib/bpf/gen_loader.c void bpf_gen__init(struct bpf_gen *gen, int log_level) · line 131

Quoted 3 lines · Kumar Kartikeya Dwivedi
tools/lib/bpf/gen_loader.c +2 -0
+	/* amount of stack actually used, only used to calculate iterations, not stack offset */
+	nr_progs_sz = offsetof(struct loader_stack, prog_fd[nr_progs + 1]);

I think '+ 1' would be one too many.
When nr_progs == 1 the offsetof(struct loader_stack, prog_fd[1]) would cover btf_fd, inner_map_fd, and prog_fd[0].

Ooh, right, my bad, thanks for fixing it :).

Quoted 13 lines

tools/lib/bpf/gen_loader.c void bpf_gen__init(struct bpf_gen *gen, int log_level) · line 139

Quoted 10 lines
tools/lib/bpf/gen_loader.c +6 -2
 	/* jump over cleanup code */
 	emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0,
-			      /* size of cleanup code below */
-			      (stack_sz / 4) * 3 + 2));
+			      /* size of cleanup code below (including map fd cleanup) */
+			      (nr_progs_sz / 4) * 3 + 2 +
+			      /* 6 insns for emit_sys_close_blob,
+			       * 6 insns for debug_regs in emit_sys_close_blob
+			       */
+			      (nr_maps * (6 + (gen->log_level ? 6 : 0)))));

I've removed the extra () in the above.

Quoted 4 lines

And pushed to bpf tree.
Please confirm that +1 removal was correct.

Thanks for the quick debugging and fix. Much appreciate it.

--
Kartikeya