Skip to content

Commit 1dfc9d6

Browse files
krzkhtejun
authored andcommitted
workqueue: devres: Add device-managed allocate workqueue
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> Acked-by: Tejun Heo <tj@kernel.org> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 6de23f8 commit 1dfc9d6

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

Documentation/driver-api/driver-model/devres.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,7 @@ SPI
464464

465465
WATCHDOG
466466
devm_watchdog_register_device()
467+
468+
WORKQUEUE
469+
devm_alloc_workqueue()
470+
devm_alloc_ordered_workqueue()

include/linux/workqueue.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,26 @@ __printf(1, 4) struct workqueue_struct *
512512
alloc_workqueue_noprof(const char *fmt, unsigned int flags, int max_active, ...);
513513
#define alloc_workqueue(...) alloc_hooks(alloc_workqueue_noprof(__VA_ARGS__))
514514

515+
/**
516+
* devm_alloc_workqueue - Resource-managed allocate a workqueue
517+
* @dev: Device to allocate workqueue for
518+
* @fmt: printf format for the name of the workqueue
519+
* @flags: WQ_* flags
520+
* @max_active: max in-flight work items, 0 for default
521+
* @...: args for @fmt
522+
*
523+
* Resource managed workqueue, see alloc_workqueue() for details.
524+
*
525+
* The workqueue will be automatically destroyed on driver detach. Typically
526+
* this should be used in drivers already relying on devm interafaces.
527+
*
528+
* RETURNS:
529+
* Pointer to the allocated workqueue on success, %NULL on failure.
530+
*/
531+
__printf(2, 5) struct workqueue_struct *
532+
devm_alloc_workqueue(struct device *dev, const char *fmt, unsigned int flags,
533+
int max_active, ...);
534+
515535
#ifdef CONFIG_LOCKDEP
516536
/**
517537
* 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,
568588
*/
569589
#define alloc_ordered_workqueue(fmt, flags, args...) \
570590
alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, ##args)
591+
#define devm_alloc_ordered_workqueue(dev, fmt, flags, args...) \
592+
devm_alloc_workqueue(dev, fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, ##args)
571593

572594
#define create_workqueue(name) \
573595
alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_PERCPU, 1, (name))

kernel/workqueue.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <linux/mempolicy.h>
4242
#include <linux/freezer.h>
4343
#include <linux/debug_locks.h>
44+
#include <linux/device/devres.h>
4445
#include <linux/lockdep.h>
4546
#include <linux/idr.h>
4647
#include <linux/jhash.h>
@@ -5891,6 +5892,33 @@ struct workqueue_struct *alloc_workqueue_noprof(const char *fmt,
58915892
}
58925893
EXPORT_SYMBOL_GPL(alloc_workqueue_noprof);
58935894

5895+
static void devm_workqueue_release(void *res)
5896+
{
5897+
destroy_workqueue(res);
5898+
}
5899+
5900+
__printf(2, 5) struct workqueue_struct *
5901+
devm_alloc_workqueue(struct device *dev, const char *fmt, unsigned int flags,
5902+
int max_active, ...)
5903+
{
5904+
struct workqueue_struct *wq;
5905+
va_list args;
5906+
int ret;
5907+
5908+
va_start(args, max_active);
5909+
wq = alloc_workqueue(fmt, flags, max_active, args);
5910+
va_end(args);
5911+
if (!wq)
5912+
return NULL;
5913+
5914+
ret = devm_add_action_or_reset(dev, devm_workqueue_release, wq);
5915+
if (ret)
5916+
return NULL;
5917+
5918+
return wq;
5919+
}
5920+
EXPORT_SYMBOL_GPL(devm_alloc_workqueue);
5921+
58945922
#ifdef CONFIG_LOCKDEP
58955923
__printf(1, 5)
58965924
struct workqueue_struct *

0 commit comments

Comments
 (0)