From nobody Tue Dec 2 01:04:49 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 52C432D062A for ; Fri, 21 Nov 2025 19:01:48 +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=1763751712; cv=none; b=suP/44lr/HABl09PndNbHPwGhnAKBrJX9oojOfbDddY+APdJVEi64ak/rETncxQFsNNc8rFUuYJ1yxV5f8nYPwuswtMfRTfBgN6zKceyv1D21nTuN5FZqphOJ4pjjdSj+lrFIVPxrzTKiF/RcHBYgSAiEAo9xEW0yfLWppKq4Hg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763751712; c=relaxed/simple; bh=78KZ/c4wgHXmQeGbNUWS4G4uY5tusxinQh09S7pvyA4=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=I/aJRAzBZWI6IwvSCEFMA1u245J67vXl7UHjOkdha0Dy0GP46Sgmwf4SQKNvIO0CZP2M1ZmWMmp7SFzB792AbHFQ+IKK+yOHAuRXCj51a+ZXy7nvTk1diCaUunUoKl3IypmsRzI/B909mHghaimO/ByXYtD0g+KR+eLKUV1r4KU= 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=wyHeWEy4; 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="wyHeWEy4" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34377900dbcso5442393a91.2 for ; Fri, 21 Nov 2025 11:01:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763751707; x=1764356507; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=NNfY0cGXnA+B+i6oHk/Z0RldeymrIx4uaaf3isYsSu8=; b=wyHeWEy4lXcqf+NcM9msW3RZjRV0kZtICtthS/Or1a6tohLK9MQc4Qt4CAePkBriz4 5RLNFbKfCI1PDlwnaS1m1ZJH6oCe9vglhPin2NRp4KMWPDNcDnTbrtoaQd7vdbTriu5I ACMdM6P29HXb2a5DdsHoVLz6iZ+hxhjt3kzZCcg9Hqo39QeQRczQzZ9BdOl9+JirJj7k DeQPsEnRff8qIWrn8jy5cZVQ2ilruTFvQAHw31trHCtzXtL3eFLUBlcszrwUd+fLLG/f 4HfnZ1ZlkvTdfn9S4uYMVm3oCIU9wlv4LyhBPVacHWUiQERvJgqLA8R7nWaiKwK0Q6DD dTqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763751707; x=1764356507; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=NNfY0cGXnA+B+i6oHk/Z0RldeymrIx4uaaf3isYsSu8=; b=a1KAyztXet5OVXmpJGSVfg+3uUdj8NMXH2LoN9XRBzNnvqQzki/RLF0pBH5w1dhGCB j5s+y6sN69S5fHzfXTmn3nsaGuu2YwUuM6dHxwwehtWu6Jq5kOPHNpbFH7uQf5SAguVl S1PcNGZ8/i9IPZd+n8/WDY+jqgt1KSFJdJn7RwYfaBaoR/3mTUlAKbRsr36vOdPPOPst OM/j5lohohb4LCSQ6nFEc3a/L6YZJpKiywGm7E+HLWQYrmlyHbfJv2DpHufj4H91ShnW 6ncbdg8zkgWMcfpzAkRcXcbavzrA7LX84KaLolOerJBXC/iDdFUKE5w9k5st0ljUPGHs LYrA== X-Gm-Message-State: AOJu0Yw3VRPmM4Us3gru4wuep11pFJXWhyBk2SoF5pD8vd3920lNk9HC xZ9GNdx1fxKZL6/yCyHXMhZhp19YGSC6T+ZBDCt5gJZukYVA/QowG5KgmXtOm2Xioot6LTAe4rP T/pD0VDbz2RU5W6rXnU4M5zfvSsHxlip3d271AihivJ2EKHLhft7bmMlL5krCEu6ND3xhRSo1GP DExRD7ssuw4EOOsE3XbOFyv6JIeYXxU7qRfWaaDsty5VRZtYk/ X-Google-Smtp-Source: AGHT+IHP0fbbMljzweTxzbPL9mb144q/z3vuNtTyI2Drwy1nfNBPU1DTLAaV+pog9BIAPDf4KSiCBJxZiY/H X-Received: from pjbep18.prod.google.com ([2002:a17:90a:e652:b0:341:adc3:c051]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d606:b0:343:b610:901c with SMTP id 98e67ed59e1d1-34733f3e4ddmr3893863a91.26.1763751707428; Fri, 21 Nov 2025 11:01:47 -0800 (PST) Date: Fri, 21 Nov 2025 19:01:35 +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.rc2.455.g230fcf2819-goog Message-ID: <20251121190144.845002-1-jstultz@google.com> Subject: [PATCH v2] sched: Fix psi_dequeue for Proxy Execution From: John Stultz To: LKML Cc: John Stultz , K Prateek Nayak , 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_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 Tested-by: Haiyue Wang --- 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 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 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/sched/stats.h b/kernel/sched/stats.h index 26f3fd4d34cea..73bd6bca4d310 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.rc2.455.g230fcf2819-goog