diff mbox series

[bpf-next,v4,10/14] libbpf: Move data structure manipulation macros to bpf_common_helpers.h

Message ID 20220208051635.2160304-11-iii@linux.ibm.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Fix accessing syscall arguments | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 9 maintainers not CCed: andrii@kernel.org linux-kselftest@vger.kernel.org kpsingh@kernel.org john.fastabend@gmail.com kafai@fb.com songliubraving@fb.com shuah@kernel.org yhs@fb.com netdev@vger.kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning CHECK: Macro argument 'MEMBER' may be better as '(MEMBER)' to avoid precedence issues WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next fail VM_Test

Commit Message

Ilya Leoshkevich Feb. 8, 2022, 5:16 a.m. UTC
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@linux.ibm.com>
---
 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

Comments

Andrii Nakryiko Feb. 8, 2022, 10:14 p.m. UTC | #1
On Mon, Feb 7, 2022 at 9:16 PM Ilya Leoshkevich <iii@linux.ibm.com> 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@linux.ibm.com>
> ---
>  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
>
Ilya Leoshkevich Feb. 8, 2022, 11:37 p.m. UTC | #2
On Tue, 2022-02-08 at 14:14 -0800, Andrii Nakryiko wrote:
> On Mon, Feb 7, 2022 at 9:16 PM Ilya Leoshkevich <iii@linux.ibm.com>
> 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@linux.ibm.com>
> > ---
> >  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.

I didn't want to add yet another copy of
offsetof/sizeof_field/offsetofend to the code. However, since we don't
need offsetofend anymore, this patch will be dropped.

> 
> >  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
> >
diff mbox series

Patch

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
 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__ */