Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3889098pxv; Mon, 19 Jul 2021 11:11:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUcPDyyIahM/J1g8usB2oJgK02cMwWJbF0W6lpN9G5uJH47z+bw/+j0VoNv5CIrP/yKoyf X-Received: by 2002:a5d:9e4f:: with SMTP id i15mr17073580ioi.58.1626718306192; Mon, 19 Jul 2021 11:11:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626718306; cv=none; d=google.com; s=arc-20160816; b=kFP28G+r1TFdZxy/WBqZGzN3OFkLwb0bWYKs2qdTsS/SeKCf/nX23GgOQ/Th40rY/d GxxJ7zCaFJCyKxi7MnEhysu6aLcm9meMoGAI4i5ytjeV42ExNvftzRDLQN8qogf45s6g WD5oM7R5fbw/Ck19MeGMhK+aNKH12CXlJiBxmM8srqhULSFf/n6wW1dszvwCp/RGsqUJ TCP0jOdcqjdpFFNad7B1YFGRGWnCPDVRbWqB0K179egdtK6BS2hL9bDncPbgU/RalThZ dMszbNBDGuVKF6p1/V3uLwMfDvR/IjYGdGJVrlbtX90F1UG9Gx/tVlVpA/ZK1NWkvDyG GeBw== 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=L/9iKuSnKByD54eDsg9ECtDM8RKRBDjLYEHZhErKHyw=; b=o5v2lt6LNynEtpeZgnYSWjQXYz/3vHtVJgurUCSHb8j9zCxvr18Y1p8aTuTgBFG//y ZAyBpk8Dp0r8AYuhrKEH/TpwWqxD36bJ8yK30dQ+ZbVHlLAF8La0Lp/38kTuLaxy7qzq ZC3lpQ9k44R3Jblpl7MOPOyRnI/BVYRA06G362ZmFFhze5BMHwlSMh3kNoYIjED/MaDb GuFDJbCLf4/cKgDDG85Xlo72BS9/KjmBghVZa0TJrG0xMgRocfyDr5t45ovEuz1iZuwX 8ng2c2ZUds4VOOeaA70ryxM2EcCq1qBCy9ZQdsUOmqQYogi5HvbWqG/U3YNQS8v25cA6 HBKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CeZdBbOw; 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 v10si3129112ilh.117.2021.07.19.11.11.34; Mon, 19 Jul 2021 11:11:46 -0700 (PDT) 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=CeZdBbOw; 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 S1378806AbhGSR0U (ORCPT + 99 others); Mon, 19 Jul 2021 13:26:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:46660 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349865AbhGSPp0 (ORCPT ); Mon, 19 Jul 2021 11:45:26 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5116760C40; Mon, 19 Jul 2021 16:26:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626711964; bh=kKMCEHCS9EGuoSCxnMrK/LQAQe697nLK2NLSkDtwF9Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CeZdBbOwB75ObpbShW29PgmOvVTVZ2KJBYONfFBIoRUb8wZRI5dNyklc0rJdzejVf wwODP5RBnLrjg5zPGylGkt46zwzk5ldYht2pjGW/bIlEQXfhY78daaN5me0oWz35DT scKokrQ7VQV+sxdH5l+uF+X1X6NijwCrCjRcht1M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhihao Cheng , Richard Weinberger , Sasha Levin Subject: [PATCH 5.12 199/292] ubifs: Set/Clear I_LINKABLE under i_lock for whiteout inode Date: Mon, 19 Jul 2021 16:54:21 +0200 Message-Id: <20210719144949.031103544@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210719144942.514164272@linuxfoundation.org> References: <20210719144942.514164272@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: Zhihao Cheng [ Upstream commit a801fcfeef96702fa3f9b22ad56c5eb1989d9221 ] xfstests-generic/476 reports a warning message as below: WARNING: CPU: 2 PID: 30347 at fs/inode.c:361 inc_nlink+0x52/0x70 Call Trace: do_rename+0x502/0xd40 [ubifs] ubifs_rename+0x8b/0x180 [ubifs] vfs_rename+0x476/0x1080 do_renameat2+0x67c/0x7b0 __x64_sys_renameat2+0x6e/0x90 do_syscall_64+0x66/0xe0 entry_SYSCALL_64_after_hwframe+0x44/0xae Following race case can cause this: rename_whiteout(Thread 1) wb_workfn(Thread 2) ubifs_rename do_rename __writeback_single_inode spin_lock(&inode->i_lock) whiteout->i_state |= I_LINKABLE inode->i_state &= ~dirty; ---- How race happens on i_state: (tmp = whiteout->i_state | I_LINKABLE) (tmp = inode->i_state & ~dirty) (whiteout->i_state = tmp) (inode->i_state = tmp) ---- spin_unlock(&inode->i_lock) inc_nlink(whiteout) WARN_ON(!(inode->i_state & I_LINKABLE)) !!! Fix to add i_lock to avoid i_state update race condition. Fixes: 9e0a1fff8db56ea ("ubifs: Implement RENAME_WHITEOUT") Signed-off-by: Zhihao Cheng Signed-off-by: Richard Weinberger Signed-off-by: Sasha Levin --- fs/ubifs/dir.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c index d9d8d7794eff..af1ea5fcd38d 100644 --- a/fs/ubifs/dir.c +++ b/fs/ubifs/dir.c @@ -1337,7 +1337,10 @@ static int do_rename(struct inode *old_dir, struct dentry *old_dentry, goto out_release; } + spin_lock(&whiteout->i_lock); whiteout->i_state |= I_LINKABLE; + spin_unlock(&whiteout->i_lock); + whiteout_ui = ubifs_inode(whiteout); whiteout_ui->data = dev; whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0)); @@ -1430,7 +1433,11 @@ static int do_rename(struct inode *old_dir, struct dentry *old_dentry, inc_nlink(whiteout); mark_inode_dirty(whiteout); + + spin_lock(&whiteout->i_lock); whiteout->i_state &= ~I_LINKABLE; + spin_unlock(&whiteout->i_lock); + iput(whiteout); } -- 2.30.2