All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 bpf-next 0/2] Provide NULL and KERNEL_VERSION macros in bpf_helpers.h
@ 2021-03-17 20:05 Andrii Nakryiko
  2021-03-17 20:05 ` [PATCH v3 bpf-next 1/2] libbpf: provide " Andrii Nakryiko
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2021-03-17 20:05 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel; +Cc: andrii, kernel-team

Provide NULL and KERNEL_VERSION macros in bpf_helpers.h. Patch #2 removes such
custom NULL definition from one of the selftests.

v2->v3:
  - instead of vmlinux.h, do this in bpf_helpers.h;
  - added KERNEL_VERSION, which comes up periodically as well;
  - I dropped strict compilation patches for now, because we run into new
    warnings (e.g., not checking read() result) in kernel-patches CI, which
    I can't even reproduce locally. Also -Wdiscarded-qualifiers pragma for
    jit_disasm.c is not supported by Clang, it needs to be
    -Wincompatible-pointer-types-discards-qualifiers for Clang; we don't have
    to deal with that in this patch set;
v1->v2:
  - fix few typos and wrong copy/paste;
  - fix #pragma push -> pop.

Andrii Nakryiko (2):
  libbpf: provide NULL and KERNEL_VERSION macros in bpf_helpers.h
  selftests/bpf: drop custom NULL #define in skb_pkt_end selftest

 tools/lib/bpf/bpf_helpers.h                     | 16 +++++++++++++++-
 tools/testing/selftests/bpf/progs/skb_pkt_end.c |  1 -
 2 files changed, 15 insertions(+), 2 deletions(-)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 bpf-next 1/2] libbpf: provide NULL and KERNEL_VERSION macros in bpf_helpers.h
  2021-03-17 20:05 [PATCH v3 bpf-next 0/2] Provide NULL and KERNEL_VERSION macros in bpf_helpers.h Andrii Nakryiko
@ 2021-03-17 20:05 ` Andrii Nakryiko
  2021-03-17 20:05 ` [PATCH v3 bpf-next 2/2] selftests/bpf: drop custom NULL #define in skb_pkt_end selftest Andrii Nakryiko
  2021-03-18  3:28 ` [PATCH v3 bpf-next 0/2] Provide NULL and KERNEL_VERSION macros in bpf_helpers.h Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2021-03-17 20:05 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel; +Cc: andrii, kernel-team

Given that vmlinux.h is not compatible with headers like stddef.h, NULL poses
an annoying problem: it is defined as #define, so is not captured in BTF, so
is not emitted into vmlinux.h. This leads to users either sticking to explicit
0, or defining their own NULL (as progs/skb_pkt_end.c does).

But it's easy for bpf_helpers.h to provide (conditionally) NULL definition.
Similarly, KERNEL_VERSION is another commonly missed macro that came up
multiple times. So this patch adds both of them, along with offsetof(), that
also is typically defined in stddef.h, just like NULL.

This might cause compilation warning for existing BPF applications defining
their own NULL and/or KERNEL_VERSION already:

  progs/skb_pkt_end.c:7:9: warning: 'NULL' macro redefined [-Wmacro-redefined]
  #define NULL 0
          ^
  /tmp/linux/tools/testing/selftests/bpf/tools/include/vmlinux.h:4:9: note: previous definition is here
  #define NULL ((void *)0)
	  ^

It is trivial to fix, though, so long-term benefits outweight temporary
inconveniences.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/lib/bpf/bpf_helpers.h | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
index 53ff81c49dbd..cc2e51c64a54 100644
--- a/tools/lib/bpf/bpf_helpers.h
+++ b/tools/lib/bpf/bpf_helpers.h
@@ -40,8 +40,22 @@
 #define __weak __attribute__((weak))
 #endif
 
