]> git.hungrycats.org Git - linux/commitdiff
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)
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

index 72c1ced888a95f710c72329e177bd19d73cd6d00..f097f9631a8574a5792c90cb72f82a012674879c 100644 (file)
@@ -102,10 +102,10 @@ config BTRFS_ALLOCATOR_HINTS
          They cannot separate the two chunk classes and are outside the
          tiering use case targeted by this feature.
 
+         A hint change applies to new chunks at once and is written to the
+         device item at the next transaction commit (sync to force it).
          Device replace copies the allocation hint to the target device;
          the target's hint cannot be changed until the replace finishes.
-         Hint updates are not serialized with chunk allocation, so a
-         concurrent new chunk may use either the old or the new setting.
 
          You are advised to watch your free space closely with btrfs tools
          instead of relying on df only.
index 903501370cbb1e12e7c5ae8998fc5ef8450b5eeb..062fd114f5088a0b08ff4d04df2179422c1eccb0 100644 (file)
@@ -995,6 +995,9 @@ error:
         * on the target writes type 0 over it.
         */
        tgt_device->type = src_device->type;
+       /* A hint change not yet committed goes to the target's dev item. */
+       if (test_and_clear_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &src_device->dev_state))
+               set_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &tgt_device->dev_state);
        src_device->devid = BTRFS_DEV_REPLACE_DEVID;
        memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
        memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
index 29cc1fba9e9c89457823dff4cbb647fb5aa08456..6e1d0636f9af90088732b9ed5404d3e58558d362 100644 (file)
@@ -2017,30 +2017,22 @@ static ssize_t btrfs_devinfo_type_store(struct kobject *kobj,
                                        const char *buf, size_t len)
 {
        struct btrfs_fs_info *fs_info;
-       struct btrfs_root *root;
        struct btrfs_device *device;
        int ret;
-       struct btrfs_trans_handle *trans;
-
-       u64 type, prev_type;
+       u64 type;
 
        device = container_of(kobj, struct btrfs_device, devid_kobj);
        fs_info = device->fs_info;
        if (!fs_info)
                return -EPERM;
 
-       /*
-        * Changing the type field requires starting a transaction which will cause a NULL dereference in
-        * __reserve_bytes if the file system is not fully open. Thus, return EBUSY if the file system is not fully
-        * initialized.
-        */
-       if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
-               return -EBUSY;
-
-       root = fs_info->chunk_root;
        if (sb_rdonly(fs_info->sb))
                return -EROFS;
 
+       /* The dev item is written by the next transaction commit. */
+       if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
+               return -EBUSY;
+
        ret = kstrtou64(buf, 0, &type);
        if (ret < 0)
                return -EINVAL;
@@ -2049,51 +2041,44 @@ static ssize_t btrfs_devinfo_type_store(struct kobject *kobj,
        if (!btrfs_dev_allocation_hint_valid(type))
                return -EINVAL;
 
+       /* Seed devices are read-only and not on fs_devices->devices. */
+       if (device->fs_devices != fs_info->fs_devices)
+               return -EROFS;
+
        /*
-        * A replace target has no dev item of its own until the replace
-        * finishes and it takes over the source's dev item and type.  After
-        * the finish, the source has the replace devid and no dev item.  The
-        * source's type may change during the replace: the target inherits
-        * it at the finish.
+        * No transaction here.  Device remove drains this attribute while it
+        * holds a transaction handle and device_list_mutex, so a commit (which
+        * waits for that handle) or a join (which can wait for
+        * device_list_mutex when it creates pending block groups) from inside
+        * the store can deadlock against it.  Set the hint and mark the dev
+        * item dirty; btrfs_update_dirty_device_types() writes it at the next
+        * commit, as the label and feature stores do with the super block.
+        *
+        * chunk_mutex serializes with chunk allocation, which reads the hint,
+        * and with the end of a device replace, which moves the hint and the
+        * dirty bit to the target.  A replace target has no dev item of its
+        * own until then.
         */
+       mutex_lock(&fs_info->chunk_mutex);
        if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state) ||
-           device->devid == BTRFS_DEV_REPLACE_DEVID)
+           device->devid == BTRFS_DEV_REPLACE_DEVID) {
+               mutex_unlock(&fs_info->chunk_mutex);
                return -EBUSY;
-
-       trans = btrfs_start_transaction(root, 1);
-       if (IS_ERR(trans))
-               return PTR_ERR(trans);
-
-       prev_type = device->type;
-       device->type = type;
-
-       ret = btrfs_update_device(trans, device);
-
+       }
+       WRITE_ONCE(device->type, type);
        /*
-        * No dev item: the device is being removed, or lost its devid to a
-        * replace that finished after the checks above.  Nothing was changed,
-        * so there is nothing to abort.
+        * The commit reads the type after it sees the bit, without
+        * chunk_mutex: order the store before the bit.
         */
-       if (ret == -ENOENT) {
-               btrfs_end_transaction(trans);
-               ret = -ENODEV;
-               goto abort;
-       }
+       smp_mb__before_atomic();
+       set_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &device->dev_state);
+       mutex_unlock(&fs_info->chunk_mutex);
 
