Skip to content

Commit 1258fae

Browse files
committed
workqueue: Replace pool->attach_mutex with global wq_pool_attach_mutex
To improve workqueue visibility, we want to be able to access workqueue information from worker tasks. The per-pool attach mutex makes that difficult because there's no way of stabilizing task -> worker pool association without knowing the pool first. Worker attach/detach is a slow path and there's no need for different pools to be able to perform them concurrently. This patch replaces the per-pool attach_mutex with global wq_pool_attach_mutex to prepare for visibility improvement changes. Signed-off-by: Tejun Heo <[email protected]>
1 parent e6506eb commit 1258fae

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

kernel/workqueue.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ enum {
6666
* be executing on any CPU. The pool behaves as an unbound one.
6767
*
6868
* Note that DISASSOCIATED should be flipped only while holding
69-
* attach_mutex to avoid changing binding state while
69+
* wq_pool_attach_mutex to avoid changing binding state while
7070
* worker_attach_to_pool() is in progress.
7171
*/
7272
POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */
@@ -123,7 +123,7 @@ enum {
123123
* cpu or grabbing pool->lock is enough for read access. If
124124
* POOL_DISASSOCIATED is set, it's identical to L.
125125
*
126-
* A: pool->attach_mutex protected.
126+
* A: wq_pool_attach_mutex protected.
127127
*
128128
* PL: wq_pool_mutex protected.
129129
*
@@ -166,7 +166,6 @@ struct worker_pool {
166166
/* L: hash of busy workers */
167167

168168
struct worker *manager; /* L: purely informational */
169-
struct mutex attach_mutex; /* attach/detach exclusion */
170169
struct list_head workers; /* A: attached workers */
171170
struct completion *detach_completion; /* all workers detached */
172171

@@ -297,6 +296,7 @@ static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
297296
static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
298297

299298
static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
299+
static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */
300300
static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
301301
static DECLARE_WAIT_QUEUE_HEAD(wq_manager_wait); /* wait for manager to go away */
302302

@@ -399,14 +399,14 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
399399
* @worker: iteration cursor
400400
* @pool: worker_pool to iterate workers of
401401
*
402-
* This must be called with @pool->attach_mutex.
402+
* This must be called with wq_pool_attach_mutex.
403403
*
404404
* The if/else clause exists only for the lockdep assertion and can be
405405
* ignored.
406406
*/
407407
#define for_each_pool_worker(worker, pool) \
408408
list_for_each_entry((worker), &(pool)->workers, node) \
409-
if (({ lockdep_assert_held(&pool->attach_mutex); false; })) { } \
409+
if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \
410410
else
411411

412412
/**
@@ -1724,7 +1724,7 @@ static struct worker *alloc_worker(int node)
17241724
static void worker_attach_to_pool(struct worker *worker,
17251725
struct worker_pool *pool)
17261726
{
1727-
mutex_lock(&pool->attach_mutex);
1727+
mutex_lock(&wq_pool_attach_mutex);
17281728

17291729
/*
17301730
* set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
@@ -1733,16 +1733,16 @@ static void worker_attach_to_pool(struct worker *worker,
17331733
set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
17341734

17351735
/*
1736-
* The pool->attach_mutex ensures %POOL_DISASSOCIATED remains
1737-
* stable across this function. See the comments above the
1738-
* flag definition for details.
1736+
* The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains
1737+
* stable across this function. See the comments above the flag
1738+
* definition for details.
17391739
*/
17401740
if (pool->flags & POOL_DISASSOCIATED)
17411741
worker->flags |= WORKER_UNBOUND;
17421742

17431743
list_add_tail(&worker->node, &pool->workers);
17441744

1745-
mutex_unlock(&pool->attach_mutex);
1745+
mutex_unlock(&wq_pool_attach_mutex);
17461746
}
17471747

17481748
/**
@@ -1759,11 +1759,11 @@ static void worker_detach_from_pool(struct worker *worker,
17591759
{
17601760
struct completion *detach_completion = NULL;
17611761

1762-
mutex_lock(&pool->attach_mutex);
1762+
mutex_lock(&wq_pool_attach_mutex);
17631763
list_del(&worker->node);
17641764
if (list_empty(&pool->workers))
17651765
detach_completion = pool->detach_completion;
1766-
mutex_unlock(&pool->attach_mutex);
1766+
mutex_unlock(&wq_pool_attach_mutex);
17671767

17681768
/* clear leftover flags without pool->lock after it is detached */
17691769
worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND);
@@ -3271,7 +3271,6 @@ static int init_worker_pool(struct worker_pool *pool)
32713271

32723272
timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0);
32733273

