rsrc.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. };
  51. void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
  52. void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
  53. struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx);
  54. int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, void *rsrc);
  55. int io_import_fixed(int ddir, struct iov_iter *iter,
  56. struct io_mapped_ubuf *imu,
  57. u64 buf_addr, size_t len);
  58. int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg);
  59. void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
  60. int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
  61. int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
  62. unsigned int nr_args, u64 __user *tags);
  63. void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
  64. int io_sqe_files_unregister(struct io_ring_ctx *ctx);
  65. int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
  66. unsigned nr_args, u64 __user *tags);
  67. int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
  68. unsigned nr_args);
  69. int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
  70. unsigned size, unsigned type);
  71. int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
  72. unsigned int size, unsigned int type);
  73. static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
  74. {
  75. lockdep_assert_held(&ctx->uring_lock);
  76. if (node && !--node->refs)
  77. io_rsrc_node_ref_zero(node);
  78. }
  79. static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx,
  80. struct io_rsrc_node *node)
  81. {
  82. node->refs++;
  83. }
  84. static inline void __io_req_set_rsrc_node(struct io_kiocb *req,
  85. struct io_ring_ctx *ctx)
  86. {
  87. lockdep_assert_held(&ctx->uring_lock);
  88. req->rsrc_node = ctx->rsrc_node;
  89. io_charge_rsrc_node(ctx, ctx->rsrc_node);
  90. }
  91. static inline void io_req_set_rsrc_node(struct io_kiocb *req,
  92. struct io_ring_ctx *ctx,
  93. unsigned int issue_flags)
  94. {
  95. if (!req->rsrc_node) {
  96. io_ring_submit_lock(ctx, issue_flags);
  97. __io_req_set_rsrc_node(req, ctx);
  98. io_ring_submit_unlock(ctx, issue_flags);
  99. }
  100. }
  101. static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
  102. {
  103. unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
  104. unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
  105. return &data->tags[table_idx][off];
  106. }
  107. static inline int io_rsrc_init(struct io_ring_ctx *ctx)
  108. {
  109. ctx->rsrc_node = io_rsrc_node_alloc(ctx);
  110. return ctx->rsrc_node ? 0 : -ENOMEM;
  111. }
  112. int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
  113. int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
  114. int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
  115. static inline void __io_unaccount_mem(struct user_struct *user,
  116. unsigned long nr_pages)
  117. {
  118. atomic_long_sub(nr_pages, &user->locked_vm);
  119. }
  120. #endif