Skip to contentSkip to conversation

liburing/barrier.h: Add prefix io_uring to barriers

PATCH In review Julia Suvorova 5 participants 8 messages Aug 12, 2019 – Aug 20, 2019 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 liburing/barrier.h: Add prefix io_uring to barriers

The names of the barriers conflict with the namespaces of other projects when trying to directly include liburing.h. Avoid using popular global names.

---
 src/include/liburing.h         |  9 +++++---
 src/include/liburing/barrier.h | 42 ++++++++++++++++++----------------
 src/queue.c                    |  2 +-
 test/io_uring_enter.c          |  2 +-
 4 files changed, 30 insertions(+), 25 deletions(-)
src/include/liburing.h +6 -3
index fb78cd3..7d7c9df 100644
@@ -88,9 +88,12 @@ extern int io_uring_register_eventfd(struct io_uring *ring, int fd);
 extern int io_uring_unregister_eventfd(struct io_uring *ring);
 
 #define io_uring_for_each_cqe(ring, head, cqe)					\
-	/* smp_load_acquire() enforces the order of tail and CQE reads. */	\
+	/*									\
+	 * io_uring_smp_load_acquire() enforces the order of tail		\
+	 * and CQE reads.							\
+	 */									\
 	for (head = *(ring)->cq.khead;						\
-	     (cqe = (head != smp_load_acquire((ring)->cq.ktail) ?		\
+	     (cqe = (head != io_uring_smp_load_acquire((ring)->cq.ktail) ?	\
 		&(ring)->cq.cqes[head & (*(ring)->cq.kring_mask)] : NULL));	\
 	     head++)								\
 
@@ -108,7 +111,7 @@ static inline void io_uring_cq_advance(struct io_uring *ring,
 		 * Ensure that the kernel only sees the new value of the head
 		 * index after the CQEs have been read.
 		 */
-		smp_store_release(cq->khead, *cq->khead + nr);
+		io_uring_smp_store_release(cq->khead, *cq->khead + nr);
 	}
 }
 
src/include/liburing/barrier.h +22 -20

81 lines not on this page · raw message

src/queue.c +1 -1
index 74a077f..007733c 100644
@@ -104,7 +104,7 @@ static int __io_uring_submit(struct io_uring *ring, unsigned wait_nr)
 	 * Ensure that the kernel sees the SQE updates before it sees the tail
 	 * update.
 	 */
-	smp_store_release(sq->ktail, ktail);
+	io_uring_smp_store_release(sq->ktail, ktail);
 
 	flags = 0;
 	if (wait_nr || sq_ring_needs_enter(ring, &flags)) {
test/io_uring_enter.c +1 -1
index c2030c1..8190178 100644
@@ -266,7 +266,7 @@ main(int argc, char **argv)
 	 * Ensure that the kernel sees the SQE update before it sees the tail
 	 * update.
 	 */
-	smp_store_release(sq->ktail, ktail);
+	io_uring_smp_store_release(sq->ktail, ktail);
 
 	ret = io_uring_enter(ring.ring_fd, 1, 0, 0, NULL);
 	/* now check to see if our sqe was dropped */
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw

src/include/liburing/barrier.h after the acquire operation executes. This is implemented using · line 29

Quoted 13 lines · Julia Suvorova
src/include/liburing/barrier.h +1 -1
index 98be9e5..4f3d1d7 100644
@@ -23,7 +23,7 @@ after the acquire operation executes. This is implemented using
 /* From tools/include/linux/compiler.h */
 /* Optimization barrier */
 /* The "volatile" is due to gcc bugs */
-#define barrier() __asm__ __volatile__("": : :"memory")
+#define io_uring_barrier()     __asm__ __volatile__("": : :"memory")
 
 /* From tools/virtio/linux/compiler.h */
 #define WRITE_ONCE(var, val) \

Please prefix WRITE_ONCE() and READ_ONCE() with IO_URING_ as well. They are fairly likely to be used in code derived from the Linux kernel.

Otherwise:

#2 lore raw
In reply to Julia Suvorova

src/include/liburing/barrier.h after the acquire operation executes. This is implemented using · line 40

Quoted 10 lines · Julia Suvorova
src/include/liburing/barrier.h +5 -5
-#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()
+#define io_uring_mb()		asm volatile("mfence" ::: "memory")
+#define io_uring_rmb()		asm volatile("lfence" ::: "memory")
+#define io_uring_wmb()		asm volatile("sfence" ::: "memory")
+#define io_uring_smp_rmb()	io_uring_barrier()
+#define io_uring_smp_wmb()	io_uring_barrier()

Do users of liburing need these macros? If not, have you considered to move these macros to a new header file that is only used inside liburing and such that these macros are no longer visible to liburing users?

Thanks,

Bart.

maintainer
#3 lore raw
Quoted 15 lines · Bart Van Assche

src/include/liburing/barrier.h after the acquire operation executes. This is implemented using · line 40

Quoted 10 lines · Julia Suvorova
src/include/liburing/barrier.h +5 -5
-#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()
+#define io_uring_mb()		asm volatile("mfence" ::: "memory")
+#define io_uring_rmb()		asm volatile("lfence" ::: "memory")
+#define io_uring_wmb()		asm volatile("sfence" ::: "memory")
+#define io_uring_smp_rmb()	io_uring_barrier()
+#define io_uring_smp_wmb()	io_uring_barrier()

Do users of liburing need these macros? If not, have you considered to move these macros to a new header file that is only used inside liburing and such that these macros are no longer visible to liburing users?

The exposed API should not need any explicit barriers from the user, so this suggestion makes a lot of sense to me.

Signature · 1 line
Jens Axboe
#4 lore raw
Quoted 18 lines · Jens Axboe
Quoted 15 lines · Bart Van Assche

src/include/liburing/barrier.h after the acquire operation executes. This is implemented using · line 40

Quoted 10 lines · Julia Suvorova
src/include/liburing/barrier.h +5 -5
-#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()
+#define io_uring_mb()               asm volatile("mfence" ::: "memory")
+#define io_uring_rmb()              asm volatile("lfence" ::: "memory")
+#define io_uring_wmb()              asm volatile("sfence" ::: "memory")
+#define io_uring_smp_rmb()  io_uring_barrier()
+#define io_uring_smp_wmb()  io_uring_barrier()

Do users of liburing need these macros? If not, have you considered to move these macros to a new header file that is only used inside liburing and such that these macros are no longer visible to liburing users?

Quoted 2 lines

The exposed API should not need any explicit barriers from the user, so this suggestion makes a lot of sense to me.

How about moving the definition of io_uring_cqe_seen() with whole io_uring_cq_advance() and io_uring_for_each_cqe() from liburing.h to queue.c? This way we can cover all barriers, and leave barrier.h local.

Do you need io_uring_cq_advance and io_uring_for_each_cqe in the library?

Best regards, Julia Suvorova.

#5 lore raw
Quoted 23 lines · Julia Suvorova
Quoted 18 lines · Jens Axboe
Quoted 15 lines · Bart Van Assche

src/include/liburing/barrier.h after the acquire operation executes. This is implemented using · line 40

Quoted 10 lines · Julia Suvorova
src/include/liburing/barrier.h +5 -5
-#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()
+#define io_uring_mb()               asm volatile("mfence" ::: "memory")
+#define io_uring_rmb()              asm volatile("lfence" ::: "memory")
+#define io_uring_wmb()              asm volatile("sfence" ::: "memory")
+#define io_uring_smp_rmb()  io_uring_barrier()
+#define io_uring_smp_wmb()  io_uring_barrier()

Do users of liburing need these macros? If not, have you considered to move these macros to a new header file that is only used inside liburing and such that these macros are no longer visible to liburing users?

Quoted 2 lines

The exposed API should not need any explicit barriers from the user, so this suggestion makes a lot of sense to me.

Quoted 7 lines

How about moving the definition of io_uring_cqe_seen() with whole io_uring_cq_advance() and io_uring_for_each_cqe() from liburing.h to queue.c? This way we can cover all barriers, and leave barrier.h local.

Do you need io_uring_cq_advance and io_uring_for_each_cqe in the library?

This is one of the usage patterns:

io_uring_cqe* cqe;
int head;
int count = 0;

io_uring_for_each_cqe(&m_io_uring, head, cqe)

{
    /* ... */
    count++;
}

io_uring_cq_advance(&m_io_uring, count);

A little bit more performance is squeezed out this way.

Hrvoje Zeba

maintainer
#6 lore raw
In reply to Julia Suvorova
Quoted 3 lines · Julia Suvorova

The names of the barriers conflict with the namespaces of other projects when trying to directly include liburing.h. Avoid using popular global names.

I have applied this now. I don't think we can avoid having the barriers in the namespace, as we do need them in various macros.

Signature · 1 line
Jens Axboe
#7 lore raw
Quoted 8 lines · Jens Axboe
Quoted 3 lines · Julia Suvorova

The names of the barriers conflict with the namespaces of other projects when trying to directly include liburing.h. Avoid using popular global names.

I have applied this now. I don't think we can avoid having the barriers in the namespace, as we do need them in various macros.

Thanks. I've sent a follow-up patch to cover arm barriers too.

Best regards, Julia Suvorova.