Skip to contentSkip to conversation

Optimize i386 memory barriers

PATCH In review Bart Van Assche 2 participants 6 messages Jul 8, 2019 Block layer

lore
Share & tools
lore
#0 lore raw

cover Optimize i386 memory barriers

Hi Jens,

The four patches in this series optimize i386 memory barriers and fix the 32-bit liburing build. Please consider these patches for the official liburing repository.

Thanks,

Bart.

Bart Van Assche (4):
Makefiles: Support specifying CFLAGS on the command line Fix the 32-bit build
Change __x86_64 into __x86_64__
Optimize i386 memory barriers

 examples/Makefile        |  3 ++-
 src/Makefile             |  2 +-
 src/barrier.h            | 24 ++++++++----------------
 test/Makefile            |  3 ++-
 test/io_uring_register.c | 11 ++++++-----
 test/send_recvmsg.c      |  4 ++--
 6 files changed, 21 insertions(+), 26 deletions(-)
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
#1 lore raw

PATCH 1/4 Makefiles: Support specifying CFLAGS on the command line

This patch makes the liburing build work as expected for e.g. the following command:

make CFLAGS=-m32

and avoids that the build fails as follows for the above command:

make[1]: Entering directory 'liburing/test' cc -m32 -o poll poll.c -luring
/usr/bin/ld: cannot find -luring
collect2: error: ld returned 1 exit status Makefile:18: recipe for target 'poll' failed make[1]: *** [poll] Error 1
make[1]: Leaving directory 'software/liburing/test' Makefile:12: recipe for target 'all' failed
make: *** [all] Error 2

---
 examples/Makefile | 3 ++-
 src/Makefile      | 2 +-
 test/Makefile     | 3 ++-
 3 files changed, 5 insertions(+), 3 deletions(-)
examples/Makefile +2 -1
index 6fe9bb9d15b2..ed73fcdcdaa2 100644
@@ -1,4 +1,5 @@
-CFLAGS ?= -g -O2 -Wall -D_GNU_SOURCE -L../src/
+CFLAGS ?= -g -O2
+override CFLAGS += -Wall -D_GNU_SOURCE -L../src/
 
 all_targets += io_uring-test io_uring-cp link-cp
 
src/Makefile +1 -1
index b68b57e73013..954e05ea6160 100644
@@ -3,7 +3,7 @@ includedir=$(prefix)/include
 libdir=$(prefix)/lib
 
 CFLAGS ?= -g -fomit-frame-pointer -O2
-CFLAGS += -Wall -I.
+override CFLAGS += -Wall -I.
 SO_CFLAGS=-shared -fPIC $(CFLAGS)
 L_CFLAGS=$(CFLAGS)
 LINK_FLAGS=
test/Makefile +2 -1
index b94e29ecbbff..52b4c9e53d22 100644
@@ -1,4 +1,5 @@
-CFLAGS ?= -g -O2 -Wall -D_GNU_SOURCE -L../src/
+CFLAGS ?= -g -O2
+override CFLAGS += -Wall -D_GNU_SOURCE -L../src/
 
 all_targets += poll poll-cancel ring-leak fsync io_uring_setup io_uring_register \
 	       io_uring_enter nop sq-full cq-full 35fa71a030ca-test \
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
#2 lore raw

PATCH 2/4 Fix the 32-bit build

In reply to Bart Van Assche

Fix the warnings reported when building liburing as follows:

make CFLAGS=-m32

---
 test/io_uring_register.c | 11 ++++++-----
 test/send_recvmsg.c      |  4 ++--
 2 files changed, 8 insertions(+), 7 deletions(-)
test/io_uring_register.c +6 -5
index 070d0ff3d511..32e52179af29 100644
@@ -140,7 +140,7 @@ test_max_fds(int uring_fd)
 		perror("mmap fd_as");
 		exit(1);
 	}
-	printf("allocated %lu bytes of address space\n", UINT_MAX * sizeof(int));
+	printf("allocated %zu bytes of address space\n", UINT_MAX * sizeof(int));
 
 	fdtable_fd = mkstemp(template);
 	if (fdtable_fd < 0) {
@@ -178,7 +178,8 @@ test_max_fds(int uring_fd)
 		fds = mmap(fds, 128*1024*1024, PROT_READ|PROT_WRITE,
 			   MAP_SHARED|MAP_FIXED, fdtable_fd, 0);
 		if (fds == MAP_FAILED) {
-			printf("mmap failed at offset %lu\n", (char *)fd_as - (char *)fds);
+			printf("mmap failed at offset %lu\n",
+			       (unsigned long)((char *)fd_as - (char *)fds));
 			exit(1);
 		}
 	}
@@ -217,7 +218,7 @@ test_max_fds(int uring_fd)
 	close(fdtable_fd);
 	ret = munmap(fd_as, UINT_MAX * sizeof(int));
 	if (ret != 0) {
-		printf("munmap(%lu) failed\n", UINT_MAX * sizeof(int));
+		printf("munmap(%zu) failed\n", UINT_MAX * sizeof(int));
 		exit(1);
 	}
 
