Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Oct 2004 14:55:16 -0400
From:      John Baldwin <jhb@FreeBSD.org>
To:        freebsd-hackers@FreeBSD.org
Cc:        Hans Petter Selasky <hselasky@c2i.net>
Subject:   Re: msleep(9) and recursed mutex
Message-ID:  <200410111455.16920.jhb@FreeBSD.org>
In-Reply-To: <20041011173042.B744@curly.tele2.no>
References:  <1097500245.416a86556c692@imp1-q.free.fr> <20041011173042.B744@curly.tele2.no>

next in thread | previous in thread | raw e-mail | index | archive | help
On Monday 11 October 2004 11:30 am, Hans Petter Selasky wrote:
> On Mon, Oct 11, 2004 at 03:10:45PM +0200, damien.bergamini@free.fr wrote:
> > msleep(9) behaves strangely with recursed mutexes.
> > In the attached code, calling foo_f() will make the kernel hang for
> > two seconds.
> > It seems like msleep does not release the mtx mutex completely but
> > simply decrement its reference count by one. When calling msleep, the
> > mtx mutex is still held which prevent the interrupt from happening.
> > msleep will then return error code EWOULDBLOCK after two seconds.
> > If I remove calls to mtx_lock/unlock from function foo_g(), it works
> > without problem but this is not a solution since foo_g() can be
> > called outside of function foo_f().
> > Of course, the mtx mutex was created with the MTX_RECURSE flag set.
> >
> > Is it an expected behaviour? In msleep(9) it is said:
> > "The msleep() function is a variation on tsleep.  The parameter mtx
> > is a mutex which will be released before sleeping and reacquired
> > before msleep() returns."
> >
> > Seems like the mutex is not *really* released if it recurses.
>
> Only the "Giant" mutex is released if it recurses. By the way there is
> a bug in msleep:
>
> mtx_lock(&Giant);
> msleep(xxx, &Giant, 0, "foo", 0);
> mtx_unlock(&Giant);

This is a bug.  Giant is a special mutex that should not be used in properly 
locked code.  That is, if you are using msleep() or condition variables you 
should be using it with your own mutexes rather than Giant.  We can add some 
assertions to ensure that Giant is not passed in and that recursive mutexes 
are not used though.  Actually, we already assert that the mutex is not 
recursed.

-- 
John Baldwin <jhb@FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
"Power Users Use the Power to Serve"  =  http://www.FreeBSD.org



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200410111455.16920.jhb>