Commit b54a1404 authored by Jens Axboe's avatar Jens Axboe
Browse files

io_uring/rsrc: add io_rsrc_node_lookup() helper



There are lots of spots open-coding this functionality, add a generic
helper that does the node lookup in a speculation safe way.

Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 3597f278
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -240,10 +240,12 @@ static int __io_sync_cancel(struct io_uring_task *tctx,
	/* fixed must be grabbed every time since we drop the uring_lock */
	if ((cd->flags & IORING_ASYNC_CANCEL_FD) &&
	    (cd->flags & IORING_ASYNC_CANCEL_FD_FIXED)) {
		if (unlikely(fd >= ctx->file_table.data.nr))
		struct io_rsrc_node *node;

		node = io_rsrc_node_lookup(&ctx->file_table.data, fd);
		if (unlikely(!node))
			return -EBADF;
		fd = array_index_nospec(fd, ctx->file_table.data.nr);
		cd->file = io_file_from_index(&ctx->file_table, fd);
		cd->file = io_slot_file(node);
		if (!cd->file)
			return -EBADF;
	}
+9 −7
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
				 u32 slot_index)
	__must_hold(&req->ctx->uring_lock)
{
	struct io_rsrc_node *node;
	struct io_rsrc_node *node, *old_node;

	if (io_is_uring_fops(file))
		return -EBADF;
@@ -71,9 +71,9 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
	if (!node)
		return -ENOMEM;

	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]);
	old_node = io_rsrc_node_lookup(&ctx->file_table.data, slot_index);
	if (old_node)
		io_put_rsrc_node(old_node);
	else
		io_file_bitmap_set(&ctx->file_table, slot_index);

@@ -123,15 +123,17 @@ int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,

int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
{
	struct io_rsrc_node *node;

	if (unlikely(!ctx->file_table.data.nr))
		return -ENXIO;
	if (offset >= ctx->file_table.data.nr)
		return -EINVAL;

	offset = array_index_nospec(offset, ctx->file_table.data.nr);
	if (!ctx->file_table.data.nodes[offset])
	node = io_rsrc_node_lookup(&ctx->file_table.data, offset);
	if (!node)
		return -EBADF;
	io_put_rsrc_node(ctx->file_table.data.nodes[offset]);
	io_put_rsrc_node(node);
	ctx->file_table.data.nodes[offset] = NULL;
	io_file_bitmap_clear(&ctx->file_table, offset);
	return 0;
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ static inline struct file *io_slot_file(struct io_rsrc_node *node)
static inline struct file *io_file_from_index(struct io_file_table *table,
					      int index)
{
	struct io_rsrc_node *node = table->data.nodes[index];
	struct io_rsrc_node *node = io_rsrc_node_lookup(&table->data, index);

	if (node)
		return io_slot_file(node);
+1 −5
Original line number Diff line number Diff line
@@ -1879,16 +1879,12 @@ inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
	struct file *file = NULL;

	io_ring_submit_lock(ctx, issue_flags);
	if (unlikely((unsigned int)fd >= ctx->file_table.data.nr))
		goto out;
	fd = array_index_nospec(fd, ctx->file_table.data.nr);
	node = ctx->file_table.data.nodes[fd];
	node = io_rsrc_node_lookup(&ctx->file_table.data, fd);
	if (node) {
		io_req_assign_rsrc_node(req, node);
		req->flags |= io_slot_flags(node);
		file = io_slot_file(node);
	}
out:
	io_ring_submit_unlock(ctx, issue_flags);
	return file;
}
+15 −16
Original line number Diff line number Diff line
@@ -172,22 +172,24 @@ static int io_msg_ring_data(struct io_kiocb *req, unsigned int issue_flags)
	return __io_msg_ring_data(target_ctx, msg, issue_flags);
}

static struct file *io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags)
static int io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags)
{
	struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
	struct io_ring_ctx *ctx = req->ctx;
	struct file *file = NULL;
	int idx = msg->src_fd;
	struct io_rsrc_node *node;
	int ret = -EBADF;

	io_ring_submit_lock(ctx, issue_flags);
	if (likely(idx < ctx->file_table.data.nr)) {
		idx = array_index_nospec(idx, ctx->file_table.data.nr);
		file = io_file_from_index(&ctx->file_table, idx);
		if (file)
			get_file(file);
	node = io_rsrc_node_lookup(&ctx->file_table.data, msg->src_fd);
	if (node) {
		msg->src_file = io_slot_file(node);
		if (msg->src_file)
			get_file(msg->src_file);
		req->flags |= REQ_F_NEED_CLEANUP;
		ret = 0;
	}
	io_ring_submit_unlock(ctx, issue_flags);
	return file;
	return ret;
}

static int io_msg_install_complete(struct io_kiocb *req, unsigned int issue_flags)
@@ -256,7 +258,6 @@ static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
	struct io_ring_ctx *target_ctx = req->file->private_data;
	struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
	struct io_ring_ctx *ctx = req->ctx;
	struct file *src_file = msg->src_file;

	if (msg->len)
		return -EINVAL;
@@ -264,12 +265,10 @@ static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
		return -EINVAL;
	if (target_ctx->flags & IORING_SETUP_R_DISABLED)
		return -EBADFD;
	if (!src_file) {
		src_file = io_msg_grab_file(req, issue_flags);
		if (!src_file)
			return -EBADF;
		msg->src_file = src_file;
		req->flags |= REQ_F_NEED_CLEANUP;
	if (!msg->src_file) {
		int ret = io_msg_grab_file(req, issue_flags);
		if (unlikely(ret))
			return ret;
	}

	if (io_msg_need_remote(target_ctx))
Loading