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
169 169
170 struct list_head idle_list; /* L: list of idle workers */170 struct list_head idle_list; /* L: list of idle workers */
171 struct timer_list idle_timer; /* L: worker idle timeout */171 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 */
173 175
174 /* a workers is either on busy_hash or idle_list, or the manager */176 /* a workers is either on busy_hash or idle_list, or the manager */
175 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);177 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
@@ -2023,17 +2025,61 @@ static void destroy_worker(struct worker *worker) · 1847 unmodified lines
2023 wake_up_process(worker->task);2025 wake_up_process(worker->task);
2024}2026}
2025 2027
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 */
2026static void idle_worker_timeout(struct timer_list *t)2038static void idle_worker_timeout(struct timer_list *t)
2027{2039{
2028 struct worker_pool *pool = from_timer(pool, t, idle_timer);2040 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;
2029 2045
2030 raw_spin_lock_irq(&pool->lock);2046 raw_spin_lock_irq(&pool->lock);
2031 2047
2032 while (too_many_workers(pool)) {2048 if (too_many_workers(pool)) {
2033 struct worker *worker;2049 struct worker *worker;
2034 unsigned long expires;2050 unsigned long expires;
2035 2051
2036 /* idle_list is kept in LIFO order, check the last one */2052 /* 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 */
2073static 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
2037 worker = list_entry(pool->idle_list.prev, struct worker, entry);2083 worker = list_entry(pool->idle_list.prev, struct worker, entry);
2038 expires = worker->last_active + IDLE_WORKER_TIMEOUT;2084 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2039 2085
@@ -3483,6 +3529,7 @@ static int init_worker_pool(struct worker_pool *pool) · 1443 unmodified lines
3483 hash_init(pool->busy_hash);3529 hash_init(pool->busy_hash);
3484 3530
3485 timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE);3531 timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE);
3532 INIT_WORK(&pool->idle_cull_work, idle_cull_fn);
3486 3533
3487 timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0);3534 timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0);
3488 3535
@@ -3630,6 +3677,7 @@ static void put_unbound_pool(struct worker_pool *pool) · 141 unmodified lines
3630 3677
3631 /* shut down the timers */3678 /* shut down the timers */
3632 del_timer_sync(&pool->idle_timer);3679 del_timer_sync(&pool->idle_timer);
3680 cancel_work_sync(&pool->idle_cull_work);
3633 del_timer_sync(&pool->mayday_timer);3681 del_timer_sync(&pool->mayday_timer);
3634 3682
3635 /* RCU protected to allow dereferences from get_work_pool() */3683 /* RCU protected to allow dereferences from get_work_pool() */