All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alan Stern <stern@rowland.harvard.edu>
To: Rusty Russell <rusty@rustcorp.com.au>
Cc: Greg KH <greg@kroah.com>, <torvalds@osdl.org>,
	<linux-kernel@vger.kernel.org>, <mochel@digitalimplant.org>
Subject: Re: PATCH: (as177)  Add class_device_unregister_wait() and platform_device_unregister_wait() to the driver model core
Date: Mon, 26 Jan 2004 10:51:14 -0500 (EST)	[thread overview]
Message-ID: <Pine.LNX.4.44L0.0401261016530.822-100000@ida.rowland.org> (raw)
In-Reply-To: <20040126165035.2fda1b3e.rusty@rustcorp.com.au>

On Mon, 26 Jan 2004, Rusty Russell wrote:

> If you want to safely remove parts of the kernel, you have to maintain
> reference counts.  At least with any sane scheme I've seen.
> 
> I know, I should go read the code...
> Rusty.

The problem I raised originally is that in many cases the reference count
can't be maintained properly without preventing the module from ever
exiting at all.  The difficulty is that the reference count won't go
down to 0 until the module code deregisters itself from some list.  But
the deregistration only happens within the module's exit routine!

A two-step exit process, like that used for kobjects, would avoid this 
difficulty.  During the first step the module would deregister itself.  
The second step, unloading from memory, would occur when the reference 
count was 0.

Contrary what Linus said, in many modules it's not necessary to update the 
module's reference count with every single transaction (packet or 
whatever) that goes through.  Usually a single registration event, or just 
a small handful, is critical for unloading.

A good case in point is the one that led to the start of this thread.  A
USB host controller driver registers a USB bus that it will manage, and
from then on it handles lots of USB packets.  It's not necessary to update
the driver module's reference count with every packet; all that matters is
that the module has to wait for the bus to be released before it can be
unloaded from memory.  (That's because existing mechanisms cause each
packet to hold a reference to a USB device and the device holds a
reference to the bus.)  For lack of any other way to avoid exiting early
we simply have to wait for the bus's release callback to finish -- not
waiting will cause a kernel panic if the module unloads before the release
method runs.

Furthermore, in other cases where it _is_ necessary to update a reference
count with every packet, it's not necessary that doing so involve a lot of
additional overhead such as acquiring a lock of some sort.  If some
driver, like a network interface driver, is managing lots of packets then
it must _already_ be using a lock to keep track of things like the total
number of outstanding packets.  Any extra work could be done under the
protection of this pre-existing lock and would involve minimal overhead.


One aspect of what Linus wrote is absolutely right, however: Getting this 
to work right, for all the loadable kernel modules, would be quite 
difficult.  Here's one way to attack that, an incremental approach.

Create a new module entry point, the module_unreg routine.  For all
existing modules this entry point would be undefined and hence not used.  
The module_unreg routine is called to start the deregistration process,
invoked say by some special flag to rmmod.  The module_exit routine would
then be called when an unregistered module's reference count dropped to 0.  
Existing modules would experience exactly the same sequence of events as
they do now, and newly-written modules could take advantage of the extra
mechanism.

Admittedly, this is just a theoretical exercise.  Linus said that module 
unloading should basically be unsupported.  I have no doubt that making it 
even more complicated, like this, is not something he would approve of.

Alan Stern


  reply	other threads:[~2004-01-26 15:51 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-23 16:58 PATCH: (as177) Add class_device_unregister_wait() and platform_device_unregister_wait() to the driver model core Alan Stern
2004-01-23 17:42 ` Linus Torvalds
2004-01-23 18:03   ` Alan Stern
2004-01-23 18:10     ` viro
2004-01-23 18:18       ` Greg KH
2004-01-23 18:15     ` Linus Torvalds
2004-01-23 18:31       ` Greg KH
2004-01-23 18:11   ` Greg KH
2004-01-23 18:19     ` Linus Torvalds
2004-01-23 18:27       ` Greg KH
2004-01-25 17:32       ` Alan Stern
2004-01-25 19:02         ` Linus Torvalds
2004-01-25 20:21           ` viro
2004-01-27  6:51             ` Rusty Russell
2004-01-27 13:56               ` Roman Zippel
2004-01-27 23:29                 ` Rusty Russell
2004-01-28  2:36                   ` Roman Zippel
2004-01-28  3:54                     ` Rusty Russell
2004-01-25 23:12           ` Steve Youngs
2004-01-26  3:22             ` Adam Kropelin
2004-01-26  5:06               ` Steve Youngs
2004-01-26  5:21                 ` Valdis.Kletnieks
2004-01-26  5:55                   ` Steve Youngs
2004-01-26  6:25                     ` Valdis.Kletnieks
2004-01-26  8:48                     ` Helge Hafting
2004-01-26 15:50                 ` Adam Kropelin
2004-01-26 16:22           ` Roman Zippel
2004-01-27 19:32             ` Russell King
2004-01-27 20:28               ` Greg KH
2004-01-27 20:29             ` Greg KH
2004-01-28  2:03               ` Roman Zippel
2004-01-28  2:17                 ` viro
2004-01-28  2:53                   ` Roman Zippel
2004-01-27  6:41           ` Rusty Russell
2004-01-23 19:45     ` viro
2004-01-26  5:50     ` Rusty Russell
2004-01-26 15:51       ` Alan Stern [this message]
2004-01-27 22:55         ` Rusty Russell

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=Pine.LNX.4.44L0.0401261016530.822-100000@ida.rowland.org \
    --to=stern@rowland.harvard.edu \
    --cc=greg@kroah.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mochel@digitalimplant.org \
    --cc=rusty@rustcorp.com.au \
    --cc=torvalds@osdl.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.