workqueue: Cleanup affnity for detached/DISASSOCIATED workers
Lai Jiangshan
reviewercover workqueue: Cleanup affnity for detached/DISASSOCIATED workers
From: Lai Jiangshan <[email protected]>
The commit d64f2fa064f8 ("kernel/workqueue: Let rescuers follow unbound wq cpumask changes") updates the affinity for unbound wq's rescuer.
Do extra cleanup to make it more consistent:
consistency with the workers:
* Not for attached rescuers which are managed by pool's affinity
* Also for PERCPU wq's detached rescuers
* Also for DISASSOCIATED pools' workers
* Also for newly created rescuers (starting from without attached)
consistency with the affinity:
* use wq_unbound_cpumask in all the cases.
Lai Jiangshan (3):
workqueue: Update the rescuer's affinity only when it is detached
workqueue: Let DISASSOCIATED workers follow unbound wq cpumask changes
workqueue: Init rescuer's affinities as wq_unbound_cpumask
kernel/workqueue.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-)
Signature · 1 line
2.19.1.6.gb485710b
Lai Jiangshan
reviewerPATCH 1/3 workqueue: Update the rescuer's affinity only when it is detached
From: Lai Jiangshan <[email protected]>
When a rescuer is attached to a pool, its affinity should be only managed by the pool.
But updating the detached rescuer's affinity is still meaningful so that it will not disrupt isolated CPUs when it is to be waken up.
But the commit d64f2fa064f8 ("kernel/workqueue: Let rescuers follow unbound wq cpumask changes") updates the affinity unconditionally, and causes some issues
1) it also changes the affinity when the rescuer is already attached to a pool, which violates the affinity management.
2) the said commit tries to update the affinity of the rescuers, but it misses the rescuers of the PERCPU workqueues, and isolated CPUs can be possibly disrupted by these rescuers when they are summoned.
3) The affinity to set to the rescuers should be consistent in all paths when a rescuer is in detached state. The affinity could be either wq_unbound_cpumask or unbound_effective_cpumask(wq). Related paths:
rescuer's worker_detach_from_pool()
update wq_unbound_cpumask
update wq's cpumask
init_rescuer()
Both affinities are Ok as long as they are consistent in all paths. But using unbound_effective_cpumask(wq) requres much more code to maintain the consistency, and it doesn't make much sense since the affinity is only effective when the rescuer is not processing works. wq_unbound_cpumask is more favorable.
Fix the 1) issue by testing rescuer->pool before updating with wq_pool_attach_mutex held.
Fix the 2) issue by moving the rescuer's affinity updating code to the place updating wq_unbound_cpumask and make it also update for PERCPU workqueues.
Partially cleanup the 3) consistency issue by using wq_unbound_cpumask. So that the path of "updating wq's cpumask" doesn't need to maintain it. and both the paths of "updating wq_unbound_cpumask" and "rescuer's worker_detach_from_pool()" use wq_unbound_cpumask.
Cleanup for init_rescuer()'s consistency for affinity can be done in future.
--- kernel/workqueue.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/workqueue.c +5 -5
@@ -5411,11 +5411,6 @@ static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx)/* update node_nr_active->max */ wq_update_node_max_active (ctx->wq,-1 ); -/* rescuer needs to respect wq cpumask changes */ -if (ctx->wq->rescuer) -set_cpus_allowed_ptr (ctx->wq->rescuer->task, -unbound_effective_cpumask (ctx->wq)); -mutex_unlock (&ctx->wq->mutex); }
@@ -6974,6 +6969,11 @@ static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask)if (!ret) {mutex_lock (&wq_pool_attach_mutex);cpumask_copy (wq_unbound_cpumask, unbound_cpumask); +/* rescuer needs to respect cpumask changes when it is not attached */ +list_for_each_entry (wq, &workqueues, list) { +if (wq->rescuer && !wq->rescuer->pool) +unbind_worker (wq->rescuer); + }mutex_unlock (&wq_pool_attach_mutex); }return ret;
Signature · 1 line
2.19.1.6.gb485710b
Lai Jiangshan
reviewerPATCH 2/3 workqueue: Let DISASSOCIATED workers follow unbound wq cpumask changes
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
When workqueue cpumask changes are committed, the DISASSOCIATED workers affinity is not touched and this might be a problem down the line for isolated setups when the DISASSOCIATED pools still have works to run after the cpu is offline.
Make sure the workers' affinity is updated every time a workqueue cpumask changes, so these workers can't break isolation.
--- kernel/workqueue.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
kernel/workqueue.c +13 -0
@@ -6967,6 +6967,10 @@ static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) }if (!ret) { +int cpu; +struct worker_pool *pool; +struct worker *worker; +mutex_lock (&wq_pool_attach_mutex);cpumask_copy (wq_unbound_cpumask, unbound_cpumask);/* rescuer needs to respect cpumask changes when it is not attached */
@@ -6974,6 +6978,15 @@ static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask)if (wq->rescuer && !wq->rescuer->pool)unbind_worker (wq->rescuer); } +/* DISASSOCIATED worker needs to respect wq_unbound_cpumask */ +for_each_possible_cpu (cpu) { +for_each_cpu_worker_pool (pool, cpu) { +if (!(pool->flags &POOL_DISASSOCIATED )) +continue ; +for_each_pool_worker (worker, pool) +unbind_worker (worker); + } + }mutex_unlock (&wq_pool_attach_mutex); }return ret;
Signature · 1 line
2.19.1.6.gb485710b
Lai Jiangshan
reviewerPATCH 3/3 workqueue: Init rescuer's affinities as wq_unbound_cpumask
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
The affinity to set to the rescuers should be consistent in all paths when a rescuer is in detached state. The affinity could be either wq_unbound_cpumask or unbound_effective_cpumask(wq).
Related paths:
rescuer's worker_detach_from_pool()
update wq_unbound_cpumask
update wq's cpumask
init_rescuer()
Both affinities are Ok as long as they are consistent in all paths.
In the commit 449b31ad2937 ("workqueue: Init rescuer's affinities as the wq's effective cpumask") makes init_rescuer use unbound_effective_cpumask(wq) which is consistent with then apply_wqattrs_commit().
But using unbound_effective_cpumask(wq) requres much more code to maintain the consistency, and it doesn't make much sense since the affinity is only effective when the rescuer is not processing works. wq_unbound_cpumask is more favorable.
So apply_wqattrs_commit() and the path of "updating wq's cpumask" had been changed to not update the rescuer's affinity, and both the paths of "updating wq_unbound_cpumask" and "rescuer's worker_detach_from_pool()" had been changed to use wq_unbound_cpumask.
Now, make init_rescuer() use wq_unbound_cpumask for rescuer's affinity and make all the paths consistent.
--- kernel/workqueue.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/workqueue.c +5 -2
@@ -5644,10 +5644,13 @@ static int init_rescuer(struct workqueue_struct *wq) } wq->rescuer = rescuer; -if (wq->flags &WQ_UNBOUND ) -kthread_bind_mask (rescuer->task,unbound_effective_cpumask (wq)); + +/* initial cpumask is consistent with the detached rescuer and unbind_worker() */ +if (cpumask_intersects (wq_unbound_cpumask, cpu_active_mask)) +kthread_bind_mask (rescuer->task, wq_unbound_cpumask);else kthread_bind_mask (rescuer->task, cpu_possible_mask); +wake_up_process (rescuer->task);return 0 ;
Signature · 1 line
2.19.1.6.gb485710b
Tejun Heo
maintainerQuoted 4 lines · Lai Jiangshan
Lai Jiangshan (3):
workqueue: Update the rescuer's affinity only when it is detached
workqueue: Let DISASSOCIATED workers follow unbound wq cpumask changes
workqueue: Init rescuer's affinities aswq_unbound_cpumask
Applied 1-3 to wq/for-6.19.
Thanks.
--
tejun