From patchwork Fri Mar 7 00:29:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Burkov X-Patchwork-Id: 14005588 Received: from fhigh-a1-smtp.messagingengine.com (fhigh-a1-smtp.messagingengine.com [103.168.172.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FBB38F4A for ; Fri, 7 Mar 2025 00:28:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741307296; cv=none; b=e/WPEuu37HlofegOGlYwHylqy+QxvZFWlYH0ZqXBAvkhE26nEVz/mlMEd/xIAKkdUpCihcmhIoZNYYoDDr3sMwwRZcvwWiVrC9BMEUkv+WUq28k+pZ9dI+tDUVzZ2YDKgLoSkb6tckzqgcrkzT3GK5E0Xl6QX4Vjv+nhAtM3mG0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741307296; c=relaxed/simple; bh=5Fo2OHag6NBEm/dVepz2haqPsR1WAIqJuls2LS9P26U=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c5HnAKO7fOmIRFM/S05Fa9Hd8qlGoBpDTGwpfR5JLUTlj4fvmqNaQQKDGR1jpvgKdf7EAuGgTAgQjAzdEVQHzeD+4B5W5z+20ldeDbrlSmLGV0xLHzuSG6hsQVlRasXdqheNK83k7lQJFaIbnARd90z2pY62lHHhc8c0wGcRqlo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io; spf=pass smtp.mailfrom=bur.io; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b=Dh364Ji9; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=nvv+rntb; arc=none smtp.client-ip=103.168.172.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bur.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b="Dh364Ji9"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="nvv+rntb" Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailfhigh.phl.internal (Postfix) with ESMTP id 3648C1140152; Thu, 6 Mar 2025 19:28:13 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-02.internal (MEProxy); Thu, 06 Mar 2025 19:28:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bur.io; h=cc :content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1741307293; x= 1741393693; bh=pXmfqm5ZlsKs691hu+4fmzp9bc8pYFVHp5iDm63cXxQ=; b=D h364Ji9t+Kv/WpVCxo0MBbATwX6tLXEn7nwTz5aN8Gfr5tKF3Sr9QBXUtpMbyAuO UhUnUf9Nk1pea2NqsNzcblKzn5Ku/+MS0qJXkurIJ2H9gnz+cm2iG30RfOlQ61Xx 2SkCyRhbzyl9rlwBEnWsZZdhz0RatkGlQjMiRF9emjxkQ48hQF1eKk0FO09CUqRa gcvZip3IMzce25uTQhDhq+sCfvzkzh2rnPVed94QBRucCzLh+edBxHYTXlXpNlgx d1cZefhnBVFhzPb3CtnxOj879TmrBmReJWFcUwnzv0t2GbZ2M5wYSTROcPHjh7LV LFeve7onXfeAbHlNCj0Eg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1741307293; x=1741393693; bh=pXmfqm5ZlsKs691hu+4fmzp9bc8p YFVHp5iDm63cXxQ=; b=nvv+rntbqkXRaZIfA7qdYUOZj9qHqbEzl7QoxnUlZ9zw PMrSzxpET0ILvtKJXBJJ4LSE40SDcKT7bdisfBvqfF8ELMEcSwwQcB5ugR4ifIJQ K7gxw5zi9p7dxpWmtSwQXxF1CxyD/ZVy+SUlVVWEf7ueSFBYv4Ps+8d16cuuWdsj o1yLKttmiF7crn/IFIPhUzmbPrTa1HaWa2x1byrGI7QGKCF/Hz0RchbaQW774Cri 8mJP3TAiZtSkRhXL8fG//t0yWy6zCi12mRPK7TLABTDbFW7+2WiuS1suME8S3ThE h0PkJ6UANb3lnlR5eqzLpK4uNw+c0IWiEzwlCpzbvg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddutdelvddtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehorhhishcuuehurhhkohhv uceosghorhhishessghurhdrihhoqeenucggtffrrghtthgvrhhnpeeiueffuedvieeuje fhheeigfekvedujeejjeffvedvhedtudefiefhkeegueehleenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhrihhssegsuhhrrdhiohdpnh gspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplhhinhhu gidqsghtrhhfshesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehkvghrnh gvlhdqthgvrghmsehfsgdrtghomh X-ME-Proxy: Feedback-ID: i083147f8:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 6 Mar 2025 19:28:12 -0500 (EST) From: Boris Burkov To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 1/5] btrfs: fix bg refcount race in btrfs_create_pending_block_groups Date: Thu, 6 Mar 2025 16:29:01 -0800 Message-ID: X-Mailer: git-send-email 2.48.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To avoid a race where mark_bg_unused spuriously "moved" the block_group from one bg_list attachment to another without taking a ref, we mark a new block group with the bit BLOCK_GROUP_FLAG_NEW. However, this fix is not quite complete. Since it does not use the unused_bg_lock, it is possible for the following race to occur: create_pending_block_groups mark_bg_unused if list_empty // false list_del_init clear_bit else if (test_bit) // true list_move_tail And we get into the exact same broken ref count situation. Those look something like: [ 1272.943113] ------------[ cut here ]------------ [ 1272.943527] refcount_t: underflow; use-after-free. [ 1272.943967] WARNING: CPU: 1 PID: 61 at lib/refcount.c:28 refcount_warn_saturate+0xba/0x110 [ 1272.944731] Modules linked in: btrfs virtio_net xor zstd_compress raid6_pq null_blk [last unloaded: btrfs] [ 1272.945550] CPU: 1 UID: 0 PID: 61 Comm: kworker/u32:1 Kdump: loaded Tainted: G W 6.14.0-rc5+ #108 [ 1272.946368] Tainted: [W]=WARN [ 1272.946585] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.16.3-1-1 04/01/2014 [ 1272.947273] Workqueue: btrfs_discard btrfs_discard_workfn [btrfs] [ 1272.947788] RIP: 0010:refcount_warn_saturate+0xba/0x110 [ 1272.948180] Code: 01 01 e8 e9 c7 a9 ff 0f 0b c3 cc cc cc cc 80 3d 3f 4a de 01 00 75 85 48 c7 c7 00 9b 9f 8f c6 05 2f 4a de 01 01 e8 c6 c7 a9 ff <0f> 0b c3 cc cc cc cc 80 3d 1d 4a de 01 00 0f 85 5e ff ff ff 48 c7 [ 1272.949532] RSP: 0018:ffffbf1200247df0 EFLAGS: 00010282 [ 1272.949901] RAX: 0000000000000000 RBX: ffffa14b00e3f800 RCX: 0000000000000000 [ 1272.950437] RDX: 0000000000000000 RSI: ffffbf1200247c78 RDI: 00000000ffffdfff [ 1272.950986] RBP: ffffa14b00dc2860 R08: 00000000ffffdfff R09: ffffffff90526268 [ 1272.951512] R10: ffffffff904762c0 R11: 0000000063666572 R12: ffffa14b00dc28c0 [ 1272.952024] R13: 0000000000000000 R14: ffffa14b00dc2868 R15: 000001285dcd12c0 [ 1272.952850] FS: 0000000000000000(0000) GS:ffffa14d33c40000(0000) knlGS:0000000000000000 [ 1272.953458] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 1272.953931] CR2: 00007f838cbda000 CR3: 000000010104e000 CR4: 00000000000006f0 [ 1272.954474] Call Trace: [ 1272.954655] [ 1272.954812] ? refcount_warn_saturate+0xba/0x110 [ 1272.955173] ? __warn.cold+0x93/0xd7 [ 1272.955487] ? refcount_warn_saturate+0xba/0x110 [ 1272.955816] ? report_bug+0xe7/0x120 [ 1272.956103] ? handle_bug+0x53/0x90 [ 1272.956424] ? exc_invalid_op+0x13/0x60 [ 1272.956700] ? asm_exc_invalid_op+0x16/0x20 [ 1272.957011] ? refcount_warn_saturate+0xba/0x110 [ 1272.957399] btrfs_discard_cancel_work.cold+0x26/0x2b [btrfs] [ 1272.957853] btrfs_put_block_group.cold+0x5d/0x8e [btrfs] [ 1272.958289] btrfs_discard_workfn+0x194/0x380 [btrfs] [ 1272.958729] process_one_work+0x130/0x290 [ 1272.959026] worker_thread+0x2ea/0x420 [ 1272.959335] ? __pfx_worker_thread+0x10/0x10 [ 1272.959644] kthread+0xd7/0x1c0 [ 1272.959872] ? __pfx_kthread+0x10/0x10 [ 1272.960172] ret_from_fork+0x30/0x50 [ 1272.960474] ? __pfx_kthread+0x10/0x10 [ 1272.960745] ret_from_fork_asm+0x1a/0x30 [ 1272.961035] [ 1272.961238] ---[ end trace 0000000000000000 ]--- Though we have seen them in the async discard workfn as well. It is most likely to happen after a relocation finishes which cancels discard, tears down the block group, etc. Fix this fully by taking the lock around the list_del_init + clear_bit so that the two are done atomically. Fixes: 0657b20c5a76 ("btrfs: fix use-after-free of new block group that became unused") Signed-off-by: Boris Burkov Reviewed-by: Qu Wenruo --- fs/btrfs/block-group.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index 64f0268dcf02..2db1497b58d9 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -2797,8 +2797,11 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) /* Already aborted the transaction if it failed. */ next: btrfs_dec_delayed_refs_rsv_bg_inserts(fs_info); + + spin_lock(&fs_info->unused_bgs_lock); list_del_init(&block_group->bg_list); clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags); + spin_unlock(&fs_info->unused_bgs_lock); /* * If the block group is still unused, add it to the list of From patchwork Fri Mar 7 00:29:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Burkov X-Patchwork-Id: 14005589 Received: from fout-a6-smtp.messagingengine.com (fout-a6-smtp.messagingengine.com [103.168.172.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 51C508BE5 for ; Fri, 7 Mar 2025 00:28:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.149 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741307298; cv=none; b=N7z8BdnQ+WvT1jWvhP/OhedN4bw/NquZ1c79OfOr0OCnbfpiSDZEKflq+yEKb2Q2hLc1VM5K3K4le1x692RgjMBLD2/ubUtTbqM8XOQqHY8ZrIv6kZEUuH6SACQmnXZhn+hoYoM84uVZZaKjjIbbCNL6gLasrKIulvBwRrDCMS8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741307298; c=relaxed/simple; bh=Brsv55xiz6XbuyAwZiyK5xdL5xwTpZ+DzABrVmMXyWg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Bw54pVYMKG9yVAxlSfpdbungMhqSqIqYB+Pod9V6r8ZVpJTdhr6dTzUTh6GraWhMdsd8N5q+FbN4kms7QPlPHx9Q1/JrIMMTCQx9j5gJeOLQLpwN6dO95ngX/2zTgE2LpGYzGWpsqMBBD7fCOomTYTefdC+J+Q8zqSVp6fA1YUo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io; spf=pass smtp.mailfrom=bur.io; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b=Zt4CKgUm; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=jwPnGAPN; arc=none smtp.client-ip=103.168.172.149 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bur.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b="Zt4CKgUm"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="jwPnGAPN" Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id 9F5D113814FA; Thu, 6 Mar 2025 19:28:15 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Thu, 06 Mar 2025 19:28:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bur.io; h=cc :content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1741307295; x= 1741393695; bh=4oASwHp+PTuoKLofQVT8u6bofO42RkiWG1gho51M/ow=; b=Z t4CKgUmCoEeVwE5LGEA66RcNMOtVCdrBQ4N/Hh2O1RpvIo9qOcQe6SzMVlsJxpD9 +gz646sAbi4rLXTGWrg9xz1BX3COpP9+p7c/0ii6la46Dolz7ycBNiJsPYmpmzmU Rwc2OBi/s7W6ZUybn7DxScfjx8tzOV/DQI2pKGwbS/RmwItDs1OOpCa68pyep4uT 1Wvr1nFSXTPPTAIIDY0UbOJHqX4ESrhpv8yp7wComzPWsw03WGuAYRocY09LMraJ XknKxwv/ayO3bFFeU4B+qcquguCru0XQkHv5WT53xNhaDG188aiYBU83HvZ8QSvL timKpa6bj2xtxdRfTpB+g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1741307295; x=1741393695; bh=4oASwHp+PTuoKLofQVT8u6bofO42 RkiWG1gho51M/ow=; b=jwPnGAPNH6+VmlD0TdzIAgBsEtVpLGHdY/yyt70HYw+7 nBdw5AaRbuOeokg9XOUwmMiywwUdrZT53Q5vY7AOoB9ZevjA2psZdFlh3XeZz6RA OV0QanuUfYCYhiQOFAxUlWc/c07x9x7JsmHs2wfx9lUg90Ukl28fVG3eYl75pWa9 EKk9jRnK/PJA89hy+MJbD2b7vt61sQfc8Jqv6lambH/BwO/FG1E0OaoK3rwGcfcP 4esQ+8BNZoJ6yZko/p/GWZSLXZL+cC+ETQSgrYhW3mgB26n+8sy8yATXJNcff/W6 WhQizgc0ku4kJbK9xw0MStELfdAjBfSbNmXwDYbo0Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddutdelvddtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehorhhishcuuehurhhkohhv uceosghorhhishessghurhdrihhoqeenucggtffrrghtthgvrhhnpeeiueffuedvieeuje fhheeigfekvedujeejjeffvedvhedtudefiefhkeegueehleenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhrihhssegsuhhrrdhiohdpnh gspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplhhinhhu gidqsghtrhhfshesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehkvghrnh gvlhdqthgvrghmsehfsgdrtghomh X-ME-Proxy: Feedback-ID: i083147f8:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 6 Mar 2025 19:28:15 -0500 (EST) From: Boris Burkov To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 2/5] btrfs: fix bg->bg_list list_del refcount races Date: Thu, 6 Mar 2025 16:29:02 -0800 Message-ID: <8ba94e9758ff9d5278ed86fcff2acdd429d5deee.1741306938.git.boris@bur.io> X-Mailer: git-send-email 2.48.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 If there is any chance at all of racing with mark_bg_unused, better safe than sorry. Otherwise we risk the following interleaving (bg_list refcount in parens) T1 (some random op) T2 (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. Signed-off-by: Boris Burkov Reviewed-by: Qu Wenruo --- fs/btrfs/extent-tree.c | 3 +++ fs/btrfs/transaction.c | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 5de1a1293c93..80560065cc1b 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -2868,7 +2868,10 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) block_group->length, &trimmed); + 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 db8fe291d010..c98a8efd1bea 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c @@ -160,7 +160,9 @@ void btrfs_put_transaction(struct btrfs_transaction *transaction) cache = list_first_entry(&transaction->deleted_bgs, struct btrfs_block_group, bg_list); + 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); } @@ -2096,7 +2098,10 @@ 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); + spin_lock(&fs_info->unused_bgs_lock); list_del_init(&block_group->bg_list); + btrfs_put_block_group(block_group); + spin_unlock(&fs_info->unused_bgs_lock); } } From patchwork Fri Mar 7 00:29:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Burkov X-Patchwork-Id: 14005590 Received: from fout-a6-smtp.messagingengine.com (fout-a6-smtp.messagingengine.com [103.168.172.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8162827701 for ; Fri, 7 Mar 2025 00:28:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.149 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741307300; cv=none; b=MIAN7TcwTt5Vrr+4MiamKioFcsN/s5wzzR5GwqqKg6cWtSJ2A9qzOL0iIgMi1BdFzCgCmJtsGiZVZRmJAM69S1mJQqYaIRPeU6Vgleh3zUlhpgqH4iVgGX9aXLPx7pIbXnI06KpF4PuBPz6UAcnazDsDUcI8GndYFlJ/BWddrv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741307300; c=relaxed/simple; bh=RCx7Enbup5l5Nsg4PjOog7yN62nKFfJ6kLkswDQSgSQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nEtStGL5jFjA+jQJez5IEA7kds56sqJnUsF/MMXkF/ENUHBoxCe2VLCPLBTy+uOhR+GuSSdtHa0Len5GGK1sX+pQOgz72f5TXL2pnOwvD4HANbvIczrtxMAeCNnNBqz3n6DGtYpEX36xBK2OCf0B0TV+uGQqm1Z1SjMWnnmEWFs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io; spf=pass smtp.mailfrom=bur.io; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b=A+4GJd55; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=xvNwesyW; arc=none smtp.client-ip=103.168.172.149 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bur.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b="A+4GJd55"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="xvNwesyW" Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfout.phl.internal (Postfix) with ESMTP id 7552C13826AA; Thu, 6 Mar 2025 19:28:17 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-09.internal (MEProxy); Thu, 06 Mar 2025 19:28:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bur.io; h=cc :content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1741307297; x= 1741393697; bh=czrxCCb2zq5m1SFHoFLIObpPSGpKFuIyKsF9YBmcy30=; b=A +4GJd55prfkHbGGoqPRLVyXgFtmSTXk6JGqTuyFAnHCXDq9XFogpDrCTP1YIuQj6 pNSbG38jLe7bKBqPGJlMUjbCIhn1MZgwFBDMA/MwL2awHQJ1J8asOBQFkcsLp3NE OqsxTabdWy7Ly7V/oA5NjoGxRKeFh5QK8UOqNPKiU8SPMijDGzhlVCInVzQq4VBI KQHZjUY7Jc31KKf9qbSXOheqiXSSn4M8iS8XOhK+yXFpk0YlZdXhULc65uxEJ1G8 AQzcdYKfesy+O6jo02D5a76XNxaAHQd4uItx2nbAojZ7Hni3DTHvFlOW6P5Rt/Q2 TKOJgtfnzujjmg1SWVglw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1741307297; x=1741393697; bh=czrxCCb2zq5m1SFHoFLIObpPSGpK FuIyKsF9YBmcy30=; b=xvNwesyWlxT1fsZbrVkUGq0tu/E7L+SSxhPEskaPpCjd Y81bw8bIrLfkZsxarQFbpV6BcXa1y0y5irlYZfksj8uGStzmRHffkwef+dCp9R6i 1SBVVMgVotS4rViQzRLuJl+yBVAv6eKtovh2pkf7fFCHL2UCRM333nXgo5FCzyNU fS/vJsGb4MuMgQK9UaEM+jpjg2OiFrqe473Nwo79lrHfegyzoBgFQofkhAXbgXBW 5Ql8K9fnMamyIBMPku5FRu1q9LCWTgYOYCbIpeWJWqwcw4UnqbbEqXYNZVRcXQWo E/DPsyMY4Dz4GEl3Gro1rSNn6ePoBLgBrUGjqbtu8w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddutdelvddtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehorhhishcuuehurhhkohhv uceosghorhhishessghurhdrihhoqeenucggtffrrghtthgvrhhnpeeiueffuedvieeuje fhheeigfekvedujeejjeffvedvhedtudefiefhkeegueehleenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhrihhssegsuhhrrdhiohdpnh gspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplhhinhhu gidqsghtrhhfshesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehkvghrnh gvlhdqthgvrghmsehfsgdrtghomh X-ME-Proxy: Feedback-ID: i083147f8:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 6 Mar 2025 19:28:16 -0500 (EST) From: Boris Burkov To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 3/5] btrfs: make discard_workfn block_group ref explicit Date: Thu, 6 Mar 2025 16:29:03 -0800 Message-ID: X-Mailer: git-send-email 2.48.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently, the async discard machinery owns a ref to the block_group when the block_group is queued on a discard list. However, to handle races with discard cancellation and the discard workfn, we have some cute logic to detect that the block_group is *currently* running in the workfn, to protect the workfn's usage amidst cancellation. As far as I can tell, this doesn't have any overt bugs (though finish_discard_pass and remove_from_discard_list racing can have a surprising outcome for the caller of remove_from_discard_list in that it is again added at the end) But it is needlessly complicated to rely on locking and the nullity of discard_ctl->block_group. Simplify this significantly by just taking a refcount while we are in the workfn and uncondtionally drop it in both the remove and workfn paths, regardless of if they race. Signed-off-by: Boris Burkov Reviewed-by: Filipe Manana --- fs/btrfs/discard.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/fs/btrfs/discard.c b/fs/btrfs/discard.c index e815d165cccc..d6eef4bd9e9d 100644 --- a/fs/btrfs/discard.c +++ b/fs/btrfs/discard.c @@ -167,13 +167,7 @@ static bool remove_from_discard_list(struct btrfs_discard_ctl *discard_ctl, block_group->discard_eligible_time = 0; queued = !list_empty(&block_group->discard_list); list_del_init(&block_group->discard_list); - /* - * If the block group is currently running in the discard workfn, we - * don't want to deref it, since it's still being used by the workfn. - * The workfn will notice this case and deref the block group when it is - * finished. - */ - if (queued && !running) + if (queued) btrfs_put_block_group(block_group); spin_unlock(&discard_ctl->lock); @@ -260,9 +254,10 @@ static struct btrfs_block_group *peek_discard_list( block_group->discard_cursor = block_group->start; block_group->discard_state = BTRFS_DISCARD_EXTENTS; } - discard_ctl->block_group = block_group; } if (block_group) { + btrfs_get_block_group(block_group); + discard_ctl->block_group = block_group; *discard_state = block_group->discard_state; *discard_index = block_group->discard_index; } @@ -493,9 +488,20 @@ static void btrfs_discard_workfn(struct work_struct *work) block_group = peek_discard_list(discard_ctl, &discard_state, &discard_index, now); - if (!block_group || !btrfs_run_discard_work(discard_ctl)) + if (!block_group) return; + if (!btrfs_run_discard_work(discard_ctl)) { + spin_lock(&discard_ctl->lock); + btrfs_put_block_group(block_group); + discard_ctl->block_group = NULL; + spin_unlock(&discard_ctl->lock); + return; + } if (now < block_group->discard_eligible_time) { + spin_lock(&discard_ctl->lock); + btrfs_put_block_group(block_group); + discard_ctl->block_group = NULL; + spin_unlock(&discard_ctl->lock); btrfs_discard_schedule_work(discard_ctl, false); return; } @@ -547,15 +553,7 @@ static void btrfs_discard_workfn(struct work_struct *work) spin_lock(&discard_ctl->lock); discard_ctl->prev_discard = trimmed; discard_ctl->prev_discard_time = now; - /* - * If the block group was removed from the discard list while it was - * running in this workfn, then we didn't deref it, since this function - * still owned that reference. But we set the discard_ctl->block_group - * back to NULL, so we can use that condition to know that now we need - * to deref the block_group. - */ - if (discard_ctl->block_group == NULL) - btrfs_put_block_group(block_group); + btrfs_put_block_group(block_group); discard_ctl->block_group = NULL; __btrfs_discard_schedule_work(discard_ctl, now, false); spin_unlock(&discard_ctl->lock); From patchwork Fri Mar 7 00:29:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Burkov X-Patchwork-Id: 14005591 Received: from fhigh-a1-smtp.messagingengine.com (fhigh-a1-smtp.messagingengine.com [103.168.172.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56F7D35968 for ; Fri, 7 Mar 2025 00:28:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741307302; cv=none; b=IRsBTgFocbxZF75140WYDWIXb12/T5syf+h4h8X0/INVsTbYokCa0EWoTIgOo5AsH0uiADp2qlt94niNyZPNrYy2uGq90hPw1Tr+ByXikV6RP3StF0ke0jMALQTD9TVl+NnCRGmMQFYaJAmjtRRnkPPEkr4LEqxu3gRNHl8X0WM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741307302; c=relaxed/simple; bh=5R61vsuKej5on6uPLXupeI9ZwCWM+66F87c6WqwldG0=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b5/ULlHg5l53C97UAs5AeCAYJE0sllqT70/CxSA5PWFafMQjgSFIelhXUFXuu39PstdRTgLZ9hpoly3ta7pxZ0PvGOckUebIgrrI4KvI7id120cowMLVXNdzVZEeo+ANVqR6RFPmoRvSF6qIzNCABKrBLWu+eAa5o+v6Dk5xAWI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io; spf=pass smtp.mailfrom=bur.io; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b=DA/GMYP2; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=doo9EFRE; arc=none smtp.client-ip=103.168.172.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bur.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b="DA/GMYP2"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="doo9EFRE" Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfhigh.phl.internal (Postfix) with ESMTP id 2640E1140161; Thu, 6 Mar 2025 19:28:19 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-09.internal (MEProxy); Thu, 06 Mar 2025 19:28:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bur.io; h=cc :content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1741307299; x= 1741393699; bh=camFk/O0eOjGXJfsCsluThCunGrAlyNqHxROmfEIKxk=; b=D A/GMYP2j+84iLiQkvoB0agfS/Z4bh0HXxtdOzyQ1+rH9wlOyGwKQC73JNYXyck65 S4PuimJp7w+wt8+iYjAUf3HBf0bOHB75cyuriBwyF3Wv2yYCtNFaclWPpSftmHPl 64b28poV0zAmUU7cR98qtst1Wq0mVPCo7ZU6/sD0jR5j2f9fO/aIDgUnul/swMmw BGlQ0ZpPHkLWCa73zpj6TZv0VxFPrbx02zYrm4s8Cr9HTN7yqGb50l3tkJbQ9swK YNHeYm4W+9u9dTj6OSg5VexpmzERBSFhD7mIhNbgmDMekm8z6hlm59K4dH/cLbXY s4Vh7s/Kz2qNu3Q6Cz+dg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1741307299; x=1741393699; bh=camFk/O0eOjGXJfsCsluThCunGrA lyNqHxROmfEIKxk=; b=doo9EFREtx52/I3s26jhMHKrbSP1jijDKKAy7U+PIi8A cgjMmAK5VgOPtBmIznD7vPLZXrEidE7P4S1blrdm9Pn16KWuDUBuFnH1Q2aZLfi6 3o2cp57kueXirtIiXj2InFc3vFsACTcqAX5Li+QbjHHmZwJiqrrhMKUkSfVVtGFz fWWXycaG96Tqm/x+B1ZG/NReY7j8/n8o/IP4hbfzcdjXVMwADzJju8vptnY8viiK +I6v8r8qv7RjH88UFxNzYdGYr6l0kgpDYLuBaQRM3njSkCm8rh59pWV171x/tENi wH9iiQRaEB6VNmujdrgNP1heI04VMTdk8kqdgMSO9g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddutdelvddtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehorhhishcuuehurhhkohhv uceosghorhhishessghurhdrihhoqeenucggtffrrghtthgvrhhnpeeiueffuedvieeuje fhheeigfekvedujeejjeffvedvhedtudefiefhkeegueehleenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhrihhssegsuhhrrdhiohdpnh gspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplhhinhhu gidqsghtrhhfshesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehkvghrnh gvlhdqthgvrghmsehfsgdrtghomh X-ME-Proxy: Feedback-ID: i083147f8:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 6 Mar 2025 19:28:18 -0500 (EST) From: Boris Burkov To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 4/5] btrfs: explicitly ref count block_group on new_bgs list Date: Thu, 6 Mar 2025 16:29:04 -0800 Message-ID: <817581cbc85cfda4c2232fecbfdb6b615b7067ca.1741306938.git.boris@bur.io> X-Mailer: git-send-email 2.48.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 All other users of the bg_list list_head inc the refcount when adding to a list and dec it when deleting from the list. Just for the sake of uniformity and to try to avoid refcounting bugs, do it for this list as well. Signed-off-by: Boris Burkov --- fs/btrfs/block-group.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index 2db1497b58d9..e4071897c9a8 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -2801,6 +2801,7 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) spin_lock(&fs_info->unused_bgs_lock); list_del_init(&block_group->bg_list); clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags); + btrfs_put_block_group(block_group); spin_unlock(&fs_info->unused_bgs_lock); /* @@ -2939,6 +2940,7 @@ struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *tran } #endif + btrfs_get_block_group(cache); list_add_tail(&cache->bg_list, &trans->new_bgs); btrfs_inc_delayed_refs_rsv_bg_inserts(fs_info); From patchwork Fri Mar 7 00:29:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Burkov X-Patchwork-Id: 14005592 Received: from fhigh-a1-smtp.messagingengine.com (fhigh-a1-smtp.messagingengine.com [103.168.172.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7BFA847B for ; Fri, 7 Mar 2025 00:28:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741307303; cv=none; b=ojbL7bXHGON7pBOo/SmQNnRRk8ii/vJEIE2ex6fkYg8BLGcFaKQ1+Am+ZsDsM4CoFzLR7lyT+U2fo49kRQ0vEZmi6xdL9lJeAABYG2ur6xgr4fJEm/wgt7qGnmt/SsWMliuPdNu+iKBZJ2n+za5S/Qh/MawR+OREG/IMjCy3kR0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741307303; c=relaxed/simple; bh=QqUv0P3FUjcPyMWlhy2zBGoumDG49go17hl7pOZgn3w=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D8Fcomg3MNZmXKRBmDr65cp+zCCoJDZC4hp41vfbHjSQXKi9Ew68d2H5aIOOTc6XNF6cnC24Ebvw8zDrH82rNc+6ZCUGU8/VboJRu3OZP6+PHaJvuoqcN8GCsraQFB/lFEo7U77VSgoR6Hq5Xibs/RtnNvwWJVrN84254KjpotE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io; spf=pass smtp.mailfrom=bur.io; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b=jhatErlw; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=rQfsoIUo; arc=none smtp.client-ip=103.168.172.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bur.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bur.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bur.io header.i=@bur.io header.b="jhatErlw"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="rQfsoIUo" Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfhigh.phl.internal (Postfix) with ESMTP id DB08E1140190; Thu, 6 Mar 2025 19:28:20 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-08.internal (MEProxy); Thu, 06 Mar 2025 19:28:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bur.io; h=cc :content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1741307300; x= 1741393700; bh=0Q3wNFG+LyXz/cLYYxCXwoxA5w8FrbdH3LzRAlBftZo=; b=j hatErlw3oxKJryIVS3qVCoMCJPZ1i5SkrEuBeo5/7Z+waRE70ala+rQR/Y+rtZuA MywuBZsucM3/PTDH5EwKnWc/+VeUtG+TYuFPfPbSpSj5Uu+Rvwz4dua8VDr/31aO VMkD1uH7BQyNZMDdfS/U1tVjOo9sIDBgaxuSD5naznlA3wwFAzIIQuhPfXLHiWfo 06CQQrPmgerKSs1exoitFWYWuwnKE7SbXPsFDbEN3XeH342qs3Mz7xB/U26pnv1w MrU4yjGkYPcZquF99kc1xBxFrvQa2zHvr8uU/TE9NSWoCnORY6XI2+dm6j8KYoFN a3Hy6eivpuBxZAeuOPryA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1741307300; x=1741393700; bh=0Q3wNFG+LyXz/cLYYxCXwoxA5w8F rbdH3LzRAlBftZo=; b=rQfsoIUolUBTxSDBKXcNZuAxajYXAg9nHxRuvV1Ay2/O guiGx9TCx+TrBV0Sdm7GEyHxwmPDZ9WSWPp/63nNzd4V8N+EKV5e21pXTGZRKDnW n3594NHqECQaUH3v5rP3CrSRcGcLvUpEgUzVENQGVB3Iyg+iH7uYAijo+GZiaJs8 oLvdTF5VbypQ5cht57C36mxAIvYSNA8G+7JRQsPPYH1mOh4irlyX2qfxCEkn9JN5 szmSWdboU5qm4cH/o0cVVulk17vC0Y1K9MM2dITEZehjom+vvcK/sU9UCpRq39Y6 u5uA9YewI1hXn0sHbppshuZe0Ui0W8oDFu4rIbItGg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddutdelvddtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepuehorhhishcuuehurhhkohhv uceosghorhhishessghurhdrihhoqeenucggtffrrghtthgvrhhnpeeiueffuedvieeuje fhheeigfekvedujeejjeffvedvhedtudefiefhkeegueehleenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhrihhssegsuhhrrdhiohdpnh gspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplhhinhhu gidqsghtrhhfshesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehkvghrnh gvlhdqthgvrghmsehfsgdrtghomh X-ME-Proxy: Feedback-ID: i083147f8:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 6 Mar 2025 19:28:20 -0500 (EST) From: Boris Burkov To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 5/5] btrfs: codify pattern for adding block_group to bg_list Date: Thu, 6 Mar 2025 16:29:05 -0800 Message-ID: <91e9ffb668376e4c67753ca92c8ca8737c07258c.1741306938.git.boris@bur.io> X-Mailer: git-send-email 2.48.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Similar to mark_bg_unused and mark_bg_to_reclaim, we have a few places that use bg_list with refcounting, mostly for retrying failures to reclaim/delete unused. These have custom logic for handling locking and refcounting the bg_list properly, but they actually all want to do the same thing, so pull that logic out into a helper. Unfortunately, mark_bg_unused does still need the NEW flag to avoid prematurely marking stuff unused (even if refcount is fine, we don't want to mess with bg creation), so it cannot use the new helper. Signed-off-by: Boris Burkov Reviewed-by: Filipe Manana --- fs/btrfs/block-group.c | 54 +++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index e4071897c9a8..a570d89ff0c3 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -1455,6 +1455,31 @@ static bool clean_pinned_extents(struct btrfs_trans_handle *trans, return ret == 0; } +/* + * link the block_group to l via bg_list + * + * Use this rather than list_add_tail directly to ensure proper respect + * to locking and refcounting. + * + * @bg: the block_group to link to the list. + * @l: the list to link it to. + * Returns: true if the bg was linked with a refcount bump and false otherwise. + */ +static bool btrfs_link_bg_list(struct btrfs_block_group *bg, struct list_head *l) +{ + struct btrfs_fs_info *fs_info = bg->fs_info; + bool added = false; + + spin_lock(&fs_info->unused_bgs_lock); + if (list_empty(&bg->bg_list)) { + btrfs_get_block_group(bg); + list_add_tail(&bg->bg_list, l); + added = true; + } + spin_unlock(&fs_info->unused_bgs_lock); + return added; +} + /* * Process the unused_bgs list and remove any that don't have any allocated * space inside of them. @@ -1570,8 +1595,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) * drop under the "next" label for the * fs_info->unused_bgs list. */ - btrfs_get_block_group(block_group); - list_add_tail(&block_group->bg_list, &retry_list); + btrfs_link_bg_list(block_group, &retry_list); trace_btrfs_skip_unused_block_group(block_group); spin_unlock(&block_group->lock); @@ -1968,20 +1992,8 @@ void btrfs_reclaim_bgs_work(struct work_struct *work) spin_unlock(&space_info->lock); next: - if (ret && !READ_ONCE(space_info->periodic_reclaim)) { - /* Refcount held by the reclaim_bgs list after splice. */ - spin_lock(&fs_info->unused_bgs_lock); - /* - * This block group might be added to the unused list - * during the above process. Move it back to the - * reclaim list otherwise. - */ - if (list_empty(&bg->bg_list)) { - btrfs_get_block_group(bg); - list_add_tail(&bg->bg_list, &retry_list); - } - spin_unlock(&fs_info->unused_bgs_lock); - } + if (ret && !READ_ONCE(space_info->periodic_reclaim)) + btrfs_link_bg_list(bg, &retry_list); btrfs_put_block_group(bg); mutex_unlock(&fs_info->reclaim_bgs_lock); @@ -2021,13 +2033,8 @@ void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg) { struct btrfs_fs_info *fs_info = bg->fs_info; - spin_lock(&fs_info->unused_bgs_lock); - if (list_empty(&bg->bg_list)) { - btrfs_get_block_group(bg); + if (btrfs_link_bg_list(bg, &fs_info->reclaim_bgs)) trace_btrfs_add_reclaim_block_group(bg); - list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs); - } - spin_unlock(&fs_info->unused_bgs_lock); } static int read_bg_from_eb(struct btrfs_fs_info *fs_info, const struct btrfs_key *key, @@ -2940,8 +2947,7 @@ struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *tran } #endif - btrfs_get_block_group(cache); - list_add_tail(&cache->bg_list, &trans->new_bgs); + btrfs_link_bg_list(cache, &trans->new_bgs); btrfs_inc_delayed_refs_rsv_bg_inserts(fs_info); set_avail_alloc_bits(fs_info, type);