Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp517451pxb; Thu, 9 Sep 2021 06:17:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwpWto9nhZwUG2Ct8P//RctH1pZPDtmp6dBcQT7SzTotczvbpT/mcSWlL2qxn9XzNHZLKD1 X-Received: by 2002:a05:6e02:1a6b:: with SMTP id w11mr981148ilv.21.1631193435464; Thu, 09 Sep 2021 06:17:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631193435; cv=none; d=google.com; s=arc-20160816; b=s7O7nKB7pp9bVLKeRb26U1etk8UCJz3jpRUqPubdHUkiMyCYly6igSdo0h15yg3cnR xrJnoCafGdyDvN6xuOuYjbIlQ0dEGApx7UXio8klydah2tP1rGA3Jp2PrqHu52zMtRNc NLilZ2ccNI5+nnO4x6s2VFENWGVdfv0HnPr+GRe8fgNzs2rYNyoK+gFWTRqQZejV5IRZ TcH4nPY7di1kHxaBXpCwCZo5FIv6FLcyA99PU2uS+vLl0OCfFvF/0amwQOfl++HQp5K9 aCERiNMkWJ5TCEU423cj/hS+PW8mxZZ0TelGqoKZTjoP70Dqxi1Bvm9fLtePF36s4bLH UV+A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=CesqDhXswGtA+/hUbNbeaTilQXlpq/BkRCH0Rns36PI=; b=k3SVqqylLL8ONCzZmdn3R+YZLvN/ysdU+7uaUCC2k/Ap/xBbUrlD7nHdvnn5RIS/Nq WfiQsJ4qktxoFe6fFhumFz0cQ5LuOfeh89CojsxxZWzoWo+N2TUGPv10qRDhq822bwY8 dql7XuqwN36AkCfnq/4uavRmioG26fCjY2BaaguRh0yejqdAuy+h2gaQIkwMAQxt7wNh 1qmzx9i+CBinNwu2F7HgSaV1uqBPGuIaIocUN/GcGVBBxOEnFrHVU+WJPa4nvD5cdgjr 33u+fQKXdHr+24EEspjjQX8BUAWLKTTg5bRFqw083IyB9GM0E+1UApYHw1t/8HSfNPS7 ZUHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cv3zKUgg; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a2si1604566ilp.123.2021.09.09.06.16.57; Thu, 09 Sep 2021 06:17:15 -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=@kernel.org header.s=k20201202 header.b=cv3zKUgg; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356647AbhIINOl (ORCPT + 99 others); Thu, 9 Sep 2021 09:14:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:43552 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355735AbhIINDl (ORCPT ); Thu, 9 Sep 2021 09:03:41 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D29536327F; Thu, 9 Sep 2021 11:59:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631188794; bh=zxIikZRaq4hB/3fhVzLvGkoBhdcJrIX7mwAZc2aU5dU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cv3zKUggy89WJEzni4DqAYroAQKT1Dg4rrA8q8++xleb7XB2PNAJnY+QzieAab/6d qLnfY2Qwne9Arl7u8WSiu8ssIKah12bnrb78VrQfHxh1T34uoyNumKsIAFiRX556p5 FS9lw70dNYs3EDgPp1XDH5aH4f1vLbZ5KEYdXGJfHiB3yeQ2G1XBe+iUp6arUybdAp lZQ7FLVezUN1+iqVWXQMCoIF2j945JMPVk2WeGIg5KrGPpPF/9Ejab+XlgcIa+WyhW oi2hCwM7/HtjzKzZbLyWnofay/7I18SdPGOEYkhH2LwHTxBTHZzYya8wKh7I+OwnUE 1E5byOfZY7V/A== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Qu Wenruo , Ritesh Harjani , Filipe Manana , David Sterba , Sasha Levin , linux-btrfs@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 43/59] btrfs: subpage: fix race between prepare_pages() and btrfs_releasepage() Date: Thu, 9 Sep 2021 07:58:44 -0400 Message-Id: <20210909115900.149795-43-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210909115900.149795-1-sashal@kernel.org> References: <20210909115900.149795-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo [ Upstream commit e0467866198f7f536806f39e5d0d91ae8018de08 ] [BUG] When running generic/095, there is a high chance to crash with subpage data RW support: assertion failed: PagePrivate(page) && page->private ------------[ cut here ]------------ kernel BUG at fs/btrfs/ctree.h:3403! Internal error: Oops - BUG: 0 [#1] SMP CPU: 1 PID: 3567 Comm: fio Tainted: 5.12.0-rc7-custom+ #17 Hardware name: Khadas VIM3 (DT) Call trace: assertfail.constprop.0+0x28/0x2c [btrfs] btrfs_subpage_assert+0x80/0xa0 [btrfs] btrfs_subpage_set_uptodate+0x34/0xec [btrfs] btrfs_page_clamp_set_uptodate+0x74/0xa4 [btrfs] btrfs_dirty_pages+0x160/0x270 [btrfs] btrfs_buffered_write+0x444/0x630 [btrfs] btrfs_direct_write+0x1cc/0x2d0 [btrfs] btrfs_file_write_iter+0xc0/0x160 [btrfs] new_sync_write+0xe8/0x180 vfs_write+0x1b4/0x210 ksys_pwrite64+0x7c/0xc0 __arm64_sys_pwrite64+0x24/0x30 el0_svc_common.constprop.0+0x70/0x140 do_el0_svc+0x28/0x90 el0_svc+0x2c/0x54 el0_sync_handler+0x1a8/0x1ac el0_sync+0x170/0x180 Code: f0000160 913be042 913c4000 955444bc (d4210000) ---[ end trace 3fdd39f4cccedd68 ]--- [CAUSE] Although prepare_pages() calls find_or_create_page(), which returns the page locked, but in later prepare_uptodate_page() calls, we may call btrfs_readpage() which will unlock the page before it returns. This leaves a window where btrfs_releasepage() can sneak in and release the page, clearing page->private and causing above ASSERT(). [FIX] In prepare_uptodate_page(), we should not only check page->mapping, but also PagePrivate() to ensure we are still holding the correct page which has proper fs context setup. Reported-by: Ritesh Harjani Tested-by: Ritesh Harjani Reviewed-by: Filipe Manana Signed-off-by: Qu Wenruo Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/file.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index dd2504322a87..7798355098db 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -1343,7 +1343,18 @@ static int prepare_uptodate_page(struct inode *inode, unlock_page(page); return -EIO; } - if (page->mapping != inode->i_mapping) { + + /* + * Since btrfs_readpage() will unlock the page before it + * returns, there is a window where btrfs_releasepage() can + * be called to release the page. + * Here we check both inode mapping and PagePrivate() to + * make sure the page was not released. + * + * The private flag check is essential for subpage as we need + * to store extra bitmap using page->private. + */ + if (page->mapping != inode->i_mapping || !PagePrivate(page)) { unlock_page(page); return -EAGAIN; } -- 2.30.2