diff mbox series

[bpf,v2,2/2] bpf: Harden __bpf_kfunc tag against linker kfunc removal

Message ID e9c64e9b5c073dabd457ff45128aabcab7630098.1717477560.git.Tony.Ambardar@gmail.com (mailing list archive)
State Accepted
Commit 7bdcedd5c8fb88e7176b93812b139eca5fe0aa46
Delegated to: BPF
Headers show
Series bpf: Fix linker optimization removing kfuncs | expand

Checks

Context Check Description
bpf/vmtest-bpf-VM_Test-45 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-46 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-VM_Test-43 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-44 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 6637 this patch: 6637
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers fail 1 blamed authors not CCed: void@manifault.com; 1 maintainers not CCed: void@manifault.com
netdev/build_clang fail Errors and warnings before: 344 this patch: 344
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn fail Errors and warnings before: 7005 this patch: 7005
netdev/checkpatch fail CHECK: From:/Signed-off-by: email comments mismatch: 'From: Tony Ambardar <tony.ambardar@gmail.com>' != 'Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>' ERROR: Macros with complex values should be enclosed in parentheses
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc

Commit Message

Tony Ambardar June 4, 2024, 5:23 a.m. UTC
BPF kfuncs are often not directly referenced and may be inadvertently
removed by optimization steps during kernel builds, thus the __bpf_kfunc
tag mitigates against this removal by including the __used macro. However,
this macro alone does not prevent removal during linking, and may still
yield build warnings (e.g. on mips64el):

    LD      vmlinux
    BTFIDS  vmlinux
  WARN: resolve_btfids: unresolved symbol bpf_verify_pkcs7_signature
  WARN: resolve_btfids: unresolved symbol bpf_lookup_user_key
  WARN: resolve_btfids: unresolved symbol bpf_lookup_system_key
  WARN: resolve_btfids: unresolved symbol bpf_key_put
  WARN: resolve_btfids: unresolved symbol bpf_iter_task_next
  WARN: resolve_btfids: unresolved symbol bpf_iter_css_task_new
  WARN: resolve_btfids: unresolved symbol bpf_get_file_xattr
  WARN: resolve_btfids: unresolved symbol bpf_ct_insert_entry
  WARN: resolve_btfids: unresolved symbol bpf_cgroup_release
  WARN: resolve_btfids: unresolved symbol bpf_cgroup_from_id
  WARN: resolve_btfids: unresolved symbol bpf_cgroup_acquire
  WARN: resolve_btfids: unresolved symbol bpf_arena_free_pages
    NM      System.map
    SORTTAB vmlinux
    OBJCOPY vmlinux.32

Update the __bpf_kfunc tag to better guard against linker optimization by
including the new __retain compiler macro, which fixes the warnings above.

Verify the __retain macro with readelf by checking object flags for 'R':

  $ readelf -Wa kernel/trace/bpf_trace.o
  Section Headers:
    [Nr]  Name              Type     Address  Off  Size ES Flg Lk Inf Al
  ...
    [178] .text.bpf_key_put PROGBITS 00000000 6420 0050 00 AXR  0   0  8
  ...
  Key to Flags:
  ...
    R (retain), D (mbind), p (processor specific)

Link: https://lore.kernel.org/bpf/ZlmGoT9KiYLZd91S@krava/T/
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/r/202401211357.OCX9yllM-lkp@intel.com/
Fixes: 57e7c169cd6a ("bpf: Add __bpf_kfunc tag for marking kernel functions as kfuncs")
Cc: stable@vger.kernel.org # v6.6+
Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
---
 include/linux/btf.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Jiri Olsa June 4, 2024, 7:56 a.m. UTC | #1
