kbuf.h 5.7 KB

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