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