All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ilya Leoshkevich <iii@linux.ibm.com>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Paul Walmsley <paul.walmsley@sifive.com>,
	"Naveen N . Rao" <naveen.n.rao@linux.vnet.ibm.com>,
	Mark Rutland <mark.rutland@arm.com>
Cc: bpf@vger.kernel.org, Ilya Leoshkevich <iii@linux.ibm.com>
Subject: [PATCH bpf-next v4 10/14] libbpf: Move data structure manipulation macros to bpf_common_helpers.h
Date: Tue,  8 Feb 2022 06:16:31 +0100	[thread overview]
Message-ID: <20220208051635.2160304-11-iii@linux.ibm.com> (raw)
In-Reply-To: <20220208051635.2160304-1-iii@linux.ibm.com>

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
 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


  parent reply	other threads:[~2022-02-08  5:32 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-08  5:16 [PATCH bpf-next v4 00/14] Fix accessing syscall arguments Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 01/14] selftests/bpf: Fix an endianness issue in bpf_syscall_macro test Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 02/14] selftests/bpf: Fix a potential offsetofend redefinition in test_cls_redirect Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 03/14] selftests/bpf: Compile bpf_syscall_macro test also with user headers Ilya Leoshkevich
2022-02-08 22:06   ` Andrii Nakryiko
2022-02-08 23:11     ` Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 04/14] libbpf: Fix a typo in bpf_tracing.h Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 05/14] libbpf: Generalize overriding syscall parameter access macros Ilya Leoshkevich
2022-02-08 22:05   ` Andrii Nakryiko
2022-02-08 23:08     ` Ilya Leoshkevich
2022-02-08 23:21       ` Andrii Nakryiko
2022-02-08 23:30         ` Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 06/14] libbpf: Add PT_REGS_SYSCALL_REGS macro Ilya Leoshkevich
2022-02-08 22:08   ` Andrii Nakryiko
2022-02-08 23:26     ` Ilya Leoshkevich
2022-02-08 23:43       ` Andrii Nakryiko
2022-02-08  5:16 ` [PATCH bpf-next v4 07/14] selftests/bpf: Use PT_REGS_SYSCALL_REGS in bpf_syscall_macro Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 08/14] libbpf: Use struct pt_regs when compiling with kernel headers Ilya Leoshkevich
2022-02-08 22:12   ` Andrii Nakryiko
2022-02-08 23:35     ` Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 09/14] libbpf: Fix riscv register names Ilya Leoshkevich
2022-02-08  5:16 ` Ilya Leoshkevich [this message]
2022-02-08 22:14   ` [PATCH bpf-next v4 10/14] libbpf: Move data structure manipulation macros to bpf_common_helpers.h Andrii Nakryiko
2022-02-08 23:37     ` Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 11/14] libbpf: Fix accessing the first syscall argument on s390 Ilya Leoshkevich
2022-02-08 13:10   ` Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 12/14] s390: add a comment that warns that orig_gpr2 should not be moved Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 13/14] libbpf: Fix accessing the first syscall argument on arm64 Ilya Leoshkevich
2022-02-08  5:16 ` [PATCH bpf-next v4 14/14] arm64: add a comment that warns that orig_x0 should not be moved Ilya Leoshkevich
2022-02-08 19:25   ` Alexei Starovoitov
2022-02-08 19:46     ` Ilya Leoshkevich
2022-02-08 21:11       ` Alexei Starovoitov
2022-02-08 21:46         ` Ilya Leoshkevich
2022-02-08 22:23           ` Andrii Nakryiko
2022-02-08 23:39             ` Ilya Leoshkevich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220208051635.2160304-11-iii@linux.ibm.com \
    --to=iii@linux.ibm.com \
    --cc=agordeev@linux.ibm.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=borntraeger@linux.ibm.com \
    --cc=bpf@vger.kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=daniel@iogearbox.net \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=mark.rutland@arm.com \
    --cc=mpe@ellerman.id.au \
    --cc=naveen.n.rao@linux.vnet.ibm.com \
    --cc=paul.walmsley@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.