Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp427011pxb; Wed, 24 Feb 2021 06:05:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJxLs/j4h/ynizZQGV0I2A6ygWVgVMsFmOQoNv+1u/ozknjGzBV+vmj2Lhj4lArFlIJEmyt+ X-Received: by 2002:a05:6402:1155:: with SMTP id g21mr33406593edw.279.1614175510858; Wed, 24 Feb 2021 06:05:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614175510; cv=none; d=google.com; s=arc-20160816; b=a6aGndjJlTP1ifzd4wbRJe1+UObIF9srkYA4Y8lpWj85xxqNcyK+sKu7Z05y5S7pzq Y/Se0dcuZMOfIAx9jQ1aJuwb5SrwTDy6s5p+5H171Q+4BV9saG7RmFRgjT4D/WretG8t 9/rW6L/7Z3fDPAEKF1ZdPEixDAAZZcaOKjs828v+3woFmWonZXZgBqtVh2y/wpbhWYh/ R5Ei9Vvvtr6KsR0Ks15gdiiUBjXaoyWbeo7m3p7X4/zx52Wir9bqeTXdRTYILyxES4ba 5HS9sI/PCraPV+j/pMfCPdABxpi3MFiiFwzTrbq9Q4Tjj4A2G7toxUti5yosjPgJ8wqK LXOw== 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=8Ctkedp71QhF/LRxMFqIGRE5eeevEeSDGu5KwwqlsYM=; b=VN6fwoHMM2+UF6hVTBgYxr4J5hvFIFt9c/Gpyr6/OUtP2FnS0ARgUQR3hND7Mq9wl2 0ux/t2nBKXx+r8bO8+PDHwg58QU2X0VaUe0v8zWkgQcqb0f8RkNu4m/LOBkx2GCP6nXw k4Ms8dUJvmdbPrnK83tRS2zJX9WSnn3GpC+J7uHtje3p4rXFLtwRJ8nLp4xg6qTV5uz4 IP22GD29M+51cFn+QpUoT3yFLdRPc6dZLGMx+T4BmspX+ES28pMZ8C/toNoYQxH5/vNA 29EXjY+VM+oHzEchRuq2SYnAemX0xibMttN/cmSSdmbHVAgeEuzfIPu+ZGFd8vmThXNX PozQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LnToJtsl; 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 x27si222704ejb.603.2021.02.24.06.04.38; Wed, 24 Feb 2021 06:05:10 -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=LnToJtsl; 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 S235690AbhBXNzL (ORCPT + 99 others); Wed, 24 Feb 2021 08:55:11 -0500 Received: from mail.kernel.org ([198.145.29.99]:58124 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235757AbhBXNGO (ORCPT ); Wed, 24 Feb 2021 08:06:14 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 78A6564F7C; Wed, 24 Feb 2021 12:54:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614171253; bh=X1W6JD8j8dQnRoGOSb0dACFGg5pmNBlgnpMZVbf1QGc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LnToJtsli8PppOPaaGGfkWA0GSl8gtNyUUcv8tJ3TI86pgx528mjA/J95+3Fm5ldb kjXLmiutiScNWjXKhmlwIHfiVQmMrqgmPvHT6TEIOBnVwUcSORjWVR/Z95ieSu3ycu c1Pu1zgydB3bC6TaJKrkTDBxsEFrTXQYdqLmTU3p8TY3+/0FklXZf7hSytHMS/3qO/ iJSq8C275vGSRbOagG+nENG8e7KTT6uvW26npTxGcq8gMhPfUXL0IzmxuAJZFdtTKZ itYM7GZVC71YaCbwCZxWdVglqWk0b9q7P3ZkiLKqfDJcVcLvW5OTT5IOZVMXFO4I9h CkvjoAd6yuJvg== 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 5.4 24/40] f2fs: fix to set/clear I_LINKABLE under i_lock Date: Wed, 24 Feb 2021 07:53:24 -0500 Message-Id: <20210224125340.483162-24-sashal@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210224125340.483162-1-sashal@kernel.org> References: <20210224125340.483162-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 5d9584281935f..3a97ac56821ba 100644 --- a/fs/f2fs/namei.c +++ b/fs/f2fs/namei.c @@ -797,7 +797,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); @@ -996,7 +1000,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