Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4161787pxv; Mon, 19 Jul 2021 18:54:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxkhXBHaALbfXJ22IXUy0Nnm5Wq26Sj32lKPhQmhOCFPTn6wUoaQ8D8E0bzidYii1jwGQ7S X-Received: by 2002:a05:6402:d54:: with SMTP id ec20mr39321479edb.41.1626746091434; Mon, 19 Jul 2021 18:54:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626746091; cv=none; d=google.com; s=arc-20160816; b=z3gXKL05NeIGfyhp3G+FGrVBjLfAES0YUy/J2Ejt/QyKBU6LKBFpCV7AJjFiWBFGGs HZD74EHQd2UOMW3zFeUvqZpX6FQPtRA62aA85S1g+UJsOsMxeNUpXxnlVTSS6fINIAlS miTIXvsmblrsTKQ2erBaLPd+LuWjgOWAP6Rdqfx1V3lekD049GTOmLl7rv0vVnQsbbJv n/EsPlLtWTU3fDAYsD2epuCB0FxrviO7ffdwOaBLqrXa/+G9yJoFGyFTU53qhFaGrAv3 RO/30d9IceVtH/R0jqCUmVUxL39oTyc6BDy5poKtBbHjjc6byUi7na0nVZTndIDJPZUV fxOA== 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=tfEhOm3MLyoKvCbuZLZR7vH03J3QvQn1nNPPo0hypO4=; b=odKvm2c+MlMWs+uCWHODQVF8YL6K4gjhhwtDqDf0zEMTOXVXsHqegMqINhUMyk7c9x +hlzvWXryDkLE/7xfGx2tf7JaHfF8CxiIgG23rCQ5nzzHQ8pGsM8YLa4tBCb/s9/cgnM UO1g+yFHm14L0C/0MbsSmdZC3kDazt+f3E48Pi7CRgXOOVaGCEy5kJRpxEcAoPZk6sFE rjQQMZLk4bwUX9xDe8otKBT97S4hR4tMEy5mT+sIvAvAL0EDL+0OhpgJhAbpp1oOrXL0 8NvTwOr2J5BbOv/qNHQedeZpje5u6RU3tjvplfDZvIM6SBH7yb8kN40LTf1srJ0fRYYF Ubhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="o3Ztew/F"; 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 w4si21593689edr.349.2021.07.19.18.54.29; Mon, 19 Jul 2021 18:54:51 -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="o3Ztew/F"; 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 S1345686AbhGSPNn (ORCPT + 99 others); Mon, 19 Jul 2021 11:13:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:40962 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344253AbhGSOsn (ORCPT ); Mon, 19 Jul 2021 10:48:43 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4548F61289; Mon, 19 Jul 2021 15:27:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626708460; bh=iVA0Irtyj5+vISta1uNTPfwIhilsrb46kFwIemRbYWc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o3Ztew/FapXlGTraAo2pDk8Gjedo9baZ2CAijyIg6cqyn46Ds2gDQbt/TfwBw5cqb WVz6OzJ4YzGQ1x3IgGVdOGrcBWLUfZHM1INzRHe8PswI3uiVgxoUJAsDExcdbfa/QN OBAvQpcgx3bLy9viVUCXKBrGuV9ooz9SKr5vsW5U= 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.14 294/315] ubifs: Set/Clear I_LINKABLE under i_lock for whiteout inode Date: Mon, 19 Jul 2021 16:53:03 +0200 Message-Id: <20210719144953.135574307@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210719144942.861561397@linuxfoundation.org> References: <20210719144942.861561397@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 9d5face7fdc0..de0d63a347ac 100644 --- a/fs/ubifs/dir.c +++ b/fs/ubifs/dir.c @@ -1408,7 +1408,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)); @@ -1501,7 +1504,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