kbuf.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. };
  55. void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
  56. unsigned int issue_flags);
  57. int io_buffers_select(struct io_kiocb *req, struct buf_sel_arg *arg,
  58. unsigned int issue_flags);
  59. int io_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg);
  60. void io_destroy_buffers(struct io_ring_ctx *ctx);
  61. int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
  62. int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags);
  63. int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
  64. int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags);
  65. int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
  66. int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
  67. int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg);
  68. void __io_put_kbuf(struct io_kiocb *req, int len, unsigned issue_flags);
  69. bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags);
  70. void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl);
  71. struct io_buffer_list *io_pbuf_get_bl(struct io_ring_ctx *ctx,
  72. unsigned long bgid);
  73. int io_pbuf_mmap(struct file *file, struct vm_area_struct *vma);
  74. static inline bool io_kbuf_recycle_ring(struct io_kiocb *req)
  75. {
  76. /*
  77. * We don't need to recycle for REQ_F_BUFFER_RING, we can just clear
  78. * the flag and hence ensure that bl->head doesn't get incremented.
  79. * If the tail has already been incremented, hang on to it.
  80. * The exception is partial io, that case we should increment bl->head
  81. * to monopolize the buffer.
  82. */
  83. if (req->buf_list) {
  84. req->buf_index = req->buf_list->bgid;
  85. req->flags &= ~(REQ_F_BUFFER_RING|REQ_F_BUFFERS_COMMIT);
  86. return true;
  87. }
  88. return false;
  89. }
  90. static inline bool io_do_buffer_select(struct io_kiocb *req)
  91. {
  92. if (!(req->flags & REQ_F_BUFFER_SELECT))
  93. return false;
  94. return !(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING));
  95. }
  96. static inline bool io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags)
  97. {
  98. if (req->flags & REQ_F_BL_NO_RECYCLE)
  99. return false;
  100. if (req->flags & REQ_F_BUFFER_SELECTED)
  101. return io_kbuf_recycle_legacy(req, issue_flags);
  102. if (req->flags & REQ_F_BUFFER_RING)
  103. return io_kbuf_recycle_ring(req);
  104. return false;
  105. }
  106. /* Mapped buffer ring, return io_uring_buf from head */
  107. #define io_ring_head_to_buf(br, head, mask) &(br)->bufs[(head) & (mask)]
  108. static inline bool io_kbuf_commit(struct io_kiocb *req,
  109. struct io_buffer_list *bl, int len, int nr)
  110. {
  111. if (unlikely(!(req->flags & REQ_F_BUFFERS_COMMIT)))
  112. return true;
  113. req->flags &= ~REQ_F_BUFFERS_COMMIT;
  114. if (unlikely(len < 0))
  115. return true;
  116. if (bl->flags & IOBL_INC) {
  117. struct io_uring_buf *buf;
  118. buf = io_ring_head_to_buf(bl->buf_ring, bl->head, bl->mask);
  119. if (WARN_ON_ONCE(len > buf->len))
  120. len = buf->len;
  121. buf->len -= len;
  122. if (buf->len) {
  123. buf->addr += len;
  124. return false;
  125. }
  126. }
  127. bl->head += nr;
  128. return true;
  129. }
  130. static inline bool __io_put_kbuf_ring(struct io_kiocb *req, int len, int nr)
  131. {
  132. struct io_buffer_list *bl = req->buf_list;
  133. bool ret = true;
  134. if (bl) {
  135. ret = io_kbuf_commit(req, bl, len, nr);
  136. req->buf_index = bl->bgid;
  137. }
  138. req->flags &= ~REQ_F_BUFFER_RING;
  139. return ret;
  140. }
  141. static inline void __io_put_kbuf_list(struct io_kiocb *req, int len,
  142. struct list_head *list)
  143. {
  144. if (req->flags & REQ_F_BUFFER_RING) {
  145. __io_put_kbuf_ring(req, len, 1);
  146. } else {
  147. req->buf_index = req->kbuf->bgid;
  148. list_add(&req->kbuf->list, list);
  149. req->flags &= ~REQ_F_BUFFER_SELECTED;
  150. }
  151. }
  152. static inline void io_kbuf_drop(struct io_kiocb *req)
  153. {
  154. lockdep_assert_held(&req->ctx->completion_lock);
  155. if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
  156. return;
  157. /* len == 0 is fine here, non-ring will always drop all of it */
  158. __io_put_kbuf_list(req, 0, &req->ctx->io_buffers_comp);
  159. }
  160. static inline unsigned int __io_put_kbufs(struct io_kiocb *req, int len,
  161. int nbufs, unsigned issue_flags)
  162. {
  163. unsigned int ret;
  164. if (!(req->flags & (REQ_F_BUFFER_RING | REQ_F_BUFFER_SELECTED)))
  165. return 0;
  166. ret = IORING_CQE_F_BUFFER | (req->buf_index << IORING_CQE_BUFFER_SHIFT);
  167. if (req->flags & REQ_F_BUFFER_RING) {
  168. if (!__io_put_kbuf_ring(req, len, nbufs))
  169. ret |= IORING_CQE_F_BUF_MORE;
  170. } else {
  171. __io_put_kbuf(req, len, issue_flags);
  172. }
  173. return ret;
  174. }
  175. static inline unsigned int io_put_kbuf(struct io_kiocb *req, int len,
  176. unsigned issue_flags)
  177. {
  178. return __io_put_kbufs(req, len, 1, issue_flags);
  179. }
  180. static inline unsigned int io_put_kbufs(struct io_kiocb *req, int len,
  181. int nbufs, unsigned issue_flags)
  182. {
  183. return __io_put_kbufs(req, len, nbufs, issue_flags);
  184. }
  185. #endif