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