History

workqueue: Convert the idle_timer to a timer + work_struct

A later patch will require a sleepable context in the idle worker timeout function. Converting worker_pool.idle_timer to a delayed_work gives us just that, however this would imply turning all idle_timer expiries into scheduler events (waking up a worker to handle the dwork). Instead, implement a "custom dwork" where the timer callback does some extra checks before queuing the associated work. No change in functionality intended.

Valentin Schneider committed 3 years ago

Commit 3f959aa1 parent 793777b1 file · +50 −2Browse files

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

Unified · Split

kernel/workqueue.c+50−2
@@ -169,7 +169,9 @@ struct worker_pool { · 168 unmodified lines
169169
170170 struct list_head idle_list; /* L: list of idle workers */
171171 struct timer_list idle_timer; /* L: worker idle timeout */
172− struct timer_list mayday_timer; /* L: SOS timer for workers */
172+ struct work_struct idle_cull_work; /* L: worker idle cleanup */
173+
174+ struct timer_list mayday_timer; /* L: SOS timer for workers */
173175
174176 /* a workers is either on busy_hash or idle_list, or the manager */
175177 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
@@ -2023,17 +2025,61 @@ static void destroy_worker(struct worker *worker) · 1847 unmodified lines
20232025 wake_up_process(worker->task);
20242026 }
20252027
2028+/**
2029+ * idle_worker_timeout - check if some idle workers can now be deleted.
2030+ * @t: The pool's idle_timer that just expired
2031+ *
2032+ * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in
2033+ * worker_leave_idle(), as a worker flicking between idle and active while its
2034+ * pool is at the too_many_workers() tipping point would cause too much timer
2035+ * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let
2036+ * it expire and re-evaluate things from there.
2037+ */
20262038 static void idle_worker_timeout(struct timer_list *t)
20272039 {
20282040 struct worker_pool *pool = from_timer(pool, t, idle_timer);
2041+ bool do_cull = false;
2042+
2043+ if (work_pending(&pool->idle_cull_work))
2044+ return;
20292045
20302046 raw_spin_lock_irq(&pool->lock);
20312047
2032− while (too_many_workers(pool)) {
2048+ if (too_many_workers(pool)) {
20332049 struct worker *worker;
20342050 unsigned long expires;
20352051
20362052 /* idle_list is kept in LIFO order, check the last one */
2053+ worker = list_entry(pool->idle_list.prev, struct worker, entry);
2054+ expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2055+ do_cull = !time_before(jiffies, expires);
2056+
2057+ if (!do_cull)
2058+ mod_timer(&pool->idle_timer, expires);
2059+ }
2060+ raw_spin_unlock_irq(&pool->lock);
2061+
2062+ if (do_cull)
2063+ queue_work(system_unbound_wq, &pool->idle_cull_work);
2064+}
2065+
2066+/**
2067+ * idle_cull_fn - cull workers that have been idle for too long.
2068+ * @work: the pool's work for handling these idle workers
2069+ *
2070+ * This goes through a pool's idle workers and gets rid of those that have been
2071+ * idle for at least IDLE_WORKER_TIMEOUT seconds.
2072+ */
2073+static void idle_cull_fn(struct work_struct *work)
2074+{
2075+ struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work);
2076+
2077+ raw_spin_lock_irq(&pool->lock);
2078+
2079+ while (too_many_workers(pool)) {
2080+ struct worker *worker;
2081+ unsigned long expires;
2082+
20372083 worker = list_entry(pool->idle_list.prev, struct worker, entry);
20382084 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
20392085
@@ -3483,6 +3529,7 @@ static int init_worker_pool(struct worker_pool *pool) · 1443 unmodified lines
34833529 hash_init(pool->busy_hash);
34843530
34853531 timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE);
3532+ INIT_WORK(&pool->idle_cull_work, idle_cull_fn);
34863533
34873534 timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0);
34883535
@@ -3630,6 +3677,7 @@ static void put_unbound_pool(struct worker_pool *pool) · 141 unmodified lines
36303677
36313678 /* shut down the timers */
36323679 del_timer_sync(&pool->idle_timer);
3680+ cancel_work_sync(&pool->idle_cull_work);
36333681 del_timer_sync(&pool->mayday_timer);
36343682
36353683 /* RCU protected to allow dereferences from get_work_pool() */