mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Boris Burkov <boris@bur.io>, Qu Wenruo <wqu@suse.com>,
	Filipe Manana <fdmanana@suse.com>,
	David Sterba <dsterba@suse.com>, Sasha Levin <sashal@kernel.org>,
	clm@fb.com, josef@toxicpanda.com, linux-btrfs@vger.kernel.org
Subject: [PATCH AUTOSEL 6.13 37/49] btrfs: harden block_group::bg_list against list_del() races
Date: Thu,  3 Apr 2025 15:03:56 -0400	[thread overview]
Message-ID: <20250403190408.2676344-37-sashal@kernel.org> (raw)
In-Reply-To: <20250403190408.2676344-1-sashal@kernel.org>

From: Boris Burkov <boris@bur.io>

[ Upstream commit 7511e29cf1355b2c47d0effb39e463119913e2f6 ]

As far as I can tell, these calls of list_del_init() on bg_list cannot
run concurrently with btrfs_mark_bg_unused() or btrfs_mark_bg_to_reclaim(),
as they are in transaction error paths and situations where the block
group is readonly.

However, if there is any chance at all of racing with mark_bg_unused(),
or a different future user of bg_list, better to be safe than sorry.

Otherwise we risk the following interleaving (bg_list refcount in parens)

T1 (some random op)                       T2 (btrfs_mark_bg_unused)
                                        !list_empty(&bg->bg_list); (1)
list_del_init(&bg->bg_list); (1)
                                        list_move_tail (1)
btrfs_put_block_group (0)
                                        btrfs_delete_unused_bgs
                                             bg = list_first_entry
                                             list_del_init(&bg->bg_list);
                                             btrfs_put_block_group(bg); (-1)

Ultimately, this results in a broken ref count that hits zero one deref
early and the real final deref underflows the refcount, resulting in a WARNING.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 fs/btrfs/extent-tree.c |  8 ++++++++
 fs/btrfs/transaction.c | 12 ++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 3c6f7fecbb9aa..840e7959b0772 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -2873,7 +2873,15 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
 						   block_group->length,
 						   &trimmed);
 
+		/*
+		 * Not strictly necessary to lock, as the block_group should be
+		 * read-only from btrfs_delete_unused_bgs().
+		 */
+		ASSERT(block_group->ro);
+		spin_lock(&fs_info->unused_bgs_lock);
 		list_del_init(&block_group->bg_list);
+		spin_unlock(&fs_info->unused_bgs_lock);
+
 		btrfs_unfreeze_block_group(block_group);
 		btrfs_put_block_group(block_group);
 
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 914a22cc950b5..7c789b4b946b3 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -160,7 +160,13 @@ void btrfs_put_transaction(struct btrfs_transaction *transaction)
 			cache = list_first_entry(&transaction->deleted_bgs,
 						 struct btrfs_block_group,
 						 bg_list);
+			/*
+			 * Not strictly necessary to lock, as no other task will be using a
+			 * block_group on the deleted_bgs list during a transaction abort.
+			 */
+			spin_lock(&transaction->fs_info->unused_bgs_lock);
 			list_del_init(&cache->bg_list);
+			spin_unlock(&transaction->fs_info->unused_bgs_lock);
 			btrfs_unfreeze_block_group(cache);
 			btrfs_put_block_group(cache);
 		}
@@ -2097,7 +2103,13 @@ static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
 
        list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
                btrfs_dec_delayed_refs_rsv_bg_inserts(fs_info);
+		/*
+		* Not strictly necessary to lock, as no other task will be using a
+		* block_group on the new_bgs list during a transaction abort.
+		*/
+	       spin_lock(&fs_info->unused_bgs_lock);
                list_del_init(&block_group->bg_list);
+	       spin_unlock(&fs_info->unused_bgs_lock);
        }
 }
 
