From patchwork Thu Dec 17 16:19:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11980175 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=-13.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham 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 3291BC2BB48 for ; Thu, 17 Dec 2020 16:19:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F3CE02342C for ; Thu, 17 Dec 2020 16:19:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728204AbgLQQT6 (ORCPT ); Thu, 17 Dec 2020 11:19:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728109AbgLQQT5 (ORCPT ); Thu, 17 Dec 2020 11:19:57 -0500 Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 779EFC0617A7 for ; Thu, 17 Dec 2020 08:19:17 -0800 (PST) Received: by mail-io1-xd2a.google.com with SMTP id y5so28012196iow.5 for ; Thu, 17 Dec 2020 08:19:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=l3Iugrzv5AcZw4A0i3n4W+/08URx3AE48WmSwpXG8sU=; b=kSzEhAdr4HA8g96BObh97VBvUBN59ILBercmAOObT+DzJ7a2+1MBtlwq7ITX1ny9oG R2Cck+i5qIvf0kC5tPGDU6im9/fxitb0o+UnC87CekR5CQ0IXeGn1PqEMszuuzCtay/F +tp1uLhbKw//SLYZzx1DZgaTlwhdw86vdeLf+ZEnQZt0ZsJgxpkzIooJBfKevhHroR7P 0Orb1ZNGSNl3E479ohTYxLOi5++KhtG9ItfSECDAEZkwlde1F26uG8frbqXY2ekpem2N 5YJcXpJpD8wvTsBlr5Zyz4b6zyfPGCv+AnMsORrd2tXb5HtAfPe82SEeOUkTsJC78Mln Axng== 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=l3Iugrzv5AcZw4A0i3n4W+/08URx3AE48WmSwpXG8sU=; b=s1LQjliwSGuYBAtMTDAvZDn8gtPBjDxS06z5bV420lCmEXdHMtwWs48HvdLwlW2Hy1 pxCHLVeOv6bbcRaM3eWTSmbEabCJLB42Ddhff+uVaW0yGOcY0JPuGG9cRmzSg5O5qsMz lZezxANhSS96Yes42GfE1f3ddbPDrPqqUORY5fqeFbUsf6648bX90OvwFmtI+w6V6+IS DRy7+7JFjHN8chET+ksU60B5KuW5DXQgd/VVDOzScqgy0rW02NCZ6dtP/UMGKOQV6OyQ QZ/urb0XBv4MpNI2VG/1wQtMyr1w38fdlOYklCyyaH6YbD4/JzkHCvfgBtFpbB5x5+sQ e/aA== X-Gm-Message-State: AOAM530kfslbbfSXduBKaCuGGy0LFdfJLA+ZAWQz4OGfTzrvMzSmEPLL 0bdyhf6LU+1gQ/T1pUEZs6NYLvo0yHfEBQ== X-Google-Smtp-Source: ABdhPJzGjI9Ly/jySolAEMH94vSWY1+Stu7TrqiJESJV3yFOQ+d3litaJnEJXdgZfQXRGQygxac06g== X-Received: by 2002:a6b:700f:: with SMTP id l15mr11814702ioc.22.1608221956528; Thu, 17 Dec 2020 08:19:16 -0800 (PST) Received: from p1.localdomain ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id k76sm3849957ilk.36.2020.12.17.08.19.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Dec 2020 08:19:16 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, Jens Axboe Subject: [PATCH 1/4] fs: make unlazy_walk() error handling consistent Date: Thu, 17 Dec 2020 09:19:08 -0700 Message-Id: <20201217161911.743222-2-axboe@kernel.dk> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201217161911.743222-1-axboe@kernel.dk> References: <20201217161911.743222-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Most callers check for non-zero return, and assume it's -ECHILD (which it always will be). One caller uses the actual error return. Clean this up and make it fully consistent, by having unlazy_walk() return a bool instead. Rename it to try_to_unlazy() and return true on success, and failure on error. That's easier to read. No functional changes in this patch. Cc: Al Viro Signed-off-by: Jens Axboe --- fs/namei.c | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 03d0e11e4f36..c31ddddcef3c 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -669,17 +669,17 @@ static bool legitimize_root(struct nameidata *nd) */ /** - * unlazy_walk - try to switch to ref-walk mode. + * try_to_unlazy - try to switch to ref-walk mode. * @nd: nameidata pathwalk data - * Returns: 0 on success, -ECHILD on failure + * Returns: true on success, false on failure * - * unlazy_walk attempts to legitimize the current nd->path and nd->root + * try_to_unlazy attempts to legitimize the current nd->path and nd->root * for ref-walk mode. * Must be called from rcu-walk context. - * Nothing should touch nameidata between unlazy_walk() failure and + * Nothing should touch nameidata between try_to_unlazy() failure and * terminate_walk(). */ -static int unlazy_walk(struct nameidata *nd) +static bool try_to_unlazy(struct nameidata *nd) { struct dentry *parent = nd->path.dentry; @@ -694,14 +694,14 @@ static int unlazy_walk(struct nameidata *nd) goto out; rcu_read_unlock(); BUG_ON(nd->inode != parent->d_inode); - return 0; + return true; out1: nd->path.mnt = NULL; nd->path.dentry = NULL; out: rcu_read_unlock(); - return -ECHILD; + return false; } /** @@ -792,7 +792,7 @@ static int complete_walk(struct nameidata *nd) */ if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED))) nd->root.mnt = NULL; - if (unlikely(unlazy_walk(nd))) + if (!try_to_unlazy(nd)) return -ECHILD; } @@ -1466,7 +1466,7 @@ static struct dentry *lookup_fast(struct nameidata *nd, unsigned seq; dentry = __d_lookup_rcu(parent, &nd->last, &seq); if (unlikely(!dentry)) { - if (unlazy_walk(nd)) + if (!try_to_unlazy(nd)) return ERR_PTR(-ECHILD); return NULL; } @@ -1567,10 +1567,8 @@ static inline int may_lookup(struct nameidata *nd) { if (nd->flags & LOOKUP_RCU) { int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); - if (err != -ECHILD) + if (err != -ECHILD || !try_to_unlazy(nd)) return err; - if (unlazy_walk(nd)) - return -ECHILD; } return inode_permission(nd->inode, MAY_EXEC); } @@ -1592,7 +1590,7 @@ static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq) // unlazy even if we fail to grab the link - cleanup needs it bool grabbed_link = legitimize_path(nd, link, seq); - if (unlazy_walk(nd) != 0 || !grabbed_link) + if (!try_to_unlazy(nd) != 0 || !grabbed_link) return -ECHILD; if (nd_alloc_stack(nd)) @@ -1634,7 +1632,7 @@ static const char *pick_link(struct nameidata *nd, struct path *link, touch_atime(&last->link); cond_resched(); } else if (atime_needs_update(&last->link, inode)) { - if (unlikely(unlazy_walk(nd))) + if (!try_to_unlazy(nd)) return ERR_PTR(-ECHILD); touch_atime(&last->link); } @@ -1651,11 +1649,8 @@ static const char *pick_link(struct nameidata *nd, struct path *link, get = inode->i_op->get_link; if (nd->flags & LOOKUP_RCU) { res = get(NULL, inode, &last->done); - if (res == ERR_PTR(-ECHILD)) { - if (unlikely(unlazy_walk(nd))) - return ERR_PTR(-ECHILD); + if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) res = get(link->dentry, inode, &last->done); - } } else { res = get(link->dentry, inode, &last->done); } @@ -2193,7 +2188,7 @@ static int link_path_walk(const char *name, struct nameidata *nd) } if (unlikely(!d_can_lookup(nd->path.dentry))) { if (nd->flags & LOOKUP_RCU) { - if (unlazy_walk(nd)) + if (!try_to_unlazy(nd)) return -ECHILD; } return -ENOTDIR; @@ -3127,7 +3122,6 @@ static const char *open_last_lookups(struct nameidata *nd, struct inode *inode; struct dentry *dentry; const char *res; - int error; nd->flags |= op->intent; @@ -3151,9 +3145,8 @@ static const char *open_last_lookups(struct nameidata *nd, } else { /* create side of things */ if (nd->flags & LOOKUP_RCU) { - error = unlazy_walk(nd); - if (unlikely(error)) - return ERR_PTR(error); + if (!try_to_unlazy(nd)) + return ERR_PTR(-ECHILD); } audit_inode(nd->name, dir, AUDIT_INODE_PARENT); /* trailing slashes? */ @@ -3162,9 +3155,7 @@ static const char *open_last_lookups(struct nameidata *nd, } if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { - error = mnt_want_write(nd->path.mnt); - if (!error) - got_write = true; + got_write = !mnt_want_write(nd->path.mnt); /* * do _not_ fail yet - we might not need that or fail with * a different error; let lookup_open() decide; we'll be From patchwork Thu Dec 17 16:19:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11980177 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=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham 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 29CA2C2BB9A for ; Thu, 17 Dec 2020 16:20:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F25612342C for ; Thu, 17 Dec 2020 16:19:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728582AbgLQQT7 (ORCPT ); Thu, 17 Dec 2020 11:19:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728109AbgLQQT6 (ORCPT ); Thu, 17 Dec 2020 11:19:58 -0500 Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41758C0617B0 for ; Thu, 17 Dec 2020 08:19:18 -0800 (PST) Received: by mail-io1-xd2d.google.com with SMTP id z136so28051189iof.3 for ; Thu, 17 Dec 2020 08:19:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dH4yhvV2A033Po00xq4v2MOxDj8pbUF6BP+iJCY6sVo=; b=B1i0qKow9CCnMstMkdzHcbLY0nUpqWzsjWghECXQft23Vqmnfk4Vb7k3/5uwdQSe5+ 90F+gkf6sy75aLvAo18uwAG4Y9jMRM8uJxQbpi50TEgeHuvf6ljIX/imTYf9RxRVplgn P0Bw3wabPywI9WNbRVkssZ2daVJznt8xzsAeqj5GYflT3sgyPHa6rvcjJqv5E8MOAMll ynMNxPGI61yvyxujXu2YyI/L3T0INWPi1u/3OxFXSG6gua4vvl+CJUpw299oESe1YpRm sBHwLogeEhCu8XHaYMYfk3p9psXZ7S34/RO/rlFz/oDJNxFpRBqBX/tGKFpCs7m0rWsm yNMw== 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=dH4yhvV2A033Po00xq4v2MOxDj8pbUF6BP+iJCY6sVo=; b=JQEbvINzTLZNlSdnirQab7ZxPl9ec6V7hrXabOd4lkOA4Zp/edjjllPM853QiCnV4x MjGs41cH0dEKaAMNMERN7llgs70kETYm7tp4J4omMsQExLysfN142ciCqk2/Hj1q54Os LrBGT5Pg1KXkpJPS1+imQKnt22ykZHRX01EAlsjxpY6DZdj5yDNdo15uU/oLSDRUwg6H WECljlnCLSuNfkss+vhRlem2hAzC2r0uvG8QK9gI7CRenPoda0bINDhW5chJOaP7cdct cP1kswMXdTz72E5Lte8Py59fe7T2fh147txc9UbuKmb5KbtR2LsgxuBhnEf9eRZR775P rjRg== X-Gm-Message-State: AOAM532iMYLv8E2Q/iuXR4oeYVVtijU29pZrv2ecizWx/lpZxeNEAbvn C1+aB7Se23tEmf4PZecRIIiys/w+EWGFkA== X-Google-Smtp-Source: ABdhPJyU6tVzgofAgt3iF6QljRrueGPzUIdXQuukp6TRegKsSv62+qTnCMTjGhv+gAFeArktXOyFvw== X-Received: by 2002:a02:b02:: with SMTP id 2mr48456145jad.15.1608221957464; Thu, 17 Dec 2020 08:19:17 -0800 (PST) Received: from p1.localdomain ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id k76sm3849957ilk.36.2020.12.17.08.19.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Dec 2020 08:19:16 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, Jens Axboe Subject: [PATCH 2/4] fs: add support for LOOKUP_CACHED Date: Thu, 17 Dec 2020 09:19:09 -0700 Message-Id: <20201217161911.743222-3-axboe@kernel.dk> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201217161911.743222-1-axboe@kernel.dk> References: <20201217161911.743222-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org io_uring always punts opens to async context, since there's no control over whether the lookup blocks or not. Add LOOKUP_CACHED to support just doing the fast RCU based lookups, which we know will not block. If we can do a cached path resolution of the filename, then we don't have to always punt lookups for a worker. During path resolution, we always do LOOKUP_RCU first. If that fails and we terminate LOOKUP_RCU, then fail a LOOKUP_CACHED attempt as well. Cc: Al Viro Signed-off-by: Jens Axboe --- fs/namei.c | 9 +++++++++ include/linux/namei.h | 1 + 2 files changed, 10 insertions(+) diff --git a/fs/namei.c b/fs/namei.c index c31ddddcef3c..e051eb0fa7f9 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -686,6 +686,8 @@ static bool try_to_unlazy(struct nameidata *nd) BUG_ON(!(nd->flags & LOOKUP_RCU)); nd->flags &= ~LOOKUP_RCU; + if (nd->flags & LOOKUP_CACHED) + goto out1; if (unlikely(!legitimize_links(nd))) goto out1; if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) @@ -722,6 +724,8 @@ static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned se BUG_ON(!(nd->flags & LOOKUP_RCU)); nd->flags &= ~LOOKUP_RCU; + if (nd->flags & LOOKUP_CACHED) + goto out2; if (unlikely(!legitimize_links(nd))) goto out2; if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) @@ -792,6 +796,7 @@ static int complete_walk(struct nameidata *nd) */ if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED))) nd->root.mnt = NULL; + nd->flags &= ~LOOKUP_CACHED; if (!try_to_unlazy(nd)) return -ECHILD; } @@ -2202,6 +2207,10 @@ static const char *path_init(struct nameidata *nd, unsigned flags) int error; const char *s = nd->name->name; + /* LOOKUP_CACHED requires RCU, ask caller to retry */ + if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED) + return ERR_PTR(-EAGAIN); + if (!*s) flags &= ~LOOKUP_RCU; if (flags & LOOKUP_RCU) diff --git a/include/linux/namei.h b/include/linux/namei.h index a4bb992623c4..b9605b2b46e7 100644 --- a/include/linux/namei.h +++ b/include/linux/namei.h @@ -46,6 +46,7 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT}; #define LOOKUP_NO_XDEV 0x040000 /* No mountpoint crossing. */ #define LOOKUP_BENEATH 0x080000 /* No escaping from starting point. */ #define LOOKUP_IN_ROOT 0x100000 /* Treat dirfd as fs root. */ +#define LOOKUP_CACHED 0x200000 /* Only do cached lookup */ /* LOOKUP_* flags which do scope-related checks based on the dirfd. */ #define LOOKUP_IS_SCOPED (LOOKUP_BENEATH | LOOKUP_IN_ROOT) From patchwork Thu Dec 17 16:19:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11980179 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=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham 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 5E0BFC4361B for ; Thu, 17 Dec 2020 16:20:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 365F82342C for ; Thu, 17 Dec 2020 16:20:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728654AbgLQQUA (ORCPT ); Thu, 17 Dec 2020 11:20:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728109AbgLQQT7 (ORCPT ); Thu, 17 Dec 2020 11:19:59 -0500 Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 254EDC06138C for ; Thu, 17 Dec 2020 08:19:19 -0800 (PST) Received: by mail-il1-x130.google.com with SMTP id 2so26334954ilg.9 for ; Thu, 17 Dec 2020 08:19:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ol+tmUketwscpuPuBQaI1Z7kOiVhZH3tyzDhGgPjybU=; b=0LPMk8OW45H/vwx4e1nPSySr7etylJ5I9SWavfnWHcP9tczXMk22hLerawj2OTp6xV mGP8333+4XTlXIiN0+M4P3h239Q0kbgwwQt9OSMqJndFevbLp/U+e62Z6RBHI71xRY+c R0MSeg0nLKcsRh0d7F/6BjlDdeG2PZGcHBNp7yvJ+R3wXbA9XiBSQdQfQMEx4dMgwf20 AcYw0mh/x1yt5xF/ZvfFrrGWoE7D/izhJkoQoPlXWonRkoXZ7EYhhRnxnOyvusFrV96G OblpBxSxOaYPcl0ZsqnbQ1c0d+tZL+/c9GdkirK023UxXWfiWHf77ksV6Wseyn8+RcPQ STmg== 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=Ol+tmUketwscpuPuBQaI1Z7kOiVhZH3tyzDhGgPjybU=; b=RzCRhi/d2A1Cj+gP4Ef/NMXFNLETcKz+tlbDo1DqHnQeoFZn3NNqfp3vSMIuMF9pWT CUcOTccMQ6DrY0Y4h1KXlDozPA+TIwjdUO5oHngyzuhIe1akfvE1qnBjUUT30f5uCuHg 3ObJI6pDg4A3s8ueSHPFUetLR7IV1JSDm89SM6pn+i7NoooSbjy8qBH2gxLeQPF5/2sa hPIS7NNcCUXlTvSph9Nond2+hl0PMhjzvkEKpRy4PsnEGehK/8glJg5v2E1+T4cobdtN gygflS2m0l9H3wheoKfEiOCYS4fg9objATMRy2u+ZUTsrGZIa0sY2GnjtPaD4VK3Y/1w 7vQQ== X-Gm-Message-State: AOAM5319nRBRPudvAQGzC/qM8ZFEoNm/ZV6mqeqCklcRn9FsJzmrRc8I vp0vapKcJrtSEMv33U6uGRGqS/vkIWt2Nw== X-Google-Smtp-Source: ABdhPJxMQ8/iGY5qb+nQHTU/LhfzTacUaYn+29v8xA4LCfgEY1JD27Ycd3vjPP0KdbGHJnoRWo267Q== X-Received: by 2002:a05:6e02:1525:: with SMTP id i5mr50826860ilu.14.1608221958275; Thu, 17 Dec 2020 08:19:18 -0800 (PST) Received: from p1.localdomain ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id k76sm3849957ilk.36.2020.12.17.08.19.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Dec 2020 08:19:17 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, Jens Axboe Subject: [PATCH 3/4] fs: expose LOOKUP_CACHED through openat2() RESOLVE_CACHED Date: Thu, 17 Dec 2020 09:19:10 -0700 Message-Id: <20201217161911.743222-4-axboe@kernel.dk> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201217161911.743222-1-axboe@kernel.dk> References: <20201217161911.743222-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Now that we support non-blocking path resolution internally, expose it via openat2() in the struct open_how ->resolve flags. This allows applications using openat2() to limit path resolution to the extent that it is already cached. If the lookup cannot be satisfied in a non-blocking manner, openat2(2) will return -1/-EAGAIN. Cc: Al Viro Signed-off-by: Jens Axboe --- fs/open.c | 6 ++++++ include/linux/fcntl.h | 2 +- include/uapi/linux/openat2.h | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/fs/open.c b/fs/open.c index 1e06e443a565..ca5444733acd 100644 --- a/fs/open.c +++ b/fs/open.c @@ -1091,6 +1091,12 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op) lookup_flags |= LOOKUP_BENEATH; if (how->resolve & RESOLVE_IN_ROOT) lookup_flags |= LOOKUP_IN_ROOT; + if (how->resolve & RESOLVE_CACHED) { + /* Don't bother even trying for create/truncate/tmpfile open */ + if (flags & (O_TRUNC | O_CREAT | O_TMPFILE)) + return -EAGAIN; + lookup_flags |= LOOKUP_CACHED; + } op->lookup_flags = lookup_flags; return 0; diff --git a/include/linux/fcntl.h b/include/linux/fcntl.h index 921e750843e6..766fcd973beb 100644 --- a/include/linux/fcntl.h +++ b/include/linux/fcntl.h @@ -19,7 +19,7 @@ /* List of all valid flags for the how->resolve argument: */ #define VALID_RESOLVE_FLAGS \ (RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS | \ - RESOLVE_BENEATH | RESOLVE_IN_ROOT) + RESOLVE_BENEATH | RESOLVE_IN_ROOT | RESOLVE_CACHED) /* List of all open_how "versions". */ #define OPEN_HOW_SIZE_VER0 24 /* sizeof first published struct */ diff --git a/include/uapi/linux/openat2.h b/include/uapi/linux/openat2.h index 58b1eb711360..a5feb7604948 100644 --- a/include/uapi/linux/openat2.h +++ b/include/uapi/linux/openat2.h @@ -35,5 +35,9 @@ struct open_how { #define RESOLVE_IN_ROOT 0x10 /* Make all jumps to "/" and ".." be scoped inside the dirfd (similar to chroot(2)). */ +#define RESOLVE_CACHED 0x20 /* Only complete if resolution can be + completed through cached lookup. May + return -EAGAIN if that's not + possible. */ #endif /* _UAPI_LINUX_OPENAT2_H */ From patchwork Thu Dec 17 16:19:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11980181 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=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham 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 1744BC2BB48 for ; Thu, 17 Dec 2020 16:20:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DFBCA2342C for ; Thu, 17 Dec 2020 16:20:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728684AbgLQQUB (ORCPT ); Thu, 17 Dec 2020 11:20:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728649AbgLQQUA (ORCPT ); Thu, 17 Dec 2020 11:20:00 -0500 Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 049D0C061282 for ; Thu, 17 Dec 2020 08:19:20 -0800 (PST) Received: by mail-il1-x131.google.com with SMTP id g1so26354504ilk.7 for ; Thu, 17 Dec 2020 08:19:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JTR3cI7lOqIwEx0cNt3QyNMnkPxMg2CNmpQoqv+u4Qw=; b=oxM2hMlKdpi+ZLxbEEvp3e+7RlQqMy/XIwtoI7pcp1Tl7Jj+Z8YvWOuNlf8f0wFpeC 7SDMj/CaKlOoExGcjL/NIvivDOWTVHXLKm3AWiGm14oz5y9pdXTnA1zL8Drc2l4E9nr7 18y+k9UAmGsif1sVIH5BWKn448eukUgeI0U1s4G45V+kxrW86rnk9TYEf1dgI8actg/L D6WfpCv/G7mPASADFYELJSf5/uHzx+MCJPZx9Jv1FgnsO3GJk186s2Zw8+Rbr1/xg2iC P1nnki9dKYmP/Kq4u/Rvy9greWvM51balrFwcsgQsLCVsU4oxvKJbuQVowJ2+gY8ONjv K+cw== 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=JTR3cI7lOqIwEx0cNt3QyNMnkPxMg2CNmpQoqv+u4Qw=; b=sR/8KzMfwc8eYGAeF30SUfd9P+aJUHtBuJQescfazLr9JsWLoyH9ynIUudUXk4ppLF r1juNvDHBM/1AuloQp4idyoZyRgRt8H9fR6EiMNVb0C3AblYxznGNru1bZJPv2YA8AU2 3Nywtqyjl9Oa8oMJS61i4ag4RPb0gGVVWGdoG2Mc7Bx9m3FJ0k7Ze4oXmtDFsdCE5tIN hNYKnVnQ/EbU2qgY3DFgnGuFWeuCac49Bd2/BYoQy/zxjVzPg5dx8Ys5tvK0QUbhpKHd eOPXGhImnMklk140WeOhkxkfxZzLc97Hur0G9mAUW+WqpLCtsiQMkz1voPF0CPRoTyF3 w8Kw== X-Gm-Message-State: AOAM530X0Tj1OenP++T+DGiPb7lTKSrBVb+ZCQkc9PPGrgu2bd11lCCY mgdFTmBH/kXxEaHOvzDcJNtb/IVam95Y2A== X-Google-Smtp-Source: ABdhPJwS+K1Fnt4Tq+UXt2nCghVMgr2UhsWmS6gaROsDvVAIl4/CBr5lbZY1XM4MUqzEzIzmLdX+wA== X-Received: by 2002:a92:d0ca:: with SMTP id y10mr51278782ila.68.1608221959109; Thu, 17 Dec 2020 08:19:19 -0800 (PST) Received: from p1.localdomain ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id k76sm3849957ilk.36.2020.12.17.08.19.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Dec 2020 08:19:18 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, Jens Axboe Subject: [PATCH 4/4] io_uring: enable LOOKUP_CACHED path resolution for filename lookups Date: Thu, 17 Dec 2020 09:19:11 -0700 Message-Id: <20201217161911.743222-5-axboe@kernel.dk> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201217161911.743222-1-axboe@kernel.dk> References: <20201217161911.743222-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Instead of being pessimistic and assume that path lookup will block, use LOOKUP_CACHED to attempt just a cached lookup. This ensures that the fast path is always done inline, and we only punt to async context if IO is needed to satisfy the lookup. For forced nonblock open attempts, mark the file O_NONBLOCK over the actual ->open() call as well. We can safely clear this again before doing fd_install(), so it'll never be user visible that we fiddled with it. Signed-off-by: Jens Axboe --- fs/io_uring.c | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 6f9392c35eef..5a703c8a4521 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -487,7 +487,6 @@ struct io_sr_msg { struct io_open { struct file *file; int dfd; - bool ignore_nonblock; struct filename *filename; struct open_how how; unsigned long nofile; @@ -3996,7 +3995,6 @@ static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe return ret; } req->open.nofile = rlimit(RLIMIT_NOFILE); - req->open.ignore_nonblock = false; req->flags |= REQ_F_NEED_CLEANUP; return 0; } @@ -4038,39 +4036,48 @@ static int io_openat2(struct io_kiocb *req, bool force_nonblock) { struct open_flags op; struct file *file; + bool nonblock_set; + bool resolve_nonblock; int ret; - if (force_nonblock && !req->open.ignore_nonblock) - return -EAGAIN; - ret = build_open_flags(&req->open.how, &op); if (ret) goto err; + nonblock_set = op.open_flag & O_NONBLOCK; + resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; + if (force_nonblock) { + /* + * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, + * it'll always -EAGAIN + */ + if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) + return -EAGAIN; + op.lookup_flags |= LOOKUP_CACHED; + op.open_flag |= O_NONBLOCK; + } 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); + /* only retry if RESOLVE_CACHED wasn't already set by application */ + if ((!resolve_nonblock && force_nonblock) && file == ERR_PTR(-EAGAIN)) { + /* + * We could hang on to this 'fd', but seems like 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); + return -EAGAIN; + } + if (IS_ERR(file)) { put_unused_fd(ret); ret = PTR_ERR(file); - /* - * A work-around to ensure that /proc/self works that way - * that it should - if we get -EOPNOTSUPP back, then assume - * that proc_self_get_link() failed us because we're in async - * context. We should be safe to retry this from the task - * itself with force_nonblock == false set, as it should not - * block on lookup. Would be nice to know this upfront and - * avoid the async dance, but doesn't seem feasible. - */ - if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) { - req->open.ignore_nonblock = true; - refcount_inc(&req->refs); - io_req_task_queue(req); - return 0; - } } else { + if (force_nonblock && !nonblock_set) + file->f_flags &= ~O_NONBLOCK; fsnotify_open(file); fd_install(ret, file); }