public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: tglx@linutronix.de, linux-kernel@vger.kernel.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: Re: [PATCH 0/6] locking/rtmutex: Avoid PI state recursion through sched_submit_work()
Date: Wed, 16 Aug 2023 16:58:18 +0200	[thread overview]
Message-ID: <20230816145818.GA989936@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <20230816134630.KO12Djeh@linutronix.de>

On Wed, Aug 16, 2023 at 03:46:30PM +0200, Sebastian Andrzej Siewior wrote:
> On 2023-08-16 12:19:04 [+0200], To Peter Zijlstra wrote:
> > On 2023-08-16 11:42:57 [+0200], Peter Zijlstra wrote:
> > > Not the same -- this is namespace_lock(), right? That's a regular rwsem
> > > afaict and that *should* be good. Clearly I messed something up.
> > 
> > Most likely. I do see it also fom inode_lock() which does down_write().
> > I see it only to originate from rwbase_write_lock().
> 
> I've been looking at what you did and what we had.
> I'm not sure if your additional debug/assert code figured it out or me
> looking at it, but in rwbase_write_lock() for down_write(), we had this
> beauty with a comment that you made go away:
> 
> |        * Take the rtmutex as a first step. For rwsem this will also
> |        * invoke sched_submit_work() to flush IO and workers.
> |	 */
> |         if (rwbase_rtmutex_lock_state(rtm, state))
> 

Yeah, I can't quite remember why I killed that comment, I think because
it was either 'obvious' or confusing at the time. Or perhaps I was too
lazy to type, ... :-)

> for rw_semaphore we don't have any explicit rwbase_sched_submit_work()
> but relied on this one. Now that I look at it again,
> rwbase_rtmutex_lock_state() can succeed in the fast path so we don't
> flush/ invoke rwbase_pre_schedule().
> So you rightfully removed the comment as it was misleading but we do
> need that rwbase_pre_schedule() thingy before
> raw_spin_lock_irqsave(&rtm->wait_lock).

Right, it's both the fast-path and the fact that rt_mutex_slowlock()
will also do post_schedule() and reset the flag.

I've ended up with the below, but it is quite horrible.. but let me go
stare at the futex wreckage before trying to clean things up.

--- a/kernel/locking/rwbase_rt.c
+++ b/kernel/locking/rwbase_rt.c
@@ -270,6 +270,7 @@ static int __sched rwbase_write_lock(str
 
 out_unlock:
 	raw_spin_unlock_irqrestore(&rtm->wait_lock, flags);
+	rt_mutex_post_schedule();
 	return 0;
 }
 
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -1412,8 +1412,30 @@ static inline void __downgrade_write(str
 #define rwbase_restore_current_state()			\
 	__set_current_state(TASK_RUNNING)
 
-#define rwbase_rtmutex_lock_state(rtm, state)		\
-	__rt_mutex_lock(rtm, state)
+/*
+ * Variant of __rt_mutex_lock() that unconditionally does
+ * rt_mutex_pre_schedule() and keeps it on success.
+ */
+static __always_inline int
+rwbase_rtmutex_lock_state(struct rt_mutex_base *lock, unsigned int state)
+{
+	unsigned long flags;
+	int ret;
+
+	rt_mutex_pre_schedule();
+
+	if (likely(rt_mutex_try_acquire(lock)))
+		return 0;
+
+	raw_spin_lock_irqsave(&lock->wait_lock, flags);
+	ret = __rt_mutex_slowlock_locked(lock, NULL, state);
+	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
+
+	if (ret)
+		rt_mutex_post_schedule();
+
+	return ret;
+}
 
 #define rwbase_rtmutex_slowlock_locked(rtm, state)	\
 	__rt_mutex_slowlock_locked(rtm, NULL, state)

  reply	other threads:[~2023-08-16 14:59 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 ` [PATCH 4/6] sched: Provide rt_mutex specific scheduler helpers Peter Zijlstra
2023-08-15 11:01 ` [PATCH 5/6] locking/rtmutex: Use " 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 [this message]
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=20230816145818.GA989936@hirez.programming.kicks-ass.net \
    --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