Skip to contentSkip to conversation

workqueue: remove unneeded test before wake up next worker

PATCH Superseded Lai Jiangshan 2 participants 9 messages Jul 16, 2014 – Jul 22, 2014 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 workqueue: remove unneeded test before wake up next worker

In this code:

	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
		wake_up_worker(pool);

the first test is unneeded. Even the first test is removed, it doesn't affect the wake-up logic when WORKER_UNBOUND. And it will not introduce any useless wake-up when !WORKER_UNBOUND since the nr_running >= 1 except only one case. It will introduce useless/redundant wake-up when cpu_intensive, but this case is rare and next patch will also remove this redundant wake-up.

---
 kernel/workqueue.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
kernel/workqueue.c +2 -5
index f8d54c1..6d11b9a 100644
@@ -2047,11 +2047,8 @@ __acquires(&pool->lock)
 	if (unlikely(cpu_intensive))
 		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
 
-	/*
-	 * Unbound pool isn't concurrency managed and work items should be
-	 * executed ASAP.  Wake up another worker if necessary.
-	 */
-	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
+	/* Wake up another worker if necessary. */
+	if (need_more_worker(pool))
 		wake_up_worker(pool);
 
 	/*
Signature · 1 line
1.7.4.4
#1 lore raw

PATCH 2/2 workqueue: remove the argument @wakeup from worker_set_flags()

worker_set_flags() doesn't necessarily wake next worker and the @wakeup can be removed, the caller can use the following conbination instead when needed:

	worker_set_flags();
	if (need_more_worker(pool))

wake_up_worker(pool);

---
 kernel/workqueue.c |   25 ++++++-------------------
 1 files changed, 6 insertions(+), 19 deletions(-)
kernel/workqueue.c +6 -19
index 6d11b9a..1a14f18 100644
@@ -867,35 +867,22 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  * worker_set_flags - set worker flags and adjust nr_running accordingly
  * @worker: self
  * @flags: flags to set
- * @wakeup: wakeup an idle worker if necessary
  *
- * Set @flags in @worker->flags and adjust nr_running accordingly.  If
- * nr_running becomes zero and @wakeup is %true, an idle worker is
- * woken up.
+ * Set @flags in @worker->flags and adjust nr_running accordingly.
  *
  * CONTEXT:
  * spin_lock_irq(pool->lock)
  */
-static inline void worker_set_flags(struct worker *worker, unsigned int flags,
-				    bool wakeup)
+static inline void worker_set_flags(struct worker *worker, unsigned int flags)
 {
 	struct worker_pool *pool = worker->pool;
 
 	WARN_ON_ONCE(worker->task != current);
 
-	/*
-	 * If transitioning into NOT_RUNNING, adjust nr_running and
-	 * wake up an idle worker as necessary if requested by
-	 * @wakeup.
-	 */
+	/* If transitioning into NOT_RUNNING, adjust nr_running. */
 	if ((flags & WORKER_NOT_RUNNING) &&
 	    !(worker->flags & WORKER_NOT_RUNNING)) {
-		if (wakeup) {
-			if (atomic_dec_and_test(&pool->nr_running) &&
-			    !list_empty(&pool->worklist))
-				wake_up_worker(pool);
-		} else
-			atomic_dec(&pool->nr_running);
+		atomic_dec(&pool->nr_running);
 	}
 
 	worker->flags |= flags;
@@ -2045,7 +2032,7 @@ __acquires(&pool->lock)
 	 * management.  They're the scheduler's responsibility.
 	 */
 	if (unlikely(cpu_intensive))
-		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
+		worker_set_flags(worker, WORKER_CPU_INTENSIVE);
 
 	/* Wake up another worker if necessary. */
 	if (need_more_worker(pool))
@@ -2204,7 +2191,7 @@ recheck:
 		}
 	} while (keep_working(pool));
 
-	worker_set_flags(worker, WORKER_PREP, false);
+	worker_set_flags(worker, WORKER_PREP);
 sleep:
 	/*
 	 * pool->lock is held and there's no work to process and no need to
Signature · 1 line
1.7.4.4
maintainer
#2 lore raw

Re: [PATCH 2/2] workqueue: remove the argument @wakeup from worker_set_flags()

Quoted 7 lines · Lai Jiangshan

worker_set_flags() doesn't necessarily wake next worker and the @wakeup can be removed, the caller can use the following conbination instead when needed:

	worker_set_flags();
	if (need_more_worker(pool))

wake_up_worker(pool);

Hmmm, yeah, there were more places where worker_set_flags() was used but it does seem excessive now.

kernel/workqueue.c __acquires(&pool->lock) · line 2035

Quoted 6 lines
kernel/workqueue.c +1 -1
@@ -2045,7 +2032,7 @@ __acquires(&pool->lock)
 	 * management.  They're the scheduler's responsibility.
 	 */
 	if (unlikely(cpu_intensive))
-		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
+		worker_set_flags(worker, WORKER_CPU_INTENSIVE);

But let's do this separately. Please drop the previous patch and perform need_more_worker() test explicitly after setting CPU_INTENSIVE.

Thanks.

Signature · 1 line
tejun
maintainer
#3 lore raw

Re: [PATCH 2/2] workqueue: remove the argument @wakeup from worker_set_flags()

Quoted 12 lines · Tejun Heo
Quoted 7 lines · Lai Jiangshan

worker_set_flags() doesn't necessarily wake next worker and the @wakeup can be removed, the caller can use the following conbination instead when needed:

	worker_set_flags();
	if (need_more_worker(pool))

wake_up_worker(pool);

Hmmm, yeah, there were more places where worker_set_flags() was used but it does seem excessive now.

Quoted 10 lines

kernel/workqueue.c __acquires(&pool->lock) · line 2035

Quoted 6 lines
kernel/workqueue.c +1 -1
@@ -2045,7 +2032,7 @@ __acquires(&pool->lock)
 	 * management.  They're the scheduler's responsibility.
 	 */
 	if (unlikely(cpu_intensive))
-		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
+		worker_set_flags(worker, WORKER_CPU_INTENSIVE);

But let's do this separately. Please drop the previous patch and perform need_more_worker() test explicitly after setting CPU_INTENSIVE.

So, we can do it together at need_more_workers() but let's please explain how different cases would behave there.

Thanks.

Signature · 1 line
tejun
maintainer
#4 lore raw
In reply to Lai Jiangshan
Quoted 21 lines · Lai Jiangshan

In this code:

	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
		wake_up_worker(pool);

the first test is unneeded. Even the first test is removed, it doesn't affect the wake-up logic when WORKER_UNBOUND. And it will not introduce any useless wake-up when !WORKER_UNBOUND since the nr_running >= 1 except only one case. It will introduce useless/redundant wake-up when cpu_intensive, but this case is rare and next patch will also remove this redundant wake-up.

---
 kernel/workqueue.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
kernel/workqueue.c +0 -0
index f8d54c1..6d11b9a 100644
@@ -2047,11 +2047,8 @@ __acquires(&pool->lock)
 	if (unlikely(cpu_intensive))

kernel/workqueue.c __acquires(&pool->lock) · line 2052

Quoted 10 lines
kernel/workqueue.c +2 -5
 		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
 
-	/*
-	 * Unbound pool isn't concurrency managed and work items should be
-	 * executed ASAP.  Wake up another worker if necessary.
-	 */
-	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
+	/* Wake up another worker if necessary. */
+	if (need_more_worker(pool))
 		wake_up_worker(pool);

What does this buy us? Sure, it may achieve about the same operation but it's a lot more confusing. need_more_worker() is specifically for concurrency management. Applying it to unmanaged workers could lead to okay behavior but conflating the two to save one test on worker flags doesn't seem like a good trade-off to me.

Thanks.

Signature · 1 line
tejun
maintainer
#5 lore raw
Quoted 24 lines · Tejun Heo
Quoted 23 lines · Lai Jiangshan

In this code:

	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
		wake_up_worker(pool);

the first test is unneeded. Even the first test is removed, it doesn't affect the wake-up logic when WORKER_UNBOUND. And it will not introduce any useless wake-up when !WORKER_UNBOUND since the nr_running >= 1 except only one case. It will introduce useless/redundant wake-up when cpu_intensive, but this case is rare and next patch will also remove this redundant wake-up.

---
 kernel/workqueue.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
kernel/workqueue.c +0 -0
index f8d54c1..6d11b9a 100644
@@ -2047,11 +2047,8 @@ __acquires(&pool->lock)
 	if (unlikely(cpu_intensive))
 		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
 
Quoted 14 lines

kernel/workqueue.c __acquires(&pool->lock) · line 2052

Quoted 8 lines
kernel/workqueue.c +2 -5
-	/*
-	 * Unbound pool isn't concurrency managed and work items should be
-	 * executed ASAP.  Wake up another worker if necessary.
-	 */
-	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
+	/* Wake up another worker if necessary. */
+	if (need_more_worker(pool))
 		wake_up_worker(pool);

What does this buy us? Sure, it may achieve about the same operation but it's a lot more confusing. need_more_worker() is specifically for concurrency management. Applying it to unmanaged workers could lead to okay behavior but conflating the two to save one test on worker flags doesn't seem like a good trade-off to me.

I take this back. We do guarantee that need_more_worker() returns %true for unbound pools and make use of that fact but I'd like it to retain the comment about unbound pools.

Thanks.

Signature · 1 line
tejun
#6 lore raw

PATCH v2 1/2 workqueue: remove unneeded test before wake up next worker

In this code:

	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
		wake_up_worker(pool);

