Three questions about FreeBSD kernel internals
John Baldwin
jhb at freebsd.org
Mon Jan 14 06:50:32 PST 2008
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
More information about the freebsd-hackers
mailing list