[v6,3/4] workqueue: Convert the idle_timer to a timer + work_struct

Message ID 20221128183109.446754-4-vschneid@redhat.com
State New
Headers
Series workqueue: destroy_worker() vs isolated CPUs |

Commit Message

Valentin Schneider Nov. 28, 2022, 6:31 p.m. UTC
  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.

Signed-off-by: Valentin Schneider <vschneid@redhat.com>
---
 kernel/workqueue.c | 51 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 48 insertions(+), 3 deletions(-)
  

Comments

Tejun Heo Nov. 30, 2022, 9:06 p.m. UTC | #1
On Mon, Nov 28, 2022 at 06:31:08PM +0000, Valentin Schneider wrote:
> @@ -1806,7 +1808,9 @@ static void worker_enter_idle(struct worker *worker)
>  	/* idle_list is LIFO */
>  	list_add(&worker->entry, &pool->idle_list);
>  
> -	if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
> +	if (too_many_workers(pool) &&
> +	    !timer_pending(&pool->idle_timer) &&
> +	    !work_pending(&pool->idle_cull_work))

Just checking the timer should be enough here, I think.

>  		mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
>  
>  	/* Sanity check nr_running. */
> @@ -2019,17 +2023,56 @@ static void destroy_worker(struct worker *worker)
>  	wake_up_process(worker->task);
>  }
>  
> +/*
> + * idle_worker_timeout - check if some idle workers can now be deleted.

Might as well turn it into a proper function comment starting w/ "/**" and
with argument list.

> + *
> + * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in
> + * worker_leave_idle(), as a worker flicking between idle and active while its
> + * pool is at the too_many_workers() tipping point would cause too much timer
> + * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let
> + * it expire and re-evaluate things from there.
> + */
>  static void idle_worker_timeout(struct timer_list *t)
>  {
>  	struct worker_pool *pool = from_timer(pool, t, idle_timer);
> +	bool do_cull = false;
> +
> +	if (work_pending(&pool->idle_cull_work))
> +		return;
>  
>  	raw_spin_lock_irq(&pool->lock);
>  
> -	while (too_many_workers(pool)) {
> +	if (too_many_workers(pool)) {
>  		struct worker *worker;
>  		unsigned long expires;
>  
>  		/* idle_list is kept in LIFO order, check the last one */
> +		worker = list_entry(pool->idle_list.prev, struct worker, entry);
> +		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
> +		do_cull = !time_before(jiffies, expires);
> +
> +		if (!do_cull)
> +			mod_timer(&pool->idle_timer, expires);
> +	}
> +	raw_spin_unlock_irq(&pool->lock);
> +
> +	if (do_cull)
> +		queue_work(system_unbound_wq, &pool->idle_cull_work);
> +}
> +
> +/*
> + * idle_cull_fn - cull workers that have been idle for too long.
> + */

Please turn it into a full function comment or drop the wings (ie. make it
an one-liner).

> +static void idle_cull_fn(struct work_struct *work)
> +{
> +	struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work);
> +
> +	raw_spin_lock_irq(&pool->lock);
> +
> +	while (too_many_workers(pool)) {
> +		struct worker *worker;
> +		unsigned long expires;
> +

Other than that, looks great to me.

Thanks.
  
Valentin Schneider Dec. 1, 2022, 11:01 a.m. UTC | #2
On 30/11/22 11:06, Tejun Heo wrote:
> On Mon, Nov 28, 2022 at 06:31:08PM +0000, Valentin Schneider wrote:
>> @@ -1806,7 +1808,9 @@ static void worker_enter_idle(struct worker *worker)
>>      /* idle_list is LIFO */
>>      list_add(&worker->entry, &pool->idle_list);
>>
>> -	if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
>> +	if (too_many_workers(pool) &&
>> +	    !timer_pending(&pool->idle_timer) &&
>> +	    !work_pending(&pool->idle_cull_work))
>
> Just checking the timer should be enough here, I think.
>

That would let the timer be re-armed when the cull work is pending, which
itself will re-arm the timer to the next non-culled idle worker expiry (if
there is any remaining).

Not an issue per se, it's just that having the cull work pending is a
"promise" that the timer will be re-armed if and when necessary.

I think in cases where the cull work doesn't get to run for a while, not
having the extra work_pending() check and just arming the timer in
worker_enter_idle() might be cheaper than repeatedly checking both
timer_pending() and work_pending(), but otherwise I would assume not arming
the timer would be preferred.

>>              mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
>>
>>      /* Sanity check nr_running. */
>> @@ -2019,17 +2023,56 @@ static void destroy_worker(struct worker *worker)
>>      wake_up_process(worker->task);
>>  }
>>
>> +/*
>> + * idle_worker_timeout - check if some idle workers can now be deleted.
>
> Might as well turn it into a proper function comment starting w/ "/**" and
> with argument list.
>

Ack.

>> + *
>> + * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in
>> + * worker_leave_idle(), as a worker flicking between idle and active while its
>> + * pool is at the too_many_workers() tipping point would cause too much timer
>> + * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let
>> + * it expire and re-evaluate things from there.
>> + */
>>  static void idle_worker_timeout(struct timer_list *t)
>>  {
>>      struct worker_pool *pool = from_timer(pool, t, idle_timer);
>> +	bool do_cull = false;
>> +
>> +	if (work_pending(&pool->idle_cull_work))
>> +		return;
>>
>>      raw_spin_lock_irq(&pool->lock);
>>
>> -	while (too_many_workers(pool)) {
>> +	if (too_many_workers(pool)) {
>>              struct worker *worker;
>>              unsigned long expires;
>>
>>              /* idle_list is kept in LIFO order, check the last one */
>> +		worker = list_entry(pool->idle_list.prev, struct worker, entry);
>> +		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
>> +		do_cull = !time_before(jiffies, expires);
>> +
>> +		if (!do_cull)
>> +			mod_timer(&pool->idle_timer, expires);
>> +	}
>> +	raw_spin_unlock_irq(&pool->lock);
>> +
>> +	if (do_cull)
>> +		queue_work(system_unbound_wq, &pool->idle_cull_work);
>> +}
>> +
>> +/*
>> + * idle_cull_fn - cull workers that have been idle for too long.
>> + */
>
> Please turn it into a full function comment or drop the wings (ie. make it
> an one-liner).
>

Patch 4/4 adds the rest of the comment, but I can make the whole thing
appear in patch 4 if you prefer.
  

Patch

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 8185a42848c50..c8b1466a9c070 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -169,7 +169,9 @@  struct worker_pool {
 
 	struct list_head	idle_list;	/* L: list of idle workers */
 	struct timer_list	idle_timer;	/* L: worker idle timeout */
-	struct timer_list	mayday_timer;	/* L: SOS timer for workers */
+	struct work_struct      idle_cull_work; /* L: worker idle cleanup */
+
+	struct timer_list	mayday_timer;	  /* L: SOS timer for workers */
 
 	/* a workers is either on busy_hash or idle_list, or the manager */
 	DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
@@ -1806,7 +1808,9 @@  static void worker_enter_idle(struct worker *worker)
 	/* idle_list is LIFO */
 	list_add(&worker->entry, &pool->idle_list);
 
-	if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
+	if (too_many_workers(pool) &&
+	    !timer_pending(&pool->idle_timer) &&
+	    !work_pending(&pool->idle_cull_work))
 		mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
 
 	/* Sanity check nr_running. */
@@ -2019,17 +2023,56 @@  static void destroy_worker(struct worker *worker)
 	wake_up_process(worker->task);
 }
 
+/*
+ * idle_worker_timeout - check if some idle workers can now be deleted.
+ *
+ * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in
+ * worker_leave_idle(), as a worker flicking between idle and active while its
+ * pool is at the too_many_workers() tipping point would cause too much timer
+ * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let
+ * it expire and re-evaluate things from there.
+ */
 static void idle_worker_timeout(struct timer_list *t)
 {
 	struct worker_pool *pool = from_timer(pool, t, idle_timer);
+	bool do_cull = false;
+
+	if (work_pending(&pool->idle_cull_work))
+		return;
 
 	raw_spin_lock_irq(&pool->lock);
 
-	while (too_many_workers(pool)) {
+	if (too_many_workers(pool)) {
 		struct worker *worker;
 		unsigned long expires;
 
 		/* idle_list is kept in LIFO order, check the last one */
+		worker = list_entry(pool->idle_list.prev, struct worker, entry);
+		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
+		do_cull = !time_before(jiffies, expires);
+
+		if (!do_cull)
+			mod_timer(&pool->idle_timer, expires);
+	}
+	raw_spin_unlock_irq(&pool->lock);
+
+	if (do_cull)
+		queue_work(system_unbound_wq, &pool->idle_cull_work);
+}
+
+/*
+ * idle_cull_fn - cull workers that have been idle for too long.
+ */
+static void idle_cull_fn(struct work_struct *work)
+{
+	struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work);
+
+	raw_spin_lock_irq(&pool->lock);
+
+	while (too_many_workers(pool)) {
+		struct worker *worker;
+		unsigned long expires;
+
 		worker = list_entry(pool->idle_list.prev, struct worker, entry);
 		expires = worker->last_active + IDLE_WORKER_TIMEOUT;
 
@@ -3479,6 +3522,7 @@  static int init_worker_pool(struct worker_pool *pool)
 	hash_init(pool->busy_hash);
 
 	timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE);
+	INIT_WORK(&pool->idle_cull_work, idle_cull_fn);
 
 	timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0);
 
@@ -3626,6 +3670,7 @@  static void put_unbound_pool(struct worker_pool *pool)
 
 	/* shut down the timers */
 	del_timer_sync(&pool->idle_timer);
+	cancel_work_sync(&pool->idle_cull_work);
 	del_timer_sync(&pool->mayday_timer);
 
 	/* RCU protected to allow dereferences from get_work_pool() */