On Mon, Feb 7, 2022 at 9:16 PM Ilya Leoshkevich <iii@xxxxxxxxxxxxx> wrote: > > These macros are useful for both libbpf and bpf progs, so put them into > a separate header dedicated to this use case. > > Signed-off-by: Ilya Leoshkevich <iii@xxxxxxxxxxxxx> > --- > tools/lib/bpf/Makefile | 2 +- > tools/lib/bpf/bpf_common_helpers.h | 30 ++++++++++++++++++++++++++ > tools/lib/bpf/bpf_helpers.h | 15 +------------ > tools/testing/selftests/bpf/bpf_util.h | 10 +-------- > 4 files changed, 33 insertions(+), 24 deletions(-) > create mode 100644 tools/lib/bpf/bpf_common_helpers.h > > diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile > index b8b37fe76006..60b06c22e0a1 100644 > --- a/tools/lib/bpf/Makefile > +++ b/tools/lib/bpf/Makefile > @@ -239,7 +239,7 @@ install_lib: all_cmd > > SRC_HDRS := bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h xsk.h \ > bpf_helpers.h bpf_tracing.h bpf_endian.h bpf_core_read.h \ > - skel_internal.h libbpf_version.h > + skel_internal.h libbpf_version.h bpf_common_helpers.h Wait, how did we get from fixing s390x syscall arg fetching to exposing a new public API header from libbpf?... I feel like I missed a few revisions and discussion threads. > GEN_HDRS := $(BPF_GENERATED) > > INSTALL_PFX := $(DESTDIR)$(prefix)/include/bpf > diff --git a/tools/lib/bpf/bpf_common_helpers.h b/tools/lib/bpf/bpf_common_helpers.h > new file mode 100644 > index 000000000000..79db303b6ae2 > --- /dev/null > +++ b/tools/lib/bpf/bpf_common_helpers.h > @@ -0,0 +1,30 @@ > +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ > +#ifndef __BPF_COMMON_HELPERS__ > +#define __BPF_COMMON_HELPERS__ > + > +/* > + * Helper macros that can be used both by libbpf and bpf progs. > + */ > + > +#ifndef offsetof > +#define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER) > +#endif > + > +#ifndef sizeof_field > +#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) > +#endif > + > +#ifndef offsetofend > +#define offsetofend(TYPE, MEMBER) \ > + (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) > +#endif > + > +#ifndef container_of > +#define container_of(ptr, type, member) \ > + ({ \ > + void *__mptr = (void *)(ptr); \ > + ((type *)(__mptr - offsetof(type, member))); \ > + }) > +#endif > + > +#endif > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h > index 44df982d2a5c..1e8b609c1000 100644 > --- a/tools/lib/bpf/bpf_helpers.h > +++ b/tools/lib/bpf/bpf_helpers.h > @@ -2,6 +2,7 @@ > #ifndef __BPF_HELPERS__ > #define __BPF_HELPERS__ > > +#include "bpf_common_helpers.h" > /* > * Note that bpf programs need to include either > * vmlinux.h (auto-generated from BTF) or linux/types.h > @@ -61,20 +62,6 @@ > #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c))) > #endif > > -/* > - * Helper macros to manipulate data structures > - */ > -#ifndef offsetof > -#define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER) > -#endif > -#ifndef container_of > -#define container_of(ptr, type, member) \ > - ({ \ > - void *__mptr = (void *)(ptr); \ > - ((type *)(__mptr - offsetof(type, member))); \ > - }) > -#endif > - > /* > * Helper macro to throw a compilation error if __bpf_unreachable() gets > * built into the resulting code. This works given BPF back end does not > diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h > index a3352a64c067..bc0b741b1eef 100644 > --- a/tools/testing/selftests/bpf/bpf_util.h > +++ b/tools/testing/selftests/bpf/bpf_util.h > @@ -6,6 +6,7 @@ > #include <stdlib.h> > #include <string.h> > #include <errno.h> > +#include <bpf/bpf_common_helpers.h> > #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */ > > static inline unsigned int bpf_num_possible_cpus(void) > @@ -31,13 +32,4 @@ static inline unsigned int bpf_num_possible_cpus(void) > # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) > #endif > > -#ifndef sizeof_field > -#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) > -#endif > - > -#ifndef offsetofend > -#define offsetofend(TYPE, MEMBER) \ > - (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) > -#endif > - > #endif /* __BPF_UTIL__ */ > -- > 2.34.1 >