Re: [PATCH v2] selftests/bpf: workarounds for GCC BPF build

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Monday, January 6th, 2025 at 10:59 AM, Eduard Zingerman <eddyz87@xxxxxxxxx> wrote:

> 
> 
> On Mon, 2025-01-06 at 18:54 +0000, Ihor Solodrai wrote:
> 
> > Various compilation errors happen when BPF programs in selftests/bpf
> > are built with GCC BPF. For more details see the discussion at [1].
> > 
> > The changes only affect test_progs-bpf_gcc, which is built only if
> > BPF_GCC is set:
> > * Pass -std=gnu17 to gcc in order to avoid errors on bool types
> > declarations in vmlinux.h
> > * Pass -fno-strict-aliasing for tests that trigger uninitialized
> > variable warning on BPF_RAW_INSNS [2]
> > 
> > [1] https://lore.kernel.org/bpf/EYcXjcKDCJY7Yb0GGtAAb7nLKPEvrgWdvWpuNzXm2qi6rYMZDixKv5KwfVVMBq17V55xyC-A1wIjrqG3aw-Imqudo9q9X7D7nLU2gWgbN0w=@pm.me/
> > [2] https://lore.kernel.org/bpf/87pll3c8bt.fsf@xxxxxxxxxx/
> > 
> > CC: Jose E. Marchesi jose.marchesi@xxxxxxxxxx
> > Signed-off-by: Ihor Solodrai ihor.solodrai@xxxxx
> > 
> > ---
> > 
> > v1: https://lore.kernel.org/bpf/20250104001751.1869849-1-ihor.solodrai@xxxxx/
> > 
> > tools/testing/selftests/bpf/Makefile | 6 +++++-
> > 1 file changed, 5 insertions(+), 1 deletion(-)
> > 
> > diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
> > index eb4d21651aa7..b043791fe6db 100644
> > --- a/tools/testing/selftests/bpf/Makefile
> > +++ b/tools/testing/selftests/bpf/Makefile
> > @@ -69,6 +69,10 @@ progs/timer_crash.c-CFLAGS := -fno-strict-aliasing
> > progs/test_global_func9.c-CFLAGS := -fno-strict-aliasing
> > progs/verifier_nocsr.c-CFLAGS := -fno-strict-aliasing
> > 
> > +# Uninitialized variable warning on BPF_RAW_INSN
> > +progs/verifier_bpf_fastcall.c-CFLAGS := -fno-strict-aliasing
> > +progs/verifier_search_pruning.c-CFLAGS := -fno-strict-aliasing
> 
> 
> Specifying -fno-strict-aliasing for a sub-set of tests is not convenient,
> as this list would have to be extended each time __imm_insn macro is used.
> Either this flag should be used for test_progs compilation as a whole,
> or the macro should be updated to use union as it was suggested previously.
> Personally, I don't like the aliasing rules and would prefer -fno-strict-aliasing,
> but changing macro is a simple and non-intrusive update, so I think it's a better option.

__imm_insn is not the only thing breaking the aliasing rules. An
example from bind* tests:

    In file included from progs/bind4_prog.c:15:
    progs/bind4_prog.c: In function ‘bind_v4_prog’:
    progs/bind_prog.h:9:36: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
        9 |         (((volatile __u16 *)&(src))[w] << 16 * w)
          |                                    ^
    progs/bind4_prog.c:138:21: note: in expansion of macro ‘load_word’
      138 |         user_ip4 |= load_word(ctx->user_ip4, 0, sizeof(user_ip4));
          |                     ^~~~~~~~~
    In file included from progs/bind6_prog.c:15:
    progs/bind6_prog.c: In function ‘bind_v6_prog’:
    progs/bind_prog.h:9:36: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
        9 |         (((volatile __u16 *)&(src))[w] << 16 * w)
          |                                    ^
    progs/bind6_prog.c:151:29: note: in expansion of macro ‘load_word’
      151 |                 user_ip6 |= load_word(ctx->user_ip6[i], 0, sizeof(user_ip6));
          |                             ^~~~~~~~~

And also:

    $ grep -rl --include="[Mm]akefile*" 'fno-strict-aliasing' | sort
    arch/arm64/kernel/vdso32/Makefile
    arch/loongarch/vdso/Makefile
    arch/mips/vdso/Makefile
    arch/parisc/boot/compressed/Makefile
    arch/powerpc/boot/Makefile
    arch/s390/purgatory/Makefile
    arch/x86/boot/compressed/Makefile
    arch/x86/Makefile
    drivers/firmware/efi/libstub/Makefile
    Makefile
    selftests/bpf/Makefile
    selftests/kvm/Makefile
    selftests/net/tcp_ao/Makefile
    tools/scripts/Makefile.include
    tools/testing/selftests/bpf/Makefile
    tools/testing/selftests/kvm/Makefile
    tools/testing/selftests/net/tcp_ao/Makefile
    tools/testing/vsock/Makefile
    tools/virtio/Makefile

So yeah, just setting this flag for all tests makes sense.

I was wondering how clang handles this, and it turns out
-fno-strict-aliasing is true by default in clang [1]:

    -fno-strict-aliasing    Disable optimizations based on strict aliasing rules (default)

[1]: https://clang.llvm.org/docs/UsersManual.html

> 
> > # Some utility functions use LLVM libraries
> > jit_disasm_helpers.c-CFLAGS = $(LLVM_CFLAGS)
> > 
> > @@ -507,7 +511,7 @@ endef
> > # Build BPF object using GCC
> > define GCC_BPF_BUILD_RULE
> > $(call msg,GCC-BPF,$4,$2)
> > - $(Q)$(BPF_GCC) $3 -DBPF_NO_PRESERVE_ACCESS_INDEX -Wno-attributes -O2 -c $1 -o $2
> > + $(Q)$(BPF_GCC) $3 -DBPF_NO_PRESERVE_ACCESS_INDEX -Wno-attributes -O2 -std=gnu17 -c $1 -o $2
> > endef
> > 
> > SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
> 
> 






[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux