mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Breno Leitao <leitao@debian.org>
To: Tejun Heo <tj@kernel.org>, Lai Jiangshan <jiangshanlai@gmail.com>
Cc: linux-kernel@vger.kernel.org, marco.crivellari@suse.com,
	 Breno Leitao <leitao@debian.org>,
	kernel-team@meta.com
Subject: [PATCH wq/for-7.4 v2 3/3] workqueue: add a percpu_concurrency_managed workqueue attribute
Date: Fri, 25 Sep 2026 06:09:52 -0700	[thread overview]
Message-ID: <20260925-wq_final-v2-3-860ee052169e@debian.org> (raw)
In-Reply-To: <20260925-wq_final-v2-0-860ee052169e@debian.org>

Introduce percpu_concurrency_managed, similar to the discussion in [1],
and use it when deciding to do concurrency management, instead of
relying on WQ_PERCPU (the only WQ type that does CM as of now).

Do not set it for WQ_BH. BH uses static per-cpu pools too, but it forces
max_active to INT_MAX and does not impose the normal max-active
throttle.

The attr is workqueue-only. wqattrs_clear_for_pool() clears it before
attrs are stored in a worker_pool. Pool selection still uses WQ_PERCPU;
attrs do not switch a workqueue onto static per-cpu pools yet.

Link: https://lore.kernel.org/all/8a1437751a6da2c166284b1ca562fb0a@kernel.org/ [1]
Signed-off-by: Breno Leitao <leitao@debian.org>
---
 include/linux/workqueue.h |  9 +++++++++
 kernel/workqueue.c        | 40 ++++++++++++++++++++++++++--------------
 2 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index a283766a192aaf..32747f797c4449 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -204,6 +204,15 @@ struct workqueue_attrs {
 	 */
 	enum wq_affn_scope affn_scope;
 
+	/**
+	 * @percpu_concurrency_managed: use per-cpu concurrency accounting
+	 *
+	 * Workqueues with this set meter active work per CPU against
+	 * percpu_max_active. Workqueues with this clear either use unbound
+	 * max_active accounting or do not impose a max_active limit.
+	 */
+	bool percpu_concurrency_managed;
+
 	/**
 	 * @ordered: work items must be executed one by one in queueing order
 	 */
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 545332d6118159..7059fc1bd9b090 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -424,9 +424,9 @@ struct workqueue_struct {
 
 static int wq_user_max_active(struct workqueue_struct *wq)
 {
-	if (wq->flags & WQ_UNBOUND)
-		return READ_ONCE(wq->saved_max_active);
-	return READ_ONCE(wq->saved_percpu_max_active);
+	if (READ_ONCE(wq->attrs->percpu_concurrency_managed))
+		return READ_ONCE(wq->saved_percpu_max_active);
+	return READ_ONCE(wq->saved_max_active);
 }
 
 /*
@@ -1861,11 +1861,13 @@ static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill)
 
 	lockdep_assert_held(&pool->lock);
 
-	/*
-	 * A concurrency-managed per-cpu pool accounts nr_active per pwq, so
-	 * pwq->nr_active against wq->percpu_max_active is sufficient.
-	 */
-	if (is_percpu_pool(pool)) {
+	/* BH workqueues use per-cpu pools but do not throttle max_active. */
+	if (pool->flags & POOL_BH) {
+		obtained = true;
+		goto out;
+	}
+
+	if (READ_ONCE(wq->attrs->percpu_concurrency_managed)) {
 		obtained = pwq->nr_active < READ_ONCE(wq->percpu_max_active);
 		goto out;
 	}
@@ -2091,6 +2093,7 @@ static void node_activate_pending_pwq(struct wq_node_nr_active *nna,
  */
 static void pwq_dec_nr_active(struct pool_workqueue *pwq)
 {
+	struct workqueue_struct *wq = pwq->wq;
 	struct worker_pool *pool = pwq->pool;
 	struct wq_node_nr_active *nna;
 
@@ -2102,16 +2105,15 @@ static void pwq_dec_nr_active(struct pool_workqueue *pwq)
 	 */
 	pwq->nr_active--;
 
-	/*
-	 * A concurrency-managed per-cpu pool only needs to kick the first
-	 * inactive work item on @pwq itself.
-	 */
-	if (is_percpu_pool(pool)) {
+	if (pool->flags & POOL_BH)
+		return;
+
+	if (READ_ONCE(wq->attrs->percpu_concurrency_managed)) {
 		pwq_activate_first_inactive(pwq, false);
 		return;
 	}
 
-	nna = wq_node_nr_active(pwq->wq, pool->node);
+	nna = wq_node_nr_active(wq, pool->node);
 
 	/*
 	 * If @pwq is for an unbound workqueue, it's more complicated because
@@ -5034,6 +5036,7 @@ static void copy_workqueue_attrs(struct workqueue_attrs *to,
 	 * get_unbound_pool() explicitly clears the fields.
 	 */
 	to->affn_scope = from->affn_scope;
+	to->percpu_concurrency_managed = from->percpu_concurrency_managed;
 	to->ordered = from->ordered;
 }
 
@@ -5044,6 +5047,7 @@ static void copy_workqueue_attrs(struct workqueue_attrs *to,
 static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs)
 {
 	attrs->affn_scope = WQ_AFFN_NR_TYPES;
+	attrs->percpu_concurrency_managed = false;
 	attrs->ordered = false;
 	if (attrs->affn_strict)
 		cpumask_copy(attrs->cpumask, cpu_possible_mask);
@@ -5851,6 +5855,10 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,
 	if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
 		return -EINVAL;
 
+	/* Attrs cannot switch a workqueue to per-cpu pools yet. */
+	if (WARN_ON(attrs->percpu_concurrency_managed))
+		return -EINVAL;
+
 	mutex_lock(&wq_pool_mutex);
 	ret = apply_workqueue_attrs_locked(wq, attrs);
 	mutex_unlock(&wq_pool_mutex);
@@ -5942,6 +5950,10 @@ static struct workqueue_attrs *alloc_wq_std_attrs(struct workqueue_struct *wq)
 	if (wq->flags & __WQ_ORDERED)
 		attrs->ordered = true;
 
+	/* BH uses per-cpu pools but not max_active concurrency management. */
+	if ((wq->flags & WQ_PERCPU) && !(wq->flags & WQ_BH))
+		attrs->percpu_concurrency_managed = true;
+
 	return attrs;
 }
 

-- 
2.53.0-Meta


  parent reply	other threads:[~2026-09-25 13:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-25 13:09 [PATCH wq/for-7.4 v2 0/3] workqueue: Keep max_active and percpu_max_active in sync Breno Leitao
2026-09-25 13:09 ` [PATCH wq/for-7.4 v2 1/3] workqueue: Move the initial max_active setup into a helper Breno Leitao
2026-09-25 13:09 ` [PATCH wq/for-7.4 v2 2/3] workqueue: Keep the limits of both max_active domains current Breno Leitao
2026-09-28 20:15   ` Tejun Heo
2026-09-25 13:09 ` Breno Leitao [this message]
2026-09-28 21:31   ` [PATCH wq/for-7.4 v2 3/3] workqueue: add a percpu_concurrency_managed workqueue attribute Tejun Heo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260925-wq_final-v2-3-860ee052169e@debian.org \
    --to=leitao@debian.org \
    --cc=jiangshanlai@gmail.com \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marco.crivellari@suse.com \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®