the first test is unneeded. Even the first test is removed, it doesn't affect the wake-up logic when WORKER_UNBOUND. And it will not introduce any useless wake-up when !WORKER_UNBOUND since the nr_running >= 1 except only one case. It will introduce useless/redundant wake-up when cpu_intensive, but this case is rare and next patch will also remove this redundant wake-up.

---
 kernel/workqueue.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
kernel/workqueue.c +6 -3
index a791a8c..cd75689 100644
@@ -2048,10 +2048,13 @@ __acquires(&pool->lock)
 		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
 
 	/*
-	 * Unbound pool isn't concurrency managed and work items should be
-	 * executed ASAP.  Wake up another worker if necessary.
+	 * Wake up another worker if necessary.  It is a no-op
+	 * when the current worker is concurrency managed since
+	 * pool->nr_running >= 1.  But it is required for non-concurrency
+	 * managed workers, mainly for unbound pool which requries
+	 * chain execution of currently pending work items ASAP.
 	 */
-	if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
+	if (need_more_worker(pool))
 		wake_up_worker(pool);
 
 	/*
Signature · 1 line
1.7.4.4
#7 lore raw

PATCH v2 2/2 workqueue: remove the argument @wakeup from worker_set_flags()

worker_set_flags() doesn't necessarily wake next worker and the @wakeup can be removed, the caller can use the following conbination instead when needed:

	worker_set_flags();
	if (need_more_worker(pool))

wake_up_worker(pool);

It reduces the machine-code and footprint for the process_one_work() if worker_set_flags() is really inlined.

---
 kernel/workqueue.c |   27 ++++++++-------------------
 1 files changed, 8 insertions(+), 19 deletions(-)
kernel/workqueue.c +8 -19
index cd75689..40b1f00 100644
@@ -867,35 +867,22 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  * worker_set_flags - set worker flags and adjust nr_running accordingly
  * @worker: self
  * @flags: flags to set
- * @wakeup: wakeup an idle worker if necessary
  *
- * Set @flags in @worker->flags and adjust nr_running accordingly.  If
- * nr_running becomes zero and @wakeup is %true, an idle worker is
- * woken up.
+ * Set @flags in @worker->flags and adjust nr_running accordingly.
  *
  * CONTEXT:
  * spin_lock_irq(pool->lock)
  */
-static inline void worker_set_flags(struct worker *worker, unsigned int flags,
-				    bool wakeup)
+static inline void worker_set_flags(struct worker *worker, unsigned int flags)
 {
 	struct worker_pool *pool = worker->pool;
 
 	WARN_ON_ONCE(worker->task != current);
 
-	/*
-	 * If transitioning into NOT_RUNNING, adjust nr_running and
-	 * wake up an idle worker as necessary if requested by
-	 * @wakeup.
-	 */
+	/* If transitioning into NOT_RUNNING, adjust nr_running. */
 	if ((flags & WORKER_NOT_RUNNING) &&
 	    !(worker->flags & WORKER_NOT_RUNNING)) {
-		if (wakeup) {
-			if (atomic_dec_and_test(&pool->nr_running) &&
-			    !list_empty(&pool->worklist))
-				wake_up_worker(pool);
-		} else
-			atomic_dec(&pool->nr_running);
+		atomic_dec(&pool->nr_running);
 	}
 
 	worker->flags |= flags;
@@ -2043,9 +2030,11 @@ __acquires(&pool->lock)
 	/*
 	 * CPU intensive works don't participate in concurrency
 	 * management.  They're the scheduler's responsibility.
+	 * Another worker should be woken up after WORKER_CPU_INTENSIVE
+	 * is set, see next code.
 	 */
 	if (unlikely(cpu_intensive))
-		worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
+		worker_set_flags(worker, WORKER_CPU_INTENSIVE);
 
 	/*
 	 * Wake up another worker if necessary.  It is a no-op
@@ -2210,7 +2199,7 @@ recheck:
 		}
 	} while (keep_working(pool));
 
-	worker_set_flags(worker, WORKER_PREP, false);
+	worker_set_flags(worker, WORKER_PREP);
 sleep:
 	/*
 	 * pool->lock is held and there's no work to process and no need to
Signature · 1 line
1.7.4.4
maintainer
#8 lore raw

Re: [PATCH 2/2 V2] workqueue: remove the argument @wakeup from worker_set_flags()

Quoted 4 lines · Lai Jiangshan

worker_set_flags() doesn't necessarily wake next worker and the @wakeup can be removed, the caller can use the following conbination instead when needed:

Quoted 8 lines
	worker_set_flags();
	if (need_more_worker(pool))

wake_up_worker(pool);

It reduces the machine-code and footprint for the process_one_work() if worker_set_flags() is really inlined.

Applied 1-2 to wq/for-3.17 w/ descriptions and comments updated.

Thanks.

Signature · 1 line
tejun