On Mon, Jun 03, 2024 at 10:23:16PM -0700, Tony Ambardar wrote:
> BPF kfuncs are often not directly referenced and may be inadvertently
> removed by optimization steps during kernel builds, thus the __bpf_kfunc
> tag mitigates against this removal by including the __used macro. However,
> this macro alone does not prevent removal during linking, and may still
> yield build warnings (e.g. on mips64el):
> 
>     LD      vmlinux
>     BTFIDS  vmlinux
>   WARN: resolve_btfids: unresolved symbol bpf_verify_pkcs7_signature
>   WARN: resolve_btfids: unresolved symbol bpf_lookup_user_key
>   WARN: resolve_btfids: unresolved symbol bpf_lookup_system_key
>   WARN: resolve_btfids: unresolved symbol bpf_key_put
>   WARN: resolve_btfids: unresolved symbol bpf_iter_task_next
>   WARN: resolve_btfids: unresolved symbol bpf_iter_css_task_new
>   WARN: resolve_btfids: unresolved symbol bpf_get_file_xattr
>   WARN: resolve_btfids: unresolved symbol bpf_ct_insert_entry
>   WARN: resolve_btfids: unresolved symbol bpf_cgroup_release
>   WARN: resolve_btfids: unresolved symbol bpf_cgroup_from_id
>   WARN: resolve_btfids: unresolved symbol bpf_cgroup_acquire
>   WARN: resolve_btfids: unresolved symbol bpf_arena_free_pages
>     NM      System.map
>     SORTTAB vmlinux
>     OBJCOPY vmlinux.32
> 
> Update the __bpf_kfunc tag to better guard against linker optimization by
> including the new __retain compiler macro, which fixes the warnings above.
> 
> Verify the __retain macro with readelf by checking object flags for 'R':
> 
>   $ readelf -Wa kernel/trace/bpf_trace.o
>   Section Headers:
>     [Nr]  Name              Type     Address  Off  Size ES Flg Lk Inf Al
>   ...
>     [178] .text.bpf_key_put PROGBITS 00000000 6420 0050 00 AXR  0   0  8
>   ...
>   Key to Flags:
>   ...
>     R (retain), D (mbind), p (processor specific)
> 
> Link: https://lore.kernel.org/bpf/ZlmGoT9KiYLZd91S@krava/T/
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/r/202401211357.OCX9yllM-lkp@intel.com/
> Fixes: 57e7c169cd6a ("bpf: Add __bpf_kfunc tag for marking kernel functions as kfuncs")
> Cc: stable@vger.kernel.org # v6.6+
> Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>

tested on mips64 cross build and the warnings are gone
and related functions are in the vmlinux

patchset looks good to me

Tested-by: Jiri Olsa <jolsa@kernel.org>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka

> ---
>  include/linux/btf.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index f9e56fd12a9f..7c3e40c3295e 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -82,7 +82,7 @@
>   * as to avoid issues such as the compiler inlining or eliding either a static
>   * kfunc, or a global kfunc in an LTO build.
>   */
> -#define __bpf_kfunc __used noinline
> +#define __bpf_kfunc __used __retain noinline
>  
>  #define __bpf_kfunc_start_defs()					       \
>  	__diag_push();							       \
> -- 
> 2.34.1
>
Geert Uytterhoeven June 25, 2024, 10:46 a.m. UTC | #2
Hi Tony,

On Mon, 3 Jun 2024, Tony Ambardar wrote:
> BPF kfuncs are often not directly referenced and may be inadvertently
> removed by optimization steps during kernel builds, thus the __bpf_kfunc
> tag mitigates against this removal by including the __used macro. However,
> this macro alone does not prevent removal during linking, and may still
> yield build warnings (e.g. on mips64el):
>
>    LD      vmlinux
>    BTFIDS  vmlinux
>  WARN: resolve_btfids: unresolved symbol bpf_verify_pkcs7_signature
>  WARN: resolve_btfids: unresolved symbol bpf_lookup_user_key
>  WARN: resolve_btfids: unresolved symbol bpf_lookup_system_key
>  WARN: resolve_btfids: unresolved symbol bpf_key_put
>  WARN: resolve_btfids: unresolved symbol bpf_iter_task_next
>  WARN: resolve_btfids: unresolved symbol bpf_iter_css_task_new
>  WARN: resolve_btfids: unresolved symbol bpf_get_file_xattr
>  WARN: resolve_btfids: unresolved symbol bpf_ct_insert_entry
>  WARN: resolve_btfids: unresolved symbol bpf_cgroup_release
>  WARN: resolve_btfids: unresolved symbol bpf_cgroup_from_id
>  WARN: resolve_btfids: unresolved symbol bpf_cgroup_acquire
>  WARN: resolve_btfids: unresolved symbol bpf_arena_free_pages
>    NM      System.map
>    SORTTAB vmlinux
>    OBJCOPY vmlinux.32
>
> Update the __bpf_kfunc tag to better guard against linker optimization by
> including the new __retain compiler macro, which fixes the warnings above.
>
> Verify the __retain macro with readelf by checking object flags for 'R':
>
>  $ readelf -Wa kernel/trace/bpf_trace.o
>  Section Headers:
>    [Nr]  Name              Type     Address  Off  Size ES Flg Lk Inf Al
>  ...
>    [178] .text.bpf_key_put PROGBITS 00000000 6420 0050 00 AXR  0   0  8
>  ...
>  Key to Flags:
>  ...
>    R (retain), D (mbind), p (processor specific)
>
> Link: https://lore.kernel.org/bpf/ZlmGoT9KiYLZd91S@krava/T/
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/r/202401211357.OCX9yllM-lkp@intel.com/
> Fixes: 57e7c169cd6a ("bpf: Add __bpf_kfunc tag for marking kernel functions as kfuncs")
> Cc: stable@vger.kernel.org # v6.6+
> Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>

