CINXE.COM
LKML: Thomas Gleixner: [patch V3 16/18] posix-timers: Dont iterate /proc/$PID/timers with sighand:: Siglock held
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!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=UTF-8" /><title>LKML: Thomas Gleixner: [patch V3 16/18] posix-timers: Dont iterate /proc/$PID/timers with sighand:: Siglock held</title><link href="/css/message.css" rel="stylesheet" type="text/css" /><link href="/css/wrap.css" rel="alternate stylesheet" type="text/css" title="wrap" /><link href="/css/nowrap.css" rel="stylesheet" type="text/css" title="nowrap" /><link href="/favicon.ico" rel="shortcut icon" /><script src="/js/simple-calendar.js" type="text/javascript"></script><script src="/js/styleswitcher.js" type="text/javascript"></script><link rel="alternate" type="application/rss+xml" title="lkml.org : last 100 messages" href="/rss.php" /><link rel="alternate" type="application/rss+xml" title="lkml.org : last messages by Thomas Gleixner" href="/groupie.php?aid=" /><!--Matomo--><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(["setDoNotTrack", true]); _paq.push(["disableCookies"]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//m.lkml.org/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!--End Matomo Code--></head><body onload="es.jasper.simpleCalendar.init();" itemscope="itemscope" itemtype="http://schema.org/BlogPosting"><table border="0" cellpadding="0" cellspacing="0"><tr><td width="180" align="center"><a href="/"><img style="border:0;width:135px;height:32px" src="/images/toprowlk.gif" alt="lkml.org" /></a></td><td width="32">聽</td><td class="nb"><div><a class="nb" href="/lkml"> [lkml]</a> 聽 <a class="nb" href="/lkml/2025"> [2025]</a> 聽 <a class="nb" href="/lkml/2025/3"> [Mar]</a> 聽 <a class="nb" href="/lkml/2025/3/8"> [8]</a> 聽 <a class="nb" href="/lkml/last100"> [last100]</a> 聽 <a href="/rss.php"><img src="/images/rss-or.gif" border="0" alt="RSS Feed" /></a></div><div>Views: <a href="#" class="nowrap" onclick="setActiveStyleSheet('wrap');return false;">[wrap]</a><a href="#" class="wrap" onclick="setActiveStyleSheet('nowrap');return false;">[no wrap]</a> 聽 <a class="nb" href="/lkml/mheaders/2025/3/8/443" onclick="this.href='/lkml/headers'+'/2025/3/8/443';">[headers]</a>聽 <a href="/lkml/bounce/2025/3/8/443">[forward]</a>聽 </div></td><td width="32">聽</td></tr><tr><td valign="top"><div class="es-jasper-simpleCalendar" baseurl="/lkml/"></div><div class="threadlist">Messages in this thread</div><ul class="threadlist"><li class="root"><a href="/lkml/2025/3/8/426">First message in thread</a></li><li><a href="/lkml/2025/3/8/426">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/8/427">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/8/616">Frederic Weisbecker</a></li><li><a href="/lkml/2025/3/13/634">"tip-bot2 for Thomas Gleixner"</a></li></ul></li><li><a href="/lkml/2025/3/8/428">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/11/914">Frederic Weisbecker</a><ul><li><a href="/lkml/2025/3/11/977">Thomas Gleixner</a></li></ul></li><li><a href="/lkml/2025/3/13/633">"tip-bot2 for Eric Dumazet"</a></li></ul></li><li><a href="/lkml/2025/3/8/430">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/13/631">"tip-bot2 for Eric Dumazet"</a></li></ul></li><li><a href="/lkml/2025/3/8/431">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/13/630">"tip-bot2 for Thomas Gleixner"</a></li></ul></li><li><a href="/lkml/2025/3/8/432">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/13/632">"tip-bot2 for Thomas Gleixner"</a></li></ul></li><li><a href="/lkml/2025/3/8/433">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/13/629">"tip-bot2 for Thomas Gleixner"</a></li></ul></li><li><a href="/lkml/2025/3/8/434">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/13/627">"tip-bot2 for Thomas Gleixner"</a></li></ul></li><li><a href="/lkml/2025/3/8/435">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/13/626">"tip-bot2 for Thomas Gleixner"</a></li></ul></li><li><a href="/lkml/2025/3/8/436">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/10/13">Frederic Weisbecker</a><ul><li><a href="/lkml/2025/3/10/148">Thomas Gleixner</a></li></ul></li><li><a href="/lkml/2025/3/10/256">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/13/628">"tip-bot2 for Thomas Gleixner"</a></li></ul></li></ul></li><li><a href="/lkml/2025/3/8/437">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/10/634">Frederic Weisbecker</a><ul><li><a href="/lkml/2025/3/10/1329">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/10/1664">Frederic Weisbecker</a></li></ul></li></ul></li><li><a href="/lkml/2025/3/13/625">"tip-bot2 for Peter Zijlstra"</a></li></ul></li><li><a href="/lkml/2025/3/8/438">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/10/1716">Frederic Weisbecker</a></li><li><a href="/lkml/2025/3/11/927">Frederic Weisbecker</a></li><li><a href="/lkml/2025/3/13/623">"tip-bot2 for Eric Dumazet"</a></li></ul></li><li><a href="/lkml/2025/3/8/439">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/11/932">Frederic Weisbecker</a></li><li><a href="/lkml/2025/3/13/624">"tip-bot2 for Thomas Gleixner"</a></li></ul></li><li><a href="/lkml/2025/3/8/440">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/13/622">"tip-bot2 for Thomas Gleixner"</a></li></ul></li><li><a href="/lkml/2025/3/8/441">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/11/940">Frederic Weisbecker</a></li><li><a href="/lkml/2025/3/13/621">"tip-bot2 for Thomas Gleixner"</a></li><li><a href="/lkml/2025/3/13/1624">David Laight</a></li><li><a href="/lkml/2025/3/17/208">"Nysal Jan K.A."</a></li></ul></li><li><a href="/lkml/2025/3/8/442">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/11/1110">Frederic Weisbecker</a></li><li><a href="/lkml/2025/3/13/620">"tip-bot2 for Thomas Gleixner"</a></li></ul></li><li class="origin"><a href="/lkml/2025/3/9/14">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/9/14">Cyrill Gorcunov</a></li><li><a href="/lkml/2025/3/11/1103">Frederic Weisbecker</a></li><li><a href="/lkml/2025/3/13/619">"tip-bot2 for Thomas Gleixner"</a></li></ul></li><li><a href="/lkml/2025/3/8/444">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/9/13">Cyrill Gorcunov</a></li><li><a href="/lkml/2025/3/11/1588">Frederic Weisbecker</a><ul><li><a href="/lkml/2025/3/11/1615">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/11/1618">Thomas Gleixner</a></li></ul></li><li><a href="/lkml/2025/3/12/649">Cyrill Gorcunov</a></li></ul></li></ul></li><li><a href="/lkml/2025/3/8/445">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/10/254">Thomas Gleixner</a><ul><li><a href="/lkml/2025/3/11/1599">Frederic Weisbecker</a></li><li><a href="/lkml/2025/3/13/617">"tip-bot2 for Thomas Gleixner"</a></li></ul></li></ul></li></ul></li></ul><div class="threadlist">Patch in this message</div><ul class="threadlist"><li><a href="/lkml/diff/2025/3/8/443/1">Get diff 1</a></li></ul></td><td width="32" rowspan="2" class="c" valign="top"><img src="/images/icornerl.gif" width="32" height="32" alt="/" /></td><td class="c" rowspan="2" valign="top" style="padding-top: 1em"><table><tr><td><table><tr><td class="lp">From</td><td class="rp" itemprop="author">Thomas Gleixner <></td></tr><tr><td class="lp">Subject</td><td class="rp" itemprop="name">[patch V3 16/18] posix-timers: Dont iterate /proc/$PID/timers with sighand:: Siglock held</td></tr><tr><td class="lp">Date</td><td class="rp" itemprop="datePublished">Sat, 8 Mar 2025 17:48:45 +0100 (CET)</td></tr></table></td><td></td></tr></table><pre itemprop="articleBody">The readout of /proc/$PID/timers holds sighand::siglock with interrupts<br />disabled. That is required to protect against concurrent modifications of<br />the task::signal::posix_timers list because the list is not RCU safe.<br /><br />With the conversion of the timer storage to a RCU protected hlist, this is<br />not longer required.<br /><br />The only requirement is to protect the returned entry against a concurrent<br />free, which is trivial as the timers are RCU protected.<br /><br />Removing the trylock of sighand::siglock is benign because the life time of<br />task_struct::signal is bound to the life time of the task_struct itself.<br /><br />There are two scenarios where this matters:<br /><br /> 1) The process is life and not about to be checkpointed<br /><br /> 2) The process is stopped via ptrace for checkpointing<br /><br />#1 is a racy snapshot of the armed timers and nothing can rely on it. It's<br /> not more than debug information and it has been that way before because<br /> sighand lock is dropped when the buffer is full and the restart of<br /> the iteration might find a completely different set of timers.<br /><br /> The task and therefore task::signal cannot be freed as timers_start()<br /> acquired a reference count via get_pid_task().<br /><br />#2 the process is stopped for checkpointing so nothing can delete or create<br /> timers at this point. Neither can the process exit during the traversal.<br /><br /> If CRIU fails to observe an exit in progress prior to the dissimination<br /> of the timers, then there are more severe problems to solve in the CRIU<br /> mechanics as they can't rely on posix timers being enabled in the first<br /> place.<br /><br />Therefore replace the lock acquisition with rcu_read_lock() and switch the<br />timer storage traversal over to seq_hlist_*_rcu().<br /><br />Signed-off-by: Thomas Gleixner <tglx@linutronix.de><br /><br />---<br /> fs/proc/base.c | 48 ++++++++++++++++++++----------------------------<br /> 1 file changed, 20 insertions(+), 28 deletions(-)<br /><br />--- a/fs/proc/base.c<br />+++ b/fs/proc/base.c<br />@@ -2497,11 +2497,9 @@ static const struct file_operations proc<br /> <br /> #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)<br /> struct timers_private {<br />- struct pid *pid;<br />- struct task_struct *task;<br />- struct sighand_struct *sighand;<br />- struct pid_namespace *ns;<br />- unsigned long flags;<br />+ struct pid *pid;<br />+ struct task_struct *task;<br />+ struct pid_namespace *ns;<br /> };<br /> <br /> static void *timers_start(struct seq_file *m, loff_t *pos)<br />@@ -2512,54 +2510,48 @@ static void *timers_start(struct seq_fil<br /> if (!tp->task)<br /> return ERR_PTR(-ESRCH);<br /> <br />- tp->sighand = lock_task_sighand(tp->task, &tp->flags);<br />- if (!tp->sighand)<br />- return ERR_PTR(-ESRCH);<br />-<br />- return seq_hlist_start(&tp->task->signal->posix_timers, *pos);<br />+ rcu_read_lock();<br />+ return seq_hlist_start_rcu(&tp->task->signal->posix_timers, *pos);<br /> }<br /> <br /> static void *timers_next(struct seq_file *m, void *v, loff_t *pos)<br /> {<br /> struct timers_private *tp = m->private;<br />- return seq_hlist_next(v, &tp->task->signal->posix_timers, pos);<br />+<br />+ return seq_hlist_next_rcu(v, &tp->task->signal->posix_timers, pos);<br /> }<br /> <br /> static void timers_stop(struct seq_file *m, void *v)<br /> {<br /> struct timers_private *tp = m->private;<br /> <br />- if (tp->sighand) {<br />- unlock_task_sighand(tp->task, &tp->flags);<br />- tp->sighand = NULL;<br />- }<br />-<br /> if (tp->task) {<br /> put_task_struct(tp->task);<br /> tp->task = NULL;<br />+ rcu_read_unlock();<br /> }<br /> }<br /> <br /> static int show_timer(struct seq_file *m, void *v)<br /> {<br />- struct k_itimer *timer;<br />- struct timers_private *tp = m->private;<br />- int notify;<br /> static const char * const nstr[] = {<br />- [SIGEV_SIGNAL] = "signal",<br />- [SIGEV_NONE] = "none",<br />- [SIGEV_THREAD] = "thread",<br />+ [SIGEV_SIGNAL] = "signal",<br />+ [SIGEV_NONE] = "none",<br />+ [SIGEV_THREAD] = "thread",<br /> };<br /> <br />- timer = hlist_entry((struct hlist_node *)v, struct k_itimer, list);<br />- notify = timer->it_sigev_notify;<br />+ struct k_itimer *timer = hlist_entry((struct hlist_node *)v, struct k_itimer, list);<br />+ struct timers_private *tp = m->private;<br />+ int notify = timer->it_sigev_notify;<br />+<br />+ guard(spinlock_irq)(&timer->it_lock);<br />+ if (!posixtimer_valid(timer))<br />+ return 0;<br /> <br /> seq_printf(m, "ID: %d\n", timer->it_id);<br />- seq_printf(m, "signal: %d/%px\n",<br />- timer->sigq.info.si_signo,<br />+ seq_printf(m, "signal: %d/%px\n", timer->sigq.info.si_signo,<br /> timer->sigq.info.si_value.sival_ptr);<br />- seq_printf(m, "notify: %s/%s.%d\n",<br />- nstr[notify & ~SIGEV_THREAD_ID],<br />+ seq_printf(m, "notify: %s/%s.%d\n", nstr[notify & ~SIGEV_THREAD_ID],<br /> (notify & SIGEV_THREAD_ID) ? "tid" : "pid",<br /> pid_nr_ns(timer->it_pid, tp->ns));<br /> seq_printf(m, "ClockID: %d\n", timer->it_clock);<br /><br /></pre></td><td width="32" rowspan="2" class="c" valign="top"><img src="/images/icornerr.gif" width="32" height="32" alt="\" /></td></tr><tr><td align="right" valign="bottom"> 聽 </td></tr><tr><td align="right" valign="bottom">聽</td><td class="c" valign="bottom" style="padding-bottom: 0px"><img src="/images/bcornerl.gif" width="32" height="32" alt="\" /></td><td class="c">聽</td><td class="c" valign="bottom" style="padding-bottom: 0px"><img src="/images/bcornerr.gif" width="32" height="32" alt="/" /></td></tr><tr><td align="right" valign="top" colspan="2"> 聽 </td><td class="lm">Last update: 2025-03-08 17:52 聽聽 [W:1.188 / U:1.121 seconds]<br />漏2003-2020 <a href="http://blog.jasper.es/"><span itemprop="editor">Jasper Spaans</span></a>|hosted at <a href="https://www.digitalocean.com/?refcode=9a8e99d24cf9">Digital Ocean</a> and my Meterkast|<a href="http://blog.jasper.es/categories.html#lkml-ref">Read the blog</a></td><td>聽</td></tr></table><script language="javascript" src="/js/styleswitcher.js" type="text/javascript"></script></body></html>