3274-
mutex_init(&pool->attach_mutex);
32753274
INIT_LIST_HEAD(&pool->workers);
32763275

32773276
ida_init(&pool->worker_ida);
@@ -3354,10 +3353,10 @@ static void put_unbound_pool(struct worker_pool *pool)
33543353
WARN_ON(pool->nr_workers || pool->nr_idle);
33553354
spin_unlock_irq(&pool->lock);
33563355

3357-
mutex_lock(&pool->attach_mutex);
3356+
mutex_lock(&wq_pool_attach_mutex);
33583357
if (!list_empty(&pool->workers))
33593358
pool->detach_completion = &detach_completion;
3360-
mutex_unlock(&pool->attach_mutex);
3359+
mutex_unlock(&wq_pool_attach_mutex);
33613360

33623361
if (pool->detach_completion)
33633362
wait_for_completion(pool->detach_completion);
@@ -4600,7 +4599,7 @@ static void unbind_workers(int cpu)
46004599
struct worker *worker;
46014600

46024601
for_each_cpu_worker_pool(pool, cpu) {
4603-
mutex_lock(&pool->attach_mutex);
4602+
mutex_lock(&wq_pool_attach_mutex);
46044603
spin_lock_irq(&pool->lock);
46054604

46064605
/*
@@ -4616,7 +4615,7 @@ static void unbind_workers(int cpu)
46164615
pool->flags |= POOL_DISASSOCIATED;
46174616

46184617
spin_unlock_irq(&pool->lock);
4619-
mutex_unlock(&pool->attach_mutex);
4618+
mutex_unlock(&wq_pool_attach_mutex);
46204619

46214620
/*
46224621
* Call schedule() so that we cross rq->lock and thus can
@@ -4657,7 +4656,7 @@ static void rebind_workers(struct worker_pool *pool)
46574656
{
46584657
struct worker *worker;
46594658

4660-
lockdep_assert_held(&pool->attach_mutex);
4659+
lockdep_assert_held(&wq_pool_attach_mutex);
46614660

46624661
/*
46634662
* Restore CPU affinity of all workers. As all idle workers should
@@ -4727,7 +4726,7 @@ static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
47274726
static cpumask_t cpumask;
47284727
struct worker *worker;
47294728

4730-
lockdep_assert_held(&pool->attach_mutex);
4729+
lockdep_assert_held(&wq_pool_attach_mutex);
47314730

47324731
/* is @cpu allowed for @pool? */
47334732
if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
@@ -4762,14 +4761,14 @@ int workqueue_online_cpu(unsigned int cpu)
47624761
mutex_lock(&wq_pool_mutex);
47634762

47644763
for_each_pool(pool, pi) {
4765-
mutex_lock(&pool->attach_mutex);
4764+
mutex_lock(&wq_pool_attach_mutex);
47664765

47674766
if (pool->cpu == cpu)
47684767
rebind_workers(pool);
47694768
else if (pool->cpu < 0)
47704769
restore_unbound_workers_cpumask(pool, cpu);
47714770

4772-
mutex_unlock(&pool->attach_mutex);
4771+
mutex_unlock(&wq_pool_attach_mutex);
47734772
}
47744773

47754774
/* update NUMA affinity of unbound workqueues */

0 commit comments

Comments
 (0)