kbuf.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <linux/io_uring_types.h>
  6. enum {
  7. /* ring mapped provided buffers */
  8. IOBL_BUF_RING = 1,
  9. /* buffers are consumed incrementally rather than always fully */
  10. IOBL_INC = 2,
  11. };
  12. struct io_buffer_list {
  13. /*
  14. * If the IOBL_BUF_RING flag is set, then buf_ring is used. If not, then
  15. * these are classic provided buffers and ->buf_list is used.
  16. */
  17. union {
  18. struct list_head buf_list;
  19. struct io_uring_buf_ring *buf_ring;
  20. };
  21. /* count of classic/legacy buffers in buffer list */
  22. int nbufs;
  23. __u16 bgid;
  24. /* below is for ring provided buffers */
  25. __u16 nr_entries;
  26. __u16 head;
  27. __u16 mask;
  28. __u16 flags;
  29. struct io_mapped_region region;
  30. };
  31. struct io_buffer {
  32. struct list_head list;
  33. __u64 addr;
  34. __u32 len;
  35. __u16 bid;
  36. __u16 bgid;
  37. };
  38. enum {
  39. /* can alloc a bigger vec */
  40. KBUF_MODE_EXPAND = 1,
  41. /* if bigger vec allocated, free old one */
  42. KBUF_MODE_FREE = 2,
  43. };
  44. struct buf_sel_arg {
  45. struct iovec *iovs;
  46. size_t out_len;
  47. size_t max_len;
  48. unsigned short nr_iovs;
  49. unsigned short mode;
  50. unsigned short buf_group;
  51. unsigned short partial_map;
  52. };
  53. struct io_br_sel io_buffer_select(struct io_kiocb *req, size_t *len,
  54. unsigned buf_group, unsigned int issue_flags);
  55. int io_buffers_select(struct io_kiocb *req, struct buf_sel_arg *arg,
  56. struct io_br_sel *sel, unsigned int issue_flags);
  57. int io_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg,
  58. struct io_br_sel *sel);
  59. void io_destroy_buffers(struct io_ring_ctx *ctx);
  60. int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
  61. int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
  62. int io_manage_buffers_legacy(struct io_kiocb *req, unsigned int issue_flags);
  63. int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
  64. int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
  65. int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg);
  66. bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags);
  67. void io_kbuf_drop_legacy(struct io_kiocb *req);
  68. unsigned int __io_put_kbufs(struct io_kiocb *req, struct io_buffer_list *bl,
  69. int len, int nbufs);
  70. bool io_kbuf_commit(struct io_kiocb *req,
  71. struct io_buffer_list *bl, int len, int nr);
  72. struct io_mapped_region *io_pbuf_get_region(struct io_ring_ctx *ctx,
  73. unsigned int bgid);
  74. static inline bool io_kbuf_recycle_ring(struct io_kiocb *req,
  75. struct io_buffer_list *bl)
  76. {
  77. if (bl) {
  78. req->flags &= ~(REQ_F_BUFFER_RING|REQ_F_BUFFERS_COMMIT);
  79. return true;
  80. }
  81. return false;
  82. }
  83. static inline bool io_do_buffer_select(struct io_kiocb *req)
  84. {
  85. if (!(req->flags & REQ_F_BUFFER_SELECT))
  86. return false;
  87. return !(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING));
  88. }
  89. static inline bool io_kbuf_recycle(struct io_kiocb *req, struct io_buffer_list *bl,
  90. unsigned issue_flags)
  91. {
  92. if (req->flags & REQ_F_BL_NO_RECYCLE)
  93. return false;
  94. if (req->flags & REQ_F_BUFFER_RING)
  95. return io_kbuf_recycle_ring(req, bl);
  96. if (req->flags & REQ_F_BUFFER_SELECTED)
  97. return io_kbuf_recycle_legacy(req, issue_flags);
  98. return false;
  99. }
  100. static inline unsigned int io_put_kbuf(struct io_kiocb *req, int len,
  101. struct io_buffer_list *bl)
  102. {
  103. if (!(req->flags & (REQ_F_BUFFER_RING | REQ_F_BUFFER_SELECTED)))
  104. return 0;
  105. return __io_put_kbufs(req, bl, len, 1);
  106. }
  107. static inline unsigned int io_put_kbufs(struct io_kiocb *req, int len,
  108. struct io_buffer_list *bl, int nbufs)
  109. {
  110. if (!(req->flags & (REQ_F_BUFFER_RING | REQ_F_BUFFER_SELECTED)))
  111. return 0;
  112. return __io_put_kbufs(req, bl, len, nbufs);
  113. }
  114. #endif