libbpf: provide NULL and KERNEL_VERSION macros in bpf_helpers.h
authorAndrii Nakryiko <andrii@kernel.org>
Wed, 17 Mar 2021 20:05:09 +0000 (13:05 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 18 Mar 2021 01:48:05 +0000 (18:48 -0700)
commit9ae2c26e43248b722e79fe867be38062c9dd1e5f
tree63141ed8cdfad03afaa036f477da212228920ad9
parent97a19caf1b1f6a9d4f620a9d51405a1973bd4641
libbpf: provide NULL and KERNEL_VERSION macros in bpf_helpers.h

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>
Link: https://lore.kernel.org/r/20210317200510.1354627-2-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/bpf_helpers.h