0

I create a kernel thread during the initialization of a kernel module like this:

static int __init module_init(module)
{
    ...

    tdata = kmalloc(sizeof(*data));
    if (!data) {
        ...
    }

    task = kthread_run(tfunc, tdata, "memtester");
    if (!task) {
        pr_err("memtester: Could not create new thread\n");
        goto out_err
    }

    ...
}

I unload the module like this:

static void __exit memtester_exit(void)
{
    printk(KERN_INFO "memtester exiting");
    if (task)
        kthread_stop(task);
    kfree(tdata);
}

and the thread function is like this:

int tfunc(void *data)
{
    ...
    for (i = 0; i < some_limit; ++i) {
        do_work_a();

        /* Here I need to wait for delay usec */
        for (j = 0; j < delay / 1000; j++) {
            if (kthread_should_stop())
                goto out

            udelay(1000)

            if (kthread_should_stop())
                goto out
        }

        do_work_b();
    }
out:
    do_exit(0)
}

In the thread function I need to do something at do_work_a(), wait for delay usec and then do something else in do_work_b(). My problem is that if I want to remove the module while the thread is running, the system hangs.

I expected that when I unload the module and kthread_stop() executes then at some point the thread will execute kthread_should_stop() and break the loop. At the same time kthread_stop() would block until the thread exits.

If I leave the thread to finish its loop and exit by itself everything works like a charm. If I try to unload the module though, while the thread runs the whole system hangs.

do_work_{a,b}() is guaranteed to return.

Can anyone notice what am I missing?

  • Are `do_work_a` or `do_work_b` execute some blocking call (acquiring mutex, `wait_event`, etc.)? `kthead_stop()` can [interrupt waiting](http://stackoverflow.com/questions/35296447/how-to-kill-a-wait-queue-in-kernel-module/35299216#35299216). Also, if you use kthread_stop, you should [ensure that thread is not terminated at this call](http://stackoverflow.com/a/32932087/3440745). Possible way for waiting in thread until `kthread_stop` is called see [here](http://stackoverflow.com/a/11516927/3440745). – Tsyvarev Feb 15 '16 at 16:56
  • `do_work_a` and `do_work_b` do not block. actually they're doing some memcopies. Thanks for the tip. – B. Chalios Feb 15 '16 at 17:40
  • **Statements within the inner for-loop will NOT be executed even once if delay < 1000**. Thus attempting to remove the module could hang the system until the outer for-loop executes `some_limit` iterations. Depending upon how the `do_work_*()` functions are implemented and the rest of their logical dependencies in the kernel, this could be a livelock or deadlock. – TheCodeArtist Feb 20 '16 at 17:09

0 Answers0