From nobody Fri Apr 3 10:17:06 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 CBA2C3C061A for ; Tue, 24 Mar 2026 19:13:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774379635; cv=none; b=uWny44JKBt/hEHtJBR2dscyaCNrsK4fr7xQayt1y7EI9liCxXbGH5NKiykE8qJvjP4ysBzODZrcMhnXzyMDm3ur9xUcwC/LpVm5ForNrODYKJiYuNzmbUcWjJRXb2mhsUdW3SGHI2TxhiI89HfdXF4cXrw7DM1K8iJzzqAqL1Fs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774379635; c=relaxed/simple; bh=5vWmbZ72LnrTfT8IJ/kbRaBRf3CyXBDSIU9uSpfjdhg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TF0VmHjfSQK9dChgDN4sTCqpoTntBxSbWxXzeAYI7LYA1nr+cmI4Ynh+7kPOLkQFbynF2PTBicCuCIihI+zih1OUQwSINrv8S33+4rlHIL+AxlKjcpWq83YRvIDgZCSSed/uqzn3Yp8O/7nIChXHTw2uVMqSI2WdZhJ8ya3enKo= 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=TaEbvhdS; arc=none smtp.client-ip=209.85.215.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="TaEbvhdS" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-c73781252edso23241266a12.0 for ; Tue, 24 Mar 2026 12:13:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774379633; x=1774984433; 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=Nvn72xE70j3mk1nVv0Rrlv//tMOmDYSjJGcB5sLcQ+E=; b=TaEbvhdSf0B5FiEl6ui1H2uxRlCxspidI+q1sBoZ2tazLqxCV5xTCJ3/hK0q7ndFsy AMOrNP9u9M125gYci9Aivx7ITJuJeojfboKnCIyAvT69YUeVpmFsaxo24gvZmSID4wVs O8eYRrzvfCZ/t5TnyB87sucWG/Jj+MgnoEHhon3qE7yUhfaSms5TINx/VRRwUE2NMn6I HHnN/uWLMFFq/OfvNURaVA5eaoXUsdIDNDJOsStHDOyGzvglpSmhX8vOihXlRGQORwKh xXnbwf+OjvfCJBt2fbtNES2v2KJaCl6b9XdaMUgPheMdkyBtPlVHFwfSS4UKsZ0WDfJn w4Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774379633; x=1774984433; 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=Nvn72xE70j3mk1nVv0Rrlv//tMOmDYSjJGcB5sLcQ+E=; b=QmL/OudS7HTLJ3nN1z69D9ri9yXfD3S7EsKxboED4tbYeoczwEjCF/bbH1WUyBXnUY RSiahXGA5fFF/OUie9buuqI3QMg9kZsoK1R8ZdZHf/y1vsYvo1ez0fkSNqMsIGlTBUjU SdaiHfOsj6cv2iDhnxa4qwY6TAeKxdGk+glglIlSahscP3+PP5UagCvx5KUcGKLeHjzu iCfcA7P3z/AKoi64TjB9wUoESgEmtLbXP7qr+WXplBN8pO6yi13Y7Byvsj76kwk0E8eJ N9U/N5CcfCtiK2kbu/CK5OXIB1Q26PedPd69oZqI7eWbLKA4iI+UDrehMRI6T0UnauYO M3fg== X-Gm-Message-State: AOJu0Yx4yHdf/DlxoIJ0XbwMkgY8CNVd8ALP0IcPdofjh8bB23701hRQ huBPQC6e0daHI2MJsPGzvQqpMctOO1qlNGQy71oRIHCrky1PXu9l6EKtUtOdeBk1Q3gpPTk4L77 AHEDDzt3+NO+ybsPd1VkFRQxgtuTrgh1JAEZI85s31Py4kzVVol+kOJ5DjyurKZ2iFnBhUCm8BD ZSk/lrj3cdwpQvg1dOA38qrHSDBLg4bBmyGIm1V2DLDC/0/b/p X-Received: from pfbbw25.prod.google.com ([2002:a05:6a00:4099:b0:824:9ab3:ebe8]) (user=jstultz job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:88d3:0:b0:82a:1044:3563 with SMTP id d2e1a72fcca58-82c6df392fcmr732502b3a.23.1774379632885; Tue, 24 Mar 2026 12:13:52 -0700 (PDT) Date: Tue, 24 Mar 2026 19:13:23 +0000 In-Reply-To: <20260324191337.1841376-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: <20260324191337.1841376-1-jstultz@google.com> X-Mailer: git-send-email 2.53.0.1018.g2bb0e51243-goog Message-ID: <20260324191337.1841376-9-jstultz@google.com> Subject: [PATCH v26 08/10] 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 b316b6015ffea..4ed24ef590f73 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4920,6 +4920,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); @@ -6865,10 +6893,14 @@ static void __sched notrace __schedule(int sched_mo= de) rq_set_donor(rq, next); 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; + } } if (rq->donor =3D=3D prev_donor && prev !=3D next) { struct task_struct *donor =3D rq->donor; --=20 2.53.0.1018.g2bb0e51243-goog