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;
22 /* below is for ring provided buffers */
30 /* ring mapped provided buffers */
32 /* ring mapped provided buffers, but mmap'ed by application */
37 struct list_head list;
45 /* can alloc a bigger vec */
47 /* if bigger vec allocated, free old one */
59 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
60 unsigned int issue_flags);
61 int io_buffers_select(struct io_kiocb *req, struct buf_sel_arg *arg,
62 unsigned int issue_flags);
63 int io_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg);
64 void io_destroy_buffers(struct io_ring_ctx *ctx);
66 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
67 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags);
69 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
70 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags);
72 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
73 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
74 int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg);
76 void __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags);
78 bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags);
80 void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl);
81 struct io_buffer_list *io_pbuf_get_bl(struct io_ring_ctx *ctx,
83 int io_pbuf_mmap(struct file *file, struct vm_area_struct *vma);
85 static inline bool io_kbuf_recycle_ring(struct io_kiocb *req)
88 * We don't need to recycle for REQ_F_BUFFER_RING, we can just clear
89 * the flag and hence ensure that bl->head doesn't get incremented.
90 * If the tail has already been incremented, hang on to it.
91 * The exception is partial io, that case we should increment bl->head
92 * to monopolize the buffer.
95 req->buf_index = req->buf_list->bgid;
96 req->flags &= ~(REQ_F_BUFFER_RING|REQ_F_BUFFERS_COMMIT);
102 static inline bool io_do_buffer_select(struct io_kiocb *req)
104 if (!(req->flags & REQ_F_BUFFER_SELECT))
106 return !(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING));
109 static inline bool io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags)
111 if (req->flags & REQ_F_BL_NO_RECYCLE)
113 if (req->flags & REQ_F_BUFFER_SELECTED)
114 return io_kbuf_recycle_legacy(req, issue_flags);
115 if (req->flags & REQ_F_BUFFER_RING)
116 return io_kbuf_recycle_ring(req);
120 static inline void __io_put_kbuf_ring(struct io_kiocb *req, int nr)
122 struct io_buffer_list *bl = req->buf_list;
125 if (req->flags & REQ_F_BUFFERS_COMMIT) {
127 req->flags &= ~REQ_F_BUFFERS_COMMIT;
129 req->buf_index = bl->bgid;
131 req->flags &= ~REQ_F_BUFFER_RING;
134 static inline void __io_put_kbuf_list(struct io_kiocb *req,
135 struct list_head *list)
137 if (req->flags & REQ_F_BUFFER_RING) {
138 __io_put_kbuf_ring(req, 1);
140 req->buf_index = req->kbuf->bgid;
141 list_add(&req->kbuf->list, list);
142 req->flags &= ~REQ_F_BUFFER_SELECTED;
146 static inline void io_kbuf_drop(struct io_kiocb *req)
148 lockdep_assert_held(&req->ctx->completion_lock);
150 if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
153 __io_put_kbuf_list(req, &req->ctx->io_buffers_comp);
156 static inline unsigned int __io_put_kbufs(struct io_kiocb *req, int nbufs,
157 unsigned issue_flags)
161 if (!(req->flags & (REQ_F_BUFFER_RING | REQ_F_BUFFER_SELECTED)))
164 ret = IORING_CQE_F_BUFFER | (req->buf_index << IORING_CQE_BUFFER_SHIFT);
165 if (req->flags & REQ_F_BUFFER_RING)
166 __io_put_kbuf_ring(req, nbufs);
168 __io_put_kbuf(req, issue_flags);
172 static inline unsigned int io_put_kbuf(struct io_kiocb *req,
173 unsigned issue_flags)
175 return __io_put_kbufs(req, 1, issue_flags);
178 static inline unsigned int io_put_kbufs(struct io_kiocb *req, int nbufs,
179 unsigned issue_flags)
181 return __io_put_kbufs(req, nbufs, issue_flags);