Thanks for your patch, which is now commit 7bdcedd5c8fb88e7
("bpf: Harden __bpf_kfunc tag against linker kfunc removal") in
v6.10-rc5.

This is causing build failures on ARM with
CONFIG_LD_DEAD_CODE_DATA_ELIMINATION=y:

     net/core/filter.c:11859:1: error: ‘retain’ attribute ignored [-Werror=attributes]
     11859 | {
           | ^
     net/core/filter.c:11872:1: error: ‘retain’ attribute ignored [-Werror=attributes]
     11872 | {
           | ^
     net/core/filter.c:11885:1: error: ‘retain’ attribute ignored [-Werror=attributes]
     11885 | {
           | ^
     net/core/filter.c:11906:1: error: ‘retain’ attribute ignored [-Werror=attributes]
     11906 | {
           | ^
     net/core/filter.c:12092:1: error: ‘retain’ attribute ignored [-Werror=attributes]
     12092 | {
           | ^
     net/core/xdp.c:713:1: error: ‘retain’ attribute ignored [-Werror=attributes]
       713 | {
           | ^
     net/core/xdp.c:736:1: error: ‘retain’ attribute ignored [-Werror=attributes]
       736 | {
           | ^
     net/core/xdp.c:769:1: error: ‘retain’ attribute ignored [-Werror=attributes]
       769 | {
           | ^
     [...]

My compiler is arm-linux-gnueabihf-gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04).

> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -82,7 +82,7 @@
>  * as to avoid issues such as the compiler inlining or eliding either a static
>  * kfunc, or a global kfunc in an LTO build.
>  */
> -#define __bpf_kfunc __used noinline
> +#define __bpf_kfunc __used __retain noinline
>
> #define __bpf_kfunc_start_defs()					       \
> 	__diag_push();							       \

Gr{oetje,eeting}s,

 						Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
 							    -- Linus Torvalds
Jiri Olsa June 26, 2024, 9:52 a.m. UTC | #3
On Tue, Jun 25, 2024 at 12:46:48PM +0200, Geert Uytterhoeven wrote:
> 	Hi Tony,
> 
> On Mon, 3 Jun 2024, Tony Ambardar wrote:
> > BPF kfuncs are often not directly referenced and may be inadvertently
> > removed by optimization steps during kernel builds, thus the __bpf_kfunc
> > tag mitigates against this removal by including the __used macro. However,
> > this macro alone does not prevent removal during linking, and may still
> > yield build warnings (e.g. on mips64el):
> > 
> >    LD      vmlinux
> >    BTFIDS  vmlinux
> >  WARN: resolve_btfids: unresolved symbol bpf_verify_pkcs7_signature
> >  WARN: resolve_btfids: unresolved symbol bpf_lookup_user_key
> >  WARN: resolve_btfids: unresolved symbol bpf_lookup_system_key
> >  WARN: resolve_btfids: unresolved symbol bpf_key_put
> >  WARN: resolve_btfids: unresolved symbol bpf_iter_task_next
> >  WARN: resolve_btfids: unresolved symbol bpf_iter_css_task_new
> >  WARN: resolve_btfids: unresolved symbol bpf_get_file_xattr
> >  WARN: resolve_btfids: unresolved symbol bpf_ct_insert_entry
> >  WARN: resolve_btfids: unresolved symbol bpf_cgroup_release
> >  WARN: resolve_btfids: unresolved symbol bpf_cgroup_from_id
> >  WARN: resolve_btfids: unresolved symbol bpf_cgroup_acquire
> >  WARN: resolve_btfids: unresolved symbol bpf_arena_free_pages
> >    NM      System.map
> >    SORTTAB vmlinux
> >    OBJCOPY vmlinux.32
> > 
> > Update the __bpf_kfunc tag to better guard against linker optimization by
> > including the new __retain compiler macro, which fixes the warnings above.
> > 
> > Verify the __retain macro with readelf by checking object flags for 'R':
> > 
> >  $ readelf -Wa kernel/trace/bpf_trace.o
> >  Section Headers:
> >    [Nr]  Name              Type     Address  Off  Size ES Flg Lk Inf Al
> >  ...
> >    [178] .text.bpf_key_put PROGBITS 00000000 6420 0050 00 AXR  0   0  8
> >  ...
> >  Key to Flags:
> >  ...
> >    R (retain), D (mbind), p (processor specific)
> > 
> > Link: https://lore.kernel.org/bpf/ZlmGoT9KiYLZd91S@krava/T/
> > Reported-by: kernel test robot <lkp@intel.com>
> > Closes: https://lore.kernel.org/r/202401211357.OCX9yllM-lkp@intel.com/
> > Fixes: 57e7c169cd6a ("bpf: Add __bpf_kfunc tag for marking kernel functions as kfuncs")
> > Cc: stable@vger.kernel.org # v6.6+
> > Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
> 
> Thanks for your patch, which is now commit 7bdcedd5c8fb88e7
> ("bpf: Harden __bpf_kfunc tag against linker kfunc removal") in
> v6.10-rc5.
> 
> This is causing build failures on ARM with
> CONFIG_LD_DEAD_CODE_DATA_ELIMINATION=y:
> 
>     net/core/filter.c:11859:1: error: ‘retain’ attribute ignored [-Werror=attributes]
>     11859 | {
>           | ^
>     net/core/filter.c:11872:1: error: ‘retain’ attribute ignored [-Werror=attributes]
>     11872 | {
>           | ^
>     net/core/filter.c:11885:1: error: ‘retain’ attribute ignored [-Werror=attributes]
>     11885 | {
>           | ^
>     net/core/filter.c:11906:1: error: ‘retain’ attribute ignored [-Werror=attributes]
>     11906 | {
>           | ^
>     net/core/filter.c:12092:1: error: ‘retain’ attribute ignored [-Werror=attributes]
>     12092 | {
>           | ^
>     net/core/xdp.c:713:1: error: ‘retain’ attribute ignored [-Werror=attributes]
>       713 | {
>           | ^
>     net/core/xdp.c:736:1: error: ‘retain’ attribute ignored [-Werror=attributes]
>       736 | {
>           | ^
>     net/core/xdp.c:769:1: error: ‘retain’ attribute ignored [-Werror=attributes]
>       769 | {
>           | ^
>     [...]
> 
> My compiler is arm-linux-gnueabihf-gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04).

hum, so it'd mean __has_attribute(__retain__) returns true while gcc still
ignores the retain attribute.. like in this bug which seems similar:
  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99587
but not sure how it got fixed.. any chance you can upgrade gcc and retest?

jirka

> 
> > --- a/include/linux/btf.h
> > +++ b/include/linux/btf.h
> > @@ -82,7 +82,7 @@
> >  * as to avoid issues such as the compiler inlining or eliding either a static
> >  * kfunc, or a global kfunc in an LTO build.
> >  */
> > -#define __bpf_kfunc __used noinline
> > +#define __bpf_kfunc __used __retain noinline
> > 
> > #define __bpf_kfunc_start_defs()					       \
> > 	__diag_push();							       \
> 
> Gr{oetje,eeting}s,
> 
> 						Geert
> 
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
> 
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
> 							    -- Linus Torvalds
Geert Uytterhoeven June 26, 2024, 11:40 a.m. UTC | #4
Hi Jiri,

On Wed, Jun 26, 2024 at 11:52 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> On Tue, Jun 25, 2024 at 12:46:48PM +0200, Geert Uytterhoeven wrote:
> > On Mon, 3 Jun 2024, Tony Ambardar wrote:
> > > BPF kfuncs are often not directly referenced and may be inadvertently
> > > removed by optimization steps during kernel builds, thus the __bpf_kfunc
> > > tag mitigates against this removal by including the __used macro. However,
> > > this macro alone does not prevent removal during linking, and may still
> > > yield build warnings (e.g. on mips64el):
> > >
> > >    LD      vmlinux
> > >    BTFIDS  vmlinux
> > >  WARN: resolve_btfids: unresolved symbol bpf_verify_pkcs7_signature
> > >  WARN: resolve_btfids: unresolved symbol bpf_lookup_user_key
> > >  WARN: resolve_btfids: unresolved symbol bpf_lookup_system_key
> > >  WARN: resolve_btfids: unresolved symbol bpf_key_put
> > >  WARN: resolve_btfids: unresolved symbol bpf_iter_task_next
> > >  WARN: resolve_btfids: unresolved symbol bpf_iter_css_task_new
> > >  WARN: resolve_btfids: unresolved symbol bpf_get_file_xattr
> > >  WARN: resolve_btfids: unresolved symbol bpf_ct_insert_entry
> > >  WARN: resolve_btfids: unresolved symbol bpf_cgroup_release
> > >  WARN: resolve_btfids: unresolved symbol bpf_cgroup_from_id
> > >  WARN: resolve_btfids: unresolved symbol bpf_cgroup_acquire
> > >  WARN: resolve_btfids: unresolved symbol bpf_arena_free_pages
> > >    NM      System.map
> > >    SORTTAB vmlinux
> > >    OBJCOPY vmlinux.32
> > >
> > > Update the __bpf_kfunc tag to better guard against linker optimization by
> > > including the new __retain compiler macro, which fixes the warnings above.
> > >
> > > Verify the __retain macro with readelf by checking object flags for 'R':
> > >
> > >  $ readelf -Wa kernel/trace/bpf_trace.o
> > >  Section Headers:
> > >    [Nr]  Name              Type     Address  Off  Size ES Flg Lk Inf Al
> > >  ...
> > >    [178] .text.bpf_key_put PROGBITS 00000000 6420 0050 00 AXR  0   0  8
> > >  ...
> > >  Key to Flags:
> > >  ...
> > >    R (retain), D (mbind), p (processor specific)
> > >
> > > Link: https://lore.kernel.org/bpf/ZlmGoT9KiYLZd91S@krava/T/
> > > Reported-by: kernel test robot <lkp@intel.com>
> > > Closes: https://lore.kernel.org/r/202401211357.OCX9yllM-lkp@intel.com/
> > > Fixes: 57e7c169cd6a ("bpf: Add __bpf_kfunc tag for marking kernel functions as kfuncs")
> > > Cc: stable@vger.kernel.org # v6.6+
> > > Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
> >
> > Thanks for your patch, which is now commit 7bdcedd5c8fb88e7
> > ("bpf: Harden __bpf_kfunc tag against linker kfunc removal") in
> > v6.10-rc5.
> >
> > This is causing build failures on ARM with
> > CONFIG_LD_DEAD_CODE_DATA_ELIMINATION=y:
> >
> >     net/core/filter.c:11859:1: error: ‘retain’ attribute ignored [-Werror=attributes]
> >     11859 | {
> >           | ^
> >     net/core/filter.c:11872:1: error: ‘retain’ attribute ignored [-Werror=attributes]
> >     11872 | {
> >           | ^
> >     net/core/filter.c:11885:1: error: ‘retain’ attribute ignored [-Werror=attributes]
> >     11885 | {
> >           | ^
> >     net/core/filter.c:11906:1: error: ‘retain’ attribute ignored [-Werror=attributes]
> >     11906 | {
> >           | ^
> >     net/core/filter.c:12092:1: error: ‘retain’ attribute ignored [-Werror=attributes]
> >     12092 | {
> >           | ^
> >     net/core/xdp.c:713:1: error: ‘retain’ attribute ignored [-Werror=attributes]
> >       713 | {
> >           | ^
> >     net/core/xdp.c:736:1: error: ‘retain’ attribute ignored [-Werror=attributes]
> >       736 | {
> >           | ^
> >     net/core/xdp.c:769:1: error: ‘retain’ attribute ignored [-Werror=attributes]
> >       769 | {
> >           | ^
> >     [...]
> >
> > My compiler is arm-linux-gnueabihf-gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04).
>
> hum, so it'd mean __has_attribute(__retain__) returns true while gcc still
> ignores the retain attribute.. like in this bug which seems similar:
>   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99587
> but not sure how it got fixed.. any chance you can upgrade gcc and retest?

Indeed, __has_attribute(__retain__) returns true, while the attribute
is not supported.

My test program:

cat > /tmp/a.c <<EOF
#if __has_attribute(__retain__)
#warning __retain__ OK
#else
#warning No __retain__
#endif

int x __attribute__((__retain__));
EOF

$ arm-linux-gnueabihf-gcc-11 -c /tmp/a.c # gcc version 11.4.0 (Ubuntu
11.4.0-1ubuntu1~22.04))

/tmp/a.c:2:2: warning: #warning __retain__ OK [-Wcpp]
    2 | #warning __retain__ OK
      |  ^~~~~~~
/tmp/a.c:7:1: warning: ‘retain’ attribute ignored [-Wattributes]
    7 | int x __attribute__((__retain__));
      | ^~~

Oops

$ arm-linux-gnueabihf-gcc-12 -c /tmp/a.c # gcc version 12.3.0 (Ubuntu
12.3.0-1ubuntu1~22.04)
/tmp/a.c:2:2: warning: #warning __retain__ OK [-Wcpp]
    2 | #warning __retain__ OK
      |  ^~~~~~~

Fixed

It works fine with the native gcc-11:

$ gcc-11 -c /tmp/a.c # gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
/tmp/a.c:2:2: warning: #warning __retain__ OK [-Wcpp]
    2 | #warning __retain__ OK
      |  ^~~~~~~

I gave it a try on all installed gcc-11 compilers.

/usr/bin/aarch64-linux-gnu-gcc-11
/usr/bin/alpha-linux-gnu-gcc-11
/usr/bin/arm-linux-gnueabi-gcc-11
/usr/bin/arm-linux-gnueabihf-gcc-11
/usr/bin/hppa64-linux-gnu-gcc-11
/usr/bin/hppa-linux-gnu-gcc-11
/usr/bin/m68k-linux-gnu-gcc-11
/usr/bin/powerpc64le-linux-gnu-gcc-11
/usr/bin/powerpc64-linux-gnu-gcc-11
/usr/bin/powerpc-linux-gnu-gcc-11
/usr/bin/riscv64-linux-gnu-gcc-11
/usr/bin/s390x-linux-gnu-gcc-11
/usr/bin/sh4-linux-gnu-gcc-11
/usr/bin/sparc64-linux-gnu-gcc-11
/usr/bin/x86_64-linux-gnu-gcc-11
/usr/bin/x86_64-linux-gnux32-gcc-11

All of them failed (incl. x32), except for the native x86_64-linux-gnu-gcc-11.

It works fine with all installed gcc-12 compilers
(arm-linux-gnueabihf-gcc-12, m68k-linux-gnu-gcc-12, x86_64-linux-gnu-gcc-12).

With gcc-9, the absence of __retain__ is detected correctly.

Gr{oetje,eeting}s,

                        Geert
diff mbox series

Patch

diff --git a/include/linux/btf.h b/include/linux/btf.h
index f9e56fd12a9f..7c3e40c3295e 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -82,7 +82,7 @@ 
  * as to avoid issues such as the compiler inlining or eliding either a static
  * kfunc, or a global kfunc in an LTO build.
  */
-#define __bpf_kfunc __used noinline
+#define __bpf_kfunc __used __retain noinline
 
 #define __bpf_kfunc_start_defs()					       \
 	__diag_push();							       \