Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp2963817pxb; Tue, 12 Jan 2021 03:02:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJxufctCcxOZzRRygY7jKCZQQTOgzYwVp+u/xP6jctkV+47d/ujLPotRcDtvxUFmnWIBXXQy X-Received: by 2002:a50:d491:: with SMTP id s17mr2835205edi.169.1610449371139; Tue, 12 Jan 2021 03:02:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610449371; cv=none; d=google.com; s=arc-20160816; b=y8qIhphNIkHRm1Wx0NCeEJuQgav54SBx7w+jcvx892umT8Nr/isXvkAVDIf/YjUD1t GM7jSxEMlL+dFw1o7xW1NwlWdJU9WNfIH0JHs42RiO7pQC/5KD7alLxho2hdl7Y969rd bEahKkagrVug+23qboTTXWNYQVv3i0M839eyuHauJte3H3kyHYwcpENNG6Z+pHkqlIn6 dHDZ3/Aweqdp7CH2ape+c9S+9dPogCYZQEozHsi2kwU0E6ivJt01x7//qUDOVBsAAbTN J7b/NKPdNeUQFCoHCxFButlWKGhRCzAeqnjdhx/VcrlOJjq0EfdoPaLxNY6CWypKWKTH 1pvg== 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 :message-id:date:subject:cc:to:from; bh=6UN5JoFHNVB71+8JpK9+t89lnpzgcLB6nCZrqmD/hV4=; b=Fc0nQLVj+ERwZrjl6ogl6g76cl4LaKlEg2XYqTxF3KeAWfy95BKX6BZrGJ2YTG+Hqp RN8GZEcUjojAa8xJ9VYMnFvRzcVpha32nYfPB5qf6r1QFn+en2NPqy/RG7KbeFV4p+jB vAOMBzKw+i8LKhVDp05Uq0nqKrD5M7UoFgl5ZYfAZ5xO9y/fnhFMKAQDubk3K57nfdKT 6OZSXimPqcOnnCUx/zgqeYg+OhfRnQls9DsLp0D8XmtazVWm+1t25gX5ZncRSuZwFoDs +frMWJdaeQXPlp3eOkzXOX1N7gtzem0trhJtyx7mN8a5G3mmoYDWsmA0BbDE+APnMWjr iGrA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r15si657942eja.145.2021.01.12.03.02.27; Tue, 12 Jan 2021 03:02:51 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729110AbhALB4P (ORCPT + 99 others); Mon, 11 Jan 2021 20:56:15 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:10705 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726734AbhALB4O (ORCPT ); Mon, 11 Jan 2021 20:56:14 -0500 Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DFDCB5MGPzl3vf; Tue, 12 Jan 2021 09:54:14 +0800 (CST) Received: from szvp000203569.huawei.com (10.120.216.130) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.498.0; Tue, 12 Jan 2021 09:55:23 +0800 From: Chao Yu To: CC: , , , Chao Yu Subject: [PATCH] f2fs: fix to set/clear I_LINKABLE under i_lock Date: Tue, 12 Jan 2021 09:55:09 +0800 Message-ID: <20210112015509.28083-1-yuchao0@huawei.com> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.120.216.130] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- fs/f2fs/namei.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c index 6edb1ab579a1..887804968576 100644 --- a/fs/f2fs/namei.c +++ b/fs/f2fs/namei.c @@ -855,7 +855,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); @@ -1041,7 +1045,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.29.2