History

workqueue: separate iteration role from worker_idr

worker_idr has the iteration (iterating for attached workers) and worker ID duties. These two duties don't have to be tied together. We can separate them and use a list for tracking attached workers and iteration. Before this separation, it wasn't possible to add rescuer workers to worker_idr due to rescuer workers couldn't allocate ID dynamically because ID-allocation depends on memory-allocation, which rescuer can't depend on. After separation, we can easily add the rescuer workers to the list for iteration without any memory-allocation. It is required when we attach the rescuer worker to the pool in later patch. tj: Minor description updates.

Lai Jiangshan committed 12 years ago

Commit da028461 parent 3347fc92 files · +17 −13Browse files

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

Unified · Split

kernel/workqueue.c+15−13
@@ -161,7 +161,8 @@ struct worker_pool { · 160 unmodified lines
161161 /* see manage_workers() for details on the two manager mutexes */
162162 struct mutex manager_arb; /* manager arbitration */
163163 struct mutex manager_mutex; /* manager exclusion */
164− struct idr worker_idr; /* M: worker IDs and iteration */
164+ struct idr worker_idr; /* M: worker IDs */
165+ struct list_head workers; /* M: attached workers */
165166 struct completion *detach_completion; /* all workers detached */
166167
167168 struct workqueue_attrs *attrs; /* I: worker attributes */
@@ -363,7 +364,6 @@ static void copy_workqueue_attrs(struct workqueue_attrs *to, · 195 unmodified lines
363364 /**
364365 * for_each_pool_worker - iterate through all workers of a worker_pool
365366 * @worker: iteration cursor
366− * @wi: integer used for iteration
367367 * @pool: worker_pool to iterate workers of
368368 *
369369 * This must be called with @pool->manager_mutex.
@@ -371,8 +371,8 @@ static void copy_workqueue_attrs(struct workqueue_attrs *to, · 1 unmodified line
371371 * The if/else clause exists only for the lockdep assertion and can be
372372 * ignored.
373373 */
374−#define for_each_pool_worker(worker, wi, pool) \
375− idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
374+#define for_each_pool_worker(worker, pool) \
375+ list_for_each_entry((worker), &(pool)->workers, node) \
376376 if (({ lockdep_assert_held(&pool->manager_mutex); false; })) { } \
377377 else
378378
@@ -1674,6 +1674,7 @@ static struct worker *alloc_worker(void) · 1295 unmodified lines
16741674 if (worker) {
16751675 INIT_LIST_HEAD(&worker->entry);
16761676 INIT_LIST_HEAD(&worker->scheduled);
1677+ INIT_LIST_HEAD(&worker->node);
16771678 /* on creation a worker is in !idle && prep state */
16781679 worker->flags = WORKER_PREP;
16791680 }
@@ -1696,7 +1697,8 @@ static void worker_detach_from_pool(struct worker *worker, · 16 unmodified lines
16961697
16971698 mutex_lock(&pool->manager_mutex);
16981699 idr_remove(&pool->worker_idr, worker->id);
1699− if (idr_is_empty(&pool->worker_idr))
1700+ list_del(&worker->node);
1701+ if (list_empty(&pool->workers))
17001702 detach_completion = pool->detach_completion;
17011703 mutex_unlock(&pool->manager_mutex);
17021704
@@ -1772,6 +1774,8 @@ static struct worker *create_worker(struct worker_pool *pool) · 69 unmodified lines
17721774
17731775 /* successful, commit the pointer to idr */
17741776 idr_replace(&pool->worker_idr, worker, worker->id);
1777+ /* successful, attach the worker to the pool */
1778+ list_add_tail(&worker->node, &pool->workers);
17751779
17761780 return worker;
17771781
@@ -3483,6 +3487,7 @@ static int init_worker_pool(struct worker_pool *pool) · 1705 unmodified lines
34833487 mutex_init(&pool->manager_arb);
34843488 mutex_init(&pool->manager_mutex);
34853489 idr_init(&pool->worker_idr);
3490+ INIT_LIST_HEAD(&pool->workers);
34863491
34873492 INIT_HLIST_NODE(&pool->hash_node);
34883493 pool->refcnt = 1;
@@ -3548,7 +3553,7 @@ static void put_unbound_pool(struct worker_pool *pool) · 59 unmodified lines
35483553 spin_unlock_irq(&pool->lock);
35493554
35503555 mutex_lock(&pool->manager_mutex);
3551− if (!idr_is_empty(&pool->worker_idr))
3556+ if (!list_empty(&pool->workers))
35523557 pool->detach_completion = &detach_completion;
35533558 mutex_unlock(&pool->manager_mutex);
35543559
@@ -4533,7 +4538,6 @@ static void wq_unbind_fn(struct work_struct *work) · 978 unmodified lines
45334538 int cpu = smp_processor_id();
45344539 struct worker_pool *pool;
45354540 struct worker *worker;
4536− int wi;
45374541
45384542 for_each_cpu_worker_pool(pool, cpu) {
45394543 WARN_ON_ONCE(cpu != smp_processor_id());
@@ -4548,7 +4552,7 @@ static void wq_unbind_fn(struct work_struct *work) · 8 unmodified lines
45484552 * before the last CPU down must be on the cpu. After
45494553 * this, they may become diasporas.
45504554 */
4551− for_each_pool_worker(worker, wi, pool)
4555+ for_each_pool_worker(worker, pool)
45524556 worker->flags |= WORKER_UNBOUND;
45534557
45544558 pool->flags |= POOL_DISASSOCIATED;
@@ -4594,7 +4598,6 @@ static void wq_unbind_fn(struct work_struct *work) · 39 unmodified lines
45944598 static void rebind_workers(struct worker_pool *pool)
45954599 {
45964600 struct worker *worker;
4597− int wi;
45984601
45994602 lockdep_assert_held(&pool->manager_mutex);
46004603
@@ -4605,13 +4608,13 @@ static void rebind_workers(struct worker_pool *pool) · 4 unmodified lines
46054608 * of all workers first and then clear UNBOUND. As we're called
46064609 * from CPU_ONLINE, the following shouldn't fail.
46074610 */
4608− for_each_pool_worker(worker, wi, pool)
4611+ for_each_pool_worker(worker, pool)
46094612 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
46104613 pool->attrs->cpumask) < 0);
46114614
46124615 spin_lock_irq(&pool->lock);
46134616
4614− for_each_pool_worker(worker, wi, pool) {
4617+ for_each_pool_worker(worker, pool) {
46154618 unsigned int worker_flags = worker->flags;
46164619
46174620 /*
@@ -4663,7 +4666,6 @@ static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) · 45 unmodified lines
46634666 {
46644667 static cpumask_t cpumask;
46654668 struct worker *worker;
4666− int wi;
46674669
46684670 lockdep_assert_held(&pool->manager_mutex);
46694671
@@ -4677,7 +4679,7 @@ static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) · 7 unmodified lines
46774679 return;
46784680
46794681 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4680− for_each_pool_worker(worker, wi, pool)
4682+ for_each_pool_worker(worker, pool)
46814683 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
46824684 pool->attrs->cpumask) < 0);
46834685 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t