]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
io_uring/kbuf: refactor __io_remove_buffers
authorPavel Begunkov <asml.silence@gmail.com>
Tue, 13 May 2025 17:26:50 +0000 (18:26 +0100)
committerJens Axboe <axboe@kernel.dk>
Tue, 13 May 2025 20:45:55 +0000 (14:45 -0600)
__io_remove_buffers used for two purposes, the first is removing
buffers for non ring based lists, which implies that it can be called
multiple times for the same list. And the second is for destroying
lists, which is not perfectly reentrable for ring based lists.

It's confusing, so just have a helper for the legacy pbuf buffer
removal, make sure it's not called for ring pbuf, and open code all ring
pbuf destruction into io_put_bl().

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/0ae416b099d311ad23f285cea02f2c94c8ae9a6c.1747150490.git.asml.silence@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
io_uring/kbuf.c

index eb666c02f488fba9b38bafe0a5daac3084a52afc..df8aeb42e910618771137fd4e06bd6dd6075e49d 100644 (file)
@@ -376,45 +376,33 @@ unsigned int __io_put_kbufs(struct io_kiocb *req, int len, int nbufs)
        return ret;
 }
 
-static int __io_remove_buffers(struct io_ring_ctx *ctx,
-                              struct io_buffer_list *bl, unsigned nbufs)
+static int io_remove_buffers_legacy(struct io_ring_ctx *ctx,
+                                   struct io_buffer_list *bl,
+                                   unsigned long nbufs)
 {
-       unsigned i = 0;
-
-       /* shouldn't happen */
-       if (!nbufs)
-               return 0;
-
-       if (bl->flags & IOBL_BUF_RING) {
-               i = bl->buf_ring->tail - bl->head;
-               io_free_region(ctx, &bl->region);
-               /* make sure it's seen as empty */
-               INIT_LIST_HEAD(&bl->buf_list);
-               bl->flags &= ~IOBL_BUF_RING;
-               return i;
-       }
+       unsigned long i = 0;
+       struct io_buffer *nxt;
 
        /* protects io_buffers_cache */
        lockdep_assert_held(&ctx->uring_lock);
+       WARN_ON_ONCE(bl->flags & IOBL_BUF_RING);
 
-       while (!list_empty(&bl->buf_list)) {
-               struct io_buffer *nxt;
-
+       for (i = 0; i < nbufs && !list_empty(&bl->buf_list); i++) {
                nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
                list_del(&nxt->list);
                kfree(nxt);
-
-               if (++i == nbufs)
-                       return i;
                cond_resched();
        }
-
        return i;
 }
 
 static void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
 {
-       __io_remove_buffers(ctx, bl, -1U);
+       if (bl->flags & IOBL_BUF_RING)
+               io_free_region(ctx, &bl->region);
+       else
+               io_remove_buffers_legacy(ctx, bl, -1U);
+
        kfree(bl);
 }
 
@@ -477,7 +465,7 @@ int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
                ret = -EINVAL;
                /* can't use provide/remove buffers command on mapped buffers */
                if (!(bl->flags & IOBL_BUF_RING))
-                       ret = __io_remove_buffers(ctx, bl, p->nbufs);
+                       ret = io_remove_buffers_legacy(ctx, bl, p->nbufs);
        }
        io_ring_submit_unlock(ctx, issue_flags);
        if (ret < 0)