From nobody Sun Feb 8 15:01:44 2026 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) (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 C28961F460B for ; Mon, 3 Mar 2025 10:53:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740999215; cv=none; b=UPQsPw1spyF2MxEepMMJOuSYX/MMlowsHbayJkvc62pLo+TiPVtPPCwXr2TOcDq2rSVkUqfRYwkel6gt65DI4oNHdHFsIOADhl8gBQKFd4d5xqBp7srWxfFCDASwySVwgkWlIzdtRozLV1xRnBfeKfN+wCwztQsO/OhjVSxzxxw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740999215; c=relaxed/simple; bh=NA0aZjnv29CzK7eDSeZL4KPyo9JgFZx3N5dSEtDTILA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=n9JmqXZrJICxO+VppqxpyHnpmxtY699KBcemcRpLeZ7sKfN6nLxz9BrNmPPmAh+AXr2lzTiVs95rK1ztwa281M/3Z9Q7XQVMDG0CDgCIlCrkAEib5d3n577zMFweMw0libghBLk6NJOgyoXhiQxakPGKFZqa/DzAGMTUjOAFHMM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 523ArJRx092018; Mon, 3 Mar 2025 18:53:19 +0800 (+08) (envelope-from Xuewen.Yan@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4Z5wXL58vBz2K8nM6; Mon, 3 Mar 2025 18:48:34 +0800 (CST) Received: from BJ10918NBW01.spreadtrum.com (10.0.73.73) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Mon, 3 Mar 2025 18:53:17 +0800 From: Xuewen Yan To: , , , , , , , , CC: , , , Subject: [RFC PATCH V2 1/3] sched/fair: Fixup-wake_up_sync-vs-DELAYED_DEQUEUE Date: Mon, 3 Mar 2025 18:52:39 +0800 Message-ID: <20250303105241.17251-2-xuewen.yan@unisoc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250303105241.17251-1-xuewen.yan@unisoc.com> References: <20250303105241.17251-1-xuewen.yan@unisoc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL: SHSQR01.spreadtrum.com 523ArJRx092018 Content-Type: text/plain; charset="utf-8" Delayed dequeued feature keeps a sleeping task enqueued until its lag has elapsed. As a result, it stays also visible in rq->nr_running. So when in wake_affine_idle(), we should use the real running-tasks in rq to check whether we should place the wake-up task to current cpu. On the other hand, add a helper function to return the nr-delayed. Fixes: 152e11f6df29 ("sched/fair: Implement delayed dequeue") Signed-off-by: Xuewen Yan Reviewed-by: Vincent Guittot --- V2: - add helper function (Vincent) --- kernel/sched/fair.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 1c0ef435a7aa..a354f29c4f6f 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -7181,6 +7181,11 @@ static bool dequeue_task_fair(struct rq *rq, struct = task_struct *p, int flags) return true; } =20 +static inline unsigned int cfs_h_nr_delayed(struct rq *rq) +{ + return (rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable); +} + #ifdef CONFIG_SMP =20 /* Working cpumask for: sched_balance_rq(), sched_balance_newidle(). */ @@ -7342,8 +7347,12 @@ wake_affine_idle(int this_cpu, int prev_cpu, int syn= c) if (available_idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu)) return available_idle_cpu(prev_cpu) ? prev_cpu : this_cpu; =20 - if (sync && cpu_rq(this_cpu)->nr_running =3D=3D 1) - return this_cpu; + if (sync) { + struct rq *rq =3D cpu_rq(this_cpu); + + if ((rq->nr_running - cfs_h_nr_delayed(rq)) =3D=3D 1) + return this_cpu; + } =20 if (available_idle_cpu(prev_cpu)) return prev_cpu; --=20 2.25.1 From nobody Sun Feb 8 15:01:44 2026 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) (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 82B8E1F30B3 for ; Mon, 3 Mar 2025 10:53:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740999222; cv=none; b=uT5ze9QnTU2Nx5MjN/wXjImae68JhvNlVx/8+TK4Uf0tMFvIl2tKZOpehGUQWe36dsc9HcoV4dQPDXXqK3QLDKudbta9sscL34CegTQvxNfeL93BPLrfgmL8gPeJU0wpqVJGRp2TuxR/b9wR05ScdJiJNha7HQ5aUJ4NpQq4tNs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740999222; c=relaxed/simple; bh=IxLafNvvFttrmOiNrSQRs+lSseO4cQaIwe4/IL5N2Gk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LcOSPehZwhWR92LWY0r5QH4TOv1V2GALCEwERVL5cOz+MLcuqeqmS/sC5J3DCfxuUeOr5uu2PYwB0YDjjvwBZ4KXnbtDJ4piTd04zogqEmsozbpnacPQnox02rVs0ffztay3T3VvkESKN7olBwsgPYXU+ZaSvOm1R/lwvAbTxJU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 523ArPdi092378; Mon, 3 Mar 2025 18:53:25 +0800 (+08) (envelope-from Xuewen.Yan@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4Z5wXR4r0zz2K8nM6; Mon, 3 Mar 2025 18:48:39 +0800 (CST) Received: from BJ10918NBW01.spreadtrum.com (10.0.73.73) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Mon, 3 Mar 2025 18:53:22 +0800 From: Xuewen Yan To: , , , , , , , , CC: , , , Subject: [RFC PATCH V2 2/3] sched/fair: Do not consider the sched-delayed task when yield Date: Mon, 3 Mar 2025 18:52:40 +0800 Message-ID: <20250303105241.17251-3-xuewen.yan@unisoc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250303105241.17251-1-xuewen.yan@unisoc.com> References: <20250303105241.17251-1-xuewen.yan@unisoc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL: SHSQR01.spreadtrum.com 523ArPdi092378 Content-Type: text/plain; charset="utf-8" When task call sched_yield, if there is only one task in rq, it is no need to yield, however now, the rq->nr_running include the sched-delayed tasks which are indeed not runnable tasks. So sub the sched-delayed tasks when check the nr-running. Fixes: 152e11f6df29 ("sched/fair: Implement delayed dequeue") Signed-off-by: Xuewen Yan --- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index a354f29c4f6f..8797f6872155 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -8992,7 +8992,7 @@ static void yield_task_fair(struct rq *rq) /* * Are we the only task in the tree? */ - if (unlikely(rq->nr_running =3D=3D 1)) + if (unlikely((rq->nr_running - cfs_h_nr_delayed(rq)) =3D=3D 1)) return; =20 clear_buddies(cfs_rq, se); --=20 2.25.1 From nobody Sun Feb 8 15:01:44 2026 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) (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 754FD1F8AE2 for ; Mon, 3 Mar 2025 10:53:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740999227; cv=none; b=Q6oKQ6SqtxmbSPnXAuU9CS26Of00ogxwWturH9VDXBlMeMI3dO/bqhP/1sbhlA6J5op4vAskqj3s1OVlc3mCM0s4aCI2dU2iwcr335d2XnTmAbJLd/d1JXNV/rjZlKJ+kDN7kQP9pHgShY6KeoGCJWPphnswVsAjtLZ3zCaxP2Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740999227; c=relaxed/simple; bh=ajaYSo1Ace+j6quvrpngCRHOZEge52pf4Olu9YPWJIQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=oa/x5V+ESZAFdFrj5kB2BSlqztSc0F8ecr3XYrhzt5qTUQWJUoNOkEDDQ99UriTFLlP9EYeHYAOw9ybmiB2qavQan738+Eg/V2fhdBUJs4/d6ny/b0OpKj5hrtREupPi+cfnREYQYo7dN/pEWAc3atqEMsL+SoWiCV3IqxBkDyg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 523ArVvd092780; Mon, 3 Mar 2025 18:53:31 +0800 (+08) (envelope-from Xuewen.Yan@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4Z5wXZ23DHz2K8nM6; Mon, 3 Mar 2025 18:48:46 +0800 (CST) Received: from BJ10918NBW01.spreadtrum.com (10.0.73.73) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Mon, 3 Mar 2025 18:53:29 +0800 From: Xuewen Yan To: , , , , , , , , CC: , , , Subject: [RFC PATCH V2 3/3] sched: Do not consider the delayed task when cpu is about to enter idle Date: Mon, 3 Mar 2025 18:52:41 +0800 Message-ID: <20250303105241.17251-4-xuewen.yan@unisoc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250303105241.17251-1-xuewen.yan@unisoc.com> References: <20250303105241.17251-1-xuewen.yan@unisoc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL: SHSQR01.spreadtrum.com 523ArVvd092780 Content-Type: text/plain; charset="utf-8" When there are one task with sched-delayed and one task which is descheduling, Using nr-running to determine CPU idle may be incorrect. For example: task-A is sched_delayed, task-B is descheduling: 1. before schedule(): rq-nr-running=3D2, task-A->on_rq=3D1; task-B->on_rq=3D1; 2. after block_task(B): rq-nr-running=3D1, task-A->on_rq=3D1; task-B->on_rq=3D0; 3. after pick_next_task(), because the task-A would be dequeued: rq-nr-running=3D0, task-A->on_rq=3D0; task-B->on_rq=3D0; In ttwu_queue_cond, it hope the nr-running to be 0 after the step 2, however, now the nr-running is not 0. So sub the nr-delayed-task when checking the rq-nr-running. Fixes: 152e11f6df29 ("sched/fair: Implement delayed dequeue") Signed-off-by: Xuewen Yan --- kernel/sched/core.c | 2 +- kernel/sched/fair.c | 5 ----- kernel/sched/sched.h | 5 +++++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 67189907214d..6569f220c2fb 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3963,7 +3963,7 @@ static inline bool ttwu_queue_cond(struct task_struct= *p, int cpu) * p->on_cpu can be whatever, we've done the dequeue, so * the wakee has been accounted out of ->nr_running. */ - if (!cpu_rq(cpu)->nr_running) + if (!(cpu_rq(cpu)->nr_running - cfs_h_nr_delayed(cpu_rq(cpu)))) return true; =20 return false; diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 8797f6872155..29ee1ce17036 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -7181,11 +7181,6 @@ static bool dequeue_task_fair(struct rq *rq, struct = task_struct *p, int flags) return true; } =20 -static inline unsigned int cfs_h_nr_delayed(struct rq *rq) -{ - return (rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable); -} - #ifdef CONFIG_SMP =20 /* Working cpumask for: sched_balance_rq(), sched_balance_newidle(). */ diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index c8512a9fb022..3996b0c5c332 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3609,6 +3609,11 @@ static inline bool is_per_cpu_kthread(struct task_st= ruct *p) } #endif =20 +static inline unsigned int cfs_h_nr_delayed(struct rq *rq) +{ + return (rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable); +} + extern void swake_up_all_locked(struct swait_queue_head *q); extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_qu= eue *wait); =20 --=20 2.25.1