io_uring/mock_file.c

Source file repositories/reference/linux-study-clean/io_uring/mock_file.c

File Facts

System
Linux kernel
Corpus path
io_uring/mock_file.c
Extension
.c
Size
8470 bytes
Lines
352
Domain
Kernel Services
Bucket
io_uring
Inferred role
Kernel Services: operation-table or driver-model contract
Status
pattern implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

static const struct file_operations io_mock_fops = {
	.owner		= THIS_MODULE,
	.release	= io_mock_release,
	.uring_cmd	= io_mock_cmd,
	.read_iter	= io_mock_read_iter,
	.write_iter	= io_mock_write_iter,
	.llseek		= io_mock_llseek,
};

static const struct file_operations io_mock_poll_fops = {
	.owner		= THIS_MODULE,
	.release	= io_mock_release,
	.uring_cmd	= io_mock_cmd,
	.read_iter	= io_mock_read_iter,
	.write_iter	= io_mock_write_iter,
	.llseek		= io_mock_llseek,
	.poll		= io_mock_poll,
};

#define IO_VALID_CREATE_FLAGS (IORING_MOCK_CREATE_F_SUPPORT_NOWAIT | \
				IORING_MOCK_CREATE_F_POLL)

static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flags)
{
	const struct file_operations *fops = &io_mock_fops;
	const struct io_uring_sqe *sqe = cmd->sqe;
	struct io_uring_mock_create mc, __user *uarg;
	struct file *file;
	struct io_mock_file *mf __free(kfree) = NULL;
	size_t uarg_size;

	/*
	 * It's a testing only driver that allows exercising edge cases
	 * that wouldn't be possible to hit otherwise.
	 */
	add_taint(TAINT_TEST, LOCKDEP_STILL_OK);

	uarg = u64_to_user_ptr(READ_ONCE(sqe->addr));
	uarg_size = READ_ONCE(sqe->len);

	if (sqe->ioprio || sqe->__pad1 || sqe->addr3 || sqe->file_index)
		return -EINVAL;
	if (uarg_size != sizeof(mc))
		return -EINVAL;

	memset(&mc, 0, sizeof(mc));
	if (copy_from_user(&mc, uarg, uarg_size))
		return -EFAULT;
	if (!mem_is_zero(mc.__resv, sizeof(mc.__resv)))
		return -EINVAL;
	if (mc.flags & ~IO_VALID_CREATE_FLAGS)
		return -EINVAL;
	if (mc.file_size > SZ_1G)
		return -EINVAL;
	if (mc.rw_delay_ns > NSEC_PER_SEC)
		return -EINVAL;

	mf = kzalloc_obj(*mf, GFP_KERNEL_ACCOUNT);
	if (!mf)
		return -ENOMEM;

	init_waitqueue_head(&mf->poll_wq);
	mf->size = mc.file_size;
	mf->rw_delay_ns = mc.rw_delay_ns;
	if (mc.flags & IORING_MOCK_CREATE_F_POLL) {
		fops = &io_mock_poll_fops;
		mf->pollable = true;
	}

	FD_PREPARE(fdf, O_RDWR | O_CLOEXEC,
		   anon_inode_create_getfile("[io_uring_mock]", fops, mf,
					     O_RDWR | O_CLOEXEC, NULL));
	if (fdf.err)
		return fdf.err;

	retain_and_null_ptr(mf);
	file = fd_prepare_file(fdf);
	file->f_mode |= FMODE_READ | FMODE_CAN_READ | FMODE_WRITE |
			FMODE_CAN_WRITE | FMODE_LSEEK;
	if (mc.flags & IORING_MOCK_CREATE_F_SUPPORT_NOWAIT)
		file->f_mode |= FMODE_NOWAIT;

	mc.out_fd = fd_prepare_fd(fdf);
	if (copy_to_user(uarg, &mc, uarg_size))
		return -EFAULT;

	fd_publish(fdf);
	return 0;
}

Annotation

Implementation Notes