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: Tejun Heo <tj@kernel.org>
Reviewed-by: Lai Jiangshan <laijs@cn.fujitsu.com>
This commit is contained in:
Tejun Heo 2013-03-12 11:29:58 -07:00
parent 1711696955
commit 24b8a84718

View file

@ -3595,6 +3595,8 @@ EXPORT_SYMBOL_GPL(work_on_cpu);
void freeze_workqueues_begin(void)
{
struct worker_pool *pool;
struct workqueue_struct *wq;
struct pool_workqueue *pwq;
int id;
spin_lock_irq(&workqueue_lock);
@ -3602,25 +3604,26 @@ void freeze_workqueues_begin(void)
WARN_ON_ONCE(workqueue_freezing);
workqueue_freezing = true;
/* set FREEZING */
for_each_pool(pool, id) {
struct workqueue_struct *wq;
spin_lock(&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(pool->cpu, wq);
if (pwq && pwq->pool == pool &&
(wq->flags & WQ_FREEZABLE))
pwq->max_active = 0;
}
spin_unlock(&pool->lock);
}
/* suppress further executions by setting max_active to zero */
list_for_each_entry(wq, &workqueues, list) {
if (!(wq->flags & WQ_FREEZABLE))
continue;
for_each_pwq(pwq, wq) {
spin_lock(&pwq->pool->lock);
pwq->max_active = 0;
spin_unlock(&pwq->pool->lock);
}
}
spin_unlock_irq(&workqueue_lock);
}
@ -3639,25 +3642,22 @@ void freeze_workqueues_begin(void)
*/
bool freeze_workqueues_busy(void)
{
unsigned int cpu;
bool busy = false;
struct workqueue_struct *wq;
struct pool_workqueue *pwq;
spin_lock_irq(&workqueue_lock);
WARN_ON_ONCE(!workqueue_freezing);
for_each_wq_cpu(cpu) {
struct workqueue_struct *wq;
list_for_each_entry(wq, &workqueues, list) {
if (!(wq->flags & WQ_FREEZABLE))
continue;
/*
* nr_active is monotonically decreasing. It's safe
* to peek without lock.
*/
list_for_each_entry(wq, &workqueues, list) {
struct pool_workqueue *pwq = get_pwq(cpu, wq);
if (!pwq || !(wq->flags & WQ_FREEZABLE))
continue;
for_each_pwq(pwq, wq) {
WARN_ON_ONCE(pwq->nr_active < 0);
if (pwq->nr_active) {
busy = true;
@ -3681,40 +3681,43 @@ bool freeze_workqueues_busy(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);
if (!workqueue_freezing)
goto out_unlock;
for_each_wq_cpu(cpu) {
struct worker_pool *pool;
struct workqueue_struct *wq;
/* clear FREEZING */
for_each_pool(pool, id) {
spin_lock(&pool->lock);
WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
pool->flags &= ~POOL_FREEZING;
spin_unlock(&pool->lock);
}
for_each_std_worker_pool(pool, cpu) {
spin_lock(&pool->lock);
/* restore max_active and repopulate worklist */
list_for_each_entry(wq, &workqueues, list) {
if (!(wq->flags & WQ_FREEZABLE))
continue;
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);
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;
out_unlock:
spin_unlock_irq(&workqueue_lock);