Re: [PATCH] btrfs: fix delayed_refs_rsv leak when realloc shrinks an extent
From: Shunbo Shi
Date: Thu Nov 05 2026 - 05:46:38 EST
On Wed, 4 Nov 2026 at 17:41, Filipe Manana wrote:
>
> This last part of the sentence is unintelligible... an opening quote
> without an ending quote, a backslash for no reason, and everything
> after the opening quote doesn't make any sense.
Sorry about that, I will rewrite the whole paragraph in v2 along
the lines you suggested:
... updates existing->num_bytes but neither adjusts the space used
for checksums nor releases excess space from the delayed refs block
reserve.
> We can't update delayed_ref_csums_deletions blindly without looking
> into existing->total_ref_mod.
> This else branch should be:
>
> else if (new_leaves > old_leaves && existing->total_ref_mod < 0)
>
> And I probably missed something more.
> Sashiko is complaining about that I think and more:
>
> https://sashiko.dev/#/patchset/20261104082834.25047-1-shishunbo%40cmss.chinamobile.com
You were right to be suspicious about the grow branch, and the actual
problem is worse than the missing total_ref_mod check: since the head
reaches its total_ref_mod transition in the same call (realloc with
ref_mod == 1 turns -1 into 0), the release below subtracts the new
size from the rsv immediately, while trans->delayed_ref_csum_deletions
is only folded into the rsv size by btrfs_update_delayed_refs_rsv()
after we return. The rsv only holds the reservation for the old size
at that point, so its size underflows and the reserved bytes leak (or
reservations of other pending delayed refs get released prematurely),
as Sashiko's report spells out in detail.
So v2 changes the grow direction to take effect immediately as well:
it adds btrfs_delayed_refs_rsv_add(), which grows
delayed_refs_rsv.size before the transition can release the new size,
keeping both directions symmetric with the release. The outer check
still uses total_ref_mod < 0 (the reservation state before the update
is applied), so the adjustment only runs while the head actually
holds a reservation.
Thanks for the review, v2 follows shortly in this thread.
--
Shunbo Shi