diff mbox series

Is tools/testing/selftests/bpf/ maintained?

Message ID adfab6e8-b1de-4efc-a9ef-84e219c91833@I-love.SAKURA.ne.jp (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series Is tools/testing/selftests/bpf/ maintained? | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Tetsuo Handa Oct. 12, 2023, 1:39 p.m. UTC
Hello.

I'm having problem with finding BPF LSM examples that work.
I tried building tools/testing/selftests/bpf/progs/lsm.c and
tools/testing/selftests/bpf/prog_tests/test_lsm.c explained at
https://docs.kernel.org/bpf/prog_lsm.html , but got a lot of errors.

----------------------------------------
root@ubuntu:/usr/src# git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux.git
Cloning into 'linux'...
remote: Total 9723739 (delta 8227084), reused 9723739 (delta 8227084)
Receiving objects: 100% (9723739/9723739), 1.81 GiB | 4.04 MiB/s, done.
Resolving deltas: 100% (8227084/8227084), done.
Checking objects: 100% (33554432/33554432), done.
Updating files: 100% (81759/81759), done.
root@ubuntu:/usr/src# cd linux
root@ubuntu:/usr/src/linux# git describe
v6.6-rc5-72-g401644852d0b
root@ubuntu:/usr/src/linux# make -s headers
root@ubuntu:/usr/src/linux# make -sC tools/testing/selftests/bpf/
  MKDIR    libbpf
Warning: Kernel ABI header at 'tools/include/uapi/linux/if_xdp.h' differs from latest version at 'include/uapi/linux/if_xdp.h'
  TEST-HDR [test_progs] tests.h
  EXT-OBJ  [test_progs] testing_helpers.o
  EXT-OBJ  [test_progs] cap_helpers.o
  EXT-OBJ  [test_progs] unpriv_helpers.o
  BINARY   test_verifier
  BINARY   test_tag
  MKDIR    bpftool

  GEN      vmlinux.h
  CLNG-BPF [test_maps] async_stack_depth.bpf.o
progs/async_stack_depth.c:8:19: error: field has incomplete type 'struct bpf_timer'
        struct bpf_timer timer;
                         ^
/usr/src/linux/tools/testing/selftests/bpf/tools/include/bpf/bpf_helper_defs.h:41:8: note: forward declaration of 'struct bpf_timer'
struct bpf_timer;
       ^
1 error generated.
make: *** [Makefile:598: /usr/src/linux/tools/testing/selftests/bpf/async_stack_depth.bpf.o] Error 1
----------------------------------------

To fix these errors, something like the following
(this seems to be a fraction) is needed. What am I missing?

----------------------------------------
----------------------------------------

Comments

Eduard Zingerman Oct. 12, 2023, 4:34 p.m. UTC | #1
On Thu, 2023-10-12 at 22:39 +0900, Tetsuo Handa wrote:
> Hello.
> 
> I'm having problem with finding BPF LSM examples that work.
> I tried building tools/testing/selftests/bpf/progs/lsm.c and
> tools/testing/selftests/bpf/prog_tests/test_lsm.c explained at
> https://docs.kernel.org/bpf/prog_lsm.html , but got a lot of errors.

Hello,

> Is tools/testing/selftests/bpf/ maintained?

It pretty much is, build it every day :)
And we have a CI too: https://github.com/kernel-patches/bpf .

I tried setting up a minimal Debian chroot to showcase the build
and came up with the following list of commands:

# Use trixie to get llvm-16
sudo /usr/sbin/debootstrap --variant=buildd --arch=amd64 trixie trixie-chroot/ http://deb.debian.org/debian
# don't forget to umount with 'umount -R ...'
sudo mount --rbind /dev/pts trixie-chroot/dev/pts
sudo mount -t proc proc trixie-chroot/proc
sudo chroot trixie-chroot

# The reset of commands are from chroot itself, first as root
apt install build-essential llvm clang lld bc flex bison pahole git \
    libelf-dev libssl-dev docutils-common rsync
# Note: you might want to build pahole from source
useradd -d /home/eddy -s /bin/bash eddy
mkdir /home/eddy
chown eddy /home/eddy
su eddy

# Now as a user 'eddy':
cd /home/eddy
git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux/
./scripts/kconfig/merge_config.sh tools/testing/selftests/bpf/config tools/testing/selftests/bpf/config.x86_64
# Note: kernel build is mandatory, as vmlinux.h is constructed from DWARF in ./vmlinux
make -j14
make -j14 headers
make -j14 -C tools/testing/selftests/bpf/

