Re: programming for preemption (was: [PATCH] 2.5.46: access permission filesystem)

Roland Dreier
2002-11-11T18:16:18Z

>>>>> "Olaf" == Olaf Dietsche <olaf.dietsche#list.linux-kernel@t-online.de> writes:

Olaf> So this means kmalloc(GFP_KERNEL) inside spinlock is not
Olaf> necessarily dangerous, but should be avoided if possible? Is
Olaf> using a semaphore better than using spinlocks?

You should never kmalloc(GFP_KERNEL) while holding a spinlock, since
it is dangerous even without preempt. kmalloc(GFP_KERNEL) may sleep,
which will lead to deadlock (the code holding the spinlock gets
scheduled out because of the kmalloc, then some other code tries to
take the lock -- deadlock).

A semaphore is safer, because if you fail to get the semaphore you
will go to sleep, which allows the process that holds the semaphore to
get scheduled again and release it. However you cannot use semaphores
in interrupt handlers -- you must be in process context when you
down() the semaphore. (Note that it is OK to up() a semaphore from an
interrupt handler)

Best,
Roland