rsrc.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifndef IOU_RSRC_H
  3. #define IOU_RSRC_H
  4. #define IO_NODE_ALLOC_CACHE_MAX 32
  5. #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
  6. #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT)
  7. #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
  8. enum {
  9. IORING_RSRC_FILE = 0,
  10. IORING_RSRC_BUFFER = 1,
  11. };
  12. struct io_rsrc_put {
  13. u64 tag;
  14. union {
  15. void *rsrc;
  16. struct file *file;
  17. struct io_mapped_ubuf *buf;
  18. };
  19. };
  20. struct io_rsrc_data {
  21. struct io_ring_ctx *ctx;
  22. u64 **tags;
  23. unsigned int nr;
  24. u16 rsrc_type;
  25. bool quiesce;
  26. };
  27. struct io_rsrc_node {
  28. struct io_ring_ctx *ctx;
  29. int refs;
  30. bool empty;
  31. u16 type;
  32. struct list_head node;
  33. struct io_rsrc_put item;
  34. };
  35. struct io_mapped_ubuf {
  36. u64 ubuf;
  37. unsigned int len;
  38. unsigned int nr_bvecs;
  39. unsigned int folio_shift;
  40. refcount_t refs;
  41. unsigned long acct_pages;
  42. struct bio_vec bvec[] __counted_by(nr_bvecs);
  43. };
  44. struct io_imu_folio_data {
  45. /* Head folio can be partially included in the fixed buf */
  46. unsigned int nr_pages_head;
  47. /* For non-head/tail folios, has to be fully included */
  48. unsigned int nr_pages_mid;
  49. unsigned int folio_shift;
  50. unsigned long first_folio_page_idx;
  51. };
  52. void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
  53. void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
  54. struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx);
  55. int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, void *rsrc);
  56. int io_import_fixed(int ddir, struct iov_iter *iter,
  57. struct io_mapped_ubuf *imu,
  58. u64 buf_addr, size_t len);
  59. int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg);
  60. void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
  61. int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
  62. int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
  63. unsigned int nr_args, u64 __user *tags);
  64. void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
  65. int io_sqe_files_unregister(struct io_ring_ctx *ctx);
  66. int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
  67. unsigned nr_args, u64 __user *tags);
  68. int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
  69. unsigned nr_args);
  70. int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
  71. unsigned size, unsigned type);
  72. int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
  73. unsigned int size, unsigned int type);
  74. static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
  75. {
  76. lockdep_assert_held(&ctx->uring_lock);
  77. if (node && !--node->refs)
  78. io_rsrc_node_ref_zero(node);
  79. }
  80. static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx,
  81. struct io_rsrc_node *node)
  82. {
  83. node->refs++;
  84. }
  85. static inline void __io_req_set_rsrc_node(struct io_kiocb *req,
  86. struct io_ring_ctx *ctx)
  87. {
  88. lockdep_assert_held(&ctx->uring_lock);
  89. req->rsrc_node = ctx->rsrc_node;
  90. io_charge_rsrc_node(ctx, ctx->rsrc_node);
  91. }
  92. static inline void io_req_set_rsrc_node(struct io_kiocb *req,
  93. struct io_ring_ctx *ctx,
  94. unsigned int issue_flags)
  95. {
  96. if (!req->rsrc_node) {
  97. io_ring_submit_lock(ctx, issue_flags);
  98. __io_req_set_rsrc_node(req, ctx);
  99. io_ring_submit_unlock(ctx, issue_flags);
  100. }
  101. }
  102. static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
  103. {
  104. unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
  105. unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
  106. return &data->tags[table_idx][off];
  107. }
  108. static inline int io_rsrc_init(struct io_ring_ctx *ctx)
  109. {
  110. ctx->rsrc_node = io_rsrc_node_alloc(ctx);
  111. return ctx->rsrc_node ? 0 : -ENOMEM;
  112. }
  113. int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
  114. int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
  115. int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
  116. static inline void __io_unaccount_mem(struct user_struct *user,
  117. unsigned long nr_pages)
  118. {
  119. atomic_long_sub(nr_pages, &user->locked_vm);
  120. }
  121. #endif