public inbox for [email protected]
 help / color / mirror / Atom feed
From: Pavel Begunkov <[email protected]>
To: Jens Axboe <[email protected]>, [email protected], [email protected]
Subject: [PATCH 2/5] io_uring: copy hdr consistently for send and recv
Date: Sun, 15 Nov 2020 10:35:41 +0000	[thread overview]
Message-ID: <b280f7e761bbc7f6fbd3c2fefb5152b38ec5698d.1605435507.git.asml.silence@gmail.com> (raw)
In-Reply-To: <[email protected]>

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 <[email protected]>
---
 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


  parent reply	other threads:[~2020-11-15 10:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-15 10:35 [RFC 0/5] support for {send,recv}[msg] with registered bufs Pavel Begunkov
2020-11-15 10:35 ` [PATCH 1/5] io_uring: move io_recvmsg_copy_hdr() Pavel Begunkov
2020-11-15 10:35 ` Pavel Begunkov [this message]
2020-11-15 10:35 ` [PATCH 3/5] io_uring: opcode independent import_fixed Pavel Begunkov
2020-11-15 10:35 ` [PATCH 4/5] io_uring: send/recv with registered buffer Pavel Begunkov
2020-11-15 10:35 ` [PATCH 5/5] io_uring: sendmsg/recvmsg with registered buffers Pavel Begunkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b280f7e761bbc7f6fbd3c2fefb5152b38ec5698d.1605435507.git.asml.silence@gmail.com \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox