Skip to contentSkip to conversation

libbpf: avoid mmap for size 0 sections

PATCH In review David Faust 2 participants 3 messages Jul 27, 2022 – Jul 28, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: avoid mmap for size 0 sections

When populating maps in bpf_object__init_global_data_maps(), recognized sections with no data (e.g. a .bss with size 0) lead to an mmap of 0 bytes which fails with EINVAL.

Add a check to skip mapping sections which are present, but empty.

---
 tools/lib/bpf/libbpf.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +4 -0
index b01fe01b0761..4e7ceb4f5a27 100644
@@ -1642,6 +1642,10 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj)
 	for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
 		sec_desc = &obj->efile.secs[sec_idx];
 
+		/* Skip recognized sections with size 0. */
+		if (sec_desc->data && sec_desc->data->d_size == 0)
+		  continue;
+
 		switch (sec_desc->sec_type) {
 		case SEC_DATA:
 			sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
Signature · 1 line
2.36.1
#1 lore raw
Quoted 16 lines · David Faust

When populating maps in bpf_object__init_global_data_maps(), recognized sections with no data (e.g. a .bss with size 0) lead to an mmap of 0 bytes which fails with EINVAL.

Add a check to skip mapping sections which are present, but empty.

---
 tools/lib/bpf/libbpf.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index b01fe01b0761..4e7ceb4f5a27 100644

tools/lib/bpf/libbpf.c static int bpf_object__init_global_data_maps(struct bpf_object *obj) · line 1647

Quoted 6 lines
tools/lib/bpf/libbpf.c +3 -0
@@ -1642,6 +1642,10 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj)
 	for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
 		sec_desc = &obj->efile.secs[sec_idx];
 
+		/* Skip recognized sections with size 0. */
+		if (sec_desc->data && sec_desc->data->d_size == 0)
+		  continue;

nit missing tab indent

also we seem to check for size in bpf_object__elf_collect before adding SEC_DATA/SEC_RODATA but not SEC_BSS

I think the check should be rather in bpf_object__elf_collect before we add the desc for it

jirka

Quoted 7 lines
tools/lib/bpf/libbpf.c +1 -0
+
 		switch (sec_desc->sec_type) {
 		case SEC_DATA:
 			sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
Signature · 1 line
2.36.1
#2 lore raw
Quoted 26 lines · Jiri Olsa
Quoted 16 lines · David Faust

When populating maps in bpf_object__init_global_data_maps(), recognized sections with no data (e.g. a .bss with size 0) lead to an mmap of 0 bytes which fails with EINVAL.

Add a check to skip mapping sections which are present, but empty.

---
 tools/lib/bpf/libbpf.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index b01fe01b0761..4e7ceb4f5a27 100644

tools/lib/bpf/libbpf.c static int bpf_object__init_global_data_maps(struct bpf_object *obj) · line 1647

Quoted 6 lines
tools/lib/bpf/libbpf.c +3 -0
@@ -1642,6 +1642,10 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj)
 	for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
 		sec_desc = &obj->efile.secs[sec_idx];
 
+		/* Skip recognized sections with size 0. */
+		if (sec_desc->data && sec_desc->data->d_size == 0)
+		  continue;

nit missing tab indent

Quoted 5 lines

also we seem to check for size in bpf_object__elf_collect before adding SEC_DATA/SEC_RODATA but not SEC_BSS

I think the check should be rather in bpf_object__elf_collect before we add the desc for it

I see, thanks. Will send an updated patch.

David

Quoted 10 lines

jirka

Quoted 7 lines
tools/lib/bpf/libbpf.c +1 -0
+
 		switch (sec_desc->sec_type) {
 		case SEC_DATA:
 			sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
Signature · 1 line
2.36.1