2002-11-11 23:13:02

by Olaf Dietsche

[permalink] [raw]
Subject: Re: programming for preemption (was: [PATCH] 2.5.46: accesspermission filesystem)

Andrew Morton <[email protected]> writes:

> Olaf Dietsche wrote:
>>
>> Thanks for this hint. So this means kmalloc(GFP_KERNEL) inside
>> spinlock is not necessarily dangerous, but should be avoided if
>> possible?
>
> It can lock an SMP kernel up. This CPU can switch to another task in the
> page allocator and then, within the context of the new task, come around
> and try to take the same lock.

Alright, this means kmalloc(GFP_KERNEL) inside spinlock is a bug.

>> Is using a semaphore better than using spinlocks?

Andrew Morton <[email protected]> writes:

> A semaphore won't have that problem. If your CPU comes around again onto
> the already-held lock it will just switch to another task.

Roland Dreier <[email protected]> writes:

> 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)

So, as a rule of thumb, I would say use semaphores, if you need some
locking. And in interrupt context, use spinlocks. Do spinlocks have
other benefits, beside being interrupt safe?

Regards, Olaf.