<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:΢ÈíÑźÚ
}
--></style></head>
<body class='hmmessage'><div dir='ltr'><br>&nbsp;<BR><div id="SkyDrivePlaceholder"></div><div>&gt; From: mulyadi.santosa@gmail.com<br>&gt; Date: Sun, 17 Feb 2013 12:37:21 +0700<br>&gt; Subject: Re: spinlock deadlock<br>&gt; To: buyit@live.cn<br>&gt; CC: kernelnewbies@kernelnewbies.org<br>&gt; <br>&gt; On Fri, Feb 15, 2013 at 3:52 PM, buyitian &lt;buyit@live.cn&gt; wrote:<br>&gt; &gt; is it possible that printk cause deadlock? the path is as below:<br>&gt; &gt;<br>&gt; &gt; 1. taskA runs on CPU0, and run schedule to acqire the rq-&gt;lock.<br>&gt; &gt; 2. taskA calls printk while holding rq-&gt;lock.<br>&gt; <br>&gt; I began to get rusty on this...but, why do you grab run queue lock?<br>&gt; <br>&gt; </div><div>&nbsp;</div><div>&nbsp;&nbsp; i want to know why the authors use spinlock and semaphore like this, because the change history is very insteresting:</div><div>1. in patch 0b5e1c5255e7ee8670e077e8224e5c2281229a5b: Peter did below changes:</div><div>&nbsp;&nbsp;&nbsp; (1). in function console_trylock_for_printk():</div><blockquote style="margin-right: 0px;" dir="ltr"><blockquote style="margin-right: 0px;" dir="ltr"><div>if (!can_use_console(cpu)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;console_locked = 0;<br>-&nbsp;&nbsp;&nbsp;up(&amp;console_sem);<br>+&nbsp;&nbsp;&nbsp;wake = 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;retval = 0;<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;printk_cpu = UINT_MAX;<br>&nbsp;&nbsp;spin_unlock(&amp;logbuf_lock);<br>+&nbsp;if (wake)<br>+&nbsp;&nbsp;up(&amp;console_sem);</div></blockquote></blockquote><div>&nbsp;</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(2). in function console_unlock():</div><div>&nbsp;</div><blockquote style="margin-right: 0px;" dir="ltr"><blockquote style="margin-right: 0px;" dir="ltr"><div>-&nbsp;up(&amp;console_sem);<br>&nbsp;&nbsp;spin_unlock_irqrestore(&amp;logbuf_lock, flags);<br>+&nbsp;up(&amp;console_sem);<br>&nbsp;&nbsp;if (wake_klogd)<br>&nbsp;&nbsp;&nbsp;wake_up_klogd();<br>&nbsp;}&nbsp;</div></blockquote></blockquote><div>&nbsp;</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this patch is to prevent deadlock between rq-&gt;lock and logbuf_lock. i can understand this thanks to Rabin.</div><div>&nbsp;</div><div>2. in patch 07354eb1a74d1e1ece29f8bafe0b46e8c77a95ef, Thomas did changes as below, which reverted the change from peter, i don't know why:</div><div>&nbsp;&nbsp;&nbsp; in function console_trylock_for_printk():</div><div>&nbsp;</div><blockquote style="margin-right: 0px;" dir="ltr"><div>-&nbsp;spin_unlock(&amp;logbuf_lock);<br>&nbsp;&nbsp;if (wake)<br>&nbsp;&nbsp;&nbsp;up(&amp;console_sem);<br>+&nbsp;raw_spin_unlock(&amp;logbuf_lock);</div></blockquote><div>&nbsp;</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and this change exists in the latest kernel source code. it seems that deadlock bewteen rq-&gt;lock and logbuf_lock comes back, who can explain this, thanks.</div><div>&nbsp;</div><div>&nbsp;&nbsp;&nbsp;&nbsp; BTW: if i place printk inside schedule(), i may get rq-&gt;lock before calling printk.</div><div><br>&gt; -- <br>&gt; regards,<br>&gt; <br>&gt; Mulyadi Santosa<br>&gt; Freelance Linux trainer and consultant<br>&gt; <br>&gt; blog: the-hydra.blogspot.com<br>&gt; training: mulyaditraining.blogspot.com<br></div>                                               </div></body>
</html>