From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from stravinsky.debian.org (stravinsky.debian.org [82.195.75.108]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AA6C37A83B for ; Tue, 14 Jul 2026 11:42:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=82.195.75.108 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784029329; cv=none; b=CbiMglF5CVVbFdRkWjE2kERK1Gg5eCTofBfTkckDyF2CtebY3LAdr+sc0Y+TPlNLtOtu7/JnYTW5wKMOu8CjVHZJRMaC/2iBsX9gWbNUQjscOOByfXAP9BbFpgFfTpDDojhzBkGuX6iH5CK0jHldf9R51lB+4uVyRRXCvkCh8iw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784029329; c=relaxed/simple; bh=O5nG5dGY/0v3rta/SA4ljsRF6HsvrLtweQSMzPEU+eM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VMN1vPp3XvRnl3aP8a6CM8GuXf3DixVhHHwG4eBP/pGglX2Mcl14xm5poP9U/e97uFHC7mc1KMr4SUUx+sKHynUrFuFe34nmuInpviZn0/jEXlthdFEOVFrKx5LHHZU9+MPg7/F08Zum5p9Z5IWIjf59Wrl/5VJ9gegqL1gCDhU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=debian.org; spf=pass smtp.mailfrom=debian.org; dkim=pass (2048-bit key) header.d=debian.org header.i=@debian.org header.b=r/g0bL4E; arc=none smtp.client-ip=82.195.75.108 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=debian.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=debian.org header.i=@debian.org header.b="r/g0bL4E" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debian.org; s=smtpauto.stravinsky; h=X-Debian-User:Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description; bh=bysurook0VEt/+rcFeZpuonxKanyjQDj/VlIc7vFigA=; b=r/g0bL4EZBb6DdOvW3OeHVKJg7 /4GGaSeUkXcr66QEgrR7y0TBAUVW5pSHfoK1GgPAiPHguBmu56Cu+NVGdBBaxfydY8WljeQAyindf +wzQEiscrGLUkRdBwVIb9sXzDoGrJSxmTk78ZyWCdLEyHI7YIZDjmQVXMLPNz1m6441A2hPbrB8LH 9t8lU9DutH3MRhpOjskvFsi+TG338cR4xeo2wMh87tVvnHwxxWTONxL1SOwVlGH6V6zCTKB0Xhy6Z LuafpyXycmpG8fYFNJJx21TM+P3IP5ffPlpJhO5OPJWeLObgV5L1Yj+YzGLbBCam+UVZfXf12Tzw/ GWmqcDOA==; Received: from authenticated-user by stravinsky.debian.org with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.96) (envelope-from ) id 1wjbWO-002FZ9-1C; Tue, 14 Jul 2026 11:41:56 +0000 From: Breno Leitao Date: Tue, 14 Jul 2026 04:41:47 -0700 Subject: [PATCH RFC 1/3] workqueue: introduce alloc_pwq() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260714-tejun1-v1-1-024d59241386@debian.org> References: <20260714-tejun1-v1-0-024d59241386@debian.org> In-Reply-To: <20260714-tejun1-v1-0-024d59241386@debian.org> To: Tejun Heo , Lai Jiangshan Cc: linux-kernel@vger.kernel.org, marco.crivellari@suse.com, frederic@kernel.org, Breno Leitao , kernel-team@meta.com X-Mailer: b4 0.16-dev-d5d98 X-Developer-Signature: v=1; a=openpgp-sha256; l=4739; i=leitao@debian.org; h=from:subject:message-id; bh=O5nG5dGY/0v3rta/SA4ljsRF6HsvrLtweQSMzPEU+eM=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBqViB9IqmJHT7heKXIGA/BamjpD6AvvJK1r28mE P1q9Wgq44aJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCalYgfQAKCRA1o5Of/Hh3 bXCzEACoahvHqMPGaex2X2q+qIXcGg6JRcixh1xjtTkkCDvlQPa61tNio9sjgrUmBCuPZPaslKp pl/VrOSPV0gCDhW70VzTif+14nu5fLjwHKSKz0a04MyU471mTYP8Pn8Xj0s46K8KN0zaLOLczE0 nchBtpqGYaNveSS0BVwRHWt61feNfkEJegyTa6C68Nhxq3oSu+wFUQIY1fOHjOUzKI3eKRjb3wh 8GGFkvlf5F7ySgtfXQ502MiQ2mBnZ4R+Z43VojMb2x/YmUSLI01RiFTZXTpjJI1biqzyZUu4sCZ jkh0yNkpVvj9V1AqtqBiCX9o9KBIMWx0Unq3h91/Vo2QuL1yOzVVGgtHFS0sl2AnGRpCLdn3Uv/ dY9hCtiNtZWJkzQ6UMWF1WtglLDRqTOtjZRMqkDq7zHnNTf5E0nFxl8Hl9KBri4fKk7VWiCnrE/ m0MH6O18j66dixpXdr8A5YK5gUBo1nLD0Qw5epNNXkPHgEdyDQl+maojKVgfloWmssc6lEFOrHF 1W+Fxe5ITDBmrUI+WHD6hhefa3Lk54KNUhQuSZm3V0yMh07y3LMkhVGeyX/BkOEO1AFGmsEF8hJ FjvLqkv1gX0BgZS2z1dUNTCUe5wODWq9bwwKl0FAoubruBgGzzMn0EWdcQAN+RJ38Ju1ta6ojNQ iGU8pLsW0539qbg== X-Developer-Key: i=leitao@debian.org; a=openpgp; fpr=AC8539A6E8F46702CA4A439B35A3939FFC78776D X-Debian-User: leitao Factor the static per-cpu pool lookup out of alloc_and_link_pwqs() into get_percpu_pool(), and add alloc_pwq() -- a common pwq allocator that picks the backing pool by workqueue type: a percpu workqueue uses the static per-cpu pool for @cpu (get_percpu_pool()), an unbound workqueue uses a pool matching @attrs (get_unbound_pool()). The paired put_pwq_pool() releases only unbound pools; static per-cpu pools are permanent. alloc_pwq() replaces alloc_unbound_pwq() and gives the workqueue core a single entry point that can produce a pwq pointing at either kind of pool -- the building block for unifying percpu and unbound workqueues. No functional change: the current alloc_pwq() callers only operate on unbound workqueues, so its percpu branch is not exercised yet. Signed-off-by: Breno Leitao --- kernel/workqueue.c | 61 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 94f37ea762365..c32e173af2335 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -5354,8 +5354,38 @@ static void link_pwq(struct pool_workqueue *pwq) list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); } -/* obtain a pool matching @attr and create a pwq associating the pool and @wq */ -static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, +/* Return the static per-cpu worker_pool that backs @wq on @cpu. */ +static struct worker_pool *get_percpu_pool(struct workqueue_struct *wq, int cpu) +{ + struct worker_pool __percpu *pools; + bool highpri; + + WARN_ON(wq->flags & WQ_UNBOUND); + + highpri = wq->flags & WQ_HIGHPRI; + if (wq->flags & WQ_BH) + pools = bh_worker_pools; + else + pools = cpu_worker_pools; + + return &per_cpu_ptr(pools, cpu)[highpri]; +} + +/* release the pool obtained for @wq's pwq; only unbound pools are refcounted */ +static void put_pwq_pool(struct workqueue_struct *wq, struct worker_pool *pool) +{ + if (!(wq->flags & WQ_UNBOUND)) + return; + + put_unbound_pool(pool); +} + +/* + * Obtain the pool backing @wq on @cpu and create a pwq associating it with @wq. + * A percpu @wq uses the static per-cpu pool for @cpu; an unbound @wq uses a + * pool matching @attrs. + */ +static struct pool_workqueue *alloc_pwq(struct workqueue_struct *wq, int cpu, const struct workqueue_attrs *attrs) { struct worker_pool *pool; @@ -5363,13 +5393,16 @@ static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, lockdep_assert_held(&wq_pool_mutex); - pool = get_unbound_pool(attrs); + if (wq->flags & WQ_UNBOUND) + pool = get_unbound_pool(attrs); + else + pool = get_percpu_pool(wq, cpu); if (!pool) return NULL; pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); if (!pwq) { - put_unbound_pool(pool); + put_pwq_pool(wq, pool); return NULL; } @@ -5478,7 +5511,7 @@ apply_wqattrs_prepare(struct workqueue_struct *wq, copy_workqueue_attrs(new_attrs, attrs); wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); - ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); + ctx->dfl_pwq = alloc_pwq(wq, -1, new_attrs); if (!ctx->dfl_pwq) goto out_free; @@ -5488,7 +5521,7 @@ apply_wqattrs_prepare(struct workqueue_struct *wq, ctx->pwq_tbl[cpu] = ctx->dfl_pwq; } else { wq_calc_pod_cpumask(new_attrs, cpu); - ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); + ctx->pwq_tbl[cpu] = alloc_pwq(wq, cpu, new_attrs); if (!ctx->pwq_tbl[cpu]) goto out_free; } @@ -5632,7 +5665,7 @@ static void unbound_wq_update_pwq(struct workqueue_struct *wq, int cpu) return; /* create a new pwq */ - pwq = alloc_unbound_pwq(wq, target_attrs); + pwq = alloc_pwq(wq, cpu, target_attrs); if (!pwq) { pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", wq->name); @@ -5668,19 +5701,9 @@ static int alloc_and_link_pwqs(struct workqueue_struct *wq) goto enomem; if (!(wq->flags & WQ_UNBOUND)) { - struct worker_pool __percpu *pools; - - if (wq->flags & WQ_BH) - pools = bh_worker_pools; - else - pools = cpu_worker_pools; - for_each_possible_cpu(cpu) { - struct pool_workqueue **pwq_p; - struct worker_pool *pool; - - pool = &(per_cpu_ptr(pools, cpu)[highpri]); - pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); + struct pool_workqueue **pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); + struct worker_pool *pool = get_percpu_pool(wq, cpu); *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); -- 2.53.0-Meta