From owner-freebsd-hackers@FreeBSD.ORG Mon Oct 11 21:27:24 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 318EF16A4CE for ; Mon, 11 Oct 2004 21:27:24 +0000 (GMT) Received: from mail1.speakeasy.net (mail1.speakeasy.net [216.254.0.201]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0A19743D3F for ; Mon, 11 Oct 2004 21:27:24 +0000 (GMT) (envelope-from jhb@FreeBSD.org) Received: (qmail 20750 invoked from network); 11 Oct 2004 21:27:23 -0000 Received: from dsl027-160-063.atl1.dsl.speakeasy.net (HELO server.baldwin.cx) ([216.27.160.63]) (envelope-sender ) encrypted SMTP for ; 11 Oct 2004 21:27:23 -0000 Received: from [10.50.40.210] (gw1.twc.weather.com [216.133.140.1]) (authenticated bits=0) by server.baldwin.cx (8.12.11/8.12.11) with ESMTP id i9BLRH6P042482; Mon, 11 Oct 2004 17:27:19 -0400 (EDT) (envelope-from jhb@FreeBSD.org) From: John Baldwin To: freebsd-hackers@FreeBSD.org Date: Mon, 11 Oct 2004 14:57:17 -0400 User-Agent: KMail/1.6.2 References: <1097500245.416a86556c692@imp1-q.free.fr> In-Reply-To: <1097500245.416a86556c692@imp1-q.free.fr> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200410111457.17529.jhb@FreeBSD.org> X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on server.baldwin.cx cc: damien.bergamini@free.fr Subject: Re: msleep(9) and recursed mutex X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Oct 2004 21:27:24 -0000 On Monday 11 October 2004 09:10 am, 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. This is a feature. If you had INVARIANTS on you would get a panic here: if (mtx != NULL) { mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED); WITNESS_SAVE(&mtx->mtx_object, mtx); mtx_unlock(mtx); } When developing kernel code it is highly recommended that you run with INVARIANTS and INVARIANT_SUPPORT turned on. You should probably have your foo_g() function assert that the lock is held and fix all the callers. Either that or add a wrapper around foo_g() for the callers that don't call with the mutex locked. -- John Baldwin <>< http://www.FreeBSD.org/~jhb/ "Power Users Use the Power to Serve" = http://www.FreeBSD.org