* [PATCH] btrfs: preallocate extent changeset before acquiring extent_io_tree lock
@ 2026-04-17 6:41 tchou
2026-04-17 7:15 ` Qu Wenruo
0 siblings, 1 reply; 2+ messages in thread
From: tchou @ 2026-04-17 6:41 UTC (permalink / raw)
To: linux-btrfs; +Cc: clm, dsterba
In btrfs_clear_extent_bit_changeset(), the extent changeset ulist may need
to allocate new nodes. Currently, this can happen while holding the
extent_io_tree->lock spinlock. Although ulist_prealloc() uses GFP_NOFS to
avoid deadlock with filesystem reclaim, it's better to preallocate before
acquiring the spinlock to:
1. Avoid potential allocation failures while holding the lock
2. Be consistent with set_extent_bit() which already preallocates both
extent state and changeset before the spinlock
3. Reduce lock contention by not doing allocations under the lock
Preallocate the changeset ulist node before acquiring the spinlock,
mirroring the pattern used for extent state preallocation.
---
fs/btrfs/extent-io-tree.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/fs/btrfs/extent-io-tree.c b/fs/btrfs/extent-io-tree.c
index 626702244809..b5ba650cdb55 100644
--- a/fs/btrfs/extent-io-tree.c
+++ b/fs/btrfs/extent-io-tree.c
@@ -663,6 +663,9 @@ int btrfs_clear_extent_bit_changeset(struct extent_io_tree *tree, u64 start, u64
*/
prealloc = alloc_extent_state(mask);
}
+ /* Preallocate the extent changeset ulist node before acquiring spinlock. */
+ if (changeset)
+ extent_changeset_prealloc(changeset, mask);
spin_lock(&tree->lock);
if (cached_state) {
--
2.34.1
Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] btrfs: preallocate extent changeset before acquiring extent_io_tree lock
2026-04-17 6:41 [PATCH] btrfs: preallocate extent changeset before acquiring extent_io_tree lock tchou
@ 2026-04-17 7:15 ` Qu Wenruo
0 siblings, 0 replies; 2+ messages in thread
From: Qu Wenruo @ 2026-04-17 7:15 UTC (permalink / raw)
To: tchou, linux-btrfs; +Cc: clm, dsterba
在 2026/4/17 16:11, tchou 写道:
> In btrfs_clear_extent_bit_changeset(), the extent changeset ulist may need
> to allocate new nodes. Currently, this can happen while holding the
> extent_io_tree->lock spinlock. Although ulist_prealloc() uses GFP_NOFS to
> avoid deadlock with filesystem reclaim, it's better to preallocate before
> acquiring the spinlock to:
>
> 1. Avoid potential allocation failures while holding the lock
> 2. Be consistent with set_extent_bit() which already preallocates both
> extent state and changeset before the spinlock
> 3. Reduce lock contention by not doing allocations under the lock
>
> Preallocate the changeset ulist node before acquiring the spinlock,
> mirroring the pattern used for extent state preallocation.
Missing the SoB line.
Otherwise looks good to me.
Thanks,
Qu
> ---
> fs/btrfs/extent-io-tree.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/fs/btrfs/extent-io-tree.c b/fs/btrfs/extent-io-tree.c
> index 626702244809..b5ba650cdb55 100644
> --- a/fs/btrfs/extent-io-tree.c
> +++ b/fs/btrfs/extent-io-tree.c
> @@ -663,6 +663,9 @@ int btrfs_clear_extent_bit_changeset(struct extent_io_tree *tree, u64 start, u64
> */
> prealloc = alloc_extent_state(mask);
> }
> + /* Preallocate the extent changeset ulist node before acquiring spinlock. */
> + if (changeset)
> + extent_changeset_prealloc(changeset, mask);
>
> spin_lock(&tree->lock);
> if (cached_state) {
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-04-17 7:16 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-17 6:41 [PATCH] btrfs: preallocate extent changeset before acquiring extent_io_tree lock tchou
2026-04-17 7:15 ` Qu Wenruo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox