Best way to handle ERESTARTSYS in kthread?

456 views Asked by At

I have a kthread that blocks on a wait event until a timeout is reached. However, if a system event interrupts it (such as the system going into suspend), it returns -ERESTARTSYS. I'm curious to know how to handle this situation, since I don't want the kthread to die, just wait until the system-wide event is handled (such as suspend).

    while (!kthread_should_stop()) {
        const int timeout_ms = 30000;

        rc = wait_event_interruptible_timeout(_wq,
            kthread_should_stop(),
            msecs_to_jiffies(timeout_ms));

        /* How to handle -ERESTARTSYS??? */
        if (rc)
            return rc; /* maybe return it?? */

        if (kthread_should_stop())
            break;

        /* do processing ... */
    }

If I was to return ERESTARTSYS from the kthread, would the kernel restart the thread? Should I put the wait_event_interruptible_timeout into a tight loop that only exists when it returns 0?

1

There are 1 answers

2
Tsyvarev On BEST ANSWER

If your thread is not freezable(by default), it will be suspended/hibernated automatically by the kernel core. So, you can just repeat waiting(physically, it will be repeated after resuming).

If you mark your thread freezable, in case of such signal you should either call try_to_freeze() manually or use wait_event_freezable_timeout macro, which call it automatically(but you should also check for this signal and restart waiting).

For more information about freezing kthreads see Documentation/power/freezing-of-tasks.txt in the kernel sources.