#include <linux/module.h>
#include <linux/workqueue.h>
#include <linux/wait.h>
#include <linux/atomic.h>
#include <linux/sched.h>
static DECLARE_WAIT_QUEUE_HEAD(stall_wq_head);
static atomic_t wake_condition = ATOMIC_INIT(0);
static struct work_struct stall_work1;
static struct work_struct stall_work2;
static void stall_work2_fn(struct work_struct *work)
{
pr_info("wq_stall: second work item finally ran\n");
}
static void stall_work1_fn(struct work_struct *work)
{
pr_info("wq_stall: first work item running on cpu %d\n",
raw_smp_processor_id());
schedule_work(&stall_work2);
current->flags &= ~PF_WQ_WORKER;
pr_info("wq_stall: entering wait_event_idle (PF_WQ_WORKER cleared)\n");
pr_info("wq_stall: expect 'BUG: workqueue lockup' in ~30-60s\n");
wait_event_idle(stall_wq_head, atomic_read(&wake_condition) != 0);
current->flags |= PF_WQ_WORKER;
pr_info("wq_stall: woke up, PF_WQ_WORKER restored\n");
}
static int __init wq_stall_init(void)
{
pr_info("wq_stall: loading\n");
INIT_WORK(&stall_work1, stall_work1_fn);
INIT_WORK(&stall_work2, stall_work2_fn);
schedule_work(&stall_work1);
return 0;
}
static void __exit wq_stall_exit(void)
{
pr_info("wq_stall: unloading\n");
atomic_set(&wake_condition, 1);
wake_up(&stall_wq_head);
flush_work(&stall_work1);
flush_work(&stall_work2);
pr_info("wq_stall: all work flushed, module unloaded\n");
}
module_init(wq_stall_init);
module_exit(wq_stall_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Reproduce workqueue stall caused by PF_WQ_WORKER misuse");
MODULE_AUTHOR("Breno Leitao <leitao@debian.org>");