bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] libbpf: avoid mmap for size 0 sections
@ 2022-07-27 20:48 David Faust
  2022-07-28 14:15 ` Jiri Olsa
  0 siblings, 1 reply; 3+ messages in thread
From: David Faust @ 2022-07-27 20:48 UTC (permalink / raw)
  To: bpf; +Cc: 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.

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;
+
 		switch (sec_desc->sec_type) {
 		case SEC_DATA:
 			sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
-- 
2.36.1


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

* Re: [PATCH bpf-next] libbpf: avoid mmap for size 0 sections
  2022-07-27 20:48 [PATCH bpf-next] libbpf: avoid mmap for size 0 sections David Faust
@ 2022-07-28 14:15 ` Jiri Olsa
  2022-07-28 19:29   ` David Faust
  0 siblings, 1 reply; 3+ messages in thread
From: Jiri Olsa @ 2022-07-28 14:15 UTC (permalink / raw)
  To: David Faust; +Cc: bpf

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
> 

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

* Re: [PATCH bpf-next] libbpf: avoid mmap for size 0 sections
  2022-07-28 14:15 ` Jiri Olsa
@ 2022-07-28 19:29   ` David Faust
  0 siblings, 0 replies; 3+ messages in thread
From: David Faust @ 2022-07-28 19:29 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: bpf



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
>>

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

end of thread, other threads:[~2022-07-28 19:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-27 20:48 [PATCH bpf-next] libbpf: avoid mmap for size 0 sections David Faust
2022-07-28 14:15 ` Jiri Olsa
2022-07-28 19:29   ` David Faust

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).