Hope this helps,
Eduard
Andrii Nakryiko Oct. 12, 2023, 4:35 p.m. UTC | #2
On Thu, Oct 12, 2023 at 6:40 AM Tetsuo Handa
<penguin-kernel@i-love.sakura.ne.jp> wrote:
>
> Hello.
>
> I'm having problem with finding BPF LSM examples that work.
> I tried building tools/testing/selftests/bpf/progs/lsm.c and
> tools/testing/selftests/bpf/prog_tests/test_lsm.c explained at
> https://docs.kernel.org/bpf/prog_lsm.html , but got a lot of errors.

Make sure you a) have necessary kernel configs set up (see [0], there
are also arch-specific configs) and b) build kernel before building
selftests, because selftests generate vmlinux.h header with all kernel
types based on latest built kernel image.

  [0] https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/config

>
> ----------------------------------------
> root@ubuntu:/usr/src# git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux.git
> Cloning into 'linux'...
> remote: Total 9723739 (delta 8227084), reused 9723739 (delta 8227084)
> Receiving objects: 100% (9723739/9723739), 1.81 GiB | 4.04 MiB/s, done.
> Resolving deltas: 100% (8227084/8227084), done.
> Checking objects: 100% (33554432/33554432), done.
> Updating files: 100% (81759/81759), done.
> root@ubuntu:/usr/src# cd linux
> root@ubuntu:/usr/src/linux# git describe
> v6.6-rc5-72-g401644852d0b
> root@ubuntu:/usr/src/linux# make -s headers
> root@ubuntu:/usr/src/linux# make -sC tools/testing/selftests/bpf/
>   MKDIR    libbpf
> Warning: Kernel ABI header at 'tools/include/uapi/linux/if_xdp.h' differs from latest version at 'include/uapi/linux/if_xdp.h'
>   TEST-HDR [test_progs] tests.h
>   EXT-OBJ  [test_progs] testing_helpers.o
>   EXT-OBJ  [test_progs] cap_helpers.o
>   EXT-OBJ  [test_progs] unpriv_helpers.o
>   BINARY   test_verifier
>   BINARY   test_tag
>   MKDIR    bpftool
>
>   GEN      vmlinux.h
>   CLNG-BPF [test_maps] async_stack_depth.bpf.o
> progs/async_stack_depth.c:8:19: error: field has incomplete type 'struct bpf_timer'
>         struct bpf_timer timer;
>                          ^
> /usr/src/linux/tools/testing/selftests/bpf/tools/include/bpf/bpf_helper_defs.h:41:8: note: forward declaration of 'struct bpf_timer'
> struct bpf_timer;
>        ^
> 1 error generated.
> make: *** [Makefile:598: /usr/src/linux/tools/testing/selftests/bpf/async_stack_depth.bpf.o] Error 1
> ----------------------------------------
>
> To fix these errors, something like the following
> (this seems to be a fraction) is needed. What am I missing?
>

kernel image used for vmlinux.h generation is probably too old

[...]
Tetsuo Handa Oct. 13, 2023, 1:15 p.m. UTC | #3
Thank you for showing complete command line.

On 2023/10/13 1:34, Eduard Zingerman wrote:
> # Note: kernel build is mandatory, as vmlinux.h is constructed from DWARF in ./vmlinux

is what I was missing. Makefile rules should explicitly describe dependency on vmlinux ,
or at least emit message to teach users about the need to build vmlinux ?

But I still get error. I'm using Ubuntu 22.04.3 LTS.

----------------------------------------
root@ubuntu:/usr/src/linux# make -C tools/testing/selftests/bpf/
make: Entering directory '/usr/src/linux/tools/testing/selftests/bpf'
  CLNG-BPF [test_maps] verifier_and.bpf.o
