diff mbox series

[v2,3/3] btf: Avoid weak external references

Message ID 20240409150132.4097042-8-ardb+git@google.com (mailing list archive)
State New
Headers show
Series kbuild: Avoid weak external linkage where possible | expand

Commit Message

Ard Biesheuvel April 9, 2024, 3:01 p.m. UTC
From: Ard Biesheuvel <ardb@kernel.org>

If the BTF code is enabled in the build configuration, the start/stop
BTF markers are guaranteed to exist in the final link but not during the
first linker pass.

Avoid GOT based relocations to these markers in the final executable by
providing preliminary definitions that will be used by the first linker
pass, and superseded by the actual definitions in the subsequent ones.

Make the preliminary definitions dependent on CONFIG_DEBUG_INFO_BTF so
that inadvertent references to this section will trigger a link failure
if they occur in code that does not honour CONFIG_DEBUG_INFO_BTF.

Note that Clang will notice that taking the address of__start_BTF cannot
yield NULL any longer, so testing for that condition is no longer
needed.

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 include/asm-generic/vmlinux.lds.h | 9 +++++++++
 kernel/bpf/btf.c                  | 4 ++--
 kernel/bpf/sysfs_btf.c            | 6 +++---
 3 files changed, 14 insertions(+), 5 deletions(-)

Comments

Arnd Bergmann April 9, 2024, 3:05 p.m. UTC | #1
On Tue, Apr 9, 2024, at 17:01, Ard Biesheuvel wrote:
> From: Ard Biesheuvel <ardb@kernel.org>
>
> If the BTF code is enabled in the build configuration, the start/stop
> BTF markers are guaranteed to exist in the final link but not during the
> first linker pass.
>
> Avoid GOT based relocations to these markers in the final executable by
> providing preliminary definitions that will be used by the first linker
> pass, and superseded by the actual definitions in the subsequent ones.
>
> Make the preliminary definitions dependent on CONFIG_DEBUG_INFO_BTF so
> that inadvertent references to this section will trigger a link failure
> if they occur in code that does not honour CONFIG_DEBUG_INFO_BTF.
>
> Note that Clang will notice that taking the address of__start_BTF cannot
> yield NULL any longer, so testing for that condition is no longer
> needed.
>
> Acked-by: Andrii Nakryiko <andrii@kernel.org>
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>

Acked-by: Arnd Bergmann <arnd@arndb.de>
Jiri Olsa April 10, 2024, 8:21 a.m. UTC | #2
On Tue, Apr 09, 2024 at 05:01:36PM +0200, Ard Biesheuvel wrote:
> From: Ard Biesheuvel <ardb@kernel.org>
> 
> If the BTF code is enabled in the build configuration, the start/stop
> BTF markers are guaranteed to exist in the final link but not during the
> first linker pass.
> 
> Avoid GOT based relocations to these markers in the final executable by
> providing preliminary definitions that will be used by the first linker
> pass, and superseded by the actual definitions in the subsequent ones.
> 
> Make the preliminary definitions dependent on CONFIG_DEBUG_INFO_BTF so
> that inadvertent references to this section will trigger a link failure
> if they occur in code that does not honour CONFIG_DEBUG_INFO_BTF.
> 
> Note that Clang will notice that taking the address of__start_BTF cannot
> yield NULL any longer, so testing for that condition is no longer
> needed.
> 
> Acked-by: Andrii Nakryiko <andrii@kernel.org>
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> ---
>  include/asm-generic/vmlinux.lds.h | 9 +++++++++
>  kernel/bpf/btf.c                  | 4 ++--
>  kernel/bpf/sysfs_btf.c            | 6 +++---
>  3 files changed, 14 insertions(+), 5 deletions(-)
> 
> diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
> index e8449be62058..4cb3d88449e5 100644
> --- a/include/asm-generic/vmlinux.lds.h
> +++ b/include/asm-generic/vmlinux.lds.h
> @@ -456,6 +456,7 @@
>   * independent code.
>   */
>  #define PRELIMINARY_SYMBOL_DEFINITIONS					\
> +	PRELIMINARY_BTF_DEFINITIONS					\
>  	PROVIDE(kallsyms_addresses = .);				\
>  	PROVIDE(kallsyms_offsets = .);					\
>  	PROVIDE(kallsyms_names = .);					\
> @@ -466,6 +467,14 @@
>  	PROVIDE(kallsyms_markers = .);					\
>  	PROVIDE(kallsyms_seqs_of_names = .);
>  
> +#ifdef CONFIG_DEBUG_INFO_BTF
> +#define PRELIMINARY_BTF_DEFINITIONS					\
> +	PROVIDE(__start_BTF = .);					\
> +	PROVIDE(__stop_BTF = .);
> +#else
> +#define PRELIMINARY_BTF_DEFINITIONS
> +#endif

hi,
I'm getting following compilation fail when CONFIG_DEBUG_INFO_BTF is disabled

	[jolsa@krava linux-qemu]$ make 
	  CALL    scripts/checksyscalls.sh
	  DESCEND objtool
	  INSTALL libsubcmd_headers
	  UPD     include/generated/utsversion.h
	  CC      init/version-timestamp.o
	  LD      .tmp_vmlinux.kallsyms1
	ld: kernel/bpf/btf.o: in function `btf_parse_vmlinux':
	/home/jolsa/kernel/linux-qemu/kernel/bpf/btf.c:5988: undefined reference to `__start_BTF'
	ld: /home/jolsa/kernel/linux-qemu/kernel/bpf/btf.c:5989: undefined reference to `__stop_BTF'
	ld: /home/jolsa/kernel/linux-qemu/kernel/bpf/btf.c:5989: undefined reference to `__start_BTF'
	make[2]: *** [scripts/Makefile.vmlinux:37: vmlinux] Error 1
	make[1]: *** [/home/jolsa/kernel/linux-qemu/Makefile:1160: vmlinux] Error 2
	make: *** [Makefile:240: __sub-make] Error 2

maybe the assumption was that kernel/bpf/btf.o is compiled only
for CONFIG_DEBUG_INFO_BTF, but it's actually:

  obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o

I guess we just need !CONFIG_DEBUG_INFO_BTF version of btf_parse_vmlinux
function

jirka

> +
>  /*
>   * Read only Data
>   */
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 90c4a32d89ff..46a56bf067a8 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -5642,8 +5642,8 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat
>  	return ERR_PTR(err);
>  }
>  
> -extern char __weak __start_BTF[];
> -extern char __weak __stop_BTF[];
> +extern char __start_BTF[];
> +extern char __stop_BTF[];
>  extern struct btf *btf_vmlinux;
>  
>  #define BPF_MAP_TYPE(_id, _ops)
> diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c
> index ef6911aee3bb..fedb54c94cdb 100644
> --- a/kernel/bpf/sysfs_btf.c
> +++ b/kernel/bpf/sysfs_btf.c
> @@ -9,8 +9,8 @@
>  #include <linux/sysfs.h>
>  
>  /* See scripts/link-vmlinux.sh, gen_btf() func for details */
> -extern char __weak __start_BTF[];
> -extern char __weak __stop_BTF[];
> +extern char __start_BTF[];
> +extern char __stop_BTF[];
>  
>  static ssize_t
>  btf_vmlinux_read(struct file *file, struct kobject *kobj,
> @@ -32,7 +32,7 @@ static int __init btf_vmlinux_init(void)
>  {
>  	bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;
>  
> -	if (!__start_BTF || bin_attr_btf_vmlinux.size == 0)
> +	if (bin_attr_btf_vmlinux.size == 0)
>  		return 0;
>  
>  	btf_kobj = kobject_create_and_add("btf", kernel_kobj);
> -- 
> 2.44.0.478.gd926399ef9-goog
> 
>
Ard Biesheuvel April 10, 2024, 8:37 a.m. UTC | #3
On Wed, 10 Apr 2024 at 10:22, Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Tue, Apr 09, 2024 at 05:01:36PM +0200, Ard Biesheuvel wrote:
> > From: Ard Biesheuvel <ardb@kernel.org>
> >
> > If the BTF code is enabled in the build configuration, the start/stop
> > BTF markers are guaranteed to exist in the final link but not during the
> > first linker pass.
> >
> > Avoid GOT based relocations to these markers in the final executable by
> > providing preliminary definitions that will be used by the first linker
> > pass, and superseded by the actual definitions in the subsequent ones.
> >
> > Make the preliminary definitions dependent on CONFIG_DEBUG_INFO_BTF so
> > that inadvertent references to this section will trigger a link failure
> > if they occur in code that does not honour CONFIG_DEBUG_INFO_BTF.
> >
> > Note that Clang will notice that taking the address of__start_BTF cannot
> > yield NULL any longer, so testing for that condition is no longer
> > needed.
> >
> > Acked-by: Andrii Nakryiko <andrii@kernel.org>
> > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> > ---
> >  include/asm-generic/vmlinux.lds.h | 9 +++++++++
> >  kernel/bpf/btf.c                  | 4 ++--
> >  kernel/bpf/sysfs_btf.c            | 6 +++---
> >  3 files changed, 14 insertions(+), 5 deletions(-)
> >
> > diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
> > index e8449be62058..4cb3d88449e5 100644
> > --- a/include/asm-generic/vmlinux.lds.h
> > +++ b/include/asm-generic/vmlinux.lds.h
> > @@ -456,6 +456,7 @@
> >   * independent code.
> >   */
> >  #define PRELIMINARY_SYMBOL_DEFINITIONS                                       \
> > +     PRELIMINARY_BTF_DEFINITIONS                                     \
> >       PROVIDE(kallsyms_addresses = .);                                \
> >       PROVIDE(kallsyms_offsets = .);                                  \
> >       PROVIDE(kallsyms_names = .);                                    \
> > @@ -466,6 +467,14 @@
> >       PROVIDE(kallsyms_markers = .);                                  \
> >       PROVIDE(kallsyms_seqs_of_names = .);
> >
> > +#ifdef CONFIG_DEBUG_INFO_BTF
> > +#define PRELIMINARY_BTF_DEFINITIONS                                  \
> > +     PROVIDE(__start_BTF = .);                                       \
> > +     PROVIDE(__stop_BTF = .);
> > +#else
> > +#define PRELIMINARY_BTF_DEFINITIONS
> > +#endif
>
> hi,
> I'm getting following compilation fail when CONFIG_DEBUG_INFO_BTF is disabled
>
>         [jolsa@krava linux-qemu]$ make
>           CALL    scripts/checksyscalls.sh
>           DESCEND objtool
>           INSTALL libsubcmd_headers
>           UPD     include/generated/utsversion.h
>           CC      init/version-timestamp.o
>           LD      .tmp_vmlinux.kallsyms1
>         ld: kernel/bpf/btf.o: in function `btf_parse_vmlinux':
>         /home/jolsa/kernel/linux-qemu/kernel/bpf/btf.c:5988: undefined reference to `__start_BTF'
>         ld: /home/jolsa/kernel/linux-qemu/kernel/bpf/btf.c:5989: undefined reference to `__stop_BTF'
>         ld: /home/jolsa/kernel/linux-qemu/kernel/bpf/btf.c:5989: undefined reference to `__start_BTF'
>         make[2]: *** [scripts/Makefile.vmlinux:37: vmlinux] Error 1
>         make[1]: *** [/home/jolsa/kernel/linux-qemu/Makefile:1160: vmlinux] Error 2
>         make: *** [Makefile:240: __sub-make] Error 2
>
> maybe the assumption was that kernel/bpf/btf.o is compiled only
> for CONFIG_DEBUG_INFO_BTF, but it's actually:
>
>   obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o
>

Interesting. I did build test this with and without
CONFIG_DEBUG_INFO_BTF, but not with CONFIG_BPF_SYSCALL=y and
CONFIG_DEBUG_INFO_BTF=n.

> I guess we just need !CONFIG_DEBUG_INFO_BTF version of btf_parse_vmlinux
> function
>

The below gives me a working build.

--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -5971,6 +5971,9 @@ struct btf *btf_parse_vmlinux(void)
        struct btf *btf = NULL;
        int err;

+       if (!IS_ENABLED(CONFIG_DEBUG_INFO_BTF))
+               return ERR_PTR(-ENOENT);
+
        env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
        if (!env)
                return ERR_PTR(-ENOMEM);
Jiri Olsa April 10, 2024, 9:30 a.m. UTC | #4
On Wed, Apr 10, 2024 at 10:37:42AM +0200, Ard Biesheuvel wrote:
> On Wed, 10 Apr 2024 at 10:22, Jiri Olsa <olsajiri@gmail.com> wrote:
> >
> > On Tue, Apr 09, 2024 at 05:01:36PM +0200, Ard Biesheuvel wrote:
> > > From: Ard Biesheuvel <ardb@kernel.org>
> > >
> > > If the BTF code is enabled in the build configuration, the start/stop
> > > BTF markers are guaranteed to exist in the final link but not during the
> > > first linker pass.
> > >
> > > Avoid GOT based relocations to these markers in the final executable by
> > > providing preliminary definitions that will be used by the first linker
> > > pass, and superseded by the actual definitions in the subsequent ones.
> > >
> > > Make the preliminary definitions dependent on CONFIG_DEBUG_INFO_BTF so
> > > that inadvertent references to this section will trigger a link failure
> > > if they occur in code that does not honour CONFIG_DEBUG_INFO_BTF.
> > >
> > > Note that Clang will notice that taking the address of__start_BTF cannot
> > > yield NULL any longer, so testing for that condition is no longer
> > > needed.
> > >
> > > Acked-by: Andrii Nakryiko <andrii@kernel.org>
> > > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> > > ---
> > >  include/asm-generic/vmlinux.lds.h | 9 +++++++++
> > >  kernel/bpf/btf.c                  | 4 ++--
> > >  kernel/bpf/sysfs_btf.c            | 6 +++---
> > >  3 files changed, 14 insertions(+), 5 deletions(-)
> > >
> > > diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
> > > index e8449be62058..4cb3d88449e5 100644
> > > --- a/include/asm-generic/vmlinux.lds.h
> > > +++ b/include/asm-generic/vmlinux.lds.h
> > > @@ -456,6 +456,7 @@
> > >   * independent code.
> > >   */
> > >  #define PRELIMINARY_SYMBOL_DEFINITIONS                                       \
> > > +     PRELIMINARY_BTF_DEFINITIONS                                     \
> > >       PROVIDE(kallsyms_addresses = .);                                \
> > >       PROVIDE(kallsyms_offsets = .);                                  \
> > >       PROVIDE(kallsyms_names = .);                                    \
> > > @@ -466,6 +467,14 @@
> > >       PROVIDE(kallsyms_markers = .);                                  \
> > >       PROVIDE(kallsyms_seqs_of_names = .);
> > >
> > > +#ifdef CONFIG_DEBUG_INFO_BTF
> > > +#define PRELIMINARY_BTF_DEFINITIONS                                  \
> > > +     PROVIDE(__start_BTF = .);                                       \
> > > +     PROVIDE(__stop_BTF = .);
> > > +#else
> > > +#define PRELIMINARY_BTF_DEFINITIONS
> > > +#endif
> >
> > hi,
> > I'm getting following compilation fail when CONFIG_DEBUG_INFO_BTF is disabled
> >
> >         [jolsa@krava linux-qemu]$ make
> >           CALL    scripts/checksyscalls.sh
> >           DESCEND objtool
> >           INSTALL libsubcmd_headers
> >           UPD     include/generated/utsversion.h
> >           CC      init/version-timestamp.o
> >           LD      .tmp_vmlinux.kallsyms1
> >         ld: kernel/bpf/btf.o: in function `btf_parse_vmlinux':
> >         /home/jolsa/kernel/linux-qemu/kernel/bpf/btf.c:5988: undefined reference to `__start_BTF'
> >         ld: /home/jolsa/kernel/linux-qemu/kernel/bpf/btf.c:5989: undefined reference to `__stop_BTF'
> >         ld: /home/jolsa/kernel/linux-qemu/kernel/bpf/btf.c:5989: undefined reference to `__start_BTF'
> >         make[2]: *** [scripts/Makefile.vmlinux:37: vmlinux] Error 1
> >         make[1]: *** [/home/jolsa/kernel/linux-qemu/Makefile:1160: vmlinux] Error 2
> >         make: *** [Makefile:240: __sub-make] Error 2
> >
> > maybe the assumption was that kernel/bpf/btf.o is compiled only
> > for CONFIG_DEBUG_INFO_BTF, but it's actually:
> >
> >   obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o
> >
> 
> Interesting. I did build test this with and without
> CONFIG_DEBUG_INFO_BTF, but not with CONFIG_BPF_SYSCALL=y and
> CONFIG_DEBUG_INFO_BTF=n.
> 
> > I guess we just need !CONFIG_DEBUG_INFO_BTF version of btf_parse_vmlinux
> > function
> >
> 
> The below gives me a working build.
> 
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -5971,6 +5971,9 @@ struct btf *btf_parse_vmlinux(void)
>         struct btf *btf = NULL;
>         int err;
> 
> +       if (!IS_ENABLED(CONFIG_DEBUG_INFO_BTF))
> +               return ERR_PTR(-ENOENT);

nice, so this basically eliminates the rest of the function,
I did not know this would work

build's fine now, thanks

jirka

> +
>         env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
>         if (!env)
>                 return ERR_PTR(-ENOMEM);
diff mbox series

Patch

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index e8449be62058..4cb3d88449e5 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -456,6 +456,7 @@ 
  * independent code.
  */
 #define PRELIMINARY_SYMBOL_DEFINITIONS					\
+	PRELIMINARY_BTF_DEFINITIONS					\
 	PROVIDE(kallsyms_addresses = .);				\
 	PROVIDE(kallsyms_offsets = .);					\
 	PROVIDE(kallsyms_names = .);					\
@@ -466,6 +467,14 @@ 
 	PROVIDE(kallsyms_markers = .);					\
 	PROVIDE(kallsyms_seqs_of_names = .);
 
+#ifdef CONFIG_DEBUG_INFO_BTF
+#define PRELIMINARY_BTF_DEFINITIONS					\
+	PROVIDE(__start_BTF = .);					\
+	PROVIDE(__stop_BTF = .);
+#else
+#define PRELIMINARY_BTF_DEFINITIONS
+#endif
+
 /*
  * Read only Data
  */
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 90c4a32d89ff..46a56bf067a8 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -5642,8 +5642,8 @@  static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat
 	return ERR_PTR(err);
 }
 
-extern char __weak __start_BTF[];
-extern char __weak __stop_BTF[];
+extern char __start_BTF[];
+extern char __stop_BTF[];
 extern struct btf *btf_vmlinux;
 
 #define BPF_MAP_TYPE(_id, _ops)
diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c
index ef6911aee3bb..fedb54c94cdb 100644
--- a/kernel/bpf/sysfs_btf.c
+++ b/kernel/bpf/sysfs_btf.c
@@ -9,8 +9,8 @@ 
 #include <linux/sysfs.h>
 
 /* See scripts/link-vmlinux.sh, gen_btf() func for details */
-extern char __weak __start_BTF[];
-extern char __weak __stop_BTF[];
+extern char __start_BTF[];
+extern char __stop_BTF[];
 
 static ssize_t
 btf_vmlinux_read(struct file *file, struct kobject *kobj,
@@ -32,7 +32,7 @@  static int __init btf_vmlinux_init(void)
 {
 	bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;
 
-	if (!__start_BTF || bin_attr_btf_vmlinux.size == 0)
+	if (bin_attr_btf_vmlinux.size == 0)
 		return 0;
 
 	btf_kobj = kobject_create_and_add("btf", kernel_kobj);