From patchwork Wed Sep 29 03:04:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Kent X-Patchwork-Id: 12524447 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A77DC433EF for ; Wed, 29 Sep 2021 03:04:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0F36560F24 for ; Wed, 29 Sep 2021 03:04:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243780AbhI2DG2 (ORCPT ); Tue, 28 Sep 2021 23:06:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230022AbhI2DG1 (ORCPT ); Tue, 28 Sep 2021 23:06:27 -0400 Received: from smtp01.aussiebb.com.au (smtp01.aussiebb.com.au [IPv6:2403:5800:3:25::1001]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64A66C06161C; Tue, 28 Sep 2021 20:04:47 -0700 (PDT) Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp01.aussiebb.com.au (Postfix) with ESMTP id 3598F1002B4; Wed, 29 Sep 2021 13:04:43 +1000 (AEST) X-Virus-Scanned: Debian amavisd-new at smtp01.aussiebb.com.au Received: from smtp01.aussiebb.com.au ([127.0.0.1]) by localhost (smtp01.aussiebb.com.au [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id F1NrE2DSUH3X; Wed, 29 Sep 2021 13:04:43 +1000 (AEST) Received: by smtp01.aussiebb.com.au (Postfix, from userid 116) id 01C9F100298; Wed, 29 Sep 2021 13:04:42 +1000 (AEST) Received: from mickey.themaw.net (unknown [100.72.131.210]) by smtp01.aussiebb.com.au (Postfix) with ESMTP id C02771002AD; Wed, 29 Sep 2021 13:04:39 +1000 (AEST) Subject: [PATCH] kernfs: don't create a negative dentry if inactive node exists From: Ian Kent To: Greg Kroah-Hartman , Tejun Heo Cc: Hou Tao , David Howells , Miklos Szeredi , Rick Lindsley , Al Viro , Carlos Maiolino , linux-fsdevel , Kernel Mailing List Date: Wed, 29 Sep 2021 11:04:34 +0800 Message-ID: <163288467430.30015.16308604689059471602.stgit@mickey.themaw.net> User-Agent: StGit/0.23 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org In kernfs_iop_lookup() a negative dentry is created if there's no kernfs node associated with the dentry or the node is inactive. But inactive kernfs nodes are meant to be invisible to the VFS and creating a negative dentry for these can have unexpected side effects when the node transitions to an active state. The point of creating negative dentries is to avoid the expensive alloc/free cycle that occurs if there are frequent lookups for kernfs attributes that don't exist. So kernfs nodes that are not yet active should not result in a negative dentry being created so when they transition to an active state VFS lookups can create an associated dentry is a natural way. Signed-off-by: Ian Kent --- fs/kernfs/dir.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index ba581429bf7b..a957c944cf3a 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -1111,7 +1111,14 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir, kn = kernfs_find_ns(parent, dentry->d_name.name, ns); /* attach dentry and inode */ - if (kn && kernfs_active(kn)) { + if (kn) { + /* Inactive nodes are invisible to the VFS so don't + * create a negative. + */ + if (!kernfs_active(kn)) { + up_read(&kernfs_rwsem); + return NULL; + } inode = kernfs_get_inode(dir->i_sb, kn); if (!inode) inode = ERR_PTR(-ENOMEM);