From: Peter Zijlstra <peterz@infradead.org>
To: gor@linux.ibm.com, jpoimboe@redhat.com, jikos@kernel.org,
mbenes@suse.cz, pmladek@suse.com, mingo@kernel.org
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
joe.lawrence@redhat.com, fweisbec@gmail.com, tglx@linutronix.de,
hca@linux.ibm.com, svens@linux.ibm.com, sumanthk@linux.ibm.com,
live-patching@vger.kernel.org, paulmck@kernel.org
Subject: [RFC][PATCH 2/7] sched: Fix task_try_func()
Date: Wed, 22 Sep 2021 13:05:08 +0200 [thread overview]
Message-ID: <20210922110836.005204425@infradead.org> (raw)
In-Reply-To: 20210922110506.703075504@infradead.org
Clarify and fix task_try_func(). Move the smp_rmb() up to avoid
re-loading p->on_rq in the false case, but add a p->on_rq reload after
acquiring rq->lock, after all, it could have gotten dequeued while
waiting for the lock.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
kernel/sched/core.c | 37 ++++++++++++++++++++++++++++++-------
1 file changed, 30 insertions(+), 7 deletions(-)
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -4005,7 +4005,7 @@ try_to_wake_up(struct task_struct *p, un
* Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in
* __schedule(). See the comment for smp_mb__after_spinlock().
*
- * A similar smb_rmb() lives in try_invoke_on_locked_down_task().
+ * A similar smb_rmb() lives in task_try_func().
*/
smp_rmb();
if (READ_ONCE(p->on_rq) && ttwu_runnable(p, wake_flags))
@@ -4124,25 +4124,48 @@ try_to_wake_up(struct task_struct *p, un
*/
int task_try_func(struct task_struct *p, task_try_f func, void *arg)
{
+ unsigned int state;
struct rq_flags rf;
- int ret = -EAGAIN; /* raced, try again later */
struct rq *rq;
+ int ret = -EAGAIN; /* raced, try again later */
raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
+
+ state = READ_ONCE(p->__state);
+
+ /*
+ * Ensure we load p->on_rq after p->__state, otherwise it would be
+ * possible to, falsely, observe p->on_rq == 0.
+ *
+ * See try_to_wake_up() for a longer comment.
+ */
+ smp_rmb();
+
if (p->on_rq) {
rq = __task_rq_lock(p, &rf);
- if (task_rq(p) == rq)
+
+ /* re-check p->on_rq now that we hold rq->lock */
+ if (p->on_rq && task_rq(p) == rq)
ret = func(p, arg);
+
rq_unlock(rq, &rf);
+
} else {
- switch (READ_ONCE(p->__state)) {
+
+ switch (state) {
case TASK_RUNNING:
case TASK_WAKING:
+ /*
+ * We raced against wakeup, try again later.
+ */
break;
+
default:
- smp_rmb(); // See smp_rmb() comment in try_to_wake_up().
- if (!p->on_rq)
- ret = func(p, arg);
+ /*
+ * Since we hold ->pi_lock, we serialize against
+ * try_to_wake_up() and any blocked state must remain.
+ */
+ ret = func(p, arg);
}
}
raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags);
next prev parent reply other threads:[~2021-09-22 11:11 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-22 11:05 [RFC][PATCH 0/7] sched,rcu,context_tracking,livepatch: Improve livepatch task transitions for idle and NOHZ_FULL cpus Peter Zijlstra
2021-09-22 11:05 ` [RFC][PATCH 1/7] sched,rcu: Rework try_invoke_on_locked_down_task() Peter Zijlstra
2021-09-22 11:05 ` Peter Zijlstra [this message]
2021-09-22 11:05 ` [RFC][PATCH 3/7] sched,livepatch: Use task_try_func() Peter Zijlstra
2021-09-23 12:05 ` Petr Mladek
2021-09-23 13:17 ` Peter Zijlstra
2021-09-23 13:47 ` Peter Zijlstra
2021-09-22 11:05 ` [RFC][PATCH 4/7] sched: Simplify wake_up_*idle*() Peter Zijlstra
2021-09-22 11:05 ` [RFC][PATCH 5/7] sched,livepatch: Use wake_up_if_idle() Peter Zijlstra
2021-09-22 13:05 ` Miroslav Benes
2021-09-23 12:19 ` Petr Mladek
2021-09-22 11:05 ` [RFC][PATCH 6/7] context_tracking: Provide SMP ordering using RCU Peter Zijlstra
2021-09-22 15:17 ` Paul E. McKenney
2021-09-22 19:33 ` Peter Zijlstra
2021-09-22 19:47 ` Peter Zijlstra
2021-09-22 19:59 ` Paul E. McKenney
2021-09-22 19:53 ` Paul E. McKenney
2021-09-22 20:02 ` Peter Zijlstra
2021-09-23 12:10 ` Petr Mladek
2021-09-24 18:57 ` Joel Savitz
2021-09-27 14:33 ` Petr Mladek
2021-09-22 11:05 ` [RFC][PATCH 7/7] livepatch,context_tracking: Avoid disturbing NOHZ_FULL tasks Peter Zijlstra
2021-09-23 13:14 ` Petr Mladek
2021-09-23 13:28 ` Peter Zijlstra
2021-09-24 7:33 ` Petr Mladek
2021-09-23 13:22 ` [RFC][PATCH 0/7] sched,rcu,context_tracking,livepatch: Improve livepatch task transitions for idle and NOHZ_FULL cpus Petr Mladek
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=20210922110836.005204425@infradead.org \
--to=peterz@infradead.org \
--cc=fweisbec@gmail.com \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=jikos@kernel.org \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=mingo@kernel.org \
--cc=paulmck@kernel.org \
--cc=pmladek@suse.com \
--cc=sumanthk@linux.ibm.com \
--cc=svens@linux.ibm.com \
--cc=tglx@linutronix.de \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.