Date: Mon, 14 Jan 2008 08:23:43 -0500 From: John Baldwin <jhb@freebsd.org> To: freebsd-hackers@freebsd.org Cc: Ed Schouten <ed@fxq.nl> Subject: Re: Three questions about FreeBSD kernel internals Message-ID: <200801140832.32347.jhb@freebsd.org> In-Reply-To: <20080113160127.GF80300@hoeg.nl> References: <20080113160127.GF80300@hoeg.nl>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200801140832.32347.jhb>