Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1981263pxb; Fri, 5 Mar 2021 04:42:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJz8qUJSBzJ6dncx0IqTef2/v4G3KsrUUsTlzn6rdXfUd/L9bTTKd/2TsaVb2rYYzMpyDeUB X-Received: by 2002:a05:6402:11c6:: with SMTP id j6mr9196630edw.138.1614948161964; Fri, 05 Mar 2021 04:42:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614948161; cv=none; d=google.com; s=arc-20160816; b=GaKT+ls7P61yXWnYm3pg+uSeE7FBd8TLg9iLtzfmnoVJFlTYbV3Dk7cZjyDChEL14Z vkABPMkPtJKIJYxhCgb6SrKFG8/anBaDaSKEcNAQKKd3lsUrBDcaHC/BCd5Odezcmn1p Q1wJ2OrxMHHRBDhAPwS1DOt8YubSwmF8dlhcujk+6X/ClY+ETTZE6lQrlCcbw3G5Evo4 9iQF1soGH23b0RQApie6fu9zw5v8EC2qijqHUhmGYZNEGLeVUMXDTjJGkqiCmdpSEXS9 6Z85DhHSE2xoAkBPm+DNAHo4UDsj8HDDlnpLS8B5x+4nfEfO4kDhXlN+ch5emVJiEqMc vuag== 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=PEKnDSE/w11SOYtKKCM4jA7GJcwTvyUIrEHyzltfd3g=; b=gVBbBOpztHLRWCw9gY+sb9FwB9sOL2hj/JXZ+itkW6UHLGRpFIHtik6XrhC0uwDqkF f5yI4V3qzPYfclopUVvo6vwQ4Srm/HIsOuOd7IN9YWswNYOSzqhKYbBgvh1kRxnC9/r9 rRZUDjBKeFTRkpujTKV+FK9cTQb889cbO+0ZJTn++Zm07Ux12xkD8IN8MuqL4P8SKKRX Oss2aC6z8IFesJmgl4OGxeTBQmrbChthPlu1uv2J0vTPvC70XGP8y9WDKIb35rj04hpb k0O7GCqQHq1gdDsxD5JgBiZSxdRwipf489vM27URKBaYAo70kMt7mjVgoj+ol0epKOyN QsHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XOsNyJtz; 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 y5si1397013edw.446.2021.03.05.04.42.19; Fri, 05 Mar 2021 04:42:41 -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=XOsNyJtz; 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 S232236AbhCEMjW (ORCPT + 99 others); Fri, 5 Mar 2021 07:39:22 -0500 Received: from mail.kernel.org ([198.145.29.99]:52294 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232477AbhCEMim (ORCPT ); Fri, 5 Mar 2021 07:38:42 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id D381C64EE8; Fri, 5 Mar 2021 12:38:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614947922; bh=ZG6MeAOlQj9aP/zAjEgnAH8GoxyQc6z907PWPzeLi5o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XOsNyJtzrR6pMSZ1Vc5qmazYr/lCQvf8S+EHXPfMDUXg+vp4cxhpTC6oy7lO+Io0Q OHZRcl3uO1UQfQroQnqhFtsQ4zT0q+4nRgB03d7D/uonhUYLDBjfhz8OIGY44T4oxM d7MKsygpLkE8VSe1g0Bj5AOXqlt2TxEzajgvFPr0= 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 4.19 36/52] f2fs: fix to set/clear I_LINKABLE under i_lock Date: Fri, 5 Mar 2021 13:22:07 +0100 Message-Id: <20210305120855.433477815@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210305120853.659441428@linuxfoundation.org> References: <20210305120853.659441428@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 8617e742d087..e20a0f9e6845 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.30.1