From: Peter Zijlstra <peterz@infradead.org>
To: bigeasy@linutronix.de, tglx@linutronix.de
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
bsegall@google.com, boqun.feng@gmail.com, swood@redhat.com,
bristot@redhat.com, dietmar.eggemann@arm.com, mingo@redhat.com,
jstultz@google.com, juri.lelli@redhat.com, mgorman@suse.de,
rostedt@goodmis.org, vschneid@redhat.com,
vincent.guittot@linaro.org, longman@redhat.com, will@kernel.org
Subject: [PATCH 4/6] sched: Provide rt_mutex specific scheduler helpers
Date: Tue, 15 Aug 2023 13:01:25 +0200 [thread overview]
Message-ID: <20230815111430.355375399@infradead.org> (raw)
In-Reply-To: 20230815110121.117752409@infradead.org
With PREEMPT_RT there is a rt_mutex recursion problem where
sched_submit_work() can use an rtlock (aka spinlock_t). More
specifically what happens is:
mutex_lock() /* really rt_mutex */
...
__rt_mutex_slowlock_locked()
task_blocks_on_rt_mutex()
// enqueue current task as waiter
// do PI chain walk
rt_mutex_slowlock_block()
schedule()
sched_submit_work()
...
spin_lock() /* really rtlock */
...
__rt_mutex_slowlock_locked()
task_blocks_on_rt_mutex()
// enqueue current task as waiter *AGAIN*
// *CONFUSION*
Fix this by making rt_mutex do the sched_submit_work() early, before
it enqueues itself as a waiter -- before it even knows *if* it will
wait.
[[ basically Thomas' patch but with different naming and a few asserts
added ]]
Originally-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
include/linux/sched.h | 3 +++
include/linux/sched/rt.h | 4 ++++
kernel/sched/core.c | 36 ++++++++++++++++++++++++++++++++----
3 files changed, 39 insertions(+), 4 deletions(-)
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -906,6 +906,9 @@ struct task_struct {
* ->sched_remote_wakeup gets used, so it can be in this word.
*/
unsigned sched_remote_wakeup:1;
+#ifdef CONFIG_RT_MUTEXES
+ unsigned sched_rt_mutex:1;
+#endif
/* Bit to tell LSMs we're in execve(): */
unsigned in_execve:1;
--- a/include/linux/sched/rt.h
+++ b/include/linux/sched/rt.h
@@ -30,6 +30,10 @@ static inline bool task_is_realtime(stru
}
#ifdef CONFIG_RT_MUTEXES
+extern void rt_mutex_pre_schedule(void);
+extern void rt_mutex_schedule(void);
+extern void rt_mutex_post_schedule(void);
+
/*
* Must hold either p->pi_lock or task_rq(p)->lock.
*/
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -6738,9 +6738,6 @@ static inline void sched_submit_work(str
static DEFINE_WAIT_OVERRIDE_MAP(sched_map, LD_WAIT_CONFIG);
unsigned int task_flags;
- if (task_is_running(tsk))
- return;
-
/*
* Establish LD_WAIT_CONFIG context to ensure none of the code called
* will use a blocking primitive -- which would lead to recursion.
@@ -6798,7 +6795,12 @@ asmlinkage __visible void __sched schedu
{
struct task_struct *tsk = current;
- sched_submit_work(tsk);
+#ifdef CONFIG_RT_MUTEXES
+ lockdep_assert(!tsk->sched_rt_mutex);
+#endif
+
+ if (!task_is_running(tsk))
+ sched_submit_work(tsk);
__schedule_loop(SM_NONE);
sched_update_worker(tsk);
}
@@ -7059,6 +7061,32 @@ static void __setscheduler_prio(struct t
#ifdef CONFIG_RT_MUTEXES
+/*
+ * Would be more useful with typeof()/auto_type but they don't mix with
+ * bit-fields. Since it's a local thing, use int. Keep the generic sounding
+ * name such that if someone were to implement this function we get to compare
+ * notes.
+ */
+#define fetch_and_set(x, v) ({ int _x = (x); (x) = (v); _x; })
+
+void rt_mutex_pre_schedule(void)
+{
+ lockdep_assert(!fetch_and_set(current->sched_rt_mutex, 1));
+ sched_submit_work(current);
+}
+
+void rt_mutex_schedule(void)
+{
+ lockdep_assert(current->sched_rt_mutex);
+ __schedule_loop(SM_NONE);
+}
+
+void rt_mutex_post_schedule(void)
+{
+ sched_update_worker(current);
+ lockdep_assert(fetch_and_set(current->sched_rt_mutex, 0));
+}
+
static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
{
if (pi_task)
next prev parent reply other threads:[~2023-08-15 11:18 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-15 11:01 [PATCH 0/6] locking/rtmutex: Avoid PI state recursion through sched_submit_work() Peter Zijlstra
2023-08-15 11:01 ` [PATCH 1/6] sched: Constrain locks in sched_submit_work() Peter Zijlstra
2023-08-15 11:01 ` [PATCH 2/6] locking/rtmutex: Avoid unconditional slowpath for DEBUG_RT_MUTEXES Peter Zijlstra
2023-08-15 11:01 ` [PATCH 3/6] sched: Extract __schedule_loop() Peter Zijlstra
2023-08-15 22:33 ` Phil Auld
2023-08-15 22:39 ` Peter Zijlstra
2023-08-16 14:14 ` Phil Auld
2023-08-15 22:42 ` Phil Auld
2023-08-16 10:01 ` Sebastian Andrzej Siewior
2023-08-16 11:39 ` Phil Auld
2023-08-16 12:20 ` Sebastian Andrzej Siewior
2023-08-16 12:48 ` Phil Auld
2023-08-15 11:01 ` Peter Zijlstra [this message]
2023-08-15 11:01 ` [PATCH 5/6] locking/rtmutex: Use rt_mutex specific scheduler helpers Peter Zijlstra
2023-08-15 11:01 ` [PATCH 6/6] locking/rtmutex: Add a lockdep assert to catch potential nested blocking Peter Zijlstra
2023-08-15 16:15 ` [PATCH 0/6] locking/rtmutex: Avoid PI state recursion through sched_submit_work() Peter Zijlstra
2023-08-16 8:58 ` Sebastian Andrzej Siewior
2023-08-16 9:42 ` Peter Zijlstra
2023-08-16 10:19 ` Sebastian Andrzej Siewior
2023-08-16 13:46 ` Sebastian Andrzej Siewior
2023-08-16 14:58 ` Peter Zijlstra
2023-08-16 15:22 ` Peter Zijlstra
2023-08-16 15:25 ` Sebastian Andrzej Siewior
2023-08-17 6:59 ` Sebastian Andrzej Siewior
2023-08-17 8:26 ` Peter Zijlstra
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=20230815111430.355375399@infradead.org \
--to=peterz@infradead.org \
--cc=bigeasy@linutronix.de \
--cc=boqun.feng@gmail.com \
--cc=bristot@redhat.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=jstultz@google.com \
--cc=juri.lelli@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=rostedt@goodmis.org \
--cc=swood@redhat.com \
--cc=tglx@linutronix.de \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=will@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