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

Roland Dreier (roland@topspin.com)
11 Nov 2002 10:23:05 -0800


>>>>> "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
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/