All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rusty Russell <rusty@rustcorp.com.au>
To: Daniel Phillips <phillips@arcor.de>
Cc: Roman Zippel <zippel@linux-m68k.org>, linux-kernel@vger.kernel.org
Subject: Re: [RFC] change format of LSM hooks
Date: Fri, 18 Oct 2002 12:04:15 +1000	[thread overview]
Message-ID: <20021018022503.1E5F52C24A@lists.samba.org> (raw)
In-Reply-To: Your message of "Thu, 17 Oct 2002 19:20:10 +0200." <E182EJl-0004Rd-00@starship>

In message <E182EJl-0004Rd-00@starship> you write:
> > The current spinlock is horrible.
> 
> Is it?  You must be thinking about much more intensive use of the spinlock as
> with per-op calls as opposed to per-attach (mount).  I'd planned to make the 
> spinlocks per-module, but your per-cpu code looks just fine.

Actually, after I thought about it harder, I was inspired by your
point that the race can be prevented with a spinlock (as currently
done).  I spent last night writing a new solution to this (I'm testing
it now), which works as follows:

/* We only need protection against local interrupts. */
#ifndef __HAVE_ARCH_LOCAL_INC
#define local_inc(x) atomic_inc(x)
#define local_dec(x) atomic_dec(x)
#endif

static inline int try_module_get(struct module *module)
{
	int ret = 1;

	if (module) {
		unsigned int cpu = get_cpu();
		if (likely(module->live))
			local_inc(&module->ref[cpu]);
		else
			ret = 0;
		put_cpu();
	}
	return ret;
}

static inline void module_put(struct module *module)
{
	if (module) {
		unsigned int cpu = get_cpu();
		local_dec(&module->ref[cpu]);
		/* Maybe they're waiting for us to drop reference? */
		if (unlikely(!module->live))
			wake_up_process(module->waiter);
		put_cpu();
	}
}

Now, these are both short, sweet, and cache-local.  The unload stage
becomes harder.  We stop all the reference counts by scheduling a
thread on every cpu, then having them all do a local_irq_disable.
This means we know that the refcounts won't change, and we can safely
sum them.  If they're zero (or the non-block flag isn't set), we set
"module->live" to false and release the CPUs.

Now, in the sleeping case, I sleep uninterruptible, but drop the
module mutex first, so other module stuff can happen at the same time.

This completely prevents the spurious unload race.

> > I don't know of any code which does this now, but it is at least a
> > theoretical problem.
> 
> To resolve this, start the module in can-increment state, do the module 
> initialization, register the notifiers, and finally register the interface.
> In other words, the module never needs to be in can't-increment state at 
> initialization.  (The module writer must ensure they have the correct, 
> raceless initial state of whatever the notifiers are notifying about, which 
> strikes me as a little tricky in itself.)

Yes, this basically means two-state init for modules, which I shy
clear of in general.  But if any modules care, they can do this
themselves, though, by setting "THIS_MODULE.live = 1;" to activate
the notifiers during their init routine.

Basically, with the other one solved, I'm happy to place this on the
shoulders of any module which really cares.

> > IMHO, the benifits of having it in-kernel outweigh the slight extra
> > size.
> 
> I'll cast my vote for your in-kernel linker, in the mistaken belief that 
> democracy has anything to do with the question.  Does this make progress 
> towards eliminating one of create_module or init_module?

Yes, query_module, create_module and /proc/ksyms all gone.

> > > > ...The second is the "die-mother-fucker-die"
> 
> I'd use this feature in filesystem development, regardless of the risks, 
> since I regularly do worse things to the kernel anyway.  But don't you think 
> the rmmod parameter should be different for the ask-nicely vs the 
> shoot-in-the-head flavor?  How about -f -f or -F for the latter?

Yes, currently:
	rusty@mingo:~$ /sbin/rmmod       
	Usage: /sbin/rmmod [--wait|-f] <modulename>
	  The --wait option begins a module removal even if it is used
	  and will stop new users from accessing the module (so it
	  should eventually fall to zero).
	  The -f option forces a module unload, and may crash your
	  machine.  This requires the Forced Module Removal option
	  when the kernel was compiled.

