]> 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:11 +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 5d0a95986be187333781eeba04f273f6d69095d7..30f2bbfc7d74632282c02d8bd4cb2bca593dc864 100644 (file)
@@ -103,10 +103,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 acce88f91d41255a345e3d6fb9c32f60771fad3a..c0b6754d4eb5f85df442a5571ee541022b9b5593 100644 (file)
@@ -973,6 +973,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 80dc62c34003da9bb994f5ccf670a5a7d2d33c39..488b8545f4a352509053d500d0efc48aed391287 100644 (file)
@@ -2172,30 +2172,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;
@@ -2204,51 +2196,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 32835d0271be9760c8766d5aa1bd3617d02497cc..021773c1afa778460add3d9957aa7beff66ead84 100644 (file)
@@ -2242,6 +2242,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 dde31c39a1054c9395a841290dabc0c44ede4962..a80345bba7bf4132b595199fd2103beb8cebbf4d 100644 (file)
@@ -3102,7 +3102,7 @@ noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
        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);
@@ -8199,6 +8199,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 d4827023b440f364243cb2f4c4e6cc068c26edc7..ef958d557da3f8dbbdbef9a17ffd2a842e4378b4 100644 (file)
@@ -101,6 +101,8 @@ enum btrfs_raid_types {
 #define BTRFS_DEV_STATE_REPLACE_TGT    (3)
 #define BTRFS_DEV_STATE_FLUSH_SENT     (4)
 #define BTRFS_DEV_STATE_NO_READA       (5)
+/* dev item needs writing at the next commit (allocation hint changed) */
+#define BTRFS_DEV_STATE_TYPE_DIRTY     (6)
 
 /* Special value encoding failure to write primary super block. */
 #define BTRFS_SUPER_PRIMARY_WRITE_ERROR                (INT_MAX / 2)
@@ -792,6 +794,11 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
 int btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
 int btrfs_init_dev_stats(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);