1 // SPDX-License-Identifier: GPL-2.0
5 #include <uapi/linux/io_uring.h>
7 struct io_buffer_list {
9 * If ->buf_nr_pages is set, then buf_pages/buf_ring are used. If not,
10 * then these are classic provided buffers and ->buf_list is used.
13 struct list_head buf_list;
15 struct page **buf_pages;
16 struct io_uring_buf_ring *buf_ring;
21 /* below is for ring provided buffers */
29 struct list_head list;
36 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
37 unsigned int issue_flags);
38 void __io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags);
39 void io_destroy_buffers(struct io_ring_ctx *ctx);
41 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
42 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags);
44 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
45 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags);
47 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
48 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
50 unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags);
52 static inline bool io_do_buffer_select(struct io_kiocb *req)
54 if (!(req->flags & REQ_F_BUFFER_SELECT))
56 return !(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING));
59 static inline void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags)
61 if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
64 * For legacy provided buffer mode, don't recycle if we already did
65 * IO to this buffer. For ring-mapped provided buffer mode, we should
66 * increment ring->head to explicitly monopolize the buffer to avoid
69 if ((req->flags & REQ_F_BUFFER_SELECTED) &&
70 (req->flags & REQ_F_PARTIAL_IO))
74 * READV uses fields in `struct io_rw` (len/addr) to stash the selected
75 * buffer data. However if that buffer is recycled the original request
76 * data stored in addr is lost. Therefore forbid recycling for now.
78 if (req->opcode == IORING_OP_READV)
81 __io_kbuf_recycle(req, issue_flags);
84 static inline unsigned int __io_put_kbuf_list(struct io_kiocb *req,
85 struct list_head *list)
87 if (req->flags & REQ_F_BUFFER_RING) {
89 req->buf_list->head++;
90 req->flags &= ~REQ_F_BUFFER_RING;
92 list_add(&req->kbuf->list, list);
93 req->flags &= ~REQ_F_BUFFER_SELECTED;
96 return IORING_CQE_F_BUFFER | (req->buf_index << IORING_CQE_BUFFER_SHIFT);
99 static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req)
101 lockdep_assert_held(&req->ctx->completion_lock);
103 if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
105 return __io_put_kbuf_list(req, &req->ctx->io_buffers_comp);
108 static inline unsigned int io_put_kbuf(struct io_kiocb *req,
109 unsigned issue_flags)
112 if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
114 return __io_put_kbuf(req, issue_flags);