diff mbox series

[liburing,2/4] add mask parameter to io_uring_buf_ring_add

Message ID 20220613131253.974205-3-dylany@fb.com (mailing list archive)
State New
Headers show
Series Buffer ring API fixes | expand

Commit Message

Dylan Yudaken June 13, 2022, 1:12 p.m. UTC
Without the mask parameter, it's not feasible to use this API without
knowing where the tail is and performing some arithmetic

Signed-off-by: Dylan Yudaken <dylany@fb.com>
---
 man/io_uring_buf_ring_add.3  |  5 +++++
 man/io_uring_buf_ring_mask.3 | 27 +++++++++++++++++++++++++++
 src/include/liburing.h       | 13 +++++++++++--
 test/send_recvmsg.c          |  3 ++-
 4 files changed, 45 insertions(+), 3 deletions(-)
 create mode 100644 man/io_uring_buf_ring_mask.3
diff mbox series

Patch

diff --git a/man/io_uring_buf_ring_add.3 b/man/io_uring_buf_ring_add.3
index 741cba6..9d8283b 100644
--- a/man/io_uring_buf_ring_add.3
+++ b/man/io_uring_buf_ring_add.3
@@ -13,6 +13,7 @@  io_uring_buf_ring_add \- add buffers to a shared buffer ring
 .BI "                          void *" addr ",
 .BI "                          unsigned int " len ",
 .BI "                          unsigned short " bid ",
+.BI "                          int " mask ",
 .BI "                          int " buf_offset ");"
 .fi
 .SH DESCRIPTION
@@ -28,6 +29,9 @@  and is of
 bytes of length.
 .I bid
 is the buffer ID, which will be returned in the CQE.
+.I mask
+is the size mask of the ring, available from
+.BR io_uring_buf_ring_mask (3) .
 .I buf_offset
 is the offset to insert at from the current tail. If just one buffer is provided
 before the ring tail is committed with
@@ -44,5 +48,6 @@  must be incremented by one for each buffer added.
 None
 .SH SEE ALSO
 .BR io_uring_register_buf_ring (3),
+.BR io_uring_buf_ring_mask (3),
 .BR io_uring_buf_ring_advance (3),
 .BR io_uring_buf_ring_cq_advance (3)
diff --git a/man/io_uring_buf_ring_mask.3 b/man/io_uring_buf_ring_mask.3
new file mode 100644
index 0000000..9160663
--- /dev/null
+++ b/man/io_uring_buf_ring_mask.3
@@ -0,0 +1,27 @@ 
+.\" Copyright (C) 2022 Dylan Yudaken <dylany@fb.com>
+.\"
+.\" SPDX-License-Identifier: LGPL-2.0-or-later
+.\"
+.TH io_uring_buf_ring_mask 3 "June 13, 2022" "liburing-2.2" "liburing Manual"
+.SH NAME
+io_uring_buf_ring_mask \- Calculate buffer ring mask size
+.SH SYNOPSIS
+.nf
+.B #include <liburing.h>
+.PP
+.BI "int io_uring_buf_ring_mask(__u32 " ring_entries ");"
+.fi
+.SH DESCRIPTION
+.PP
+.BR io_uring_buf_ring_mask (3)
+calculates the appropriate size mask for a buffer ring.
+.IR ring_entries
+is the ring entries as specified in
+.BR io_uring_register_buf_ring (3) .
+
+.SH RETURN VALUE
+Size mask for the buffer ring.
+
+.SH SEE ALSO
+.BR io_uring_register_buf_ring (3),
+.BR io_uring_buf_ring_add (3)
diff --git a/src/include/liburing.h b/src/include/liburing.h
index 6eece30..9beef0b 100644
--- a/src/include/liburing.h
+++ b/src/include/liburing.h
@@ -1089,14 +1089,23 @@  static inline struct io_uring_sqe *_io_uring_get_sqe(struct io_uring *ring)
 	return NULL;
 }
 
+/*
+ * Return the appropriate mask for a buffer ring of size 'ring_entries'
+ */
+static inline int io_uring_buf_ring_mask(__u32 ring_entries)
+{
+	return ring_entries - 1;
+}
+
 /*
  * Assign 'buf' with the addr/len/buffer ID supplied
  */
 static inline void io_uring_buf_ring_add(struct io_uring_buf_ring *br,
 					 void *addr, unsigned int len,
-					 unsigned short bid, int buf_offset)
+					 unsigned short bid, int mask,
+					 int buf_offset)
 {
-	struct io_uring_buf *buf = &br->bufs[br->tail + buf_offset];
+	struct io_uring_buf *buf = &br->bufs[(br->tail + buf_offset) & mask];
 
 	buf->addr = (unsigned long) (uintptr_t) addr;
 	buf->len = len;
diff --git a/test/send_recvmsg.c b/test/send_recvmsg.c
index 44a01b0..6f18bae 100644
--- a/test/send_recvmsg.c
+++ b/test/send_recvmsg.c
@@ -199,7 +199,8 @@  static void *recv_fn(void *data)
 			}
 
 			br = ptr;
-			io_uring_buf_ring_add(br, buf, sizeof(buf), BUF_BID, 0);
+			io_uring_buf_ring_add(br, buf, sizeof(buf), BUF_BID,
+					      io_uring_buf_ring_mask(1), 0);
 			io_uring_buf_ring_advance(br, 1);
 		} else {
 			struct io_uring_sqe *sqe;