need for another mutex type/flag?

John Baldwin jhb at
Mon Jan 26 06:58:33 PST 2009

On Monday 26 January 2009 5:51:40 am Alfred Perlstein wrote:
> * Julian Elischer <julian at> [090125 22:53] wrote:
> > Alfred Perlstein wrote:
> > >
> > >Jeff, I think that Julian really wants to prevent a sleep inside
> > >his context.  Right now, I think we only check for mutexes held
> > >before a sleep that arne't sleepable.  It might make sense to allow
> > >one to just mark a thread non-sleepable even though no mutex is
> > >held.
> > >
> > >Julian, is that right?
> > 
> > basically, though I don't know the details of implementation..
> > I just know that mutexes per se aren't bad for netgraph but
> > that node authors need some guidance on how to use them and
> > some way to prove to them when they do the wrong thing.
> The way to add the assertion you want would be to keep a count
> inside of the thread structure "td_nosleep", set to 0 at thread
> creation, then you can do this:
> TD_SLEEP_NO(td);  /* td->td_nosleep++ */
> call_some_untrusted_code();
> TD_SLEEP_OK(td);  /* td->td_nosleep-- */
> Then add this to subr_witness.c:witness_warn():
>    if (flags & WARN_SLEEPOK && td->td_nosleep != 0) {
>       printf("Sleeping in unsleepable context.\n");
>       n++;   /* this variable is local to witness_warn() 
>                 and triggers an ASSERT at the end */
>    }
> I could have sworn we already had such a feature, but it appears
> that it's only accessable if you're holding a lock, if you added
> this counter, then you could catch sleeps without needing a lock
> held.

We have this feature already for sleeping, but I think Julian isn't worried 
about sleeping (i.e. *sleep() or cv_*wait*()), but wants to prevent the code 
from acquiring any other locks.  It's easy to add a MTX_LEAF, I'm just not 
sure if we really want to micro-manage the code that much.  WITNESS will 
already catch any LORs, and if they are acquiring a rarely-contested lock 
then they aren't going to back up traffic in the common case.

John Baldwin

More information about the freebsd-arch mailing list