progs/verifier_and.c:58:16: error: invalid operand for instruction
        asm volatile ("                                 \
                      ^
<inline asm>:1:184: note: instantiated into assembly here
                                                        r1 = 0;                                                 *(u64*)(r10 - 8) = r1;                                  r2 = r10;                                               r2 += -8;                                               r1 = map_hash_48b ll;                           call 1;                         if r0 == 0 goto l0_1;                                   r1 = *(u32*)(r0 + 0);                                   r9 = 1;                                                 w1 %= 2;                                                w1 += 1;                                                w9 &= w1;                                               w9 += 1;                                                w9 >>= 1;                                               w3 = 1;                                                 w3 -= w9;                                               w3 *= 0x10000000;                                       r0 += r3;                                               *(u32*)(r0 + 0) = r3;                           l0_1:   r0 = r0;                                                exit;                                         
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
1 error generated.
make: *** [Makefile:598: /usr/src/linux/tools/testing/selftests/bpf/verifier_and.bpf.o] Error 1
make: Leaving directory '/usr/src/linux/tools/testing/selftests/bpf'
----------------------------------------
Eduard Zingerman Oct. 13, 2023, 1:25 p.m. UTC | #4
On Fri, 2023-10-13 at 22:15 +0900, Tetsuo Handa wrote:
> Thank you for showing complete command line.
> 
> On 2023/10/13 1:34, Eduard Zingerman wrote:
> > # Note: kernel build is mandatory, as vmlinux.h is constructed from DWARF in ./vmlinux
> 
> is what I was missing. Makefile rules should explicitly describe dependency on vmlinux ,
> or at least emit message to teach users about the need to build vmlinux ?

Yes, that would be nice.

> But I still get error. I'm using Ubuntu 22.04.3 LTS.

I think you are using clang-14, which does not like u32 instructions.
At least I get the same error message as you with clang-14.
If so, please try using clang-16 instead.

> 
> ----------------------------------------
> root@ubuntu:/usr/src/linux# make -C tools/testing/selftests/bpf/
> make: Entering directory '/usr/src/linux/tools/testing/selftests/bpf'
>   CLNG-BPF [test_maps] verifier_and.bpf.o
> progs/verifier_and.c:58:16: error: invalid operand for instruction
>         asm volatile ("                                 \
>                       ^
> <inline asm>:1:184: note: instantiated into assembly here
>                                                         r1 = 0;                                                 *(u64*)(r10 - 8) = r1;                                  r2 = r10;                                               r2 += -8;                                               r1 = map_hash_48b ll;                           call 1;                         if r0 == 0 goto l0_1;                                   r1 = *(u32*)(r0 + 0);                                   r9 = 1;                                                 w1 %= 2;                                                w1 += 1;                                                w9 &= w1;                                               w9 += 1;                                                w9 >>= 1;                                               w3 = 1;                                                 w3 -= w9;                                               w3 *= 0x10000000;                                       r0 += r3;                                               *(u32*)(r0 + 0) = r3;                           l0_1:   r0 = r0;                                                exit;                                         
>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ^
> 1 error generated.
> make: *** [Makefile:598: /usr/src/linux/tools/testing/selftests/bpf/verifier_and.bpf.o] Error 1
> make: Leaving directory '/usr/src/linux/tools/testing/selftests/bpf'
> ----------------------------------------
>
Tetsuo Handa Oct. 13, 2023, 2:17 p.m. UTC | #5
On 2023/10/13 22:25, Eduard Zingerman wrote:
> I think you are using clang-14, which does not like u32 instructions.
> At least I get the same error message as you with clang-14.
> If so, please try using clang-16 instead.

Yes, I did something like below and build succeeded. Thank you.

wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
./llvm.sh 16
apt remove clang
ln -s /usr/bin/clang-16 /usr/bin/clang
Eduard Zingerman Oct. 13, 2023, 2:23 p.m. UTC | #6
On Fri, 2023-10-13 at 23:17 +0900, Tetsuo Handa wrote:
> Yes, I did something like below and build succeeded. Thank you.

Great :)

> wget https://apt.llvm.org/llvm.sh
> chmod +x llvm.sh
> ./llvm.sh 16
> apt remove clang
> ln -s /usr/bin/clang-16 /usr/bin/clang

I didn't know LLVM has a script to deal with repos, thanks.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/progs/async_stack_depth.c b/tools/testing/selftests/bpf/progs/async_stack_depth.c
index 3517c0e01206..0318229d8fb2 100644
--- a/tools/testing/selftests/bpf/progs/async_stack_depth.c
+++ b/tools/testing/selftests/bpf/progs/async_stack_depth.c
@@ -4,6 +4,11 @@ 
 
 #include "bpf_misc.h"
 
+struct bpf_timer {
+	__u64 :64;
+	__u64 :64;
+} __attribute__((aligned(8)));
+
 struct hmap_elem {
 	struct bpf_timer timer;
 };
diff --git a/tools/testing/selftests/bpf/progs/cb_refs.c b/tools/testing/selftests/bpf/progs/cb_refs.c
index 76d661b20e87..d1fb43346dc1 100644
--- a/tools/testing/selftests/bpf/progs/cb_refs.c
+++ b/tools/testing/selftests/bpf/progs/cb_refs.c
@@ -2,6 +2,9 @@ 
 #include <vmlinux.h>
 #include <bpf/bpf_tracing.h>
 #include <bpf/bpf_helpers.h>
+struct prog_test_member1 {
+	int a;
+};
 #include "../bpf_testmod/bpf_testmod_kfunc.h"
 
 struct map_value {
diff --git a/tools/testing/selftests/bpf/progs/cgrp_ls_attach_cgroup.c b/tools/testing/selftests/bpf/progs/cgrp_ls_attach_cgroup.c
index 8aeba1b75c83..5de35c0e08cc 100644
--- a/tools/testing/selftests/bpf/progs/cgrp_ls_attach_cgroup.c
+++ b/tools/testing/selftests/bpf/progs/cgrp_ls_attach_cgroup.c
@@ -14,7 +14,7 @@  struct socket_cookie {
 };
 
 struct {
-	__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+	__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
 	__uint(map_flags, BPF_F_NO_PREALLOC);
 	__type(key, int);
 	__type(value, struct socket_cookie);
diff --git a/tools/testing/selftests/bpf/progs/cgrp_ls_negative.c b/tools/testing/selftests/bpf/progs/cgrp_ls_negative.c
index d41f90e2ab64..21043a18b67d 100644
--- a/tools/testing/selftests/bpf/progs/cgrp_ls_negative.c
+++ b/tools/testing/selftests/bpf/progs/cgrp_ls_negative.c
@@ -8,7 +8,7 @@ 
 char _license[] SEC("license") = "GPL";
 
 struct {
-	__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+	__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
 	__uint(map_flags, BPF_F_NO_PREALLOC);
 	__type(key, int);
 	__type(value, long);
diff --git a/tools/testing/selftests/bpf/progs/cgrp_ls_recursion.c b/tools/testing/selftests/bpf/progs/cgrp_ls_recursion.c
index a043d8fefdac..cc175f004266 100644
--- a/tools/testing/selftests/bpf/progs/cgrp_ls_recursion.c
+++ b/tools/testing/selftests/bpf/progs/cgrp_ls_recursion.c
@@ -8,14 +8,14 @@ 
 char _license[] SEC("license") = "GPL";
 
 struct {
-	__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+	__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
 	__uint(map_flags, BPF_F_NO_PREALLOC);
 	__type(key, int);
 	__type(value, long);
 } map_a SEC(".maps");
 
 struct {
-	__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+	__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
 	__uint(map_flags, BPF_F_NO_PREALLOC);
 	__type(key, int);
 	__type(value, long);
diff --git a/tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c b/tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c
index 4c7844e1dbfa..0130e2e6b3d7 100644
--- a/tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c
+++ b/tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c
@@ -9,7 +9,7 @@ 
 char _license[] SEC("license") = "GPL";
 
 struct {
-	__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+	__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
 	__uint(map_flags, BPF_F_NO_PREALLOC);
 	__type(key, int);
 	__type(value, long);
diff --git a/tools/testing/selftests/bpf/progs/cgrp_ls_tp_btf.c b/tools/testing/selftests/bpf/progs/cgrp_ls_tp_btf.c
index 9ebb8e2fe541..e47c88c8790c 100644
--- a/tools/testing/selftests/bpf/progs/cgrp_ls_tp_btf.c
+++ b/tools/testing/selftests/bpf/progs/cgrp_ls_tp_btf.c
@@ -8,14 +8,14 @@ 
 char _license[] SEC("license") = "GPL";
 
 struct {
-	__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+	__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
 	__uint(map_flags, BPF_F_NO_PREALLOC);
 	__type(key, int);
 	__type(value, long);
 } map_a SEC(".maps");
 
 struct {
-	__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+	__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
 	__uint(map_flags, BPF_F_NO_PREALLOC);
 	__type(key, int);
 	__type(value, long);
diff --git a/tools/testing/selftests/bpf/progs/dummy_st_ops_fail.c b/tools/testing/selftests/bpf/progs/dummy_st_ops_fail.c
index 0bf969a0b5ed..f681d6f15c43 100644
--- a/tools/testing/selftests/bpf/progs/dummy_st_ops_fail.c
+++ b/tools/testing/selftests/bpf/progs/dummy_st_ops_fail.c
@@ -3,6 +3,18 @@ 
 
 #include "vmlinux.h"
 #include <bpf/bpf_helpers.h>
+
+struct bpf_dummy_ops_state {
+	int val;
+};
+
+struct bpf_dummy_ops {
+	int (*test_1)(struct bpf_dummy_ops_state *cb);
+	int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
+		      char a3, unsigned long a4);
+	int (*test_sleepable)(struct bpf_dummy_ops_state *cb);
+};
+
 #include <bpf/bpf_tracing.h>
 
 #include "bpf_misc.h"
diff --git a/tools/testing/selftests/bpf/progs/map_kptr.c b/tools/testing/selftests/bpf/progs/map_kptr.c
index da30f0d59364..b0db7b893461 100644
--- a/tools/testing/selftests/bpf/progs/map_kptr.c
+++ b/tools/testing/selftests/bpf/progs/map_kptr.c
@@ -68,7 +68,7 @@  struct lru_pcpu_hash_map {
 } lru_pcpu_hash_map SEC(".maps");
 
 struct cgrp_ls_map {
-	__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
+	__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
 	__uint(map_flags, BPF_F_NO_PREALLOC);
 	__type(key, int);
 	__type(value, struct map_value);