From nobody Tue Dec 2 02:43:02 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 A534F2D8399 for ; Tue, 18 Nov 2025 05:52:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763445168; cv=none; b=gfRFAxKvgKlwZ/NxFePANTlJ3RSOH87m7Pa4cPnGU5pJIEhAhkFYBcWYDbvKU/9uXywkp7dst+gtxIq0EUs8fpVu0GB7m+6E2avOXuXnEIXH9MIS9cFHMxpwXgShUGI4wjFP0bG31ssm1ZVXjsOG10+b1q3dFEsFrTV5LzjLOBk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763445168; c=relaxed/simple; bh=I9k/bF78qO5Qov+LsMMyHuVx0m8z9vu3dfsPCpy+WBw=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=iXwuHBagh/m2hKLaTjcAx41iNZSsEz9IzT8FgNXyr074OQ+Abid02WBXmNSmg7VT2wBOeiGnyoOJ1CQCPcwFWqO7H+oe1yEDxJABKZ7fKaJBuaXIsyvT3lWOzEMg0wRGHkzQLYzBF+nPVkT4CuBtX0Iy/gPJ8nrSciGb498Btmk= 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=Bia6uEip; arc=none smtp.client-ip=209.85.214.201 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="Bia6uEip" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-297fbfb4e53so92206445ad.1 for ; Mon, 17 Nov 2025 21:52:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763445166; x=1764049966; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=QOnUB6O9YOJFcHVG2GENeUI+86wRQEOj+D5/8znK/J4=; b=Bia6uEipMpF1TqIQ8JM0eaxQY1rAJCWQLA/W8b77irkbouSNamnnyolbjIzww2etcw Pv9GsPiEvo2162iWkzn4abQkrmC3s6tA26DK/bteMPvgr9xDWoWuKouy/fZy9JtV8UkA YqV2R8IYbYw/fouyTrhIwuxZOgfM9Kd3dRyKJqneBTZYgypqJHceDnHFuzljiNolk3Uh epyYnBxV56zKihAXapglAvZ3GQQzlffhF+BNW6xdAHorsvSbsnBd94bFifPvcEwlh4ex 14aLU0zMt52cccEs+BTAU9wnIyXQ6HHZr1TC9oJVY7CsrqHvfyEHwbNjmVIzLdfodjjD /IUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763445166; x=1764049966; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QOnUB6O9YOJFcHVG2GENeUI+86wRQEOj+D5/8znK/J4=; b=DVz2hId5QQN+DRdKQ00XYKDvEuzfUoCSqmMLAjs8jhY4dGJCjizawcORK+BJ/mXh2U PtJcj+QZ3Vf5XyKlzwFenkFTq2yUwDkH2etAqOGoRQ0L29S0yKprCEURrdnUED9lWKzY msjbZvGg2+SXjE77N3fv9ZM6iX/Pe2/7eccRiPOIqXvBBd057Q6RUwcwikxtWuofChhx 5oW+SUeiIABh7fPhscE6X3uMOqztjSEUnVrDdHh11hTu+KnAFJ/sLPz6wbUv1IZ0HzBs 1ne6bD8oxIaPjrenoBOgwvfwryBRU3TNKS/joKQZt1XmpZTu82/T7qUDeTfsErLLvi0X VOoQ== X-Gm-Message-State: AOJu0Yyl6lb91yMtwGUWF6PxNjL3coh1l7Mhq6mKsNROj9zEX/DPSoXP CtWyZUDTU+wOUq7ECk5Dw8t/5JhNYzy7YafuJLGpuwGOSQn3aRrJ9iOd8lskuHRAzEaj70Mq0jV eysMFiRn2KvL83WUzF+lurmAkFgBAwexzE6m6JWL/ieYSiJP49/csE4KwueCqmOBLgKGIwgHmr8 uWc292rvolMBtSeLZ3i7W94gdBfgZETjLqs4KFXJShSPsST343 X-Google-Smtp-Source: AGHT+IGJkTrPVtWml4AEoahA929t2267QrlAEK81s66wHJwwFayihzUWozgqao3gN7y1vQI3YWCWrqKuuJoX X-Received: from plbkt3.prod.google.com ([2002:a17:903:883:b0:298:3a03:dc2e]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:a8f:b0:295:7453:b58b with SMTP id d9443c01a7336-2986a6b56b4mr175951595ad.4.1763445165494; Mon, 17 Nov 2025 21:52:45 -0800 (PST) Date: Tue, 18 Nov 2025 05:52:23 +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.rc1.455.g30608eb744-goog Message-ID: <20251118055242.4030849-1-jstultz@google.com> Subject: [PATCH] sched: Fix psi_dequeue for Proxy Execution From: John Stultz To: LKML Cc: John Stultz , K Prateek Nayak , Joel Fernandes , Qais Yousef , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Valentin Schneider , Johannes Weiner , 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_switch_task() 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 Acked-by: Johannes Weiner --- v13: * Reworked for collision v15: * Fixed commit message typo noticed by Todd Kjos v24: * 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. 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: kernel-team@android.com --- kernel/sched/stats.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/sched/stats.h b/kernel/sched/stats.h index 26f3fd4d34cea..a38459813b537 100644 --- a/kernel/sched/stats.h +++ b/kernel/sched/stats.h @@ -180,8 +180,12 @@ 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.rc1.455.g30608eb744-goog