1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter 4. Hard IRQ Context</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Unreliable Guide To Locking"><link rel="up" href="index.html" title="Unreliable Guide To Locking"><link rel="prev" href="lock-softirqs.html" title="Locking Between Softirqs"><link rel="next" href="hardirq-hardirq.html" title="Locking Between Two Hard IRQ Handlers"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 4. Hard IRQ Context</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="lock-softirqs.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="hardirq-hardirq.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="hardirq-context"></a>Chapter 4. Hard IRQ Context</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="sect1"><a href="hardirq-context.html#hardirq-softirq">Locking Between Hard IRQ and Softirqs/Tasklets</a></span></dt><dt><span class="sect1"><a href="hardirq-hardirq.html">Locking Between Two Hard IRQ Handlers</a></span></dt></dl></div><p> 2 Hardware interrupts usually communicate with a 3 tasklet or softirq. Frequently this involves putting work in a 4 queue, which the softirq will take out. 5 </p><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="hardirq-softirq"></a>Locking Between Hard IRQ and Softirqs/Tasklets</h2></div></div></div><p> 6 If a hardware irq handler shares data with a softirq, you have 7 two concerns. Firstly, the softirq processing can be 8 interrupted by a hardware interrupt, and secondly, the 9 critical region could be entered by a hardware interrupt on 10 another CPU. This is where <code class="function">spin_lock_irq()</code> is 11 used. It is defined to disable interrupts on that cpu, then grab 12 the lock. <code class="function">spin_unlock_irq()</code> does the reverse. 13 </p><p> 14 The irq handler does not to use 15 <code class="function">spin_lock_irq()</code>, because the softirq cannot 16 run while the irq handler is running: it can use 17 <code class="function">spin_lock()</code>, which is slightly faster. The 18 only exception would be if a different hardware irq handler uses 19 the same lock: <code class="function">spin_lock_irq()</code> will stop 20 that from interrupting us. 21 </p><p> 22 This works perfectly for UP as well: the spin lock vanishes, 23 and this macro simply becomes <code class="function">local_irq_disable()</code> 24 (<code class="filename">include/asm/smp.h</code>), which 25 protects you from the softirq/tasklet/BH being run. 26 </p><p> 27 <code class="function">spin_lock_irqsave()</code> 28 (<code class="filename">include/linux/spinlock.h</code>) is a variant 29 which saves whether interrupts were on or off in a flags word, 30 which is passed to <code class="function">spin_unlock_irqrestore()</code>. This 31 means that the same code can be used inside an hard irq handler (where 32 interrupts are already off) and in softirqs (where the irq 33 disabling is required). 34 </p><p> 35 Note that softirqs (and hence tasklets and timers) are run on 36 return from hardware interrupts, so 37 <code class="function">spin_lock_irq()</code> also stops these. In that 38 sense, <code class="function">spin_lock_irqsave()</code> is the most 39 general and powerful locking function. 40 </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="lock-softirqs.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="hardirq-hardirq.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Locking Between Softirqs </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Locking Between Two Hard IRQ Handlers</td></tr></table></div></body></html> 41