recvmsg() goes through a custom written msg headers/iovec copying helper, do that for sendmsg() as well. Apart from being more consistent in general, it allows to extend it (e.g. for registered buffers) without duplication. Signed-off-by: Pavel Begunkov <asml.silence@xxxxxxxxx> --- fs/io_uring.c | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index bcd6f63af711..88daf5fc7e8e 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -4494,16 +4494,18 @@ static int io_setup_async_msg(struct io_kiocb *req, return -EAGAIN; } -static int __io_recvmsg_copy_hdr(struct io_kiocb *req, - struct io_async_msghdr *iomsg) +static int __io_msg_copy_hdr(struct io_kiocb *req, + struct io_async_msghdr *iomsg, int rw) { + struct sockaddr __user **save_addr; struct io_sr_msg *sr = &req->sr_msg; struct iovec __user *uiov; size_t iov_len; int ret; - ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, - &iomsg->uaddr, &uiov, &iov_len); + save_addr = (rw == READ) ? &iomsg->uaddr : NULL; + ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, save_addr, + &uiov, &iov_len); if (ret) return ret; @@ -4517,7 +4519,7 @@ static int __io_recvmsg_copy_hdr(struct io_kiocb *req, sr->len); iomsg->iov = NULL; } else { - ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, + ret = __import_iovec(rw, uiov, iov_len, UIO_FASTIOV, &iomsg->iov, &iomsg->msg.msg_iter, false); if (ret > 0) @@ -4528,9 +4530,10 @@ static int __io_recvmsg_copy_hdr(struct io_kiocb *req, } #ifdef CONFIG_COMPAT -static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, - struct io_async_msghdr *iomsg) +static int __io_compat_msg_copy_hdr(struct io_kiocb *req, + struct io_async_msghdr *iomsg, int rw) { + struct sockaddr __user **save_addr; struct compat_msghdr __user *msg_compat; struct io_sr_msg *sr = &req->sr_msg; struct compat_iovec __user *uiov; @@ -4539,8 +4542,9 @@ static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, int ret; msg_compat = (struct compat_msghdr __user *) sr->umsg; - ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, - &ptr, &len); + save_addr = (rw == READ) ? &iomsg->uaddr : NULL; + ret = __get_compat_msghdr(&iomsg->msg, msg_compat, save_addr, + &ptr, &len); if (ret) return ret; @@ -4559,7 +4563,7 @@ static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, sr->len = iomsg->iov[0].iov_len; iomsg->iov = NULL; } else { - ret = __import_iovec(READ, (struct iovec __user *)uiov, len, + ret = __import_iovec(rw, (struct iovec __user *)uiov, len, UIO_FASTIOV, &iomsg->iov, &iomsg->msg.msg_iter, true); if (ret < 0) @@ -4585,8 +4589,8 @@ static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, return kbuf; } -static int io_recvmsg_copy_hdr(struct io_kiocb *req, - struct io_async_msghdr *iomsg) +static int io_import_msg(struct io_kiocb *req, struct io_async_msghdr *iomsg, + int rw) { struct io_buffer *kbuf; int ret; @@ -4595,14 +4599,16 @@ static int io_recvmsg_copy_hdr(struct io_kiocb *req, iomsg->iov = iomsg->fast_iov; #ifdef CONFIG_COMPAT if (req->ctx->compat) - ret = __io_compat_recvmsg_copy_hdr(req, iomsg); + ret = __io_compat_msg_copy_hdr(req, iomsg, rw); else #endif - ret = __io_recvmsg_copy_hdr(req, iomsg); + ret = __io_msg_copy_hdr(req, iomsg, rw); if (ret < 0) return ret; if (req->flags & REQ_F_BUFFER_SELECT) { + if (rw != READ) + return -EINVAL; /* init is always done with uring_lock held */ kbuf = io_recv_buffer_select(req, false); if (IS_ERR(kbuf)) @@ -4614,15 +4620,6 @@ static int io_recvmsg_copy_hdr(struct io_kiocb *req, return 0; } -static int io_sendmsg_copy_hdr(struct io_kiocb *req, - struct io_async_msghdr *iomsg) -{ - iomsg->iov = iomsg->fast_iov; - iomsg->msg.msg_name = &iomsg->addr; - return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, - req->sr_msg.msg_flags, &iomsg->iov); -} - static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) { struct io_async_msghdr *async_msg = req->async_data; @@ -4643,7 +4640,7 @@ static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) if (!async_msg || !io_op_defs[req->opcode].needs_async_data) return 0; - ret = io_sendmsg_copy_hdr(req, async_msg); + ret = io_import_msg(req, async_msg, WRITE); if (!ret) req->flags |= REQ_F_NEED_CLEANUP; return ret; @@ -4663,7 +4660,7 @@ static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, kmsg = req->async_data; if (!kmsg) { - ret = io_sendmsg_copy_hdr(req, &iomsg); + ret = io_import_msg(req, &iomsg, WRITE); if (ret) return ret; kmsg = &iomsg; @@ -4760,7 +4757,7 @@ static int io_recvmsg_prep(struct io_kiocb *req, if (!async_msg || !io_op_defs[req->opcode].needs_async_data) return 0; - ret = io_recvmsg_copy_hdr(req, async_msg); + ret = io_import_msg(req, async_msg, READ); if (!ret) req->flags |= REQ_F_NEED_CLEANUP; return ret; @@ -4780,7 +4777,7 @@ static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, kmsg = req->async_data; if (!kmsg) { - ret = io_recvmsg_copy_hdr(req, &iomsg); + ret = io_import_msg(req, &iomsg, READ); if (ret) return ret; kmsg = &iomsg; -- 2.24.0