On Thu, Mar 4, 2010 at 2:16 AM, Frederic Weisbecker <fweisbec@xxxxxxxxx> wrote: > 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. Hi, Thanks for the clarification. That is what I had suspected too and wanted to clarify. > > The best is to allocate before you take the spinlock. Yes, that is what I ended coding up. Thanks again - Manish > > -- Thanks - Manish ================================== [$\*.^ -- I miss being one of them ================================== -- To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx Please read the FAQ at http://kernelnewbies.org/FAQ