public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tools/lib/bpf: add pr_warn() to more -EINVAL cases
@ 2023-12-07 18:09 Sergei Trofimovich
  2023-12-07 22:58 ` Eduard Zingerman
  0 siblings, 1 reply; 5+ messages in thread
From: Sergei Trofimovich @ 2023-12-07 18:09 UTC (permalink / raw)
  To: bpf
  Cc: linux-kernel, Sergei Trofimovich, Alexei Starovoitov,
	Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu,
	Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev,
	Hao Luo, Jiri Olsa

Before the change on `i686-linux` `systemd` build failed as:

    $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
    Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)

After the change it fails as:

    $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
    libbpf: ELF section #9 has inconsistent alignment in src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
    Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)

Now it's slightly easier to figure out what is wrong with an ELF file.

CC: Alexei Starovoitov <ast@kernel.org>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: Andrii Nakryiko <andrii@kernel.org>
CC: Martin KaFai Lau <martin.lau@linux.dev>
CC: Song Liu <song@kernel.org>
CC: Yonghong Song <yonghong.song@linux.dev>
CC: John Fastabend <john.fastabend@gmail.com>
CC: KP Singh <kpsingh@kernel.org>
CC: Stanislav Fomichev <sdf@google.com>
CC: Hao Luo <haoluo@google.com>
CC: Jiri Olsa <jolsa@kernel.org>
CC: bpf@vger.kernel.org
Signed-off-by: Sergei Trofimovich <slyich@gmail.com>
---
 tools/lib/bpf/linker.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 5ced96d99f8c..71bb4916b762 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -719,13 +719,22 @@ static int linker_sanity_check_elf(struct src_obj *obj)
 			return -EINVAL;
 		}
 
-		if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign))
+		if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign)) {
+			pr_warn("ELF section #%zu alignment is non pow-of-2 alignment in %s\n",
+				sec->sec_idx, obj->filename);
 			return -EINVAL;
-		if (sec->shdr->sh_addralign != sec->data->d_align)
+		}
+		if (sec->shdr->sh_addralign != sec->data->d_align) {
+			pr_warn("ELF section #%zu has inconsistent alignment in %s\n",
+				sec->sec_idx, obj->filename);
 			return -EINVAL;
+		}
 
-		if (sec->shdr->sh_size != sec->data->d_size)
+		if (sec->shdr->sh_size != sec->data->d_size) {
+			pr_warn("ELF section #%zu has inconsistent section size in %s\n",
+				sec->sec_idx, obj->filename);
 			return -EINVAL;
+		}
 
 		switch (sec->shdr->sh_type) {
 		case SHT_SYMTAB:
-- 
2.42.0


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

* Re: [PATCH] tools/lib/bpf: add pr_warn() to more -EINVAL cases
  2023-12-07 18:09 [PATCH] tools/lib/bpf: add pr_warn() to more -EINVAL cases Sergei Trofimovich
@ 2023-12-07 22:58 ` Eduard Zingerman
  2023-12-08 21:51   ` [PATCH v2] libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf Sergei Trofimovich
  0 siblings, 1 reply; 5+ messages in thread
From: Eduard Zingerman @ 2023-12-07 22:58 UTC (permalink / raw)
  To: Sergei Trofimovich, bpf
  Cc: linux-kernel, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa

On Thu, 2023-12-07 at 18:09 +0000, Sergei Trofimovich wrote:
> Before the change on `i686-linux` `systemd` build failed as:
> 
>     $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
>     Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)
> 
> After the change it fails as:
> 
>     $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
>     libbpf: ELF section #9 has inconsistent alignment in src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
>     Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)
> 
> Now it's slightly easier to figure out what is wrong with an ELF file.

Hi Sergei,

Thank you for adding these prints.
Could you please make a few adjustments, as noted below.
Also, please add "libbpf:" prefix in subject and mention
linker_sanity_check_elf in it, e.g.:

  libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf
  
or something like that.

[...]

> diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
> index 5ced96d99f8c..71bb4916b762 100644
> --- a/tools/lib/bpf/linker.c
> +++ b/tools/lib/bpf/linker.c
> @@ -719,13 +719,22 @@ static int linker_sanity_check_elf(struct src_obj *obj)
>  			return -EINVAL;
>  		}
>  
> -		if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign))
> +		if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign)) {
> +			pr_warn("ELF section #%zu alignment is non pow-of-2 alignment in %s\n",
> +				sec->sec_idx, obj->filename);

Could you please also print values for shdr->sh_addralign here?
And also print shdr->sh_addralign/data->d_align, shdr->sh_size/data->d_size
in corresponding pr_warn() calls below.

>  			return -EINVAL;
> -		if (sec->shdr->sh_addralign != sec->data->d_align)
> +		}
> +		if (sec->shdr->sh_addralign != sec->data->d_align) {
> +			pr_warn("ELF section #%zu has inconsistent alignment in %s\n",
> +				sec->sec_idx, obj->filename);
>  			return -EINVAL;
> +		}
>  
> -		if (sec->shdr->sh_size != sec->data->d_size)
> +		if (sec->shdr->sh_size != sec->data->d_size) {
> +			pr_warn("ELF section #%zu has inconsistent section size in %s\n",
> +				sec->sec_idx, obj->filename);
>  			return -EINVAL;
> +		}
>  
>  		switch (sec->shdr->sh_type) {
>  		case SHT_SYMTAB:

A few lines below this one there is:

		case SHT_PROGBITS:
			if (sec->shdr->sh_flags & SHF_EXECINSTR) {
				if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0)
					return -EINVAL;
			}
			break;

Could you please add pr_warn() there as well?

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

* [PATCH v2] libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf
  2023-12-07 22:58 ` Eduard Zingerman
