From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sender4-pp-o92.zoho.com (sender4-pp-o92.zoho.com [136.143.188.92]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EFF9E32B10A for ; Tue, 7 Jul 2026 15:47:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.92 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783439270; cv=pass; b=s4B6TpqPSbth71F4eweJftMGBTvZfRDtc9H5VKSOnhemakyA52fCS4jkm5MV3mYf7a4XwpIM8ZVbW7rEu8MyDHZ1gxqNClc9Nk6MFvKitOiXuwmS2G/kh82KPo1jPunQ92MsAvGHm61f93Rvbv0kScecEy5bPCXX0iool+YDJik= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783439270; c=relaxed/simple; bh=Nh3S9eLbck9PQ/lcwRxGQRbd7K6olcKWdrft2+vr300=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=J7kUfVj9gB4FUKOf8JO/A9sp6a0Yh6T3zfsOIYqM5tkQSHqSmEXmouSoPT0ITurHqKHPebUENFWOiEOK8+zYShF+WUZQx5iCs2ddQg67R1Q5qMBqQwJuntfNJbgoeZF3+7BXmgIlN96BQ0koAu/ycY86SpsPss8Wm5a+eqWQsPU= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=zohomail.com; spf=pass smtp.mailfrom=zohomail.com; dkim=pass (1024-bit key) header.d=zohomail.com header.i=kingxukai@zohomail.com header.b=KO5ht3ZR; arc=pass smtp.client-ip=136.143.188.92 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=zohomail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zohomail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=zohomail.com header.i=kingxukai@zohomail.com header.b="KO5ht3ZR" ARC-Seal: i=1; a=rsa-sha256; t=1783439228; cv=none; d=zohomail.com; s=zohoarc; b=DE/bFpMHnOWpOrXPPRnqDXrE+Od/ni7N6E6bLOqotOgkEQG2xBAQAHGZN49hBvVCMNrAf2pPK0EKDfkcc3FGYh/ZzRiyXBLDjzfLtgJttzhhewTf+A+tsnrF+7qCZz0cHUR6KI3BblC21CEY58tpYKqqOasZPr1a6FVKL/lQGlA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783439228; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=OyaY36YRFYp+zGjKq9RMFyzWBrbWsv/JdTs+9cN2s6Y=; b=B1jeZzYKBjkvDH3/zNGv1xy/alSAu4QhY6BnMYZIwUo6ig1rvuOlOtxAdLW3igp3FpWR4Yvs5lGFR0/1LKDr9sNI2NbnTG5Y+gCU2yVc7gFqNKDrxi5LYCJaBzfKqvDlMkiVvCq4pbv8uGtYEgK4SZ0MQomJ4os4ptsFIc0hdv4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=zohomail.com; spf=pass smtp.mailfrom=kingxukai@zohomail.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1783439228; s=zm2022; d=zohomail.com; i=kingxukai@zohomail.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Feedback-ID:Reply-To; bh=OyaY36YRFYp+zGjKq9RMFyzWBrbWsv/JdTs+9cN2s6Y=; b=KO5ht3ZRNe9xOlGMMLBgXsoDduR8P4ADs0PM1VLKYoU73WeDV620k2+qiL6VBfy9 Hmtca9mUco1t9zu9PKT8eP3EFG2xpT3bfrxjM6Jy0+qRvBce7xkcdNXyDJIX5sapfLq w80SmcNP/n9djUrUbq2zIvS4IHBu1hLhm/tYu3wY= Received: by mx.zohomail.com with SMTPS id 1783439225160408.7010658485649; Tue, 7 Jul 2026 08:47:05 -0700 (PDT) From: Xukai Wang Date: Tue, 07 Jul 2026 23:46:38 +0800 Subject: [PATCH RFC] sched/proxy: Defer donor commit until after proxy resolution Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260707-sched-proxy-v1-1-5928bf6dedf0@zohomail.com> References: <20260707-sched-proxy-v1-0-5928bf6dedf0@zohomail.com> In-Reply-To: <20260707-sched-proxy-v1-0-5928bf6dedf0@zohomail.com> To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak Cc: linux-kernel@vger.kernel.org, Xukai Wang X-Mailer: b4 0.15.2 Feedback-ID: zu080112271849537e1c83303b10c3d8e300009b211bc9e0e111eb4deba7da956c582b3ddd596e0446b7e723:ZohoMail X-Zoho-CM-AccountID: 2ee5dd3c83366259b2ba1e9826250ffebed1ef2dd213857d649ad25aba73b429 X-ZohoMailClient: External pick_next_task() currently commits the selected task into the scheduling class state before proxy execution has resolved whether that task should really become the committed donor. With proxy execution enabled, the task returned by pick_next_task() may be blocked. __schedule() then calls find_proxy_task() to resolve the proxy chain. If find_proxy_task() returns NULL, __schedule() retries through pick_again, but the picked donor has already gone through put_prev_set_next_task(). In that case the class current state was speculatively switched to a donor which is not the final scheduling decision. Make pick_next_task() return only the selected candidate task. Move the put_prev_set_next_task() call into __schedule(), next to rq_set_donor(), so the class commit happens at the schedule level. For proxy execution, keep the picked donor separate from the actual task to run: donor = task selected by pick_next_task() next = task returned by find_proxy_task(), possibly the mutex owner Only commit the donor after proxy-chain resolution succeeds: put_prev_set_next_task(rq, prev_donor, donor); rq_set_donor(rq, donor); If find_proxy_task() returns NULL, the candidate donor has not been committed and __schedule() can retry without first undoing a speculative set_next() on that donor. A proxy candidate is not necessarily the committed rq->donor anymore. Update proxy_deactivate() accordingly. If the task being blocked is still the committed donor, proxy_resched_idle() is needed to drop rq/class current references before block_task() clears ->on_rq. If it is only an uncommitted proxy candidate, it is still queued and can be blocked directly. proxy_migrate_task() still switches the rq to idle before dropping the rq lock. Migrating a task found in the proxy chain abandons the current proxy pick attempt, and switching the committed donor to idle leaves rq->donor and the class current state in a defined state while the chain is modified and the task is attached elsewhere. This keeps the proxy donor accounting semantics intact: the scheduling class state is committed to the donor, while the actual context switch may still be to the proxy owner returned by find_proxy_task(). Signed-off-by: Xukai Wang --- kernel/sched/core.c | 70 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 2e7cde033a31..3c1a23a9c12c 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6146,7 +6146,6 @@ __pick_next_task(struct rq *rq, struct rq_flags *rf) if (!p) p = pick_task_idle(rq, rf); - put_prev_set_next_task(rq, rq->donor, p); return p; } @@ -6157,10 +6156,8 @@ __pick_next_task(struct rq *rq, struct rq_flags *rf) p = class->pick_task(rq, rf); if (unlikely(p == RETRY_TASK)) goto restart; - if (p) { - put_prev_set_next_task(rq, rq->donor, p); + if (p) return p; - } } BUG(); /* The idle class should always have a runnable task. */ @@ -6257,7 +6254,7 @@ pick_next_task(struct rq *rq, struct rq_flags *rf) rq->dl_server = rq->core_dl_server; rq->core_pick = NULL; rq->core_dl_server = NULL; - goto out_set_next; + goto out_return_next; } prev_balance(rq, rf); @@ -6311,7 +6308,7 @@ pick_next_task(struct rq *rq, struct rq_flags *rf) */ WARN_ON_ONCE(fi_before); task_vruntime_update(rq, next, false); - goto out_set_next; + goto out_return_next; } } @@ -6441,8 +6438,7 @@ pick_next_task(struct rq *rq, struct rq_flags *rf) resched_curr(rq_i); } -out_set_next: - put_prev_set_next_task(rq, rq->donor, next); +out_return_next: if (rq->core->core_forceidle_count && next == rq->idle) queue_core_balance(rq); @@ -6755,15 +6751,23 @@ static void proxy_deactivate(struct rq *rq, struct task_struct *donor) WARN_ON_ONCE(state == TASK_RUNNING); WARN_ON_ONCE(donor->blocked_on); /* - * Because we got donor from pick_next_task(), it is *crucial* - * that we call proxy_resched_idle() before we deactivate it. - * As once we deactivate donor, donor->on_rq is set to zero, - * which allows ttwu() to immediately try to wake the task on - * another rq. So we cannot use *any* references to donor - * after that point. So things like cfs_rq->curr or rq->donor - * need to be changed from next *before* we deactivate. + * A proxy candidate is not necessarily the committed rq->donor. + * pick_next_task() only selected it; the class current state is + * updated later, after proxy-chain resolution. + * + * If @donor is still the committed donor, the rq and the scheduling + * class may hold current references to it, such as rq->donor or + * cfs_rq->curr/h_curr. Drop those references before block_task(), + * because block_task() clears donor->on_rq and a concurrent wakeup + * may then move the task elsewhere. + * + * If @donor is only an uncommitted proxy candidate, it is still a + * queued task, not the class current task, so it can be blocked + * directly. */ - proxy_resched_idle(rq); + if (donor == rq->donor) + proxy_resched_idle(rq); + block_task(rq, donor, state); } @@ -6816,15 +6820,17 @@ static void proxy_migrate_task(struct rq *rq, struct rq_flags *rf, lockdep_assert_rq_held(rq); WARN_ON(p == rq->curr); /* - * Since we are migrating a blocked donor, it could be rq->donor, - * and we want to make sure there aren't any references from this - * rq to it before we drop the lock. This avoids another cpu - * jumping in and grabbing the rq lock and referencing rq->donor - * or cfs_rq->curr, etc after we have migrated it to another cpu, - * and before we pick_again in __schedule. + * Migrating a task found in the proxy chain abandons the current proxy + * pick attempt and drops this rq's lock. + * + * The picked proxy candidate has not necessarily been committed, so + * @p is not necessarily rq->donor. Switch the currently committed + * donor to idle before dropping the lock, leaving rq->donor and the + * class current state in a well-defined state while the chain is + * modified and @p is attached elsewhere. * - * So call proxy_resched_idle() to drop the rq->donor references - * before we release the lock. + * If @p is rq->donor, this also drops the direct rq/class-current + * references to @p before it leaves this rq. */ proxy_resched_idle(rq); @@ -7147,11 +7153,11 @@ static void __sched notrace __schedule(int sched_mode) rq->next_class = next->sched_class; if (sched_proxy_exec()) { struct task_struct *prev_donor = rq->donor; + struct task_struct *donor = next; - rq_set_donor(rq, next); - next->blocked_donor = NULL; - if (unlikely(next->is_blocked)) { - next = find_proxy_task(rq, next, &rf); + donor->blocked_donor = NULL; + if (unlikely(donor->is_blocked)) { + next = find_proxy_task(rq, donor, &rf); if (!next) { zap_balance_callbacks(rq); goto pick_again; @@ -7161,8 +7167,11 @@ static void __sched notrace __schedule(int sched_mode) goto keep_resched; } } - if (rq->donor == prev_donor && prev != next) { - struct task_struct *donor = rq->donor; + + put_prev_set_next_task(rq, prev_donor, donor); + rq_set_donor(rq, donor); + + if (donor == prev_donor && prev != next) { /* * When transitioning like: * @@ -7179,6 +7188,7 @@ static void __sched notrace __schedule(int sched_mode) donor->sched_class->set_next_task(rq, donor, true); } } else { + put_prev_set_next_task(rq, rq->donor, next); rq_set_donor(rq, next); } -- 2.34.1