From: Lai Jiangshan <jiangshanlai@gmail.com>
To: linux-kernel@vger.kernel.org, Tejun Heo <tj@kernel.org>
Cc: Lai Jiangshan <laijs@linux.alibaba.com>,
Lai Jiangshan <jiangshanlai@gmail.com>
Subject: [RFC PATCH 7/7] workqueue: Replace pool lock with preemption disabling in wq_worker_sleeping()
Date: Tue, 7 Dec 2021 15:35:43 +0800 [thread overview]
Message-ID: <20211207073543.61092-8-jiangshanlai@gmail.com> (raw)
In-Reply-To: <20211207073543.61092-1-jiangshanlai@gmail.com>
From: Lai Jiangshan <laijs@linux.alibaba.com>
Once upon a time, wq_worker_sleeping() was called with rq lock held,
so wq_worker_sleeping() can not use pool lock. Instead it used "X:"
protection: preemption disabled on local cpu and wq_worker_sleeping()
didn't depend on rq lock to work even with it held.
Now, wq_worker_sleeping() isn't called with rq lock held and is using
pool lock. But the functionality of "X:" protection isn't removed and
wq_worker_running() is still using it.
So we can also use "X:" protection in wq_worker_sleeping() and avoid
locking the pool lock.
This patch also documents that only idle_list.next is under "X:"
protection, not the whole idle_list because destroy_worker() in idle
timer can remove non-first idle workers. Idle timer can be possible
strayed in different CPU, or even in the same CPU, it can interrupt
preemption disabled context.
Signed-off-by: Lai Jiangshan <laijs@linux.alibaba.com>
---
kernel/workqueue.c | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 33f1106b4f99..6c30edbe2fc2 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -162,7 +162,8 @@ struct worker_pool {
int nr_workers; /* L: total number of workers */
int nr_idle; /* L: currently idle workers */
- struct list_head idle_list; /* X: list of idle workers */
+ /* idle_list.next and first_idle_worker(): X: first idle worker */
+ 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 */
@@ -819,6 +820,11 @@ static bool too_many_workers(struct worker_pool *pool)
int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
int nr_busy = pool->nr_workers - nr_idle;
+ /*
+ * nr_idle must be at least 2 to allow a manager and at least an idle
+ * worker in idle_list so that idle_worker_timeout() doesn't touch
+ * idle_list.next.
+ */
return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
}
@@ -826,7 +832,7 @@ static bool too_many_workers(struct worker_pool *pool)
* Wake up functions.
*/
-/* Return the first idle worker. Safe with preemption disabled */
+/* Return the first idle worker. Safe with "X:" protection */
static struct worker *first_idle_worker(struct worker_pool *pool)
{
if (unlikely(list_empty(&pool->idle_list)))
@@ -905,7 +911,7 @@ void wq_worker_sleeping(struct task_struct *task)
return;
worker->sleeping = 1;
- raw_spin_lock_irq(&pool->lock);
+ preempt_disable();
/*
* Recheck in case unbind_workers() preempted us. We don't
@@ -913,7 +919,7 @@ void wq_worker_sleeping(struct task_struct *task)
* and nr_running has been reset.
*/
if (worker->flags & WORKER_NOT_RUNNING) {
- raw_spin_unlock_irq(&pool->lock);
+ preempt_enable();
return;
}
@@ -923,10 +929,9 @@ void wq_worker_sleeping(struct task_struct *task)
* Please read comment there.
*
* NOT_RUNNING is clear. This means that we're bound to and
- * running on the local cpu w/ rq lock held and preemption
- * disabled, which in turn means that none else could be
- * manipulating idle_list, so dereferencing idle_list without pool
- * lock is safe.
+ * running on the local cpu with preemption disabled, which in turn
+ * means that no one else could be manipulating idle_list.next
+ * so dereferencing idle_list.next without pool lock is safe.
*/
if (atomic_dec_and_test(&pool->nr_running) &&
!list_empty(&pool->worklist)) {
@@ -934,7 +939,7 @@ void wq_worker_sleeping(struct task_struct *task)
if (next)
wake_up_process(next->task);
}
- raw_spin_unlock_irq(&pool->lock);
+ preempt_enable();
}
/**
--
2.19.1.6.gb485710b
next prev parent reply other threads:[~2021-12-07 7:36 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-07 7:35 [PATCH 0/7] workqueue: cleanups for schedule callbacks Lai Jiangshan
2021-12-07 7:35 ` [PATCH 1/7] workqueue: Remove the outdated comment before wq_worker_sleeping() Lai Jiangshan
2021-12-07 7:35 ` [PATCH 2/7] workqueue: Remove the advanced kicking of the idle workers in rebind_workers() Lai Jiangshan
2021-12-07 7:35 ` [PATCH 3/7] workqueue: Remove outdated comment about exceptional workers in unbind_workers() Lai Jiangshan
2021-12-09 22:16 ` Tejun Heo
2021-12-07 7:35 ` [PATCH 4/7] workqueue: Remove schedule() " Lai Jiangshan
2021-12-07 7:35 ` [PATCH 5/7] workqueue: Move the code of waking a worker up " Lai Jiangshan
2021-12-07 7:35 ` [PATCH 6/7] workqueue: Remove the cacheline_aligned for nr_running Lai Jiangshan
2021-12-09 22:07 ` Tejun Heo
2021-12-09 23:31 ` Lai Jiangshan
2021-12-09 22:27 ` Tejun Heo
2021-12-07 7:35 ` Lai Jiangshan [this message]
2021-12-09 22:14 ` [RFC PATCH 7/7] workqueue: Replace pool lock with preemption disabling in wq_worker_sleeping() 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=20211207073543.61092-8-jiangshanlai@gmail.com \
--to=jiangshanlai@gmail.com \
--cc=laijs@linux.alibaba.com \
--cc=linux-kernel@vger.kernel.org \
--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®