+/* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include
+ * any system-level headers (such as stddef.h, linux/version.h, etc), and
+ * commonly-used macros like NULL and KERNEL_VERSION aren't available through
+ * vmlinux.h. This just adds unnecessary hurdles and forces users to re-define
+ * them on their own. So as a convenience, provide such definitions here.
+ */
+#ifndef NULL
+#define NULL ((void *)0)
+#endif
+
+#ifndef KERNEL_VERSION
+#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c))
+#endif
+
 /*
- * Helper macro to manipulate data structures
+ * Helper macros to manipulate data structures
  */
 #ifndef offsetof
 #define offsetof(TYPE, MEMBER)	((unsigned long)&((TYPE *)0)->MEMBER)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 bpf-next 2/2] selftests/bpf: drop custom NULL #define in skb_pkt_end selftest
  2021-03-17 20:05 [PATCH v3 bpf-next 0/2] Provide NULL and KERNEL_VERSION macros in bpf_helpers.h Andrii Nakryiko
  2021-03-17 20:05 ` [PATCH v3 bpf-next 1/2] libbpf: provide " Andrii Nakryiko
@ 2021-03-17 20:05 ` Andrii Nakryiko
  2021-03-18  3:28 ` [PATCH v3 bpf-next 0/2] Provide NULL and KERNEL_VERSION macros in bpf_helpers.h Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2021-03-17 20:05 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel; +Cc: andrii, kernel-team

Now that bpftool generates NULL definition as part of vmlinux.h, drop custom
NULL definition in skb_pkt_end.c.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/testing/selftests/bpf/progs/skb_pkt_end.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/progs/skb_pkt_end.c b/tools/testing/selftests/bpf/progs/skb_pkt_end.c
index cf6823f42e80..7f2eaa2f89f8 100644
--- a/tools/testing/selftests/bpf/progs/skb_pkt_end.c
+++ b/tools/testing/selftests/bpf/progs/skb_pkt_end.c
@@ -4,7 +4,6 @@
 #include <bpf/bpf_core_read.h>
 #include <bpf/bpf_helpers.h>
 
-#define NULL 0
 #define INLINE __always_inline
 
 #define skb_shorter(skb, len) ((void *)(long)(skb)->data + (len) > (void *)(long)skb->data_end)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3 bpf-next 0/2] Provide NULL and KERNEL_VERSION macros in bpf_helpers.h
  2021-03-17 20:05 [PATCH v3 bpf-next 0/2] Provide NULL and KERNEL_VERSION macros in bpf_helpers.h Andrii Nakryiko
  2021-03-17 20:05 ` [PATCH v3 bpf-next 1/2] libbpf: provide " Andrii Nakryiko
  2021-03-17 20:05 ` [PATCH v3 bpf-next 2/2] selftests/bpf: drop custom NULL #define in skb_pkt_end selftest Andrii Nakryiko
@ 2021-03-18  3:28 ` Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2021-03-18  3:28 UTC (permalink / raw)
  To: Andrii Nakryiko, bpf, netdev, daniel; +Cc: kernel-team

On 3/17/21 1:05 PM, Andrii Nakryiko wrote:
> Provide NULL and KERNEL_VERSION macros in bpf_helpers.h. Patch #2 removes such
> custom NULL definition from one of the selftests.
> 
> v2->v3:
>    - instead of vmlinux.h, do this in bpf_helpers.h;
>    - added KERNEL_VERSION, which comes up periodically as well;
>    - I dropped strict compilation patches for now, because we run into new
>      warnings (e.g., not checking read() result) in kernel-patches CI, which
>      I can't even reproduce locally. Also -Wdiscarded-qualifiers pragma for
>      jit_disasm.c is not supported by Clang, it needs to be
>      -Wincompatible-pointer-types-discards-qualifiers for Clang; we don't have
>      to deal with that in this patch set;
> v1->v2:
>    - fix few typos and wrong copy/paste;
>    - fix #pragma push -> pop.

Applied.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-03-18  3:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-17 20:05 [PATCH v3 bpf-next 0/2] Provide NULL and KERNEL_VERSION macros in bpf_helpers.h Andrii Nakryiko
2021-03-17 20:05 ` [PATCH v3 bpf-next 1/2] libbpf: provide " Andrii Nakryiko
2021-03-17 20:05 ` [PATCH v3 bpf-next 2/2] selftests/bpf: drop custom NULL #define in skb_pkt_end selftest Andrii Nakryiko
2021-03-18  3:28 ` [PATCH v3 bpf-next 0/2] Provide NULL and KERNEL_VERSION macros in bpf_helpers.h Alexei Starovoitov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.