From patchwork Tue Oct 18 02:32:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Kent X-Patchwork-Id: 3905 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1746459wrs; Mon, 17 Oct 2022 19:50:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Jq1ohFx+aLZvJY6x9NnVp+/OEDfweyt/p6sB256RNMv0hPeptXcN+CeT1gXFzguFfOD/J X-Received: by 2002:a05:6402:70c:b0:459:7b65:fead with SMTP id w12-20020a056402070c00b004597b65feadmr609110edx.209.1666061449988; Mon, 17 Oct 2022 19:50:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666061449; cv=none; d=google.com; s=arc-20160816; b=kDlZCWjU4feOKMsVvbDW5TbyNcFDWP/Y+I+CE9HoN/8TkSq0Asj+jMyydjm6Y5mMi2 qEpqxT4RPV445U8ImRx6wJjdsre2FOZxOEmZ/zUQRfpRwWi0UfQx+QLkGONTFyFpgjEn WxJa70E+/QHJuLdwSrB56jrwji6ADY7Prfvs3qNeDuXg0caQyXJhN7TxRJLCX/pJoRQQ SgdoQcYxs2IZ+9ESXbe6UWtWVuPaisfRZRZn6DLw0FVDZ4sECub/BNTCvXM5btkDBBNM boqt4Q56kWuhBZW/YJgUfMiP81/P4NkcTYNz+t56ayZzSE8Pk9m2nCEhtQH1YqpoSU4b Qskg== 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:references:in-reply-to:message-id:date:cc:to:from :subject; bh=WeBLvlw43Q04SBQ4j9FqRARqidIAevq9fsF4RadZmh0=; b=sEg6AJV11CqokD9YL+T0ATZDK+nWUq6ncqfQ7eE1tR1/VH7rWgp7tqbgNC+V20/Jiw 8G3D3jJjAkJQ1bcsuc+jdcGxCO+EDCaTSmXcx/IQ30/vX+GS6C5oDKSK1o8xNTU+HaTx 4pdN4w4ZLCQSgeiXmN6zWsyU0op6VxnMuyaNJviW8xF3koTUM48CXpxC9Ba+ywzyVibp o25pY2c5tdW8mL6qx6b/z3PvYUGQEjd+plRsQMTFjJI61VP4NNfzJdfLl0nIv8Eqhqqu fItMZqjX3x2NALkJwASExFFmA7PgrTKWi41cLt77MYJ6yr/48eosTyf21TbTUTuQot+w 69YQ== 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 z72-20020a509e4e000000b004596db363c4si10975935ede.264.2022.10.17.19.50.25; Mon, 17 Oct 2022 19:50:49 -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 S230077AbiJRCcv (ORCPT + 99 others); Mon, 17 Oct 2022 22:32:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229822AbiJRCcs (ORCPT ); Mon, 17 Oct 2022 22:32:48 -0400 Received: from smtp01.aussiebb.com.au (smtp01.aussiebb.com.au [121.200.0.92]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7630C92F48; Mon, 17 Oct 2022 19:32:47 -0700 (PDT) Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp01.aussiebb.com.au (Postfix) with ESMTP id B9A01100626; Tue, 18 Oct 2022 13:32:44 +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 6T_nZIWt-tY1; Tue, 18 Oct 2022 13:32:44 +1100 (AEDT) Received: by smtp01.aussiebb.com.au (Postfix, from userid 116) id A5E6810061D; Tue, 18 Oct 2022 13:32:44 +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 E5B0B1005F4; Tue, 18 Oct 2022 13:32:42 +1100 (AEDT) Subject: [PATCH 1/2] kernfs: dont take i_lock on inode attr read 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, 18 Oct 2022 10:32:42 +0800 Message-ID: <166606036215.13363.1288735296954908554.stgit@donald.themaw.net> In-Reply-To: <166606025456.13363.3829702374064563472.stgit@donald.themaw.net> References: <166606025456.13363.3829702374064563472.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?1746992051125035778?= X-GMAIL-MSGID: =?utf-8?q?1746992051125035778?= The kernfs write lock is held when the kernfs node inode attributes are updated. Therefore, when either kernfs_iop_getattr() or kernfs_iop_permission() are called the kernfs node inode attributes won't change. Consequently concurrent kernfs_refresh_inode() calls always copy the same values from the kernfs node. So there's no need to take the inode i_lock to get consistent values for generic_fillattr() and generic_permission(), the kernfs read lock is sufficient. Signed-off-by: Ian Kent Reviewed-by: Miklos Szeredi Acked-by: Tejun Heo Signed-off-by: Ian Kent Acked-by: Miklos Szeredi Reviewed-by: Imran Khan --- fs/kernfs/inode.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c index 3d783d80f5da..74f3453f4639 100644 --- a/fs/kernfs/inode.c +++ b/fs/kernfs/inode.c @@ -190,10 +190,8 @@ int kernfs_iop_getattr(struct user_namespace *mnt_userns, struct kernfs_root *root = kernfs_root(kn); down_read(&root->kernfs_rwsem); - spin_lock(&inode->i_lock); kernfs_refresh_inode(kn, inode); generic_fillattr(&init_user_ns, inode, stat); - spin_unlock(&inode->i_lock); up_read(&root->kernfs_rwsem); return 0; @@ -288,10 +286,8 @@ int kernfs_iop_permission(struct user_namespace *mnt_userns, root = kernfs_root(kn); down_read(&root->kernfs_rwsem); - spin_lock(&inode->i_lock); kernfs_refresh_inode(kn, inode); ret = generic_permission(&init_user_ns, inode, mask); - spin_unlock(&inode->i_lock); up_read(&root->kernfs_rwsem); return ret; From patchwork Tue Oct 18 02:32:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Kent X-Patchwork-Id: 3903 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1742943wrs; Mon, 17 Oct 2022 19:34:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4tM/JaFW8+jWANiG61BDuGwnOhbq2xKz7b8z0d2W9R2nYobagOlfUDsxarNPinhBYyrZTc X-Received: by 2002:a17:907:7f25:b0:78d:e76a:ef18 with SMTP id qf37-20020a1709077f2500b0078de76aef18mr568759ejc.378.1666060466365; Mon, 17 Oct 2022 19:34:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666060466; cv=none; d=google.com; s=arc-20160816; b=qcPeUj4Zcb3Ovx7Dj0GYwyDFic55nUt+2gdgzw9iO7s7SdcJyQxDirFVgbjenzxjff E4vZKfSbxU0oqkmXpyH/L28dPAKJtBscdTo9SZiTYUS3/ZpawjjkVPOpW45N+hwToqnK IOP8J4ZjqAcdWSgMHOjPOv8OpVLoKyAU1+CZhaGGlHU9nBGLIlB9QKBqh9H4BMomo+nj +J0ApGUzB6LV8l5YN/dkut/m5H9IRy4pnk0N845nuBYrmXCbdA1X7FDrFws09MG3HXMk ZlbCub7TRHpHhMFrziRRp4WjZWVDOfHJGRCQ3aI+GQrKrW6RFCYtJxCRSJgv5HCF+Nyy w8YA== 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:references:in-reply-to:message-id:date:cc:to:from :subject; bh=nF+xiGmKIosR2omH76YgOnVyRsi2LsPha4mUJRsiwG4=; b=IcRAi0iSmmiceDYbRzW4Jfh4ZDxjg12171PX5DvE2+Q8eG5FW6w5NKfg7caoFMsR25 nJ3fNz/SDYd8XOLbSmD5FO4VI1tGtt+5RUtGWUrJOtgNIgjBAxRqAspd9gt2ZAdU0RKB u4yybQB2owDrpk6yPfiSDM9GqjRSuPpWAVk9dycZM/VTzVodx8X3N0oml4zT1NC+NZBY b2kPR8kVFt9mpCbbtXUWmTUcbVsvEijrSq1txvYDiSwM7let7RkCnTRXwz0UmmN6Gn4/ oShZ0qVsgXzC+yJ9Ncc/xfBFhjNpWkomTVvgmTy/y674eU2WmAkWbernvdwsdDvs2Ul3 rCGg== 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 qb36-20020a1709077ea400b0078d00203ab2si11575646ejc.41.2022.10.17.19.34.02; Mon, 17 Oct 2022 19:34:26 -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 S230499AbiJRCdG (ORCPT + 99 others); Mon, 17 Oct 2022 22:33:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229822AbiJRCc5 (ORCPT ); Mon, 17 Oct 2022 22:32:57 -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 20EE695AD2; Mon, 17 Oct 2022 19:32:54 -0700 (PDT) Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp01.aussiebb.com.au (Postfix) with ESMTP id 582C8100627; Tue, 18 Oct 2022 13:32:52 +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 udJbdEVks39P; Tue, 18 Oct 2022 13:32:52 +1100 (AEDT) Received: by smtp01.aussiebb.com.au (Postfix, from userid 116) id 50934100625; Tue, 18 Oct 2022 13:32:52 +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 50C85100319; Tue, 18 Oct 2022 13:32:50 +1100 (AEDT) Subject: [PATCH 2/2] kernfs: dont take i_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, 18 Oct 2022 10:32:49 +0800 Message-ID: <166606036967.13363.9336408133975631967.stgit@donald.themaw.net> In-Reply-To: <166606025456.13363.3829702374064563472.stgit@donald.themaw.net> References: <166606025456.13363.3829702374064563472.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?1746991019467736271?= X-GMAIL-MSGID: =?utf-8?q?1746991019467736271?= 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 inode i_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 inode i_lock isn't needed. Signed-off-by: Ian Kent Reviewed-by: Miklos Szeredi Acked-by: Tejun Heo --- 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.