Linux Audio

Check our new training course

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