Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796183575; b=xY6UBW0fNMMf+1OAW0L7PhOtfHO6hUBTQcVWFBvxrKh3SYr87F+0nuBS43qirWYtPKBh r8v92etyaPmK4HRpqa+3Kd56PvVb0fArzG/cx5AmubGRyIaIqGGradcfzX9l/rpVVjAMj WQ8PY3hGSZx7zK77/OyNiiZ3iztxUI2qUlbmWbtClAo00quxfL2/GDXYqaGEimYPFyb3W KikZc/wWi0toJnnRfob9yfm1gFiYrbz57D1ViMYiEpZpDWv0J3F3KRirtbpvqI4853kMs /b8mypxUgSVsjOd6nvqeD0cc/o5jo0etLcZQev3UAFPnVbWCOQHpvS1JjMR12Wxu1MQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796183575; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Received:Date:In-Reply-To:Mime-Version: References:X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=PMYwJ8qxyfXbr1gQUVRIJgQxvSBWHRxjHA3K+pAuQgs=; b=moy0t9r6DY8SJtfE1K3vwk/biyKgPVXu3AWhK6wikgUM7vbhme0IDlTYfVPQ/GJabDAT rnsbnUq1w2Oq2yiVgXMUxRS3t4V7nrNf/SoYRLfXB9f1AEBDl0U/Iw3XbsmzjpbqjH3dO hoqK9Px6QgRd9etgn7lseJT5O9mlWiGSoP/JpdPa/pwlx63Jub/Zxq+QIJRXLjaMlAy3n F52mjEPqTYRgQtT6eNzjACu+RjSK7OkXlqmwUw4jJf9MESZXpmwlwP5Gjio2tH1CQidZg HYLHNF7B/wSfxUEDn7ApWRmdQ0BBDPdvq5QZBeD9zxEj5V7075+UEOWJn7f8Wm78cTg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jstultz.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=LDhSz6zp; arc=none smtp.remote-ip=209.85.214.199 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D083F606A337 for ; Wed, 2 Dec 2026 03:52:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D083F606A337 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.a=rsa-sha256 header.s=20260707 header.b=LDhSz6zp Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2f0578f1d51so141296825ad.3 for ; Tue, 01 Dec 2026 19:52:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796183574; x=1796788374; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=PMYwJ8qxyfXbr1gQUVRIJgQxvSBWHRxjHA3K+pAuQgs=; b=LDhSz6zpj5Mcgz0537VAiR0nz4ou+o2CLTirjCWqC1q5cy1GJshuestyMRmAAVDE9j EV+SHG4KJLamLz3keGoDCaDfGRYrLGTEFS/AMWemt8eyDzlqeW4eT7LFkUVpI9Z9+fiR a/fNyQzT8BssB26NS4EWra/cjf8d47dSEE3yAaIH1K/KNqy+5J5tphfuinc895xbh+TS uoELMrXnLfMb7AQl9ZTzGFLW/p8HrDvgutSYRxFvxVVFkdxSetFGC5wHdD8yS8HJESx3 7t+jJ8YBdV1v5kHWJgkUjI1N5HVZnCBaaqkBOVgQlNyL6zo//ToUbPskZJW+sSfIUCqE SiJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796183574; x=1796788374; h=content-type: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:content-type; bh=PMYwJ8qxyfXbr1gQUVRIJgQxvSBWHRxjHA3K+pAuQgs=; b=0Ypkc+Gi3JZhIkO0RQspVwWSgG7XotJAHowt10C/KLi+Qb+TskgUCIcTAERus64Ywl 2YnvHlSlhuq28PTyH+cJCV5b+g6xW8tB/OO4PfyYpDxNN9HK9CJL/nak5SP1r5ObT5uZ cXs+zzJEWe1ZEHolpN2t5IqROwFCb2us3lpD04w0050qSlsoDKAEGL7tWiOXp2UMYVKo 5M9+EyrhNiZ5JT8DCpidJbXAu78PIT7mTRETUWcGxRzZseLEANXQjk8vN+K11D7FDttW hrDquClz5XcIHgi8VbDqce820nC1F25iep9a+Wh331WW9oCmjHJzijbti454qXGUzjOY c3OQ== X-Gm-Message-State: APuU4Z90CAXbCOo5L91EEtSMaQ0/nlkE+7weIKs44YMlvcnY4MzHB/1F 37aA/ejuXNBurY3HVFQOobVexiPFxgy0Hs2sIzvTfiR2ywg7QgxaIcnCGkkS+gga97ULZ9SAQ8n YQ14gMeC6d+/DJTu6dzSk97kJF+R44iTb38z4ZPuD9ePFeRkO4t+oUYDob1kTZ6sHD7Z8415Pmc k6paQKUeby23TbkExUtTB0+20AwTQ0ofOBURo3849Exgq7L0BA X-Received: from plhc3.prod.google.com ([2002:a17:903:2343:b0:2e9:5442:b91a]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:8cc:b0:2ed:2d16:ceac with SMTP id d9443c01a7336-2f03f14a3a7mr407822385ad.36.1796183573737; Tue, 01 Dec 2026 19:52:53 -0800 (PST) Date: Wed, 2 Dec 2026 03:52:33 +0000 In-Reply-To: <20261202035248.1210026-1-jstultz@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202035248.1210026-1-jstultz@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202035248.1210026-6-jstultz@google.com> Subject: [PATCH v34 05/14] sched/proxy: Clear blocked_donor when the donor stops donating From: John Stultz To: LKML Cc: Mathieu Desnoyers , John Stultz , Peter Zijlstra , Ingo Molnar , K Prateek Nayak , Thomas Gleixner , Juri Lelli , Vincent Guittot , Valentin Schneider , Suleiman Souhlal , Andrea Righi , Johannes Weiner , Metin Kaya , Dietmar Eggemann , Steven Rostedt , Zimuzo Ezeozue , Waiman Long , Boqun Feng , "Paul E. McKenney" , Joel Fernandes , Qais Yousef , Xuewen Yan , kuyo chang , hupu , Zhan Xusheng , "zhidao su (Xiaomi)" , Hui Su , "Shubhang Kaushik (Ampere)" , kernel-team@android.com Content-Type: text/plain; charset="UTF-8" From: Mathieu Desnoyers find_proxy_task() sets owner->blocked_donor to the task that donates to the owner. __mutex_unlock_slowpath() reads current->blocked_donor to hand the mutex to that task. The comment of the field says that it is set in schedule() and only stable under preempt_disable(). So the pointer is valid until the owner schedules again. But the donor can stop donating while the owner keeps running. It is woken, or a change of its scheduling class or of its affinity takes it off the runqueue. The owner keeps its blocked_donor until it schedules. Meanwhile the donor can run on another CPU and exit. This would be a use after free: __mutex_unlock_slowpath() would take the blocked_lock of a freed task. It was found by reading the code, and has not been reproduced. The owner must stay in the kernel without scheduling for as long as the donor needs to exit and be freed. Fix this by clearing rq->curr->blocked_donor when the task it points at is woken, or leaves the runqueue in sched_proxy_block_task() or via deactivate_task(). The clear takes the blocked_lock of the running task, which the unlock path holds while it uses the pointer. Fixes: 1628b25248d0 ("sched: Add blocked_donor link to task for smarter mutex handoffs") Signed-off-by: Mathieu Desnoyers [jstultz: Minor tweaks. Moved call to deactivate_task() to catch the case more generally then calling from numerous other sites] Signed-off-by: John Stultz --- Cc: Peter Zijlstra Cc: Ingo Molnar Cc: K Prateek Nayak Cc: Thomas Gleixner Cc: Juri Lelli Cc: Vincent Guittot Cc: Valentin Schneider Cc: Suleiman Souhlal Cc: Andrea Righi Cc: Johannes Weiner Cc: Metin Kaya Cc: Dietmar Eggemann Cc: Steven Rostedt Cc: Zimuzo Ezeozue Cc: Waiman Long Cc: Boqun Feng Cc: "Paul E. McKenney" Cc: Joel Fernandes Cc: Qais Yousef Cc: Xuewen Yan Cc: kuyo chang Cc: hupu Cc: Zhan Xusheng Cc: zhidao su (Xiaomi) Cc: Hui Su Cc: Shubhang Kaushik (Ampere) Cc: kernel-team@android.com --- kernel/sched/core.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index eb2ab495f1df0..ff85346a9c49d 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2186,6 +2186,8 @@ static inline void uclamp_post_fork(struct task_struct *p) { } static inline void init_uclamp(void) { } #endif /* !CONFIG_UCLAMP_TASK */ +static inline void proxy_clear_curr_donor(struct rq *rq, struct task_struct *p); + bool sched_task_on_rq(struct task_struct *p) { return task_on_rq_queued(p); @@ -2275,6 +2277,9 @@ void deactivate_task(struct rq *rq, struct task_struct *p, int flags) WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING); ASSERT_EXCLUSIVE_WRITER(p->on_rq); + /* check curr's blocked_donor back pointer and clear it if it is p */ + proxy_clear_curr_donor(rq, p); + /* * Code explicitly relies on TASK_ON_RQ_MIGRATING begin set *before* * dequeue_task() and cleared *after* enqueue_task(). @@ -2612,6 +2617,33 @@ static inline bool is_cpu_allowed(struct task_struct *p, int cpu) * is done. */ +#ifdef CONFIG_SCHED_PROXY_EXEC +/* + * @p stops donating on @rq: it is woken, or it leaves the runqueue. The + * task that runs can still point at @p with blocked_donor until it + * schedules, and __mutex_unlock_slowpath() dereferences that pointer. + * Meanwhile @p can run on another CPU and exit. So clear the pointer. + * The unlock path reads it under the blocked_lock of the running task. + */ +static inline void proxy_clear_curr_donor(struct rq *rq, struct task_struct *p) +{ + struct task_struct *curr = rq->curr; + + lockdep_assert_rq_held(rq); + + if (READ_ONCE(curr->blocked_donor) != p) + return; + + guard(raw_spinlock)(&curr->blocked_lock); + if (curr->blocked_donor == p) + WRITE_ONCE(curr->blocked_donor, NULL); +} +#else /* !CONFIG_SCHED_PROXY_EXEC */ +static inline void proxy_clear_curr_donor(struct rq *rq, struct task_struct *p) +{ +} +#endif /* !CONFIG_SCHED_PROXY_EXEC */ + /* * move_queued_task - move a queued task to new rq. * @@ -3864,6 +3896,7 @@ static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p) { bool dequeued; + proxy_clear_curr_donor(rq, p); /* * Typically per __set_task_cpu(), task_cpu(p) == p->wake_cpu. * @@ -6947,6 +6980,7 @@ void sched_proxy_block_task(struct rq *rq, struct task_struct *p) if (WARN_ON_ONCE(state == TASK_RUNNING)) return; + proxy_clear_curr_donor(rq, p); if (task_current_donor(rq, p)) proxy_reset_donor(rq); -- 2.56.1.911.g883d8bcb20-goog