[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20250801192157.912805-1-jstultz@google.com>
Date: Fri, 1 Aug 2025 19:21:20 +0000
From: John Stultz <jstultz@...gle.com>
To: LKML <linux-kernel@...r.kernel.org>
Cc: John Stultz <jstultz@...gle.com>,
syzbot+602c4720aed62576cd79@...kaller.appspotmail.com,
Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>, Ingo Molnar <mingo@...hat.com>,
Peter Zijlstra <peterz@...radead.org>, Juri Lelli <juri.lelli@...hat.com>,
Vincent Guittot <vincent.guittot@...aro.org>, Dietmar Eggemann <dietmar.eggemann@....com>,
Valentin Schneider <valentin.schneider@....com>, K Prateek Nayak <kprateek.nayak@....com>,
Suleiman Souhlal <suleiman@...gle.com>, airlied@...il.com, mripard@...nel.org,
simona@...ll.ch, tzimmermann@...e.de, dri-devel@...ts.freedesktop.org,
kernel-team@...roid.com
Subject: [PATCH v2] locking: Fix __clear_task_blocked_on() warning from
__ww_mutex_wound() path
The __clear_task_blocked_on() helper added a number of sanity
checks ensuring we hold the mutex wait lock and that the task
we are clearing blocked_on pointer (if set) matches the mutex.
However, there is an edge case in the _ww_mutex_wound() logic
where we need to clear the blocked_on pointer for the task that
owns the mutex, not the task that is waiting on the mutex.
For this case the sanity checks aren't valid, so handle this
by allowing a NULL lock to skip the additional checks.
This was easier to miss, I realized, as the test-ww_mutex
driver only exercises the wait-die class of ww_mutexes.
I've got a follow up patch to extend the test so that it
will exercise both.
Fixes: a4f0b6fef4b0 ("locking/mutex: Add p->blocked_on wrappers for correctness checks")
Reported-by: syzbot+602c4720aed62576cd79@...kaller.appspotmail.com
Reported-by: Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>
Closes: https://lore.kernel.org/lkml/68894443.a00a0220.26d0e1.0015.GAE@google.com/
Signed-off-by: John Stultz <jstultz@...gle.com>
---
v2:
* Rewording of "lock" to "mutex" in commit and comment for
clarity
* Rework __clear_task_blocked_on() to use READ_ONCE and WRITE_ONCE
so we don't trip over the WARNING if two instances race, as suggested
by K Prateek Nayak and Maarten Lankhorst
Cc: Ingo Molnar <mingo@...hat.com>
Cc: Peter Zijlstra <peterz@...radead.org>
Cc: Juri Lelli <juri.lelli@...hat.com>
Cc: Vincent Guittot <vincent.guittot@...aro.org>
Cc: Dietmar Eggemann <dietmar.eggemann@....com>
Cc: Valentin Schneider <valentin.schneider@....com>
Cc: K Prateek Nayak <kprateek.nayak@....com>
Cc: Suleiman Souhlal <suleiman@...gle.com>
Cc: Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>
Cc: airlied@...il.com
Cc: mripard@...nel.org
Cc: simona@...ll.ch
Cc: tzimmermann@...e.de
Cc: dri-devel@...ts.freedesktop.org
Cc: kernel-team@...roid.com
---
include/linux/sched.h | 23 +++++++++++++----------
kernel/locking/ww_mutex.h | 6 +++++-
2 files changed, 18 insertions(+), 11 deletions(-)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 40d2fa90df425..700b50d29f7fe 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2166,16 +2166,19 @@ static inline void set_task_blocked_on(struct task_struct *p, struct mutex *m)
static inline void __clear_task_blocked_on(struct task_struct *p, struct mutex *m)
{
- WARN_ON_ONCE(!m);
- /* Currently we serialize blocked_on under the mutex::wait_lock */
- lockdep_assert_held_once(&m->wait_lock);
- /*
- * There may be cases where we re-clear already cleared
- * blocked_on relationships, but make sure we are not
- * clearing the relationship with a different lock.
- */
- WARN_ON_ONCE(m && p->blocked_on && p->blocked_on != m);
- p->blocked_on = NULL;
+ if (m) {
+ struct mutex *blocked_on = READ_ONCE(p->blocked_on);
+
+ /* Currently we serialize blocked_on under the mutex::wait_lock */
+ lockdep_assert_held_once(&m->wait_lock);
+ /*
+ * There may be cases where we re-clear already cleared
+ * blocked_on relationships, but make sure we are not
+ * clearing the relationship with a different lock.
+ */
+ WARN_ON_ONCE(blocked_on && blocked_on != m);
+ }
+ WRITE_ONCE(p->blocked_on, NULL);
}
static inline void clear_task_blocked_on(struct task_struct *p, struct mutex *m)
diff --git a/kernel/locking/ww_mutex.h b/kernel/locking/ww_mutex.h
index 086fd5487ca77..31a785afee6c0 100644
--- a/kernel/locking/ww_mutex.h
+++ b/kernel/locking/ww_mutex.h
@@ -342,8 +342,12 @@ static bool __ww_mutex_wound(struct MUTEX *lock,
* When waking up the task to wound, be sure to clear the
* blocked_on pointer. Otherwise we can see circular
* blocked_on relationships that can't resolve.
+ *
+ * NOTE: We pass NULL here instead of lock, because we
+ * are waking the mutex owner, who may be currently
+ * blocked on a different mutex.
*/
- __clear_task_blocked_on(owner, lock);
+ __clear_task_blocked_on(owner, NULL);
wake_q_add(wake_q, owner);
}
return true;
--
2.50.1.565.gc32cd1483b-goog
Powered by blists - more mailing lists