Skip to contentSkip to conversation

workqueue: check for NULL wq in queue_delayed_work_on()

PATCH In review baichen 2 participants 3 messages Mar 16 – Mar 17 Workqueue

lore
Share & tools
lore

baichen

author
#0 lore raw

PATCH 1/1 workqueue: check for NULL wq in queue_delayed_work_on()

If queue_delayed_work() is called with a NULL wq, the existing WARN_ON_ONCE(!wq) in __queue_delayed_work() only prints a warning but doesn't prevent the subsequent NULL pointer dereference when accessing wq->flags, causing a panic.
Add a NULL check at the beginning of queue_delayed_work_on() to catch this error early, print a warning with stack dump, and return false to the caller instead of crashing later.
Additionally, if the delay time is relatively long or there are too many kernel log prints in a short time, the previous warning message may be overwritten, making such issues difficult to debug. Relying on kernel log alone makes it impossible to diagnose this issue, and one must resort to fulldump to solve it.

Cc: Tejun Heo <[email protected]>,
Lai Jiangshan <[email protected]>,
[email protected]

---
 kernel/workqueue.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/workqueue.c +8 -1
index cd5171208964..6cf0c5eefa07 100644
@@ -2510,7 +2510,6 @@ static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
 	struct timer_list *timer = &dwork->timer;
 	struct work_struct *work = &dwork->work;
 
-	WARN_ON_ONCE(!wq);
 	WARN_ON_ONCE(timer->function != delayed_work_timer_fn);
 	WARN_ON_ONCE(timer_pending(timer));
 	WARN_ON_ONCE(!list_empty(&work->entry));
@@ -2569,6 +2568,14 @@ bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
 	bool ret = false;
 	unsigned long irq_flags;
 
+	if (unlikely(!wq)) {
+		WARN_ON_ONCE(1);
+		pr_warn("workqueue: %s() called with NULL wq, dumping stack:\n",
+			__func__);
+		dump_stack();
+		return false;
+	}
+
 	/* read the comment in __queue_work() */
 	local_irq_save(irq_flags);
 
Signature · 1 line
2.52.0
maintainer
#1 lore raw

Hello,

Calling workqueue APIs with a NULL wq is always a caller bug and adding NULL checks to all APIs isn't the right approach. The WARN was there as a debug aid but it doesn't actually prevent the crash, so it's misleading. I'm removing it instead.

Thanks.

--
tejun

maintainer
#2 lore raw

PATCH 1/1 workqueue: Remove NULL wq WARN in __queue_delayed_work()

In reply to baichen

Remove the WARN_ON_ONCE(!wq) which doesn't serve any useful purpose.

---
Applied to wq/for-7.1.

 kernel/workqueue.c | 1 -
 1 file changed, 1 deletion(-)
kernel/workqueue.c +0 -1
index c1743b20a524..63acaa3e1d6a 100644
@@ -2510,7 +2510,6 @@ static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
 	struct timer_list *timer = &dwork->timer;
 	struct work_struct *work = &dwork->work;
 
-	WARN_ON_ONCE(!wq);
 	WARN_ON_ONCE(timer->function != delayed_work_timer_fn);
 	WARN_ON_ONCE(timer_pending(timer));
 	WARN_ON_ONCE(!list_empty(&work->entry));

--
tejun