mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiaming Zhang <r772577952@gmail.com>
To: David Sterba <dsterba@suse.com>, linux-btrfs@vger.kernel.org
Cc: Chris Mason <mason@kernel.org>, Daniel Vacek <neelx@suse.com>,
	linux-kernel@vger.kernel.org,
	Jiaming Zhang <r772577952@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH] btrfs: free old device name after releasing device_list_mutex
Date: Sun,  6 Dec 2026 14:06:52 +0800	[thread overview]
Message-ID: <20261206060652.4101624-1-r772577952@gmail.com> (raw)

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 <r772577952@gmail.com>
---
 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


                 reply	other threads:[~2026-12-06  6:07 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206060652.4101624-1-r772577952@gmail.com \
    --to=r772577952@gmail.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=neelx@suse.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®