title: printk interrupt disabling and locking
body: <p>I have a question regarding the implementation of printk() in the 3.10 kernel. I see it <a href="http://lxr.free-electrons.com/source/kernel/printk.c?v=3.10#L1513/%22calls%22" rel="nofollow">calls</a> <code>local_irq_save</code> at the beginning. I see it then <a href="http://lxr.free-electrons.com/source/kernel/printk.c?v=3.10#L1535/%22calls%22" rel="nofollow">calls</a> <code>raw_spin_lock(&amp;logbuf_lock)</code>. What's the purpose of logbuf_lock if interrupts have been already disabled prior to this? Is it because even though interrupts are disabled on the current CPU other CPUs can still call printk so they need to be stopped from writing to the log buffer?</p>
  
  <p>Basically I have three questions:</p>
  
  <ol>
  <li><p>I see printk acquires logbuf_lock and writes to the log buffer and then tries to grab the console semaphore and releases logbuf_lock. Then inside <code>console_unlock</code> inside a loop it acquires logbuf_lock and disables interrupts, then releases logbuf_lock and calls the console drivers, and then restores interrupts. What's the purpose of this locking/disabling interrupt sequence?</p></li>
  <li><p>I see comments in printk() about the log buffer possibly being filled up again so the buffer may have to get flushed to the console again. How would this situation occur given all the locking I asked about in #1 above?</p></li>
  <li><p>If code on only 1 CPU were to be calling printk() at any given moment, could interrupts still be handled on other cores in an SMP system? I'm trying to also understand the impact of printk on interrupt latency.</p></li>
  </ol>
  
  <p>Thanks.</p>
  
  <p>A few follow-ups:</p>
  
  <p>Can you clarify this: </p>
  
  <blockquote>
    <p><code>local_irq_save()</code>protects against interrupts on the local CPU (and also avoids being rescheduled on another CPU while using a cpu variable to access per-CPU data)</p>
  </blockquote>
  
  <p>Do you mean calling <code>local_irq_save()</code> will prevent the current thread from being rescheduled on another CPU only if it accesses per-CPU data or it prevents the current thread from being rescheduled on another CPU period?  What is the purpose of local_irq_save() in the case of printk() here? I remember reading a thread on LMKL that said the disabling of interrupts was to ensure the order of entries in the log buffer reflected the actual order in which the printk() calls occurred.</p>