History

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.

Tejun Heo committed 8 years ago

Commit a2d812a1 parent 1258fae2 files · +9 −9Browse files

Showing the changes to kernel/workqueue.c (1 of 2 files). Show all

Unified · Split

kernel/workqueue.c+8−8
@@ -1741,6 +1741,7 @@ static void worker_attach_to_pool(struct worker *worker, · 1740 unmodified lines
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 }
@@ -1748,19 +1749,21 @@ static void worker_attach_to_pool(struct worker *worker, · 1 unmodified line
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) · 32 unmodified lines
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 @@ woke_up: · 430 unmodified lines
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 @@ repeat: · 124 unmodified lines
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 @@ repeat: · 43 unmodified lines
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 }