blob: 997c56d32ee6c9ef6674bb2f90a47ea2407259b4 [file] [log] [blame]
Jens Axboe453b3292022-05-24 21:43:10 -06001// 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 Axboec98817e2022-05-26 09:44:31 -06007#include <linux/nospec.h>
Jens Axboe453b3292022-05-24 21:43:10 -06008#include <linux/io_uring.h>
9
10#include <uapi/linux/io_uring.h>
11
Jens Axboe453b3292022-05-24 21:43:10 -060012#include "io_uring.h"
Jens Axboec98817e2022-05-26 09:44:31 -060013#include "rsrc.h"
14#include "filetable.h"
Jens Axboe453b3292022-05-24 21:43:10 -060015
Jens Axboec98817e2022-05-26 09:44:31 -060016static int io_file_bitmap_get(struct io_ring_ctx *ctx)
Jens Axboe453b3292022-05-24 21:43:10 -060017{
18 struct io_file_table *table = &ctx->file_table;
Pavel Begunkov6e73dff2022-06-25 11:55:38 +010019 unsigned long nr = ctx->file_alloc_end;
Jens Axboe453b3292022-05-24 21:43:10 -060020 int ret;
21
Savino Dicanosa02a4d922023-03-21 19:44:02 +000022 if (!table->bitmap)
23 return -ENFILE;
24
Jens Axboe453b3292022-05-24 21:43:10 -060025 do {
26 ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);
27 if (ret != nr)
28 return ret;
29
Pavel Begunkov6e73dff2022-06-25 11:55:38 +010030 if (table->alloc_hint == ctx->file_alloc_start)
Jens Axboe453b3292022-05-24 21:43:10 -060031 break;
Jens Axboe453b3292022-05-24 21:43:10 -060032 nr = table->alloc_hint;
Pavel Begunkov6e73dff2022-06-25 11:55:38 +010033 table->alloc_hint = ctx->file_alloc_start;
Jens Axboe453b3292022-05-24 21:43:10 -060034 } while (1);
35
36 return -ENFILE;
37}
38
39bool 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
55void 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 Axboec98817e2022-05-26 09:44:31 -060062
Jens Axboef110ed82022-06-13 04:42:56 -060063static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
64 u32 slot_index)
Jens Axboec98817e2022-05-26 09:44:31 -060065 __must_hold(&req->ctx->uring_lock)
66{
Jens Axboec98817e2022-05-26 09:44:31 -060067 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 Hellwig4bfb0c92023-06-20 13:32:35 +020081 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
82 io_slot_file(file_slot));
Jens Axboec98817e2022-05-26 09:44:31 -060083 if (ret)
Pavel Begunkovc87fd582023-04-13 15:28:13 +010084 return ret;
85
Jens Axboec98817e2022-05-26 09:44:31 -060086 file_slot->file_ptr = 0;
Jens Axboe340f6342024-05-07 15:09:02 -060087 } else {
88 io_file_bitmap_set(&ctx->file_table, slot_index);
Jens Axboec98817e2022-05-26 09:44:31 -060089 }
90
Jens Axboe6e5e6d22023-12-19 12:36:34 -070091 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
92 io_fixed_file_set(file_slot, file);
Jens Axboe6e5e6d22023-12-19 12:36:34 -070093 return 0;
Jens Axboec98817e2022-05-26 09:44:31 -060094}
95
Jens Axboef110ed82022-06-13 04:42:56 -060096int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
97 unsigned int file_slot)
98{
99 bool alloc_slot = file_slot == IORING_FILE_INDEX_ALLOC;
100 int ret;
101
102 if (alloc_slot) {
103 ret = io_file_bitmap_get(ctx);
104 if (unlikely(ret < 0))
105 return ret;
106 file_slot = ret;
107 } else {
108 file_slot--;
109 }
110
111 ret = io_install_fixed_file(ctx, file, file_slot);
112 if (!ret && alloc_slot)
113 ret = file_slot;
114 return ret;
115}
Jens Axboec98817e2022-05-26 09:44:31 -0600116/*
117 * Note when io_fixed_fd_install() returns error value, it will ensure
118 * fput() is called correspondingly.
119 */
120int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
121 struct file *file, unsigned int file_slot)
122{
Jens Axboec98817e2022-05-26 09:44:31 -0600123 struct io_ring_ctx *ctx = req->ctx;
124 int ret;
125
126 io_ring_submit_lock(ctx, issue_flags);
Jens Axboef110ed82022-06-13 04:42:56 -0600127 ret = __io_fixed_fd_install(ctx, file, file_slot);
Jens Axboec98817e2022-05-26 09:44:31 -0600128 io_ring_submit_unlock(ctx, issue_flags);
Jens Axboef110ed82022-06-13 04:42:56 -0600129
Jens Axboec98817e2022-05-26 09:44:31 -0600130 if (unlikely(ret < 0))
131 fput(file);
132 return ret;
133}
Jens Axboef110ed82022-06-13 04:42:56 -0600134
135int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
136{
137 struct io_fixed_file *file_slot;
Jens Axboef110ed82022-06-13 04:42:56 -0600138 int ret;
139
140 if (unlikely(!ctx->file_data))
141 return -ENXIO;
142 if (offset >= ctx->nr_user_files)
143 return -EINVAL;
Jens Axboef110ed82022-06-13 04:42:56 -0600144
145 offset = array_index_nospec(offset, ctx->nr_user_files);
146 file_slot = io_fixed_file_slot(&ctx->file_table, offset);
147 if (!file_slot->file_ptr)
148 return -EBADF;
149
Christoph Hellwig4bfb0c92023-06-20 13:32:35 +0200150 ret = io_queue_rsrc_removal(ctx->file_data, offset,
151 io_slot_file(file_slot));
Jens Axboef110ed82022-06-13 04:42:56 -0600152 if (ret)
153 return ret;
154
155 file_slot->file_ptr = 0;
156 io_file_bitmap_clear(&ctx->file_table, offset);
Jens Axboef110ed82022-06-13 04:42:56 -0600157 return 0;
158}
Pavel Begunkov6e73dff2022-06-25 11:55:38 +0100159
160int io_register_file_alloc_range(struct io_ring_ctx *ctx,
161 struct io_uring_file_index_range __user *arg)
162{
163 struct io_uring_file_index_range range;
164 u32 end;
165
166 if (copy_from_user(&range, arg, sizeof(range)))
167 return -EFAULT;
168 if (check_add_overflow(range.off, range.len, &end))
169 return -EOVERFLOW;
170 if (range.resv || end > ctx->nr_user_files)
171 return -EINVAL;
172
173 io_file_table_set_alloc_range(ctx, range.off, range.len);
174 return 0;
175}