Skip to contentSkip to conversation

selftests/bpf: remove ARRAY_SIZE defines from tests

PATCH In review Shuah Khan 3 participants 10 messages Dec 10, 2021 – Dec 17, 2021

Subsystems (5)
lore
Share & tools
lore

Shuah Khan

author maintainer
#0 lore raw

PATCH 1/1 selftests/bpf: remove ARRAY_SIZE defines from tests

ARRAY_SIZE is defined in multiple test files. Remove the definitions and include header file for the define instead.

Remove ARRAY_SIZE define and add include bpf_util.h to bring in the define.

---
 tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +----
 tools/testing/selftests/bpf/progs/profiler.inc.h      | 5 +----
 tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +----
 tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 +---
 tools/testing/selftests/bpf/progs/test_sysctl_prog.c  | 5 +----
 5 files changed, 5 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -4
index 1d8918dfbd3f..7a5ebd330689 100644
@@ -5,6 +5,7 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 #include <bpf/bpf_core_read.h>
+#include <bpf/bpf_util.h>
 
 #include <errno.h>
 
@@ -23,10 +24,6 @@ bool skip = false;
 #define BADPTR			0
 #endif
 
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
-#endif
-
 struct {
 	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
 	__uint(max_entries, 1);
tools/testing/selftests/bpf/progs/profiler.inc.h +1 -4
index 4896fdf816f7..aad30994ecd7 100644
@@ -4,6 +4,7 @@
 #include <bpf/bpf_core_read.h>
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
+#include <bpf/bpf_util.h>
 
 #include "profiler.h"
 
@@ -132,10 +133,6 @@ struct {
 	__uint(max_entries, 16);
 } disallowed_exec_inodes SEC(".maps");
 
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
-#endif
-
 static INLINE bool IS_ERR(const void* ptr)
 {
 	return IS_ERR_VALUE((unsigned long)ptr);
tools/testing/selftests/bpf/progs/test_sysctl_loop1.c +1 -4
index 553a282d816a..c7c512e0af79 100644
@@ -8,10 +8,7 @@
 #include <linux/bpf.h>
 
 #include <bpf/bpf_helpers.h>
-
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-#endif
+#include <bpf/bpf_util.h>
 
 /* tcp_mem sysctl has only 3 ints, but this test is doing TCP_MEM_LOOPS */
 #define TCP_MEM_LOOPS 28  /* because 30 doesn't fit into 512 bytes of stack */
tools/testing/selftests/bpf/progs/test_sysctl_loop2.c +1 -3
index 2b64bc563a12..57cda15d0032 100644
@@ -8,10 +8,8 @@
 #include <linux/bpf.h>
 
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_util.h>
 
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-#endif
 
 /* tcp_mem sysctl has only 3 ints, but this test is doing TCP_MEM_LOOPS */
 #define TCP_MEM_LOOPS 20  /* because 30 doesn't fit into 512 bytes of stack */
tools/testing/selftests/bpf/progs/test_sysctl_prog.c +1 -4
index 5489823c83fc..6047c39eb457 100644
@@ -8,6 +8,7 @@
 #include <linux/bpf.h>
 
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_util.h>
 
 /* Max supported length of a string with unsigned long in base 10 (pow2 - 1). */
 #define MAX_ULONG_STR_LEN 0xF
@@ -15,10 +16,6 @@
 /* Max supported length of sysctl value string (pow2). */
 #define MAX_VALUE_STR_LEN 0x40
 
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-#endif
-
 const char tcp_mem_name[] = "net/ipv4/tcp_mem";
 static __always_inline int is_tcp_mem(struct bpf_sysctl *ctx)
 {
Signature · 1 line
2.32.0
#1 lore raw
Quoted 21 lines · Shuah Khan

ARRAY_SIZE is defined in multiple test files. Remove the definitions and include header file for the define instead.

Remove ARRAY_SIZE define and add include bpf_util.h to bring in the define.

---
 tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +----
 tools/testing/selftests/bpf/progs/profiler.inc.h      | 5 +----
 tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +----
 tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 +---
 tools/testing/selftests/bpf/progs/test_sysctl_prog.c  | 5 +----
 5 files changed, 5 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -0
index 1d8918dfbd3f..7a5ebd330689 100644

tools/testing/selftests/bpf/progs/netif_receive_skb.c line 8

Quoted 4 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -0
@@ -5,6 +5,7 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 #include <bpf/bpf_core_read.h>
+#include <bpf/bpf_util.h>

It doesn't look like you've built it.

progs/test_sysctl_prog.c:11:10: fatal error: 'bpf/bpf_util.h' file not found

#include <bpf/bpf_util.h>
         ^~~~~~~~~~~~~~~~
  CLNG-BPF [test_maps] socket_cookie_prog.o
progs/test_sysctl_loop2.c:11:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
         ^~~~~~~~~~~~~~~~
1 error generated.
In file included from progs/profiler2.c:6:
progs/profiler.inc.h:7:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
         ^~~~~~~~~~~~~~~~
author maintainer
#2 lore raw
Quoted 30 lines · Alexei Starovoitov
Quoted 21 lines · Shuah Khan

ARRAY_SIZE is defined in multiple test files. Remove the definitions and include header file for the define instead.

Remove ARRAY_SIZE define and add include bpf_util.h to bring in the define.

---
  tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +----
  tools/testing/selftests/bpf/progs/profiler.inc.h      | 5 +----
  tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +----
  tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 +---
  tools/testing/selftests/bpf/progs/test_sysctl_prog.c  | 5 +----
  5 files changed, 5 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -0
index 1d8918dfbd3f..7a5ebd330689 100644

tools/testing/selftests/bpf/progs/netif_receive_skb.c line 8

Quoted 4 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -0
@@ -5,6 +5,7 @@
  #include <bpf/bpf_helpers.h>
  #include <bpf/bpf_tracing.h>
  #include <bpf/bpf_core_read.h>
+#include <bpf/bpf_util.h>

It doesn't look like you've built it.

progs/test_sysctl_prog.c:11:10: fatal error: 'bpf/bpf_util.h' file not found

Quoted 12 lines
#include <bpf/bpf_util.h>
          ^~~~~~~~~~~~~~~~
   CLNG-BPF [test_maps] socket_cookie_prog.o
progs/test_sysctl_loop2.c:11:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
          ^~~~~~~~~~~~~~~~
1 error generated.
In file included from progs/profiler2.c:6:
progs/profiler.inc.h:7:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
          ^~~~~~~~~~~~~~~~

Sorry about that. I built it - I think something is wrong in my env. Build fails complaining about not finding vmlinux - I overlooked that the failure happened before it got to progs.

Error: failed to load BTF from .../vmlinux: No such file or directory

I do have the kernel built with gcc. Is there a clang dependency?

thanks,
-- Shuah

#3 lore raw
Quoted 45 lines · Shuah Khan
Quoted 30 lines · Alexei Starovoitov
Quoted 21 lines · Shuah Khan

ARRAY_SIZE is defined in multiple test files. Remove the definitions and include header file for the define instead.

Remove ARRAY_SIZE define and add include bpf_util.h to bring in the define.

---
  tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +----
  tools/testing/selftests/bpf/progs/profiler.inc.h      | 5 +----
  tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +----
  tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 +---
  tools/testing/selftests/bpf/progs/test_sysctl_prog.c  | 5 +----
  5 files changed, 5 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -0
index 1d8918dfbd3f..7a5ebd330689 100644

tools/testing/selftests/bpf/progs/netif_receive_skb.c line 8

Quoted 4 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -0
@@ -5,6 +5,7 @@
  #include <bpf/bpf_helpers.h>
  #include <bpf/bpf_tracing.h>
  #include <bpf/bpf_core_read.h>
+#include <bpf/bpf_util.h>

It doesn't look like you've built it.

progs/test_sysctl_prog.c:11:10: fatal error: 'bpf/bpf_util.h' file not found

Quoted 12 lines
#include <bpf/bpf_util.h>
          ^~~~~~~~~~~~~~~~
   CLNG-BPF [test_maps] socket_cookie_prog.o
progs/test_sysctl_loop2.c:11:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
          ^~~~~~~~~~~~~~~~
1 error generated.
In file included from progs/profiler2.c:6:
progs/profiler.inc.h:7:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
          ^~~~~~~~~~~~~~~~
Quoted 5 lines

Sorry about that. I built it - I think something is wrong in my env. Build fails complaining about not finding vmlinux - I overlooked that the failure happened before it got to progs.

Error: failed to load BTF from .../vmlinux: No such file or directory

Please make sure that you build vmlinux before you build selftests, BPF selftests use vmlinux to generate vmlinux.h with all kernel types (among other things). So please also make sure that all the setting in selftests/bpf/config were used in your Kconfig.

Quoted 2 lines

I do have the kernel built with gcc. Is there a clang dependency?

Yes, you'll need recent enough Clang. Probably the easiest is to get one of the latest nightly packages.

Quoted 4 lines

thanks,
-- Shuah

author maintainer
#4 lore raw
Quoted 50 lines · Andrii Nakryiko
Quoted 49 lines · Shuah Khan
Quoted 34 lines · Alexei Starovoitov
Quoted 21 lines · Shuah Khan

ARRAY_SIZE is defined in multiple test files. Remove the definitions and include header file for the define instead.

Remove ARRAY_SIZE define and add include bpf_util.h to bring in the define.

---
   tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +----
   tools/testing/selftests/bpf/progs/profiler.inc.h      | 5 +----
   tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +----
   tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 +---
   tools/testing/selftests/bpf/progs/test_sysctl_prog.c  | 5 +----
   5 files changed, 5 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -0
index 1d8918dfbd3f..7a5ebd330689 100644

tools/testing/selftests/bpf/progs/netif_receive_skb.c line 8

Quoted 4 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -0
@@ -5,6 +5,7 @@
   #include <bpf/bpf_helpers.h>
   #include <bpf/bpf_tracing.h>
   #include <bpf/bpf_core_read.h>
+#include <bpf/bpf_util.h>

It doesn't look like you've built it.

progs/test_sysctl_prog.c:11:10: fatal error: 'bpf/bpf_util.h' file not found #include <bpf/bpf_util.h>
^~~~~~~~~~~~~~~~
CLNG-BPF [test_maps] socket_cookie_prog.o
progs/test_sysctl_loop2.c:11:10: fatal error: 'bpf/bpf_util.h' file not found

Quoted 8 lines
#include <bpf/bpf_util.h>
           ^~~~~~~~~~~~~~~~
1 error generated.
In file included from progs/profiler2.c:6:
progs/profiler.inc.h:7:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
           ^~~~~~~~~~~~~~~~

Sorry about that. I built it - I think something is wrong in my env. Build fails complaining about not finding vmlinux - I overlooked that the failure happened before it got to progs.

Quoted 8 lines
Quoted 1 line

Error: failed to load BTF from .../vmlinux: No such file or directory

Please make sure that you build vmlinux before you build selftests, BPF selftests use vmlinux to generate vmlinux.h with all kernel types (among other things). So please also make sure that all the setting in selftests/bpf/config were used in your Kconfig.

Quoted 1 line

The problem in my env. is that I don't have CONFIG_DEBUG_INFO_BTF in my config and then don't have the dwarves and llvm-strip on my system. Pains of upgrading.

I am all set now. On the other hand the vmlinux.h is a mess. It has no guards for defines and including stdio.h and this generated vmlinux.h causes all sorts of problems.

thanks,
-- Shuah

#5 lore raw
Quoted 61 lines · Shuah Khan
Quoted 50 lines · Andrii Nakryiko
Quoted 49 lines · Shuah Khan
Quoted 34 lines · Alexei Starovoitov
Quoted 21 lines · Shuah Khan

ARRAY_SIZE is defined in multiple test files. Remove the definitions and include header file for the define instead.

Remove ARRAY_SIZE define and add include bpf_util.h to bring in the define.

---
   tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +----
   tools/testing/selftests/bpf/progs/profiler.inc.h      | 5 +----
   tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +----
   tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 +---
   tools/testing/selftests/bpf/progs/test_sysctl_prog.c  | 5 +----
   5 files changed, 5 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -0
index 1d8918dfbd3f..7a5ebd330689 100644

tools/testing/selftests/bpf/progs/netif_receive_skb.c line 8

Quoted 4 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -0
@@ -5,6 +5,7 @@
   #include <bpf/bpf_helpers.h>
   #include <bpf/bpf_tracing.h>
   #include <bpf/bpf_core_read.h>
+#include <bpf/bpf_util.h>

It doesn't look like you've built it.

progs/test_sysctl_prog.c:11:10: fatal error: 'bpf/bpf_util.h' file not found #include <bpf/bpf_util.h>
^~~~~~~~~~~~~~~~
CLNG-BPF [test_maps] socket_cookie_prog.o
progs/test_sysctl_loop2.c:11:10: fatal error: 'bpf/bpf_util.h' file not found

Quoted 8 lines
#include <bpf/bpf_util.h>
           ^~~~~~~~~~~~~~~~
1 error generated.
In file included from progs/profiler2.c:6:
progs/profiler.inc.h:7:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
           ^~~~~~~~~~~~~~~~

Sorry about that. I built it - I think something is wrong in my env. Build fails complaining about not finding vmlinux - I overlooked that the failure happened before it got to progs.

Quoted 8 lines
Quoted 1 line

Error: failed to load BTF from .../vmlinux: No such file or directory

Please make sure that you build vmlinux before you build selftests, BPF selftests use vmlinux to generate vmlinux.h with all kernel types (among other things). So please also make sure that all the setting in selftests/bpf/config were used in your Kconfig.

Quoted 1 line
Quoted 7 lines

The problem in my env. is that I don't have CONFIG_DEBUG_INFO_BTF in my config and then don't have the dwarves and llvm-strip on my system. Pains of upgrading.

I am all set now. On the other hand the vmlinux.h is a mess. It has no guards for defines and including stdio.h and this generated vmlinux.h causes all sorts of problems.

It does have

#ifndef __VMLINUX_H__
#define __VMLINUX_H__

Are we talking about the same vmlinux.h here?

As for stdio.h. vmlinux.h doesn't co-exist with other headers well, it's a known inconvenience we are trying to fix at Clang side. But stdio.h makes no sense as vmlinux.h is supposed to be used only from the BPF program side, where stdio.h never worked. So not sure what you are trying to do, but vmlinux.h works fine for cases it was created for.

Quoted 3 lines

thanks,
-- Shuah

author maintainer
#6 lore raw
Quoted 70 lines · Andrii Nakryiko
Quoted 61 lines · Shuah Khan
Quoted 50 lines · Andrii Nakryiko
Quoted 49 lines · Shuah Khan
Quoted 34 lines · Alexei Starovoitov
Quoted 21 lines · Shuah Khan

ARRAY_SIZE is defined in multiple test files. Remove the definitions and include header file for the define instead.

Remove ARRAY_SIZE define and add include bpf_util.h to bring in the define.

---
    tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +----
    tools/testing/selftests/bpf/progs/profiler.inc.h      | 5 +----
    tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +----
    tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 +---
    tools/testing/selftests/bpf/progs/test_sysctl_prog.c  | 5 +----
    5 files changed, 5 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -0
index 1d8918dfbd3f..7a5ebd330689 100644

tools/testing/selftests/bpf/progs/netif_receive_skb.c line 8

Quoted 4 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -0
@@ -5,6 +5,7 @@
    #include <bpf/bpf_helpers.h>
    #include <bpf/bpf_tracing.h>
    #include <bpf/bpf_core_read.h>
+#include <bpf/bpf_util.h>

It doesn't look like you've built it.

progs/test_sysctl_prog.c:11:10: fatal error: 'bpf/bpf_util.h' file not found #include <bpf/bpf_util.h>
^~~~~~~~~~~~~~~~
CLNG-BPF [test_maps] socket_cookie_prog.o
progs/test_sysctl_loop2.c:11:10: fatal error: 'bpf/bpf_util.h' file not found

Quoted 8 lines
#include <bpf/bpf_util.h>
            ^~~~~~~~~~~~~~~~
1 error generated.
In file included from progs/profiler2.c:6:
progs/profiler.inc.h:7:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
            ^~~~~~~~~~~~~~~~

Sorry about that. I built it - I think something is wrong in my env. Build fails complaining about not finding vmlinux - I overlooked that the failure happened before it got to progs.

Quoted 8 lines
Quoted 1 line

Error: failed to load BTF from .../vmlinux: No such file or directory

Please make sure that you build vmlinux before you build selftests, BPF selftests use vmlinux to generate vmlinux.h with all kernel types (among other things). So please also make sure that all the setting in selftests/bpf/config were used in your Kconfig.

Quoted 1 line
Quoted 7 lines

The problem in my env. is that I don't have CONFIG_DEBUG_INFO_BTF in my config and then don't have the dwarves and llvm-strip on my system. Pains of upgrading.

I am all set now. On the other hand the vmlinux.h is a mess. It has no guards for defines and including stdio.h and this generated vmlinux.h causes all sorts of problems.

Quoted 7 lines

It does have

#ifndef __VMLINUX_H__
#define __VMLINUX_H__

Are we talking about the same vmlinux.h here?

Yes we are. The guard it has works when vmlinux.h is included twice. It defines a lot of common defines which are the problem. Unless you add guards around each one of them, including vmlinux.h is problematic if you also include other standard includes.

You can try to include bpf_util.h for example from one of the test in progs to see the problem.

thanks,
-- Shuah

#7 lore raw
Quoted 80 lines · Shuah Khan
Quoted 70 lines · Andrii Nakryiko
Quoted 61 lines · Shuah Khan
Quoted 50 lines · Andrii Nakryiko
Quoted 49 lines · Shuah Khan
Quoted 34 lines · Alexei Starovoitov
Quoted 21 lines · Shuah Khan

ARRAY_SIZE is defined in multiple test files. Remove the definitions and include header file for the define instead.

Remove ARRAY_SIZE define and add include bpf_util.h to bring in the define.

---
    tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +----
    tools/testing/selftests/bpf/progs/profiler.inc.h      | 5 +----
    tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +----
    tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 +---
    tools/testing/selftests/bpf/progs/test_sysctl_prog.c  | 5 +----
    5 files changed, 5 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -0
index 1d8918dfbd3f..7a5ebd330689 100644

tools/testing/selftests/bpf/progs/netif_receive_skb.c line 8

Quoted 4 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -0
@@ -5,6 +5,7 @@
    #include <bpf/bpf_helpers.h>
    #include <bpf/bpf_tracing.h>
    #include <bpf/bpf_core_read.h>
+#include <bpf/bpf_util.h>

It doesn't look like you've built it.

progs/test_sysctl_prog.c:11:10: fatal error: 'bpf/bpf_util.h' file not found #include <bpf/bpf_util.h>
^~~~~~~~~~~~~~~~
CLNG-BPF [test_maps] socket_cookie_prog.o
progs/test_sysctl_loop2.c:11:10: fatal error: 'bpf/bpf_util.h' file not found

Quoted 8 lines
#include <bpf/bpf_util.h>
            ^~~~~~~~~~~~~~~~
1 error generated.
In file included from progs/profiler2.c:6:
progs/profiler.inc.h:7:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
            ^~~~~~~~~~~~~~~~

Sorry about that. I built it - I think something is wrong in my env. Build fails complaining about not finding vmlinux - I overlooked that the failure happened before it got to progs.

Quoted 8 lines
Quoted 1 line

Error: failed to load BTF from .../vmlinux: No such file or directory

Please make sure that you build vmlinux before you build selftests, BPF selftests use vmlinux to generate vmlinux.h with all kernel types (among other things). So please also make sure that all the setting in selftests/bpf/config were used in your Kconfig.

Quoted 1 line
Quoted 7 lines

The problem in my env. is that I don't have CONFIG_DEBUG_INFO_BTF in my config and then don't have the dwarves and llvm-strip on my system. Pains of upgrading.

I am all set now. On the other hand the vmlinux.h is a mess. It has no guards for defines and including stdio.h and this generated vmlinux.h causes all sorts of problems.

Quoted 7 lines

It does have

#ifndef __VMLINUX_H__
#define __VMLINUX_H__

Are we talking about the same vmlinux.h here?

Quoted 7 lines

Yes we are. The guard it has works when vmlinux.h is included twice. It defines a lot of common defines which are the problem. Unless you add guards around each one of them, including vmlinux.h is problematic if you also include other standard includes.

You can try to include bpf_util.h for example from one of the test in progs to see the problem.

bpf_util.h is a user-space header, it's not going to work from the BPF program side. If you look at any of progs/*.c (all of which are BPF program-side source code), not a single one is including bpf_util.h.

Quoted 3 lines

thanks,
-- Shuah

author maintainer
#8 lore raw
Quoted 86 lines · Andrii Nakryiko
Quoted 85 lines · Shuah Khan
Quoted 70 lines · Andrii Nakryiko
Quoted 61 lines · Shuah Khan
Quoted 50 lines · Andrii Nakryiko
Quoted 49 lines · Shuah Khan
Quoted 34 lines · Alexei Starovoitov
Quoted 21 lines · Shuah Khan

ARRAY_SIZE is defined in multiple test files. Remove the definitions and include header file for the define instead.

Remove ARRAY_SIZE define and add include bpf_util.h to bring in the define.

---
     tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +----
     tools/testing/selftests/bpf/progs/profiler.inc.h      | 5 +----
     tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +----
     tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 +---
     tools/testing/selftests/bpf/progs/test_sysctl_prog.c  | 5 +----
     5 files changed, 5 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -0
index 1d8918dfbd3f..7a5ebd330689 100644

tools/testing/selftests/bpf/progs/netif_receive_skb.c line 8

Quoted 4 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -0
@@ -5,6 +5,7 @@
     #include <bpf/bpf_helpers.h>
     #include <bpf/bpf_tracing.h>
     #include <bpf/bpf_core_read.h>
+#include <bpf/bpf_util.h>

It doesn't look like you've built it.

progs/test_sysctl_prog.c:11:10: fatal error: 'bpf/bpf_util.h' file not found #include <bpf/bpf_util.h>
^~~~~~~~~~~~~~~~
CLNG-BPF [test_maps] socket_cookie_prog.o
progs/test_sysctl_loop2.c:11:10: fatal error: 'bpf/bpf_util.h' file not found

Quoted 8 lines
#include <bpf/bpf_util.h>
             ^~~~~~~~~~~~~~~~
1 error generated.
In file included from progs/profiler2.c:6:
progs/profiler.inc.h:7:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
             ^~~~~~~~~~~~~~~~

Sorry about that. I built it - I think something is wrong in my env. Build fails complaining about not finding vmlinux - I overlooked that the failure happened before it got to progs.

Quoted 8 lines
Quoted 1 line

Error: failed to load BTF from .../vmlinux: No such file or directory

Please make sure that you build vmlinux before you build selftests, BPF selftests use vmlinux to generate vmlinux.h with all kernel types (among other things). So please also make sure that all the setting in selftests/bpf/config were used in your Kconfig.

Quoted 1 line
Quoted 7 lines

The problem in my env. is that I don't have CONFIG_DEBUG_INFO_BTF in my config and then don't have the dwarves and llvm-strip on my system. Pains of upgrading.

I am all set now. On the other hand the vmlinux.h is a mess. It has no guards for defines and including stdio.h and this generated vmlinux.h causes all sorts of problems.

Quoted 7 lines

It does have

#ifndef __VMLINUX_H__
#define __VMLINUX_H__

Are we talking about the same vmlinux.h here?

Yes we are. The guard it has works when vmlinux.h is included twice. It defines a lot of common defines which are the problem. Unless you add guards around each one of them, including vmlinux.h is problematic if you also include other standard includes.

Quoted 7 lines
Quoted 2 lines

You can try to include bpf_util.h for example from one of the test in progs to see the problem.

bpf_util.h is a user-space header, it's not going to work from the BPF program side. If you look at any of progs/*.c (all of which are BPF program-side source code), not a single one is including bpf_util.h.

Whether bpf_util.h can be included from progs isn't the main thing here. progs/test*.c including vmlinux.h (most of them seem to) can,'t include any standard .h files.

"including vmlinux.h is problematic if a test also had to include other
standard includes."

This makes this header file restrictive and works in one case and one case only when no other standard headers aren't included.

thanks,
-- Shuah

#9 lore raw
Quoted 100 lines · Shuah Khan
Quoted 86 lines · Andrii Nakryiko
Quoted 85 lines · Shuah Khan
Quoted 70 lines · Andrii Nakryiko
Quoted 61 lines · Shuah Khan
Quoted 50 lines · Andrii Nakryiko
Quoted 49 lines · Shuah Khan
Quoted 34 lines · Alexei Starovoitov
Quoted 21 lines · Shuah Khan

ARRAY_SIZE is defined in multiple test files. Remove the definitions and include header file for the define instead.

Remove ARRAY_SIZE define and add include bpf_util.h to bring in the define.

---
     tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +----
     tools/testing/selftests/bpf/progs/profiler.inc.h      | 5 +----
     tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +----
     tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 4 +---
     tools/testing/selftests/bpf/progs/test_sysctl_prog.c  | 5 +----
     5 files changed, 5 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -0
index 1d8918dfbd3f..7a5ebd330689 100644

tools/testing/selftests/bpf/progs/netif_receive_skb.c line 8

Quoted 4 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -0
@@ -5,6 +5,7 @@
     #include <bpf/bpf_helpers.h>
     #include <bpf/bpf_tracing.h>
     #include <bpf/bpf_core_read.h>
+#include <bpf/bpf_util.h>

It doesn't look like you've built it.

progs/test_sysctl_prog.c:11:10: fatal error: 'bpf/bpf_util.h' file not found #include <bpf/bpf_util.h>
^~~~~~~~~~~~~~~~
CLNG-BPF [test_maps] socket_cookie_prog.o
progs/test_sysctl_loop2.c:11:10: fatal error: 'bpf/bpf_util.h' file not found

Quoted 8 lines
#include <bpf/bpf_util.h>
             ^~~~~~~~~~~~~~~~
1 error generated.
In file included from progs/profiler2.c:6:
progs/profiler.inc.h:7:10: fatal error: 'bpf/bpf_util.h' file not found
#include <bpf/bpf_util.h>
             ^~~~~~~~~~~~~~~~

Sorry about that. I built it - I think something is wrong in my env. Build fails complaining about not finding vmlinux - I overlooked that the failure happened before it got to progs.

Quoted 8 lines
Quoted 1 line

Error: failed to load BTF from .../vmlinux: No such file or directory

Please make sure that you build vmlinux before you build selftests, BPF selftests use vmlinux to generate vmlinux.h with all kernel types (among other things). So please also make sure that all the setting in selftests/bpf/config were used in your Kconfig.

Quoted 1 line
Quoted 7 lines

The problem in my env. is that I don't have CONFIG_DEBUG_INFO_BTF in my config and then don't have the dwarves and llvm-strip on my system. Pains of upgrading.

I am all set now. On the other hand the vmlinux.h is a mess. It has no guards for defines and including stdio.h and this generated vmlinux.h causes all sorts of problems.

Quoted 7 lines

It does have

#ifndef __VMLINUX_H__
#define __VMLINUX_H__

Are we talking about the same vmlinux.h here?

Yes we are. The guard it has works when vmlinux.h is included twice. It defines a lot of common defines which are the problem. Unless you add guards around each one of them, including vmlinux.h is problematic if you also include other standard includes.

Quoted 7 lines
Quoted 2 lines

You can try to include bpf_util.h for example from one of the test in progs to see the problem.

bpf_util.h is a user-space header, it's not going to work from the BPF program side. If you look at any of progs/*.c (all of which are BPF program-side source code), not a single one is including bpf_util.h.

Whether bpf_util.h can be included from progs isn't the main thing here. progs/test*.c including vmlinux.h (most of them seem to) can,'t include any standard .h files.

Quoted 6 lines

"including vmlinux.h is problematic if a test also had to include other
standard includes."

This makes this header file restrictive and works in one case and one case only when no other standard headers aren't included.

It does work with other BPF-side headers that libbpf provides:
bpf_tracing.h, bpf_core_read.h, etc. Yes, it doesn't work with other kernel or non-kernel headers. We are well aware of this limitation and are currently trying to convince the Clang community to let us fix that with a new attribute for Clang.

But I'm not sure what we are discussing at this point. I think we established that bpf_util.h is a user-space header and can't be used from the BPF side.

Quoted 2 lines

thanks,
-- Shuah