Cheers,
Rusty.
--
  Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

  reply	other threads:[~2002-10-18  2:19 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-18  2:05 [PATCH] In-kernel module loader 1/7 Rusty Russell
2002-09-18 22:59 ` Roman Zippel
2002-09-19  1:00   ` Rusty Russell
2002-09-19  2:19     ` Daniel Jacobowitz
2002-09-19  3:57       ` Rusty Russell
2002-09-19 10:44     ` Roman Zippel
2002-09-19 12:51       ` Rusty Russell
2002-09-19 13:54         ` Roman Zippel
2002-09-19 18:38           ` Greg KH
2002-09-19 18:58             ` Alan Cox
2002-09-19 20:11               ` Greg KH
2002-09-19 20:42                 ` Roman Zippel
2002-09-30 15:32                 ` Daniel Phillips
2002-10-03 18:53                   ` Rob Landley
2002-10-04  0:10                     ` Daniel Phillips
2002-10-15  3:25                   ` Rusty Russell
2002-10-15 15:28                     ` Daniel Phillips
2002-10-15 23:53                       ` Rusty Russell
2002-10-16  2:59                         ` Daniel Phillips
2002-10-16  6:11                           ` Rusty Russell
2002-10-16 17:33                             ` Daniel Phillips
2002-10-16 22:48                               ` Rusty Russell
2002-10-17  1:57                                 ` Daniel Phillips
2002-10-17  7:41                                   ` Rusty Russell
2002-10-17 14:49                                     ` Roman Zippel
2002-10-17 14:56                                     ` your mail Kai Germaschewski
2002-10-18  2:47                                       ` Rusty Russell
2002-10-18 21:50                                         ` Kai Germaschewski
2002-10-17 17:20                                     ` [RFC] change format of LSM hooks Daniel Phillips
2002-10-18  2:04                                       ` Rusty Russell [this message]
2002-10-17 17:25                                     ` Daniel Phillips
2002-10-16  8:15                         ` [PATCH] In-kernel module loader 1/7 Chris Wright
2002-09-19 20:10             ` Roman Zippel
2002-09-20  1:22             ` Rusty Russell
2002-09-20  4:32               ` Greg KH
2002-09-20  9:25                 ` Rusty Russell
2002-09-21  7:38               ` Kevin O'Connor
2002-09-22 23:31                 ` Rusty Russell
2002-09-19 23:44           ` Rusty Russell
2002-09-20  9:32             ` Roman Zippel
2002-09-21  4:17               ` Rusty Russell
2002-09-21 17:09                 ` Roman Zippel
2002-09-23  0:20                   ` Rusty Russell
2002-09-24 10:16                     ` Roman Zippel
2002-09-24 14:54                       ` Rusty Russell
2002-09-25  0:46                         ` Roman Zippel
2002-09-25  5:50                           ` Rusty Russell
2002-09-25 11:36                             ` Roman Zippel
2002-09-25 12:53                               ` Rusty Russell
2002-09-25 21:28                                 ` Roman Zippel
2002-09-26  1:49                                   ` Rusty Russell
2002-09-26 23:38                                     ` Roman Zippel
2002-09-27  1:11                                       ` Scott Murray
2002-09-27  1:34                                         ` Roman Zippel
2002-09-28  0:48                                           ` David Lang
2002-10-15  4:53                                       ` Rusty Russell
     [not found] <20021015194545.GC15864@kroah.com>
     [not found] ` <20021015.124502.130514745.davem@redhat.com>
     [not found]   ` <20021015201209.GE15864@kroah.com>
     [not found]     ` <20021015.131037.96602290.davem@redhat.com>
     [not found]       ` <20021015202828.GG15864@kroah.com>
2002-10-16  0:07         ` [RFC] change format of LSM hooks Greg KH
2002-10-16  0:03           ` David S. Miller
2002-10-16  8:15           ` Greg KH
2002-10-16 18:59             ` Greg KH
2002-10-16 16:33               ` joe perches
2002-10-16 23:46                 ` Greg KH
2002-10-16 23:56                   ` David S. Miller
2002-10-16 19:07               ` Greg KH
2002-10-17  1:41           ` Rusty Russell
2002-10-17  3:33             ` Daniel Phillips
2002-10-17 13:21           ` Christoph Hellwig
2002-10-17 16:55             ` Greg KH

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=20021018022503.1E5F52C24A@lists.samba.org \
    --to=rusty@rustcorp.com.au \
    --cc=linux-kernel@vger.kernel.org \
    --cc=phillips@arcor.de \
    --cc=zippel@linux-m68k.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.