Device name and RCU string
David Sterba
maintainerv2 cover Device name and RCU string
v2:
- drop patch adding RCU protection around update_dev_time(), there's a sleeping allocation inside; after revisiting the RCU requirements, I don't think it's needed and we can access the raw string (the same way it was done with rcu_string), the time update is called from scratch superblocks and after the ->dev_list hook is removed, so there's no chance it can be reached from device_list_add
After recent simplifications of the RCU usage in messages, this patchset implements the RCU protection directly without the RCU string so this API can be removed completely as we don't have nor plan anything else to use it for.
David Sterba (2):
btrfs: open code RCU for device name
btrfs: remove struct rcu_string
fs/btrfs/rcu-string.h | 40 ---------------------------------------- fs/btrfs/volumes.c | 29 +++++++++++++++++------------ fs/btrfs/volumes.h | 6 +++--- fs/btrfs/zoned.c | 23 +++++++++++------------ 4 files changed, 31 insertions(+), 67 deletions(-) delete mode 100644 fs/btrfs/rcu-string.h
Signature · 1 line
2.49.0
David Sterba
maintainerPATCH v2 1/2 btrfs: open code RCU for device name
The RCU protected string is only used for a device name, and RCU is used so we can print the name and eventually synchronize against the rare device rename in device_list_add().
We don't need the whole API just for that. Open code all the helpers and access to the string itself.
Notable change is in device_list_add() when the device name is changed, which is the only place that can actually happen at the same time as message prints using the device name under RCU read lock.
Previously there was kfree_rcu() which used the embedded rcu_head to delay freeing the object depending on the RCU mechanism. Now there's kfree_rcu_mightsleep() which does not need the rcu_head and waits for the grace period.
Sleeping is safe in this context and as this is a rare event it won't interfere with the rest as it's holding the device_list_mutex.
Straightforward changes:
- rcu_string_strdup -> kstrdup
- rcu_str_deref -> rcu_dereference
- drop ->str from safe contexts
Historical notes:
Introduced in 606686eeac45 ("Btrfs: use rcu to protect device->name") with a vague reference of the potential problem described in
https://lore.kernel.org/all/[email protected]/ .
The RCU protection looks like the easiest and most lightweight way of protecting the rare event of device rename racing device_list_add() with a random printk() that uses the device name.
Alternatives: a spin lock would require to protect the printk anyway, a fixed buffer for the name would be eventually wrong in case the new name is overwritten when being printed, an array switching pointers and cleaning them up eventually resembles RCU too much.
The cleanups up to this patch should hide special case of RCU to the minimum that only the name needs rcu_dereference(), which can be further cleaned up to use btrfs_dev_name().
--- fs/btrfs/volumes.c | 28 +++++++++++++++++----------- fs/btrfs/volumes.h | 5 +++-- fs/btrfs/zoned.c | 22 +++++++++++----------- 3 files changed, 31 insertions(+), 24 deletions(-)
fs/btrfs/volumes.c +17 -11
@@ -658,7 +658,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,if (!device->name)return -EINVAL ; - ret =btrfs_get_bdev_and_sb (device->name->str, flags, holder,1 , + ret =btrfs_get_bdev_and_sb (device->name, flags, holder,1 , &bdev_file, &disk_super);if (ret)return ret;
@@ -702,7 +702,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,if (device->devt != device->bdev->bd_dev) {btrfs_warn (NULL ,"device %s maj:min changed from %d:%d to %d:%d" , - device->name->str,MAJOR (device->devt), + device->name,MAJOR (device->devt),MINOR (device->devt),MAJOR (device->bdev->bd_dev),MINOR (device->bdev->bd_dev));
@@ -750,7 +750,7 @@ static bool is_same_device(struct btrfs_device *device, const char *new_path) goto out;rcu_read_lock (); - ret =strscpy (old_path,rcu_str_deref (device->name),PATH_MAX ); + ret =strscpy (old_path,rcu_dereference (device->name),PATH_MAX );rcu_read_unlock ();if (ret <0 ) goto out;
@@ -783,7 +783,7 @@ static noinline struct btrfs_device *device_list_add(const char *path, {struct btrfs_device *device;struct btrfs_fs_devices *fs_devices =NULL ; -struct rcu_string *name; +const char *name;u64 found_transid =btrfs_super_generation (disk_super);u64 devid =btrfs_stack_device_id (&disk_super->dev_item);dev_t path_devt;
@@ -891,6 +891,8 @@ static noinline struct btrfs_device *device_list_add(const char *path, current->comm, task_pid_nr(current)); }else if (!device->name || !is_same_device (device, path)) { +const char *old_name; +/* * When FS is already mounted. * 1. If you are here and if the device->name is NULL that
@@ -958,13 +960,17 @@ static noinline struct btrfs_device *device_list_add(const char *path, task_pid_nr(current)); } - name =rcu_string_strdup (path,GFP_NOFS ); + name =kstrdup (path,GFP_NOFS );if (!name) {mutex_unlock (&fs_devices->device_list_mutex);return ERR_PTR (-ENOMEM ); } -kfree_rcu (device->name, rcu); +rcu_read_lock (); + 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--;clear_bit (BTRFS_DEV_STATE_MISSING , &device->dev_state);
@@ -1013,7 +1019,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)* uuid mutex so nothing we touch in here is going to disappear. */ if (orig_dev->name) - dev_path = orig_dev->name->str; + dev_path = orig_dev->name; device =btrfs_alloc_device (NULL , &orig_dev->devid, orig_dev->uuid, dev_path);
@@ -1415,7 +1421,7 @@ static bool btrfs_skip_registration(struct btrfs_super_block *disk_super,list_for_each_entry (device, &fs_devices->devices, dev_list) {if (device->bdev && (device->bdev->bd_dev == devt) && -strcmp (device->name->str, path) !=0 ) { +strcmp (device->name, path) !=0 ) {mutex_unlock (&fs_devices->device_list_mutex);/* Do not skip registration. */
@@ -2165,7 +2171,7 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct btrfs_devicbtrfs_kobject_uevent (bdev,KOBJ_CHANGE );/* Update ctime/mtime for device path for libblkid */ -update_dev_time (device->name->str); +update_dev_time (device->name); } int btrfs_rm_device(struct btrfs_fs_info *fs_info,
@@ -6924,9 +6930,9 @@ struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,generate_random_uuid (dev->uuid);if (path) { -struct rcu_string *name; +const char *name; - name =rcu_string_strdup (path,GFP_KERNEL ); + name =kstrdup (path,GFP_KERNEL );if (!name) {btrfs_free_device (dev);return ERR_PTR (-ENOMEM );
fs/btrfs/volumes.h +3 -2
@@ -114,7 +114,8 @@ struct btrfs_device {struct btrfs_fs_devices *fs_devices;struct btrfs_fs_info *fs_info; -struct rcu_string __rcu *name; +/* Device path or NULL if missing. */ +const char __rcu *name;u64 generation;
@@ -846,7 +847,7 @@ static inline const char *btrfs_dev_name(const struct btrfs_device *device)if (!device ||test_bit (BTRFS_DEV_STATE_MISSING , &device->dev_state))return "<missing disk>" ;else -return rcu_str_deref (device->name); +return rcu_dereference (device->name); }static inline void btrfs_warn_unknown_chunk_allocation (enum btrfs_chunk_allocation_policy pol)
fs/btrfs/zoned.c +11 -11
@@ -266,7 +266,7 @@ static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,if (ret <0 ) {btrfs_err (device->fs_info,"zoned: failed to read zone %llu on %s (devid %llu)" , - pos,rcu_str_deref (device->name), + pos,rcu_dereference (device->name), device->devid);return ret; }
@@ -398,14 +398,14 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)if (zone_info->zone_size >BTRFS_MAX_ZONE_SIZE ) {btrfs_err (fs_info,"zoned: %s: zone size %llu larger than supported maximum %llu" , -rcu_str_deref (device->name), +rcu_dereference (device->name), zone_info->zone_size,BTRFS_MAX_ZONE_SIZE ); ret = -EINVAL ; goto out; }else if (zone_info->zone_size <BTRFS_MIN_ZONE_SIZE ) {btrfs_err (fs_info,"zoned: %s: zone size %llu smaller than supported minimum %u" , -rcu_str_deref (device->name), +rcu_dereference (device->name), zone_info->zone_size,BTRFS_MIN_ZONE_SIZE ); ret = -EINVAL ; goto out;
@@ -421,7 +421,7 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)if (max_active_zones && max_active_zones <BTRFS_MIN_ACTIVE_ZONES ) {btrfs_err (fs_info,"zoned: %s: max active zones %u is too small, need at least %u active zones" , -rcu_str_deref (device->name), max_active_zones, +rcu_dereference (device->name), max_active_zones,BTRFS_MIN_ACTIVE_ZONES ); ret = -EINVAL ; goto out;
@@ -463,7 +463,7 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)if (!zone_info->zone_cache) {btrfs_err (device->fs_info,"zoned: failed to allocate zone cache for %s" , -rcu_str_deref (device->name)); +rcu_dereference (device->name)); ret = -ENOMEM ; goto out; }
@@ -500,7 +500,7 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)if (nreported != zone_info->nr_zones) {btrfs_err (device->fs_info,"inconsistent number of zones on %s (%u/%u)" , -rcu_str_deref (device->name), nreported, +rcu_dereference (device->name), nreported, zone_info->nr_zones); ret = -EIO ; goto out;
@@ -510,7 +510,7 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)if (nactive > max_active_zones) {btrfs_err (device->fs_info,"zoned: %u active zones on %s exceeds max_active_zones %u" , - nactive,rcu_str_deref (device->name), + nactive,rcu_dereference (device->name), max_active_zones); ret = -EIO ; goto out;
@@ -578,7 +578,7 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)btrfs_info (fs_info,"%s block device %s, %u %szones of %llu bytes" , - model,rcu_str_deref (device->name), zone_info->nr_zones, + model,rcu_dereference (device->name), zone_info->nr_zones, emulated, zone_info->zone_size);return 0 ;
@@ -1186,7 +1186,7 @@ int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)btrfs_warn ( device->fs_info,"zoned: resetting device %s (devid %llu) zone %llu for allocation" , -rcu_str_deref (device->name), device->devid, pos >> shift); +rcu_dereference (device->name), device->devid, pos >> shift);WARN_ON_ONCE (1 ); ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
@@ -1348,7 +1348,7 @@ static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,if (zone.type ==BLK_ZONE_TYPE_CONVENTIONAL ) {btrfs_err (fs_info,"zoned: unexpected conventional zone %llu on device %s (devid %llu)" , - zone.start <<SECTOR_SHIFT ,rcu_str_deref (device->name), + zone.start <<SECTOR_SHIFT ,rcu_dereference (device->name), device->devid);up_read (&dev_replace->rwsem);return -EIO ;
@@ -1362,7 +1362,7 @@ static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,btrfs_err (fs_info,"zoned: offline/readonly zone %llu on device %s (devid %llu)" , (info->physical >> device->zone_info->zone_size_shift), -rcu_str_deref (device->name), device->devid); +rcu_dereference (device->name), device->devid); info->alloc_offset =WP_MISSING_DEV ;break ;case BLK_ZONE_COND_EMPTY :
Signature · 1 line
2.49.0
David Sterba
maintainerPATCH v2 2/2 btrfs: remove struct rcu_string
In reply to David SterbaThe only use for device name has been removed so we can kill the RCU string API.
--- fs/btrfs/rcu-string.h | 40 ---------------------------------------- fs/btrfs/volumes.c | 1 - fs/btrfs/volumes.h | 1 - fs/btrfs/zoned.c | 1 - 4 files changed, 43 deletions(-) delete mode 100644 fs/btrfs/rcu-string.h
fs/btrfs/rcu-string.h deleted +0 -40
@@ -1,40 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -/* -* Copyright (C) 2012 Red Hat. All rights reserved. -*/ - -#ifndef BTRFS_RCU_STRING_H -#define BTRFS_RCU_STRING_H - -#include <linux/types.h> -#include <linux/string.h> -#include <linux/slab.h> -#include <linux/rcupdate.h> -#include <linux/printk.h> - -struct rcu_string { -struct rcu_head rcu; -char str[]; -}; - -static inline struct rcu_string *rcu_string_strdup (const char *src,gfp_t mask) -{ -size_t len =strlen (src) +1 ; -struct rcu_string *ret =kzalloc (sizeof (struct rcu_string ) + - (len *sizeof (char )), mask); -if (!ret) -return ret; -/* Warn if the source got unexpectedly truncated. */ -if (WARN_ON (strscpy (ret->str, src, len) <0 )) { -kfree (ret); -return NULL ; - } -return ret; -} - -#define rcu_str_deref (rcu_str) ({ \ - struct rcu_string *__str = rcu_dereference(rcu_str); \ - __str->str; \ -}) - -#endif
fs/btrfs/volumes.c +0 -1
@@ -18,7 +18,6 @@#include "transaction.h" #include "volumes.h" #include "raid56.h" -#include "rcu-string.h" #include "dev-replace.h" #include "sysfs.h" #include "tree-checker.h"
fs/btrfs/volumes.h +0 -1
@@ -21,7 +21,6 @@#include <uapi/linux/btrfs.h> #include <uapi/linux/btrfs_tree.h> #include "messages.h" -#include "rcu-string.h" #include "extent-io-tree.h" struct block_device ;
fs/btrfs/zoned.c +0 -1
@@ -9,7 +9,6 @@#include "ctree.h" #include "volumes.h" #include "zoned.h" -#include "rcu-string.h" #include "disk-io.h" #include "block-group.h" #include "dev-replace.h"
Signature · 1 line
2.49.0
Quoted 8 lines · David Sterba
v2:
- drop patch adding RCU protection aroundupdate_dev_time(), there's a sleeping allocation inside; after revisiting the RCU requirements, I don't think it's needed and we can access the raw string (the same way it was done withrcu_string), the time update is called from scratch superblocks and after the ->dev_list hook is removed, so there's no chance it can be reached fromdevice_list_add
Right, I missed that.
Quoted 8 lines
After recent simplifications of the RCU usage in messages, this patchset implements the RCU protection directly without the RCU string so this API can be removed completely as we don't have nor plan anything else to use it for.
David Sterba (2):
btrfs: open code RCU for device name
btrfs: remove structrcu_string
The rest still looks good.
Quoted 11 lines
fs/btrfs/rcu-string.h | 40 ---------------------------------------- fs/btrfs/volumes.c | 29 +++++++++++++++++------------ fs/btrfs/volumes.h | 6 +++--- fs/btrfs/zoned.c | 23 +++++++++++------------ 4 files changed, 31 insertions(+), 67 deletions(-) delete mode 100644 fs/btrfs/rcu-string.h--
2.49.0
kernel test robot
build botRe: [PATCH v2 1/2] btrfs: open code RCU for device name
In reply to David Sterbakernel test robot noticed the following build warnings:
Hi David,
kernel test robot noticed the following build warnings:
[auto build test WARNING on kdave/for-next]
[also build test WARNING on next-20250626]
[cannot apply to linus/master v6.16-rc3]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/David-Sterba/btrfs-open-code-RCU-for-device-name/20250625-214017 base: https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git for-next patch link: https://lore.kernel.org/r/1e539dfd73debc86ddc7c1b1716f86ace14d51aa.1750858539.git.dsterba%40suse.com patch subject: [PATCH v2 1/2] btrfs: open code RCU for device name config: powerpc64-randconfig-r133-20250627 (https://download.01.org/0day-ci/archive/20250627/[email protected]/config) compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project e04c938cc08a90ae60440ce22d072ebc69d67ee8) reproduce: (https://download.01.org/0day-ci/archive/20250627/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> fs/btrfs/volumes.c:407:21: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const *objp @@ got char const [noderef] __rcu *name @@ fs/btrfs/volumes.c:407:21: sparse: expected void const *objp fs/btrfs/volumes.c:407:21: sparse: got char const [noderef] __rcu *name >> fs/btrfs/volumes.c:661:43: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected char const *device_path @@ got char const [noderef] __rcu *name @@ fs/btrfs/volumes.c:661:43: sparse: expected char const *device_path fs/btrfs/volumes.c:661:43: sparse: got char const [noderef] __rcu *name >> fs/btrfs/volumes.c:1022:34: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected char const *dev_path @@ got char const [noderef] __rcu *name @@ fs/btrfs/volumes.c:1022:34: sparse: expected char const *dev_path fs/btrfs/volumes.c:1022:34: sparse: got char const [noderef] __rcu *name >> fs/btrfs/volumes.c:1424:42: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected char const * @@ got char const [noderef] __rcu *name @@
fs/btrfs/volumes.c:1424:42: sparse: expected char const *
fs/btrfs/volumes.c:1424:42: sparse: got char const [noderef] __rcu *name
fs/btrfs/volumes.c:2174:31: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected char const *device_path @@ got char const [noderef] __rcu *name @@
fs/btrfs/volumes.c:2174:31: sparse: expected char const *device_path
fs/btrfs/volumes.c:2174:31: sparse: got char const [noderef] __rcu *name
vim +407 fs/btrfs/volumes.c
2208a378f35fea7 Ilya Dryomov 2013-08-12 402
f2db4d5cb457b7d Filipe Manana 2023-04-26 403 static void btrfs_free_device(struct btrfs_device *device)
48dae9cf3f95acc David Sterba 2017-10-30 404 {
bbbf7243d62d8be Nikolay Borisov 2019-03-25 405 WARN_ON(!list_empty(&device->post_commit_list));
9fc9a5a2dc4bd09 David Sterba 2025-06-09 406 /* No need to call kfree_rcu(), nothing is reading the device name. */
9fc9a5a2dc4bd09 David Sterba 2025-06-09 @407 kfree(device->name);
e965835c9829b11 Filipe Manana 2025-04-04 408 btrfs_extent_io_tree_release(&device->alloc_state);
5b316468983dfa9 Naohiro Aota 2020-11-10 409 btrfs_destroy_dev_zone_info(device);
48dae9cf3f95acc David Sterba 2017-10-30 410 kfree(device);
48dae9cf3f95acc David Sterba 2017-10-30 411 }
48dae9cf3f95acc David Sterba 2017-10-30 412 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v2 1/2] btrfs: open code RCU for device name
Quoted 41 lines · kernel test robot
Hi David,
kernel test robot noticed the following build warnings:
[auto build test WARNING on kdave/for-next]
[also build test WARNING on next-20250626]
[cannot apply to linus/master v6.16-rc3]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]url: https://github.com/intel-lab-lkp/linux/commits/David-Sterba/btrfs-open-code-RCU-for-device-name/20250625-214017 base: https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git for-next patch link: https://lore.kernel.org/r/1e539dfd73debc86ddc7c1b1716f86ace14d51aa.1750858539.git.dsterba%40suse.com patch subject: [PATCH v2 1/2] btrfs: open code RCU for device name config: powerpc64-randconfig-r133-20250627 (https://download.01.org/0day-ci/archive/20250627/[email protected]/config) compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project e04c938cc08a90ae60440ce22d072ebc69d67ee8) reproduce: (https://download.01.org/0day-ci/archive/20250627/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/sparse warnings: (new ones prefixed by >>)
Quoted 1 line
Quoted 1 line
fs/btrfs/volumes.c:407:21: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const *objp @@ got char const [noderef]__rcu*name @@
fs/btrfs/volumes.c:407:21: sparse: expected void const *objp
fs/btrfs/volumes.c:407:21: sparse: got char const [noderef]__rcu*nameQuoted 1 line
Quoted 1 line
fs/btrfs/volumes.c:661:43: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected char const*device_path@@ got char const [noderef]__rcu*name @@
fs/btrfs/volumes.c:661:43: sparse: expected char const*device_path
fs/btrfs/volumes.c:661:43: sparse: got char const [noderef]__rcu*nameQuoted 1 line
Quoted 1 line
fs/btrfs/volumes.c:1022:34: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected char const*dev_path@@ got char const [noderef]__rcu*name @@
fs/btrfs/volumes.c:1022:34: sparse: expected char const*dev_path
fs/btrfs/volumes.c:1022:34: sparse: got char const [noderef]__rcu*nameQuoted 1 line
Quoted 1 line
fs/btrfs/volumes.c:1424:42: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected char const * @@ got char const [noderef]__rcu*name @@
fs/btrfs/volumes.c:1424:42: sparse: expected char const *
fs/btrfs/volumes.c:1424:42: sparse: got char const [noderef]__rcu*name
fs/btrfs/volumes.c:2174:31: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected char const*device_path@@ got char const [noderef]__rcu*name @@
fs/btrfs/volumes.c:2174:31: sparse: expected char const*device_path
fs/btrfs/volumes.c:2174:31: sparse: got char const [noderef]__rcu*name
Quoted 8 lines
vim +407
fs/btrfs/volumes.c2208a378f35fea7 Ilya Dryomov 2013-08-12 402 f2db4d5cb457b7d Filipe Manana 2023-04-26 403 static void btrfs_free_device(struct btrfs_device *device) 48dae9cf3f95acc David Sterba 2017-10-30 404 { bbbf7243d62d8be Nikolay Borisov 2019-03-25 405 WARN_ON(!list_empty(&device->post_commit_list)); 9fc9a5a2dc4bd09 David Sterba 2025-06-09 406 /* No need to call kfree_rcu(), nothing is reading the device name. */
9fc9a5a2dc4bd09David Sterba 2025-06-09 @407kfree(device->name);
All the warnings are for places where raw ->name is used because we know it's safe given the context. A rcu_dereference would work and is not necessary but will trigger the warnings. OTOH if there isn't the __rcu annotation we can't get any checks where it would be desired.
Re: [PATCH v2 1/2] btrfs: open code RCU for device name
In reply to David SterbaHi,
Quoted 35 lines
The RCU protected string is only used for a device name, and RCU is used so we can print the name and eventually synchronize against the rare device rename in
device_list_add().We don't need the whole API just for that. Open code all the helpers and access to the string itself.
Notable change is in
device_list_add()when the device name is changed, which is the only place that can actually happen at the same time as message prints using the device name under RCU read lock.Previously there was
kfree_rcu()which used the embeddedrcu_headto delay freeing the object depending on the RCU mechanism. Now there'skfree_rcu_mightsleep()which does not need thercu_headand waits for the grace period.Sleeping is safe in this context and as this is a rare event it won't interfere with the rest as it's holding the
device_list_mutex.Straightforward changes:
-
rcu_string_strdup-> kstrdup
-rcu_str_deref->rcu_dereference
- drop ->str from safe contextsHistorical notes:
Introduced in
606686eeac45("Btrfs: use rcu to protectdevice->name") with a vague reference of the potential problem described in
https://lore.kernel.org/all/[email protected]/ .The RCU protection looks like the easiest and most lightweight way of protecting the rare event of device rename racing
device_list_add()with a randomprintk()that uses the device name.
Quoted 9 lines
Alternatives: a spin lock would require to protect the printk anyway, a fixed buffer for the name would be eventually wrong in case the new name is overwritten when being printed, an array switching pointers and cleaning them up eventually resembles RCU too much.
The cleanups up to this patch should hide special case of RCU to the minimum that only the name needs
rcu_dereference(), which can be further cleaned up to usebtrfs_dev_name().
There is still rcu warning when 'make W=1 C=1'
/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: warning: incorrect type in argument 1 (different address spaces)/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: expected void const *objp/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: got char const [noderef] __rcu *name
static void btrfs_free_device(struct btrfs_device *device)
{
WARN_ON(!list_empty(&device->post_commit_list));
/* No need to call kfree_rcu(), nothing is reading the device name. */L405: kfree(device->name);
do we need rcu_dereference here?
fs/btrfs/volumes.c +1 -1
@@ -402,7 +402,7 @@ static void btrfs_free_device(struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));/* No need to call kfree_rcu(), nothing is reading the device name. */ -kfree (device->name); +kfree (rcu_dereference (device->name));
Best Regards
Wang Yugui ([email protected])
2025/06/30
Re: [PATCH v2 1/2] btrfs: open code RCU for device name
Quoted 63 lines · Wang Yugui
Hi,
Quoted 35 lines
The RCU protected string is only used for a device name, and RCU is used so we can print the name and eventually synchronize against the rare device rename in
device_list_add().We don't need the whole API just for that. Open code all the helpers and access to the string itself.
Notable change is in
device_list_add()when the device name is changed, which is the only place that can actually happen at the same time as message prints using the device name under RCU read lock.Previously there was
kfree_rcu()which used the embeddedrcu_headto delay freeing the object depending on the RCU mechanism. Now there'skfree_rcu_mightsleep()which does not need thercu_headand waits for the grace period.Sleeping is safe in this context and as this is a rare event it won't interfere with the rest as it's holding the
device_list_mutex.Straightforward changes:
-
rcu_string_strdup-> kstrdup
-rcu_str_deref->rcu_dereference
- drop ->str from safe contextsHistorical notes:
Introduced in
606686eeac45("Btrfs: use rcu to protectdevice->name") with a vague reference of the potential problem described in
https://lore.kernel.org/all/[email protected]/ .The RCU protection looks like the easiest and most lightweight way of protecting the rare event of device rename racing
device_list_add()with a randomprintk()that uses the device name.Quoted 9 lines
Alternatives: a spin lock would require to protect the printk anyway, a fixed buffer for the name would be eventually wrong in case the new name is overwritten when being printed, an array switching pointers and cleaning them up eventually resembles RCU too much.
The cleanups up to this patch should hide special case of RCU to the minimum that only the name needs
rcu_dereference(), which can be further cleaned up to usebtrfs_dev_name().There is still rcu warning when 'make W=1 C=1'
/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: warning: incorrect type in argument 1 (different address spaces)/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: expected void const *objp/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: got char const [noderef]__rcu*namestatic void
btrfs_free_device(struct btrfs_device *device){ WARN_ON(!list_empty(&device->post_commit_list)); /* No need to call kfree_rcu(), nothing is reading the device name. */L405:
kfree(device->name);do we need
rcu_dereferencehere?
fs/btrfs/volumes.c static void btrfs_free_device(struct btrfs_device *device) · line 405
Quoted 5 lines
fs/btrfs/volumes.c +1
-1@@ -402,7 +402,7 @@ static void btrfs_free_device(struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));/* No need to call kfree_rcu(), nothing is reading the device name. */ -kfree (device->name); +kfree (rcu_dereference (device->name));
I got notified by the build bots (not CCed to the mailinglis) about this. The dereference is not needed, the comment says why. The checkers do not distinguish the context, some of them are safe like when the device is being set up and not yet accessible by other processes, and at deletion time, like here.
As we want to keep the __rcu annotation the rcu dereference is the easiest workaround.
Re: [PATCH v2 1/2] btrfs: open code RCU for device name
Quoted 76 lines · David Sterba
Quoted 63 lines · Wang Yugui
Hi,
Quoted 35 lines
The RCU protected string is only used for a device name, and RCU is used so we can print the name and eventually synchronize against the rare device rename in
device_list_add().We don't need the whole API just for that. Open code all the helpers and access to the string itself.
Notable change is in
device_list_add()when the device name is changed, which is the only place that can actually happen at the same time as message prints using the device name under RCU read lock.Previously there was
kfree_rcu()which used the embeddedrcu_headto delay freeing the object depending on the RCU mechanism. Now there'skfree_rcu_mightsleep()which does not need thercu_headand waits for the grace period.Sleeping is safe in this context and as this is a rare event it won't interfere with the rest as it's holding the
device_list_mutex.Straightforward changes:
-
rcu_string_strdup-> kstrdup
-rcu_str_deref->rcu_dereference
- drop ->str from safe contextsHistorical notes:
Introduced in
606686eeac45("Btrfs: use rcu to protectdevice->name") with a vague reference of the potential problem described in
https://lore.kernel.org/all/[email protected]/ .The RCU protection looks like the easiest and most lightweight way of protecting the rare event of device rename racing
device_list_add()with a randomprintk()that uses the device name.Quoted 9 lines
Alternatives: a spin lock would require to protect the printk anyway, a fixed buffer for the name would be eventually wrong in case the new name is overwritten when being printed, an array switching pointers and cleaning them up eventually resembles RCU too much.
The cleanups up to this patch should hide special case of RCU to the minimum that only the name needs
rcu_dereference(), which can be further cleaned up to usebtrfs_dev_name().There is still rcu warning when 'make W=1 C=1'
/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: warning: incorrect type in argument 1 (different address spaces)/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: expected void const *objp/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: got char const [noderef]__rcu*namestatic void
btrfs_free_device(struct btrfs_device *device){ WARN_ON(!list_empty(&device->post_commit_list)); /* No need to call kfree_rcu(), nothing is reading the device name. */L405:
kfree(device->name);do we need
rcu_dereferencehere?fs/btrfs/volumes.c static void btrfs_free_device(struct btrfs_device *device) · line 405
Quoted 5 lines
fs/btrfs/volumes.c +1
-1@@ -402,7 +402,7 @@ static void btrfs_free_device(struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));/* No need to call kfree_rcu(), nothing is reading the device name. */ -kfree (device->name); +kfree (rcu_dereference (device->name));I got notified by the build bots (not CCed to the mailinglis) about this. The dereference is not needed, the comment says why. The checkers do not distinguish the context, some of them are safe like when the device is being set up and not yet accessible by other processes, and at deletion time, like here.
Quoted 2 lines
As we want to keep the
__rcuannotation the rcu dereference is the easiest workaround.
I can't seem to reproduce the warning with the command, I'm going to apply this fixup:
fs/btrfs/volumes.c +10 -2
@@ -402,9 +402,17 @@ static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)static void btrfs_free_device (struct btrfs_device *device) { +const char *name; +WARN_ON (!list_empty (&device->post_commit_list)); -/* No need to call kfree_rcu(), nothing is reading the device name. */ -kfree (device->name); +/* +* No need to call kfree_rcu() or do RCU lock/unlock, nothing is +* reading the device name but the checkers complain. +*/ +rcu_read_lock (); + name =rcu_dereference (device->name); +rcu_read_unlock (); +kfree (name);btrfs_extent_io_tree_release (&device->alloc_state);btrfs_destroy_dev_zone_info (device);kfree (device);
Re: [PATCH v2 1/2] btrfs: open code RCU for device name
Quoted 91 lines · David Sterba
Quoted 76 lines · David Sterba
Quoted 63 lines · Wang Yugui
Hi,
Quoted 35 lines
The RCU protected string is only used for a device name, and RCU is used so we can print the name and eventually synchronize against the rare device rename in
device_list_add().We don't need the whole API just for that. Open code all the helpers and access to the string itself.
Notable change is in
device_list_add()when the device name is changed, which is the only place that can actually happen at the same time as message prints using the device name under RCU read lock.Previously there was
kfree_rcu()which used the embeddedrcu_headto delay freeing the object depending on the RCU mechanism. Now there'skfree_rcu_mightsleep()which does not need thercu_headand waits for the grace period.Sleeping is safe in this context and as this is a rare event it won't interfere with the rest as it's holding the
device_list_mutex.Straightforward changes:
-
rcu_string_strdup-> kstrdup
-rcu_str_deref->rcu_dereference
- drop ->str from safe contextsHistorical notes:
Introduced in
606686eeac45("Btrfs: use rcu to protectdevice->name") with a vague reference of the potential problem described in
https://lore.kernel.org/all/[email protected]/ .The RCU protection looks like the easiest and most lightweight way of protecting the rare event of device rename racing
device_list_add()with a randomprintk()that uses the device name.Quoted 9 lines
Alternatives: a spin lock would require to protect the printk anyway, a fixed buffer for the name would be eventually wrong in case the new name is overwritten when being printed, an array switching pointers and cleaning them up eventually resembles RCU too much.
The cleanups up to this patch should hide special case of RCU to the minimum that only the name needs
rcu_dereference(), which can be further cleaned up to usebtrfs_dev_name().There is still rcu warning when 'make W=1 C=1'
/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: warning: incorrect type in argument 1 (different address spaces)/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: expected void const *objp/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: got char const [noderef]__rcu*namestatic void
btrfs_free_device(struct btrfs_device *device){ WARN_ON(!list_empty(&device->post_commit_list)); /* No need to call kfree_rcu(), nothing is reading the device name. */L405:
kfree(device->name);do we need
rcu_dereferencehere?fs/btrfs/volumes.c static void btrfs_free_device(struct btrfs_device *device) · line 405
Quoted 5 lines
fs/btrfs/volumes.c +1
-1@@ -402,7 +402,7 @@ static void btrfs_free_device(struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));/* No need to call kfree_rcu(), nothing is reading the device name. */ -kfree (device->name); +kfree (rcu_dereference (device->name));I got notified by the build bots (not CCed to the mailinglis) about this. The dereference is not needed, the comment says why. The checkers do not distinguish the context, some of them are safe like when the device is being set up and not yet accessible by other processes, and at deletion time, like here.
Quoted 2 lines
As we want to keep the
__rcuannotation the rcu dereference is the easiest workaround.I can't seem to reproduce the warning with the command, I'm going to apply this fixup:
fs/btrfs/volumes.c +1
-0@@ -402,9 +402,17 @@ static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)static void btrfs_free_device (struct btrfs_device *device) { +const char *name;
fs/btrfs/volumes.c static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) · line 413
Quoted 10 lines
fs/btrfs/volumes.c +7
-2+WARN_ON (!list_empty (&device->post_commit_list)); -/* No need to call kfree_rcu(), nothing is reading the device name. */ -kfree (device->name); +/* +* No need to call kfree_rcu() or do RCU lock/unlock, nothing is +* reading the device name but the checkers complain. +*/ +rcu_read_lock (); + name =rcu_dereference (device->name);
Since it’s safe here, can we use rcu_dereference_raw without rcu_read_lock() ?
Quoted 6 lines
fs/btrfs/volumes.c +2
-0+rcu_read_unlock (); +kfree (name);btrfs_extent_io_tree_release (&device->alloc_state);btrfs_destroy_dev_zone_info (device);kfree (device);
Re: [PATCH v2 1/2] btrfs: open code RCU for device name
Quoted 11 lines · Alan Huang
fs/btrfs/volumes.c static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) · line 413
Quoted 9 lines
WARN_ON(!list_empty(&device->post_commit_list));fs/btrfs/volumes.c +6
-2-/* No need to call kfree_rcu(), nothing is reading the device name. */ -kfree (device->name); +/* +* No need to call kfree_rcu() or do RCU lock/unlock, nothing is +* reading the device name but the checkers complain. +*/ +rcu_read_lock (); + name =rcu_dereference (device->name);Since it’s safe here, can we use
rcu_dereference_rawwithoutrcu_read_lock()?
Right, thanks for the hint.
Re: [PATCH v2 1/2] btrfs: open code RCU for device name
In reply to David SterbaHi,
Quoted 77 lines · David Sterba
Quoted 76 lines · Wang Yugui
Quoted 63 lines
Hi,
Quoted 35 lines
The RCU protected string is only used for a device name, and RCU is used so we can print the name and eventually synchronize against the rare device rename in
device_list_add().We don't need the whole API just for that. Open code all the helpers and access to the string itself.
Notable change is in
device_list_add()when the device name is changed, which is the only place that can actually happen at the same time as message prints using the device name under RCU read lock.Previously there was
kfree_rcu()which used the embeddedrcu_headto delay freeing the object depending on the RCU mechanism. Now there'skfree_rcu_mightsleep()which does not need thercu_headand waits for the grace period.Sleeping is safe in this context and as this is a rare event it won't interfere with the rest as it's holding the
device_list_mutex.Straightforward changes:
-
rcu_string_strdup-> kstrdup
-rcu_str_deref->rcu_dereference
- drop ->str from safe contextsHistorical notes:
Introduced in
606686eeac45("Btrfs: use rcu to protectdevice->name") with a vague reference of the potential problem described in
https://lore.kernel.org/all/[email protected]/ .The RCU protection looks like the easiest and most lightweight way of protecting the rare event of device rename racing
device_list_add()with a randomprintk()that uses the device name.Quoted 9 lines
Alternatives: a spin lock would require to protect the printk anyway, a fixed buffer for the name would be eventually wrong in case the new name is overwritten when being printed, an array switching pointers and cleaning them up eventually resembles RCU too much.
The cleanups up to this patch should hide special case of RCU to the minimum that only the name needs
rcu_dereference(), which can be further cleaned up to usebtrfs_dev_name().There is still rcu warning when 'make W=1 C=1'
/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: warning: incorrect type in argument 1 (different address spaces)/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: expected void const *objp/usr/hpc-bio/linux-6.12.35/fs/btrfs/volumes.c:405:21: got char const [noderef]__rcu*namestatic void
btrfs_free_device(struct btrfs_device *device){ WARN_ON(!list_empty(&device->post_commit_list)); /* No need to call kfree_rcu(), nothing is reading the device name. */L405:
kfree(device->name);do we need
rcu_dereferencehere?fs/btrfs/volumes.c static void btrfs_free_device(struct btrfs_device *device) · line 405
Quoted 5 lines
fs/btrfs/volumes.c +1
-1@@ -402,7 +402,7 @@ static void btrfs_free_device(struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));/* No need to call kfree_rcu(), nothing is reading the device name. */ -kfree (device->name); +kfree (rcu_dereference (device->name));I got notified by the build bots (not CCed to the mailinglis) about this. The dereference is not needed, the comment says why. The checkers do not distinguish the context, some of them are safe like when the device is being set up and not yet accessible by other processes, and at deletion time, like here.
Quoted 5 lines
Quoted 2 lines
As we want to keep the
__rcuannotation the rcu dereference is the easiest workaround.I can't seem to reproduce the warning with the command, I'm going to apply this fixup:
steps to reproduce the warning:
1) install https://kojipkgs.fedoraproject.org/packages/sparse/0.6.4/
2) make btrfs module(kernel 6.16) with the commands
#uname_r=$(uname -r)
uname_r=$(ls /boot/vmlinuz-6.16.* 2>/dev/null) uname_r=${uname_r##/boot/vmlinuz-}
pwd_dir=$(pwd)
make -C /lib/modules/${uname_r}/build M=${pwd_dir} modules -j 20 W=1 C=1 CF="-Wnocontext"
Then we can see other 4 warnings.
fs/btrfs/volumes.c +4 -4
@@ -699,7 +699,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,if (!device->name)return -EINVAL ; - ret =btrfs_get_bdev_and_sb (device->name, flags, holder,1 , + ret =btrfs_get_bdev_and_sb (rcu_dereference (device->name), flags, holder,1 , &bdev_file, &disk_super);if (ret)return ret;
@@ -1061,7 +1061,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)* uuid mutex so nothing we touch in here is going to disappear. */ if (orig_dev->name) - dev_path = orig_dev->name; + dev_path =rcu_dereference (orig_dev->name); device =btrfs_alloc_device (NULL , &orig_dev->devid, orig_dev->uuid, dev_path);
@@ -1436,7 +1436,7 @@ static bool btrfs_skip_registration(struct btrfs_super_block *disk_super,list_for_each_entry (device, &fs_devices->devices, dev_list) {if (device->bdev && (device->bdev->bd_dev == devt) && -strcmp (device->name, path) !=0 ) { +strcmp (rcu_dereference (device->name), path) !=0 ) {mutex_unlock (&fs_devices->device_list_mutex);/* Do not skip registration. */
@@ -2197,7 +2197,7 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct btrfs_devicbtrfs_kobject_uevent (bdev,KOBJ_CHANGE );/* Update ctime/mtime for device path for libblkid */ -update_dev_time (device->name); +update_dev_time (rcu_dereference (device->name)); } int btrfs_rm_device(struct btrfs_fs_info *fs_info,
Best Regards
Wang Yugui ([email protected])
2025/07/02
Re: [PATCH v2 1/2] btrfs: open code RCU for device name
Quoted 11 lines · Wang Yugui
Quoted 2 lines
I can't seem to reproduce the warning with the command, I'm going to apply this fixup:
steps to reproduce the warning:
1) install https://kojipkgs.fedoraproject.org/packages/sparse/0.6.4/
2) make btrfs module(kernel 6.16) with the commands
#uname_r=$(uname -r)
uname_r=$(ls/boot/vmlinuz-6.16.*2>/dev/null) uname_r=${uname_r##/boot/vmlinuz-}
pwd_dir=$(pwd)
make -C/lib/modules/${uname_r}/buildM=${pwd_dir} modules -j 20 W=1 C=1 CF="-Wnocontext"
I did try the W=1 C=1 build but there were no reports, even with update sparse (which is what is run with bare C=1 I believe).
Quoted 32 lines
fs/btrfs/volumes.c +3
-3@@ -699,7 +699,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,if (!device->name)return -EINVAL ; - ret =btrfs_get_bdev_and_sb (device->name, flags, holder,1 , + ret =btrfs_get_bdev_and_sb (rcu_dereference (device->name), flags, holder,1 , &bdev_file, &disk_super);if (ret)return ret;@@ -1061,7 +1061,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)* uuid mutex so nothing we touch in here is going to disappear. */ if (orig_dev->name) - dev_path = orig_dev->name; + dev_path =rcu_dereference (orig_dev->name); device =btrfs_alloc_device (NULL , &orig_dev->devid, orig_dev->uuid, dev_path);@@ -1436,7 +1436,7 @@ static bool btrfs_skip_registration(struct btrfs_super_block *disk_super,list_for_each_entry (device, &fs_devices->devices, dev_list) {if (device->bdev && (device->bdev->bd_dev == devt) && -strcmp (device->name, path) !=0 ) { +strcmp (rcu_dereference (device->name), path) !=0 ) {mutex_unlock (&fs_devices->device_list_mutex);/* Do not skip registration. */
fs/btrfs/volumes.c void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct btrfs_devic · line 2201
Quoted 6 lines
fs/btrfs/volumes.c +1
-1@@ -2197,7 +2197,7 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct btrfs_devicbtrfs_kobject_uevent (bdev,KOBJ_CHANGE );/* Update ctime/mtime for device path for libblkid */ -update_dev_time (device->name); +update_dev_time (rcu_dereference (device->name)); }
I've updated the patch, this is basically equivalent to the same expression but does not report any RCU violation. This should be safe in all cases so the _raw's are justified for this patchset. The handling of device->name perhaps does not fit any common pattern for RCU so we can't avoid it. The protection involves no mounted filesystem (the scanning ioctl) and then access when the filesystem is mounted. We have the uuid_mutex for serializing everything.
For linux-next, I've updated the patch to use rcu_dereference_raw(), depending on when the branch is picked for merge there should be no warnings anymore.
Re: [PATCH v2 1/2] btrfs: open code RCU for device name
Quoted 11 lines · David Sterba
I've updated the patch, this is basically equivalent to the same expression but does not report any RCU violation. This should be safe in all cases so the _raw's are justified for this patchset. The handling of
device->nameperhaps does not fit any common pattern for RCU so we can't avoid it. The protection involves no mounted filesystem (the scanning ioctl) and then access when the filesystem is mounted. We have theuuid_mutexfor serializing everything.For linux-next, I've updated the patch to use
rcu_dereference_raw(), depending on when the branch is picked for merge there should be no warnings anymore.
No new warnings reported.