Prevent range tree corruption race by updating dnode_sync()

Switch to incremental range tree processing in dnode_sync() to avoid
unsafe lock dropping during zfs_range_tree_walk(). This also ensures
the free ranges remain visible to dnode_block_freed() throughout the
sync process, preventing potential stale data reads.

This patch:
 - Keeps the range tree attached during processing for visibility.
 - Processes segments one-by-one by restarting from the tree head.
 - Uses zfs_range_tree_clear() to safely handle ranges that may have
   been modified while the lock was dropped.
 - adds ASSERT()s to document that we don't expect dn_free_ranges
   modification outside of sync context.

Reviewed-by: Paul Dagnelie <paul.dagnelie@klarasystems.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Alek Pinchuk <apinchuk@axcient.com>
Issue #18186
Closes #18235
This commit is contained in:
Alek P
2026-03-23 21:34:19 -04:00
committed by Tony Hutter
parent b06caaeec4
commit 7590972f76
4 changed files with 87 additions and 45 deletions
+13
View File
@@ -652,6 +652,19 @@ extern dnode_sums_t dnode_sums;
#endif
/*
* Assert that we are not modifying the range tree for the syncing TXG from
* a non-syncing thread. We verify that either the transaction group is
* strictly newer than the one currently syncing (meaning it's being modified
* in open context), OR the current thread is the sync thread itself. If this
* triggers, it indicates a race where dn_free_ranges is being modified while
* dnode_sync() may be iterating over it.
*/
#define FREE_RANGE_VERIFY(tx, dn) \
ASSERT((tx)->tx_txg > spa_syncing_txg((dn)->dn_objset->os_spa) || \
dmu_objset_pool((dn)->dn_objset)->dp_tx.tx_sync_thread == \
curthread)
#ifdef __cplusplus
}
#endif