Skip to content

Commit e29258c

Browse files
author
Robert Foss
committed
workqueue: Add interface for user-defined workqueue lockdep map
JIRA: https://issues.redhat.com/browse/RHEL-53569 Upstream Status: v6.12-rc1 commit ec0a7d4 Author: Matthew Brost <matthew.brost@intel.com> AuthorDate: Fri Aug 9 15:28:25 2024 -0700 Commit: Tejun Heo <tj@kernel.org> CommitDate: Tue Aug 13 09:05:51 2024 -1000 Add an interface for a user-defined workqueue lockdep map, which is helpful when multiple workqueues are created for the same purpose. This also helps avoid leaking lockdep maps on each workqueue creation. v2: - Add alloc_workqueue_lockdep_map (Tejun) v3: - Drop __WQ_USER_OWNED_LOCKDEP (Tejun) - static inline alloc_ordered_workqueue_lockdep_map (Tejun) Cc: Tejun Heo <tj@kernel.org> Cc: Lai Jiangshan <jiangshanlai@gmail.com> Signed-off-by: Matthew Brost <matthew.brost@intel.com> Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Robert Foss <rfoss@redhat.com>
1 parent c2a6c84 commit e29258c

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

include/linux/workqueue.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,58 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,
500500
unsigned int flags,
501501
int max_active, ...);
502502

503+
#ifdef CONFIG_LOCKDEP
504+
/**
505+
* alloc_workqueue_lockdep_map - allocate a workqueue with user-defined lockdep_map
506+
* @fmt: printf format for the name of the workqueue
507+
* @flags: WQ_* flags
508+
* @max_active: max in-flight work items, 0 for default
509+
* @lockdep_map: user-defined lockdep_map
510+
* @...: args for @fmt
511+
*
512+
* Same as alloc_workqueue but with the a user-define lockdep_map. Useful for
513+
* workqueues created with the same purpose and to avoid leaking a lockdep_map
514+
* on each workqueue creation.
515+
*
516+
* RETURNS:
517+
* Pointer to the allocated workqueue on success, %NULL on failure.
518+
*/
519+
__printf(1, 5) struct workqueue_struct *
520+
alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active,
521+
struct lockdep_map *lockdep_map, ...);
522+
523+
/**
524+
* alloc_ordered_workqueue_lockdep_map - allocate an ordered workqueue with
525+
* user-defined lockdep_map
526+
*
527+
* @fmt: printf format for the name of the workqueue
528+
* @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
529+
* @lockdep_map: user-defined lockdep_map
530+
* @args: args for @fmt
531+
*
532+
* Same as alloc_ordered_workqueue but with the a user-define lockdep_map.
533+
* Useful for workqueues created with the same purpose and to avoid leaking a
534+
* lockdep_map on each workqueue creation.
535+
*
536+
* RETURNS:
537+
* Pointer to the allocated workqueue on success, %NULL on failure.
538+
*/
539+
__printf(1, 4) static inline struct workqueue_struct *
540+
alloc_ordered_workqueue_lockdep_map(const char *fmt, unsigned int flags,
541+
struct lockdep_map *lockdep_map, ...)
542+
{
543+
struct workqueue_struct *wq;
544+
va_list args;
545+
546+
va_start(args, lockdep_map);
547+
wq = alloc_workqueue_lockdep_map(fmt, WQ_UNBOUND | __WQ_ORDERED | flags,
548+
1, lockdep_map, args);
549+
va_end(args);
550+
551+
return wq;
552+
}
553+
#endif
554+
503555
/**
504556
* alloc_ordered_workqueue - allocate an ordered workqueue
505557
* @fmt: printf format for the name of the workqueue

kernel/workqueue.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4671,11 +4671,17 @@ static void wq_init_lockdep(struct workqueue_struct *wq)
46714671

46724672
static void wq_unregister_lockdep(struct workqueue_struct *wq)
46734673
{
4674+
if (wq->lockdep_map != &wq->__lockdep_map)
4675+
return;
4676+
46744677
lockdep_unregister_key(&wq->key);
46754678
}
46764679

46774680
static void wq_free_lockdep(struct workqueue_struct *wq)
46784681
{
4682+
if (wq->lockdep_map != &wq->__lockdep_map)
4683+
return;
4684+
46794685
if (wq->lock_name != wq->name)
46804686
kfree(wq->lock_name);
46814687
}
@@ -5665,6 +5671,28 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,
56655671
}
56665672
EXPORT_SYMBOL_GPL(alloc_workqueue);
56675673

5674+
#ifdef CONFIG_LOCKDEP
5675+
__printf(1, 5)
5676+
struct workqueue_struct *
5677+
alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags,
5678+
int max_active, struct lockdep_map *lockdep_map, ...)
5679+
{
5680+
struct workqueue_struct *wq;
5681+
va_list args;
5682+
5683+
va_start(args, lockdep_map);
5684+
wq = __alloc_workqueue(fmt, flags, max_active, args);
5685+
va_end(args);
5686+
if (!wq)
5687+
return NULL;
5688+
5689+
wq->lockdep_map = lockdep_map;
5690+
5691+
return wq;
5692+
}
5693+
EXPORT_SYMBOL_GPL(alloc_workqueue_lockdep_map);
5694+
#endif
5695+
56685696
static bool pwq_busy(struct pool_workqueue *pwq)
56695697
{
56705698
int i;

0 commit comments

Comments
 (0)