From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Oliver Neukum <oliver@neukum.org>, Greg KH <gregkh@suse.de>,
stable@kernel.org, Rickard Bellini <rickard.bellini@ericsson.com>,
"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
Torgny Johansson <torgny.johansson@ericsson.com>,
Kernel development list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] Driver core: fix race in dev_driver_string
Date: Fri, 4 Dec 2009 14:23:10 -0800 [thread overview]
Message-ID: <200912041423.10632.dmitry.torokhov@gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.44L0.0912041702510.2616-100000@iolanthe.rowland.org>
On Friday 04 December 2009 02:07:11 pm Alan Stern wrote:
> On Fri, 4 Dec 2009, Oliver Neukum wrote:
> > OK, yes. It's a bad example. However this is tricky.
> >
> > This is a bug then:
> >
> > mutex_lock(...);
> >
> > if (instance->error) {
> > rv = instance->error;
> > instance->error = 0;
> > dev_dbg(instance->dev,...);
>
> Unless you can guarantee at this point that instance->dev isn't stale,
> it is indeed a bug.
>
> > goto err_out;
> > }
> >
> > rv = -ENODEV;
> > if (instance->disconnected)
> > goto err_out;
> >
> > > One approach is to set instance->dev to NULL in disconnect(). That
> > > wouldn't do much good for your dev_dbg(), though. A better solution is
> > > to refcount the instance->dev pointer: Take a reference to the device
> > > when setting instance->dev and drop it when clearing instance->dev (or
> > > when instance is freed).
> >
> > That would mean that I am forced to adopt refcounting just to print
> > something. This seems very inelegant.
>
> What can I say? When the something you want to print can be
> deallocated at any time, there isn't much choice.
>
> Maybe reference counting is inelegant; it depends on your point of
> view. Can you think of a more elegant way to make sure that a pointer
> isn't stale?
Yes, just say "no" to device_create() and friends. Embed device structure in
yours, be mindful of lifetime rules and only use "your" device (i.e device
bound to your driver). This way, as long as your refcount your instance you
can rest assured the device structure is there as well.
--
Dmitry
next prev parent reply other threads:[~2009-12-04 22:29 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20091204044337.GE14819@suse.de>
2009-12-04 16:06 ` [PATCH] Driver core: fix race in dev_driver_string Alan Stern
2009-12-04 16:16 ` Oliver Neukum
2009-12-04 16:50 ` Greg KH
2009-12-04 19:55 ` Oliver Neukum
2009-12-04 20:57 ` Alan Stern
2009-12-04 21:18 ` Oliver Neukum
2009-12-04 21:36 ` Alan Stern
2009-12-04 21:58 ` Oliver Neukum
2009-12-04 22:07 ` Alan Stern
2009-12-04 22:23 ` Dmitry Torokhov [this message]
2009-12-04 23:50 ` Alan Stern
2009-12-05 0:35 ` Greg KH
2009-12-05 2:37 ` Alan Stern
2009-12-05 0:33 ` Greg KH
2009-12-04 16:57 ` Alan Stern
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=200912041423.10632.dmitry.torokhov@gmail.com \
--to=dmitry.torokhov@gmail.com \
--cc=gregkh@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=oliver@neukum.org \
--cc=rickard.bellini@ericsson.com \
--cc=stable@kernel.org \
--cc=stern@rowland.harvard.edu \
--cc=torgny.johansson@ericsson.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