From patchwork Fri Dec 29 21:24:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10137401 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E48B66020A for ; Fri, 29 Dec 2017 21:25:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C101C2E30C for ; Fri, 29 Dec 2017 21:25:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B36AF2E317; Fri, 29 Dec 2017 21:25:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0FEE22E30F for ; Fri, 29 Dec 2017 21:25:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750762AbdL2VY6 (ORCPT ); Fri, 29 Dec 2017 16:24:58 -0500 Received: from mail-io0-f194.google.com ([209.85.223.194]:45433 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750741AbdL2VY5 (ORCPT ); Fri, 29 Dec 2017 16:24:57 -0500 Received: by mail-io0-f194.google.com with SMTP id e204so42218268iof.12 for ; Fri, 29 Dec 2017 13:24:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=FFFem9OanCbNAYAhEdE4S+c7JpS8wbnc87YzcxP4ZV0=; b=hSd9h2BBnvuhuOAw0IdQ4P8jt820kRTMciYdYb959QS24qhDYZGy9x9NFCF2HgFOP5 xVdg4JcWrDrAg6hvdD77hUD4QrbITUCNVv3blKlkxcveJ48suZ+9NoUN8gB2pBmqOBy+ hP9zGue5QPUJCSij9Nt/S3h20xxtJDhbYwp82Q10MlByG79rOU78C9X9T2v6boLSCREU Km83+HalVCRUV9Pv3Bc1CrpmkISd5n0Lj027vjBwB4uoVbqf7ud7SCB3bsW1J9eXyLwS dMxKIuUiVq/Cx+oLlxcatgEVSZGnqRWHjXnT/9NQzjQc55KgQOSGNBqb1GPfkQ5/icRz NalA== 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; bh=FFFem9OanCbNAYAhEdE4S+c7JpS8wbnc87YzcxP4ZV0=; b=QlAyi9RCfROSDnvcTTkoctO/EHbu64gD13mkhGsBuxhF5qJVB88tZfTU+fWAG3Z5i3 yAwPjbYgSMt8PThq33CQoHndqisBwI7NDw/c6twEBvHC7/fUMFc+bRLCLyWnisUYk+8x lDRE9M4H5B/XUBg2GQr07WncRNuqc2eg9ILp17N6yO5DHQWv4l5VzyrJ87rapRx1d7ms kN9Oa+6gTwgehaGqSSjOoFpfLBVNorc/6P/zHC4bPD+SfX2IO3RYudEsoSZUxNLaU5x/ sKW4X9+kSiQuoPxghcTfG9vPCCCn79+A34EhVywW8kQF1DbHc2WqPLwpqT6onvHPLQke JosQ== X-Gm-Message-State: AKGB3mIJFLME7WsfdWg18YUIeT917ZljDw2//NxwNJcScMCDD7z4o2+l 0Uaz7SLxrE9vqU881m6Pswk= X-Google-Smtp-Source: ACJfBosyAYGD/NtV0D9N45ZtUxkFRy5ADBpBXqVx6NCoUpW9UoGaWhni68DK9hK3ew0jlmJ0s9WuDQ== X-Received: by 10.107.19.104 with SMTP id b101mr49585679ioj.297.1514582696802; Fri, 29 Dec 2017 13:24:56 -0800 (PST) Received: from zzz.Home (h184-60-19-231.mdsnwi.dsl.dynamic.tds.net. [184.60.19.231]) by smtp.gmail.com with ESMTPSA id v62sm20212802iod.83.2017.12.29.13.24.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 29 Dec 2017 13:24:56 -0800 (PST) From: Eric Biggers To: linux-mm@kvack.org Cc: linux-fsdevel@vger.kernel.org, Andrea Arcangeli , Andrew Morton , Eric Biggers Subject: [PATCH] userfaultfd: convert to use anon_inode_getfd() Date: Fri, 29 Dec 2017 15:24:03 -0600 Message-Id: <20171229212403.22800-1-ebiggers3@gmail.com> X-Mailer: git-send-email 2.15.1 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers Nothing actually calls userfaultfd_file_create() besides the userfaultfd() system call itself. So simplify things by folding it into the system call and using anon_inode_getfd() instead of anon_inode_getfile(). Do the same in resolve_userfault_fork() as well. This removes over 50 lines with no change in functionality. Signed-off-by: Eric Biggers Reviewed-by: Mike Rapoport --- fs/userfaultfd.c | 70 ++++++++------------------------------------------------ 1 file changed, 9 insertions(+), 61 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 41a75f9f23fd..b87cc2c5cfb1 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -985,24 +985,14 @@ static int resolve_userfault_fork(struct userfaultfd_ctx *ctx, struct uffd_msg *msg) { int fd; - struct file *file; - unsigned int flags = new->flags & UFFD_SHARED_FCNTL_FLAGS; - fd = get_unused_fd_flags(flags); + fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, new, + O_RDWR | (new->flags & UFFD_SHARED_FCNTL_FLAGS)); if (fd < 0) return fd; - file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, new, - O_RDWR | flags); - if (IS_ERR(file)) { - put_unused_fd(fd); - return PTR_ERR(file); - } - - fd_install(fd, file); msg->arg.reserved.reserved1 = 0; msg->arg.fork.ufd = fd; - return 0; } @@ -1884,24 +1874,10 @@ static void init_once_userfaultfd_ctx(void *mem) seqcount_init(&ctx->refile_seq); } -/** - * userfaultfd_file_create - Creates a userfaultfd file pointer. - * @flags: Flags for the userfaultfd file. - * - * This function creates a userfaultfd file pointer, w/out installing - * it into the fd table. This is useful when the userfaultfd file is - * used during the initialization of data structures that require - * extra setup after the userfaultfd creation. So the userfaultfd - * creation is split into the file pointer creation phase, and the - * file descriptor installation phase. In this way races with - * userspace closing the newly installed file descriptor can be - * avoided. Returns a userfaultfd file pointer, or a proper error - * pointer. - */ -static struct file *userfaultfd_file_create(int flags) +SYSCALL_DEFINE1(userfaultfd, int, flags) { - struct file *file; struct userfaultfd_ctx *ctx; + int fd; BUG_ON(!current->mm); @@ -1909,14 +1885,12 @@ static struct file *userfaultfd_file_create(int flags) BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC); BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK); - file = ERR_PTR(-EINVAL); if (flags & ~UFFD_SHARED_FCNTL_FLAGS) - goto out; + return -EINVAL; - file = ERR_PTR(-ENOMEM); ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL); if (!ctx) - goto out; + return -ENOMEM; atomic_set(&ctx->refcount, 1); ctx->flags = flags; @@ -1927,39 +1901,13 @@ static struct file *userfaultfd_file_create(int flags) /* prevent the mm struct to be freed */ mmgrab(ctx->mm); - file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx, - O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS)); - if (IS_ERR(file)) { + fd = anon_inode_getfd("[userfaultfd]", &userfaultfd_fops, ctx, + O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS)); + if (fd < 0) { mmdrop(ctx->mm); kmem_cache_free(userfaultfd_ctx_cachep, ctx); } -out: - return file; -} - -SYSCALL_DEFINE1(userfaultfd, int, flags) -{ - int fd, error; - struct file *file; - - error = get_unused_fd_flags(flags & UFFD_SHARED_FCNTL_FLAGS); - if (error < 0) - return error; - fd = error; - - file = userfaultfd_file_create(flags); - if (IS_ERR(file)) { - error = PTR_ERR(file); - goto err_put_unused_fd; - } - fd_install(fd, file); - return fd; - -err_put_unused_fd: - put_unused_fd(fd); - - return error; } static int __init userfaultfd_init(void)