From owner-freebsd-hackers@FreeBSD.ORG Mon Jan 14 14:50:32 2008 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6016B16A41B for ; Mon, 14 Jan 2008 14:50:32 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from elvis.mu.org (elvis.mu.org [192.203.228.196]) by mx1.freebsd.org (Postfix) with ESMTP id E1A5B13C455 for ; Mon, 14 Jan 2008 14:50:31 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from zion.baldwin.cx (66-23-211-162.clients.speedfactory.net [66.23.211.162]) by elvis.mu.org (Postfix) with ESMTP id B6BFF1A4D7C; Mon, 14 Jan 2008 06:29:17 -0800 (PST) From: John Baldwin To: freebsd-hackers@freebsd.org Date: Mon, 14 Jan 2008 08:23:43 -0500 User-Agent: KMail/1.9.7 References: <20080113160127.GF80300@hoeg.nl> In-Reply-To: <20080113160127.GF80300@hoeg.nl> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200801140832.32347.jhb@freebsd.org> Cc: Ed Schouten Subject: Re: Three questions about FreeBSD kernel internals X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Jan 2008 14:50:32 -0000 On Sunday 13 January 2008 11:01:27 am Ed Schouten wrote: > Hello everyone, > > I've got three small questions about some of the source code in the > FreeBSD kernel, based on some of the source code and the manpages. I > didn't send it to questions@, because of their technical nature. > > >>> Question 1: > > Sleepqueues and turnstiles are allocated on thread creation. Why can't > they be allocated when needed? Because malloc() uses locks (mutexes) and sleeping internally. What would happen if you blocked on a lock or condvar while trying to allocate a sleepq or turnstile? :) Hence, we allocate them in another thread's context during thread creation. > >>> Question 2: > > The kernel has three mechanisms to wait for an asynchronous event, > namely sema(9), condvar(9) and sleep(9). Strictly, there are only two > interfaces, because sema(9) is implemented using mutex(9) and > condvar(9). My question is: which interface is the preferred one when > writing new code? condvar(9) is my preference. If you just need a sleep delay you can use pause(9). > >>> Question 3: > > In the file tty_subr.c there is the following comment: > > | /* > | * Allocate an initial base set of cblocks as a 'slush'. > | * We allocate non-slush cblocks with each initial tty_open() and > | * deallocate them with each tty_close(). > | * We should adjust the slush allocation. This can't be done in > | * the i/o routines because they are sometimes called from > | * interrupt handlers when it may be unsafe to call malloc(). > | */ > > My question is whether the bottom three lines of the comment are still > accurate. If I believe the manpage, it's safe to call malloc() in > interrupt handlers, if you use M_NOWAIT. I'm not really familiar with > older xBSD implementations, but is it true that the kernel couldn't > allocate memory in interrupt handlers back then? M_NOWAIT is ok in normal interrupt handlers, it is not valid in INTR_FAST/ filter interrupt handlers used by some tty drivers (e.g. sio(4)). -- John Baldwin