Skip to content

Commit 4736cbf

Browse files
Lai Jiangshanhtejun
authored andcommitted
workqueue: separate pool-attaching code out from create_worker()
Currently, the code to attach a new worker to its pool is embedded in create_worker(). Separating this code out will make the codes clearer and will allow rescuers to share the code path later. tj: Description and comment updates. Signed-off-by: Lai Jiangshan <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 92f9c5c commit 4736cbf

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

kernel/workqueue.c

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ enum {
6666
*
6767
* Note that DISASSOCIATED should be flipped only while holding
6868
* attach_mutex to avoid changing binding state while
69-
* create_worker() is in progress.
69+
* worker_attach_to_pool() is in progress.
7070
*/
7171
POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
7272
POOL_FREEZING = 1 << 3, /* freeze in progress */
@@ -1682,14 +1682,47 @@ static struct worker *alloc_worker(void)
16821682
return worker;
16831683
}
16841684

1685+
/**
1686+
* worker_attach_to_pool() - attach a worker to a pool
1687+
* @worker: worker to be attached
1688+
* @pool: the target pool
1689+
*
1690+
* Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and
1691+
* cpu-binding of @worker are kept coordinated with the pool across
1692+
* cpu-[un]hotplugs.
1693+
*/
1694+
static void worker_attach_to_pool(struct worker *worker,
1695+
struct worker_pool *pool)
1696+
{
1697+
mutex_lock(&pool->attach_mutex);
1698+
1699+
/*
1700+
* set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1701+
* online CPUs. It'll be re-applied when any of the CPUs come up.
1702+
*/
1703+
set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
1704+
1705+
/*
1706+
* The pool->attach_mutex ensures %POOL_DISASSOCIATED remains
1707+
* stable across this function. See the comments above the
1708+
* flag definition for details.
1709+
*/
1710+
if (pool->flags & POOL_DISASSOCIATED)
1711+
worker->flags |= WORKER_UNBOUND;
1712+
1713+
list_add_tail(&worker->node, &pool->workers);
1714+
1715+
mutex_unlock(&pool->attach_mutex);
1716+
}
1717+
16851718
/**
16861719
* worker_detach_from_pool() - detach a worker from its pool
16871720
* @worker: worker which is attached to its pool
16881721
* @pool: the pool @worker is attached to
16891722
*
1690-
* Undo the attaching which had been done in create_worker(). The caller
1691-
* worker shouldn't access to the pool after detached except it has other
1692-
* reference to the pool.
1723+
* Undo the attaching which had been done in worker_attach_to_pool(). The
1724+
* caller worker shouldn't access to the pool after detached except it has
1725+
* other reference to the pool.
16931726
*/
16941727
static void worker_detach_from_pool(struct worker *worker,
16951728
struct worker_pool *pool)
@@ -1753,26 +1786,8 @@ static struct worker *create_worker(struct worker_pool *pool)
17531786
/* prevent userland from meddling with cpumask of workqueue workers */
17541787
worker->task->flags |= PF_NO_SETAFFINITY;
17551788

1756-
mutex_lock(&pool->attach_mutex);
1757-
1758-
/*
1759-
* set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1760-
* online CPUs. It'll be re-applied when any of the CPUs come up.
1761-
*/
1762-
set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
1763-
1764-
/*
1765-
* The pool->attach_mutex ensures %POOL_DISASSOCIATED
1766-
* remains stable across this function. See the comments above the
1767-
* flag definition for details.
1768-
*/
1769-
if (pool->flags & POOL_DISASSOCIATED)
1770-
worker->flags |= WORKER_UNBOUND;
1771-
17721789
/* successful, attach the worker to the pool */
1773-
list_add_tail(&worker->node, &pool->workers);
1774-
1775-
mutex_unlock(&pool->attach_mutex);
1790+
worker_attach_to_pool(worker, pool);
17761791

17771792
return worker;
17781793

0 commit comments

Comments
 (0)