Quantcast
Channel: Active questions tagged linux-kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 12244

How to immediately cancel a work item of a workqueue in a Linux kernel module?

$
0
0

Kernel module code:

#include <linux/delay.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/workqueue.h>MODULE_LICENSE("GPL");static struct workqueue_struct *queue;static void work_func(struct work_struct *work){    int i = 0;    while (i < 5) {        printk(KERN_INFO "%d\n", i);        usleep_range(1000000, 1000001);        i++;    }}DECLARE_WORK(work, work_func);int init_module(void){    queue = create_workqueue("myworkqueue");    queue_work(queue, &work);    return 0;}void cleanup_module(void){    cancel_work_sync(&work);        destroy_workqueue(queue);}

If I do:

insmod mymod.kormmod mymod

rmmod hangs on cancel_work_sync, which first waits for the work to finish, until the counting is over.

Is it possible to immediately cancel that work item?

Minimal runnable example here.

Tested in Linux kernel 4.9.


Viewing all articles
Browse latest Browse all 12244

Trending Articles