From owner-freebsd-hackers@FreeBSD.ORG Fri Dec 24 21:57:57 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 1303616A4CE for ; Fri, 24 Dec 2004 21:57:57 +0000 (GMT) Received: from fledge.watson.org (fledge.watson.org [204.156.12.50]) by mx1.FreeBSD.org (Postfix) with ESMTP id AABBF43D49 for ; Fri, 24 Dec 2004 21:57:56 +0000 (GMT) (envelope-from robert@fledge.watson.org) Received: from fledge.watson.org (localhost [127.0.0.1]) by fledge.watson.org (8.13.1/8.13.1) with ESMTP id iBOLsimi028374; Fri, 24 Dec 2004 16:54:46 -0500 (EST) (envelope-from robert@fledge.watson.org) Received: from localhost (robert@localhost)iBOLsgFW028370; Fri, 24 Dec 2004 21:54:44 GMT (envelope-from robert@fledge.watson.org) Date: Fri, 24 Dec 2004 21:54:38 +0000 (GMT) From: Robert Watson X-Sender: robert@fledge.watson.org To: Jan Engelhardt In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-hackers@freebsd.org Subject: Re: Kernel crash w/o reason 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: Fri, 24 Dec 2004 21:57:57 -0000 On Fri, 24 Dec 2004, Jan Engelhardt wrote: > >> What should I use instead? A semaphore? > > >You shouldn't have unrelated kernel threads waiting for a user > >process at all, so this sounds like a design problem, regardless > >of which mutual exclusion primitive you use. (Bear in mind that I > >haven't actually looked into what you're trying to do.) In any > >case, you can always use mutexes to implement whatever other > >synchronization mechanism you need. > > I wanted that the device can only be opened once, and holding a mutex > while it is open seemed like a simple idea. (Since mtx_trylock() will > then fail -- easy to implement.) Typically a mutex (mutex(9)) is used only for small periods of mutual exclusion in accessing a data structure, and are not "sleepable locks" in which unbounded waiting may occur by a user process in kernel. For sleepable locks, you might want to investigate an sx lock (sx(9)), or possible construct a counted locking primitive using condition variables and mutexes (condvar(9)). I've not used our kernel counting semaphore implementation (sema(9)) but would assume it falls nicely into the second category and might be what you are looking for. FreeBSD kernel locking primitives are typically designed not to be held over potentially unbounded periods, such as "return to userspace", and only some are intended for use in semi-bounded periods (i.e., "wait on disk seek"). Most device drivers rely on a mutex and an internal access counter or exclusive bit, FYI. Robert N M Watson FreeBSD Core Team, TrustedBSD Projects robert@fledge.watson.org Principal Research Scientist, McAfee Research