From: "Sorin Manolache" <sorinm@gmail.com>
To: linux-kernel@vger.kernel.org
Subject: newbie questions about while (1) in kernel mode and spinlocks
Date: Thu, 21 Dec 2006 10:41:44 +0100 [thread overview]
Message-ID: <20170a030612210141y6578602eo525e6df5f324747d@mail.gmail.com> (raw)
Dear list,
I am in the process of learning how to write linux device drivers.
I have a 2.6.16.5 kernel running on a monoprocessor machine.
#CONFIG_SMP is not set
CONFIG_DEBUG_SPINLOCK=y.
CONFIG_PREEMPT=y
CONFIG_PREEMPT_BKL=y
First question:
I wrote
while (1)
;
in the read function of a test device driver. I expect the calling
process to freeze, and then a timer interrupt to preempt the kernel
and to schedule another process. This does not happen, the whole
system freezes. I see no effect from pressing keys or moving the
mouse. Why? The hardware interrupts are not disabled, are they? Why do
the interrupt handlers not get executed?
Second question:
I wrote
spin_lock(&lck);
down(&sem); /* I know that one shouldn't sleep when holding a lock */
/* but I want to understand why */
spin_unlock(&lck);
in the read function and
up(&sem)
in the write function. The semaphore is initially locked, so the first
process invoking down will sleep.
I invoke
cat /dev/test
and the process sleeps on the semaphore. Then I invoke
echo 1 > /dev/test
and I wake up the "cat" process.
Then I intend to invoke _two_ cat processes. I expect the first one to
sleep on the semaphore and the second on to spin at the spin_lock.
Then I expect to wake up the first process by invoking an echo, the
first process to release the lock and the second process to sleep on
the semaphore. What I get is that the system freezes as soon as I
invoke the second "cat" process. Again, no effect from key presses or
mouse movements. Why? Shouldn't the timer interrupt preempt the second
"cat" process that spins on the spinlock and give control to something
else, for example to the console where I could wake up the first "cat"
process? Why do I not see any effect from mouse movements? Hardware
interrupts are not disabled, are they?
Third question:
The Linux Device Drivers book says that a spin_lock should not be
shared between a process and an interrupt handler. The explanation is
that the process may hold the lock, an interrupt occurs, the interrupt
handler spins on the lock held by the process and the system freezes.
Why should it freeze? Isn't it possible for the interrupt handler to
re-enable interrupts as its first thing, then to spin at the lock, the
timer interrupt to preempt the interrupt handler and to relinquish
control to the process which in turn will finish its critical section
and release the lock, making way for the interrupt handler to
continue.
Thank you very much for clarifying these issues.
Regards,
Sorin
next reply other threads:[~2006-12-21 9:41 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-12-21 9:41 Sorin Manolache [this message]
2006-12-21 10:05 ` Paolo Ornati
2006-12-21 10:20 ` Paolo Ornati
2006-12-21 10:40 ` Paolo Ornati
2006-12-21 23:54 ` Steven Rostedt
[not found] <fa.D8ff1OmLNpVeVOoaJAP7ENpm+Wk@ifi.uio.no>
2006-12-21 14:31 ` Robert Hancock
2006-12-21 18:14 SR, Krishna
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170a030612210141y6578602eo525e6df5f324747d@mail.gmail.com \
--to=sorinm@gmail.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®