Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf, mips: Fix build errors about __NR_bpf undeclared

Add the __NR_bpf definitions to fix the following build errors for mips:

$ cd tools/bpf/bpftool
$ make
[...]
bpf.c:54:4: error: #error __NR_bpf not defined. libbpf does not support your arch.
# error __NR_bpf not defined. libbpf does not support your arch.
^~~~~
bpf.c: In function ‘sys_bpf’:
bpf.c:66:17: error: ‘__NR_bpf’ undeclared (first use in this function); did you mean ‘__NR_brk’?
return syscall(__NR_bpf, cmd, attr, size);
^~~~~~~~
__NR_brk
[...]
In file included from gen_loader.c:15:0:
skel_internal.h: In function ‘skel_sys_bpf’:
skel_internal.h:53:17: error: ‘__NR_bpf’ undeclared (first use in this function); did you mean ‘__NR_brk’?
return syscall(__NR_bpf, cmd, attr, size);
^~~~~~~~
__NR_brk

We can see the following generated definitions:

$ grep -r "#define __NR_bpf" arch/mips
arch/mips/include/generated/uapi/asm/unistd_o32.h:#define __NR_bpf (__NR_Linux + 355)
arch/mips/include/generated/uapi/asm/unistd_n64.h:#define __NR_bpf (__NR_Linux + 315)
arch/mips/include/generated/uapi/asm/unistd_n32.h:#define __NR_bpf (__NR_Linux + 319)

The __NR_Linux is defined in arch/mips/include/uapi/asm/unistd.h:

$ grep -r "#define __NR_Linux" arch/mips
arch/mips/include/uapi/asm/unistd.h:#define __NR_Linux 4000
arch/mips/include/uapi/asm/unistd.h:#define __NR_Linux 5000
arch/mips/include/uapi/asm/unistd.h:#define __NR_Linux 6000

That is to say, __NR_bpf is:

4000 + 355 = 4355 for mips o32,
6000 + 319 = 6319 for mips n32,
5000 + 315 = 5315 for mips n64.

So use the GCC pre-defined macro _ABIO32, _ABIN32 and _ABI64 [1] to define
the corresponding __NR_bpf.

This patch is similar with commit bad1926dd2f6 ("bpf, s390: fix build for
libbpf and selftest suite").

[1] https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/config/mips/mips.h#l549

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/1637804167-8323-1-git-send-email-yangtiezhu@loongson.cn

authored by

Tiezhu Yang and committed by
Daniel Borkmann
e32cb12f 8f6f41f3

+22
+6
tools/build/feature/test-bpf.c
··· 14 14 # define __NR_bpf 349 15 15 # elif defined(__s390__) 16 16 # define __NR_bpf 351 17 + # elif defined(__mips__) && defined(_ABIO32) 18 + # define __NR_bpf 4355 19 + # elif defined(__mips__) && defined(_ABIN32) 20 + # define __NR_bpf 6319 21 + # elif defined(__mips__) && defined(_ABI64) 22 + # define __NR_bpf 5315 17 23 # else 18 24 # error __NR_bpf not defined. libbpf does not support your arch. 19 25 # endif
+6
tools/lib/bpf/bpf.c
··· 50 50 # define __NR_bpf 351 51 51 # elif defined(__arc__) 52 52 # define __NR_bpf 280 53 + # elif defined(__mips__) && defined(_ABIO32) 54 + # define __NR_bpf 4355 55 + # elif defined(__mips__) && defined(_ABIN32) 56 + # define __NR_bpf 6319 57 + # elif defined(__mips__) && defined(_ABI64) 58 + # define __NR_bpf 5315 53 59 # else 54 60 # error __NR_bpf not defined. libbpf does not support your arch. 55 61 # endif
+10
tools/lib/bpf/skel_internal.h
··· 7 7 #include <sys/syscall.h> 8 8 #include <sys/mman.h> 9 9 10 + #ifndef __NR_bpf 11 + # if defined(__mips__) && defined(_ABIO32) 12 + # define __NR_bpf 4355 13 + # elif defined(__mips__) && defined(_ABIN32) 14 + # define __NR_bpf 6319 15 + # elif defined(__mips__) && defined(_ABI64) 16 + # define __NR_bpf 5315 17 + # endif 18 + #endif 19 + 10 20 /* This file is a base header for auto-generated *.lskel.h files. 11 21 * Its contents will change and may become part of auto-generation in the future. 12 22 *