workqueue: Warn flushing of kernel-global workqueues
authorTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Mon, 28 Feb 2022 13:49:16 +0000 (22:49 +0900)
committerTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Mon, 28 Feb 2022 22:10:57 +0000 (07:10 +0900)
commitb9c20da356db1b398c1162f9d5059b381724dc8b
treead8c58a2c8a26988dfc16754dcf99fa0134fb608
parent39844b7e3084baecef52d1498b5fa81afa2cefa9
workqueue: Warn flushing of kernel-global workqueues

Since flush operation synchronously waits for completion, flushing
kernel-global WQs (e.g. system_wq) might introduce possibility of deadlock
due to unexpected locking dependency. Tejun Heo commented that it makes no
sense at all to call flush_workqueue() on the shared WQs as the caller has
no idea what it's gonna end up waiting for.

Although there is flush_scheduled_work() which flushes system_wq WQ with
"Think twice before calling this function! It's very easy to get into
trouble if you don't take great care." warning message, syzbot found a
circular locking dependency caused by flushing system_long_wq WQ [1].
Therefore, let's change the direction to that developers had better use
their local WQs if flush_workqueue() is inevitable.

To give developers time to update their modules, for now just emit
a warning message with ratelimit when flush_workqueue() is called on
kernel-global WQs. We will eventually convert this warning message into
WARN_ON() and kill flush_scheduled_work().

This patch introduces __WQ_NO_FLUSH flag for emitting warning. Don't set
this flag when creating your local WQs while updating your module, for
destroy_workqueue() will involve flush operation.

Theoretically, flushing specific work item using flush_work() queued on
kernel-global WQs (which are !WQ_MEM_RECLAIM) has possibility of deadlock.
But this patch does not emit warning when flush_work() is called on work
items queued on kernel-global WQs, based on assumption that we can create
kworker threads as needed and we won't hit max_active limit.

Link: https://syzkaller.appspot.com/bug?extid=831661966588c802aae9
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
include/linux/workqueue.h
kernel/workqueue.c