diff mbox series

[bpf-next,1/2] libbpf: xsk: remove linux/compiler.h header

Message ID 20210310080929.641212-2-bjorn.topel@gmail.com (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series libbpf/xsk cleanups | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 5 maintainers not CCed: yhs@fb.com kpsingh@kernel.org kafai@fb.com john.fastabend@gmail.com songliubraving@fb.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch fail CHECK: Macro argument 'x' may be better as '(x)' to avoid precedence issues ERROR: Macros with complex values should be enclosed in parentheses WARNING: Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Björn Töpel March 10, 2021, 8:09 a.m. UTC
From: Björn Töpel <bjorn.topel@intel.com>

In commit 291471dd1559 ("libbpf, xsk: Add libbpf_smp_store_release
libbpf_smp_load_acquire") linux/compiler.h was added as a dependency
to xsk.h, which is the user-facing API. This makes it harder for
userspace application to consume the library. Here the header
inclusion is removed, and instead {READ,WRITE}_ONCE() is added
explicitly.

Signed-off-by: Björn Töpel <bjorn.topel@intel.com>
---
 tools/lib/bpf/libbpf_util.h | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf_util.h b/tools/lib/bpf/libbpf_util.h
index cfbcfc063c81..954da9b34a34 100644
--- a/tools/lib/bpf/libbpf_util.h
+++ b/tools/lib/bpf/libbpf_util.h
@@ -5,25 +5,30 @@ 
 #define __LIBBPF_LIBBPF_UTIL_H
 
 #include <stdbool.h>
-#include <linux/compiler.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-/* Use these barrier functions instead of smp_[rw]mb() when they are
- * used in a libbpf header file. That way they can be built into the
- * application that uses libbpf.
+/* Load-Acquire Store-Release barriers used by the XDP socket
+ * library. The following macros should *NOT* be considered part of
+ * the xsk.h API, and is subject to change anytime.
+ *
+ * LIBRARY INTERNAL
  */
+
+#define __XSK_READ_ONCE(x) (*(volatile typeof(x) *)&x)
+#define __XSK_WRITE_ONCE(x, v) (*(volatile typeof(x) *)&x) = (v)
+
 #if defined(__i386__) || defined(__x86_64__)
 # define libbpf_smp_store_release(p, v)					\
 	do {								\
 		asm volatile("" : : : "memory");			\
-		WRITE_ONCE(*p, v);					\
+		__XSK_WRITE_ONCE(*p, v);				\
 	} while (0)
 # define libbpf_smp_load_acquire(p)					\
 	({								\
-		typeof(*p) ___p1 = READ_ONCE(*p);			\
+		typeof(*p) ___p1 = __XSK_READ_ONCE(*p);			\
 		asm volatile("" : : : "memory");			\
 		___p1;							\
 	})
@@ -41,11 +46,11 @@  extern "C" {
 # define libbpf_smp_store_release(p, v)					\
 	do {								\
 		asm volatile ("fence rw,w" : : : "memory");		\
-		WRITE_ONCE(*p, v);					\
+		__XSK_WRITE_ONCE(*p, v);				\
 	} while (0)
 # define libbpf_smp_load_acquire(p)					\
 	({								\
-		typeof(*p) ___p1 = READ_ONCE(*p);			\
+		typeof(*p) ___p1 = __XSK_READ_ONCE(*p);			\
 		asm volatile ("fence r,rw" : : : "memory");		\
 		___p1;							\
 	})
@@ -55,19 +60,21 @@  extern "C" {
 #define libbpf_smp_store_release(p, v)					\
 	do {								\
 		__sync_synchronize();					\
-		WRITE_ONCE(*p, v);					\
+		__XSK_WRITE_ONCE(*p, v);				\
 	} while (0)
 #endif
 
 #ifndef libbpf_smp_load_acquire
 #define libbpf_smp_load_acquire(p)					\
 	({								\
-		typeof(*p) ___p1 = READ_ONCE(*p);			\
+		typeof(*p) ___p1 = __XSK_READ_ONCE(*p);			\
 		__sync_synchronize();					\
 		___p1;							\
 	})
 #endif
 
+/* LIBRARY INTERNAL -- END */
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif