From nobody Tue Dec 2 01:03:56 2025 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 7AB4B33C533 for ; Mon, 24 Nov 2025 22:31:28 +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=1764023490; cv=none; b=gKJFNobbGsQ7AbI5naOyywuL8wcJYoL4133qMGkkARfczyBUW1e/Vnq0MauEgWDVrFEt34LohGrhZVhgIppuwO9/MrZApE0WKPfnni/KMSbvTssJuuXdFcIeI4tlVqmYuwGrAC3rv+FheRPN5L8hW7Ek4Pioij5CX23ajB4U+H8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764023490; c=relaxed/simple; bh=v/zcF9Srz6eBhbU8WwLt/GRqv69lW0hoZwLP4abxNfk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=I+yeEBzxdbUV5RbxIa9eV7GdbYUh6TCg3dUpO+VVJV6Jg6pcDNxcYUXL+w3xuVohu2JSV0OhIOV4FS8brTnaskQQlQcXkZm0bIjk69mBLKqNy+vxkeHwNOiS/u9YnS+lKAj/QPMF4ic93xsTVtkjwgps1Ykii/dnpi8QUqpHU/k= 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=4QKJ7FIk; 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="4QKJ7FIk" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-343bf6ded5cso9954808a91.0 for ; Mon, 24 Nov 2025 14:31:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764023488; x=1764628288; 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=FTJN291N9+/JGE3/Shyo2dHmnxFQmABX+nrmE06GkUY=; b=4QKJ7FIkLFaJndOUktc7mrUC4wLFaZHtEo2ChN2evJS+TPJBY4y3KwauRaVGnSDGEw n6skGD1sPBCCFzsiXE1nNPpoSqDLPyc0mMiMGDyUhEJGCEpZGl5BQ/a79NAlTl3F+onz QhhH3r+LCKJZiheMzTGtOnnSpYQCFhHMOsLk9CrVt3pX5Khupy1AdNRZxFsmkr1ljuLC dfk6r4moolbULcHlZm5ttgac6G07HjiP0glzi/SD46SjAmC8PURDQbhtZjQbur1WCmUQ k287d2Z+g/ys2jI8ROdGd7tYxsBz6Qc8QZAvLse+mSBr1Ce5qxo8FC3OsuELjTp1MlPu IMiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764023488; x=1764628288; 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=FTJN291N9+/JGE3/Shyo2dHmnxFQmABX+nrmE06GkUY=; b=UvzuubbBN4cVFFiFnwMoiCfiBcIoazBW7j75ar7a6QXAut5KF0+zyG1MF0+vhZa4Ec 1fhZSe5smtFT97bHnONHQMvs8g87NjqkZfE/QT/TYoqZjWuOdCdWiguUMIE856jOgbIL fS2go6WBSC3OStg6mIvnaFkJ4Xc3LajFnnmHq4QYMJdNw1lamAKMpszeus/bm77pxyEf X0F+l4zu5KDYk49bcP4q+v51xSTpFR8DJb/nzrkeXTuRCFrIWbi9Uuqth2tCObC3V/dz gAnyECis70qwVzOaeeyy51Ps0strXhI3aEQOt2OyCwG65Ey46A4HbjnKrS/dkeFT2/+p B7PQ== X-Gm-Message-State: AOJu0YzibSHkMO0FlMwHZExbR0K102h8IHejXNvkKdv0EnNYQTEJK+mi RfesW1wfhtw+/Sr7jhJ0YE6byEy0Fdpe5lMTanaoiN7P0zKbd7/nXIkJc6jwo9Yya19sSFRLjj8 gWhDBsar4LzrSgtIoEU6gNo0Lta2TH+4QAl0qNEHjecaZ3X7AKA5Ly1NsvGA33v6SkdWdx1aOqq 9L7+yX4xg0wN7E+nbmNCMlsFCQavrlC543AprFViXGwonfIF4u X-Google-Smtp-Source: AGHT+IHVvq04S49eCuYEGuzv4Pm5BmwPp7jcgA75U10/aoQ8QpmolsrKgGgyfemd/jxG6VLZrSYXujUQ3L11 X-Received: from pjbtb7.prod.google.com ([2002:a17:90b:53c7:b0:342:14d9:ca4f]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3c8e:b0:340:ca7d:936a with SMTP id 98e67ed59e1d1-34733f22173mr14981153a91.18.1764023487676; Mon, 24 Nov 2025 14:31:27 -0800 (PST) Date: Mon, 24 Nov 2025 22:31:01 +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-10-jstultz@google.com> Subject: [PATCH v24 09/11] sched: Have try_to_wake_up() handle return-migration for PROXY_WAKING case From: John Stultz To: LKML Cc: John Stultz , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , 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" This patch adds logic so try_to_wake_up() will notice if we are waking a task where blocked_on =3D=3D PROXY_WAKING, and if necessary dequeue the task so the wakeup will naturally return-migrate the donor task back to a cpu it can run on. This helps performance as we do the dequeue and wakeup under the locks normally taken in the try_to_wake_up() and avoids having to do proxy_force_return() from __schedule(), which has to re-take similar locks and then force a pick again loop. This was split out from the larger proxy patch, and significantly reworked. Credits for the original patch go to: Peter Zijlstra (Intel) Juri Lelli Valentin Schneider Connor O'Brien Signed-off-by: John Stultz --- v24: * Reworked proxy_needs_return() so its less nested as suggested by K Prateek * Switch to using block_task with DEQUEUE_SPECIAL as suggested by K Prateek * Fix edge case to reset wake_cpu if select_task_rq() chooses the current rq and we skip set_task_cpu() 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 --- kernel/sched/core.c | 84 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index fcf64c4db437e..e4a49c694dcd9 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3697,6 +3697,64 @@ static inline void proxy_set_task_cpu(struct task_st= ruct *p, int cpu) __set_task_cpu(p, cpu); p->wake_cpu =3D wake_cpu; } + +static bool proxy_task_runnable_but_waking(struct task_struct *p) +{ + if (!sched_proxy_exec()) + return false; + return (READ_ONCE(p->__state) =3D=3D TASK_RUNNING && + READ_ONCE(p->blocked_on) =3D=3D PROXY_WAKING); +} + +static inline struct task_struct *proxy_resched_idle(struct rq *rq); + +/* + * Checks to see if task p has been proxy-migrated to another rq + * and needs to be returned. If so, we deactivate the task here + * so that it can be properly woken up on the p->wake_cpu + * (or whichever cpu select_task_rq() picks at the bottom of + * try_to_wake_up() + */ +static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p) +{ + if (!sched_proxy_exec()) + return false; + + guard(raw_spinlock)(&p->blocked_lock); + + /* If task isn't PROXY_WAKING, we don't need to do return migration */ + if (p->blocked_on !=3D PROXY_WAKING) + return false; + + __clear_task_blocked_on(p, PROXY_WAKING); + + /* If already current, don't need to return migrate */ + if (task_current(rq, p)) + return false; + + /* If wake_cpu is targeting this cpu, don't bother return migrating */ + if (p->wake_cpu =3D=3D cpu_of(rq)) { + resched_curr(rq); + return false; + } + + /* If we're return migrating the rq->donor, switch it out for idle */ + if (task_current_donor(rq, p)) + proxy_resched_idle(rq); + + /* (ab)Use DEQUEUE_SPECIAL to ensure task is always blocked here. */ + block_task(rq, p, DEQUEUE_NOCLOCK | DEQUEUE_SPECIAL); + return true; +} +#else /* !CONFIG_SCHED_PROXY_EXEC */ +static bool proxy_task_runnable_but_waking(struct task_struct *p) +{ + return false; +} +static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p) +{ + return false; +} #endif /* CONFIG_SCHED_PROXY_EXEC */ =20 static void @@ -3784,6 +3842,8 @@ static int ttwu_runnable(struct task_struct *p, int w= ake_flags) update_rq_clock(rq); if (p->se.sched_delayed) enqueue_task(rq, p, ENQUEUE_NOCLOCK | ENQUEUE_DELAYED); + if (proxy_needs_return(rq, p)) + goto out; if (!task_on_cpu(rq, p)) { /* * When on_rq && !on_cpu the task is preempted, see if @@ -3794,6 +3854,7 @@ static int ttwu_runnable(struct task_struct *p, int w= ake_flags) ttwu_do_wakeup(p); ret =3D 1; } +out: __task_rq_unlock(rq, &rf); =20 return ret; @@ -4181,6 +4242,8 @@ int try_to_wake_up(struct task_struct *p, unsigned in= t state, int wake_flags) * it disabling IRQs (this allows not taking ->pi_lock). */ WARN_ON_ONCE(p->se.sched_delayed); + /* If p is current, we know we can run here, so clear blocked_on */ + clear_task_blocked_on(p, NULL); if (!ttwu_state_match(p, state, &success)) goto out; =20 @@ -4197,8 +4260,15 @@ int try_to_wake_up(struct task_struct *p, unsigned i= nt state, int wake_flags) */ scoped_guard (raw_spinlock_irqsave, &p->pi_lock) { smp_mb__after_spinlock(); - if (!ttwu_state_match(p, state, &success)) - break; + if (!ttwu_state_match(p, state, &success)) { + /* + * If we're already TASK_RUNNING, and PROXY_WAKING + * continue on to ttwu_runnable check to force + * proxy_needs_return evaluation + */ + if (!proxy_task_runnable_but_waking(p)) + break; + } =20 trace_sched_waking(p); =20 @@ -4305,6 +4375,16 @@ int try_to_wake_up(struct task_struct *p, unsigned i= nt state, int wake_flags) wake_flags |=3D WF_MIGRATED; psi_ttwu_dequeue(p); set_task_cpu(p, cpu); + } else if (cpu !=3D p->wake_cpu) { + /* + * If we were proxy-migrated to cpu, then + * select_task_rq() picks cpu instead of wake_cpu + * to return to, we won't call set_task_cpu(), + * leaving a stale wake_cpu pointing to where we + * proxy-migrated from. So just fixup wake_cpu here + * if its not correct + */ + p->wake_cpu =3D cpu; } =20 ttwu_queue(p, cpu, wake_flags); --=20 2.52.0.487.g5c8c507ade-goog