From patchwork Sat Aug 21 15:52:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 501302 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 E1261C432BE for ; Sat, 21 Aug 2021 15:53:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C360461206 for ; Sat, 21 Aug 2021 15:53:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234358AbhHUPyO (ORCPT ); Sat, 21 Aug 2021 11:54:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232711AbhHUPyB (ORCPT ); Sat, 21 Aug 2021 11:54:01 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53259C061756; Sat, 21 Aug 2021 08:53:21 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id x12so18695338wrr.11; Sat, 21 Aug 2021 08:53:21 -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=EaXGPCv4rG5J1NZC9xHyYL1z2IpeWIiNJsgYxDUUWOE=; b=WStex6K3KqJn+YrCKUpF27VJnmqTe5HVUbrKrF5sSVxa8d1CKtlPyDrQZCUHpQ/N37 khDZTaioYPkmpRYbECVI4hO/9uWkIsgeM7Z6FvwPEoxjkjOnC1COP0FqK2bxx/ODxktz nl8HUQ1JK0RtQQZFCQVPurVZL/T2i351r47hjqHuqAAlCRhhSL9CUk2VdoCAwS5aGDYq u7MCFTyjFSFPxW8dQj6IiuwRIcoP/l89d3nqVTwyWY8Z+jl2PY9jd6MYFXmpmv8U8Bfk Y1L21vJpaq/REPGzWP1O0PxYQzOjbGiuENNINKIUYmvKMhnh3/3T6nFtjsFrgbIDIkVm bn3g== 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=EaXGPCv4rG5J1NZC9xHyYL1z2IpeWIiNJsgYxDUUWOE=; b=jUTvW/ylSh8Gw1NL/0oLmsUAl3WpaFjGeCFfNb1dyQKsvaRPHABcCMy/DIk1hOUPmU 4c1HgvP8xjTAUD0efUvpnSA/esKEAGOarT/feToA4Ed+Rmd1GTYt87HSwVfX1VL7USbA 06JD1HNvVPE+24enIb26ngqJYq8XDQ9U4nSisZp123ZpvVKU+t4XCoX/KvvmqxwE0xcB 64a3crUKRcHoCYb6K/dUqVFYzfik+lQ0zVBg39l/WwzagHiRzFCtna/JqqLqLyeqV9iw L4WszOUvR8MfebUo0bdEwyIqHuyb6gm9CFzPQ9/Dm1cnvrf/rV9Frml3wkZWcjdR5aHF TzYA== X-Gm-Message-State: AOAM533tlbvjyfm4FsHCz1r503sLr/mfbBAWH3OPWf3UY1PlNLDKbOdE cU1wqLGRWsPHQVHaS32xwqg= X-Google-Smtp-Source: ABdhPJwh8JwR54YZAijqIKivJ2KfYh4N1PejPXdKciXvhfxqm3kBKwpK7LwKnT+oRQnd4vglVGDqZQ== X-Received: by 2002:a5d:618c:: with SMTP id j12mr4462461wru.374.1629561199997; Sat, 21 Aug 2021 08:53:19 -0700 (PDT) Received: from localhost.localdomain ([85.255.233.174]) by smtp.gmail.com with ESMTPSA id e3sm9479554wro.15.2021.08.21.08.53.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Aug 2021 08:53:19 -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 v3 1/4] net: add accept helper not installing fd Date: Sat, 21 Aug 2021 16:52:37 +0100 Message-Id: <0c5d77e34ebff09f1f2f6b9bff15b97ec8fbf8ca.1629559905.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 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 --- 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 Sat Aug 21 15:52:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 501303 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 ECE23C432BE for ; Sat, 21 Aug 2021 15:53:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C398961251 for ; Sat, 21 Aug 2021 15:53:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234680AbhHUPyR (ORCPT ); Sat, 21 Aug 2021 11:54:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232716AbhHUPyC (ORCPT ); Sat, 21 Aug 2021 11:54:02 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53483C061757; Sat, 21 Aug 2021 08:53:22 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id c129-20020a1c35870000b02902e6b6135279so7900825wma.0; Sat, 21 Aug 2021 08:53:22 -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=nGJoaHfhVSZ+JCi269k+ZI1M+4T/MONcq2oDO9wuY24=; b=HzudfrGwr/GYlsmgCwb2Njw+SHqpNnWBmeds9bc48oK98z5emcXeDKZtWfGME+Emrw 5Q9uNVUBRiZ/UibCxFMpiG6vQRqAdrqDEOgedhpN9XopquhJ7lTtfNy0c5p5njaPekPk 0zYCbE/b3WnBEa/elQObm2Ob98AGPISkGfaWO12/5bW9HdJyZwwW4TXd6GIEpYXelIX/ AlOZ08WCrthDxfoda2pvriqsnagjzBrAl60QrOkqQpQpliyXfRa2I4oQP4+2IVbd3XKw d5IMfatMBug/CjIiZSmY/Bi5dScWoXt0mHfDZYM/2MF4/Ognl5tPbfyIneLQm0Lzk4Da I1Hw== 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=nGJoaHfhVSZ+JCi269k+ZI1M+4T/MONcq2oDO9wuY24=; b=RfTE8MO6ilUXyG+cv/vvHVXST9Dyx7CItgk0/kypQc77GL+CbO02hQru7eo1N2mzgR 00kr4laBNoEw1WRBewszrbw7pq0jTqUpWEH4XT7tB7nIRIRmXmPV3c69wdNajF1ZFBeO cgwjwNQRJSpYpNDwy6+AGN+5zvfh/dX/KVXkoCqqo29vl3mmXJVPz54b0Dp9Eqy6oTXG f5y65pU9evgQndMQzMYFQxs+UxY/65gnxrZ/3sK+K/T1OSUUeq4mspDWph/UJOMCDZ/m M5n/TksYzdWt41pk7tBBJWJ6nBEobEc4V7lJnaRoVyZjAPJjeHr3V7xx+qFy8zOBZAqq yCTQ== X-Gm-Message-State: AOAM533RNNz0U2SmbMix7Vmk8Wzl78o4rNu9YS7QzSoHYJvmL85k4uqi IHTneBwKsOQ9aYB0sLdo4sU= X-Google-Smtp-Source: ABdhPJxBhWaygH+gnsNxHsK5FxlSsZdMYeLbDn5XGvDBovpw75VwSiT9qyyHqruufARZPlY9WYuKRA== X-Received: by 2002:a05:600c:210d:: with SMTP id u13mr8736217wml.57.1629561200931; Sat, 21 Aug 2021 08:53:20 -0700 (PDT) Received: from localhost.localdomain ([85.255.233.174]) by smtp.gmail.com with ESMTPSA id e3sm9479554wro.15.2021.08.21.08.53.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Aug 2021 08:53:20 -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 v3 2/4] io_uring: openat directly into fixed fd table Date: Sat, 21 Aug 2021 16:52:38 +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. 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: we can't use IOSQE_FIXED_FILE 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 --- fs/io_uring.c | 82 +++++++++++++++++++++++++++++++---- include/uapi/linux/io_uring.h | 5 ++- 2 files changed, 78 insertions(+), 9 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 0fb75aa72b69..b8ef5ac1f90d 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -1063,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); + static struct kmem_cache *req_cachep; static const struct file_operations io_uring_fops; @@ -3815,11 +3818,12 @@ static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) { const char __user *fname; + unsigned index; int ret; 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; @@ -3836,6 +3840,16 @@ static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe req->open.filename = NULL; return ret; } + + index = READ_ONCE(sqe->file_index); + req->buf_index = index; + if (index) { + if (req->open.how.flags & O_CLOEXEC) + return -EINVAL; + if (req->buf_index != index) + return -EINVAL; + } + req->open.nofile = rlimit(RLIMIT_NOFILE); req->flags |= REQ_F_NEED_CLEANUP; return 0; @@ -3873,8 +3887,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->buf_index; int ret; ret = build_open_flags(&req->open.how, &op); @@ -3893,9 +3907,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)) { @@ -3904,7 +3920,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 */ @@ -3917,7 +3934,11 @@ 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); err: putname(req->open.filename); req->flags &= ~REQ_F_NEED_CLEANUP; @@ -7846,6 +7867,50 @@ 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) +{ + struct io_ring_ctx *ctx = req->ctx; + int i = req->buf_index - 1; + bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; + struct io_fixed_file *file_slot; + int ret = -EBADF; + + if (WARN_ON_ONCE(req->buf_index == 0)) + goto err; + + 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 (i >= ctx->nr_user_files) + goto err; + + i = array_index_nospec(i, ctx->nr_user_files); + file_slot = io_fixed_file_slot(&ctx->file_table, i); + ret = -EBADF; + if (file_slot->file_ptr) + goto err; + + *io_get_tag_slot(ctx->file_data, i) = 0; + io_fixed_file_set(file_slot, file); + ret = io_sqe_file_register(ctx, file, i); + 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) { @@ -10311,6 +10376,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]; };