From nobody Tue Apr 7 14:19:03 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 40AE0340A5A for ; Fri, 13 Mar 2026 02:30:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773369040; cv=none; b=i/St5w0Pg5uQFa3ZPAEJwT+Mii/+2js/S+l/japJiYtlY7/f4ARFtm0xnz6qqaOpjDeSV8q0CNKBuLlZFzIUJ8OYs7qR9oIRtwePPyV3GaZvmCb/2OE18bYLsbz1thtm3dmsKwbKCVHeF+vbunsMiH5qjhJY/V7H+Qlb+pxt/PA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773369040; c=relaxed/simple; bh=pBvhCAtlY3dusekPnSl2YbU6yRgklqK/eM2/uf9N5tU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=f69MPu2FXxM7ckv8ezYSQZa2haitCEzTb9HjdN/QC8cXV8QF7O1aADkA3IGNhuvGIN15LVfmI21S3hFa8ZTaWZUTnoptEMN1Yt/D4/xnl/D5spos64E7qSOVyWDIzrbeAZpTRCUxmWHJPCgy35qFz7CmGrnQbAXUOThHGOogwBU= 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=BIC9yKl0; arc=none smtp.client-ip=209.85.214.202 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="BIC9yKl0" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2ae59e057f1so22121825ad.1 for ; Thu, 12 Mar 2026 19:30:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773369038; x=1773973838; 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=eMzlxle0S33TV4ZlgehDuBu1TCqN9OLsn14Om1T/gko=; b=BIC9yKl0o46q10P/1CGDmz8InD8MOI7fwd+3KA+GT6tlclpS2kYAdu/QOQ+iMaQgtj DF/a8WydUzisTnNoUb7r0dUAkffXWpjPfZ4OqZRst79rc/G3gjbbMEz3oORt5ivGfKcf HVjkVetzmp1W4ggzJbEpeT9rwTYMx2TDvG2+A0rUUgxTcNVgkGUiGUAH8HMI26D+Mvgs yri6xnOEM/T/0gMiBzMLoP4EFQWVJCnSBDB8Wd8GHsB30xYopiWswKYCmSAgpnfUFFC7 0KpExiv3sLxZ+xHTQkH2phW6BfHrBmw/Tn8eBSq4CqwQMSZeh9zl+zHIxF2bxHWpZVAi 0Fbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773369038; x=1773973838; 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=eMzlxle0S33TV4ZlgehDuBu1TCqN9OLsn14Om1T/gko=; b=k9IWu6vQZe7boxKI0nTlV/OqYOGtYTDriWmVnzj5ctxZVFEZqrzgIT1HHMC3UxPDK6 OO+fwDPHw503+KvU/g8olopXlE7zPNQBzCXHNwAP7ZinpMPsUkVL84COfM4DyYAyirpM K9ZMu7VzW5I9UNIT0WC1gTRuyYZjXgKAqpS0H+YF4UEsuOb73ZnNyeP3o91nir7yP5da SPEQtdxjSWxrBMIoUo5BOK+jspVoY5MZNCCLAaMVDulCokgUuzrr2Clug5zqzycJlkxi +wybla/c01Rf/D6brn85tPtbFCGX6n8CrGWVdAnB0F1WAO+GfXO2w2vwDLxH/crKyBS9 ICVQ== X-Gm-Message-State: AOJu0Yw+GTCtwSSdwy0lKoXlyPxjOCMpbAVa+cm7UtQUhi5a82AgLRBI GYYMcOcKxug9kcGyV8sxNAyUP5aeOHjLXtq2woEd39FFWzCfuKCRSdxAtOvrkIfix4Qx09zmP2H bft/0+YW69b4wfIiHyvrOgZ9nJjiq/5BswB7gws0ALE3dsRzsWPI0EdGX+oroGkgM45t3/s42UE /cB0WRw1EbkkiXcIFPks4RdhFnNN2hBTRmIrpBBoT/yWdVaGgs X-Received: from plmo20.prod.google.com ([2002:a17:903:18d4:b0:2ae:c8f6:b8ac]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2ec6:b0:2ae:c9be:5f2c with SMTP id d9443c01a7336-2aecaa15da9mr14642505ad.21.1773369038196; Thu, 12 Mar 2026 19:30:38 -0700 (PDT) Date: Fri, 13 Mar 2026 02:30:08 +0000 In-Reply-To: <20260313023022.2902479-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: <20260313023022.2902479-1-jstultz@google.com> X-Mailer: git-send-email 2.53.0.880.g73c4285caa-goog Message-ID: <20260313023022.2902479-8-jstultz@google.com> Subject: [PATCH v25 7/9] sched: Add logic to zap balance callbacks if we pick again 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 , 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" With proxy-exec, a task is selected to run via pick_next_task(), and then if it is a mutex blocked task, we call find_proxy_task() to find a runnable owner. If the runnable owner is on another cpu, we will need to migrate the selected donor task away, after which we will pick_again can call pick_next_task() to choose something else. However, in the first call to pick_next_task(), we may have had a balance_callback setup by the class scheduler. After we pick again, its possible pick_next_task_fair() will be called which calls sched_balance_newidle() and sched_balance_rq(). This will throw a warning: [ 8.796467] rq->balance_callback && rq->balance_callback !=3D &balance_p= ush_callback [ 8.796467] WARNING: CPU: 32 PID: 458 at kernel/sched/sched.h:1750 sched= _balance_rq+0xe92/0x1250 ... [ 8.796467] Call Trace: [ 8.796467] [ 8.796467] ? __warn.cold+0xb2/0x14e [ 8.796467] ? sched_balance_rq+0xe92/0x1250 [ 8.796467] ? report_bug+0x107/0x1a0 [ 8.796467] ? handle_bug+0x54/0x90 [ 8.796467] ? exc_invalid_op+0x17/0x70 [ 8.796467] ? asm_exc_invalid_op+0x1a/0x20 [ 8.796467] ? sched_balance_rq+0xe92/0x1250 [ 8.796467] sched_balance_newidle+0x295/0x820 [ 8.796467] pick_next_task_fair+0x51/0x3f0 [ 8.796467] __schedule+0x23a/0x14b0 [ 8.796467] ? lock_release+0x16d/0x2e0 [ 8.796467] schedule+0x3d/0x150 [ 8.796467] worker_thread+0xb5/0x350 [ 8.796467] ? __pfx_worker_thread+0x10/0x10 [ 8.796467] kthread+0xee/0x120 [ 8.796467] ? __pfx_kthread+0x10/0x10 [ 8.796467] ret_from_fork+0x31/0x50 [ 8.796467] ? __pfx_kthread+0x10/0x10 [ 8.796467] ret_from_fork_asm+0x1a/0x30 [ 8.796467] This is because if a RT task was originally picked, it will setup the rq->balance_callback with push_rt_tasks() via set_next_task_rt(). Once the task is migrated away and we pick again, we haven't processed any balance callbacks, so rq->balance_callback is not in the same state as it was the first time pick_next_task was called. To handle this, add a zap_balance_callbacks() helper function which cleans up the balance callbacks without running them. This should be ok, as we are effectively undoing the state set in the first call to pick_next_task(), and when we pick again, the new callback can be configured for the donor task actually selected. Reviewed-by: K Prateek Nayak Signed-off-by: John Stultz --- v20: * Tweaked to avoid build issues with different configs v22: * Spelling fix suggested by K Prateek * Collapsed the stub implementation to one line as suggested by K Prateek * Zap callbacks when we resched idle, as suggested by K Prateek v24: * Don't conditionalize function on CONFIG_SCHED_PROXY_EXEC as the callers will be optimized out if that is unset, and the dead function will be removed, as suggsted by K Prateek 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 | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index ec9e8fe39f9fc..af497b8c72dce 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4917,6 +4917,34 @@ static inline void finish_task(struct task_struct *p= rev) smp_store_release(&prev->on_cpu, 0); } =20 +/* + * Only called from __schedule context + * + * There are some cases where we are going to re-do the action + * that added the balance callbacks. We may not be in a state + * where we can run them, so just zap them so they can be + * properly re-added on the next time around. This is similar + * handling to running the callbacks, except we just don't call + * them. + */ +static void zap_balance_callbacks(struct rq *rq) +{ + struct balance_callback *next, *head; + bool found =3D false; + + lockdep_assert_rq_held(rq); + + head =3D rq->balance_callback; + while (head) { + if (head =3D=3D &balance_push_callback) + found =3D true; + next =3D head->next; + head->next =3D NULL; + head =3D next; + } + rq->balance_callback =3D found ? &balance_push_callback : NULL; +} + static void do_balance_callbacks(struct rq *rq, struct balance_callback *h= ead) { void (*func)(struct rq *rq); @@ -6860,10 +6888,14 @@ static void __sched notrace __schedule(int sched_mo= de) if (sched_proxy_exec()) { if (unlikely(next->blocked_on)) { next =3D find_proxy_task(rq, next, &rf); - if (!next) + if (!next) { + zap_balance_callbacks(rq); goto pick_again; - if (next =3D=3D rq->idle) + } + if (next =3D=3D rq->idle) { + zap_balance_callbacks(rq); goto keep_resched; + } } } picked: --=20 2.53.0.880.g73c4285caa-goog