@ 2023-12-08 21:51   ` Sergei Trofimovich
  2023-12-08 22:00     ` Eduard Zingerman
  2023-12-09  1:20     ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 5+ messages in thread
From: Sergei Trofimovich @ 2023-12-08 21:51 UTC (permalink / raw)
  To: bpf, Eduard Zingerman
  Cc: linux-kernel, Sergei Trofimovich, Alexei Starovoitov,
	Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu,
	Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev,
	Hao Luo, Jiri Olsa

Before the change on `i686-linux` `systemd` build failed as:

    $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
    Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)

After the change it fails as:

    $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
    libbpf: ELF section #9 has inconsistent alignment addr=8 != d=4 in src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
    Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)

Now it's slightly easier to figure out what is wrong with an ELF file.

CC: Alexei Starovoitov <ast@kernel.org>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: Andrii Nakryiko <andrii@kernel.org>
CC: Martin KaFai Lau <martin.lau@linux.dev>
CC: Song Liu <song@kernel.org>
CC: Yonghong Song <yonghong.song@linux.dev>
CC: John Fastabend <john.fastabend@gmail.com>
CC: KP Singh <kpsingh@kernel.org>
CC: Stanislav Fomichev <sdf@google.com>
CC: Hao Luo <haoluo@google.com>
CC: Jiri Olsa <jolsa@kernel.org>
CC: Eduard Zingerman <eddyz87@gmail.com>
CC: bpf@vger.kernel.org
Signed-off-by: Sergei Trofimovich <slyich@gmail.com>
---
 tools/lib/bpf/linker.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
Change since v1:
Following Eduard's suggestion added one extra pr_warn() call around
section alignment and added compared values into warning messages.

diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 5ced96d99f8c..52a2901e8bd0 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -719,13 +719,25 @@ static int linker_sanity_check_elf(struct src_obj *obj)
 			return -EINVAL;
 		}
 
-		if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign))
+		if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign)) {
+			pr_warn("ELF section #%zu alignment %llu is non pow-of-2 alignment in %s\n",
+				sec->sec_idx, (long long unsigned)sec->shdr->sh_addralign,
+				obj->filename);
 			return -EINVAL;
-		if (sec->shdr->sh_addralign != sec->data->d_align)
+		}
+		if (sec->shdr->sh_addralign != sec->data->d_align) {
+			pr_warn("ELF section #%zu has inconsistent alignment addr=%llu != d=%llu in %s\n",
+				sec->sec_idx, (long long unsigned)sec->shdr->sh_addralign,
+				(long long unsigned)sec->data->d_align, obj->filename);
 			return -EINVAL;
+		}
 
