filetable.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/file.h>
  5. #include <linux/mm.h>
  6. #include <linux/slab.h>
  7. #include <linux/nospec.h>
  8. #include <linux/io_uring.h>
  9. #include <uapi/linux/io_uring.h>
  10. #include "io_uring.h"
  11. #include "rsrc.h"
  12. #include "filetable.h"
  13. static int io_file_bitmap_get(struct io_ring_ctx *ctx)
  14. {
  15. struct io_file_table *table = &ctx->file_table;
  16. unsigned long nr = ctx->file_alloc_end;
  17. int ret;
  18. if (!table->bitmap)
  19. return -ENFILE;
  20. do {
  21. ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);
  22. if (ret != nr)
  23. return ret;
  24. if (table->alloc_hint == ctx->file_alloc_start)
  25. break;
  26. nr = table->alloc_hint;
  27. table->alloc_hint = ctx->file_alloc_start;
  28. } while (1);
  29. return -ENFILE;
  30. }
  31. bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
  32. {
  33. table->files = kvcalloc(nr_files, sizeof(table->files[0]),
  34. GFP_KERNEL_ACCOUNT);
  35. if (unlikely(!table->files))
  36. return false;
  37. table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);
  38. if (unlikely(!table->bitmap)) {
  39. kvfree(table->files);
  40. return false;
  41. }
  42. return true;
  43. }
  44. void io_free_file_tables(struct io_file_table *table)
  45. {
  46. kvfree(table->files);
  47. bitmap_free(table->bitmap);
  48. table->files = NULL;
  49. table->bitmap = NULL;
  50. }
  51. static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
  52. u32 slot_index)
  53. __must_hold(&req->ctx->uring_lock)
  54. {
  55. bool needs_switch = false;
  56. struct io_fixed_file *file_slot;
  57. int ret;
  58. if (io_is_uring_fops(file))
  59. return -EBADF;
  60. if (!ctx->file_data)
  61. return -ENXIO;
  62. if (slot_index >= ctx->nr_user_files)
  63. return -EINVAL;
  64. slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
  65. file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
  66. if (file_slot->file_ptr) {
  67. struct file *old_file;
  68. ret = io_rsrc_node_switch_start(ctx);
  69. if (ret)
  70. goto err;
  71. old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
  72. ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
  73. ctx->rsrc_node, old_file);
  74. if (ret)
  75. goto err;
  76. file_slot->file_ptr = 0;
  77. io_file_bitmap_clear(&ctx->file_table, slot_index);
  78. needs_switch = true;
  79. }
  80. ret = io_scm_file_account(ctx, file);
  81. if (!ret) {
  82. *io_get_tag_slot(ctx->file_data, slot_index) = 0;
  83. io_fixed_file_set(file_slot, file);
  84. io_file_bitmap_set(&ctx->file_table, slot_index);
  85. }
  86. err:
  87. if (needs_switch)
  88. io_rsrc_node_switch(ctx, ctx->file_data);
  89. return ret;
  90. }
  91. int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
  92. unsigned int file_slot)
  93. {
  94. bool alloc_slot = file_slot == IORING_FILE_INDEX_ALLOC;
  95. int ret;
  96. if (alloc_slot) {
  97. ret = io_file_bitmap_get(ctx);
  98. if (unlikely(ret < 0))
  99. return ret;
  100. file_slot = ret;
  101. } else {
  102. file_slot--;
  103. }
  104. ret = io_install_fixed_file(ctx, file, file_slot);
  105. if (!ret && alloc_slot)
  106. ret = file_slot;
  107. return ret;
  108. }
  109. /*
  110. * Note when io_fixed_fd_install() returns error value, it will ensure
  111. * fput() is called correspondingly.
  112. */
  113. int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
  114. struct file *file, unsigned int file_slot)
  115. {
  116. struct io_ring_ctx *ctx = req->ctx;
  117. int ret;
  118. io_ring_submit_lock(ctx, issue_flags);
  119. ret = __io_fixed_fd_install(ctx, file, file_slot);
  120. io_ring_submit_unlock(ctx, issue_flags);
  121. if (unlikely(ret < 0))
  122. fput(file);
  123. return ret;
  124. }
  125. int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
  126. {
  127. struct io_fixed_file *file_slot;
  128. struct file *file;
  129. int ret;
  130. if (unlikely(!ctx->file_data))
  131. return -ENXIO;
  132. if (offset >= ctx->nr_user_files)
  133. return -EINVAL;
  134. ret = io_rsrc_node_switch_start(ctx);
  135. if (ret)
  136. return ret;
  137. offset = array_index_nospec(offset, ctx->nr_user_files);
  138. file_slot = io_fixed_file_slot(&ctx->file_table, offset);
  139. if (!file_slot->file_ptr)
  140. return -EBADF;
  141. file = (struct file *)(file_slot->file_ptr & FFS_MASK);
  142. ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
  143. if (ret)
  144. return ret;
  145. file_slot->file_ptr = 0;
  146. io_file_bitmap_clear(&ctx->file_table, offset);
  147. io_rsrc_node_switch(ctx, ctx->file_data);
  148. return 0;
  149. }
  150. int io_register_file_alloc_range(struct io_ring_ctx *ctx,
  151. struct io_uring_file_index_range __user *arg)
  152. {
  153. struct io_uring_file_index_range range;
  154. u32 end;
  155. if (copy_from_user(&range, arg, sizeof(range)))
  156. return -EFAULT;
  157. if (check_add_overflow(range.off, range.len, &end))
  158. return -EOVERFLOW;
  159. if (range.resv || end > ctx->nr_user_files)
  160. return -EINVAL;
  161. io_file_table_set_alloc_range(ctx, range.off, range.len);
  162. return 0;
  163. }