rsrc.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifndef IOU_RSRC_H
  3. #define IOU_RSRC_H
  4. #include <linux/io_uring_types.h>
  5. #include <linux/lockdep.h>
  6. #define IO_VEC_CACHE_SOFT_CAP 256
  7. enum {
  8. IORING_RSRC_FILE = 0,
  9. IORING_RSRC_BUFFER = 1,
  10. };
  11. struct io_rsrc_node {
  12. unsigned char type;
  13. int refs;
  14. u64 tag;
  15. union {
  16. unsigned long file_ptr;
  17. struct io_mapped_ubuf *buf;
  18. };
  19. };
  20. enum {
  21. IO_IMU_DEST = 1 << ITER_DEST,
  22. IO_IMU_SOURCE = 1 << ITER_SOURCE,
  23. };
  24. enum {
  25. IO_REGBUF_F_KBUF = 1,
  26. };
  27. struct io_mapped_ubuf {
  28. u64 ubuf;
  29. unsigned int len;
  30. unsigned int nr_bvecs;
  31. unsigned int folio_shift;
  32. refcount_t refs;
  33. unsigned long acct_pages;
  34. void (*release)(void *);
  35. void *priv;
  36. u8 flags;
  37. u8 dir;
  38. struct bio_vec bvec[] __counted_by(nr_bvecs);
  39. };
  40. struct io_imu_folio_data {
  41. /* Head folio can be partially included in the fixed buf */
  42. unsigned int nr_pages_head;
  43. /* For non-head/tail folios, has to be fully included */
  44. unsigned int nr_pages_mid;
  45. unsigned int folio_shift;
  46. unsigned int nr_folios;
  47. unsigned long first_folio_page_idx;
  48. };
  49. bool io_rsrc_cache_init(struct io_ring_ctx *ctx);
  50. void io_rsrc_cache_free(struct io_ring_ctx *ctx);
  51. struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx, int type);
  52. void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node);
  53. void io_rsrc_data_free(struct io_ring_ctx *ctx, struct io_rsrc_data *data);
  54. int io_rsrc_data_alloc(struct io_rsrc_data *data, unsigned nr);
  55. struct io_rsrc_node *io_find_buf_node(struct io_kiocb *req,
  56. unsigned issue_flags);
  57. int io_import_reg_buf(struct io_kiocb *req, struct iov_iter *iter,
  58. u64 buf_addr, size_t len, int ddir,
  59. unsigned issue_flags);
  60. int io_import_reg_vec(int ddir, struct iov_iter *iter,
  61. struct io_kiocb *req, struct iou_vec *vec,
  62. unsigned nr_iovs, unsigned issue_flags);
  63. int io_prep_reg_iovec(struct io_kiocb *req, struct iou_vec *iv,
  64. const struct iovec __user *uvec, size_t uvec_segs);
  65. int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg);
  66. int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
  67. int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
  68. unsigned int nr_args, u64 __user *tags);
  69. int io_sqe_files_unregister(struct io_ring_ctx *ctx);
  70. int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
  71. unsigned nr_args, u64 __user *tags);
  72. int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
  73. unsigned nr_args);
  74. int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
  75. unsigned size, unsigned type);
  76. int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
  77. unsigned int size, unsigned int type);
  78. int io_validate_user_buf_range(u64 uaddr, u64 ulen);
  79. bool io_check_coalesce_buffer(struct page **page_array, int nr_pages,
  80. struct io_imu_folio_data *data);
  81. static inline struct io_rsrc_node *io_rsrc_node_lookup(struct io_rsrc_data *data,
  82. unsigned int index)
  83. {
  84. if (index < data->nr)
  85. return data->nodes[array_index_nospec(index, data->nr)];
  86. return NULL;
  87. }
  88. static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
  89. {
  90. lockdep_assert_held(&ctx->uring_lock);
  91. if (!--node->refs)
  92. io_free_rsrc_node(ctx, node);
  93. }
  94. static inline bool io_reset_rsrc_node(struct io_ring_ctx *ctx,
  95. struct io_rsrc_data *data, int index)
  96. {
  97. struct io_rsrc_node *node = data->nodes[index];
  98. if (!node)
  99. return false;
  100. io_put_rsrc_node(ctx, node);
  101. data->nodes[index] = NULL;
  102. return true;
  103. }
  104. int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
  105. int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
  106. int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
  107. int io_account_mem(struct user_struct *user, struct mm_struct *mm_account,
  108. unsigned long nr_pages);
  109. void io_unaccount_mem(struct user_struct *user, struct mm_struct *mm_account,
  110. unsigned long nr_pages);
  111. static inline void __io_unaccount_mem(struct user_struct *user,
  112. unsigned long nr_pages)
  113. {
  114. atomic_long_sub(nr_pages, &user->locked_vm);
  115. }
  116. void io_vec_free(struct iou_vec *iv);
  117. int io_vec_realloc(struct iou_vec *iv, unsigned nr_entries);
  118. static inline void io_vec_reset_iovec(struct iou_vec *iv,
  119. struct iovec *iovec, unsigned nr)
  120. {
  121. io_vec_free(iv);
  122. iv->iovec = iovec;
  123. iv->nr = nr;
  124. }
  125. static inline void io_alloc_cache_vec_kasan(struct iou_vec *iv)
  126. {
  127. if (IS_ENABLED(CONFIG_KASAN))
  128. io_vec_free(iv);
  129. }
  130. #endif