All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wang Yugui <wangyugui@e16-tech.com>
To: David Sterba <dsterba@suse.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH v2 1/2] btrfs: open code RCU for device name
Date: Mon, 30 Jun 2025 10:24:57 +0800	[thread overview]
Message-ID: <20250630102457.BFB9.409509F4@e16-tech.com> (raw)
In-Reply-To: <1e539dfd73debc86ddc7c1b1716f86ace14d51aa.1750858539.git.dsterba@suse.com>

Hi,

> 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/20120531155304.GF11775@ZenIV.linux.org.uk/ .
> 
> 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().
> 

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?
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -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 (wangyugui@e16-tech.com)
2025/06/30


  parent reply	other threads:[~2025-06-30  2:30 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-25 13:37 [PATCH v2 0/2] Device name and RCU string David Sterba
2025-06-25 13:37 ` [PATCH v2 1/2] btrfs: open code RCU for device name David Sterba
2025-06-27  9:13   ` kernel test robot
2025-06-27 12:32     ` David Sterba
2025-06-30  2:24   ` Wang Yugui [this message]
2025-06-30 16:21     ` David Sterba
2025-06-30 16:43       ` David Sterba
2025-06-30 17:07         ` Alan Huang
2025-07-01 14:47           ` David Sterba
2025-07-01 23:29         ` Wang Yugui
2025-07-04  3:14           ` David Sterba
2025-07-07 14:28             ` David Sterba
2025-06-25 13:37 ` [PATCH v2 2/2] btrfs: remove struct rcu_string David Sterba
2025-06-26 14:46 ` [PATCH v2 0/2] Device name and RCU string Daniel Vacek

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=20250630102457.BFB9.409509F4@e16-tech.com \
    --to=wangyugui@e16-tech.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.