Commit 24b8a847 authored by Tejun Heo's avatar Tejun Heo

workqueue: restructure pool / pool_workqueue iterations in freeze/thaw functions

The three freeze/thaw related functions - freeze_workqueues_begin(),
freeze_workqueues_busy() and thaw_workqueues() - need to iterate
through all pool_workqueues of all freezable workqueues.  They did it
by first iterating pools and then visiting all pwqs (pool_workqueues)
of all workqueues and process it if its pwq->pool matches the current
pool.  This is rather backwards and done this way partly because
workqueue didn't have fitting iteration helpers and partly to avoid
the number of lock operations on pool->lock.

Workqueue now has fitting iterators and the locking operation overhead
isn't anything to worry about - those locks are unlikely to be
contended and the same CPU visiting the same set of locks multiple
times isn't expensive.

Restructure the three functions such that the flow better matches the
logical steps and pwq iteration is done using for_each_pwq() inside
workqueue iteration.

* freeze_workqueues_begin(): Setting of FREEZING is moved into a
  separate for_each_pool() iteration.  pwq iteration for clearing
  max_active is updated as described above.

* freeze_workqueues_busy(): pwq iteration updated as described above.

* thaw_workqueues(): The single for_each_wq_cpu() iteration is broken
  into three discrete steps - clearing FREEZING, restoring max_active,
  and kicking workers.  The first and last steps use for_each_pool()
  and the second step uses pwq iteration described above.

This makes the code easier to understand and removes the use of
for_each_wq_cpu() for walking pwqs, which can't support multiple
unbound pwqs which will be needed to implement unbound workqueues with
custom attributes.

This patch doesn't introduce any visible behavior changes.
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Reviewed-by: default avatarLai Jiangshan <laijs@cn.fujitsu.com>
parent 17116969
...@@ -3595,6 +3595,8 @@ EXPORT_SYMBOL_GPL(work_on_cpu); ...@@ -3595,6 +3595,8 @@ EXPORT_SYMBOL_GPL(work_on_cpu);
void freeze_workqueues_begin(void) void freeze_workqueues_begin(void)
{ {
struct worker_pool *pool; struct worker_pool *pool;
struct workqueue_struct *wq;
struct pool_workqueue *pwq;
int id; int id;
spin_lock_irq(&workqueue_lock); spin_lock_irq(&workqueue_lock);
...@@ -3602,23 +3604,24 @@ void freeze_workqueues_begin(void) ...@@ -3602,23 +3604,24 @@ void freeze_workqueues_begin(void)
WARN_ON_ONCE(workqueue_freezing); WARN_ON_ONCE(workqueue_freezing);
workqueue_freezing = true; workqueue_freezing = true;
/* set FREEZING */
for_each_pool(pool, id) { for_each_pool(pool, id) {
struct workqueue_struct *wq;
spin_lock(&pool->lock); spin_lock(&pool->lock);
WARN_ON_ONCE(pool->flags & POOL_FREEZING); WARN_ON_ONCE(pool->flags & POOL_FREEZING);
pool->flags |= POOL_FREEZING; pool->flags |= POOL_FREEZING;
spin_unlock(&pool->lock);
}
list_for_each_entry(wq, &workqueues, list) { /* suppress further executions by setting max_active to zero */
struct pool_workqueue *pwq = get_pwq(pool->cpu, wq); list_for_each_entry(wq, &workqueues, list) {
if (!(wq->flags & WQ_FREEZABLE))
continue;
if (pwq && pwq->pool == pool && for_each_pwq(pwq, wq) {
(wq->flags & WQ_FREEZABLE)) spin_lock(&pwq->pool->lock);
pwq->max_active = 0; pwq->max_active = 0;
spin_unlock(&pwq->pool->lock);
} }
spin_unlock(&pool->lock);
} }
spin_unlock_irq(&workqueue_lock); spin_unlock_irq(&workqueue_lock);
...@@ -3639,25 +3642,22 @@ void freeze_workqueues_begin(void) ...@@ -3639,25 +3642,22 @@ void freeze_workqueues_begin(void)
*/ */
bool freeze_workqueues_busy(void) bool freeze_workqueues_busy(void)
{ {
unsigned int cpu;
bool busy = false; bool busy = false;
struct workqueue_struct *wq;
struct pool_workqueue *pwq;
spin_lock_irq(&workqueue_lock); spin_lock_irq(&workqueue_lock);
WARN_ON_ONCE(!workqueue_freezing); WARN_ON_ONCE(!workqueue_freezing);
for_each_wq_cpu(cpu) { list_for_each_entry(wq, &workqueues, list) {
struct workqueue_struct *wq; if (!(wq->flags & WQ_FREEZABLE))
continue;
/* /*
* nr_active is monotonically decreasing. It's safe * nr_active is monotonically decreasing. It's safe
* to peek without lock. * to peek without lock.
*/ */
list_for_each_entry(wq, &workqueues, list) { for_each_pwq(pwq, wq) {
struct pool_workqueue *pwq = get_pwq(cpu, wq);
if (!pwq || !(wq->flags & WQ_FREEZABLE))
continue;
WARN_ON_ONCE(pwq->nr_active < 0); WARN_ON_ONCE(pwq->nr_active < 0);
if (pwq->nr_active) { if (pwq->nr_active) {
busy = true; busy = true;
...@@ -3681,40 +3681,43 @@ bool freeze_workqueues_busy(void) ...@@ -3681,40 +3681,43 @@ bool freeze_workqueues_busy(void)
*/ */
void thaw_workqueues(void) void thaw_workqueues(void)
{ {
unsigned int cpu; struct workqueue_struct *wq;
struct pool_workqueue *pwq;
struct worker_pool *pool;
int id;
spin_lock_irq(&workqueue_lock); spin_lock_irq(&workqueue_lock);
if (!workqueue_freezing) if (!workqueue_freezing)
goto out_unlock; goto out_unlock;
for_each_wq_cpu(cpu) { /* clear FREEZING */
struct worker_pool *pool; for_each_pool(pool, id) {
struct workqueue_struct *wq; spin_lock(&pool->lock);
WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
for_each_std_worker_pool(pool, cpu) { pool->flags &= ~POOL_FREEZING;
spin_lock(&pool->lock); spin_unlock(&pool->lock);
}
WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
pool->flags &= ~POOL_FREEZING;
list_for_each_entry(wq, &workqueues, list) {
struct pool_workqueue *pwq = get_pwq(cpu, wq);
if (!pwq || pwq->pool != pool ||
!(wq->flags & WQ_FREEZABLE))
continue;
/* restore max_active and repopulate worklist */
pwq_set_max_active(pwq, wq->saved_max_active);
}
wake_up_worker(pool); /* restore max_active and repopulate worklist */
list_for_each_entry(wq, &workqueues, list) {
if (!(wq->flags & WQ_FREEZABLE))
continue;
spin_unlock(&pool->lock); for_each_pwq(pwq, wq) {
spin_lock(&pwq->pool->lock);
pwq_set_max_active(pwq, wq->saved_max_active);
spin_unlock(&pwq->pool->lock);
} }
} }
/* kick workers */
for_each_pool(pool, id) {
spin_lock(&pool->lock);
wake_up_worker(pool);
spin_unlock(&pool->lock);
}
workqueue_freezing = false; workqueue_freezing = false;
out_unlock: out_unlock:
spin_unlock_irq(&workqueue_lock); spin_unlock_irq(&workqueue_lock);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment