title: hrtimer repeating task in the Linux kernel
body: <p>My goal is to create a recurring task in the linux kernel using the <code>hrtimer</code> struct. I would like it to recur every 500 ms.</p>
  
  <p>However, I'm a little confused about how <code>hrtimer</code> works in the linux kernel (see <code>linux/hrtimer.h</code>). I know that the time is specified, and the callback should return either <code>HRTIMER_RESTART</code> or <code>HRTIMER_NORESTART</code>. I've found some sources online that state that the timer needs to be reset in the callback using the <code>hrtimer_forward</code> method. However, the sources I've seen are a little unclear on how adding the time works. Here's the code I have so far:</p>
  
  <pre><code>static struct hrtimer timer;
  
  static enum hrtimer_restart timer_callback(struct hrtimer *timer)
  {
      printk(KERN_ERR "Callback\n");
      //I know something needs to go here to reset the timer
      return HRTIMER_RESTART;
  }
  
  static int init_timer(void)
  {   
      ktime_t ktime;
      unsigned long delay_in_ms = 500L;
      printk(KERN_ERR "Timer being set up\n");
  
      ktime = ktime_set(0,delay_in_ms*1E6L);
      hrtimer_init(&amp;timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  
      timer.function = &amp;timer_callback;
      printk(KERN_ERR "Timer starting to fire\n");
      printk(KERN_ERR "in %ldms %ld\n", delay_in_ms, jiffies);
  
      hrtimer_start(&amp;timer, ktime, HRTIMER_MODE_REL);
      return 0;
  }
  
  static void clean_load_balancing_timer(void)
  {
      int cancelled = hrtimer_cancel(&amp;timer);
  
      if (cancelled)
          printk(KERN_ERR "Timer still running\n");
      else
          printk(KERN_ERR "Timer cancelled\n");
  }
  </code></pre>
  
  <p>Can someone explain exactly how resetting the timer would work in the callback function? Thanks!</p>