2022-05-25 03:43:10 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/slab.h>
|
2022-05-26 15:44:31 +00:00
|
|
|
#include <linux/nospec.h>
|
2022-05-25 03:43:10 +00:00
|
|
|
#include <linux/io_uring.h>
|
|
|
|
|
|
|
|
#include <uapi/linux/io_uring.h>
|
|
|
|
|
|
|
|
#include "io_uring.h"
|
2022-05-26 15:44:31 +00:00
|
|
|
#include "rsrc.h"
|
|
|
|
#include "filetable.h"
|
2022-05-25 03:43:10 +00:00
|
|
|
|
2022-05-26 15:44:31 +00:00
|
|
|
static int io_file_bitmap_get(struct io_ring_ctx *ctx)
|
2022-05-25 03:43:10 +00:00
|
|
|
{
|
|
|
|
struct io_file_table *table = &ctx->file_table;
|
2022-06-25 10:55:38 +00:00
|
|
|
unsigned long nr = ctx->file_alloc_end;
|
2022-05-25 03:43:10 +00:00
|
|
|
int ret;
|
|
|
|
|
2023-03-21 19:44:02 +00:00
|
|
|
if (!table->bitmap)
|
|
|
|
return -ENFILE;
|
|
|
|
|
2022-05-25 03:43:10 +00:00
|
|
|
do {
|
|
|
|
ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);
|
|
|
|
if (ret != nr)
|
|
|
|
return ret;
|
|
|
|
|
2022-06-25 10:55:38 +00:00
|
|
|
if (table->alloc_hint == ctx->file_alloc_start)
|
2022-05-25 03:43:10 +00:00
|
|
|
break;
|
|
|
|
nr = table->alloc_hint;
|
2022-06-25 10:55:38 +00:00
|
|
|
table->alloc_hint = ctx->file_alloc_start;
|
2022-05-25 03:43:10 +00:00
|
|
|
} while (1);
|
|
|
|
|
|
|
|
return -ENFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
|
|
|
|
{
|
2024-10-26 20:50:13 +00:00
|
|
|
if (io_rsrc_data_alloc(&table->data, nr_files))
|
2022-05-25 03:43:10 +00:00
|
|
|
return false;
|
|
|
|
table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);
|
2024-10-26 20:50:13 +00:00
|
|
|
if (table->bitmap)
|
|
|
|
return true;
|
|
|
|
io_rsrc_data_free(&table->data);
|
|
|
|
return false;
|
2022-05-25 03:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void io_free_file_tables(struct io_file_table *table)
|
|
|
|
{
|
2024-10-26 20:50:13 +00:00
|
|
|
io_rsrc_data_free(&table->data);
|
2022-05-25 03:43:10 +00:00
|
|
|
bitmap_free(table->bitmap);
|
|
|
|
table->bitmap = NULL;
|
|
|
|
}
|
2022-05-26 15:44:31 +00:00
|
|
|
|
2022-06-13 10:42:56 +00:00
|
|
|
static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
|
|
|
|
u32 slot_index)
|
2022-05-26 15:44:31 +00:00
|
|
|
__must_hold(&req->ctx->uring_lock)
|
|
|
|
{
|
io_uring/rsrc: get rid of per-ring io_rsrc_node list
Work in progress, but get rid of the per-ring serialization of resource
nodes, like registered buffers and files. Main issue here is that one
node can otherwise hold up a bunch of other nodes from getting freed,
which is especially a problem for file resource nodes and networked
workloads where some descriptors may not see activity in a long time.
As an example, instantiate an io_uring ring fd and create a sparse
registered file table. Even 2 will do. Then create a socket and register
it as fixed file 0, F0. The number of open files in the app is now 5,
with 0/1/2 being the usual stdin/out/err, 3 being the ring fd, and 4
being the socket. Register this socket (eg "the listener") in slot 0 of
the registered file table. Now add an operation on the socket that uses
slot 0. Finally, loop N times, where each loop creates a new socket,
registers said socket as a file, then unregisters the socket, and
finally closes the socket. This is roughly similar to what a basic
accept loop would look like.
At the end of this loop, it's not unreasonable to expect that there
would still be 5 open files. Each socket created and registered in the
loop is also unregistered and closed. But since the listener socket
registered first still has references to its resource node due to still
being active, each subsequent socket unregistration is stuck behind it
for reclaim. Hence 5 + N files are still open at that point, where N is
awaiting the final put held up by the listener socket.
Rewrite the io_rsrc_node handling to NOT rely on serialization. Struct
io_kiocb now gets explicit resource nodes assigned, with each holding a
reference to the parent node. A parent node is either of type FILE or
BUFFER, which are the two types of nodes that exist. A request can have
two nodes assigned, if it's using both registered files and buffers.
Since request issue and task_work completion is both under the ring
private lock, no atomics are needed to handle these references. It's a
simple unlocked inc/dec. As before, the registered buffer or file table
each hold a reference as well to the registered nodes. Final put of the
node will remove the node and free the underlying resource, eg unmap the
buffer or put the file.
Outside of removing the stall in resource reclaim described above, it
has the following advantages:
1) It's a lot simpler than the previous scheme, and easier to follow.
No need to specific quiesce handling anymore.
2) There are no resource node allocations in the fast path, all of that
happens at resource registration time.
3) The structs related to resource handling can all get simplified
quite a bit, like io_rsrc_node and io_rsrc_data. io_rsrc_put can
go away completely.
4) Handling of resource tags is much simpler, and doesn't require
persistent storage as it can simply get assigned up front at
registration time. Just copy them in one-by-one at registration time
and assign to the resource node.
The only real downside is that a request is now explicitly limited to
pinning 2 resources, one file and one buffer, where before just
assigning a resource node to a request would pin all of them. The upside
is that it's easier to follow now, as an individual resource is
explicitly referenced and assigned to the request.
With this in place, the above mentioned example will be using exactly 5
files at the end of the loop, not N.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-10-26 01:27:39 +00:00
|
|
|
struct io_rsrc_node *node;
|
2022-05-26 15:44:31 +00:00
|
|
|
|
|
|
|
if (io_is_uring_fops(file))
|
|
|
|
return -EBADF;
|
2024-10-26 20:50:13 +00:00
|
|
|
if (!ctx->file_table.data.nr)
|
2022-05-26 15:44:31 +00:00
|
|
|
return -ENXIO;
|
2024-10-26 20:50:13 +00:00
|
|
|
if (slot_index >= ctx->file_table.data.nr)
|
2022-05-26 15:44:31 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
io_uring/rsrc: get rid of per-ring io_rsrc_node list
Work in progress, but get rid of the per-ring serialization of resource
nodes, like registered buffers and files. Main issue here is that one
node can otherwise hold up a bunch of other nodes from getting freed,
which is especially a problem for file resource nodes and networked
workloads where some descriptors may not see activity in a long time.
As an example, instantiate an io_uring ring fd and create a sparse
registered file table. Even 2 will do. Then create a socket and register
it as fixed file 0, F0. The number of open files in the app is now 5,
with 0/1/2 being the usual stdin/out/err, 3 being the ring fd, and 4
being the socket. Register this socket (eg "the listener") in slot 0 of
the registered file table. Now add an operation on the socket that uses
slot 0. Finally, loop N times, where each loop creates a new socket,
registers said socket as a file, then unregisters the socket, and
finally closes the socket. This is roughly similar to what a basic
accept loop would look like.
At the end of this loop, it's not unreasonable to expect that there
would still be 5 open files. Each socket created and registered in the
loop is also unregistered and closed. But since the listener socket
registered first still has references to its resource node due to still
being active, each subsequent socket unregistration is stuck behind it
for reclaim. Hence 5 + N files are still open at that point, where N is
awaiting the final put held up by the listener socket.
Rewrite the io_rsrc_node handling to NOT rely on serialization. Struct
io_kiocb now gets explicit resource nodes assigned, with each holding a
reference to the parent node. A parent node is either of type FILE or
BUFFER, which are the two types of nodes that exist. A request can have
two nodes assigned, if it's using both registered files and buffers.
Since request issue and task_work completion is both under the ring
private lock, no atomics are needed to handle these references. It's a
simple unlocked inc/dec. As before, the registered buffer or file table
each hold a reference as well to the registered nodes. Final put of the
node will remove the node and free the underlying resource, eg unmap the
buffer or put the file.
Outside of removing the stall in resource reclaim described above, it
has the following advantages:
1) It's a lot simpler than the previous scheme, and easier to follow.
No need to specific quiesce handling anymore.
2) There are no resource node allocations in the fast path, all of that
happens at resource registration time.
3) The structs related to resource handling can all get simplified
quite a bit, like io_rsrc_node and io_rsrc_data. io_rsrc_put can
go away completely.
4) Handling of resource tags is much simpler, and doesn't require
persistent storage as it can simply get assigned up front at
registration time. Just copy them in one-by-one at registration time
and assign to the resource node.
The only real downside is that a request is now explicitly limited to
pinning 2 resources, one file and one buffer, where before just
assigning a resource node to a request would pin all of them. The upside
is that it's easier to follow now, as an individual resource is
explicitly referenced and assigned to the request.
With this in place, the above mentioned example will be using exactly 5
files at the end of the loop, not N.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-10-26 01:27:39 +00:00
|
|
|
node = io_rsrc_node_alloc(ctx, IORING_RSRC_FILE);
|
|
|
|
if (!node)
|
|
|
|
return -ENOMEM;
|
2023-04-13 14:28:13 +00:00
|
|
|
|
2024-10-26 20:50:13 +00:00
|
|
|
slot_index = array_index_nospec(slot_index, ctx->file_table.data.nr);
|
|
|
|
if (ctx->file_table.data.nodes[slot_index])
|
|
|
|
io_put_rsrc_node(ctx->file_table.data.nodes[slot_index]);
|
io_uring/rsrc: get rid of per-ring io_rsrc_node list
Work in progress, but get rid of the per-ring serialization of resource
nodes, like registered buffers and files. Main issue here is that one
node can otherwise hold up a bunch of other nodes from getting freed,
which is especially a problem for file resource nodes and networked
workloads where some descriptors may not see activity in a long time.
As an example, instantiate an io_uring ring fd and create a sparse
registered file table. Even 2 will do. Then create a socket and register
it as fixed file 0, F0. The number of open files in the app is now 5,
with 0/1/2 being the usual stdin/out/err, 3 being the ring fd, and 4
being the socket. Register this socket (eg "the listener") in slot 0 of
the registered file table. Now add an operation on the socket that uses
slot 0. Finally, loop N times, where each loop creates a new socket,
registers said socket as a file, then unregisters the socket, and
finally closes the socket. This is roughly similar to what a basic
accept loop would look like.
At the end of this loop, it's not unreasonable to expect that there
would still be 5 open files. Each socket created and registered in the
loop is also unregistered and closed. But since the listener socket
registered first still has references to its resource node due to still
being active, each subsequent socket unregistration is stuck behind it
for reclaim. Hence 5 + N files are still open at that point, where N is
awaiting the final put held up by the listener socket.
Rewrite the io_rsrc_node handling to NOT rely on serialization. Struct
io_kiocb now gets explicit resource nodes assigned, with each holding a
reference to the parent node. A parent node is either of type FILE or
BUFFER, which are the two types of nodes that exist. A request can have
two nodes assigned, if it's using both registered files and buffers.
Since request issue and task_work completion is both under the ring
private lock, no atomics are needed to handle these references. It's a
simple unlocked inc/dec. As before, the registered buffer or file table
each hold a reference as well to the registered nodes. Final put of the
node will remove the node and free the underlying resource, eg unmap the
buffer or put the file.
Outside of removing the stall in resource reclaim described above, it
has the following advantages:
1) It's a lot simpler than the previous scheme, and easier to follow.
No need to specific quiesce handling anymore.
2) There are no resource node allocations in the fast path, all of that
happens at resource registration time.
3) The structs related to resource handling can all get simplified
quite a bit, like io_rsrc_node and io_rsrc_data. io_rsrc_put can
go away completely.
4) Handling of resource tags is much simpler, and doesn't require
persistent storage as it can simply get assigned up front at
registration time. Just copy them in one-by-one at registration time
and assign to the resource node.
The only real downside is that a request is now explicitly limited to
pinning 2 resources, one file and one buffer, where before just
assigning a resource node to a request would pin all of them. The upside
is that it's easier to follow now, as an individual resource is
explicitly referenced and assigned to the request.
With this in place, the above mentioned example will be using exactly 5
files at the end of the loop, not N.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-10-26 01:27:39 +00:00
|
|
|
else
|
2024-05-07 21:09:02 +00:00
|
|
|
io_file_bitmap_set(&ctx->file_table, slot_index);
|
2022-05-26 15:44:31 +00:00
|
|
|
|
2024-10-26 20:50:13 +00:00
|
|
|
ctx->file_table.data.nodes[slot_index] = node;
|
io_uring/rsrc: get rid of per-ring io_rsrc_node list
Work in progress, but get rid of the per-ring serialization of resource
nodes, like registered buffers and files. Main issue here is that one
node can otherwise hold up a bunch of other nodes from getting freed,
which is especially a problem for file resource nodes and networked
workloads where some descriptors may not see activity in a long time.
As an example, instantiate an io_uring ring fd and create a sparse
registered file table. Even 2 will do. Then create a socket and register
it as fixed file 0, F0. The number of open files in the app is now 5,
with 0/1/2 being the usual stdin/out/err, 3 being the ring fd, and 4
being the socket. Register this socket (eg "the listener") in slot 0 of
the registered file table. Now add an operation on the socket that uses
slot 0. Finally, loop N times, where each loop creates a new socket,
registers said socket as a file, then unregisters the socket, and
finally closes the socket. This is roughly similar to what a basic
accept loop would look like.
At the end of this loop, it's not unreasonable to expect that there
would still be 5 open files. Each socket created and registered in the
loop is also unregistered and closed. But since the listener socket
registered first still has references to its resource node due to still
being active, each subsequent socket unregistration is stuck behind it
for reclaim. Hence 5 + N files are still open at that point, where N is
awaiting the final put held up by the listener socket.
Rewrite the io_rsrc_node handling to NOT rely on serialization. Struct
io_kiocb now gets explicit resource nodes assigned, with each holding a
reference to the parent node. A parent node is either of type FILE or
BUFFER, which are the two types of nodes that exist. A request can have
two nodes assigned, if it's using both registered files and buffers.
Since request issue and task_work completion is both under the ring
private lock, no atomics are needed to handle these references. It's a
simple unlocked inc/dec. As before, the registered buffer or file table
each hold a reference as well to the registered nodes. Final put of the
node will remove the node and free the underlying resource, eg unmap the
buffer or put the file.
Outside of removing the stall in resource reclaim described above, it
has the following advantages:
1) It's a lot simpler than the previous scheme, and easier to follow.
No need to specific quiesce handling anymore.
2) There are no resource node allocations in the fast path, all of that
happens at resource registration time.
3) The structs related to resource handling can all get simplified
quite a bit, like io_rsrc_node and io_rsrc_data. io_rsrc_put can
go away completely.
4) Handling of resource tags is much simpler, and doesn't require
persistent storage as it can simply get assigned up front at
registration time. Just copy them in one-by-one at registration time
and assign to the resource node.
The only real downside is that a request is now explicitly limited to
pinning 2 resources, one file and one buffer, where before just
assigning a resource node to a request would pin all of them. The upside
is that it's easier to follow now, as an individual resource is
explicitly referenced and assigned to the request.
With this in place, the above mentioned example will be using exactly 5
files at the end of the loop, not N.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-10-26 01:27:39 +00:00
|
|
|
io_fixed_file_set(node, file);
|
2023-12-19 19:36:34 +00:00
|
|
|
return 0;
|
2022-05-26 15:44:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 10:42:56 +00:00
|
|
|
int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
|
|
|
|
unsigned int file_slot)
|
2022-05-26 15:44:31 +00:00
|
|
|
{
|
|
|
|
bool alloc_slot = file_slot == IORING_FILE_INDEX_ALLOC;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (alloc_slot) {
|
|
|
|
ret = io_file_bitmap_get(ctx);
|
|
|
|
if (unlikely(ret < 0))
|
2022-06-13 10:42:56 +00:00
|
|
|
return ret;
|
2022-05-26 15:44:31 +00:00
|
|
|
file_slot = ret;
|
|
|
|
} else {
|
|
|
|
file_slot--;
|
|
|
|
}
|
|
|
|
|
2022-06-13 10:42:56 +00:00
|
|
|
ret = io_install_fixed_file(ctx, file, file_slot);
|
2022-05-26 15:44:31 +00:00
|
|
|
if (!ret && alloc_slot)
|
|
|
|
ret = file_slot;
|
2022-06-13 10:42:56 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Note when io_fixed_fd_install() returns error value, it will ensure
|
|
|
|
* fput() is called correspondingly.
|
|
|
|
*/
|
|
|
|
int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
|
|
|
|
struct file *file, unsigned int file_slot)
|
|
|
|
{
|
|
|
|
struct io_ring_ctx *ctx = req->ctx;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
io_ring_submit_lock(ctx, issue_flags);
|
|
|
|
ret = __io_fixed_fd_install(ctx, file, file_slot);
|
2022-05-26 15:44:31 +00:00
|
|
|
io_ring_submit_unlock(ctx, issue_flags);
|
2022-06-13 10:42:56 +00:00
|
|
|
|
2022-05-26 15:44:31 +00:00
|
|
|
if (unlikely(ret < 0))
|
|
|
|
fput(file);
|
|
|
|
return ret;
|
|
|
|
}
|
2022-06-13 10:42:56 +00:00
|
|
|
|
|
|
|
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
|
|
|
|
{
|
2024-10-26 20:50:13 +00:00
|
|
|
if (unlikely(!ctx->file_table.data.nr))
|
2022-06-13 10:42:56 +00:00
|
|
|
return -ENXIO;
|
2024-10-26 20:50:13 +00:00
|
|
|
if (offset >= ctx->file_table.data.nr)
|
2022-06-13 10:42:56 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2024-10-26 20:50:13 +00:00
|
|
|
offset = array_index_nospec(offset, ctx->file_table.data.nr);
|
|
|
|
if (!ctx->file_table.data.nodes[offset])
|
2022-06-13 10:42:56 +00:00
|
|
|
return -EBADF;
|
2024-10-26 20:50:13 +00:00
|
|
|
io_put_rsrc_node(ctx->file_table.data.nodes[offset]);
|
|
|
|
ctx->file_table.data.nodes[offset] = NULL;
|
2022-06-13 10:42:56 +00:00
|
|
|
io_file_bitmap_clear(&ctx->file_table, offset);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-06-25 10:55:38 +00:00
|
|
|
|
|
|
|
int io_register_file_alloc_range(struct io_ring_ctx *ctx,
|
|
|
|
struct io_uring_file_index_range __user *arg)
|
|
|
|
{
|
|
|
|
struct io_uring_file_index_range range;
|
|
|
|
u32 end;
|
|
|
|
|
|
|
|
if (copy_from_user(&range, arg, sizeof(range)))
|
|
|
|
return -EFAULT;
|
|
|
|
if (check_add_overflow(range.off, range.len, &end))
|
|
|
|
return -EOVERFLOW;
|
2024-10-26 20:50:13 +00:00
|
|
|
if (range.resv || end > ctx->file_table.data.nr)
|
2022-06-25 10:55:38 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
io_file_table_set_alloc_range(ctx, range.off, range.len);
|
|
|
|
return 0;
|
|
|
|
}
|