How to use hrtimer if the processing time of the callback function is dynamic?
<p>I am writing a kernel module in which I need to trigger a function on a periodic basis. The function will access a queue and process its elements. The number of elements in the queue is dynamic and so the processing time. </p>

<p>In the following code, I have added 1ms sleep to represent the processing time. I am getting this error : <code>[116588.117966] BUG: scheduling while atomic: systemd-journal/408/0x00010000</code>. If my understanding is correct, this happens since I try to sleep 1ms when the expiry time of the hr_timer is just 1us. I can increase this expiry time but the processing time of the queue can be sometimes more than seconds, sometimes in hours even. Please help me to achieve this.</p>

<pre><code>unsigned long timer_interval_ns = 1e3;
static struct hrtimer hr_timer;

enum hrtimer_restart timer_callback( struct hrtimer *timer_for_restart )
{
    uint64_t rawtime;
    struct timespec curtime;
    ktime_t currtime , interval;

    / * My Operations would take ~ 1ms, so adding 1ms for simplicity* /
    msleep(1);

    currtime = ktime_get();
    interval = ktime_set(0,timer_interval_ns);

    hrtimer_forward(timer_for_restart, currtime, interval);

    return HRTIMER_RESTART;
}

static int __init timer_init(void) {
    ktime_t ktime = ktime_set( 0, timer_interval_ns );
    hrtimer_init( &amp;hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
    hr_timer.function = &amp;timer_callback;
    hrtimer_start( &amp;hr_timer, ktime, HRTIMER_MODE_REL );
    return 0;
}
</code></pre>