From patchwork Wed Aug 25 11:25:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12457327 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 X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C06AC4320E for ; Wed, 25 Aug 2021 11:26:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7FE66610D1 for ; Wed, 25 Aug 2021 11:26:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240389AbhHYL1Q (ORCPT ); Wed, 25 Aug 2021 07:27:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239536AbhHYL1M (ORCPT ); Wed, 25 Aug 2021 07:27:12 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37804C061757; Wed, 25 Aug 2021 04:26:27 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id u15so14744936wmj.1; Wed, 25 Aug 2021 04:26:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=a1cuoQAJMwM5+2BEvIbHPHMLxmGuXDEYSXOYMtak3NM=; b=amabSo8nz0VNvKBf/27D+jsa8dryRGj3pZzzvabxeD59YSM2tjI97/iZXbjVl5oTIt 0+kf/tVqUcglXmaNg4iQKqdo3C+jWSK2xFteKmWCsAm0+Rhw4w1LD4+a5SdWbAtiy9kp N/U5tsPFRoNzQaFPZKMkLTugdOwtRHtcz324WqYQ+3GK8Z6t7FXZecTJtJGhKp5ulnJo cPLiGLHkICNdTw3T+qOTFXwAp2+9MxOwlugi3FjOhDbf2/KfVrTPE5dWUImNqIztvu7N RdBpe4+Gm0u0bbWYJXbNB+DydvU/kS7v8ExENbyIl+/gzrFw2yd5ZJLTnT3CxAp/msc6 P7OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=a1cuoQAJMwM5+2BEvIbHPHMLxmGuXDEYSXOYMtak3NM=; b=YSOLzotUfQNdfZrMjJomgyhDK2IAoai/bDUi3JmmVcORGklle4IFQePIEifj40fKaE jh3mdDHBiY+xl/VyWwyA6fCSXOn9OIN7BWRcTAMSqmQyJPqnj1iKU3/l0IGP23XVaE6T SoVJ0g7nS+Qbv8fFF//5B5HW2NXw8WzUqinPgF7aKlVqU4gRAlOksMIT1rccAyomxEts Y0DcA+0FCj02Kuligc7woMfaA03BLSm9t8NUruHNRq5YAZnA44/yOK4/3VdirZPD0pbY zXBpgH4BYx6VpqthC1OIWWTO7h2JyEwllSx311Sbe2pAa2n4FENqVD7oLz+8KZZOjHPT 5DCg== X-Gm-Message-State: AOAM532/fohabw1rmkL30Fd+TdZKSoeVOXytGa1TJzQhuAUF28ZRivvK QtXiZGhA/JHmp5cu7TwZPY74hHrGi3w= X-Google-Smtp-Source: ABdhPJwnL64ODXCgxY++i1SIP9VGp1r7N6GtiKb2tQIImcvLSD6syNMZ+9fnUMwmpdtccHgA1Ywvog== X-Received: by 2002:a1c:f60c:: with SMTP id w12mr8843491wmc.3.1629890785876; Wed, 25 Aug 2021 04:26:25 -0700 (PDT) Received: from localhost.localdomain ([85.255.232.117]) by smtp.gmail.com with ESMTPSA id b12sm25113730wrx.72.2021.08.25.04.26.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 04:26:25 -0700 (PDT) From: Pavel Begunkov To: Jens Axboe , io-uring@vger.kernel.org, Josh Triplett Cc: "David S . Miller" , Jakub Kicinski , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Stefan Metzmacher Subject: [PATCH v4 1/4] net: add accept helper not installing fd Date: Wed, 25 Aug 2021 12:25:44 +0100 Message-Id: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Introduce and reuse a helper that acts similarly to __sys_accept4_file() but returns struct file instead of installing file descriptor. Will be used by io_uring. Signed-off-by: Pavel Begunkov Acked-by: Jakub Kicinski Signed-off-by: Jens Axboe Acked-by: David S. Miller --- include/linux/socket.h | 3 ++ net/socket.c | 71 ++++++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/include/linux/socket.h b/include/linux/socket.h index 0d8e3dcb7f88..d3c1a42a2edd 100644 --- a/include/linux/socket.h +++ b/include/linux/socket.h @@ -421,6 +421,9 @@ extern int __sys_accept4_file(struct file *file, unsigned file_flags, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen, int flags, unsigned long nofile); +extern struct file *do_accept(struct file *file, unsigned file_flags, + struct sockaddr __user *upeer_sockaddr, + int __user *upeer_addrlen, int flags); extern int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen, int flags); extern int __sys_socket(int family, int type, int protocol); diff --git a/net/socket.c b/net/socket.c index 0b2dad3bdf7f..532fff5a3684 100644 --- a/net/socket.c +++ b/net/socket.c @@ -1722,32 +1722,22 @@ SYSCALL_DEFINE2(listen, int, fd, int, backlog) return __sys_listen(fd, backlog); } -int __sys_accept4_file(struct file *file, unsigned file_flags, +struct file *do_accept(struct file *file, unsigned file_flags, struct sockaddr __user *upeer_sockaddr, - int __user *upeer_addrlen, int flags, - unsigned long nofile) + int __user *upeer_addrlen, int flags) { struct socket *sock, *newsock; struct file *newfile; - int err, len, newfd; + int err, len; struct sockaddr_storage address; - if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) - return -EINVAL; - - if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) - flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; - sock = sock_from_file(file); - if (!sock) { - err = -ENOTSOCK; - goto out; - } + if (!sock) + return ERR_PTR(-ENOTSOCK); - err = -ENFILE; newsock = sock_alloc(); if (!newsock) - goto out; + return ERR_PTR(-ENFILE); newsock->type = sock->type; newsock->ops = sock->ops; @@ -1758,18 +1748,9 @@ int __sys_accept4_file(struct file *file, unsigned file_flags, */ __module_get(newsock->ops->owner); - newfd = __get_unused_fd_flags(flags, nofile); - if (unlikely(newfd < 0)) { - err = newfd; - sock_release(newsock); - goto out; - } newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name); - if (IS_ERR(newfile)) { - err = PTR_ERR(newfile); - put_unused_fd(newfd); - goto out; - } + if (IS_ERR(newfile)) + return newfile; err = security_socket_accept(sock, newsock); if (err) @@ -1794,16 +1775,38 @@ int __sys_accept4_file(struct file *file, unsigned file_flags, } /* File flags are not inherited via accept() unlike another OSes. */ - - fd_install(newfd, newfile); - err = newfd; -out: - return err; + return newfile; out_fd: fput(newfile); - put_unused_fd(newfd); - goto out; + return ERR_PTR(err); +} + +int __sys_accept4_file(struct file *file, unsigned file_flags, + struct sockaddr __user *upeer_sockaddr, + int __user *upeer_addrlen, int flags, + unsigned long nofile) +{ + struct file *newfile; + int newfd; + if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) + return -EINVAL; + + if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) + flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; + + newfd = __get_unused_fd_flags(flags, nofile); + if (unlikely(newfd < 0)) + return newfd; + + newfile = do_accept(file, file_flags, upeer_sockaddr, upeer_addrlen, + flags); + if (IS_ERR(newfile)) { + put_unused_fd(newfd); + return PTR_ERR(newfile); + } + fd_install(newfd, newfile); + return newfd; } /* From patchwork Wed Aug 25 11:25:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12457329 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 X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E20F8C432BE for ; Wed, 25 Aug 2021 11:26:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CA81261153 for ; Wed, 25 Aug 2021 11:26:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240481AbhHYL1W (ORCPT ); Wed, 25 Aug 2021 07:27:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240361AbhHYL1Q (ORCPT ); Wed, 25 Aug 2021 07:27:16 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26DF7C061757; Wed, 25 Aug 2021 04:26:28 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id d22-20020a1c1d16000000b002e7777970f0so4022447wmd.3; Wed, 25 Aug 2021 04:26:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2vpCWvHyDYTvLG1B0GApP4brIQRde2l3CEnEkrOSsDo=; b=dSFz6IwRJAwNIbimQvTEj9Iaiu64VTJSr0/wnntJuse0OpH12yWFgz6o7KADyDEhsO kWu+YIzbvFbnLGaZ2qKcqavxHjz3PVfZJn9FlOnDYPTgU+27jN4POSxFPUVSWldhVdvF 43OEdjnnEtNlnkHNQCiyafIM6yCx63pPek6gHkHWiwyreCcn5joY8FphQjXQFdvb+7Ar mW8WYP2vNJBGfKDqvv6b+5hUzvkQN5MqK3cwkLqaztTyzBlGv5H1qGAyeg0nSSUyTttF /NoAnJ63yNGZq/Gxf2qY5CCWCLE3An7WkSERMfDrmtBbiLsKKNHpyrCZy41/pJ6+JnRq IIfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2vpCWvHyDYTvLG1B0GApP4brIQRde2l3CEnEkrOSsDo=; b=lc++U2mjXtk0PuJUyubgtACKzZf6pyHrnqvd3SjDRXpuDMUTtysTVARNfNIK7M7DUp QQLTDmr+wWPndxXgbHXVZ+Fwa51OsPaBbt6S3qM7g9jxCXz0K05tffAVj+9FENH4drH7 xuQXDgkCCSkZAOqGmC2sjFzGcN69Chlsq0U72MJ7E/vNhUu5dkuQzDZc/yptrn2ELEH0 VyDD9ciqkpO2e+oljyUz6+8b9dGZqE4p8ulXwSJeB62HKKh1Hx+BUQB/4TdFs7S/BfK5 CzoGQ1RkP4k692S/A9dKvgrclfyYv7sNanekhD7RlYHIS38PLDWynJ5osKdXQ/2PtAdq RySw== X-Gm-Message-State: AOAM533tNwdYueWk3tMn0cPZ9vR+aoeDv4ZJ0yUGX++2gqXPVAQOtmEn RQ0BvYcjSJWAAYFvT0shHsorwNbdzHA= X-Google-Smtp-Source: ABdhPJxJw8laZj1s8hymX8855Xqbep4iYI30HVaaaXTUDCtXvaAi33Is+p8aFcyZG4eo2fQtA7sFcQ== X-Received: by 2002:a1c:f414:: with SMTP id z20mr8941467wma.94.1629890786701; Wed, 25 Aug 2021 04:26:26 -0700 (PDT) Received: from localhost.localdomain ([85.255.232.117]) by smtp.gmail.com with ESMTPSA id b12sm25113730wrx.72.2021.08.25.04.26.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 04:26:26 -0700 (PDT) From: Pavel Begunkov To: Jens Axboe , io-uring@vger.kernel.org, Josh Triplett Cc: "David S . Miller" , Jakub Kicinski , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Stefan Metzmacher Subject: [PATCH v4 2/4] io_uring: openat directly into fixed fd table Date: Wed, 25 Aug 2021 12:25:45 +0100 Message-Id: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Instead of opening a file into a process's file table as usual and then registering the fd within io_uring, some users may want to skip the first step and place it directly into io_uring's fixed file table. This patch adds such a capability for IORING_OP_OPENAT and IORING_OP_OPENAT2. The behaviour is controlled by setting sqe->file_index, where 0 implies the old behaviour using normal file tables. If non-zero value is specified, then it will behave as described and place the file into a fixed file slot sqe->file_index - 1. A file table should be already created, the slot should be valid and empty, otherwise the operation will fail. Keep the error codes consistent with IORING_OP_FILES_UPDATE, ENXIO and EINVAL on inappropriate fixed tables, and return EBADF on collision with already registered file. Note: IOSQE_FIXED_FILE can't be used to switch between modes, because accept takes a file, and it already uses the flag with a different meaning. Suggested-by: Josh Triplett Signed-off-by: Pavel Begunkov Signed-off-by: Jens Axboe --- fs/io_uring.c | 74 +++++++++++++++++++++++++++++++---- include/uapi/linux/io_uring.h | 5 ++- 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 11462f217aee..2ef81cd6a0ec 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -579,6 +579,7 @@ struct io_sr_msg { struct io_open { struct file *file; int dfd; + u32 file_slot; struct filename *filename; struct open_how how; unsigned long nofile; @@ -1062,6 +1063,9 @@ static void io_req_task_queue(struct io_kiocb *req); static void io_submit_flush_completions(struct io_ring_ctx *ctx); static int io_req_prep_async(struct io_kiocb *req); +static int io_install_fixed_file(struct io_kiocb *req, struct file *file, + unsigned int issue_flags, u32 slot_index); + static struct kmem_cache *req_cachep; static const struct file_operations io_uring_fops; @@ -3863,7 +3867,7 @@ static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) return -EINVAL; - if (unlikely(sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)) + if (unlikely(sqe->ioprio || sqe->buf_index)) return -EINVAL; if (unlikely(req->flags & REQ_F_FIXED_FILE)) return -EBADF; @@ -3880,6 +3884,11 @@ static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe req->open.filename = NULL; return ret; } + + req->open.file_slot = READ_ONCE(sqe->file_index); + if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) + return -EINVAL; + req->open.nofile = rlimit(RLIMIT_NOFILE); req->flags |= REQ_F_NEED_CLEANUP; return 0; @@ -3917,8 +3926,8 @@ static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) { struct open_flags op; struct file *file; - bool nonblock_set; - bool resolve_nonblock; + bool resolve_nonblock, nonblock_set; + bool fixed = !!req->open.file_slot; int ret; ret = build_open_flags(&req->open.how, &op); @@ -3937,9 +3946,11 @@ static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) op.open_flag |= O_NONBLOCK; } - ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); - if (ret < 0) - goto err; + if (!fixed) { + ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); + if (ret < 0) + goto err; + } file = do_filp_open(req->open.dfd, req->open.filename, &op); if (IS_ERR(file)) { @@ -3948,7 +3959,8 @@ static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) * marginal gain for something that is now known to be a slower * path. So just put it, and we'll get a new one when we retry. */ - put_unused_fd(ret); + if (!fixed) + put_unused_fd(ret); ret = PTR_ERR(file); /* only retry if RESOLVE_CACHED wasn't already set by application */ @@ -3961,7 +3973,12 @@ static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) file->f_flags &= ~O_NONBLOCK; fsnotify_open(file); - fd_install(ret, file); + + if (!fixed) + fd_install(ret, file); + else + ret = io_install_fixed_file(req, file, issue_flags, + req->open.file_slot - 1); err: putname(req->open.filename); req->flags &= ~REQ_F_NEED_CLEANUP; @@ -7896,6 +7913,46 @@ static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, #endif } +static int io_install_fixed_file(struct io_kiocb *req, struct file *file, + unsigned int issue_flags, u32 slot_index) +{ + struct io_ring_ctx *ctx = req->ctx; + bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; + struct io_fixed_file *file_slot; + int ret = -EBADF; + + io_ring_submit_lock(ctx, !force_nonblock); + if (file->f_op == &io_uring_fops) + goto err; + ret = -ENXIO; + if (!ctx->file_data) + goto err; + ret = -EINVAL; + if (slot_index >= ctx->nr_user_files) + goto err; + + slot_index = array_index_nospec(slot_index, ctx->nr_user_files); + file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); + ret = -EBADF; + if (file_slot->file_ptr) + goto err; + + *io_get_tag_slot(ctx->file_data, slot_index) = 0; + io_fixed_file_set(file_slot, file); + ret = io_sqe_file_register(ctx, file, slot_index); + if (ret) { + file_slot->file_ptr = 0; + goto err; + } + + ret = 0; +err: + io_ring_submit_unlock(ctx, !force_nonblock); + if (ret) + fput(file); + return ret; +} + static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, struct io_rsrc_node *node, void *rsrc) { @@ -10363,6 +10420,7 @@ static int __init io_uring_init(void) BUILD_BUG_SQE_ELEM(40, __u16, buf_group); BUILD_BUG_SQE_ELEM(42, __u16, personality); BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); + BUILD_BUG_SQE_ELEM(44, __u32, file_index); BUILD_BUG_ON(sizeof(struct io_uring_files_update) != sizeof(struct io_uring_rsrc_update)); diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 79126d5cd289..45a4f2373694 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -55,7 +55,10 @@ struct io_uring_sqe { } __attribute__((packed)); /* personality to use, if used */ __u16 personality; - __s32 splice_fd_in; + union { + __s32 splice_fd_in; + __u32 file_index; + }; __u64 __pad2[2]; }; From patchwork Wed Aug 25 11:25:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12457331 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 X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5DC2C4338F for ; Wed, 25 Aug 2021 11:26:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8E374610D1 for ; Wed, 25 Aug 2021 11:26:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240529AbhHYL1Z (ORCPT ); Wed, 25 Aug 2021 07:27:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240359AbhHYL1Q (ORCPT ); Wed, 25 Aug 2021 07:27:16 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFD26C0613C1; Wed, 25 Aug 2021 04:26:28 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id u9so974668wrg.8; Wed, 25 Aug 2021 04:26:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cBkEN0P8k5mEHRmkbodUOJHAKqSoKNmp6ynbLEfPluU=; b=P+TyQnz4XSV0F8eL0RvmM8DBJSOnwFWa3CKhAo9JeRT4DhKzPon5PlkJt+jCbi7iKE Mqr4Ohz7mbwJulwVHWEjUxAQlKNpGYAB4FmG6kdYoeup8itKw6V4SWHplEUbTgcbnwX7 EOTLS7mGLQdcV9DIWQFKEeCzAYnw/CyJe/4Mz1CNfsxJWyCvhAAGK4GqOnWGlIVWyN5D KGNdUnn2BE+gbeagTo3NbX4YEJ6fG4USAV9tBgZhHnmwHn5Jp79pLYmuzQv1FrfBgOd2 6JKWAzxUmP7uuKCivBoTDAGoWHaTZ8Pe4Uq4uY+BKebEmO16XlofkES2aMmNYFxoZsAI Vg4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cBkEN0P8k5mEHRmkbodUOJHAKqSoKNmp6ynbLEfPluU=; b=B1hLw4Pq844c+PUEug29uVTGkzISBOdy69HE3h5w8nBBkq8fiBsagII44njB2b50mQ nCXcEgUxAL2c9oJaJzXpklQk1STsoKGkO43GUgxeVBkvceuOHpItjdF/lVk8KD6LFFTj /oD3LEIUY7nbzjjV0SYzShQzKa1ZZ0ARgPKgOXyU+MSnLhUrDIC4yHAU3JzYv+HTYH9J G+OUdchmBHsRBpzQQ1/KKENieAgerPqDjsLKLac30dNGQzMIETYfgnS+McITZf31JRbc bKnUOR3HQVif3Cxccl64sgeyC36MqIoETcD6C55mKI5HitLfH/7loQ1+HvZf7ggslqeb 19fA== X-Gm-Message-State: AOAM530t6KBhhH4FmCO/wgLpxC9FSKYQ8oc28vA6jr6y7YUWi09JYhtM +ucqXmH9qmveOdcz5xKMmVw= X-Google-Smtp-Source: ABdhPJxgEsN1RiTYPUz2QS4YIxv0bzCKfWT5uPK5tpDUtVP0zIUHCUs8taPQeuOaQnEDgzWrnYY55Q== X-Received: by 2002:adf:f984:: with SMTP id f4mr22285152wrr.331.1629890787591; Wed, 25 Aug 2021 04:26:27 -0700 (PDT) Received: from localhost.localdomain ([85.255.232.117]) by smtp.gmail.com with ESMTPSA id b12sm25113730wrx.72.2021.08.25.04.26.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 04:26:27 -0700 (PDT) From: Pavel Begunkov To: Jens Axboe , io-uring@vger.kernel.org, Josh Triplett Cc: "David S . Miller" , Jakub Kicinski , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Stefan Metzmacher Subject: [PATCH v4 3/4] io_uring: hand code io_accept() fd installing Date: Wed, 25 Aug 2021 12:25:46 +0100 Message-Id: <5b73d204caa0ce979ccb98136695b60f52a3d98c.1629888991.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Make io_accept() to handle file descriptor allocations and installation. A preparation patch for bypassing file tables. Signed-off-by: Pavel Begunkov Signed-off-by: Jens Axboe --- fs/io_uring.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 2ef81cd6a0ec..a3b1a50e2537 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -4809,6 +4809,11 @@ static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); accept->flags = READ_ONCE(sqe->accept_flags); accept->nofile = rlimit(RLIMIT_NOFILE); + + if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) + return -EINVAL; + if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) + accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; return 0; } @@ -4817,20 +4822,28 @@ static int io_accept(struct io_kiocb *req, unsigned int issue_flags) struct io_accept *accept = &req->accept; bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; - int ret; + struct file *file; + int ret, fd; if (req->file->f_flags & O_NONBLOCK) req->flags |= REQ_F_NOWAIT; - ret = __sys_accept4_file(req->file, file_flags, accept->addr, - accept->addr_len, accept->flags, - accept->nofile); - if (ret == -EAGAIN && force_nonblock) - return -EAGAIN; - if (ret < 0) { + fd = __get_unused_fd_flags(accept->flags, accept->nofile); + if (unlikely(fd < 0)) + return fd; + + file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, + accept->flags); + if (IS_ERR(file)) { + ret = PTR_ERR(file); + if (ret == -EAGAIN && force_nonblock) + return -EAGAIN; if (ret == -ERESTARTSYS) ret = -EINTR; req_set_fail(req); + } else { + fd_install(fd, file); + ret = fd; } __io_req_complete(req, issue_flags, ret, 0); return 0; From patchwork Wed Aug 25 11:25:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 12457333 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 X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A8EAC43214 for ; Wed, 25 Aug 2021 11:26:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 49F0861165 for ; Wed, 25 Aug 2021 11:26:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240558AbhHYL1a (ORCPT ); Wed, 25 Aug 2021 07:27:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240369AbhHYL1Q (ORCPT ); Wed, 25 Aug 2021 07:27:16 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6886C0613D9; Wed, 25 Aug 2021 04:26:29 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id m25-20020a7bcb99000000b002e751bcb5dbso4318296wmi.5; Wed, 25 Aug 2021 04:26:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EkgdpcPWaQPjEyyP5RTALMN71ozX4FyAQ9zUilfKv9k=; b=ombk6WXWAbKL5ZvqCCbUUAAy0f2OT3fScYtcHpNvJZzYHP1DPeenonTVtjiLP2K1cG hOBd/wgK0pBfK9e4+qJcfSAx8iH89OPpl1X46Zc+rzZ10Mf4GSxSGEgLDj7DF+OYPE5n H0DnF6N+p1XPFsXakfrJk72CqdSWBwTkrjzCPFHVWOXVsEBsPtpuWFdaEMKKEWogXspp AscfqXOrYLL/0Sqa9yGMisUNEXKmmGkbAOgURrwf3r8BJYeNNmHeip98e1/bBSCSgbsW 88Vfu0818WEo5Fxx+I1rAkgDlTKniOzXn/pirgwoqADH9M0bFsvgMOgT+0e5yF+TTp5G RRCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EkgdpcPWaQPjEyyP5RTALMN71ozX4FyAQ9zUilfKv9k=; b=Jqk/CbSSWwasGcVVloxdlLSN4Ji5iKuLUfc+xeLAAvN9IE2VBUWUt/6UBQ2MsjTNpp EV/7BvEMHc5Be9Pc4NgxiPtpenHbdj8fRuKaGmAxea+ZpJKXkD2Ru8KxIwc2g38BGtAh k9s/AOmO8ygvlvaN/gCfrb3UwuoWbBuh0QvQ9ZyJs6N3ganvla2kA2KDaMohiwNPOkvm /Pc1pVlt1E3Skpq7Nijf0LES0T3cG9oRqgn27Tt5T45l0UgWiOaW1/J8cA2bwD2yIXDJ MnGyqGoEjQFVEj0eAsfwl8HbE0Cie48HVs4zuZyi5A43Dy+F5gH+GqkWGnNzKlPBtUHR 5eyA== X-Gm-Message-State: AOAM533rPSLlVOVAGTnk5c3UBnZa7PjdpY4hl3D3w7GO8ufaN9YKKCoW XR7r7grBbkcBne9sSc6Dyq8= X-Google-Smtp-Source: ABdhPJwPZRSGH5av6ne3wi2OiigUm3ipOBRr+qdPfBcoqtVkb/BaOWeTdifSQ/8S0MdMC2nto0+KPg== X-Received: by 2002:a1c:9acc:: with SMTP id c195mr8967002wme.69.1629890788478; Wed, 25 Aug 2021 04:26:28 -0700 (PDT) Received: from localhost.localdomain ([85.255.232.117]) by smtp.gmail.com with ESMTPSA id b12sm25113730wrx.72.2021.08.25.04.26.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 04:26:28 -0700 (PDT) From: Pavel Begunkov To: Jens Axboe , io-uring@vger.kernel.org, Josh Triplett Cc: "David S . Miller" , Jakub Kicinski , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Stefan Metzmacher Subject: [PATCH v4 4/4] io_uring: accept directly into fixed file table Date: Wed, 25 Aug 2021 12:25:47 +0100 Message-Id: <6d16163f376fac7ac26a656de6b42199143e9721.1629888991.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org As done with open opcodes, allow accept to skip installing fd into processes' file tables and put it directly into io_uring's fixed file table. Same restrictions and design as for open. Suggested-by: Josh Triplett Signed-off-by: Pavel Begunkov Signed-off-by: Jens Axboe --- fs/io_uring.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index a3b1a50e2537..95fd7dc7cbe6 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -514,6 +514,7 @@ struct io_accept { struct sockaddr __user *addr; int __user *addr_len; int flags; + u32 file_slot; unsigned long nofile; }; @@ -4802,7 +4803,7 @@ static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) return -EINVAL; - if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->splice_fd_in) + if (sqe->ioprio || sqe->len || sqe->buf_index) return -EINVAL; accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); @@ -4810,6 +4811,10 @@ static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) accept->flags = READ_ONCE(sqe->accept_flags); accept->nofile = rlimit(RLIMIT_NOFILE); + accept->file_slot = READ_ONCE(sqe->file_index); + if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || + (accept->flags & SOCK_CLOEXEC))) + return -EINVAL; if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) return -EINVAL; if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) @@ -4822,28 +4827,35 @@ static int io_accept(struct io_kiocb *req, unsigned int issue_flags) struct io_accept *accept = &req->accept; bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; + bool fixed = !!accept->file_slot; struct file *file; int ret, fd; if (req->file->f_flags & O_NONBLOCK) req->flags |= REQ_F_NOWAIT; - fd = __get_unused_fd_flags(accept->flags, accept->nofile); - if (unlikely(fd < 0)) - return fd; - + if (!fixed) { + fd = __get_unused_fd_flags(accept->flags, accept->nofile); + if (unlikely(fd < 0)) + return fd; + } file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, accept->flags); if (IS_ERR(file)) { + if (!fixed) + put_unused_fd(fd); ret = PTR_ERR(file); if (ret == -EAGAIN && force_nonblock) return -EAGAIN; if (ret == -ERESTARTSYS) ret = -EINTR; req_set_fail(req); - } else { + } else if (!fixed) { fd_install(fd, file); ret = fd; + } else { + ret = io_install_fixed_file(req, file, issue_flags, + accept->file_slot - 1); } __io_req_complete(req, issue_flags, ret, 0); return 0;