-       if (ret < 0) {
-               btrfs_abort_transaction(trans, ret);
-               btrfs_end_transaction(trans);
-               goto abort;
-       }
-
-       ret = btrfs_commit_transaction(trans);
-       if (ret < 0)
-               goto abort;
+       /* We don't want to do full transaction commit from inside sysfs */
+       set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
+       wake_up_process(fs_info->transaction_kthread);
 
        return len;
-abort:
-       device->type = prev_type;
-       return ret;
 }
 BTRFS_ATTR_RW(devid, type, btrfs_devinfo_type_show, btrfs_devinfo_type_store);
 #endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
index cd7dff5ba59b6d48ec97f4d9b404dbad91068928..4171a5200bedebc93244d6e093f4f787d638d9f9 100644 (file)
@@ -2280,6 +2280,9 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
                        goto lockdep_trans_commit_start_release;
        }
 
+       /* Allocation hints changed through sysfs, see btrfs_devinfo_type_store() */
+       btrfs_update_dirty_device_types(trans);
+
        btrfs_create_pending_block_groups(trans);
 
        if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
index 315a38e0a0b8701714029c8ca1d51832740242b3..cb38ca3b61a65d956dd52a54af3c4cca8a0b1de9 100644 (file)
@@ -3279,7 +3279,7 @@ int btrfs_update_device(struct btrfs_trans_handle *trans, struct btrfs_device *d
        dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
 
        btrfs_set_device_id(leaf, dev_item, device->devid);
-       btrfs_set_device_type(leaf, dev_item, device->type);
+       btrfs_set_device_type(leaf, dev_item, READ_ONCE(device->type));
        btrfs_set_device_io_align(leaf, dev_item, device->io_align);
        btrfs_set_device_io_width(leaf, dev_item, device->io_width);
        btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
@@ -8742,6 +8742,61 @@ int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
        return ret;
 }
 
+#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+/*
+ * Write the dev items whose allocation hint was changed through sysfs (see
+ * btrfs_devinfo_type_store()).  Called at the start of a transaction commit.
+ * A hint that cannot be written stays dirty and is retried at the next
+ * commit: it is not worth aborting the transaction for.
+ */
+void btrfs_update_dirty_device_types(struct btrfs_trans_handle *trans)
+{
+       struct btrfs_fs_info *fs_info = trans->fs_info;
+       struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
+       struct btrfs_device *device;
+       unsigned int dirty = 0;
+       int failed = 0;
+       int ret;
+
+       rcu_read_lock();
+       list_for_each_entry_rcu(device, &fs_devices->devices, dev_list)
+               if (test_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &device->dev_state))
+                       dirty++;
+       rcu_read_unlock();
+       if (!dirty)
+               return;
+
+       /*
+        * Reserve the chunk tree space before device_list_mutex: the
+        * reservation takes chunk_mutex and may allocate a system chunk.
+        */
+       while (dirty--)
+               btrfs_reserve_chunk_metadata(trans, false);
+
+       mutex_lock(&fs_devices->device_list_mutex);
+       list_for_each_entry(device, &fs_devices->devices, dev_list) {
+               if (!test_and_clear_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &device->dev_state))
+                       continue;
+               ret = btrfs_update_device(trans, device);
+               /* Being removed: its dev item is already gone, and the hint with it. */
+               if (ret == -ENOENT)
+                       continue;
+               if (ret) {
+                       set_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &device->dev_state);
+                       failed = ret;
+               }
+       }
+       mutex_unlock(&fs_devices->device_list_mutex);
+
+       btrfs_trans_release_chunk_metadata(trans);
+
+       if (failed)
+               btrfs_warn_rl(fs_info,
+       "failed to write the allocation hint to a dev item: %d, retrying at the next commit",
+                             failed);
+}
+#endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
+
 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
 {
        btrfs_dev_stat_inc(dev, index);
index da7c03871b838694eb44ca4ef338a66a4b6199c2..181b8a4ea14b3dde9ea611e2349498e335b18ff7 100644 (file)
@@ -110,6 +110,8 @@ static_assert(BTRFS_RAID_RAID1C4 == 8);
 
 /* Set when the device item is found in chunk tree, used to catch unexpected registered device. */
 #define BTRFS_DEV_STATE_ITEM_FOUND     (7)
+/* dev item needs writing at the next commit (allocation hint changed) */
+#define BTRFS_DEV_STATE_TYPE_DIRTY     (8)
 
 /* Special value encoding failure to write primary super block. */
 #define BTRFS_SUPER_PRIMARY_WRITE_ERROR                (INT_MAX / 2)
@@ -798,6 +800,11 @@ int btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
 int btrfs_init_writeback_bio_size(struct btrfs_fs_info *fs_info);
 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
+#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+void btrfs_update_dirty_device_types(struct btrfs_trans_handle *trans);
+#else
+static inline void btrfs_update_dirty_device_types(struct btrfs_trans_handle *trans) { }
+#endif
 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev,