mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: peterz@infradead.org, tglx@kernel.org, mingo@redhat.com,
	linux-kernel@vger.kernel.org, will@kernel.org, boqun@kernel.org,
	longman@redhat.com, rostedt@goodmis.org, clrkwllms@kernel.org,
	dvhart@infradead.org, dave@stgolabs.net, andrealmeid@igalia.com,
	shuah@kernel.org, juri.lelli@redhat.com,
	linux-rt-devel@lists.linux.dev, linux-kselftest@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH v2 1/5] locking/rtmutex: Stop spinning on the owner when the waiter is woken
Date: Mon, 16 Nov 2026 13:36:39 -0500	[thread overview]
Message-ID: <83330898-b6de-469c-ac20-7367c1eb4f0e@efficios.com> (raw)
In-Reply-To: <20261116120626.JVHxeSTu@linutronix.de>

Hi Sebastian,

> Wouldn't it make sense to return true in this case so it can re-evaluate
> the situation for timeout/ signal rather than visit the scheduler for no
> reason?

Yes. With true, rt_mutex_slowlock_block() takes the wait lock again
and sets the sleep state. Then it tries the lock, and checks the
timeout, the signal and the ww_mutex wound. A wakeup that is none of
these cannot make it spin forever: the sleep state is set again before
the next spin.

> Also, wouldn't it make sense to restrict this to
> rt_mutex_slowlock_block() (and maybe there to the !TASK_UNINTERRUPTIBLE
> case but this might result in too much code) rather than have it also
> available rtlock_slowlock_locked() where it makes no difference?

Agreed for the rtlock side. An rtlock waiter is in TASK_RTLOCK_WAIT.
The lock handoff alone makes it running, and the handoff changes the
owner, which already ends the spin. Any other wakeup goes to
saved_state. I added a bool argument, as you suggest.

I kept the check for TASK_UNINTERRUPTIBLE on the rt_mutex side. A
ww_mutex wound also wakes an uninterruptible waiter, and the ABBA case
of patch 5 needs the spin to stop there.

With both changes, the futex and ww_mutex selftests pass, with and
without PREEMPT_RT. That includes the PI cases of patch 4.

Steven, the code changed after your Reviewed-by, so I dropped the tag
in the version below. Is it still fine with you?

The planned next version of the patch follows.

Thanks,

Mathieu

-- >8 --
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Date: Fri, 6 Nov 2026 13:56:54 -0500
Subject: [PATCH] locking/rtmutex: Stop spinning on the owner when the waiter
  is woken

The top waiter of an rt_mutex sets its sleep state. Then it spins in
rtmutex_spin_on_owner() as long as the owner is running. A wakeup sets
the waiter back to TASK_RUNNING. The spin loop does not check for this.

The rtmutex_spin_on_owner() is just an optimization with the idea that
if the owner is still running on a CPU, it will likely release it soon.
If the owner gets preempted or schedules out on its own, the spinning
will stop and the waiter will sleep. This came from PREEMPT_RT adaptive
spinning for rt_spin_locks that allowed them to behave more like normal
spin locks and speed up PREEMPT_RT performance.

The problem is that when this was implemented for futex and normal
mutexes it did not account for timeouts or other mechanisms where
the owner releasing the lock wasn't the only way for the spinning
task to exit the mutex acquire. The spinning currently does not
account for anything else but the lock being released or the owner
sleeping.

The owner of a PI futex is a user task. It can run for a long time.
With an owner spinning in userspace, a 1ms pthread_mutex_timedlock()
returned up to 1.4s late in testing. The timeout signal or any signal
to the waiter will not stop it from spinning, and the signal will be
delayed until it exits the spin.

With PREEMPT_RT, two contexts that take two ww_mutexes in opposite
order spin on each other. The wound that resolves this wakes up the
younger one, but it keeps spinning. Only something else that needs one
of the two CPUs stops them. By default, the fair server does it after
950ms, to run SCHED_OTHER tasks.

Luckily, when the spinning task needs to exit the mutex lock code, its
state is set to TASK_RUNNING. By having the spin check the spinning
task's state to see if it switches to running, it can exit the spin
appropriately.

On a wakeup, the spin returns true. The caller then takes the wait lock
again and checks why it was woken, with no call to the scheduler. Only
rt_mutex_slowlock_block() asks for the check. An rtlock waiter is made
running by the lock handoff alone, and the handoff changes the owner,
which already ends the spin. Sebastian Andrzej Siewior suggested both.

Fixes: 992caf7f1724 ("locking/rtmutex: Add adaptive spinwait mechanism")
Cc: stable@vger.kernel.org
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
  kernel/locking/rtmutex.c | 22 ++++++++++++++++++----
  1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
index 5a9534c715b8..93014df90eef 100644
--- a/kernel/locking/rtmutex.c
+++ b/kernel/locking/rtmutex.c
@@ -1494,7 +1494,8 @@ static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
  #ifdef CONFIG_SMP
  static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
  				  struct rt_mutex_waiter *waiter,
-				  struct task_struct *owner)
+				  struct task_struct *owner,
+				  bool check_wakeup)
  {
  	bool res = true;
  
@@ -1510,6 +1511,13 @@ static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
  		 * the rcu_read_lock() ensures the memory stays valid.
  		 */
  		barrier();
+		/*
+		 * Stop spinning when current is woken, for example by a
+		 * timeout or a signal. Return true: the caller checks why,
+		 * with no call to the scheduler.
+		 */
+		if (check_wakeup && task_is_running(current))
+			break;
  		/*
  		 * Stop spinning when:
  		 *  - the lock owner has been scheduled out
@@ -1531,7 +1539,8 @@ static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
  #else
  static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
  				  struct rt_mutex_waiter *waiter,
-				  struct task_struct *owner)
+				  struct task_struct *owner,
+				  bool check_wakeup)
  {
  	return false;
  }
@@ -1665,7 +1674,7 @@ static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock,
  			owner = NULL;
  		raw_spin_unlock_irq_wake(&lock->wait_lock, wake_q);
  
-		if (!owner || !rtmutex_spin_on_owner(lock, waiter, owner)) {
+		if (!owner || !rtmutex_spin_on_owner(lock, waiter, owner, true)) {
  			lockevent_inc(rtmutex_slow_sleep);
  			rt_mutex_schedule();
  		}
@@ -1890,7 +1899,12 @@ static void __sched rtlock_slowlock_locked(struct rt_mutex_base *lock,
  			owner = NULL;
  		raw_spin_unlock_irq_wake(&lock->wait_lock, wake_q);
  
-		if (!owner || !rtmutex_spin_on_owner(lock, &waiter, owner)) {
+		/*
+		 * Only the lock handoff makes an rtlock waiter running,
+		 * and it changes the owner. Any other wakeup goes to
+		 * saved_state. So the spin need not check for a wakeup.
+		 */
+		if (!owner || !rtmutex_spin_on_owner(lock, &waiter, owner, false)) {
  			lockevent_inc(rtlock_slow_sleep);
  			schedule_rtlock();
  		}
-- 
2.47.3


-- 
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com

  reply	other threads:[~2026-11-16 18:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-08 14:13 [PATCH v2 0/5] locking/rtmutex: priority inheritance fixes and tests Mathieu Desnoyers
2026-11-08 14:13 ` [PATCH v2 1/5] locking/rtmutex: Stop spinning on the owner when the waiter is woken Mathieu Desnoyers
2026-11-16 12:06   ` Sebastian Andrzej Siewior
2026-11-16 18:36     ` Mathieu Desnoyers [this message]
2026-11-08 14:13 ` [PATCH v2 2/5] locking/rtmutex: Initialize the ww_mutex context of a waiter Mathieu Desnoyers
2026-11-16 13:58   ` Sebastian Andrzej Siewior
2026-11-16 18:37     ` Mathieu Desnoyers
2026-11-08 14:13 ` [PATCH v2 3/5] locking/rtmutex: Clone the sort keys in rt_mutex_enqueue_pi() Mathieu Desnoyers
2026-11-16 15:11   ` Sebastian Andrzej Siewior
2026-11-16 18:35     ` Mathieu Desnoyers
2026-11-08 14:13 ` [PATCH v2 4/5] selftests/futex: Test priority inheritance on PI futexes Mathieu Desnoyers
2026-11-16 16:56   ` Sebastian Andrzej Siewior
2026-11-16 18:48     ` Mathieu Desnoyers
2026-11-08 14:13 ` [PATCH v2 5/5] locking/ww_mutex/test: Time an ABBA with both owners running Mathieu Desnoyers

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=83330898-b6de-469c-ac20-7367c1eb4f0e@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=andrealmeid@igalia.com \
    --cc=bigeasy@linutronix.de \
    --cc=boqun@kernel.org \
    --cc=clrkwllms@kernel.org \
    --cc=dave@stgolabs.net \
    --cc=dvhart@infradead.org \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@kernel.org \
    --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

all inboxes | Powered by JetHome®