-		if (sec->shdr->sh_size != sec->data->d_size)
+		if (sec->shdr->sh_size != sec->data->d_size) {
+			pr_warn("ELF section #%zu has inconsistent section size sh=%llu != d=%llu in %s\n",
+				sec->sec_idx, (long long unsigned)sec->shdr->sh_size,
+				(long long unsigned)sec->data->d_size, obj->filename);
 			return -EINVAL;
+		}
 
 		switch (sec->shdr->sh_type) {
 		case SHT_SYMTAB:
@@ -737,8 +749,12 @@ static int linker_sanity_check_elf(struct src_obj *obj)
 			break;
 		case SHT_PROGBITS:
 			if (sec->shdr->sh_flags & SHF_EXECINSTR) {
-				if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0)
+				if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0) {
+					pr_warn("ELF section #%zu has unexpected size alignment %llu in %s\n",
+						sec->sec_idx, (long long unsigned)sec->shdr->sh_size,
+						obj->filename);
 					return -EINVAL;
+				}
 			}
 			break;
 		case SHT_NOBITS:
-- 
2.42.0


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

* Re: [PATCH v2] libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf
  2023-12-08 21:51   ` [PATCH v2] libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf Sergei Trofimovich
@ 2023-12-08 22:00     ` Eduard Zingerman
  2023-12-09  1:20     ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 5+ messages in thread
From: Eduard Zingerman @ 2023-12-08 22:00 UTC (permalink / raw)
  To: Sergei Trofimovich, bpf
  Cc: linux-kernel, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa

On Fri, 2023-12-08 at 21:51 +0000, Sergei Trofimovich wrote:
> Before the change on `i686-linux` `systemd` build failed as:
> 
>     $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
>     Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)
> 
> After the change it fails as:
> 
>     $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
>     libbpf: ELF section #9 has inconsistent alignment addr=8 != d=4 in src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
>     Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)
> 
> Now it's slightly easier to figure out what is wrong with an ELF file.
> 
> CC: Alexei Starovoitov <ast@kernel.org>
> CC: Daniel Borkmann <daniel@iogearbox.net>
> CC: Andrii Nakryiko <andrii@kernel.org>
> CC: Martin KaFai Lau <martin.lau@linux.dev>
> CC: Song Liu <song@kernel.org>
> CC: Yonghong Song <yonghong.song@linux.dev>
> CC: John Fastabend <john.fastabend@gmail.com>
> CC: KP Singh <kpsingh@kernel.org>
> CC: Stanislav Fomichev <sdf@google.com>
> CC: Hao Luo <haoluo@google.com>
> CC: Jiri Olsa <jolsa@kernel.org>
> CC: Eduard Zingerman <eddyz87@gmail.com>
> CC: bpf@vger.kernel.org
> Signed-off-by: Sergei Trofimovich <slyich@gmail.com>
> ---
>  tools/lib/bpf/linker.c | 24 ++++++++++++++++++++----
>  1 file changed, 20 insertions(+), 4 deletions(-)
> Change since v1:
> Following Eduard's suggestion added one extra pr_warn() call around
> section alignment and added compared values into warning messages.

Thank you for doing adjustments.
(Note: when I apply this patch locally full CC block is a part of the
       commit message, this is usually omitted from commit messages).

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

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

* Re: [PATCH v2] libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf
  2023-12-08 21:51   ` [PATCH v2] libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf Sergei Trofimovich
  2023-12-08 22:00     ` Eduard Zingerman
@ 2023-12-09  1:20     ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-12-09  1:20 UTC (permalink / raw)
  To: Sergei Trofimovich
  Cc: bpf, eddyz87, linux-kernel, ast, daniel, andrii, martin.lau, song,
	yonghong.song, john.fastabend, kpsingh, sdf, haoluo, jolsa

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Fri,  8 Dec 2023 21:51:00 +0000 you wrote:
> Before the change on `i686-linux` `systemd` build failed as:
> 
>     $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
>     Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)
> 
> After the change it fails as:
> 
> [...]

Here is the summary with links:
  - [v2] libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf
    https://git.kernel.org/bpf/bpf-next/c/32fa05839862

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2023-12-09  1:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-07 18:09 [PATCH] tools/lib/bpf: add pr_warn() to more -EINVAL cases Sergei Trofimovich
2023-12-07 22:58 ` Eduard Zingerman
2023-12-08 21:51   ` [PATCH v2] libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf Sergei Trofimovich
2023-12-08 22:00     ` Eduard Zingerman
2023-12-09  1:20     ` patchwork-bot+netdevbpf

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