Re: Allocating memory with spinlock held

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux