[PATCH] sched/fair: Fix h_nr_runnable leak in dequeue_hierarchy()

Prabhakar posted 1 patch 10 hours ago
kernel/sched/fair.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] sched/fair: Fix h_nr_runnable leak in dequeue_hierarchy()
Posted by Prabhakar 10 hours ago
From: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>

Commit 486c3d06b902 ("sched/fair: Avoid transient double accounting
for delayed tasks") changed the h_nr_runnable logic in
dequeue_hierarchy() from "set to 1 unless delayed" to "set to 0 if
delayed", but left the initialiser at 0:

	int h_nr_runnable = 0;
	...
	if (se->sched_delayed)
		h_nr_runnable = 0;

h_nr_runnable is therefore always 0, so dequeuing a task that is not
delayed no longer decrements cfs_rq->h_nr_runnable. The count only
grows, which inflates se->runnable_weight and skews PELT runnable_avg
and load balancing.

Initialise h_nr_runnable to 1 so that only delayed tasks, which
set_delayed() has already accounted for, skip the decrement.

Fixes: 486c3d06b902 ("sched/fair: Avoid transient double accounting for delayed tasks")
Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
---
 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 731d78155e65..2d9d3d686261 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -8294,7 +8294,7 @@ static void dequeue_hierarchy(struct task_struct *p, int flags)
 	struct sched_entity *se = &p->se;
 	bool task_throttled = flags & DEQUEUE_THROTTLE;
 	int h_nr_idle = task_has_idle_policy(p);
-	int h_nr_runnable = 0;
+	int h_nr_runnable = 1;
 	struct cfs_rq *cfs_rq;
 	bool dequeue = true;
 
-- 
2.43.0
Re: [PATCH] sched/fair: Fix h_nr_runnable leak in dequeue_hierarchy()
Posted by Chen Yu 2 hours ago
Hi Lad Prabhakar,
 
On Mon, Nov 23, 2026 at 12:12:13AM +0000, Prabhakar wrote:
> From: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
> 
> Commit 486c3d06b902 ("sched/fair: Avoid transient double accounting
> for delayed tasks") changed the h_nr_runnable logic in
> dequeue_hierarchy() from "set to 1 unless delayed" to "set to 0 if
> delayed", but left the initialiser at 0:
> 
> 	int h_nr_runnable = 0;
> 	...
> 	if (se->sched_delayed)
> 		h_nr_runnable = 0;
> 
> h_nr_runnable is therefore always 0, so dequeuing a task that is not
> delayed no longer decrements cfs_rq->h_nr_runnable. The count only
> grows, which inflates se->runnable_weight and skews PELT runnable_avg
> and load balancing.
> 
> Initialise h_nr_runnable to 1 so that only delayed tasks, which
> set_delayed() has already accounted for, skip the decrement.
> 
> Fixes: 486c3d06b902 ("sched/fair: Avoid transient double accounting for delayed tasks")
> Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
>

Thanks for the fix,

Tested-by: Chen Yu <yu.c.chen@intel.com>

Would you mind also add:

Reported-by: kernel test robot <yi1.lai@intel.com>
Closes: https://lore.kernel.org/oe-lkp/202611230853.e2e6401-lkp@intel.com

thanks,
Chenyu
Re: [PATCH] sched/fair: Fix h_nr_runnable leak in dequeue_hierarchy()
Posted by K Prateek Nayak 2 hours ago
Hello Prabhakar,

On 11/23/2026 5:42 AM, Prabhakar wrote:
> From: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
> 
> Commit 486c3d06b902 ("sched/fair: Avoid transient double accounting
> for delayed tasks") changed the h_nr_runnable logic in
> dequeue_hierarchy() from "set to 1 unless delayed" to "set to 0 if
> delayed", but left the initialiser at 0:
> 
> 	int h_nr_runnable = 0;
> 	...
> 	if (se->sched_delayed)
> 		h_nr_runnable = 0;
> 
> h_nr_runnable is therefore always 0, so dequeuing a task that is not
> delayed no longer decrements cfs_rq->h_nr_runnable. The count only
> grows, which inflates se->runnable_weight and skews PELT runnable_avg
> and load balancing.
> 
> Initialise h_nr_runnable to 1 so that only delayed tasks, which
> set_delayed() has already accounted for, skip the decrement.
> 
> Fixes: 486c3d06b902 ("sched/fair: Avoid transient double accounting for delayed tasks")
> Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>

Good catch! Feel free to include:

Reviewed-by: K Prateek Nayak <kprateek.nayak@amd.com>

> ---
>  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 731d78155e65..2d9d3d686261 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -8294,7 +8294,7 @@ static void dequeue_hierarchy(struct task_struct *p, int flags)
>  	struct sched_entity *se = &p->se;
>  	bool task_throttled = flags & DEQUEUE_THROTTLE;
>  	int h_nr_idle = task_has_idle_policy(p);
> -	int h_nr_runnable = 0;
> +	int h_nr_runnable = 1;
>  	struct cfs_rq *cfs_rq;
>  	bool dequeue = true;
>  

-- 
Thanks and Regards,
Prateek