All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Qu Wenruo <wqu@suse.com>, Ritesh Harjani <riteshh@linux.ibm.com>,
	Filipe Manana <fdmanana@suse.com>,
	David Sterba <dsterba@suse.com>, Sasha Levin <sashal@kernel.org>,
	linux-btrfs@vger.kernel.org
Subject: [PATCH AUTOSEL 4.4 28/35] btrfs: subpage: fix race between prepare_pages() and btrfs_releasepage()
Date: Thu,  9 Sep 2021 08:01:09 -0400	[thread overview]
Message-ID: <20210909120116.150912-28-sashal@kernel.org> (raw)
In-Reply-To: <20210909120116.150912-1-sashal@kernel.org>

From: Qu Wenruo <wqu@suse.com>

[ 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 <riteshh@linux.ibm.com>
Tested-by: Ritesh Harjani <riteshh@linux.ibm.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 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 2426dc56426f..c917a04118cc 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1307,7 +1307,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


  parent reply	other threads:[~2021-09-09 13:22 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-09 12:00 [PATCH AUTOSEL 4.4 01/35] crypto: mxs-dcp - Use sg_mapping_iter to copy data Sasha Levin
2021-09-09 12:00 ` Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 02/35] PCI: Use pci_update_current_state() in pci_enable_device_flags() Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 03/35] iio: dac: ad5624r: Fix incorrect handling of an optional regulator Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 04/35] video: fbdev: kyro: fix a DoS bug by restricting user input Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 05/35] netlink: Deal with ESRCH error in nlmsg_notify() Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 06/35] Smack: Fix wrong semantics in smk_access_entry() Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 07/35] usb: host: fotg210: fix the actual_length of an iso packet Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 08/35] usb: gadget: u_ether: fix a potential null pointer dereference Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 09/35] tty: serial: jsm: hold port lock when reporting modem line changes Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 10/35] bpf/tests: Fix copy-and-paste error in double word test Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 11/35] bpf/tests: Do not PASS tests without actually testing the result Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 12/35] video: fbdev: asiliantfb: Error out if 'pixclock' equals zero Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 13/35] video: fbdev: kyro: " Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 14/35] video: fbdev: riva: " Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 15/35] ipv4: ip_output.c: Fix out-of-bounds warning in ip_copy_addrs() Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 16/35] s390/jump_label: print real address in a case of a jump label bug Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 17/35] serial: 8250: Define RX trigger levels for OxSemi 950 devices Sasha Levin
2021-09-09 12:00 ` [PATCH AUTOSEL 4.4 18/35] xtensa: ISS: don't panic in rs_init Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 19/35] hvsi: don't panic on tty_register_driver failure Sasha Levin
2021-09-09 12:01   ` Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 20/35] serial: 8250_pci: make setup_port() parameters explicitly unsigned Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 21/35] Bluetooth: skip invalid hci_sync_conn_complete_evt Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 22/35] gpu: drm: amd: amdgpu: amdgpu_i2c: fix possible uninitialized-variable access in amdgpu_i2c_router_select_ddc_port() Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 23/35] ARM: tegra: tamonten: Fix UART pad setting Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 24/35] ACPICA: iASL: Fix for WPBT table with no command-line arguments Sasha Levin
2021-09-10  7:45   ` Pavel Machek
2021-09-11 14:38     ` Sasha Levin
2021-09-13 12:20       ` Rafael J. Wysocki
2021-09-13 12:20         ` [Devel] " Rafael J. Wysocki
2021-09-17 19:21     ` Moore, Robert
2021-09-17 19:21       ` [Devel] " Moore, Robert
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 25/35] rpc: fix gss_svc_init cleanup on failure Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 26/35] gfs2: Don't call dlm after protocol is unmounted Sasha Levin
2021-09-09 12:01   ` [Cluster-devel] " Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 27/35] btrfs: subpage: check if there are compressed extents inside one page Sasha Levin
2021-09-09 13:01   ` David Sterba
2021-09-09 12:01 ` Sasha Levin [this message]
2021-09-09 13:02   ` [PATCH AUTOSEL 4.4 28/35] btrfs: subpage: fix race between prepare_pages() and btrfs_releasepage() David Sterba
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 29/35] ASoC: intel: atom: Revert PCM buffer address setup workaround again Sasha Levin
2021-09-09 12:07   ` Takashi Iwai
2021-09-09 12:07     ` Takashi Iwai
2021-09-12 21:26     ` Sasha Levin
2021-09-12 21:26       ` Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 30/35] mmc: rtsx_pci: Fix long reads when clock is prescaled Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 31/35] cifs: fix wrong release in sess_alloc_buffer() failed path Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 32/35] Revert "USB: xhci: fix U1/U2 handling for hardware with XHCI_INTEL_HOST quirk set" Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 33/35] parport: remove non-zero check on count Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 34/35] ath9k: fix OOB read ar9300_eeprom_restore_internal Sasha Levin
2021-09-09 12:01 ` [PATCH AUTOSEL 4.4 35/35] net: fix NULL pointer reference in cipso_v4_doi_free Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210909120116.150912-28-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=dsterba@suse.com \
    --cc=fdmanana@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=riteshh@linux.ibm.com \
    --cc=stable@vger.kernel.org \
    --cc=wqu@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.