From: Lai Jiangshan <jiangshanlai@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Valentin Schneider <valentin.schneider@arm.com>,
Peter Zijlstra <peterz@infradead.org>, Qian Cai <cai@redhat.com>,
Vincent Donnefort <vincent.donnefort@arm.com>,
Tejun Heo <tj@kernel.org>,
"Paul E . McKenney" <paulmck@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
Hillf Danton <hdanton@sina.com>,
Lai Jiangshan <laijs@linux.alibaba.com>,
Lai Jiangshan <jiangshanlai@gmail.com>
Subject: [PATCH -tip V4 8/8] workqueue: Fix affinity of kworkers when attaching into pool
Date: Mon, 11 Jan 2021 23:26:38 +0800 [thread overview]
Message-ID: <20210111152638.2417-9-jiangshanlai@gmail.com> (raw)
In-Reply-To: <20210111152638.2417-1-jiangshanlai@gmail.com>
From: Lai Jiangshan <laijs@linux.alibaba.com>
When worker_attach_to_pool() is called, we should not put the workers
to pool->attrs->cpumask when there is or will be no CPU online in it.
Otherwise, it may cause BUG_ON(): (quote from Valentin:)
Per-CPU kworkers forcefully migrated away by hotplug via
workqueue_offline_cpu() can end up spawning more kworkers via
manage_workers() -> maybe_create_worker()
Workers created at this point will be bound using
pool->attrs->cpumask
which in this case is wrong, as the hotplug state machine already
migrated all pinned kworkers away from this CPU. This ends up
triggering the BUG_ON condition is sched_cpu_dying() (i.e. there's
a kworker enqueued on the dying rq).
(end of quote)
We need to find out where it is in the hotplug stages to determind
whether pool->attrs->cpumask is valid. So we have to check
%POOL_DISASSOCIATED and wq_unbound_online_cpumask which are indications
for the hotplug stages.
So for per-CPU kworker case, %POOL_DISASSOCIATED marks the kworkers
of the pool are bound or unboud, so it is used to detect whether
pool->attrs->cpumask is valid to use when attachment.
For unbound workers, we should not set online&!active cpumask to workers.
Just introduced wq_unound_online_cpumask has the features that going-down
cpu is cleared earlier in it than in cpu_active_mask and bring-up cpu
is set later in it than cpu_active_mask. So it is perfect to be used to
detect whether the pool->attrs->cpumask is valid to use.
To use wq_unound_online_cpumask in worker_attach_to_pool(), we need to protect
wq_unbound_online_cpumask in wq_pool_attach_mutex.
Cc: Qian Cai <cai@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Vincent Donnefort <vincent.donnefort@arm.com>
Link: https://lore.kernel.org/lkml/20201210163830.21514-3-valentin.schneider@arm.com/
Link: https://lore.kernel.org/r/ff62e3ee994efb3620177bf7b19fab16f4866845.camel@redhat.com
Reported-by: Qian Cai <cai@redhat.com>
Reviewed-by: Valentin Schneider <valentin.schneider@arm.com>
Acked-by: Tejun Heo <tj@kernel.org>
Tested-by: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Lai Jiangshan <laijs@linux.alibaba.com>
---
kernel/workqueue.c | 39 ++++++++++++++++++++++++++++++---------
1 file changed, 30 insertions(+), 9 deletions(-)
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index b012adbeff9f..d1f1b863c52a 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -310,7 +310,7 @@ static bool workqueue_freezing; /* PL: have wqs started freezing? */
/* PL: allowable cpus for unbound wqs and work items */
static cpumask_var_t wq_unbound_cpumask;
-/* PL: online cpus (cpu_online_mask with the going-down cpu cleared) */
+/* PL&A: online cpus (cpu_online_mask with the going-down cpu cleared) */
static cpumask_var_t wq_unbound_online_cpumask;
/* CPU where unbound work was last round robin scheduled from this CPU */
@@ -1849,19 +1849,36 @@ static struct worker *alloc_worker(int node)
static void worker_attach_to_pool(struct worker *worker,
struct worker_pool *pool)
{
+ bool pool_cpumask_active;
+
mutex_lock(&wq_pool_attach_mutex);
/*
- * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
- * online CPUs. It'll be re-applied when any of the CPUs come up.
+ * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED and
+ * wq_unbound_online_cpumask remain stable across this function.
+ * See the comments above the definitions of the flag and
+ * wq_unbound_online_cpumask for details.
+ *
+ * For percpu pools, whether pool->attrs->cpumask is legitimate
+ * for @worker task depends on where it is in the hotplug stages
+ * divided by workqueue_online/offline_cpu(). Refer the functions
+ * to see how they toggle %POOL_DISASSOCIATED and update cpumask
+ * of the workers.
+ *
+ * For unbound pools, whether pool->attrs->cpumask is legitimate
+ * for @worker task depends on where it is in the hotplug stages
+ * divided by workqueue_unbound_online/offline_cpu(). Refer the
+ * functions to see how they update wq_unbound_online_cpumask and
+ * update cpumask of the workers.
*/
- set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
+ pool_cpumask_active = pool->cpu >= 0 ? !(pool->flags & POOL_DISASSOCIATED) :
+ cpumask_intersects(pool->attrs->cpumask, wq_unbound_online_cpumask);
+
+ if (pool_cpumask_active)
+ WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask) < 0);
+ else
+ WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0);
- /*
- * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains
- * stable across this function. See the comments above the flag
- * definition for details.
- */
if (pool->flags & POOL_DISASSOCIATED)
worker->flags |= WORKER_UNBOUND;
@@ -5149,7 +5166,9 @@ int workqueue_unbound_online_cpu(unsigned int cpu)
int pi;
mutex_lock(&wq_pool_mutex);
+ mutex_lock(&wq_pool_attach_mutex);
cpumask_set_cpu(cpu, wq_unbound_online_cpumask);
+ mutex_unlock(&wq_pool_attach_mutex);
/* update CPU affinity of workers of unbound pools */
for_each_pool(pool, pi) {
@@ -5176,7 +5195,9 @@ int workqueue_unbound_offline_cpu(unsigned int cpu)
int pi;
mutex_lock(&wq_pool_mutex);
+ mutex_lock(&wq_pool_attach_mutex);
cpumask_clear_cpu(cpu, wq_unbound_online_cpumask);
+ mutex_unlock(&wq_pool_attach_mutex);
/* update CPU affinity of workers of unbound pools */
for_each_pool(pool, pi) {
--
2.19.1.6.gb485710b
prev parent reply other threads:[~2021-01-11 14:27 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-11 15:26 [PATCH -tip V4 0/8] workqueue: break affinity initiatively Lai Jiangshan
2021-01-11 15:26 ` [PATCH -tip V4 1/8] workqueue: split cpuhotplug callbacks for unbound workqueue Lai Jiangshan
2021-01-11 15:26 ` [PATCH -tip V4 2/8] workqueue: set pool->attr->cpumask to workers when cpu online Lai Jiangshan
2021-01-11 15:26 ` [PATCH -tip V4 3/8] workqueue: use cpu_possible_mask instead of cpu_active_mask to break affinity Lai Jiangshan
2021-01-22 17:41 ` [tip: sched/urgent] workqueue: Use " tip-bot2 for Lai Jiangshan
2021-01-11 15:26 ` [PATCH -tip V4 4/8] workqueue: Manually break affinity on pool detachment Lai Jiangshan
2021-01-11 15:26 ` [PATCH -tip V4 5/8] workqueue: introduce wq_unbound_online_cpumask Lai Jiangshan
2021-01-11 15:26 ` [PATCH -tip V4 6/8] workqueue: use wq_unbound_online_cpumask in restore_unbound_workers_cpumask() Lai Jiangshan
2021-01-11 15:26 ` [PATCH -tip V4 7/8] workqueue: Manually break affinity on hotplug for unbound pool Lai Jiangshan
2021-01-11 15:26 ` Lai Jiangshan [this message]
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=20210111152638.2417-9-jiangshanlai@gmail.com \
--to=jiangshanlai@gmail.com \
--cc=cai@redhat.com \
--cc=hdanton@sina.com \
--cc=laijs@linux.alibaba.com \
--cc=linux-kernel@vger.kernel.org \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
--cc=valentin.schneider@arm.com \
--cc=vincent.donnefort@arm.com \
/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®