filetable.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. struct io_fixed_file *file_slot;
  56. int ret;
  57. if (io_is_uring_fops(file))
  58. return -EBADF;
  59. if (!ctx->file_data)
  60. return -ENXIO;
  61. if (slot_index >= ctx->nr_user_files)
  62. return -EINVAL;
  63. slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
  64. file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
  65. if (file_slot->file_ptr) {
  66. ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
  67. io_slot_file(file_slot));
  68. if (ret)
  69. return ret;
  70. file_slot->file_ptr = 0;
  71. } else {
  72. io_file_bitmap_set(&ctx->file_table, slot_index);
  73. }
  74. *io_get_tag_slot(ctx->file_data, slot_index) = 0;
  75. io_fixed_file_set(file_slot, file);
  76. return 0;
  77. }
  78. int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
  79. unsigned int file_slot)
  80. {
  81. bool alloc_slot = file_slot == IORING_FILE_INDEX_ALLOC;
  82. int ret;
  83. if (alloc_slot) {
  84. ret = io_file_bitmap_get(ctx);
  85. if (unlikely(ret < 0))
  86. return ret;
  87. file_slot = ret;
  88. } else {
  89. file_slot--;
  90. }
  91. ret = io_install_fixed_file(ctx, file, file_slot);
  92. if (!ret && alloc_slot)
  93. ret = file_slot;
  94. return ret;
  95. }
  96. /*
  97. * Note when io_fixed_fd_install() returns error value, it will ensure
  98. * fput() is called correspondingly.
  99. */
  100. int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
  101. struct file *file, unsigned int file_slot)
  102. {
  103. struct io_ring_ctx *ctx = req->ctx;
  104. int ret;
  105. io_ring_submit_lock(ctx, issue_flags);
  106. ret = __io_fixed_fd_install(ctx, file, file_slot);
  107. io_ring_submit_unlock(ctx, issue_flags);
  108. if (unlikely(ret < 0))
  109. fput(file);
  110. return ret;
  111. }
  112. int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
  113. {
  114. struct io_fixed_file *file_slot;
  115. int ret;
  116. if (unlikely(!ctx->file_data))
  117. return -ENXIO;
  118. if (offset >= ctx->nr_user_files)
  119. return -EINVAL;
  120. offset = array_index_nospec(offset, ctx->nr_user_files);
  121. file_slot = io_fixed_file_slot(&ctx->file_table, offset);
  122. if (!file_slot->file_ptr)
  123. return -EBADF;
  124. ret = io_queue_rsrc_removal(ctx->file_data, offset,
  125. io_slot_file(file_slot));
  126. if (ret)
  127. return ret;
  128. file_slot->file_ptr = 0;
  129. io_file_bitmap_clear(&ctx->file_table, offset);
  130. return 0;
  131. }
  132. int io_register_file_alloc_range(struct io_ring_ctx *ctx,
  133. struct io_uring_file_index_range __user *arg)
  134. {
  135. struct io_uring_file_index_range range;
  136. u32 end;
  137. if (copy_from_user(&range, arg, sizeof(range)))
  138. return -EFAULT;
  139. if (check_add_overflow(range.off, range.len, &end))
  140. return -EOVERFLOW;
  141. if (range.resv || end > ctx->nr_user_files)
  142. return -EINVAL;
  143. io_file_table_set_alloc_range(ctx, range.off, range.len);
  144. return 0;
  145. }