Skip to content

Commit a2d812a

Browse files
committed
workqueue: Make worker_attach/detach_pool() update worker->pool
For historical reasons, the worker attach/detach functions don't currently manage worker->pool and the callers are manually and inconsistently updating it. This patch moves worker->pool updates into the worker attach/detach functions. This makes worker->pool consistent and clearly defines how worker->pool updates are synchronized. This will help later workqueue visibility improvements by allowing safe access to workqueue information from worker->task. Signed-off-by: Tejun Heo <[email protected]>
1 parent 1258fae commit a2d812a

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

kernel/workqueue.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,26 +1741,29 @@ static void worker_attach_to_pool(struct worker *worker,
17411741
worker->flags |= WORKER_UNBOUND;
17421742

17431743
list_add_tail(&worker->node, &pool->workers);
1744+
worker->pool = pool;
17441745

17451746
mutex_unlock(&wq_pool_attach_mutex);
17461747
}
17471748

17481749
/**
17491750
* worker_detach_from_pool() - detach a worker from its pool
17501751
* @worker: worker which is attached to its pool
1751-
* @pool: the pool @worker is attached to
17521752
*
17531753
* Undo the attaching which had been done in worker_attach_to_pool(). The
17541754
* caller worker shouldn't access to the pool after detached except it has
17551755
* other reference to the pool.
17561756
*/
1757-
static void worker_detach_from_pool(struct worker *worker,
1758-
struct worker_pool *pool)
1757+
static void worker_detach_from_pool(struct worker *worker)
17591758
{
1759+
struct worker_pool *pool = worker->pool;
17601760
struct completion *detach_completion = NULL;
17611761

17621762
mutex_lock(&wq_pool_attach_mutex);
1763+
17631764
list_del(&worker->node);
1765+
worker->pool = NULL;
1766+
17641767
if (list_empty(&pool->workers))
17651768
detach_completion = pool->detach_completion;
17661769
mutex_unlock(&wq_pool_attach_mutex);
@@ -1799,7 +1802,6 @@ static struct worker *create_worker(struct worker_pool *pool)
17991802
if (!worker)
18001803
goto fail;
18011804

1802-
worker->pool = pool;
18031805
worker->id = id;
18041806

18051807
if (pool->cpu >= 0)
@@ -2236,7 +2238,7 @@ static int worker_thread(void *__worker)
22362238

22372239
set_task_comm(worker->task, "kworker/dying");
22382240
ida_simple_remove(&pool->worker_ida, worker->id);
2239-
worker_detach_from_pool(worker, pool);
2241+
worker_detach_from_pool(worker);
22402242
kfree(worker);
22412243
return 0;
22422244
}
@@ -2367,7 +2369,6 @@ static int rescuer_thread(void *__rescuer)
23672369
worker_attach_to_pool(rescuer, pool);
23682370

23692371
spin_lock_irq(&pool->lock);
2370-
rescuer->pool = pool;
23712372

23722373
/*
23732374
* Slurp in all works issued via this workqueue and
@@ -2417,10 +2418,9 @@ static int rescuer_thread(void *__rescuer)
24172418
if (need_more_worker(pool))
24182419
wake_up_worker(pool);
24192420

2420-
rescuer->pool = NULL;
24212421
spin_unlock_irq(&pool->lock);
24222422

2423-
worker_detach_from_pool(rescuer, pool);
2423+
worker_detach_from_pool(rescuer);
24242424

24252425
spin_lock_irq(&wq_mayday_lock);
24262426
}

kernel/workqueue_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct worker {
3737
/* 64 bytes boundary on 64bit, 32 on 32bit */
3838

3939
struct task_struct *task; /* I: worker task */
40-
struct worker_pool *pool; /* I: the associated pool */
40+
struct worker_pool *pool; /* A: the associated pool */
4141
/* L: for rescuers */
4242
struct list_head node; /* A: anchored at pool->workers */
4343
/* A: runs through worker->node */

0 commit comments

Comments
 (0)