1 // SPDX-License-Identifier: GPL-2.0 2 #ifndef IOU_KBUF_H 3 #define IOU_KBUF_H 4 5 #include <uapi/linux/io_uring.h> 6 #include <linux/io_uring_types.h> 7 8 enum { 9 /* ring mapped provided buffers */ 10 IOBL_BUF_RING = 1, 11 /* buffers are consumed incrementally rather than always fully */ 12 IOBL_INC = 2, 13 }; 14 15 struct io_buffer_list { 16 /* 17 * If ->buf_nr_pages is set, then buf_pages/buf_ring are used. If not, 18 * then these are classic provided buffers and ->buf_list is used. 19 */ 20 union { 21 struct list_head buf_list; 22 struct io_uring_buf_ring *buf_ring; 23 }; 24 __u16 bgid; 25 26 /* below is for ring provided buffers */ 27 __u16 buf_nr_pages; 28 __u16 nr_entries; 29 __u16 head; 30 __u16 mask; 31 32 __u16 flags; 33 34 struct io_mapped_region region; 35 }; 36 37 struct io_buffer { 38 struct list_head list; 39 __u64 addr; 40 __u32 len; 41 __u16 bid; 42 __u16 bgid; 43 }; 44 45 enum { 46 /* can alloc a bigger vec */ 47 KBUF_MODE_EXPAND = 1, 48 /* if bigger vec allocated, free old one */ 49 KBUF_MODE_FREE = 2, 50 }; 51 52 struct buf_sel_arg { 53 struct iovec *iovs; 54 size_t out_len; 55 size_t max_len; 56 unsigned short nr_iovs; 57 unsigned short mode; 58 }; 59 60 void __user *io_buffer_select(struct io_kiocb *req, size_t *len, 61 unsigned int issue_flags); 62 int io_buffers_select(struct io_kiocb *req, struct buf_sel_arg *arg, 63 unsigned int issue_flags); 64 int io_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg); 65 void io_destroy_buffers(struct io_ring_ctx *ctx); 66 67 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); 68 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags); 69 70 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); 71 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags); 72 73 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg); 74 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg); 75 int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg); 76 77 void __io_put_kbuf(struct io_kiocb *req, int len, unsigned issue_flags); 78 79 bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags); 80 81 struct io_mapped_region *io_pbuf_get_region(struct io_ring_ctx *ctx, 82 unsigned int bgid); 83 84 static inline bool io_kbuf_recycle_ring(struct io_kiocb *req) 85 { 86 /* 87 * We don't need to recycle for REQ_F_BUFFER_RING, we can just clear 88 * the flag and hence ensure that bl->head doesn't get incremented. 89 * If the tail has already been incremented, hang on to it. 90 * The exception is partial io, that case we should increment bl->head 91 * to monopolize the buffer. 92 */ 93 if (req->buf_list) { 94 req->buf_index = req->buf_list->bgid; 95 req->flags &= ~(REQ_F_BUFFER_RING|REQ_F_BUFFERS_COMMIT); 96 return true; 97 } 98 return false; 99 } 100 101 static inline bool io_do_buffer_select(struct io_kiocb *req) 102 { 103 if (!(req->flags & REQ_F_BUFFER_SELECT)) 104 return false; 105 return !(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)); 106 } 107 108 static inline bool io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags) 109 { 110 if (req->flags & REQ_F_BL_NO_RECYCLE) 111 return false; 112 if (req->flags & REQ_F_BUFFER_SELECTED) 113 return io_kbuf_recycle_legacy(req, issue_flags); 114 if (req->flags & REQ_F_BUFFER_RING) 115 return io_kbuf_recycle_ring(req); 116 return false; 117 } 118 119 /* Mapped buffer ring, return io_uring_buf from head */ 120 #define io_ring_head_to_buf(br, head, mask) &(br)->bufs[(head) & (mask)] 121 122 static inline bool io_kbuf_commit(struct io_kiocb *req, 123 struct io_buffer_list *bl, int len, int nr) 124 { 125 if (unlikely(!(req->flags & REQ_F_BUFFERS_COMMIT))) 126 return true; 127 128 req->flags &= ~REQ_F_BUFFERS_COMMIT; 129 130 if (unlikely(len < 0)) 131 return true; 132 133 if (bl->flags & IOBL_INC) { 134 struct io_uring_buf *buf; 135 136 buf = io_ring_head_to_buf(bl->buf_ring, bl->head, bl->mask); 137 if (WARN_ON_ONCE(len > buf->len)) 138 len = buf->len; 139 buf->len -= len; 140 if (buf->len) { 141 buf->addr += len; 142 return false; 143 } 144 } 145 146 bl->head += nr; 147 return true; 148 } 149 150 static inline bool __io_put_kbuf_ring(struct io_kiocb *req, int len, int nr) 151 { 152 struct io_buffer_list *bl = req->buf_list; 153 bool ret = true; 154 155 if (bl) { 156 ret = io_kbuf_commit(req, bl, len, nr); 157 req->buf_index = bl->bgid; 158 } 159 req->flags &= ~REQ_F_BUFFER_RING; 160 return ret; 161 } 162 163 static inline void __io_put_kbuf_list(struct io_kiocb *req, int len, 164 struct list_head *list) 165 { 166 if (req->flags & REQ_F_BUFFER_RING) { 167 __io_put_kbuf_ring(req, len, 1); 168 } else { 169 req->buf_index = req->kbuf->bgid; 170 list_add(&req->kbuf->list, list); 171 req->flags &= ~REQ_F_BUFFER_SELECTED; 172 } 173 } 174 175 static inline void io_kbuf_drop(struct io_kiocb *req) 176 { 177 lockdep_assert_held(&req->ctx->completion_lock); 178 179 if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING))) 180 return; 181 182 /* len == 0 is fine here, non-ring will always drop all of it */ 183 __io_put_kbuf_list(req, 0, &req->ctx->io_buffers_comp); 184 } 185 186 static inline unsigned int __io_put_kbufs(struct io_kiocb *req, int len, 187 int nbufs, unsigned issue_flags) 188 { 189 unsigned int ret; 190 191 if (!(req->flags & (REQ_F_BUFFER_RING | REQ_F_BUFFER_SELECTED))) 192 return 0; 193 194 ret = IORING_CQE_F_BUFFER | (req->buf_index << IORING_CQE_BUFFER_SHIFT); 195 if (req->flags & REQ_F_BUFFER_RING) { 196 if (!__io_put_kbuf_ring(req, len, nbufs)) 197 ret |= IORING_CQE_F_BUF_MORE; 198 } else { 199 __io_put_kbuf(req, len, issue_flags); 200 } 201 return ret; 202 } 203 204 static inline unsigned int io_put_kbuf(struct io_kiocb *req, int len, 205 unsigned issue_flags) 206 { 207 return __io_put_kbufs(req, len, 1, issue_flags); 208 } 209 210 static inline unsigned int io_put_kbufs(struct io_kiocb *req, int len, 211 int nbufs, unsigned issue_flags) 212 { 213 return __io_put_kbufs(req, len, nbufs, issue_flags); 214 } 215 #endif 216