-- 
2.39.5


  parent reply	other threads:[~2025-04-03 19:05 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-03 19:03 [PATCH AUTOSEL 6.13 01/49] wifi: ath11k: Fix DMA buffer allocation to resolve SWIOTLB issues Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 02/49] wifi: ath11k: fix memory leak in ath11k_xxx_remove() Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 03/49] wifi: ath12k: fix memory leak in ath12k_pci_remove() Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 04/49] wifi: ath12k: Fix invalid entry fetch in ath12k_dp_mon_srng_process Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 05/49] ata: libata-core: Add 'external' to the libata.force kernel parameter Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 06/49] scsi: mpi3mr: Avoid reply queue full condition Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 07/49] scsi: mpi3mr: Synchronous access b/w reset and tm thread for reply queue Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 08/49] net: page_pool: don't cast mp param to devmem Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 09/49] f2fs: don't retry IO for corrupted data scenario Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 10/49] wifi: mac80211: add strict mode disabling workarounds Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 11/49] wifi: mac80211: ensure sdata->work is canceled before initialized Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 12/49] scsi: target: spc: Fix RSOC parameter data header size Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 13/49] net: usb: asix_devices: add FiberGecko DeviceID Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 14/49] page_pool: avoid infinite loop to schedule delayed worker Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 15/49] can: flexcan: Add quirk to handle separate interrupt lines for mailboxes Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 16/49] can: flexcan: add NXP S32G2/S32G3 SoC support Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 17/49] jfs: Fix uninit-value access of imap allocated in the diMount() function Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 18/49] mptcp: move the whole rx path under msk socket lock protection Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 19/49] fs/jfs: cast inactags to s64 to prevent potential overflow Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 20/49] fs/jfs: Prevent integer overflow in AG size calculation Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 21/49] jfs: Prevent copying of nlink with value 0 from disk inode Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 22/49] jfs: add sanity check for agwidth in dbMount Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 23/49] ata: libata-eh: Do not use ATAPI DMA for a device limited to PIO mode Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 24/49] net: sfp: add quirk for 2.5G OEM BX SFP Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 25/49] wifi: ath12k: Fix invalid data access in ath12k_dp_rx_h_undecap_nwifi Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 26/49] null_blk: replace null_process_cmd() call in null_zone_write() Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 27/49] f2fs: fix to avoid out-of-bounds access in f2fs_truncate_inode_blocks() Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 28/49] net: sfp: add quirk for FS SFP-10GM-T copper SFP+ module Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 29/49] ahci: add PCI ID for Marvell 88SE9215 SATA Controller Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 30/49] ext4: protect ext4_release_dquot against freezing Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 31/49] Revert "f2fs: rebuild nat_bits during umount" Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 32/49] ext4: ignore xattrs past end Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 33/49] cdc_ether|r8152: ThinkPad Hybrid USB-C/A Dock quirk Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 34/49] scsi: st: Fix array overflow in st_setup() Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 35/49] ahci: Marvell 88SE9215 controllers prefer DMA for ATAPI Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 36/49] btrfs: reject out-of-band dirty folios during writeback Sasha Levin
2025-04-03 19:03 ` Sasha Levin [this message]
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 38/49] wifi: mt76: mt76x2u: add TP-Link TL-WDN6200 ID to device table Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 39/49] net: vlan: don't propagate flags on open Sasha Levin
2025-04-03 19:03 ` [PATCH AUTOSEL 6.13 40/49] tracing: fix return value in __ftrace_event_enable_disable for TRACE_REG_UNREGISTER Sasha Levin
2025-04-03 19:04 ` [PATCH AUTOSEL 6.13 41/49] Bluetooth: btusb: Add new VID/PID for WCN785x Sasha Levin
2025-04-03 19:04 ` [PATCH AUTOSEL 6.13 42/49] Bluetooth: btintel_pcie: Add device id of Whale Peak Sasha Levin
2025-04-03 19:04 ` [PATCH AUTOSEL 6.13 43/49] Bluetooth: btusb: Add 13 USB device IDs for Qualcomm WCN785x Sasha Levin
2025-04-03 19:04 ` [PATCH AUTOSEL 6.13 44/49] Bluetooth: hci_uart: fix race during initialization Sasha Levin
2025-04-03 19:04 ` [PATCH AUTOSEL 6.13 45/49] Bluetooth: btusb: Add 2 HWIDs for MT7922 Sasha Levin
2025-04-03 19:04 ` [PATCH AUTOSEL 6.13 46/49] Bluetooth: hci_qca: use the power sequencer for wcn6750 Sasha Levin
2025-04-03 19:04 ` [PATCH AUTOSEL 6.13 47/49] Bluetooth: qca: simplify WCN399x NVM loading Sasha Levin
2025-04-03 19:04 ` [PATCH AUTOSEL 6.13 48/49] Bluetooth: Add quirk for broken READ_VOICE_SETTING Sasha Levin
2025-04-03 19:04 ` [PATCH AUTOSEL 6.13 49/49] Bluetooth: Add quirk for broken READ_PAGE_SCAN_TYPE 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=20250403190408.2676344-37-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=boris@bur.io \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=fdmanana@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®