On Tue, Mar 02, 2010 at 09:32:16AM +0530, Manish Katiyar wrote:
> Hello all,
> 
> Is the following code valid ???
> 
> foo() {
> ...........
> spin_lock(lock);
> ptr = kzalloc(size, GFP_KERNEL);
> spin_unlock(lock);
> .....
> }



You can't because GFP_KERNEL involves the fact
kzalloc might sleep while looking for memory
somewhere (swapping out pages to get some free
rooms).

And sleeping while holding a spinlock may
lead to a deadlock:

Task A holds spinlock in cpu0. Task A sleeps.
Task B is scheduled in cpu0, tries to take the
spinlock and then deadlock for ever.

You can use GFP_ATOMIC instead, but it's usually
not recommended as the GFP_ATOMIC pool is a pretty
limited resource.

The best is to allocate before you take the spinlock.


--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to [email protected]
Please read the FAQ at http://kernelnewbies.org/FAQ

Reply via email to