Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp460811pxb; Thu, 9 Sep 2021 05:06:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzL10VhSJKE6n9eOTWz03U0t6nYhN8Stojjo1TQAjiCQ4Zts60MLp82P5jNsgx9Ag0vRk/w X-Received: by 2002:aa7:c649:: with SMTP id z9mr2821204edr.304.1631189210855; Thu, 09 Sep 2021 05:06:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631189210; cv=none; d=google.com; s=arc-20160816; b=qe4KaE3wx+xliMx9fwBPN3vVKrARMm34io+uoeKJOWaDkKQt/yMBygYlL23X1FyrCl fsnGkjV39QZW1B103pUeHLi/kgBWz8amh//PkYpumj+2bE0wpGYUBKNnlbCLwreIHqjO rFsiO8gby9ZWhRSsAdl8AqdCr0/alWQA7gp2FU7CzyWHWLhyFxaduiaSrHSJoOo4TI9Y cJRgDh9ApbPcHppgKhuHEmJIulaqZ0pbdWwbY0u3SAiAlZXnI+TLh81Q2pa2jYjLqkxy gXBKx8wVrdVM+QdZaQfxxON/8K7YYwirl2BYu7ZAi/3htdFe5jaQ4M1xPHcX3E297Hzk estA== 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=BbEUQNxqCqzjoW/Ae7OtvXUkHiCo8oCfICneeaI0rkg=; b=Q1DRd2KHy43HGL25clnerU9ER3fBAfF6wsYCzRw+/9KTURbD+53uOgRssKOcmPjAAL svswvYGhLaO46/hTYn+8Cbme5LWfQWD5nm36l69PpA9eUuDkMyOzAW8g74MzVs1uWyAV 65ipJzh9STqt493nLLf2rIUcIhRqLtt8aciR82fnvnUi8Zo0c3VspQcYdP4WSCRvJe5Q Rj86sVSNFmATd2WvMB+OLN2gb+Reu1lOwJXCkpLUTusluV9B/xAV6h/bWc1o0MWPmpa3 qhXN9wTKaQXsBLhvrV1Z8Rl7OhBmBIllounMQaMmp6iRH2HGR22gw7r92EoE8BGSViNy ybZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=f2A885g+; 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 hy16si1436187ejc.209.2021.09.09.05.06.14; Thu, 09 Sep 2021 05:06:50 -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=f2A885g+; 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 S243005AbhIIMEB (ORCPT + 99 others); Thu, 9 Sep 2021 08:04:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:34496 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238514AbhIIL4i (ORCPT ); Thu, 9 Sep 2021 07:56:38 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1ACA86137B; Thu, 9 Sep 2021 11:45:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631187920; bh=hpAsO3uU+KLhI25iJrKKBdXIaTI0/TFBAIKiNnkz4EU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f2A885g+ZEFvJ/jb1i/aL8Pdk/a+ni4AgtrD5MdYPpcnenBSlm0DZxPmbovO8Y/R0 Mtd8nfNyJYf8+DYethY8/jvt4dJ6hJ4rqXtV45cRtqU7ynlnmvGjvYcZeLwhLpsBXi 6VfHPjW0+AV1PewTOosRNj5eC8eQohzpg2ln/FfRuHTl46kYIWE0zj8rjpklKBGBZr w/OWxQYnBKwDzFbMM5Sio7hzS9ArKsexUgeM22k+8hQpY0mYHdgrNQaJhjqFp8FKi+ S9P9ZRLrjYOQ4tQ+pZ8jHRE6iay2TJ7a6bEY5eta2LMgJ3MxK7C5xwC4RydHwMOoGl bu7G7KxobiqeQ== 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 5.14 194/252] btrfs: subpage: fix race between prepare_pages() and btrfs_releasepage() Date: Thu, 9 Sep 2021 07:40:08 -0400 Message-Id: <20210909114106.141462-194-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210909114106.141462-1-sashal@kernel.org> References: <20210909114106.141462-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 ee34497500e1..8c57af3702fa 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -1340,7 +1340,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