Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1977303pxb; Fri, 5 Mar 2021 04:36:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwVIuRQGvGvEeA5OLPvd7Cf7Cxd26Emqk8MeXO4yDff0Epfq4gw4tU5MagSInsFYoOt1dl X-Received: by 2002:a50:e1c9:: with SMTP id m9mr9317945edl.307.1614947800261; Fri, 05 Mar 2021 04:36:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614947800; cv=none; d=google.com; s=arc-20160816; b=tz7kFJTvKwEML+yB61abyZ9wPuO6uKW+t+mGh5BA8OHcXLbKkZzJIOWx6uFbv3y7T4 ZaEOaQJ5MZ+2D0VEzCieCUlwsHEJrkaALIIhN4DbQ0cKE+1g6gyxoCpqsv4qJjdpMP+V srD3bXvkqaQWfGQoDHt3uleYioKNyjX9zbpHpcyWOIwTiSRCM4kjsaR+wijtGaIsnYCF S8LI1nB4XMe9STubVDerlHLek+pCnKnQpikDzjqw+PGixQtUoJEWiih/7viE3W97D1xo UuawwTjw3xs+Zuk+KvtBKeRKr9yAVd8WTwOS5CEsZ7yTD0HNMy9RrDcmXoB+7I8yE1cQ Ug3w== 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:subject:cc:to :from:dkim-signature; bh=Nqpry0rMvviU94ciDtIoepbhO2IxZaUnG1Y93JXN50o=; b=XaD6mCQoeghKhOvac/Odrt8+czuuAgX7wu9NX13snXsY14fJQDkZFAFYkHkl7NF5Hi 5K9yaHO/6Hrt5FFs4BHr/JtDq12ITRfDjREESK0Tx8h99ZleYpL7OyD6BQs4Qw19tmvJ OgkAcxNy9I5EFKPtWPTZyysCkBqQsoiZWGMJuWonDUMaLt2ab16Q6f6dNa3NpTCLjCax KTQqzymd62Ro0SpukYgip3O2GWuI0EcluGuOP/Xu/EX0jwGDuL4+HjmsaAOcYlGl5rG6 pUUduAD2GGXpH+V0sJBPn2S+5oOX6GmMyEyLky9lqjV8qJ/nvJKHuDi8VVA/kW3kuj+M E17w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kYMbPUt7; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r9si1309322ejx.201.2021.03.05.04.36.17; Fri, 05 Mar 2021 04:36:40 -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=@linuxfoundation.org header.s=korg header.b=kYMbPUt7; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232762AbhCEMfW (ORCPT + 99 others); Fri, 5 Mar 2021 07:35:22 -0500 Received: from mail.kernel.org ([198.145.29.99]:46546 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230342AbhCEMet (ORCPT ); Fri, 5 Mar 2021 07:34:49 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id C59C06502A; Fri, 5 Mar 2021 12:34:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614947689; bh=maV2DsHmUJ8snfxc9JZDFMZXOoVKzUkJuq6TtFF9avo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kYMbPUt7osVhiGPU5Pp1cNQMh5oGGIg9AGkgCsj75QDqmWUhJlJvp7QntlD3g/jmu lW5P2NaFvzOpOYBsyR0D+edKfl68RwESipdfrNUB0knikdKhpJ06RtVitaLbElPFVW EWVilm6qe0FC5obo+t4CjXBY1W4jg222k87/e2qg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 5.4 48/72] f2fs: fix to set/clear I_LINKABLE under i_lock Date: Fri, 5 Mar 2021 13:21:50 +0100 Message-Id: <20210305120859.683869319@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210305120857.341630346@linuxfoundation.org> References: <20210305120857.341630346@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 5d9584281935..3a97ac56821b 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.30.1