1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Racing Timers: A Kernel Pastime</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="common-problems.html" title="Chapter 8. Common Problems"><link rel="prev" href="techs-deadlock-prevent.html" title="Preventing Deadlock"><link rel="next" href="Efficiency.html" title="Chapter 9. Locking Speed"></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">Racing Timers: A Kernel Pastime</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="techs-deadlock-prevent.html">Prev</a> </td><th width="60%" align="center">Chapter 8. Common Problems</th><td width="20%" align="right"> <a accesskey="n" href="Efficiency.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="racing-timers"></a>Racing Timers: A Kernel Pastime</h2></div></div></div><p> 2 Timers can produce their own special problems with races. 3 Consider a collection of objects (list, hash, etc) where each 4 object has a timer which is due to destroy it. 5 </p><p> 6 If you want to destroy the entire collection (say on module 7 removal), you might do the following: 8 </p><pre class="programlisting"> 9 /* THIS CODE BAD BAD BAD BAD: IF IT WAS ANY WORSE IT WOULD USE 10 HUNGARIAN NOTATION */ 11 spin_lock_bh(&list_lock); 12 13 while (list) { 14 struct foo *next = list->next; 15 del_timer(&list->timer); 16 kfree(list); 17 list = next; 18 } 19 20 spin_unlock_bh(&list_lock); 21 </pre><p> 22 Sooner or later, this will crash on SMP, because a timer can 23 have just gone off before the <code class="function">spin_lock_bh()</code>, 24 and it will only get the lock after we 25 <code class="function">spin_unlock_bh()</code>, and then try to free 26 the element (which has already been freed!). 27 </p><p> 28 This can be avoided by checking the result of 29 <code class="function">del_timer()</code>: if it returns 30 <span class="returnvalue">1</span>, the timer has been deleted. 31 If <span class="returnvalue">0</span>, it means (in this 32 case) that it is currently running, so we can do: 33 </p><pre class="programlisting"> 34 retry: 35 spin_lock_bh(&list_lock); 36 37 while (list) { 38 struct foo *next = list->next; 39 if (!del_timer(&list->timer)) { 40 /* Give timer a chance to delete this */ 41 spin_unlock_bh(&list_lock); 42 goto retry; 43 } 44 kfree(list); 45 list = next; 46 } 47 48 spin_unlock_bh(&list_lock); 49 </pre><p> 50 Another common problem is deleting timers which restart 51 themselves (by calling <code class="function">add_timer()</code> at the end 52 of their timer function). Because this is a fairly common case 53 which is prone to races, you should use <code class="function">del_timer_sync()</code> 54 (<code class="filename">include/linux/timer.h</code>) 55 to handle this case. It returns the number of times the timer 56 had to be deleted before we finally stopped it from adding itself back 57 in. 58 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="techs-deadlock-prevent.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="common-problems.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="Efficiency.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Preventing Deadlock </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 9. Locking Speed</td></tr></table></div></body></html> 59