aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2012-07-17 15:39:27 -0400
committerTejun Heo <tj@kernel.org>2012-07-17 15:39:27 -0400
commit628c78e7ea19d5b70d2b6a59030362168cdbe1ad (patch)
tree7867a9f82aae3d31c40356f32ae24223ae0ddf0c
parent3ce63377305b694f53e7dd0c72907591c5344224 (diff)
workqueue: remove CPU offline trustee
With the previous changes, a disassociated global_cwq now can run as an unbound one on its own - it can create workers as necessary to drain remaining works after the CPU has been brought down and manage the number of workers using the usual idle timer mechanism making trustee completely redundant except for the actual unbinding operation. This patch removes the trustee and let a disassociated global_cwq manage itself. Unbinding is moved to a work item (for CPU affinity) which is scheduled and flushed from CPU_DONW_PREPARE. This patch moves nr_running clearing outside gcwq and manager locks to simplify the code. As nr_running is unused at the point, this is safe. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: "Rafael J. Wysocki" <rjw@sisk.pl>
-rw-r--r--kernel/workqueue.c288
1 files changed, 36 insertions, 252 deletions
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index acfabb22e2c4..d1545daa74ad 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -79,13 +79,6 @@ enum {
79 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND | 79 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND |
80 WORKER_CPU_INTENSIVE, 80 WORKER_CPU_INTENSIVE,
81 81
82 /* gcwq->trustee_state */
83 TRUSTEE_START = 0, /* start */
84 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
85 TRUSTEE_BUTCHER = 2, /* butcher workers */
86 TRUSTEE_RELEASE = 3, /* release workers */
87 TRUSTEE_DONE = 4, /* trustee is done */
88
89 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */ 82 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
90 83
91 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 84 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
@@ -100,7 +93,6 @@ enum {
100 (min two ticks) */ 93 (min two ticks) */
101 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 94 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
102 CREATE_COOLDOWN = HZ, /* time to breath after fail */ 95 CREATE_COOLDOWN = HZ, /* time to breath after fail */
103 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
104 96
105 /* 97 /*
106 * Rescue workers are used only on emergencies and shared by 98 * Rescue workers are used only on emergencies and shared by
@@ -194,10 +186,6 @@ struct global_cwq {
194 struct worker_pool pools[2]; /* normal and highpri pools */ 186 struct worker_pool pools[2]; /* normal and highpri pools */
195 187
196 wait_queue_head_t rebind_hold; /* rebind hold wait */ 188 wait_queue_head_t rebind_hold; /* rebind hold wait */
197
198 struct task_struct *trustee; /* L: for gcwq shutdown */
199 unsigned int trustee_state; /* L: trustee state */
200 wait_queue_head_t trustee_wait; /* trustee wait */
201} ____cacheline_aligned_in_smp; 189} ____cacheline_aligned_in_smp;
202 190
203/* 191/*
@@ -753,11 +741,11 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task,
753 * worklist not empty test sequence is in insert_work(). 741 * worklist not empty test sequence is in insert_work().
754 * Please read comment there. 742 * Please read comment there.
755 * 743 *
756 * NOT_RUNNING is clear. This means that trustee is not in 744 * NOT_RUNNING is clear. This means that we're bound to and
757 * charge and we're running on the local cpu w/ rq lock held 745 * running on the local cpu w/ rq lock held and preemption
758 * and preemption disabled, which in turn means that none else 746 * disabled, which in turn means that none else could be
759 * could be manipulating idle_list, so dereferencing idle_list 747 * manipulating idle_list, so dereferencing idle_list without gcwq
760 * without gcwq lock is safe. 748 * lock is safe.
761 */ 749 */
762 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist)) 750 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
763 to_wakeup = first_worker(pool); 751 to_wakeup = first_worker(pool);
@@ -1217,19 +1205,16 @@ static void worker_enter_idle(struct worker *worker)
1217 /* idle_list is LIFO */ 1205 /* idle_list is LIFO */
1218 list_add(&worker->entry, &pool->idle_list); 1206 list_add(&worker->entry, &pool->idle_list);
1219 1207
1220 if (likely(gcwq->trustee_state != TRUSTEE_DONE)) { 1208 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1221 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1209 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
1222 mod_timer(&pool->idle_timer,
1223 jiffies + IDLE_WORKER_TIMEOUT);
1224 } else
1225 wake_up_all(&gcwq->trustee_wait);
1226 1210
1227 /* 1211 /*
1228 * Sanity check nr_running. Because trustee releases gcwq->lock 1212 * Sanity check nr_running. Because gcwq_unbind_fn() releases
1229 * between setting %WORKER_UNBOUND and zapping nr_running, the 1213 * gcwq->lock between setting %WORKER_UNBOUND and zapping
1230 * warning may trigger spuriously. Check iff trustee is idle. 1214 * nr_running, the warning may trigger spuriously. Check iff
1215 * unbind is not in progress.
1231 */ 1216 */
1232 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE && 1217 WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) &&
1233 pool->nr_workers == pool->nr_idle && 1218 pool->nr_workers == pool->nr_idle &&
1234 atomic_read(get_pool_nr_running(pool))); 1219 atomic_read(get_pool_nr_running(pool)));
1235} 1220}
@@ -3367,46 +3352,9 @@ EXPORT_SYMBOL_GPL(work_busy);
3367 * gcwqs serve mix of short, long and very long running works making 3352 * gcwqs serve mix of short, long and very long running works making
3368 * blocked draining impractical. 3353 * blocked draining impractical.
3369 * 3354 *
3370 * This is solved by allowing a gcwq to be detached from CPU, running it 3355 * This is solved by allowing a gcwq to be disassociated from the CPU
3371 * with unbound workers and allowing it to be reattached later if the cpu 3356 * running as an unbound one and allowing it to be reattached later if the
3372 * comes back online. A separate thread is created to govern a gcwq in 3357 * cpu comes back online.
3373 * such state and is called the trustee of the gcwq.
3374 *
3375 * Trustee states and their descriptions.
3376 *
3377 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3378 * new trustee is started with this state.
3379 *
3380 * IN_CHARGE Once started, trustee will enter this state after
3381 * assuming the manager role and making all existing
3382 * workers rogue. DOWN_PREPARE waits for trustee to
3383 * enter this state. After reaching IN_CHARGE, trustee
3384 * tries to execute the pending worklist until it's empty
3385 * and the state is set to BUTCHER, or the state is set
3386 * to RELEASE.
3387 *
3388 * BUTCHER Command state which is set by the cpu callback after
3389 * the cpu has went down. Once this state is set trustee
3390 * knows that there will be no new works on the worklist
3391 * and once the worklist is empty it can proceed to
3392 * killing idle workers.
3393 *
3394 * RELEASE Command state which is set by the cpu callback if the
3395 * cpu down has been canceled or it has come online
3396 * again. After recognizing this state, trustee stops
3397 * trying to drain or butcher and clears ROGUE, rebinds
3398 * all remaining workers back to the cpu and releases
3399 * manager role.
3400 *
3401 * DONE Trustee will enter this state after BUTCHER or RELEASE
3402 * is complete.
3403 *
3404 * trustee CPU draining
3405 * took over down complete
3406 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3407 * | | ^
3408 * | CPU is back online v return workers |
3409 * ----------------> RELEASE --------------
3410 */ 3358 */
3411 3359
3412/* claim manager positions of all pools */ 3360/* claim manager positions of all pools */
@@ -3427,61 +3375,11 @@ static void gcwq_release_management(struct global_cwq *gcwq)
3427 mutex_unlock(&pool->manager_mutex); 3375 mutex_unlock(&pool->manager_mutex);
3428} 3376}
3429 3377
3430/** 3378static void gcwq_unbind_fn(struct work_struct *work)
3431 * trustee_wait_event_timeout - timed event wait for trustee
3432 * @cond: condition to wait for
3433 * @timeout: timeout in jiffies
3434 *
3435 * wait_event_timeout() for trustee to use. Handles locking and
3436 * checks for RELEASE request.
3437 *
3438 * CONTEXT:
3439 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3440 * multiple times. To be used by trustee.
3441 *
3442 * RETURNS:
3443 * Positive indicating left time if @cond is satisfied, 0 if timed
3444 * out, -1 if canceled.
3445 */
3446#define trustee_wait_event_timeout(cond, timeout) ({ \
3447 long __ret = (timeout); \
3448 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3449 __ret) { \
3450 spin_unlock_irq(&gcwq->lock); \
3451 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3452 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3453 __ret); \
3454 spin_lock_irq(&gcwq->lock); \
3455 } \
3456 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3457})
3458
3459/**
3460 * trustee_wait_event - event wait for trustee
3461 * @cond: condition to wait for
3462 *
3463 * wait_event() for trustee to use. Automatically handles locking and
3464 * checks for CANCEL request.
3465 *
3466 * CONTEXT:
3467 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3468 * multiple times. To be used by trustee.
3469 *
3470