mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg KH <gregkh@suse.de>
To: David Herrmann <dh.herrmann@googlemail.com>
Cc: linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: [BUG] kobject module-ref race-condition or unsafe module_put(THIS_MODULE)
Date: Sun, 30 Oct 2011 00:39:35 +0200	[thread overview]
Message-ID: <20111029223935.GB3952@suse.de> (raw)
In-Reply-To: <CANq1E4SovxjRsuDN3PMqCnjBMMQeHWnvbwreXJWcDj7D7wUxWw@mail.gmail.com>

On Sat, Oct 29, 2011 at 07:52:47PM +0200, David Herrmann wrote:
> Hi
> 
> I currently do not understand how kobjects keep a reference to the
> owning module. Lets assume I provide a "release" method via a
> kobj_type for my kobject. I want to go sure my module is still alive
> when this method is called. Otherwise, this "release" method would be
> no longer available and we would jump into invalid memory. Therefore,
> I need to take a reference to my own module. This seems trivial.
> However, how do I release this reference again?
> The most simple solution might be calling module_put(THIS_MODULE) in
> my "release" method. However, if this call drops the module-refcount
> to 0 and immediately removes the module, the module_put() returns to
> my "release" function which now is no longer available.
> 
> It seems quite unlikely that the cleanup of a module is faster than
> two function-returns, however, theoretically there is a race
> condition.
> 
> The following example is based on fs/char_dev.c. Lets assume my module
> provides a  kobject structure. On init I take a ref to myself with
> try_module_get(THIS_MODULE).

No, never do that, why would you?

> I add my own kobj_type with the following release function:
> 
> static void mydev_put(struct mydev *p)
> {
> 	if (p) {
> 		kobject_put(&p->kobj);
> 		module_put(THIS_MODULE);
> 	}
> }

Ick, don't do that.

> How can we go sure that module_put() doesn't free my own module before
> it returns? Isn't a call to module_put(THIS_MODULE) always unsafe
> (unless I own at least two references)?

Yes, that's why you shouldn't be doing this :)

> Maybe I have missed some important fact here, but this seems quite
> unsafe to me. Adding a "owner" field to a kobj_type would fix that
> issue for kobjects/devices.

No, let's determine exactly what you are trying to do first, and why in
the world you are dealing with "raw" kobjects.  You should almost never
never never do that.

What driver are you writing?  Have a pointer to the code somewhere?

greg k-h

  reply	other threads:[~2011-10-29 22:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-29 17:52 David Herrmann
2011-10-29 22:39 ` Greg KH [this message]
2011-10-29 23:36   ` David Herrmann
2011-11-01 17:05     ` Greg KH
2011-10-29 23:46   ` David Herrmann

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=20111029223935.GB3952@suse.de \
    --to=gregkh@suse.de \
    --cc=dh.herrmann@googlemail.com \
    --cc=linux-kernel@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

Powered by JetHome