]> git.itanic.dy.fi Git - linux-stable/commitdiff
io_uring/kbuf: add method for returning provided buffer ring head
authorJens Axboe <axboe@kernel.dk>
Thu, 21 Dec 2023 16:02:57 +0000 (09:02 -0700)
committerJens Axboe <axboe@kernel.dk>
Thu, 21 Dec 2023 16:47:06 +0000 (09:47 -0700)
The tail of the provided ring buffer is shared between the kernel and
the application, but the head is private to the kernel as the
application doesn't need to see it. However, this also prevents the
application from knowing how many buffers the kernel has consumed.
Usually this is fine, as the information is inherently racy in that
the kernel could be consuming buffers continually, but for cleanup
purposes it may be relevant to know how many buffers are still left
in the ring.

Add IORING_REGISTER_PBUF_STATUS which will return status for a given
provided buffer ring. Right now it just returns the head, but space
is reserved for more information later in, if needed.

Link: https://github.com/axboe/liburing/discussions/1020
Signed-off-by: Jens Axboe <axboe@kernel.dk>
include/uapi/linux/io_uring.h
io_uring/kbuf.c
io_uring/kbuf.h
io_uring/register.c

index db4b913e6b39cc600db4aa792f26243aaa2b62ec..7a673b52827b160afbd9d3b3bd64d3112dc7ce7c 100644 (file)
@@ -567,6 +567,9 @@ enum {
        /* register a range of fixed file slots for automatic slot allocation */
        IORING_REGISTER_FILE_ALLOC_RANGE        = 25,
 
+       /* return status information for a buffer group */
+       IORING_REGISTER_PBUF_STATUS             = 26,
+
        /* this goes last */
        IORING_REGISTER_LAST,
 
@@ -693,6 +696,13 @@ struct io_uring_buf_reg {
        __u64   resv[3];
 };
 
+/* argument for IORING_REGISTER_PBUF_STATUS */
+struct io_uring_buf_status {
+       __u32   buf_group;      /* input */
+       __u32   head;           /* output */
+       __u32   resv[8];
+};
+
 /*
  * io_uring_restriction->opcode values
  */
index 72b6af1d2ed3b0fc7cf629608173f750cb487247..18df5a9d2f5e7defb2df04f548d9e67b737ddbaf 100644 (file)
@@ -750,6 +750,32 @@ int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
        return 0;
 }
 
+int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg)
+{
+       struct io_uring_buf_status buf_status;
+       struct io_buffer_list *bl;
+       int i;
+
+       if (copy_from_user(&buf_status, arg, sizeof(buf_status)))
+               return -EFAULT;
+
+       for (i = 0; i < ARRAY_SIZE(buf_status.resv); i++)
+               if (buf_status.resv[i])
+                       return -EINVAL;
+
+       bl = io_buffer_get_list(ctx, buf_status.buf_group);
+       if (!bl)
+               return -ENOENT;
+       if (!bl->is_mapped)
+               return -EINVAL;
+
+       buf_status.head = bl->head;
+       if (copy_to_user(arg, &buf_status, sizeof(buf_status)))
+               return -EFAULT;
+
+       return 0;
+}
+
 void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid)
 {
        struct io_buffer_list *bl;
index 9be5960817ea1c8b64fc4591e33d046f0c664c31..53dfaa71a397cedd9b52923a3f0178873aaf9960 100644 (file)
@@ -53,6 +53,7 @@ int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags);
 
 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
+int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg);
 
 void io_kbuf_mmap_list_free(struct io_ring_ctx *ctx);
 
index a4286029e920c6a31d2b3b787102a84215416b20..708dd1d89add4ab09ac4dd90a4a3bbaba410ea4d 100644 (file)
@@ -542,6 +542,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
                        break;
                ret = io_register_file_alloc_range(ctx, arg);
                break;
+       case IORING_REGISTER_PBUF_STATUS:
+               ret = -EINVAL;
+               if (!arg || nr_args != 1)
+                       break;
+               ret = io_register_pbuf_status(ctx, arg);
+               break;
        default:
                ret = -EINVAL;
                break;