From patchwork Tue Nov 1 08:27:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Kent X-Patchwork-Id: 13558 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2818328wru; Tue, 1 Nov 2022 01:29:04 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7eew88we8NC1j8X4v4ZqNH35/a84FlwAfkGC+nr1RD4M1xeN3jFI2j5s0BiJCyj9jAZgS9 X-Received: by 2002:a63:2dc3:0:b0:46b:3cf1:e16c with SMTP id t186-20020a632dc3000000b0046b3cf1e16cmr16181914pgt.556.1667291343946; Tue, 01 Nov 2022 01:29:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667291343; cv=none; d=google.com; s=arc-20160816; b=dohwjNqqGujnQTwGrv2xL6/OSsM1pXjyHjG4TGFlpWUzqt123/HA+yVueAVa9TE5R1 UOxqlNP6IRWLUZ72wf4dVBgCdH4SiBBFIGVVUrXRbDs6LGPuaGQ1m+CT7yK4CpPV7MrP SqMtpO+fzBZULf8JE5mucNlE2vkMiwbi8oR/eA2wfwGocz4ehEh7iiohhlanVom35kRE Aln+uTTZrxhr5vKwfPAT10zrmvKig4s4VN1154Eur/QZqNaJTgExXnj5eerAWt+of1HO NqC2y098hnEbIwxKN3FbVGufMXRckXgXKhQ6ls7twVwiVv8KZplyVDLBIKV6usmCpPpW lKEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:message-id:date:cc:to:from:subject; bh=fVUAoqjvG/w8VZiUN+3qjOOVHNTva5JTTF33vcpib3U=; b=kFtT/9+e1veeu3j2mWqcr50jhS2dbxQvpUxrvMo/sbQWzRtAE3zlPz8rNZNAozgRRn 0yFZoYKFxFR+krE2LvyXy8qKlTGulVdACzKGBvcR+wJ6TPOIU/PbQD1DJNMawUsT1kkJ D0O45TI4+FJGfIhR6v9OO2VM1Tq7gNr9/vl2Qtvl+V/FV+k2FowWXUZG4z2ZjtvWWyB3 3wTr5aLA3sWauYyDHDu9VnIuZ/gGkmLtido/LVJ6nQNCV8eBOEQGwKy+13Uj1EOb4xTW +muPV+J1r2U/K1TdR+BeLTMsl9+lZsbcmWUgxY8MJ8+kBeFXGXlOS0tDjlkah78yz7E1 SdUQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mu2-20020a17090b388200b001fd70129092si14104587pjb.15.2022.11.01.01.28.51; Tue, 01 Nov 2022 01:29:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230022AbiKAI2G (ORCPT + 99 others); Tue, 1 Nov 2022 04:28:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230003AbiKAI1w (ORCPT ); Tue, 1 Nov 2022 04:27:52 -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 7C6BD183AA; Tue, 1 Nov 2022 01:27:51 -0700 (PDT) Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp01.aussiebb.com.au (Postfix) with ESMTP id 1F5B910043E; Tue, 1 Nov 2022 19:27:48 +1100 (AEDT) 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 dpaJ06SxZHco; Tue, 1 Nov 2022 19:27:48 +1100 (AEDT) Received: by smtp01.aussiebb.com.au (Postfix, from userid 116) id 120E6100443; Tue, 1 Nov 2022 19:27:48 +1100 (AEDT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 Received: from donald.themaw.net (180-150-90-198.b4965a.per.nbn.aussiebb.net [180.150.90.198]) by smtp01.aussiebb.com.au (Postfix) with ESMTP id 945C210006B; Tue, 1 Nov 2022 19:27:45 +1100 (AEDT) Subject: [PATCH v2] kernfs: dont take d_lock on revalidate From: Ian Kent To: Greg Kroah-Hartman , Tejun Heo Cc: Minchan Kim , Eric Sandeen , Al Viro , Rick Lindsley , David Howells , Miklos Szeredi , Carlos Maiolino , linux-fsdevel , Kernel Mailing List Date: Tue, 01 Nov 2022 16:27:45 +0800 Message-ID: <166729126531.12154.4031617722829447442.stgit@donald.themaw.net> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748281688431437937?= X-GMAIL-MSGID: =?utf-8?q?1748281688431437937?= In kernfs_dop_revalidate() when the passed in dentry is negative the dentry directory is checked to see if it has changed and if so the negative dentry is discarded so it can refreshed. During this check the dentry d_lock is taken to mitigate against a possible concurrent rename. But if it's racing with a rename, becuase the dentry is negative, it can't be the source it must be the target and it must be going to do a d_move() otherwise the rename will return an error. In this case the parent dentry of the target will not change, it will be the same over the d_move(), only the source dentry parent may change so the dentry d_lock isn't needed. v2: Fix i_lock -> d_lock in patch subject and description. There's no change to the patch itself. Signed-off-by: Ian Kent --- fs/kernfs/dir.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 3990f3e270cb..6acd9c3d4cff 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -1073,20 +1073,30 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) /* If the kernfs parent node has changed discard and * proceed to ->lookup. + * + * There's nothing special needed here when getting the + * dentry parent, even if a concurrent rename is in + * progress. That's because the dentry is negative so + * it can only be the target of the rename and it will + * be doing a d_move() not a replace. Consequently the + * dentry d_parent won't change over the d_move(). + * + * Also kernfs negative dentries transitioning from + * negative to positive during revalidate won't happen + * because they are invalidated on containing directory + * changes and the lookup re-done so that a new positive + * dentry can be properly created. */ - spin_lock(&dentry->d_lock); + root = kernfs_root_from_sb(dentry->d_sb); + down_read(&root->kernfs_rwsem); parent = kernfs_dentry_node(dentry->d_parent); if (parent) { - spin_unlock(&dentry->d_lock); - root = kernfs_root(parent); - down_read(&root->kernfs_rwsem); if (kernfs_dir_changed(parent, dentry)) { up_read(&root->kernfs_rwsem); return 0; } - up_read(&root->kernfs_rwsem); - } else - spin_unlock(&dentry->d_lock); + } + up_read(&root->kernfs_rwsem); /* The kernfs parent node hasn't changed, leave the * dentry negative and return success.