From nobody Tue Dec 2 01:03:55 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DEA0D33D6C2 for ; Mon, 24 Nov 2025 22:31:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764023491; cv=none; b=dJpm85UUlkeG5l7mBY0WnAXYfAO9eXsj49tYLPT23alRdNoJqeuiLoRrw2PwIUIExeCG1oGp80r4dzi9ghzc8oPHXGUSyfu2oyAKiKQzg1F7DY/N22jy7okEgERq3sYQaTB3oi2PvTfstH6BmsBkP9ymkS8O+BPl+JRZP8nJAbw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764023491; c=relaxed/simple; bh=Ucsmiu/NoiNA7ehb8K9jpIU8UXamZ22tEhb5x7pNYSo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tJBwoAdQTQXsvh7Qih2Raxx5OQiQl24wz1MQbCr9RJzyTll4nv4PGXi4LLhpmv3KsytsHv98NLNkXlDsLoGprOVkgHCjky+mw/0S2TE7/uWmVyhb/yeO8Wk+UoeeCb2JEzBVjB3eGNo3py5wkG+6FESA0wYduxDJUBM+5YHc0pI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jstultz.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ZlC41WNX; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jstultz.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZlC41WNX" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-34740cc80d5so4480716a91.0 for ; Mon, 24 Nov 2025 14:31:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764023489; x=1764628289; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rSWdmSGe7yQPl8xRxcM72FT/N1f79D0LaxcCyZMZ5tA=; b=ZlC41WNXPZrwOT6Cko0i6v1GdDM+iV02oADxhAofuQ93o/Scn8DZCreHnfI5jU4mz/ RnQSB7kpLmTeCRqHWfgSnTw3Q+kz+Wro/lARx66rM5Oe5C/lMXzJFh+eAoQcKbgt+wV0 1IYy95ccrMirWVCz9QlhWOy6/DXLCXjPBy0sls469zZKTCgS/pm36B2QYbt5KHFfHcQ+ pztppnwQVEeqRJ1T+3NgJP2/x3nrPOKkr7wZdF3vxRLlxXa3KFN8neYkKQE3eBwrehZZ da3KB28nON0cYDzLlaNDLjtpoZa6ISFUFR9LkeDH0+MyQhjq2QZHGEVtib3nkX5j99Bw i0Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764023489; x=1764628289; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rSWdmSGe7yQPl8xRxcM72FT/N1f79D0LaxcCyZMZ5tA=; b=fHxdvkPMBovF1N/gBmw6gozs4CDYXUk/Q7g7UESyl8kvTVJSTD75/9/OKDM7r7bZ1z Qp5sRZ9J+nQpyOcRtyYYyz+WChCogOBt4QUSg0QyOtR4bIxqPslLlMaGppdypE957bHw sAvM4TUQyQhjdWMNvzKNKuibhC/fWJhkyZnA8+sZG/rS3mKQ9OpQjwMwG1ekcLKnbWcT RwnWjf45m680qXZXLWe4C43yfWzjcmpIepXEhsH3xohfyFBNUB/8RgyJ7DylFqyF4JNb lQ2CNm+ZTNvzWPct966555V9D9vwFEMrRCVZdyPyEfacyijq7b1UIqM9Rw4aDmJH/nVT HW1Q== X-Gm-Message-State: AOJu0Yx0DR+Wa64u8jWO2MxpORwj1IhdtQkCEQjwz98gpvgeUaEuJuvg OE9QY8nEU/wg+HbTP3szqAKIn6YJZkdFaGtCb8zJZTEzDOsyiLaPhBIasQvT+Pee8QJlYKhN+u0 odC1aUXmHmyZAZs9YFT/lxSSOS//1YOXfN3yz3g8XBcCZ9h/aFvurMnZ5NDY/kqNMnQie6Y8uct uBU0yB4bRu1SWpT1LXeqT4ZxqgnNHLQCw//Yo6vXj33Bq4nzya X-Google-Smtp-Source: AGHT+IEIBisKczKfQ13L3ryImviRmXgpTQrc1TK9cSBl6wpyLa7pofj5WKWXb7O+/+T24lSWS3spRkIlS+xO X-Received: from pjblx14.prod.google.com ([2002:a17:90b:4b0e:b0:340:b94b:e61d]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2d87:b0:340:9d52:44c1 with SMTP id 98e67ed59e1d1-34733f3f6eamr14647549a91.35.1764023489187; Mon, 24 Nov 2025 14:31:29 -0800 (PST) Date: Mon, 24 Nov 2025 22:31:02 +0000 In-Reply-To: <20251124223111.3616950-1-jstultz@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251124223111.3616950-1-jstultz@google.com> X-Mailer: git-send-email 2.52.0.487.g5c8c507ade-goog Message-ID: <20251124223111.3616950-11-jstultz@google.com> Subject: [PATCH v24 10/11] sched: Add blocked_donor link to task for smarter mutex handoffs From: John Stultz To: LKML Cc: Peter Zijlstra , Juri Lelli , Valentin Schneider , "Connor O'Brien" , John Stultz , Joel Fernandes , Qais Yousef , Ingo Molnar , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , K Prateek Nayak , Thomas Gleixner , Daniel Lezcano , Suleiman Souhlal , kuyo chang , hupu , kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Peter Zijlstra Add link to the task this task is proxying for, and use it so the mutex owner can do an intelligent hand-off of the mutex to the task that the owner is running on behalf. Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Juri Lelli Signed-off-by: Valentin Schneider Signed-off-by: Connor O'Brien [jstultz: This patch was split out from larger proxy patch] Signed-off-by: John Stultz --- v5: * Split out from larger proxy patch v6: * Moved proxied value from earlier patch to this one where it is actually used * Rework logic to check sched_proxy_exec() instead of using ifdefs * Moved comment change to this patch where it makes sense v7: * Use more descriptive term then "us" in comments, as suggested by Metin Kaya. * Minor typo fixup from Metin Kaya * Reworked proxied variable to prev_not_proxied to simplify usage v8: * Use helper for donor blocked_on_state transition v9: * Re-add mutex lock handoff in the unlock path, but only when we have a blocked donor * Slight reword of commit message suggested by Metin v18: * Add task_init initialization for blocked_donor, suggested by Suleiman v23: * Reworks for PROXY_WAKING approach suggested by PeterZ Cc: Joel Fernandes Cc: Qais Yousef Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Juri Lelli Cc: Vincent Guittot Cc: Dietmar Eggemann Cc: Valentin Schneider Cc: Steven Rostedt Cc: Ben Segall Cc: Zimuzo Ezeozue Cc: Mel Gorman Cc: Will Deacon Cc: Waiman Long Cc: Boqun Feng Cc: "Paul E. McKenney" Cc: Metin Kaya Cc: Xuewen Yan Cc: K Prateek Nayak Cc: Thomas Gleixner Cc: Daniel Lezcano Cc: Suleiman Souhlal Cc: kuyo chang Cc: hupu Cc: kernel-team@android.com --- include/linux/sched.h | 1 + init/init_task.c | 1 + kernel/fork.c | 1 + kernel/locking/mutex.c | 44 +++++++++++++++++++++++++++++++++++++++--- kernel/sched/core.c | 18 +++++++++++++++-- 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 0d6c4c31e3624..178ed37850470 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1241,6 +1241,7 @@ struct task_struct { #endif =20 struct mutex *blocked_on; /* lock we're blocked on */ + struct task_struct *blocked_donor; /* task that is boosting this task */ raw_spinlock_t blocked_lock; =20 #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER diff --git a/init/init_task.c b/init/init_task.c index 60477d74546e0..34853a511b4d8 100644 --- a/init/init_task.c +++ b/init/init_task.c @@ -177,6 +177,7 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = =3D { .mems_allowed_seq =3D SEQCNT_SPINLOCK_ZERO(init_task.mems_allowed_seq, &init_task.alloc_lock), #endif + .blocked_donor =3D NULL, #ifdef CONFIG_RT_MUTEXES .pi_waiters =3D RB_ROOT_CACHED, .pi_top_task =3D NULL, diff --git a/kernel/fork.c b/kernel/fork.c index 0697084be202f..0a9a17e25b85d 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2136,6 +2136,7 @@ __latent_entropy struct task_struct *copy_process( lockdep_init_task(p); =20 p->blocked_on =3D NULL; /* not blocked yet */ + p->blocked_donor =3D NULL; /* nobody is boosting p yet */ =20 #ifdef CONFIG_BCACHE p->sequential_io =3D 0; diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 3cb9001d15119..08f438a54f56f 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -926,7 +926,7 @@ EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible); */ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, u= nsigned long ip) { - struct task_struct *next =3D NULL; + struct task_struct *donor, *next =3D NULL; DEFINE_WAKE_Q(wake_q); unsigned long owner; unsigned long flags; @@ -945,6 +945,12 @@ static noinline void __sched __mutex_unlock_slowpath(s= truct mutex *lock, unsigne MUTEX_WARN_ON(__owner_task(owner) !=3D current); MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP); =20 + if (sched_proxy_exec() && current->blocked_donor) { + /* force handoff if we have a blocked_donor */ + owner =3D MUTEX_FLAG_HANDOFF; + break; + } + if (owner & MUTEX_FLAG_HANDOFF) break; =20 @@ -958,7 +964,34 @@ static noinline void __sched __mutex_unlock_slowpath(s= truct mutex *lock, unsigne =20 raw_spin_lock_irqsave(&lock->wait_lock, flags); debug_mutex_unlock(lock); - if (!list_empty(&lock->wait_list)) { + + if (sched_proxy_exec()) { + raw_spin_lock(¤t->blocked_lock); + /* + * If we have a task boosting current, and that task was boosting + * current through this lock, hand the lock to that task, as that + * is the highest waiter, as selected by the scheduling function. + */ + donor =3D current->blocked_donor; + if (donor) { + struct mutex *next_lock; + + raw_spin_lock_nested(&donor->blocked_lock, SINGLE_DEPTH_NESTING); + next_lock =3D __get_task_blocked_on(donor); + if (next_lock =3D=3D lock) { + next =3D donor; + __set_task_blocked_on_waking(donor, next_lock); + wake_q_add(&wake_q, donor); + current->blocked_donor =3D NULL; + } + raw_spin_unlock(&donor->blocked_lock); + } + } + + /* + * Failing that, pick any on the wait list. + */ + if (!next && !list_empty(&lock->wait_list)) { /* get the first entry from the wait-list: */ struct mutex_waiter *waiter =3D list_first_entry(&lock->wait_list, @@ -966,14 +999,19 @@ static noinline void __sched __mutex_unlock_slowpath(= struct mutex *lock, unsigne =20 next =3D waiter->task; =20 + raw_spin_lock_nested(&next->blocked_lock, SINGLE_DEPTH_NESTING); debug_mutex_wake_waiter(lock, waiter); - set_task_blocked_on_waking(next, lock); + __set_task_blocked_on_waking(next, lock); + raw_spin_unlock(&next->blocked_lock); wake_q_add(&wake_q, next); + } =20 if (owner & MUTEX_FLAG_HANDOFF) __mutex_handoff(lock, next); =20 + if (sched_proxy_exec()) + raw_spin_unlock(¤t->blocked_lock); raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q); } =20 diff --git a/kernel/sched/core.c b/kernel/sched/core.c index e4a49c694dcd9..7f42ec01192dc 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6853,7 +6853,17 @@ static void proxy_force_return(struct rq *rq, struct= rq_flags *rf, * Find runnable lock owner to proxy for mutex blocked donor * * Follow the blocked-on relation: - * task->blocked_on -> mutex->owner -> task... + * + * ,-> task + * | | blocked-on + * | v + * blocked_donor | mutex + * | | owner + * | v + * `-- task + * + * and set the blocked_donor relation, this latter is used by the mutex + * code to find which (blocked) task to hand-off to. * * Lock order: * @@ -7002,6 +7012,7 @@ find_proxy_task(struct rq *rq, struct task_struct *do= nor, struct rq_flags *rf) * rq, therefore holding @rq->lock is sufficient to * guarantee its existence, as per ttwu_remote(). */ + owner->blocked_donor =3D p; } =20 /* Handle actions we need to do outside of the guard() scope */ @@ -7102,6 +7113,7 @@ static void __sched notrace __schedule(int sched_mode) unsigned long prev_state; struct rq_flags rf; struct rq *rq; + bool prev_not_proxied; int cpu; =20 /* Trace preemptions consistently with task switches */ @@ -7174,10 +7186,12 @@ static void __sched notrace __schedule(int sched_mo= de) switch_count =3D &prev->nvcsw; } =20 + prev_not_proxied =3D !prev->blocked_donor; pick_again: assert_balance_callbacks_empty(rq); next =3D pick_next_task(rq, &rf); rq_set_donor(rq, next); + next->blocked_donor =3D NULL; if (unlikely(task_is_blocked(next))) { next =3D find_proxy_task(rq, next, &rf); if (!next) { @@ -7243,7 +7257,7 @@ static void __sched notrace __schedule(int sched_mode) rq =3D context_switch(rq, prev, next, &rf); } else { /* In case next was already curr but just got blocked_donor */ - if (!task_current_donor(rq, next)) + if (prev_not_proxied && next->blocked_donor) proxy_tag_curr(rq, next); =20 rq_unpin_lock(rq, &rf); --=20 2.52.0.487.g5c8c507ade-goog