Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7719pxb; Wed, 24 Feb 2021 16:30:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJwUegm6P2LtU4a7MlGMq82ggeNVH+SJDm+aZutwY2ll1HTIcxVVCQbk9VuLSY4GHnW4bWOV X-Received: by 2002:a17:907:c27:: with SMTP id ga39mr258428ejc.68.1614213027240; Wed, 24 Feb 2021 16:30:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614213027; cv=none; d=google.com; s=arc-20160816; b=f8JRmLzTYICjvsiuUOMGF7PmirRqJPF2QQI6/QYFfRD6pxyHpQA37blPM0NOMKcILU HA8Oemc/SnD9J3HyEAu7/JHTee6e+/q0MtqNre9bNc5yiBCQEjXJkdMVD2iBkFyxgs6t apgTM8I4gByanlGO9F2yYEa5Naau1bjHRrU1HVJfTXD/xxzVgfdxjSRFToeJ7fz59m+b DFeL4ASoGAzdhnR7KVUXixP6GNr8RbswGONPKLpoMRSrL7Rl4ZbKaRLlqiU/u8EIpYor nd7Sc9HSMqhHqZuZDIG2B6Iw2wuCLMArloLn+8IPMy7izBwyCrNlWjJvsiYrsS70P08z FzLw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=iV/Srdw1uxpfnOHRnYqbznKXmmi6Z094eIlNac6kbLk=; b=MUqgC2OnxU+XbyXme29Al38ITs+HOvuJO1IGePvMriq/U64e1NdsnJ5cgQXpt9ZJys qMIx5FbTLGi11cUqW/jymqq1QdqBzlLprP379SORS+sWKGqR7mf2l+jgNxzYa5E5GtQl 04bOU0++NMJ5FiWf1WGG3TIGzJKt+Rji4mhzjGuVWjiPZdlAPu+tAjSydE/3TjZs1h1I J5U6FM3QJWa3yaHGTIg6uvERbW4E30flxTIr3IHIU7hs0VivK3Cwho28zcks/a3BlOXV 4B0WydyvtXk8FoOtdj+qp4KdNKFvxz0ETA99oJeB/hJ/bu2QZw6NoG0s+O2W47gLnq5b VzSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fhOKr6c9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k24si2050090edq.177.2021.02.24.16.29.57; Wed, 24 Feb 2021 16:30:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fhOKr6c9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234849AbhBXOF6 (ORCPT + 99 others); Wed, 24 Feb 2021 09:05:58 -0500 Received: from mail.kernel.org ([198.145.29.99]:58436 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234103AbhBXNIk (ORCPT ); Wed, 24 Feb 2021 08:08:40 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1A0BF64F9E; Wed, 24 Feb 2021 12:54:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614171298; bh=jsqqPb9DXofZgOnQ9AAwrQBlYq22ff7hsEVbWseEDWc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fhOKr6c9tiQP8L7nFqj1Ep0B9y45RY9fQAH1iG6OpXqp+l3/aSWpDdPnApDEbS0Ex U6z+1ijYt2u7LZ74ZLJMFYbnC6BvFIrr26+tQxMvh0alNGH6dz4cJO+r8E0/Nz9b+Z u7L9wjY8N0OR2dmLPZJbPQYRitpeQz0lmbM9LabHuZTy6qrQcm0OdV7EukcIB88FNb NaCTvqU/LsINx4tXcubKuDNPurK8YFyCGbZpGhD+CeJISpG/3/rnCElWOgncL8WR6v drb2DNqi7XoRF3sWf1Xxp7kn7ApLa40ZNyEkfaeUIsW8xHnXEtOavEREfr4iHzlHkT qO5+THfefuc5Q== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Chao Yu , Jaegeuk Kim , Sasha Levin , linux-f2fs-devel@lists.sourceforge.net Subject: [PATCH AUTOSEL 4.19 18/26] f2fs: fix to set/clear I_LINKABLE under i_lock Date: Wed, 24 Feb 2021 07:54:26 -0500 Message-Id: <20210224125435.483539-18-sashal@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210224125435.483539-1-sashal@kernel.org> References: <20210224125435.483539-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chao Yu [ Upstream commit 46085f37fc9e12d5c3539fb768b5ad7951e72acf ] fsstress + fault injection test case reports a warning message as below: WARNING: CPU: 13 PID: 6226 at fs/inode.c:361 inc_nlink+0x32/0x40 Call Trace: f2fs_init_inode_metadata+0x25c/0x4a0 [f2fs] f2fs_add_inline_entry+0x153/0x3b0 [f2fs] f2fs_add_dentry+0x75/0x80 [f2fs] f2fs_do_add_link+0x108/0x160 [f2fs] f2fs_rename2+0x6ab/0x14f0 [f2fs] vfs_rename+0x70c/0x940 do_renameat2+0x4d8/0x4f0 __x64_sys_renameat2+0x4b/0x60 do_syscall_64+0x33/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xa9 Following race case can cause this: Thread A Kworker - f2fs_rename - f2fs_create_whiteout - __f2fs_tmpfile - f2fs_i_links_write - f2fs_mark_inode_dirty_sync - mark_inode_dirty_sync - writeback_single_inode - __writeback_single_inode - spin_lock(&inode->i_lock) - inode->i_state |= I_LINKABLE - inode->i_state &= ~dirty - spin_unlock(&inode->i_lock) - f2fs_add_link - f2fs_do_add_link - f2fs_add_dentry - f2fs_add_inline_entry - f2fs_init_inode_metadata - f2fs_i_links_write - inc_nlink - WARN_ON(!(inode->i_state & I_LINKABLE)) Fix to add i_lock to avoid i_state update race condition. Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/namei.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c index 8617e742d0878..e20a0f9e68455 100644 --- a/fs/f2fs/namei.c +++ b/fs/f2fs/namei.c @@ -772,7 +772,11 @@ static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry, if (whiteout) { f2fs_i_links_write(inode, false); + + spin_lock(&inode->i_lock); inode->i_state |= I_LINKABLE; + spin_unlock(&inode->i_lock); + *whiteout = inode; } else { d_tmpfile(dentry, inode); @@ -966,7 +970,11 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, err = f2fs_add_link(old_dentry, whiteout); if (err) goto put_out_dir; + + spin_lock(&whiteout->i_lock); whiteout->i_state &= ~I_LINKABLE; + spin_unlock(&whiteout->i_lock); + iput(whiteout); } -- 2.27.0