From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 9DA8637267D for ; Sat, 4 Apr 2026 05:36:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775281019; cv=none; b=tDC4gL+6rBDX1xrecj/6J6tKW99TmidNV0xJ29QJsdLRDyOvYZ8RKLxTgv+X1U7fu0ji8x8vICFl0hGdTqlQZDVUKArPQ2Vb0DG+2Jh8/scAe2ql5rqNoEUUs/W6Y+ZjLa/DJzK6QXe/caK7dhiuilWqW2TCN3Cu3hba096kb48= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775281019; c=relaxed/simple; bh=8DF1CAjyMoRMqx9bLLlT9MasF75wWUAIPKyM43lBm0w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aiUa4WGDFAUhgho5bqShLTVR/AGiX6IQXgNLCHmQOQnBE//SQ3cLKJa6AABg79kY5Aht2XQ7CwTDiOBzLfvJ3Qwc75odw/eF/BQxD9IixlR+IpxAwFpmf4+CVp+Bo8wFqKv2vdZ1+W3rv0iKTXmPew54Zb0cJKqC1wXMwuDaGbM= 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=Pod54vhK; arc=none smtp.client-ip=209.85.216.74 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="Pod54vhK" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-35d93a8149bso4002186a91.0 for ; Fri, 03 Apr 2026 22:36:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775281015; x=1775885815; 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=fdvh9En/gGOuEWyTn5zdfN4mzmJEQdNkdBVEu82mKWM=; b=Pod54vhKoSc9JUH36ZkQ0zhH6FxafhZOwYqzBCdnGDI8v/XC5CDxrk1FDRECvTdCge Jq40CZNCa7UD7K/d+ZJHZf3VjCVI4jBJ50PX0568izyku3X12eFvCfBH586xq1VODezC T39XkxWraBVmLlXoLAVGn/YKRWStsUjOzigaMmiL+iqxt4GXx8AQWrDfgdM7flcyuaa1 LU/tf77/7fAixCdGhxfBM8LCGJI+TH3zZ7vExhr4i1vSIfwZryYtQUslrns7lxbuLHPR aOPNTIBsd/JnFMfQhw22JjHtdajFHnBlxSr9dc6GlPa+Di6T8noje2aoDU799/t1lNQB 5pmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775281015; x=1775885815; 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=fdvh9En/gGOuEWyTn5zdfN4mzmJEQdNkdBVEu82mKWM=; b=kpOI/ufUWuqNnX/MDaJERIKo+idPGn4YZ4za+G5TaCZfVQ8RrXuCHrUUJutTrBq1QU BDeu6no4AeytRYgK69NX5nEKO1Xz6ZsZdNGL7IVQ8uqJzISPQKua8lIhEFP3iA8JJDzP aHd6YkYPeODPy+Fu3rWnwKYbcsZpuFCCWeH24vbPZBgck0nPe2QhOKgJ3/30lPP2WGHW EITzjESL7NXTAUIB/4eMB9D6E07kLy9O3QPTwzs3tnP0k01OXsvjdzeUzzEnK+r+heus 8u6tNCnKTHC9oH94GV5X4pA5zv//ViOwtatYlERuX+tqDmrNJD69GcGB/ltdJR0FqoLP vxhg== X-Gm-Message-State: AOJu0YzqZfojWqhTZGHpmpXKlF8Ns58ok1tTfKfx0Olk37mirhuTw06N VASEkLLPc49Nnp4Dt5w4FQGiHJDf29pH88d4Rjsb7E0jUyNyEkwBRHBd+7gMCd7hPm5bQIZYSC9 L9BOzJCqeLY1StOqcNkh5XDRYQT8zSwLamQ0dRSp9zTXNoAxNwSF7iqVjB2zI8I1pbhd1W1FfYO 1gxXubcpVaSd9YUH0pBTNb3QKgedGUZY1WHHInIasQXjLGhkFZ X-Received: from pga9.prod.google.com ([2002:a05:6a02:4f89:b0:c5e:84e5:d15c]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6d8c:b0:398:8ea8:5f9f with SMTP id adf61e73a8af0-39f16f8dbd9mr7875029637.16.1775281013795; Fri, 03 Apr 2026 22:36:53 -0700 (PDT) Date: Sat, 4 Apr 2026 05:36:25 +0000 In-Reply-To: <20260404053632.1729280-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: <20260404053632.1729280-1-jstultz@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260404053632.1729280-9-jstultz@google.com> Subject: [PATCH v27 08/10] 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-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 v25: * Simplified some logic now we don't have proxy_tag_curr() 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 | 14 +++++++++++++- 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 3ae1330801157..18665b4b973e2 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1238,6 +1238,7 @@ struct task_struct { #endif 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; #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER diff --git a/init/init_task.c b/init/init_task.c index b5f48ebdc2b6e..41c19670c8f6b 100644 --- a/init/init_task.c +++ b/init/init_task.c @@ -200,6 +200,7 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = { .mems_allowed_seq = SEQCNT_SPINLOCK_ZERO(init_task.mems_allowed_seq, &init_task.alloc_lock), #endif + .blocked_donor = NULL, #ifdef CONFIG_RT_MUTEXES .pi_waiters = RB_ROOT_CACHED, .pi_top_task = NULL, diff --git a/kernel/fork.c b/kernel/fork.c index 079802cb61002..a3d2cd4395791 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2178,6 +2178,7 @@ __latent_entropy struct task_struct *copy_process( lockdep_init_task(p); p->blocked_on = NULL; /* not blocked yet */ + p->blocked_donor = NULL; /* nobody is boosting p yet */ #ifdef CONFIG_BCACHE p->sequential_io = 0; diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 7d359647156df..65f0f35b88972 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -942,7 +942,7 @@ EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible); */ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip) { - struct task_struct *next = NULL; + struct task_struct *donor, *next = NULL; DEFINE_WAKE_Q(wake_q); unsigned long owner; unsigned long flags; @@ -961,6 +961,12 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne MUTEX_WARN_ON(__owner_task(owner) != current); MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP); + if (sched_proxy_exec() && current->blocked_donor) { + /* force handoff if we have a blocked_donor */ + owner = MUTEX_FLAG_HANDOFF; + break; + } + if (owner & MUTEX_FLAG_HANDOFF) break; @@ -974,7 +980,34 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne 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 = current->blocked_donor; + if (donor) { + struct mutex *next_lock; + + raw_spin_lock_nested(&donor->blocked_lock, SINGLE_DEPTH_NESTING); + next_lock = __get_task_blocked_on(donor); + if (next_lock == lock) { + next = donor; + __set_task_blocked_on_waking(donor, next_lock); + wake_q_add(&wake_q, donor); + current->blocked_donor = 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 = list_first_entry(&lock->wait_list, @@ -982,14 +1015,19 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne next = waiter->task; + 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); + } if (owner & MUTEX_FLAG_HANDOFF) __mutex_handoff(lock, next); + if (sched_proxy_exec()) + raw_spin_unlock(¤t->blocked_lock); raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q); } diff --git a/kernel/sched/core.c b/kernel/sched/core.c index a0d55225a62c3..9197b4274de8c 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6753,7 +6753,17 @@ static void proxy_migrate_task(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: * @@ -6893,6 +6903,7 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf) * rq, therefore holding @rq->lock is sufficient to * guarantee its existence, as per ttwu_remote(). */ + owner->blocked_donor = p; } WARN_ON_ONCE(owner && !owner->on_rq); return owner; @@ -7050,6 +7061,7 @@ static void __sched notrace __schedule(int sched_mode) clear_task_blocked_on(prev, NULL); rq_set_donor(rq, next); + next->blocked_donor = NULL; if (unlikely(next->blocked_on)) { next = find_proxy_task(rq, next, &rf); if (!next) { -- 2.53.0.1213.gd9a14994de-goog