]> git.hungrycats.org Git - linux/commit
btrfs: allocator hints: don't run a transaction from the type store
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 4 Oct 2026 14:55:00 +0000 (10:55 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 12 Nov 2026 15:26:17 +0000 (10:26 -0500)
commitb08ec73e721bfc56c3c64e2bc22c26054ec8af3a
tree7d022f10adfe0a53fbb93b3732ba3386a76cd8db
parent6d1f1f47cfdcec8654f7572361fb3d076e061ab0
btrfs: allocator hints: don't run a transaction from the type store

btrfs_devinfo_type_store() starts and commits a transaction while kernfs
holds the attribute active.  btrfs_rm_device() removes the device's
sysfs directory, draining that attribute, while it holds its own
transaction handle and fs_devices->device_list_mutex.  A type write
racing a device remove can deadlock:

  rm_device: transaction handle, device_list_mutex -> drain devinfo/<id>/type
  type store: devinfo/<id>/type active -> commit waits for rm_device's handle

Lockdep reports the cycle (kn->active -> btrfs_trans_num_writers ->
btrfs_trans_num_extwriters -> device_list_mutex) with a type writer
looping during 'btrfs device remove'.  Joining the transaction without
committing does not help: a chunk allocation during the dev item update
makes the end of the transaction take device_list_mutex.

Do what the label and feature stores do and keep transactions out of
sysfs.  The store sets device->type under chunk_mutex (which also orders
it against chunk allocation and against the end of a device replace),
marks the device BTRFS_DEV_STATE_TYPE_DIRTY and requests a commit.
btrfs_update_dirty_device_types() writes the dirty dev items at the start
of the next commit, under device_list_mutex, after the transaction
handle, the same order as btrfs_rm_device() and btrfs_run_dev_stats(), with the
chunk tree space reserved beforehand (btrfs_reserve_chunk_metadata(), as
btrfs_grow_device() does; it takes chunk_mutex, so before the mutex).  A
dev item already deleted by a device remove is skipped.  A dev item that
cannot be written stays dirty, with a rate-limited warning, and is retried
at the next commit: a hint is not worth aborting the transaction for.  The
hint takes effect for new chunks at once and reaches the disk at the next
commit, sync or unmount.

Taking chunk_mutex in the store also closes the race at the end of a
device replace: the finish moves the hint and the dirty bit to the
target under the same mutex, so a hint change on the source either
reaches the target or, once the source has the replace devid, returns
-EBUSY.  The replace target (devid 0, no dev item) still returns -EBUSY.
Also check sb_rdonly() before BTRFS_FS_OPEN, so a read-only mount
reports -EROFS rather than -EBUSY, and refuse seed devices, which are
read-only and not written by the commit-time update.

The store orders the new type before the dirty bit
(smp_mb__before_atomic(); the commit's test_and_clear_bit() is the other
half), and btrfs_update_device() reads the type with READ_ONCE(), so the
commit cannot write the old type and clear the bit on a weakly ordered
machine.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/Kconfig
fs/btrfs/dev-replace.c
fs/btrfs/sysfs.c
fs/btrfs/transaction.c
fs/btrfs/volumes.c
fs/btrfs/volumes.h