Linux CXL
 help / color / mirror / Atom feed
From: Dan Williams <dan.j.williams@intel.com>
To: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>,
	Dan Williams <dan.j.williams@intel.com>,
	<gregkh@linuxfoundation.org>
Cc: <syzbot+4762dd74e32532cda5ff@syzkaller.appspotmail.com>,
	<stable@vger.kernel.org>, Ashish Sangwan <a.sangwan@samsung.com>,
	Namjae Jeon <namjae.jeon@samsung.com>,
	Dirk Behme <dirk.behme@de.bosch.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	<linux-kernel@vger.kernel.org>, <linux-cxl@vger.kernel.org>
Subject: Re: [PATCH] driver core: Fix uevent_show() vs driver detach race
Date: Fri, 12 Jul 2024 16:49:12 -0700	[thread overview]
Message-ID: <6691c0f8da1dd_8e85329468@dwillia2-xfh.jf.intel.com.notmuch> (raw)
In-Reply-To: <dfcb0456-dd75-4b9f-9cc8-f0658cd9ce29@I-love.SAKURA.ne.jp>

Tetsuo Handa wrote:
> On 2024/07/13 4:42, Dan Williams wrote:
> > @@ -2668,8 +2670,12 @@ static int dev_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
> >  	if (dev->type && dev->type->name)
> >  		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
> >  
> > -	if (dev->driver)
> > -		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
> > +	/* Synchronize with module_remove_driver() */
> > +	rcu_read_lock();
> > +	driver = READ_ONCE(dev->driver);
> > +	if (driver)
> > +		add_uevent_var(env, "DRIVER=%s", driver->name);
> > +	rcu_read_unlock();
> >  
> 
> Given that read of dev->driver is protected using RCU,
> 
> > @@ -97,6 +98,9 @@ void module_remove_driver(struct device_driver *drv)
> >  	if (!drv)
> >  		return;
> >  
> 
> where is
> 
> 	dev->driver = NULL;
> 
> performed prior to

It happens in __device_release_driver() and several places in the driver
probe failure path. However, the point of this patch is that the
"dev->driver = NULL" event does not really matter for this sysfs
attribute.

This attribute just wants to opportunistically report the driver name to
userspace, but that result is ephemeral. I.e. as soon as a dev_uevent()
adds a DRIVER environment variable that result could be immediately
invalidated before userspace has a chance to do anything with the
result.

Even with the current device_lock() solution userspace can not depend on
the driver still being attached when it goes to act on the DRIVER
environment variable.

> > +	/* Synchronize with dev_uevent() */
> > +	synchronize_rcu();
> > +
> 
> this synchronize_rcu(), in order to make sure that
> READ_ONCE(dev->driver) in dev_uevent() observes NULL?

No, this synchronize_rcu() is to make sure that if dev_uevent() wins the
race and observes that dev->driver is not NULL that it is still safe to
dereference that result because the 'struct device_driver' object is
still live.

A 'struct device_driver' instance is typically static data in a kernel
module that does not get freed until after driver_unregister(). Calls to
driver_unregister() typically only happen at module removal time. So
this synchronize_rcu() delays module removal until dev_uevent() finishes
reading driver->name.

  reply	other threads:[~2024-07-12 23:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-12 19:42 [PATCH] driver core: Fix uevent_show() vs driver detach race Dan Williams
2024-07-12 22:18 ` Tetsuo Handa
2024-07-12 23:49   ` Dan Williams [this message]
2024-07-13  6:05     ` Tetsuo Handa
2024-07-13 17:22       ` Dan Williams
2024-07-14  7:17 ` Greg KH
2024-07-14 16:49   ` Dan Williams
2024-08-06  9:00 ` Dirk Behme

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=6691c0f8da1dd_8e85329468@dwillia2-xfh.jf.intel.com.notmuch \
    --to=dan.j.williams@intel.com \
    --cc=a.sangwan@samsung.com \
    --cc=dirk.behme@de.bosch.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=namjae.jeon@samsung.com \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    --cc=rafael@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+4762dd74e32532cda5ff@syzkaller.appspotmail.com \
    /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