From: Suleiman Souhlal <suleiman@google.com>
To: linux-kernel@vger.kernel.org
Cc: "Suleiman Souhlal" <suleiman@google.com>,
"Thomas Gleixner" <tglx@kernel.org>,
"Ingo Molnar" <mingo@redhat.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Darren Hart" <dvhart@infradead.org>,
"Davidlohr Bueso" <dave@stgolabs.net>,
"André Almeida" <andrealmeid@igalia.com>,
"Juri Lelli" <juri.lelli@redhat.com>,
"Vincent Guittot" <vincent.guittot@linaro.org>,
"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Ben Segall" <bsegall@google.com>, "Mel Gorman" <mgorman@suse.de>,
"Valentin Schneider" <vschneid@redhat.com>,
"K Prateek Nayak" <kprateek.nayak@amd.com>,
"zhidao su" <soolaugust@gmail.com>,
"John Stultz" <jstultz@google.com>,
"Qais Yousef" <qyousef@google.com>,
ssouhlal@FreeBSD.org
Subject: [RFC PATCH 01/12] sched: Abstract task_struct->blocked_on by locking primitive.
Date: Thu, 17 Sep 2026 04:33:25 +0000 [thread overview]
Message-ID: <20260917043339.2093426-2-suleiman@google.com> (raw)
In-Reply-To: <20260917043339.2093426-1-suleiman@google.com>
Abstract task_struct->blocked_on by type of locking primitive, so
that it can be used by things other than mutexes.
Signed-off-by: John Stultz <jstultz@google.com>
Signed-off-by: Suleiman Souhlal <suleiman@google.com>
---
include/linux/sched.h | 40 +++++++++++++++++++--------
kernel/fork.c | 2 +-
kernel/locking/mutex.c | 8 +++---
kernel/sched/core.c | 62 ++++++++++++++++++++++++++++++++++++------
kernel/sched/sched.h | 2 +-
5 files changed, 89 insertions(+), 25 deletions(-)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 705970d07614..6edd0c7891c5 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -832,6 +832,16 @@ struct task_ipi_mask {
struct task_ipi_mask { };
#endif
+enum blocked_on_type {
+ BO_T_NONE,
+ BO_T_MUTEX,
+};
+
+struct blocked_on_lock {
+ void *lock;
+ enum blocked_on_type type;
+};
+
struct task_struct {
#ifdef CONFIG_THREAD_INFO_IN_TASK
/*
@@ -1259,7 +1269,7 @@ struct task_struct {
struct rt_mutex_waiter *pi_blocked_on;
#endif
- struct mutex *blocked_on; /* lock we're blocked on */
+ struct blocked_on_lock blocked_on; /* lock we're blocked on */
raw_spinlock_t blocked_lock;
/*
@@ -2221,10 +2231,15 @@ extern int __cond_resched_rwlock_write(rwlock_t *lock) __must_hold(lock);
static inline struct mutex *__get_task_blocked_on(struct task_struct *p)
{
lockdep_assert_held_once(&p->blocked_lock);
- return p->blocked_on;
+ return p->blocked_on.lock;
}
-static inline void __set_task_blocked_on(struct task_struct *p, struct mutex *m)
+/*
+ * These helpers set and clear the task blocked_on pointer, as well
+ * as setting the initial blocked_on_state, or clearing it
+ */
+static inline void __set_task_blocked_on(struct task_struct *p, void *m,
+ enum blocked_on_type type)
{
WARN_ON_ONCE(!m);
/* The task should only be setting itself as blocked */
@@ -2236,11 +2251,12 @@ static inline void __set_task_blocked_on(struct task_struct *p, struct mutex *m)
* with a different mutex. Note, setting it to the same
* lock repeatedly is ok.
*/
- WARN_ON_ONCE(p->blocked_on && p->blocked_on != m);
- p->blocked_on = m;
+ WARN_ON_ONCE(p->blocked_on.lock && p->blocked_on.lock != m);
+ p->blocked_on.lock = m;
+ p->blocked_on.type = type;
}
-static inline void __clear_task_blocked_on(struct task_struct *p, struct mutex *m)
+static inline void __clear_task_blocked_on(struct task_struct *p, void *m)
{
/* Currently we serialize blocked_on under the task::blocked_lock */
lockdep_assert_held_once(&p->blocked_lock);
@@ -2249,21 +2265,23 @@ static inline void __clear_task_blocked_on(struct task_struct *p, struct mutex *
* 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;
+ WARN_ON_ONCE(m && p->blocked_on.lock && p->blocked_on.lock != m);
+ p->blocked_on.lock = NULL;
+ p->blocked_on.type = BO_T_NONE;
}
-static inline void clear_task_blocked_on(struct task_struct *p, struct mutex *m)
+static inline void clear_task_blocked_on(struct task_struct *p, void *m)
{
guard(raw_spinlock_irqsave)(&p->blocked_lock);
__clear_task_blocked_on(p, m);
}
+
#else
-static inline void __clear_task_blocked_on(struct task_struct *p, struct rt_mutex *m)
+static inline void __clear_task_blocked_on(struct task_struct *p, void *m)
{
}
-static inline void clear_task_blocked_on(struct task_struct *p, struct rt_mutex *m)
+static inline void clear_task_blocked_on(struct task_struct *p, void *m)
{
}
#endif /* !CONFIG_PREEMPT_RT */
diff --git a/kernel/fork.c b/kernel/fork.c
index a5934a317634..6b3f369aad2b 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2266,7 +2266,7 @@ __latent_entropy struct task_struct *copy_process(
lockdep_init_task(p);
- p->blocked_on = NULL; /* not blocked yet */
+ p->blocked_on.lock = NULL; /* not blocked yet */
p->blocked_donor = NULL; /* nobody is boosting p yet */
#ifdef CONFIG_BCACHE
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index 942a939cee95..b7565ad15494 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -689,7 +689,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas
}
raw_spin_lock(¤t->blocked_lock);
- __set_task_blocked_on(current, lock);
+ __set_task_blocked_on(current, lock, BO_T_MUTEX);
set_current_state(state);
trace_contention_begin(lock, LCB_F_MUTEX);
for (;;) {
@@ -734,7 +734,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas
* that has cleared our blocked_on state, re-set
* it to the lock we are trying to acquire.
*/
- __set_task_blocked_on(current, lock);
+ __set_task_blocked_on(current, lock, BO_T_MUTEX);
set_current_state(state);
/*
* Here we order against unlock; we must either see it change
@@ -762,7 +762,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas
raw_spin_lock_irqsave(&lock->wait_lock, flags);
raw_spin_lock(¤t->blocked_lock);
- __set_task_blocked_on(current, lock);
+ __set_task_blocked_on(current, lock, BO_T_MUTEX);
set_current_state(state);
if (opt_acquired)
@@ -1038,7 +1038,7 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne
*/
donor = current->blocked_donor;
if (donor) {
- struct mutex *next_lock;
+ void *next_lock;
raw_spin_lock_nested(&donor->blocked_lock, SINGLE_DEPTH_NESTING);
next_lock = __get_task_blocked_on(donor);
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 7885ff76e69f..2e8fe4b9bb88 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -150,6 +150,24 @@ static int __init setup_proxy_exec(char *str)
}
return 1;
}
+
+static inline struct task_struct *__blocked_on_owner(struct blocked_on_lock *bo)
+{
+ switch (bo->type) {
+ case BO_T_NONE:
+ return NULL;
+ case BO_T_MUTEX:
+ return __mutex_owner(bo->lock);
+ default:
+ WARN_ON_ONCE(1);
+ return NULL;
+ }
+}
+
+static inline struct task_struct *task_blocked_on_owner(struct task_struct *p)
+{
+ return __blocked_on_owner(&p->blocked_on);
+}
#else
static int __init setup_proxy_exec(char *str)
{
@@ -6799,7 +6817,7 @@ static void proxy_deactivate(struct rq *rq, struct task_struct *donor)
unsigned long state = READ_ONCE(donor->__state);
WARN_ON_ONCE(state == TASK_RUNNING);
- WARN_ON_ONCE(donor->blocked_on);
+ WARN_ON_ONCE(donor->blocked_on.lock);
/*
* Because we got donor from pick_next_task(), it is *crucial*
* that we call proxy_resched_idle() before we deactivate it.
@@ -6884,6 +6902,28 @@ static void proxy_migrate_task(struct rq *rq, struct rq_flags *rf,
proxy_reacquire_rq_lock(rq, rf);
}
+static void
+lock_blocked_on_lock(struct blocked_on_lock *bo)
+{
+ if (bo->type == BO_T_MUTEX)
+ raw_spin_lock(&((struct mutex *)bo->lock)->wait_lock);
+ else
+ WARN_ON_ONCE(1);
+}
+
+static void
+unlock_blocked_on_lock(struct blocked_on_lock *bo)
+{
+ if (bo->type == BO_T_MUTEX)
+ raw_spin_unlock(&((struct mutex *)bo->lock)->wait_lock);
+ else
+ WARN_ON_ONCE(1);
+}
+
+DEFINE_LOCK_GUARD_1(blocked_on_lock, struct blocked_on_lock,
+ lock_blocked_on_lock(_T->lock),
+ unlock_blocked_on_lock(_T->lock))
+
/*
* Find runnable lock owner to proxy for mutex blocked donor
*
@@ -6914,6 +6954,7 @@ static struct task_struct *
find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
__must_hold(__rq_lockp(rq))
{
+ struct blocked_on_lock bo, *blocked_on;
struct task_struct *owner = NULL;
bool curr_in_chain = false;
int this_cpu = cpu_of(rq);
@@ -6922,10 +6963,15 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
/* Follow blocked_on chain. */
for (p = donor; p->is_blocked; p = owner) {
- /* if its PROXY_WAKING, do return migration or run if current */
- struct mutex *mutex = p->blocked_on;
- if (!mutex) {
- clear_task_blocked_on(p, mutex);
+ /* copy the entire blocked_on structure */
+ raw_spin_lock(&p->blocked_lock);
+ bo = p->blocked_on;
+ raw_spin_unlock(&p->blocked_lock);
+ blocked_on = &bo;
+
+ /* Something changed in the chain, so pick again */
+ if (!blocked_on->lock) {
+ clear_task_blocked_on(p, NULL);
if (task_current(rq, p)) {
p->is_blocked = 0;
return p;
@@ -6937,11 +6983,11 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
* By taking mutex->wait_lock we hold off concurrent mutex_unlock()
* and ensure @owner sticks around.
*/
- guard(raw_spinlock)(&mutex->wait_lock);
+ guard(blocked_on_lock)(blocked_on);
guard(raw_spinlock)(&p->blocked_lock);
/* Check again that p is blocked with blocked_lock held */
- if (mutex != __get_task_blocked_on(p)) {
+ if (blocked_on->lock != __get_task_blocked_on(p)) {
/*
* Something changed in the blocked_on chain and
* we don't know if only at this level. So, let's
@@ -6954,7 +7000,7 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
if (task_current(rq, p))
curr_in_chain = true;
- owner = __mutex_owner(mutex);
+ owner = __blocked_on_owner(blocked_on);
if (!owner) {
/*
* If there is no owner, either clear blocked_on
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index e656c7059bf8..a386ac33e295 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2505,7 +2505,7 @@ static inline bool task_is_blocked(struct task_struct *p)
if (!sched_proxy_exec())
return false;
- return !!p->blocked_on;
+ return !!p->blocked_on.lock;
}
static inline int task_on_cpu(struct rq *rq, struct task_struct *p)
--
2.55.0.1082.g2b9226bbc0-goog
next prev parent reply other threads:[~2026-09-17 4:33 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-17 4:33 [RFC PATCH 00/12] FUTEX_PING: A stealable futex using Proxy Execution Suleiman Souhlal
2026-09-17 4:33 ` Suleiman Souhlal [this message]
2026-09-17 4:33 ` [RFC PATCH 02/12] futex: Switch PI futex to use p->pi_futex_lock instead of p->pi_lock Suleiman Souhlal
2026-09-17 15:38 ` Peter Zijlstra
2026-09-17 4:33 ` [RFC PATCH 03/12] futex: Add "ping" parameter to pi_state management functions and export them Suleiman Souhlal
2026-09-17 4:33 ` [RFC PATCH 04/12] futex: Introduce stealable PI futex, FUTEX_*_PING Suleiman Souhlal
2026-09-17 4:33 ` [RFC PATCH 05/12] futex: Implement exit_ping_state_list() Suleiman Souhlal
2026-09-17 4:33 ` [RFC PATCH 06/12] futex: Address aborting from futex_lock_ping() while owning ping_state Suleiman Souhlal
2026-09-17 4:33 ` [RFC PATCH 07/12] futex: Make FUTEX_*_PING use Proxy Execution Suleiman Souhlal
2026-09-17 13:18 ` Jihan LIN
2026-09-17 14:39 ` K Prateek Nayak
2026-09-17 15:36 ` Peter Zijlstra
2026-09-17 4:33 ` [RFC PATCH 08/12] futex: Implement PING futex handoff Suleiman Souhlal
2026-09-17 4:33 ` [RFC PATCH 09/12] futex: Wake up donor in PING futex unlock Suleiman Souhlal
2026-09-17 4:33 ` [RFC PATCH 10/12] futex: Optimistic spinning for PING futexes Suleiman Souhlal
2026-09-17 4:33 ` [RFC PATCH 11/12] futex: Allow userspace stealing " Suleiman Souhlal
2026-09-17 4:33 ` [RFC PATCH 12/12] tools/testing/futex: Add ping_bench, a tool for benchmarking futexes Suleiman Souhlal
2026-09-17 8:58 ` [RFC PATCH 00/12] FUTEX_PING: A stealable futex using Proxy Execution Peter Zijlstra
2026-09-17 17:53 ` John Stultz
2026-09-17 18:51 ` Steven Rostedt
2026-09-18 6:07 ` Suleiman Souhlal
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=20260917043339.2093426-2-suleiman@google.com \
--to=suleiman@google.com \
--cc=andrealmeid@igalia.com \
--cc=bsegall@google.com \
--cc=dave@stgolabs.net \
--cc=dietmar.eggemann@arm.com \
--cc=dvhart@infradead.org \
--cc=jstultz@google.com \
--cc=juri.lelli@redhat.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=qyousef@google.com \
--cc=rostedt@goodmis.org \
--cc=soolaugust@gmail.com \
--cc=ssouhlal@FreeBSD.org \
--cc=tglx@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
/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®