Re: Allocating memory with spinlock held

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

 



El Wed, Mar 03, 2010 at 11:14:35AM +0530 Manish Katiyar ha dit:

> Hello all,
> 
> Is the following code valid ???
> 
> foo() {
> ...........
> spin_lock(lock);
> ptr = kzalloc(size, GFP_KERNEL);
> spin_unlock(lock);
> .....
> }
> 
> 
> I guess the answer is no, but would like to confirm. Detailed/all
> possible reasons for not doing so are welcome. IIRC, I was getting a
> kernel crash with similar code with error message as "spinlock bug :
> unlocked on wrong CPU" which looked like it was getting locked on one
> cpu and due to memory allocation was getting unlocked from another
> cpu.

preemption is disabled when a spinlock is held. memory allocation can
cause the current process to be scheduled out if the requested amount
of memory isn't available immediately, but preemption is disabled due
to the spinlock.

if you *really* need to allocate memory while holding the spinlock you
must use GFP_ATOMIC instead of GFP_KERNEL, but remember that the
allocation can fail and thus you must check the return value

best regards

-- 
Matthias Kaehlcke
Embedded Linux Developer
Barcelona

               For to be free is not merely to cast off
               one's chains, but to live in a way that
              respects and enhances the freedom of others
                           (Nelson Mandela)
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-

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