<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><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.75.2" /><link rel="home" href="index.html" title="Unreliable Guide To Locking" /><link rel="up" href="ch08.html" title="Chapter 8. Common Problems" /><link rel="prev" href="ch08s02.html" title="Preventing Deadlock" /><link rel="next" href="ch09.html" title="Chapter 9. Locking Speed" /></head><body><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="ch08s02.html">Prev</a> </td><th width="60%" align="center">Chapter 8. Common Problems</th><td width="20%" align="right"> <a accesskey="n" href="ch09.html">Next</a></td></tr></table><hr /></div><div class="sect1" title="Racing Timers: A Kernel Pastime"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="racing-timers"></a>Racing Timers: A Kernel Pastime</h2></div></div></div><p> Timers can produce their own special problems with races. Consider a collection of objects (list, hash, etc) where each object has a timer which is due to destroy it. </p><p> If you want to destroy the entire collection (say on module removal), you might do the following: </p><pre class="programlisting"> /* THIS CODE BAD BAD BAD BAD: IF IT WAS ANY WORSE IT WOULD USE HUNGARIAN NOTATION */ spin_lock_bh(&list_lock); while (list) { struct foo *next = list->next; del_timer(&list->timer); kfree(list); list = next; } spin_unlock_bh(&list_lock); </pre><p> Sooner or later, this will crash on SMP, because a timer can have just gone off before the <code class="function">spin_lock_bh()</code>, and it will only get the lock after we <code class="function">spin_unlock_bh()</code>, and then try to free the element (which has already been freed!). </p><p> This can be avoided by checking the result of <code class="function">del_timer()</code>: if it returns <span class="returnvalue">1</span>, the timer has been deleted. If <span class="returnvalue">0</span>, it means (in this case) that it is currently running, so we can do: </p><pre class="programlisting"> retry: spin_lock_bh(&list_lock); while (list) { struct foo *next = list->next; if (!del_timer(&list->timer)) { /* Give timer a chance to delete this */ spin_unlock_bh(&list_lock); goto retry; } kfree(list); list = next; } spin_unlock_bh(&list_lock); </pre><p> Another common problem is deleting timers which restart themselves (by calling <code class="function">add_timer()</code> at the end of their timer function). Because this is a fairly common case which is prone to races, you should use <code class="function">del_timer_sync()</code> (<code class="filename">include/linux/timer.h</code>) to handle this case. It returns the number of times the timer had to be deleted before we finally stopped it from adding itself back in. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch08s02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch08.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch09.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>