Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3780601pxv; Mon, 19 Jul 2021 08:34:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVrVNqaMnNHN4DdKRo2Ov7q1wt8SpGOruVsfh2UAzEfA/RBoJ+JaVCKAq8L8CK9FeL6IKa X-Received: by 2002:aa7:d457:: with SMTP id q23mr34405047edr.324.1626708883687; Mon, 19 Jul 2021 08:34:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626708883; cv=none; d=google.com; s=arc-20160816; b=kYrfrrWxDQ43dY44y7ycRcfDe7+TJVUn2jgDb4Up9CoJrAB+riT0raoeSelvz4xIId 47drsiDWMKVxvJTry8yLpTSljcWo5Z8ekoRPlmtk3cx53IlAOpDYyvzMg/iFnzQjrm2L t0NekCMoenjkhGKoHExlmsau8xMDSGuId5F06gtHQP0nsw3UrafikzCvDEtrgcafOFc8 GNmh/k860E0xKA55QXmxeIg80BqLSqF+1NMASE+bNOavap4nGf9XjZW5/FwEoLhduCD3 yUMUYIN9J1BvO/3YoBsyba7i4j/D/YQOlm4mSZ762ndt0cz2wveoYoadozTw4zoG/OlY 1nSA== 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=4oPaj6hJylbw7bvCcLhTU2LTd+XUORqE7dtJuGN2GtY=; b=dnaZ8LnEm89MpiNavWNNsDN+guQ9Bjja3SxTJr8KjWYBVuFe8/rrxfAz6JvpRTKXpm /hqCO7bZfm9jw+I3vWWPPn5YGXU+bTt97ogRHkbbrnHNn1b1dcdlRbnNxXBc40EGboEj ZIHXFXDtOXtN5H811UZmnikT+dvSJRghHxvIbls/xSCKUnVMob6HvuOCZeZZl5x36qMW yYFHTrQAIxiYPcRRCwqGe1+H6HlZlKBDUrn+VcSmVayyrtfHf7Jcx1Hn3m4Lo9+RdW04 6pKnBPvID4cJsddBQJfQeLbC5yol/X85y9WFSjY5mnSvwWjFQf+Sd085LnbhiE9KtP6o eaXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=G5fX8Y7e; 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 ec7si2029720ejb.375.2021.07.19.08.34.20; Mon, 19 Jul 2021 08:34:43 -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=G5fX8Y7e; 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 S241908AbhGSOvb (ORCPT + 99 others); Mon, 19 Jul 2021 10:51:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:47094 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244123AbhGSOeD (ORCPT ); Mon, 19 Jul 2021 10:34:03 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9190361181; Mon, 19 Jul 2021 15:13:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626707604; bh=0MhgbH6QdC0c5kmzGdjW5OwP4rKEMVjEmrLTcINHv/E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G5fX8Y7ehFw5dwXKbnVX1kRFyN7b5Jx6P7n0KFXj5aNqJxQs+qIc8FruqmsAYujWE pp0rlvjRtUPNysjhDx9i55MAb/sx8WPwvDRbwfOMumunRMzPlK/JYmEyZFmr3b98vF Kkk/9YYX9/Zy8ZEAddp6E5yty3ljBQJXoA5jkauQ= 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 4.9 230/245] ubifs: Set/Clear I_LINKABLE under i_lock for whiteout inode Date: Mon, 19 Jul 2021 16:52:52 +0200 Message-Id: <20210719144947.820522067@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210719144940.288257948@linuxfoundation.org> References: <20210719144940.288257948@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 87ab02e2d666..56eed54633cf 100644 --- a/fs/ubifs/dir.c +++ b/fs/ubifs/dir.c @@ -1144,7 +1144,10 @@ static int do_rename(struct inode *old_dir, struct dentry *old_dentry, return err; } + 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)); @@ -1239,7 +1242,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