When a device scan updates a Btrfs device's path, device_list_add() replaces device->name under RCU while holding device_list_mutex. Since commit e8d58aef119a ("btrfs: open code RCU for device name"), it frees the replaced name string with kfree_rcu_mightsleep() before dropping the mutex, assuming that sleeping there is safe: rcu_assign_pointer(device->name, name); kfree_rcu_mightsleep(old_name); However, device_list_mutex cannot be held across GFP_KERNEL allocations that may enter memory reclaim (fs_reclaim). During reclaim, evicting an unlinked Btrfs inode in evict_refill_and_join() joins a transaction and takes the superblock freeze lock (sb_internal), while write_all_supers() locks device_list_mutex under sb_internal to write superblocks. Unlike two-argument kfree_rcu(), single-argument kfree_rcu_mightsleep() passes a NULL rcu_head to kvfree_call_rcu(), allowing a tracking page to be allocated in add_ptr_to_bulk_krc_lock(). When kfree_rcu_sheaf() declines the pointer (e.g., KFENCE or remote-NUMA) and the bulk-page cache is empty, it calls __get_free_page(GFP_KERNEL | ...). This creates a deadlock cycle and triggers a lockdep circular locking warning: a device scan holding device_list_mutex can enter direct reclaim and block evicting an unlinked Btrfs inode on a filesystem freeze or committing transaction, while an fsync() or transaction commit blocks waiting for device_list_mutex. Triggering this requires CAP_SYS_ADMIN to update a mounted device path via BTRFS_IOC_SCAN_DEV or BTRFS_IOC_DEVICES_READY (for example, udev renaming /dev/root after boot without an initramfs) alongside memory reclaim evicting an unlinked Btrfs inode. Move kfree_rcu_mightsleep(old_name) after releasing device_list_mutex in device_list_add(), initializing old_name to NULL at function scope since kfree_rcu_mightsleep() ignores NULL. Because old_name is already detached from device->name, deferring it needs no lock and also avoids holding device_list_mutex across a fallback synchronize_rcu() wait. Fixes: e8d58aef119a ("btrfs: open code RCU for device name") Closes: https://lore.kernel.org/all/CANypQFYW2+Bph9unThdrYXsfHBF_-pQZjB4ry+_mNS8GGAX7Og@mail.gmail.com/ Cc: stable@vger.kernel.org Assisted-by: Antigravity:Gemini Signed-off-by: Jiaming Zhang --- fs/btrfs/volumes.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index d297acfcf2fa..a78001fdf273 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -791,6 +791,7 @@ static noinline struct btrfs_device *device_list_add(const char *path, struct btrfs_device *device; struct btrfs_fs_devices *fs_devices = NULL; const char *name; + const char *old_name = NULL; u64 found_transid = btrfs_super_generation(disk_super); u64 devid = btrfs_stack_device_id(&disk_super->dev_item); dev_t path_devt; @@ -899,8 +900,6 @@ static noinline struct btrfs_device *device_list_add(const char *path, } else if (!device->name || device->devt != path_devt || should_rename_device(device)) { - const char *old_name; - /* * When FS is already mounted. * 1. If you are here and if the device->name is NULL that @@ -982,7 +981,6 @@ static noinline struct btrfs_device *device_list_add(const char *path, old_name = rcu_dereference(device->name); rcu_read_unlock(); rcu_assign_pointer(device->name, name); - kfree_rcu_mightsleep(old_name); if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { fs_devices->missing_devices--; @@ -1006,6 +1004,7 @@ static noinline struct btrfs_device *device_list_add(const char *path, fs_devices->total_devices = btrfs_super_num_devices(disk_super); mutex_unlock(&fs_devices->device_list_mutex); + kfree_rcu_mightsleep(old_name); return device; } base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.43.0