From nobody Fri Dec 19 16:26:30 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 BE05A1397 for ; Fri, 5 Dec 2025 01:27:25 +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=1764898047; cv=none; b=NiCFrI6iLW1mFSpAB4YQSlIj18FuP8TBFcSOOoBmKcS7FrniJ73M224j8nv2+mxwUYhDDYiQaiNHyaODttINDf3gAloAASxEKVCKSYfdHG+hw6HvL7ZFsx1U+rU58dQk+99ILv3eauihtJ5giWonUrePW/dF6W/ljl+bIWQumXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764898047; c=relaxed/simple; bh=ku2X8hXmaGpWiIH2juDb+KV6mbdUi8q+BT7RtFo/0kA=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=LTrWhqswwQOP4ioGMOUAxiAd5gPuv0O6h9RUA8PZuhe1Rj+LQ/PbhCtcWvH2ETv7jHSnqlrKrFg6sdLV9oFWvxfRIT8L9vrzrXprbwPPmufSj/TJkmbcwFVJ6H9tLlb97QOUh/Jhomku9rbwnwwkBxos6eGUd8/oUZeYz/zbQ/c= 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=prhCfLsY; 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="prhCfLsY" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3437b43eec4so2312305a91.3 for ; Thu, 04 Dec 2025 17:27:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764898045; x=1765502845; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=X47zQtstNAmdpN4yVzslINokfgKwk1psMjjHM6FsJkQ=; b=prhCfLsYkMEaSi1Tt+xG+jIpfAHsBzr+gQSbBPsCUooJRKZDckraJvl/CUpu4K/rgh MZOWUfe1BiprCMsNowYKz27RyNArEJn2XYGTMnX2TeSDo81a6hz7E8EgeROsQ3kHpd/0 QBo2b74omarvOH4S5XKPPxb7x5gKVgftjsfG4LIsLrLFb4VdxPglkd/6BvJIKkIWIqTq LCrFw6v05h9Pi/MR1OQ8Qiy/6fbd+EqeurhHBPbNZp+Yf3uI6faayFB3sS4TT4UfcREs 4yRDHYfljQDs4pdJnhnuhWcr1ezrg55M7YAAthM1QCSbliz+1KyWYT40HveF8N7y0fYZ FYpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764898045; x=1765502845; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=X47zQtstNAmdpN4yVzslINokfgKwk1psMjjHM6FsJkQ=; b=lK+Xf7w0wUeOPvMcQACh5Z2RVebh6asvwuIL0on9T/bGlpZziCUT7fRJHe8QnqQxqH f8magxxkG2x1tAv7oliMHTa0OpCVxiMLTjyE74kn9/swxjHFRV0Nn1Ppksnwk8ywxhb3 ev+hVTj7X4Ph/kI0fARpaHWCGcYUqiiSysP+SDjRNLhiYdI7PgzNfmiluzuUuUxrkEyA 5IXWKI4LaFHI4ce4zTq0VpB4YV6EFUilEg0wxk2TtN2K1bv7nOYjzpjMz+3icLDkw+/+ KM3mJz0V7/Wm+xee03tDdOmwcvICStmueqkJwUUpxsSraww6ZQxucRhyOBI4sAD/KtxT hOww== X-Gm-Message-State: AOJu0YzaeZMcKCIkI1sz/JMc5JMlnbVYvp2C8G/m0Nxy08phVpzNKezl C6eBA/fq+VYAZ/559IKfmIlO+yj43g2rTzZRwu49iEHsgsGizQNGJqC5nByVCHeCBORRWcw11b4 CS+zymxS41eNwQ7A9sDDlEYdCDSDjLU3zDPnqpt7J2aw0414wqfoaAmNwPVeWq+vHUAFGALLHnQ zoZRF6LtMxjRjQQ23KpaLWwzpFxPZ8E0K/1Kdwf+Y0yeqGoTSu X-Google-Smtp-Source: AGHT+IGCbz3ipIa3+vr8ulIT1vfff70zUI2WMI45vZa8ncNLJe5D4sk+a9FqnEkM39Gidad0BWSEos4w16CZ X-Received: from pjbst6.prod.google.com ([2002:a17:90b:1fc6:b0:340:631a:67fb]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:e7d0:b0:343:747e:2cac with SMTP id 98e67ed59e1d1-349126fb674mr10373710a91.31.1764898044548; Thu, 04 Dec 2025 17:27:24 -0800 (PST) Date: Fri, 5 Dec 2025 01:27:09 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.52.0.223.gf5cc29aaa4-goog Message-ID: <20251205012721.756394-1-jstultz@google.com> Subject: [PATCH v3] sched: Fix psi_dequeue for Proxy Execution From: John Stultz To: LKML Cc: John Stultz , K Prateek Nayak , Haiyue Wang , Johannes Weiner , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Suren Baghdasaryan , Steven Rostedt , Ben Segall , Zimuzo Ezeozue , Mel Gorman , Will Deacon , Waiman Long , Boqun Feng , "Paul E. McKenney" , Metin Kaya , Xuewen Yan , 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" Currently, if the sleep flag is set, psi_dequeue() doesn't change any of the psi_flags. This is because psi_task_switch() will clear TSK_ONCPU as well as other potential flags (TSK_RUNNING), and the assumption is that a voluntary sleep always consists of a task being dequeued followed shortly there after with a psi_sched_switch() call. Proxy Execution changes this expectation, as mutex-blocked tasks that would normally sleep stay on the runqueue. But in the case where the mutex-owning task goes to sleep, or the owner is on a remote cpu, we will then deactivate the blocked task shortly after. In that situation, the mutex-blocked task will have had its TSK_ONCPU cleared when it was switched off the cpu, but it will stay TSK_RUNNING. Then if we later dequeue it (as currently done if we hit a case find_proxy_task() can't yet handle, such as the case of the owner being on another rq or a sleeping owner) psi_dequeue() won't change any state (leaving it TSK_RUNNING), as it incorrectly expects a psi_task_switch() call to immediately follow. Later on when the task get woken/re-enqueued, and psi_flags are set for TSK_RUNNING, we hit an error as the task is already TSK_RUNNING: psi: inconsistent task state! task=3D188:kworker/28:0 cpu=3D28 psi_flags= =3D4 clear=3D0 set=3D4 To resolve this, extend the logic in psi_dequeue() so that if the sleep flag is set, we also check if psi_flags have TSK_ONCPU set (meaning the psi_task_switch is imminent) before we do the shortcut return. If TSK_ONCPU is not set, that means we've already switched away, and this psi_dequeue call needs to clear the flags. Fixes: be41bde4c3a8 ("sched: Add an initial sketch of the find_proxy_task()= function") Reported-by: K Prateek Nayak Closes: https://lore.kernel.org/lkml/20251117185550.365156-1-kprateek.nayak= @amd.com/ Signed-off-by: John Stultz Tested-by: K Prateek Nayak Tested-by: Haiyue Wang Acked-by: Johannes Weiner --- proxy-v13: * Reworked for collision proxy-v15: * Fixed commit message typo noticed by Todd Kjos v1 (separate from proxy series): * Reworded commit message in response to K Prateek pointing out this issue can affect us earlier in the full proxy series then I had anticipated. v2 (separate from proxy series): * Minor tweak to comment suggested by Johannes v3 (separate from proxy series): * Minor tweak to commit message suggested by Haiyue 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: Johannes Weiner Cc: Suren Baghdasaryan 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: Haiyue Wang Cc: kernel-team@android.com --- kernel/sched/stats.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/sched/stats.h b/kernel/sched/stats.h index cbf7206b3f9d7..c903f1a428911 100644 --- a/kernel/sched/stats.h +++ b/kernel/sched/stats.h @@ -180,8 +180,13 @@ static inline void psi_dequeue(struct task_struct *p, = int flags) * avoid walking all ancestors twice, psi_task_switch() handles * TSK_RUNNING and TSK_IOWAIT for us when it moves TSK_ONCPU. * Do nothing here. + * + * In the SCHED_PROXY_EXECUTION case we may do sleeping + * dequeues that are not followed by a task switch, so check + * TSK_ONCPU is set to ensure the task switch is imminent. + * Otherwise clear the flags as usual. */ - if (flags & DEQUEUE_SLEEP) + if ((flags & DEQUEUE_SLEEP) && (p->psi_flags & TSK_ONCPU)) return; =20 /* --=20 2.52.0.223.gf5cc29aaa4-goog