From patchwork Fri Jan 7 00:00:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 12705937 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E71EC43217 for ; Fri, 7 Jan 2022 00:00:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343679AbiAGAA1 (ORCPT ); Thu, 6 Jan 2022 19:00:27 -0500 Received: from ip59.38.31.103.in-addr.arpa.unknwn.cloudhost.asia ([103.31.38.59]:44530 "EHLO gnuweeb.org" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S232802AbiAGAA0 (ORCPT ); Thu, 6 Jan 2022 19:00:26 -0500 Received: from integral2.. (unknown [36.68.70.227]) by gnuweeb.org (Postfix) with ESMTPSA id E4084C1662; Fri, 7 Jan 2022 00:00:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=gnuweeb.org; s=default; t=1641513623; bh=zVcijoVrqichYyAtJLimzaz4WltQzNvqW5+uBLx1moE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jonuSsvKid7aFWo09ULcE9X/Ivakjju7B86QHHyLkURJK0RFHNUVvZ0cTMpZB5WpN Y4jpBIUuLajgMDhxVEraQe3eFfEUkv+6/gnBv3hh0HZ/5ECMNdfS12E6GTDnsI7QhT SHGNcihvMpC+toWsMr5MIva02CRP7Pb3FsQ7mhQEUHn+mn45r3kAMlRLbnoLUevtzH DOxXhq8b0GDQnNJv7ppT8v+ueilNy+kI5ADcspCsqja6K5rxR2vK8e0rFUWIBnUgtQ U4Rbia8DLXHJP50TFs7UF15PZhWBy6Wk2X13aJVekFBCzsdvB3P4P90aW38U5s0adE KN5nkG7Qtrs+w== From: Ammar Faizi To: Jens Axboe Cc: Ammar Faizi , io-uring Mailing List , netdev Mailing List , GNU/Weeb Mailing List , Linux Kernel Mailing List , Pavel Begunkov , "David S. Miller" , Jakub Kicinski , Nugra , Praveen Kumar , Ammar Faizi Subject: [RFC PATCH v4 1/3] io_uring: Rename `io_{send,recv}` to `io_{sendto,recvfrom}` Date: Fri, 7 Jan 2022 07:00:03 +0700 Message-Id: <20220107000006.1194026-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220107000006.1194026-1-ammarfaizi2@gnuweeb.org> References: <20220107000006.1194026-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-State: RFC Currently we can perform `send` and `recv` via io_uring. And now, we are going to add `sendto` and `recvfrom` support for io_uring. Note that: Calling `send(fd, buf, len, flags)` is equivalent to calling `sendto(fd, buf, len, flags, NULL, 0)`. Therefore, `sendto` is a superset of `send`. Calling `recv(fd, buf, len, flags)` is equivalent to calling `recvfrom(fd, buf, len, flags, NULL, NULL)`. Therefore, `recvfrom` is a superset of `recv`. As such, let's direct the current supported `IORING_OP_{SEND,RECV}` to `io_{sendto,recvfrom}`. These functions will also be used for `IORING_OP_{SENDTO,RECVFROM}` operation in the next patches. Cc: Nugra Signed-off-by: Ammar Faizi --- v4: - Rebase the work (sync with "for-next" branch in Jens' tree). v3: - Fix build error when CONFIG_NET is undefined for PATCH 1/3. I tried to fix it in PATCH 3/3, but it should be fixed in PATCH 1/3, otherwise it breaks the build in PATCH 1/3. v2: - Added Nugra to CC list (tester). --- --- fs/io_uring.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index d5da4a898fe8..5e45e4d6969c 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -4947,7 +4947,7 @@ static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) return 0; } -static int io_send(struct io_kiocb *req, unsigned int issue_flags) +static int io_sendto(struct io_kiocb *req, unsigned int issue_flags) { struct io_sr_msg *sr = &req->sr_msg; struct msghdr msg; @@ -5173,7 +5173,7 @@ static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) return 0; } -static int io_recv(struct io_kiocb *req, unsigned int issue_flags) +static int io_recvfrom(struct io_kiocb *req, unsigned int issue_flags) { struct io_buffer *kbuf; struct io_sr_msg *sr = &req->sr_msg; @@ -5381,8 +5381,8 @@ IO_NETOP_PREP_ASYNC(sendmsg); IO_NETOP_PREP_ASYNC(recvmsg); IO_NETOP_PREP_ASYNC(connect); IO_NETOP_PREP(accept); -IO_NETOP_FN(send); -IO_NETOP_FN(recv); +IO_NETOP_FN(sendto); +IO_NETOP_FN(recvfrom); #endif /* CONFIG_NET */ struct io_poll_table { @@ -6710,13 +6710,13 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) ret = io_sendmsg(req, issue_flags); break; case IORING_OP_SEND: - ret = io_send(req, issue_flags); + ret = io_sendto(req, issue_flags); break; case IORING_OP_RECVMSG: ret = io_recvmsg(req, issue_flags); break; case IORING_OP_RECV: - ret = io_recv(req, issue_flags); + ret = io_recvfrom(req, issue_flags); break; case IORING_OP_TIMEOUT: ret = io_timeout(req, issue_flags); From patchwork Fri Jan 7 00:00:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 12705939 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F08DC433EF for ; Fri, 7 Jan 2022 00:00:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343715AbiAGAAa (ORCPT ); Thu, 6 Jan 2022 19:00:30 -0500 Received: from ip59.38.31.103.in-addr.arpa.unknwn.cloudhost.asia ([103.31.38.59]:44544 "EHLO gnuweeb.org" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S245708AbiAGAA1 (ORCPT ); Thu, 6 Jan 2022 19:00:27 -0500 Received: from integral2.. (unknown [36.68.70.227]) by gnuweeb.org (Postfix) with ESMTPSA id 575FCC17B7; Fri, 7 Jan 2022 00:00:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=gnuweeb.org; s=default; t=1641513623; bh=WzZ3THiNZ134dV91EsO3hDKhoOTzDcbFpG9HFQHAhJk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HGsgKQxjkRKN8/+wDKpCyl7VHvQLMo5iNUpPpDlWMeZRMTh/CSPkwIIrR5dsMEH2f rKfvWHjFwTTwx/FKloWxxN3VFy/NEEmuuUOfSpC5yuuclzri1x8LarrNe4AuFSdywc 34dHqjfoynxVeMXKjxV7Z7r70lM9/uNO89YsgalZWnoKGXPgzwShioXDbsW6qKXFqb MviLqisFAesc3rRyRrBzCFL0X6SXiTkbO37Vg7353gh2/HdolQypeNkVs+aYhaKSsM KD2fFiUqoinaY6csLzxDjDucFCDRQqf2Dqm2HuPhW+GGOliYVkaW1vQeVLgrEWGZ4Q G6dz0GD16cI8g== From: Ammar Faizi To: Jens Axboe Cc: Ammar Faizi , io-uring Mailing List , netdev Mailing List , GNU/Weeb Mailing List , Linux Kernel Mailing List , Pavel Begunkov , "David S. Miller" , Jakub Kicinski , Nugra , Praveen Kumar , Ammar Faizi Subject: [RFC PATCH v4 2/3] net: Make `move_addr_to_user()` be a non static function Date: Fri, 7 Jan 2022 07:00:04 +0700 Message-Id: <20220107000006.1194026-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220107000006.1194026-1-ammarfaizi2@gnuweeb.org> References: <20220107000006.1194026-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC In order to add recvfrom support for io_uring, we need to call `move_addr_to_user()` from fs/io_uring.c. This makes `move_addr_to_user()` be a non static function so we can call it from io_uring. Cc: "David S. Miller" Cc: Jakub Kicinski Cc: netdev@vger.kernel.org Cc: Nugra Signed-off-by: Ammar Faizi --- v4: * No changes * v3: * No changes * v2: - Added Nugra to CC list (tester). --- --- include/linux/socket.h | 2 ++ net/socket.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/linux/socket.h b/include/linux/socket.h index 8ef26d89ef49..0d0bc1ace50c 100644 --- a/include/linux/socket.h +++ b/include/linux/socket.h @@ -371,6 +371,8 @@ struct ucred { #define IPX_TYPE 1 extern int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr); +extern int move_addr_to_user(struct sockaddr_storage *kaddr, int klen, + void __user *uaddr, int __user *ulen); extern int put_cmsg(struct msghdr*, int level, int type, int len, void *data); struct timespec64; diff --git a/net/socket.c b/net/socket.c index 7f64a6eccf63..af521d351c8a 100644 --- a/net/socket.c +++ b/net/socket.c @@ -267,8 +267,8 @@ int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *k * specified. Zero is returned for a success. */ -static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen, - void __user *uaddr, int __user *ulen) +int move_addr_to_user(struct sockaddr_storage *kaddr, int klen, + void __user *uaddr, int __user *ulen) { int err; int len; From patchwork Fri Jan 7 00:00:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 12705938 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E504C433F5 for ; Fri, 7 Jan 2022 00:00:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343695AbiAGAA3 (ORCPT ); Thu, 6 Jan 2022 19:00:29 -0500 Received: from ip59.38.31.103.in-addr.arpa.unknwn.cloudhost.asia ([103.31.38.59]:44554 "EHLO gnuweeb.org" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1343548AbiAGAA1 (ORCPT ); Thu, 6 Jan 2022 19:00:27 -0500 Received: from integral2.. (unknown [36.68.70.227]) by gnuweeb.org (Postfix) with ESMTPSA id 4D5EDC17CA; Fri, 7 Jan 2022 00:00:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=gnuweeb.org; s=default; t=1641513624; bh=bRprzljkPdXWkVklApgZ3wCc6hsKMQVhUVm66yb3UGk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IRxBEifU7kf4wNbMoU141NsmXy3CgcTRmH+QDd2MtAIcParZhn9wUExL1K8eWFsJy mkYpwROow3dkvJqD5zWq0fJRQsiGZq3JZj7EWgGaNUrvgXU9v/2mOzJopKYXvHdwp3 JNnALREg38kNiOqNBUK431jxvpcto8r/LkjOSD+nCB3vQBFK8HCjLOxswKpca1uqWO SDZWaw3MhHz7iwvV3fhMw21rLwGnaTCjfVkuIJm7OjMbbC2W8ySgXPRqNMKKxr3SGZ rvdiVRsVrezKnRz+xPElYd4zL4w554wS3rN6I4yuNcsuQBqjz1b5TF2lsQNuaUJpQg 4o4HfWGLHvgxQ== From: Ammar Faizi To: Jens Axboe Cc: Ammar Faizi , io-uring Mailing List , netdev Mailing List , GNU/Weeb Mailing List , Linux Kernel Mailing List , Pavel Begunkov , "David S. Miller" , Jakub Kicinski , Nugra , Praveen Kumar , Ammar Faizi Subject: [RFC PATCH v4 3/3] io_uring: Add `sendto(2)` and `recvfrom(2)` support Date: Fri, 7 Jan 2022 07:00:05 +0700 Message-Id: <20220107000006.1194026-4-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220107000006.1194026-1-ammarfaizi2@gnuweeb.org> References: <20220107000006.1194026-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-State: RFC This adds sendto(2) and recvfrom(2) support for io_uring. New opcodes: IORING_OP_SENDTO IORING_OP_RECVFROM Cc: Nugra Cc: Praveen Kumar Link: https://github.com/axboe/liburing/issues/397 Signed-off-by: Ammar Faizi --- v4: - Rebase the work (sync with "for-next" branch in Jens' tree). - Remove Tested-by tag from Nugra as this patch changes. - (Address Praveen's comment) Zero `sendto_addr_len` and `recvfrom_addr_len` on prep when the `req->opcode` is not `IORING_OP_{SENDTO,RECVFROM}`. v3: - Fix build error when CONFIG_NET is undefined should be done in the first patch, not this patch. - Add Tested-by tag from Nugra. v2: - In `io_recvfrom()`, mark the error check of `move_addr_to_user()` call as unlikely. - Fix build error when CONFIG_NET is undefined. - Added Nugra to CC list (tester). --- --- fs/io_uring.c | 82 +++++++++++++++++++++++++++++++++-- include/uapi/linux/io_uring.h | 5 ++- 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 5e45e4d6969c..3c85dd0d50b4 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -574,7 +574,15 @@ struct io_sr_msg { union { struct compat_msghdr __user *umsg_compat; struct user_msghdr __user *umsg; - void __user *buf; + + struct { + void __user *buf; + struct sockaddr __user *addr; + union { + int sendto_addr_len; + int __user *recvfrom_addr_len; + }; + }; }; int msg_flags; int bgid; @@ -1105,6 +1113,19 @@ static const struct io_op_def io_op_defs[] = { [IORING_OP_MKDIRAT] = {}, [IORING_OP_SYMLINKAT] = {}, [IORING_OP_LINKAT] = {}, + [IORING_OP_SENDTO] = { + .needs_file = 1, + .unbound_nonreg_file = 1, + .pollout = 1, + .audit_skip = 1, + }, + [IORING_OP_RECVFROM] = { + .needs_file = 1, + .unbound_nonreg_file = 1, + .pollin = 1, + .buffer_select = 1, + .audit_skip = 1, + }, }; /* requests with any of those set should undergo io_disarm_next() */ @@ -4890,12 +4911,25 @@ static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) return -EINVAL; + /* + * For IORING_OP_SEND{,TO}, the assignment to @sr->umsg + * is equivalent to an assignment to @sr->buf. + */ sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); + sr->len = READ_ONCE(sqe->len); sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; if (sr->msg_flags & MSG_DONTWAIT) req->flags |= REQ_F_NOWAIT; + if (req->opcode == IORING_OP_SENDTO) { + sr->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2)); + sr->sendto_addr_len = READ_ONCE(sqe->addr3); + } else { + sr->addr = (struct sockaddr __user *) NULL; + sr->sendto_addr_len = 0; + } + #ifdef CONFIG_COMPAT if (req->ctx->compat) sr->msg_flags |= MSG_CMSG_COMPAT; @@ -4949,6 +4983,7 @@ static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) static int io_sendto(struct io_kiocb *req, unsigned int issue_flags) { + struct sockaddr_storage address; struct io_sr_msg *sr = &req->sr_msg; struct msghdr msg; struct iovec iov; @@ -4965,10 +5000,20 @@ static int io_sendto(struct io_kiocb *req, unsigned int issue_flags) if (unlikely(ret)) return ret; - msg.msg_name = NULL; + msg.msg_control = NULL; msg.msg_controllen = 0; - msg.msg_namelen = 0; + if (sr->addr) { + ret = move_addr_to_kernel(sr->addr, sr->sendto_addr_len, + &address); + if (unlikely(ret < 0)) + goto fail; + msg.msg_name = (struct sockaddr *) &address; + msg.msg_namelen = sr->sendto_addr_len; + } else { + msg.msg_name = NULL; + msg.msg_namelen = 0; + } flags = req->sr_msg.msg_flags; if (issue_flags & IO_URING_F_NONBLOCK) @@ -4983,6 +5028,7 @@ static int io_sendto(struct io_kiocb *req, unsigned int issue_flags) return -EAGAIN; if (ret == -ERESTARTSYS) ret = -EINTR; +fail: req_set_fail(req); } __io_req_complete(req, issue_flags, ret, 0); @@ -5101,13 +5147,26 @@ static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) return -EINVAL; + /* + * For IORING_OP_RECV{,FROM}, the assignment to @sr->umsg + * is equivalent to an assignment to @sr->buf. + */ sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); + sr->len = READ_ONCE(sqe->len); sr->bgid = READ_ONCE(sqe->buf_group); sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; if (sr->msg_flags & MSG_DONTWAIT) req->flags |= REQ_F_NOWAIT; + if (req->opcode == IORING_OP_RECVFROM) { + sr->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2)); + sr->recvfrom_addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr3)); + } else { + sr->addr = (struct sockaddr __user *) NULL; + sr->recvfrom_addr_len = (int __user *) NULL; + } + #ifdef CONFIG_COMPAT if (req->ctx->compat) sr->msg_flags |= MSG_CMSG_COMPAT; @@ -5183,6 +5242,7 @@ static int io_recvfrom(struct io_kiocb *req, unsigned int issue_flags) struct iovec iov; unsigned flags; int ret, min_ret = 0; + struct sockaddr_storage address; bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; sock = sock_from_file(req->file); @@ -5200,9 +5260,10 @@ static int io_recvfrom(struct io_kiocb *req, unsigned int issue_flags) if (unlikely(ret)) goto out_free; - msg.msg_name = NULL; + msg.msg_name = sr->addr ? (struct sockaddr *) &address : NULL; msg.msg_control = NULL; msg.msg_controllen = 0; + /* We assume all kernel code knows the size of sockaddr_storage */ msg.msg_namelen = 0; msg.msg_iocb = NULL; msg.msg_flags = 0; @@ -5214,6 +5275,15 @@ static int io_recvfrom(struct io_kiocb *req, unsigned int issue_flags) min_ret = iov_iter_count(&msg.msg_iter); ret = sock_recvmsg(sock, &msg, flags); + if (ret >= 0 && sr->addr != NULL) { + int tmp; + + tmp = move_addr_to_user(&address, msg.msg_namelen, sr->addr, + sr->recvfrom_addr_len); + if (unlikely(tmp < 0)) + ret = tmp; + } + out_free: if (ret < min_ret) { if (ret == -EAGAIN && force_nonblock) @@ -6452,9 +6522,11 @@ static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) case IORING_OP_SYNC_FILE_RANGE: return io_sfr_prep(req, sqe); case IORING_OP_SENDMSG: + case IORING_OP_SENDTO: case IORING_OP_SEND: return io_sendmsg_prep(req, sqe); case IORING_OP_RECVMSG: + case IORING_OP_RECVFROM: case IORING_OP_RECV: return io_recvmsg_prep(req, sqe); case IORING_OP_CONNECT: @@ -6709,12 +6781,14 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) case IORING_OP_SENDMSG: ret = io_sendmsg(req, issue_flags); break; + case IORING_OP_SENDTO: case IORING_OP_SEND: ret = io_sendto(req, issue_flags); break; case IORING_OP_RECVMSG: ret = io_recvmsg(req, issue_flags); break; + case IORING_OP_RECVFROM: case IORING_OP_RECV: ret = io_recvfrom(req, issue_flags); break; diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 787f491f0d2a..a58cde19b4d0 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -60,7 +60,8 @@ struct io_uring_sqe { __s32 splice_fd_in; __u32 file_index; }; - __u64 __pad2[2]; + __u64 addr3; + __u64 __pad2[1]; }; enum { @@ -143,6 +144,8 @@ enum { IORING_OP_MKDIRAT, IORING_OP_SYMLINKAT, IORING_OP_LINKAT, + IORING_OP_SENDTO, + IORING_OP_RECVFROM, /* this goes last, obviously */ IORING_OP_LAST,