1*9e83d510SBreno Leitao // SPDX-License-Identifier: GPL-2.0
2*9e83d510SBreno Leitao /*
3*9e83d510SBreno Leitao * wq_stall - Test module for the workqueue stall detector.
4*9e83d510SBreno Leitao *
5*9e83d510SBreno Leitao * Deliberately creates a workqueue stall so the watchdog fires and
6*9e83d510SBreno Leitao * prints diagnostic output. Useful for verifying that the stall
7*9e83d510SBreno Leitao * detector correctly identifies stuck workers and produces useful
8*9e83d510SBreno Leitao * backtraces.
9*9e83d510SBreno Leitao *
10*9e83d510SBreno Leitao * The stall is triggered by clearing PF_WQ_WORKER before sleeping,
11*9e83d510SBreno Leitao * which hides the worker from the concurrency manager. A second
12*9e83d510SBreno Leitao * work item queued on the same pool then sits in the worklist with
13*9e83d510SBreno Leitao * no worker available to process it.
14*9e83d510SBreno Leitao *
15*9e83d510SBreno Leitao * After ~30s the workqueue watchdog fires:
16*9e83d510SBreno Leitao * BUG: workqueue lockup - pool cpus=N ...
17*9e83d510SBreno Leitao *
18*9e83d510SBreno Leitao * Build:
19*9e83d510SBreno Leitao * make -C <kernel tree> M=samples/workqueue/stall_detector modules
20*9e83d510SBreno Leitao *
21*9e83d510SBreno Leitao * Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
22*9e83d510SBreno Leitao * Copyright (c) 2026 Breno Leitao <leitao@debian.org>
23*9e83d510SBreno Leitao */
24*9e83d510SBreno Leitao
25*9e83d510SBreno Leitao #include <linux/module.h>
26*9e83d510SBreno Leitao #include <linux/workqueue.h>
27*9e83d510SBreno Leitao #include <linux/wait.h>
28*9e83d510SBreno Leitao #include <linux/atomic.h>
29*9e83d510SBreno Leitao #include <linux/sched.h>
30*9e83d510SBreno Leitao
31*9e83d510SBreno Leitao static DECLARE_WAIT_QUEUE_HEAD(stall_wq_head);
32*9e83d510SBreno Leitao static atomic_t wake_condition = ATOMIC_INIT(0);
33*9e83d510SBreno Leitao static struct work_struct stall_work1;
34*9e83d510SBreno Leitao static struct work_struct stall_work2;
35*9e83d510SBreno Leitao
stall_work2_fn(struct work_struct * work)36*9e83d510SBreno Leitao static void stall_work2_fn(struct work_struct *work)
37*9e83d510SBreno Leitao {
38*9e83d510SBreno Leitao pr_info("wq_stall: second work item finally ran\n");
39*9e83d510SBreno Leitao }
40*9e83d510SBreno Leitao
stall_work1_fn(struct work_struct * work)41*9e83d510SBreno Leitao static void stall_work1_fn(struct work_struct *work)
42*9e83d510SBreno Leitao {
43*9e83d510SBreno Leitao pr_info("wq_stall: first work item running on cpu %d\n",
44*9e83d510SBreno Leitao raw_smp_processor_id());
45*9e83d510SBreno Leitao
46*9e83d510SBreno Leitao /*
47*9e83d510SBreno Leitao * Queue second item while we're still counted as running
48*9e83d510SBreno Leitao * (pool->nr_running > 0). Since schedule_work() on a per-CPU
49*9e83d510SBreno Leitao * workqueue targets raw_smp_processor_id(), item 2 lands on the
50*9e83d510SBreno Leitao * same pool. __queue_work -> kick_pool -> need_more_worker()
51*9e83d510SBreno Leitao * sees nr_running > 0 and does NOT wake a new worker.
52*9e83d510SBreno Leitao */
53*9e83d510SBreno Leitao schedule_work(&stall_work2);
54*9e83d510SBreno Leitao
55*9e83d510SBreno Leitao /*
56*9e83d510SBreno Leitao * Hide from the workqueue concurrency manager. Without
57*9e83d510SBreno Leitao * PF_WQ_WORKER, schedule() won't call wq_worker_sleeping(),
58*9e83d510SBreno Leitao * so nr_running is never decremented and no replacement
59*9e83d510SBreno Leitao * worker is created. Item 2 stays stuck in pool->worklist.
60*9e83d510SBreno Leitao */
61*9e83d510SBreno Leitao current->flags &= ~PF_WQ_WORKER;
62*9e83d510SBreno Leitao
63*9e83d510SBreno Leitao pr_info("wq_stall: entering wait_event_idle (PF_WQ_WORKER cleared)\n");
64*9e83d510SBreno Leitao pr_info("wq_stall: expect 'BUG: workqueue lockup' in ~30-60s\n");
65*9e83d510SBreno Leitao wait_event_idle(stall_wq_head, atomic_read(&wake_condition) != 0);
66*9e83d510SBreno Leitao
67*9e83d510SBreno Leitao /* Restore so process_one_work() cleanup works correctly */
68*9e83d510SBreno Leitao current->flags |= PF_WQ_WORKER;
69*9e83d510SBreno Leitao pr_info("wq_stall: woke up, PF_WQ_WORKER restored\n");
70*9e83d510SBreno Leitao }
71*9e83d510SBreno Leitao
wq_stall_init(void)72*9e83d510SBreno Leitao static int __init wq_stall_init(void)
73*9e83d510SBreno Leitao {
74*9e83d510SBreno Leitao pr_info("wq_stall: loading\n");
75*9e83d510SBreno Leitao
76*9e83d510SBreno Leitao INIT_WORK(&stall_work1, stall_work1_fn);
77*9e83d510SBreno Leitao INIT_WORK(&stall_work2, stall_work2_fn);
78*9e83d510SBreno Leitao schedule_work(&stall_work1);
79*9e83d510SBreno Leitao
80*9e83d510SBreno Leitao return 0;
81*9e83d510SBreno Leitao }
82*9e83d510SBreno Leitao
wq_stall_exit(void)83*9e83d510SBreno Leitao static void __exit wq_stall_exit(void)
84*9e83d510SBreno Leitao {
85*9e83d510SBreno Leitao pr_info("wq_stall: unloading\n");
86*9e83d510SBreno Leitao atomic_set(&wake_condition, 1);
87*9e83d510SBreno Leitao wake_up(&stall_wq_head);
88*9e83d510SBreno Leitao flush_work(&stall_work1);
89*9e83d510SBreno Leitao flush_work(&stall_work2);
90*9e83d510SBreno Leitao pr_info("wq_stall: all work flushed, module unloaded\n");
91*9e83d510SBreno Leitao }
92*9e83d510SBreno Leitao
93*9e83d510SBreno Leitao module_init(wq_stall_init);
94*9e83d510SBreno Leitao module_exit(wq_stall_exit);
95*9e83d510SBreno Leitao
96*9e83d510SBreno Leitao MODULE_LICENSE("GPL");
97*9e83d510SBreno Leitao MODULE_DESCRIPTION("Reproduce workqueue stall caused by PF_WQ_WORKER misuse");
98*9e83d510SBreno Leitao MODULE_AUTHOR("Breno Leitao <leitao@debian.org>");
99