title: Can two hrtimer callbacks in linux kernel run at the same time?
body: <p>As stated in <a href="https://lwn.net/Articles/308545/" rel="nofollow">https://lwn.net/Articles/308545/</a> <br> hrtimer callbacks run in hard interrupt context with irqs disabled.<br>
  But what about a SMP?<br>
  Can a second callback for another hrtimer run on another core, 
  while a first callback is allready running or do they exclude each other on all cores, so that no locking is needed between them?<br>
  <br>
  edit:<br>
  When a handler for a "regular" hardware IRQ (let's call it X) is running on a core, all IRQs are disabled on only that core, but IRQ X is disabled on the whole system, so two handlers for X never run concurrently.<br>
  How do hrtimer interrupts behave in this regard?<br>
  Do they all share the same quasi IRQ, or is there one IRQ per hrtimer?
  <br><br>
  edit:<br>
  Did some experiments with two timers A and B:</p>
  
  <pre><code>// starting timer A to fire as fast as possible...
  A_ktime = ktime_set(0, 1); // 1 NS
  hrtimer_start( &amp;A, A_ktime, HRTIMER_MODE_REL );
  
  // starting timer B to fire 10 us later
  B_ktime = ktime_set(0, 10000); // 10 us
  hrtimer_start( &amp;B, B_ktime, HRTIMER_MODE_REL );
  </code></pre>
  
  <p>Put some printks into the callbacks and a huge delay into the one for timer A</p>
  
  <pre><code>// fired after 1 NS
  enum hrtimer_restart A(struct hrtimer *timer)
  {
      printk("timer A: %lu\n",jiffies);
      int i;
      for(i=0;i&lt;10000;i++){ // delay 10 seconds (1000 jiffies with HZ 100)
           udelay(1000);
      }
      printk("end timer A: %lu\n",jiffies);
  
      return HRTIMER_NORESTART;
  }
  
  // fired after 10 us
  enum hrtimer_restart B(struct hrtimer *timer)
  {
      printk("timer B: %lu\n",jiffies);
      return HRTIMER_NORESTART;
  }
  </code></pre>
  
  <p>Result was reproducible  something like</p>
  
  <pre><code>[    6.217393]     timer A: 4294937914
  [   16.220352] end timer A: 4294938914
  [   16.224059]     timer B: 4294938915
  </code></pre>
  
  <p>1000 jiffies after start of timer A, 
  when timer B was setup to fire after less than one jiffie after it.<br></p>
  
  <p>When driving this further and increasing the delay to 70 seconds,
  I got 7000 jiffies between start of timer A callback and timer B callback.</p>
  
  <pre><code>[    6.218258]     timer A: 4294937914
  [   76.220058] end timer A: 4294944914
  [   76.224192]     timer B: 4294944915
  </code></pre>
  
  <p>edit:<br></p>
  
  <p>Locking is probably required, because hrtimers
  just get enqueued an any CPU. If two of them are enqueued on the same, it might happen, that they delay each other, but there is no guarantee.</p>
  
  <p>from hrtimer.h:<br></p>
  
  <pre>
      * On SMP it is possible to have a "callback function running and enqueued"
      * status. It happens for example when a posix timer expired and the callback
      * queued a signal. Between dropping the lock which protects the posix timer
      * and reacquiring the base lock of the hrtimer, another CPU can deliver the
      * signal and rearm the timer.
  </pre>