title: Avoiding sleep while holding a spinlock
body: <p>I've recently read section <a href="http://www.makelinux.net/ldd3/chp-5-sect-5.shtml" rel="noreferrer">5.5.2</a> (Spinlocks and Atomic Context) of LDDv3 book:</p>
  
  <blockquote>
    <p>Avoiding sleep while holding a lock can be more difficult; many kernel functions can sleep, and this behavior is <strong>not always well documented</strong>. Copying data to or from user space is an obvious example: the required user-space page may need to be swapped in from the disk before the copy can proceed, and that operation clearly requires a sleep. Just about any operation that must allocate memory can sleep; kmalloc can decide to give up the processor, and wait for more memory to become available unless it is explicitly told not to. Sleeps can happen in surprising places; writing code that will execute under a spinlock <strong>requires paying attention to every function that you call</strong>.</p>
  </blockquote>
  
  <p>It's clear to me that spinlocks must always be held for the minimum time possible and I think that it's relatively easy to write correct spinlock-using code from scratch.</p>
  
  <p>Suppose, however, that we have a big project where spinlocks are widely used.
  How can we make sure that functions called from critical sections protected by spinlocks will never sleep? </p>
  
  <p>Thanks in advance!</p>