@@ -244,7 +245,7 @@ test_memlock_exceeded(int fd)
 		ret = io_uring_register(fd, IORING_REGISTER_BUFFERS, &iov, 1);
 		if (ret < 0) {
 			if (errno == ENOMEM) {
-				printf("io_uring_register of %lu bytes failed "
+				printf("io_uring_register of %zu bytes failed "
 				       "with ENOMEM (expected).\n", iov.iov_len);
 				iov.iov_len /= 2;
 				continue;
@@ -253,7 +254,7 @@ test_memlock_exceeded(int fd)
 			free(buf);
 			return 1;
 		}
-		printf("successfully registered %lu bytes (%d).\n",
+		printf("successfully registered %zu bytes (%d).\n",
 		       iov.iov_len, ret);
 		ret = io_uring_register(fd, IORING_UNREGISTER_BUFFERS, NULL, 0);
 		if (ret != 0) {
test/send_recvmsg.c +2 -2
index e6cda69664a5..9187906d3dbf 100644
@@ -64,7 +64,7 @@ static int do_recvmsg(void)
 	memset(sqe, 0, sizeof(*sqe));
 	sqe->opcode = IORING_OP_RECVMSG;
 	sqe->fd = sockfd;
-	sqe->addr = (uint64_t) &msg;
+	sqe->addr = (uintptr_t) &msg;
 	sqe->len = 1;
 
 	ret = io_uring_submit(&ring);
@@ -136,7 +136,7 @@ static int do_sendmsg(void)
 	memset(sqe, 0, sizeof(*sqe));
 	sqe->opcode = IORING_OP_SENDMSG;
 	sqe->fd = sockfd;
-	sqe->addr = (uint64_t) &msg;
+	sqe->addr = (uintptr_t) &msg;
 	sqe->len = 1;
 
 	ret = io_uring_submit(&ring);
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
#3 lore raw

PATCH 3/4 Change __x86_64 into __x86_64__

In reply to Bart Van Assche

This patch improves consistency with the Linux kernel source code.

---
 src/barrier.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
src/barrier.h +4 -4
index e1a407fccde2..eb8ee1ec9d34 100644
@@ -31,7 +31,7 @@ after the acquire operation executes. This is implemented using
 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
 
 
-#if defined(__x86_64) || defined(__i386__)
+#if defined(__x86_64__) || defined(__i386__)
 /* From tools/arch/x86/include/asm/barrier.h */
 #if defined(__i386__)
 /*
@@ -64,14 +64,14 @@ do {						\
 	___p1;					\
 })
 #endif /* defined(__x86_64__) */
-#else
+#else /* defined(__x86_64__) || defined(__i386__) */
 /*
  * Add arch appropriate definitions. Be safe and use full barriers for
  * archs we don't have support for.
  */
 #define smp_rmb()	__sync_synchronize()
 #define smp_wmb()	__sync_synchronize()
-#endif
+#endif /* defined(__x86_64__) || defined(__i386__) */
 
 /* From tools/include/asm/barrier.h */
 
@@ -92,4 +92,4 @@ do {						\
 })
 #endif
 
-#endif
+#endif /* defined(LIBURING_BARRIER_H) */
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
#4 lore raw

PATCH 4/4 Optimize i386 memory barriers

In reply to Bart Van Assche

Use identical memory barrier implementations on 32 and 64 bit Intel CPUs. In the past the Linux kernel supported 32 bit CPUs that violate the x86 ordering standard. Since io_uring is not supported by these older kernels, do not support these older CPUs in liburing. See also Linux kernel commit 5927145efd5d ("x86/cpu: Remove the CONFIG_X86_PPRO_FENCE=y quirk") # v4.16.

---
 src/barrier.h | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
src/barrier.h +4 -12
index eb8ee1ec9d34..e079cf609f26 100644
@@ -32,25 +32,18 @@ after the acquire operation executes. This is implemented using
 
 
 #if defined(__x86_64__) || defined(__i386__)
-/* From tools/arch/x86/include/asm/barrier.h */
-#if defined(__i386__)
-/*
- * Some non-Intel clones support out of order store. wmb() ceases to be a
- * nop for these.
- */
-#define mb()	asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
-#define rmb()	asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
-#define wmb()	asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
-#elif defined(__x86_64__)
+/* Adapted from arch/x86/include/asm/barrier.h */
 #define mb()	asm volatile("mfence" ::: "memory")
 #define rmb()	asm volatile("lfence" ::: "memory")
 #define wmb()	asm volatile("sfence" ::: "memory")
 #define smp_rmb() barrier()
 #define smp_wmb() barrier()
+#if defined(__i386__)
+#define smp_mb()  asm volatile("lock; addl $0,0(%%esp)" ::: "memory", "cc")
+#else
 #define smp_mb()  asm volatile("lock; addl $0,-132(%%rsp)" ::: "memory", "cc")
 #endif
 
-#if defined(__x86_64__)
 #define smp_store_release(p, v)			\
 do {						\
 	barrier();				\
@@ -63,7 +56,6 @@ do {						\
 	barrier();				\
 	___p1;					\
 })
-#endif /* defined(__x86_64__) */
 #else /* defined(__x86_64__) || defined(__i386__) */
 /*
  * Add arch appropriate definitions. Be safe and use full barriers for
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
maintainer
#5 lore raw
In reply to Bart Van Assche
Quoted 5 lines · Bart Van Assche

Hi Jens,

The four patches in this series optimize i386 memory barriers and fix the 32-bit liburing build. Please consider these patches for the official liburing repository.

Looks good, thanks Bart. Applied.

Signature · 1 line
Jens Axboe