Use user-defined workqueue lockdep map for drm sched
Subsystems (5)
Matthew Brost
maintainerv2 cover Use user-defined workqueue lockdep map for drm sched
By default, each DRM scheduler instance creates an ordered workqueue for submission, and each workqueue creation allocates a new lockdep map. This becomes problematic when a DRM scheduler is created for every user queue (e.g., in DRM drivers with firmware schedulers like Xe) due to the limited number of available lockdep maps. With numerous user queues being created and destroyed, lockdep may run out of maps, leading to lockdep being disabled. Xe mitigated this by creating a pool of workqueues for DRM scheduler use. However, this approach also encounters issues if the driver is unloaded and reloaded multiple times or if many VFs are probed.
To address this, we propose creating a single lockdep map for all DRM scheduler workqueues, which will also resolve issues for other DRM drivers that create a DRM scheduler per user queue.
This solution has been tested by unloading and reloading the Xe driver. Before this series, around 30 driver reloads would result in lockdep being turned off. After implementing the series, the driver can be unloaded and reloaded hundreds of times without issues.
v2:
- Split workqueue changes into multiple patches
- Add alloc_workqueue_lockdep_map (Tejun)
- Don't RFC
Matt
Matthew Brost (5):
workqueue: Split alloc_workqueue into internal function and lockdep
init
workqueue: Change workqueue lockdep map to pointer
workqueue: Add interface for user-defined workqueue lockdep map drm/sched: Use drm sched lockdep map for submit_wq drm/xe: Drop GuC submit_wq pool
drivers/gpu/drm/scheduler/sched_main.c | 11 ++++ drivers/gpu/drm/xe/xe_guc_submit.c | 60 +-------------------- drivers/gpu/drm/xe/xe_guc_types.h | 7 --- include/linux/workqueue.h | 25 +++++++++ kernel/workqueue.c | 75 ++++++++++++++++++++------ 5 files changed, 97 insertions(+), 81 deletions(-)
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH v2 1/5 workqueue: Split alloc_workqueue into internal function and lockdep init
Will help enable user-defined lockdep maps for workqueues.
--- kernel/workqueue.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
kernel/workqueue.c +23 -8
@@ -5657,9 +5657,9 @@ static void wq_adjust_max_active(struct workqueue_struct *wq) } __printf(1, 4) -struct workqueue_struct *alloc_workqueue (const char *fmt, -unsigned int flags, -int max_active, ...) +static struct workqueue_struct *__alloc_workqueue (const char *fmt, +unsigned int flags, +int max_active, ...) {va_list args;struct workqueue_struct *wq;
@@ -5725,12 +5725,11 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,INIT_LIST_HEAD (&wq->flusher_overflow);INIT_LIST_HEAD (&wq->maydays); -wq_init_lockdep (wq);INIT_LIST_HEAD (&wq->list);if (flags &WQ_UNBOUND ) {if (alloc_node_nr_active (wq->node_nr_active) <0 ) - goto err_unreg_lockdep; + goto err_free_wq; }if (alloc_and_link_pwqs (wq) <0 )
@@ -5762,9 +5761,6 @@ struct workqueue_struct *alloc_workqueue(const char *fmt, err_free_node_nr_active:if (wq->flags &WQ_UNBOUND )free_node_nr_active (wq->node_nr_active); -err_unreg_lockdep: -wq_unregister_lockdep (wq); -wq_free_lockdep (wq); err_free_wq:free_workqueue_attrs (wq->unbound_attrs);kfree (wq);
@@ -5773,6 +5769,25 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,destroy_workqueue (wq);return NULL ; } + +__printf(1, 4) +struct workqueue_struct *alloc_workqueue (const char *fmt, +unsigned int flags, +int max_active, ...) +{ +struct workqueue_struct *wq; +va_list args; + +va_start (args, max_active); + wq =__alloc_workqueue (fmt, flags, max_active, args); +va_end (args); +if (!wq) +return NULL ; + +wq_init_lockdep (wq); + +return wq; +}EXPORT_SYMBOL_GPL (alloc_workqueue);static bool pwq_busy (struct pool_workqueue *pwq)
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH v2 2/5 workqueue: Change workqueue lockdep map to pointer
In reply to Matthew BrostWill help enable user-defined lockdep maps for workqueues.
--- kernel/workqueue.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
kernel/workqueue.c +9 -7
@@ -366,7 +366,8 @@ struct workqueue_struct {#ifdef CONFIG_LOCKDEP char *lock_name;struct lock_class_key key; -struct lockdep_map lockdep_map; +struct lockdep_map __lockdep_map; +struct lockdep_map *lockdep_map;#endif char name[WQ_NAME_LEN ];/* I: workqueue name */
@@ -3220,7 +3221,7 @@ __acquires(&pool->lock) lockdep_start_depth =lockdep_depth (current);/* see drain_dead_softirq_workfn() */ if (!bh_draining) -lock_map_acquire (&pwq->wq->lockdep_map); +lock_map_acquire (pwq->wq->lockdep_map);lock_map_acquire (&lockdep_map);/* * Strictly speaking we should mark the invariant state without holding
@@ -3254,7 +3255,7 @@ __acquires(&pool->lock) pwq->stats[PWQ_STAT_COMPLETED ]++;lock_map_release (&lockdep_map);if (!bh_draining) -lock_map_release (&pwq->wq->lockdep_map); +lock_map_release (pwq->wq->lockdep_map); if (unlikely((worker->task && in_atomic()) || lockdep_depth(current) != lockdep_start_depth ||
@@ -3892,8 +3893,8 @@ static void touch_wq_lockdep_map(struct workqueue_struct *wq)if (wq->flags &WQ_BH )local_bh_disable (); -lock_map_acquire (&wq->lockdep_map); -lock_map_release (&wq->lockdep_map); +lock_map_acquire (wq->lockdep_map); +lock_map_release (wq->lockdep_map);if (wq->flags &WQ_BH )local_bh_enable ();
@@ -3927,7 +3928,7 @@ void __flush_workqueue(struct workqueue_struct *wq)struct wq_flusher this_flusher = { .list =LIST_HEAD_INIT (this_flusher.list), .flush_color =-1 , - .done =COMPLETION_INITIALIZER_ONSTACK_MAP (this_flusher.done, wq->lockdep_map), + .done =COMPLETION_INITIALIZER_ONSTACK_MAP (this_flusher.done, (*wq->lockdep_map)), };int next_color;
@@ -4788,7 +4789,8 @@ static void wq_init_lockdep(struct workqueue_struct *wq) lock_name = wq->name; wq->lock_name = lock_name; -lockdep_init_map (&wq->lockdep_map, lock_name, &wq->key,0 ); + wq->lockdep_map = &wq->__lockdep_map; +lockdep_init_map (wq->lockdep_map, lock_name, &wq->key,0 ); }static void wq_unregister_lockdep (struct workqueue_struct *wq)
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH v2 3/5 workqueue: Add interface for user-defined workqueue lockdep map
In reply to Matthew BrostAdd 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.
Implement a new internal workqueue flag, __WQ_USER_OWNED_LOCKDEP, to indicate that the user will set up the workqueue lockdep map using the new function alloc_workqueue_lockdep_map. Also add helper macro alloc_ordered_workqueue_lockdep_map.
v2:
- Add alloc_workqueue_lockdep_map (Tejun)
--- include/linux/workqueue.h | 25 +++++++++++++++++++++++++ kernel/workqueue.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+)
include/linux/workqueue.h +25 -0
@@ -406,6 +406,7 @@ enum wq_flags { __WQ_DRAINING =1 <<16 ,/* internal: workqueue is draining */ __WQ_ORDERED =1 <<17 ,/* internal: workqueue is ordered */ __WQ_LEGACY =1 <<18 ,/* internal: create*_workqueue() */ + __WQ_USER_OWNED_LOCKDEP =1 <<19 ,/* internal: workqueue has user owned lockdep map */ /* BH wq only allows the following flags */ __WQ_BH_ALLOWS = WQ_BH | WQ_HIGHPRI,
@@ -507,6 +508,30 @@ void workqueue_softirq_dead(unsigned int cpu); __printf(1, 4) struct workqueue_struct *alloc_workqueue (const char *fmt,unsigned int flags,int max_active, ...); +#ifdef CONFIG_LOCKDEP +/** +* alloc_workqueue_lockdep_map - allocate a workqueue with user-defined lockdep_map +* @fmt: printf format for the name of the workqueue +* @flags: WQ_* flags +* @max_active: max in-flight work items, 0 for default +* @lockdep_map: user-defined lockdep_map +* @...: args for @fmt +* +* Same as alloc_workqueue but with the a user-defined lockdep_map. Useful for +* workqueues created with the same purpose and to avoid leaking a lockdep_map +* on each workqueue creation. +* +* RETURNS: +* Pointer to the allocated workqueue on success, %NULL on failure. +*/ +__printf(1, 5) struct workqueue_struct * +alloc_workqueue_lockdep_map (const char *fmt,unsigned int flags,int max_active, +struct lockdep_map *lockdep_map, ...); + +#define alloc_ordered_workqueue_lockdep_map(fmt, flags, lockdep_map, args...) \ + alloc_workqueue_lockdep_map(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, lockdep_map, ##args) +#endif +/** * alloc_ordered_workqueue - allocate an ordered workqueue * @fmt: printf format for the name of the workqueue
kernel/workqueue.c +28 -0
@@ -4795,11 +4795,17 @@ static void wq_init_lockdep(struct workqueue_struct *wq)static void wq_unregister_lockdep (struct workqueue_struct *wq) { +if (wq->flags & __WQ_USER_OWNED_LOCKDEP) +return ; +lockdep_unregister_key (&wq->key); }static void wq_free_lockdep (struct workqueue_struct *wq) { +if (wq->flags & __WQ_USER_OWNED_LOCKDEP) +return ; +if (wq->lock_name != wq->name)kfree (wq->lock_name); }
@@ -5792,6 +5798,28 @@ struct workqueue_struct *alloc_workqueue(const char *fmt, }EXPORT_SYMBOL_GPL (alloc_workqueue); +#ifdef CONFIG_LOCKDEP +__printf(1, 5) +struct workqueue_struct * +alloc_workqueue_lockdep_map (const char *fmt,unsigned int flags, +int max_active,struct lockdep_map *lockdep_map, ...) +{ +struct workqueue_struct *wq; +va_list args; + +va_start (args, lockdep_map); + wq =__alloc_workqueue (fmt, flags | __WQ_USER_OWNED_LOCKDEP, max_active, args); +va_end (args); +if (!wq) +return NULL ; + + wq->lockdep_map = lockdep_map; + +return wq; +} +EXPORT_SYMBOL_GPL (alloc_workqueue_lockdep_map); +#endif +static bool pwq_busy (struct pool_workqueue *pwq) {int i;
Signature · 1 line
2.34.1
Tejun Heo
maintainerRe: [PATCH v2 3/5] workqueue: Add interface for user-defined workqueue lockdep map
Hello,
include/linux/workqueue.h void workqueue_softirq_dead(unsigned int cpu); · line 533
Quoted 3 lines · Matthew Brost
include/linux/workqueue.h +3
-0+#define alloc_ordered_workqueue_lockdep_map(fmt, flags, lockdep_map, args...) \ + alloc_workqueue_lockdep_map(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, lockdep_map, ##args) +#endif
alloc_ordered_workqueue() is a macro too but would you mind making this an inline function?
kernel/workqueue.c static void wq_init_lockdep(struct workqueue_struct *wq) · line 4799
Quoted 6 lines
kernel/workqueue.c +2
-0@@ -4795,11 +4795,17 @@ static void wq_init_lockdep(struct workqueue_struct *wq)static void wq_unregister_lockdep (struct workqueue_struct *wq) { +if (wq->flags & __WQ_USER_OWNED_LOCKDEP) +return ;
Do you still need the flag? Can't you test wq->lockdep_map != &wq->__lockdep_map instead?
Thanks.
Signature · 1 line
tejun
Matthew Brost
maintainerRe: [PATCH v2 3/5] workqueue: Add interface for user-defined workqueue lockdep map
Quoted 10 lines · Tejun Heo
Hello,
include/linux/workqueue.h void workqueue_softirq_dead(unsigned int cpu); · line 533
Quoted 3 lines · Matthew Brost
include/linux/workqueue.h +3
-0+#define alloc_ordered_workqueue_lockdep_map(fmt, flags, lockdep_map, args...) \ + alloc_workqueue_lockdep_map(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, lockdep_map, ##args) +#endif
alloc_ordered_workqueue()is a macro too but would you mind making this an inline function?
Not at all.
Quoted 10 lines
kernel/workqueue.c static void wq_init_lockdep(struct workqueue_struct *wq) · line 4799
Quoted 6 lines
kernel/workqueue.c +2
-0@@ -4795,11 +4795,17 @@ static void wq_init_lockdep(struct workqueue_struct *wq)static void wq_unregister_lockdep (struct workqueue_struct *wq) { +if (wq->flags & __WQ_USER_OWNED_LOCKDEP) +return ;Do you still need the flag? Can't you test
wq->lockdep_map!=&wq->__lockdep_mapinstead?
Ah, yes. That is better. Will change.
Matt
Quoted 4 lines
Thanks.
Signature · 1 line
tejun
Matthew Brost
maintainerPATCH v2 4/5 drm/sched: Use drm sched lockdep map for submit_wq
In reply to Matthew BrostAvoid leaking a lockdep map on each drm sched creation and destruction by using a single lockdep map for all drm sched allocated submit_wq.
v2:
- Use alloc_ordered_workqueue_lockdep_map (Tejun)
--- drivers/gpu/drm/scheduler/sched_main.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +11 -0
@@ -87,6 +87,12 @@#define CREATE_TRACE_POINTS #include "gpu_scheduler_trace.h" +#ifdef CONFIG_LOCKDEP +static struct lockdep_map drm_sched_lockdep_map = { + .name ="drm_sched_lockdep_map" +}; +#endif +#define to_drm_sched_job (sched_job) \ container_of((sched_job), struct drm_sched_job, queue_node)
@@ -1272,7 +1278,12 @@ int drm_sched_init(struct drm_gpu_scheduler *sched, sched->submit_wq = submit_wq; sched->own_submit_wq = false; }else { +#ifdef CONFIG_LOCKDEP + sched->submit_wq =alloc_ordered_workqueue_lockdep_map (name,0 , + &drm_sched_lockdep_map); +#else sched->submit_wq =alloc_ordered_workqueue (name,0 ); +#endif if (!sched->submit_wq)return -ENOMEM ;
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH v2 5/5 drm/xe: Drop GuC submit_wq pool
In reply to Matthew BrostNow that drm sched uses a single lockdep map for all submit_wq, drop the GuC submit_wq pool hack.
--- drivers/gpu/drm/xe/xe_guc_submit.c | 60 +----------------------------- drivers/gpu/drm/xe/xe_guc_types.h | 7 ---- 2 files changed, 1 insertion(+), 66 deletions(-)
drivers/gpu/drm/xe/xe_guc_submit.c +1 -59
@@ -224,64 +224,11 @@ static bool exec_queue_killed_or_banned_or_wedged(struct xe_exec_queue *q) EXEC_QUEUE_STATE_BANNED)); } -#ifdef CONFIG_PROVE_LOCKING -static int alloc_submit_wq (struct xe_guc *guc) -{ -int i; - -for (i =0 ; i <NUM_SUBMIT_WQ ; ++i) { - guc->submission_state.submit_wq_pool[i] = -alloc_ordered_workqueue ("submit_wq" ,0 ); -if (!guc->submission_state.submit_wq_pool[i]) - goto err_free; - } - -return 0 ; - -err_free: -while (i) -destroy_workqueue (guc->submission_state.submit_wq_pool[--i]); - -return -ENOMEM ; -} - -static void free_submit_wq (struct xe_guc *guc) -{ -int i; - -for (i =0 ; i <NUM_SUBMIT_WQ ; ++i) -destroy_workqueue (guc->submission_state.submit_wq_pool[i]); -} - -static struct workqueue_struct *get_submit_wq (struct xe_guc *guc) -{ -int idx = guc->submission_state.submit_wq_idx++ %NUM_SUBMIT_WQ ; - -return guc->submission_state.submit_wq_pool[idx]; -} -#else -static int alloc_submit_wq (struct xe_guc *guc) -{ -return 0 ; -} - -static void free_submit_wq (struct xe_guc *guc) -{ - -} - -static struct workqueue_struct *get_submit_wq (struct xe_guc *guc) -{ -return NULL ; -} -#endif -static void guc_submit_fini (struct drm_device *drm,void *arg) {struct xe_guc *guc = arg;xa_destroy (&guc->submission_state.exec_queue_lookup); -free_submit_wq (guc); }static void guc_submit_wedged_fini (struct drm_device *drm,void *arg)
@@ -337,10 +284,6 @@ int xe_guc_submit_init(struct xe_guc *guc, unsigned int num_ids)if (err)return err; - err =alloc_submit_wq (guc); -if (err) -return err; - gt->exec_queue_ops = &guc_exec_queue_ops;xa_init (&guc->submission_state.exec_queue_lookup);
@@ -1445,8 +1388,7 @@ static int guc_exec_queue_init(struct xe_exec_queue *q) timeout = (q->vm &&xe_vm_in_lr_mode (q->vm)) ?MAX_SCHEDULE_TIMEOUT :msecs_to_jiffies (q->sched_props.job_timeout_ms); err =xe_sched_init (&ge->sched, &drm_sched_ops, &xe_sched_ops, -get_submit_wq (guc), - q->lrc[0 ]->ring.size /MAX_JOB_SIZE_BYTES ,64 , +NULL , q->lrc[0 ]->ring.size /MAX_JOB_SIZE_BYTES ,64 , timeout,guc_to_gt (guc)->ordered_wq,NULL , q->name,gt_to_xe (q->gt)->drm.dev);if (err)
drivers/gpu/drm/xe/xe_guc_types.h +0 -7
@@ -72,13 +72,6 @@ struct xe_guc {atomic_t stopped;/** @submission_state.lock: protects submission state */ struct mutex lock; -#ifdef CONFIG_PROVE_LOCKING -#define NUM_SUBMIT_WQ 256 -/** @submission_state.submit_wq_pool: submission ordered workqueues pool */ -struct workqueue_struct *submit_wq_pool[NUM_SUBMIT_WQ ]; -/** @submission_state.submit_wq_idx: submission ordered workqueue index */ -int submit_wq_idx; -#endif /** @submission_state.enabled: submission is enabled */ bool enabled; } submission_state;
Signature · 1 line
2.34.1
Patchwork
CI✗ CI.Patch_applied: failure for Use user-defined workqueue lockdep map for drm sched (rev2)
In reply to Matthew Brost== Series Details ==
== Series Details ==
Series: Use user-defined workqueue lockdep map for drm sched (rev2)
URL : https://patchwork.freedesktop.org/series/136701/
State : failure
== Summary ==
=== Applying kernel patches on branch 'drm-tip' with base: ===
Base commit: 4b1ff33f467a drm-tip: 2024y-07m-30d-20h-58m-38s UTC integration manifest === git am output follows ===
error: patch failed: kernel/workqueue.c:5725
error: kernel/workqueue.c: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Applying: workqueue: Split alloc_workqueue into internal function and lockdep init Patch failed at 0001 workqueue: Split alloc_workqueue into internal function and lockdep init When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".