diff mbox series

[bpf-next] libbpf: avoid mmap for size 0 sections

Message ID 20220727204808.13210-1-david.faust@oracle.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [bpf-next] libbpf: avoid mmap for size 0 sections | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers fail 11 maintainers not CCed: haoluo@google.com sdf@google.com john.fastabend@gmail.com ast@kernel.org jolsa@kernel.org yhs@fb.com daniel@iogearbox.net martin.lau@linux.dev kpsingh@kernel.org song@kernel.org andrii@kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: Statements should start on a tabstop WARNING: suspect code indent for conditional statements (16, 18)
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for Kernel LATEST on Array with llvm-15
bpf/vmtest-bpf-next-VM_Test-1 success Logs for Kernel LATEST on Array with gcc
bpf/vmtest-bpf-next-VM_Test-2 fail Logs for Kernel LATEST on Array with gcc

Commit Message

David Faust July 27, 2022, 8:48 p.m. UTC
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.

Signed-off-by: David Faust <david.faust@oracle.com>
---
 tools/lib/bpf/libbpf.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Jiri Olsa July 28, 2022, 2:15 p.m. UTC | #1
On Wed, Jul 27, 2022 at 01:48:08PM -0700, David Faust wrote:
> 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.
> 
> Signed-off-by: David Faust <david.faust@oracle.com>
> ---
>  tools/lib/bpf/libbpf.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index b01fe01b0761..4e7ceb4f5a27 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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

> +
>  		switch (sec_desc->sec_type) {
>  		case SEC_DATA:
>  			sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
> -- 
> 2.36.1
>
David Faust July 28, 2022, 7:29 p.m. UTC | #2
On 7/28/22 07:15, Jiri Olsa wrote:
> On Wed, Jul 27, 2022 at 01:48:08PM -0700, David Faust wrote:
>> 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.
>>
>> Signed-off-by: David Faust <david.faust@oracle.com>
>> ---
>>  tools/lib/bpf/libbpf.c | 4 ++++
>>  1 file changed, 4 insertions(+)
>>
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index b01fe01b0761..4e7ceb4f5a27 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
>> @@ -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

I see, thanks. Will send an updated patch.

David

> 
> jirka
> 
>> +
>>  		switch (sec_desc->sec_type) {
>>  		case SEC_DATA:
>>  			sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
>> -- 
>> 2.36.1
>>
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index b01fe01b0761..4e7ceb4f5a27 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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));