From: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>, Tejun Heo <tj@kernel.org>,
Lai Jiangshan <jiangshanlai@gmail.com>,
Tobias Schrammm <t.schramm@manjaro.org>,
Sebastian Reichel <sre@kernel.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Dan Carpenter <dan.carpenter@linaro.org>,
Krzysztof Kozlowski <krzk@kernel.org>, Lee Jones <lee@kernel.org>,
Dzmitry Sankouski <dsankouski@gmail.com>,
Matthias Brugger <matthias.bgg@gmail.com>,
AngeloGioacchino Del Regno
<angelogioacchino.delregno@collabora.com>,
Benson Leung <bleung@chromium.org>,
Tzung-Bi Shih <tzungbi@kernel.org>
Cc: Matti Vaittinen <mazziesaccount@gmail.com>,
driver-core@lists.linux.dev, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org,
Sebastian Reichel <sebastian.reichel@collabora.com>,
linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-mediatek@lists.infradead.org,
chrome-platform@lists.linux.dev,
Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
Subject: [PATCH v2 01/10] workqueue: devres: Add device-managed allocate workqueue
Date: Thu, 05 Mar 2026 22:45:40 +0100 [thread overview]
Message-ID: <20260305-workqueue-devm-v2-1-66a38741c652@oss.qualcomm.com> (raw)
In-Reply-To: <20260305-workqueue-devm-v2-0-66a38741c652@oss.qualcomm.com>
Add a Resource-managed version of alloc_workqueue() to fix common
problem of drivers mixing devm() calls with destroy_workqueue. Such
naive and discouraged driver approach leads to difficult to debug bugs
when the driver:
1. Allocates workqueue in standard way and destroys it in driver
remove() callback,
2. Sets work struct with devm_work_autocancel(),
3. Registers interrupt handler with devm_request_threaded_irq().
Which leads to following unbind/removal path:
1. destroy_workqueue() via driver remove(),
Any interrupt coming now would still execute the interrupt handler,
which queues work on destroyed workqueue.
2. devm_irq_release(),
3. devm_work_drop() -> cancel_work_sync() on destroyed workqueue.
devm_alloc_workqueue() has two benefits:
1. Solves above problem of mix-and-match devres and non-devres code in
driver,
2. Simplify any sane drivers which were correctly using
alloc_workqueue() + devm_add_action_or_reset().
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
---
All further patches depend on this one.
Changes in v2:
1. Drop devm_create_workqueue(), devm_create_freezable_workqueue() and
devm_create_singlethread_workqueue()
2. Simplify with devm_add_action_or_reset()
3. Do not export devm_destroy_workqueue()
4. I did not move the declarations to devm-helpers.h because consensus
was not reached and I think it would not be accurate place. The main
alloc_workqueue() is here, so should be the devm- interface.
---
Documentation/driver-api/driver-model/devres.rst | 4 ++++
include/linux/workqueue.h | 22 +++++++++++++++++++
kernel/workqueue.c | 28 ++++++++++++++++++++++++
3 files changed, 54 insertions(+)
diff --git a/Documentation/driver-api/driver-model/devres.rst b/Documentation/driver-api/driver-model/devres.rst
index 7d2b897d66fa..017fb155a5bc 100644
--- a/Documentation/driver-api/driver-model/devres.rst
+++ b/Documentation/driver-api/driver-model/devres.rst
@@ -464,3 +464,7 @@ SPI
WATCHDOG
devm_watchdog_register_device()
+
+WORKQUEUE
+ devm_alloc_workqueue()
+ devm_alloc_ordered_workqueue()
diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index fc5744402a66..66a94c171b0b 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -512,6 +512,26 @@ __printf(1, 4) struct workqueue_struct *
alloc_workqueue_noprof(const char *fmt, unsigned int flags, int max_active, ...);
#define alloc_workqueue(...) alloc_hooks(alloc_workqueue_noprof(__VA_ARGS__))
+/**
+ * devm_alloc_workqueue - Resource-managed allocate a workqueue
+ * @dev: Device to allocate workqueue for
+ * @fmt: printf format for the name of the workqueue
+ * @flags: WQ_* flags
+ * @max_active: max in-flight work items, 0 for default
+ * @...: args for @fmt
+ *
+ * Resource managed workqueue, see alloc_workqueue() for details.
+ *
+ * The workqueue will be automatically destroyed on driver detach. Typically
+ * this should be used in drivers already relying on devm interafaces.
+ *
+ * RETURNS:
+ * Pointer to the allocated workqueue on success, %NULL on failure.
+ */
+__printf(2, 5) struct workqueue_struct *
+devm_alloc_workqueue(struct device *dev, const char *fmt, unsigned int flags,
+ int max_active, ...);
+
#ifdef CONFIG_LOCKDEP
/**
* alloc_workqueue_lockdep_map - allocate a workqueue with user-defined lockdep_map
@@ -568,6 +588,8 @@ alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active,
*/
#define alloc_ordered_workqueue(fmt, flags, args...) \
alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, ##args)
+#define devm_alloc_ordered_workqueue(dev, fmt, flags, args...) \
+ devm_alloc_workqueue(dev, fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, ##args)
#define create_workqueue(name) \
alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_PERCPU, 1, (name))
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index a1bfabeaef41..5cc5e6a400c9 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -41,6 +41,7 @@
#include <linux/mempolicy.h>
#include <linux/freezer.h>
#include <linux/debug_locks.h>
+#include <linux/device/devres.h>
#include <linux/lockdep.h>
#include <linux/idr.h>
#include <linux/jhash.h>
@@ -5909,6 +5910,33 @@ struct workqueue_struct *alloc_workqueue_noprof(const char *fmt,
}
EXPORT_SYMBOL_GPL(alloc_workqueue_noprof);
+static void devm_workqueue_release(void *res)
+{
+ destroy_workqueue(res);
+}
+
+__printf(2, 5) struct workqueue_struct *
+devm_alloc_workqueue(struct device *dev, const char *fmt, unsigned int flags,
+ int max_active, ...)
+{
+ struct workqueue_struct *wq;
+ va_list args;
+ int ret;
+
+ va_start(args, max_active);
+ wq = alloc_workqueue(fmt, flags, max_active, args);
+ va_end(args);
+ if (!wq)
+ return NULL;
+
+ ret = devm_add_action_or_reset(dev, devm_workqueue_release, wq);
+ if (ret)
+ return NULL;
+
+ return wq;
+}
+EXPORT_SYMBOL_GPL(devm_alloc_workqueue);
+
#ifdef CONFIG_LOCKDEP
__printf(1, 5)
struct workqueue_struct *
--
2.51.0
next prev parent reply other threads:[~2026-03-05 21:46 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-05 21:45 [PATCH v2 00/10] workqueue / drivers: Add device-managed allocate workqueue Krzysztof Kozlowski
2026-03-05 21:45 ` Krzysztof Kozlowski [this message]
2026-03-06 4:08 ` [PATCH v2 01/10] workqueue: devres: " Tejun Heo
2026-03-10 9:59 ` Krzysztof Kozlowski
2026-03-10 17:05 ` Tejun Heo
2026-03-05 21:45 ` [PATCH v2 02/10] power: supply: cw2015: Free allocated workqueue Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 03/10] power: supply: max77705: Drop duplicated IRQ error message Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 04/10] power: supply: max77705: Free allocated workqueue and fix removal order Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 05/10] power: supply: mt6370: Simplify with devm_alloc_ordered_workqueue() Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 06/10] power: supply: ipaq_micro: Simplify with devm Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 07/10] mfd: ezx-pcap: Drop memory allocation error message Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 08/10] mfd: ezx-pcap: Return directly instead of empty gotos Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 09/10] mfd: ezx-pcap: Avoid rescheduling after destroying workqueue Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 10/10] platform/chrome: cros_usbpd_logger: Simplify with devm Krzysztof Kozlowski
2026-03-10 3:26 ` Tzung-Bi Shih
2026-03-06 14:28 ` [PATCH v2 00/10] workqueue / drivers: Add device-managed allocate workqueue Andy Shevchenko
2026-03-11 7:10 ` (subset) " Sebastian Reichel
2026-03-19 16:21 ` Lee Jones
2026-03-20 3:06 ` Tzung-Bi Shih
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260305-workqueue-devm-v2-1-66a38741c652@oss.qualcomm.com \
--to=krzysztof.kozlowski@oss.qualcomm.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=bleung@chromium.org \
--cc=chrome-platform@lists.linux.dev \
--cc=corbet@lwn.net \
--cc=dakr@kernel.org \
--cc=dan.carpenter@linaro.org \
--cc=driver-core@lists.linux.dev \
--cc=dsankouski@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=jiangshanlai@gmail.com \
--cc=krzk@kernel.org \
--cc=lee@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=linux-pm@vger.kernel.org \
--cc=matthias.bgg@gmail.com \
--cc=mazziesaccount@gmail.com \
--cc=rafael@kernel.org \
--cc=sebastian.reichel@collabora.com \
--cc=skhan@linuxfoundation.org \
--cc=sre@kernel.org \
--cc=t.schramm@manjaro.org \
--cc=tj@kernel.org \
--cc=tzungbi@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.