All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH] sched/fair: Fix overflow in vruntime_eligible() causing NULL return
@ 2025-08-08  9:21 quzicheng
  0 siblings, 0 replies; 7+ messages in thread
From: quzicheng @ 2025-08-08  9:21 UTC (permalink / raw)
  To: Fang Xiang
  Cc: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com,
	vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
	rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
	vschneid@redhat.com, linux-kernel@vger.kernel.org, tanghui (C),
	Zhangqiao (2012 lab), Chenhui (Judy)

> May I ask which specific version of the 6.6 LTS branch are you using?
Hi, based on 6.6.31

^ permalink raw reply	[flat|nested] 7+ messages in thread
* [PATCH] sched/fair: Fix overflow in vruntime_eligible() causing NULL return
@ 2026-01-06 11:34 Fang Xiang
  0 siblings, 0 replies; 7+ messages in thread
From: Fang Xiang @ 2026-01-06 11:34 UTC (permalink / raw)
  To: mingo, peterz, vincent.guittot, linux-kernel; +Cc: dietmar.eggemann, fangxiang3

In the vruntime_eligible() function, the original comparison:
	return avg >= (s64)(vruntime - cfs_rq->min_vruntime) * load;
could produce incorrect results due to integer overflow in the 'avg' or
s64 part.

This overflow causes the comparison to return false even when the
mathematical result should be true, leading all tasks to be falsely
deemed ineligible. Consequently, pick_eevdf() returns NULL, triggering a
kernel crash. This issue was discovered on kernel 6.6.76, it may be incorrect
to simply spot a potentially overflowing integer type and directly use
the >= sign for comparison.

The following are the relevant attributes that cause the return of NULL:
cfs_rq:
my_q = 0xFFFFFF8019BF2600 -> (
      load = (weight = 3609742336, inv_weight = 0),
      nr_running = 647,
      h_nr_running = 647,
      idle_nr_running = 0,
      idle_h_nr_running = 0,
      avg_vruntime = -13338730231239499,
      avg_load = 3525139,
      exec_clock = 598416298647,
      min_vruntime = 1918334512120,
      tasks_timeline = (
        rb_root = (rb_node = 0xFFFFFF811B63A850),
        rb_leftmost = 0xFFFFFF810C5B2850),
      )
)

root node:
 (struct sched_entity *)(0xFFFFFF811B63A850- 0x10) = 0xFFFFFF811B63A840 -> (
    load = (weight = 2561024, inv_weight = 1717300),
    run_node = (__rb_parent_color = 1, rb_right = 0xFFFFFF8054864FD0, rb_left = 0xFFFFFF8104916390),
    deadline = 1918337366679,
    min_vruntime = 18446741380279378713,
    on_rq = 1,
    sum_exec_runtime = 349968442385,
    prev_sum_exec_runtime = 349967120379,
    vruntime = 1918336679648,
 )

leftmost node:
(struct sched_entity *)(0xFFFFFF810C5B2850- 0x10) = 0xFFFFFF810C5B2840 -> (
    load = (weight = 1048576, inv_weight = 4194304),
    deadline = 18446741380282378713,
    min_vruntime = 18446741380279378713,
    on_rq = 1,
    sum_exec_runtime = 17431358323,
    prev_sum_exec_runtime = 307584686,
    vruntime = 18446741380279378713,
    vlag = 4607980805462,
 )

Signed-off-by: Fang Xiang <fangxiang3@xiaomi.com>
---
 kernel/sched/fair.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 726fa69c4d88..f597f8869b37 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -746,6 +746,8 @@ int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se)
 	struct sched_entity *curr = cfs_rq->curr;
 	s64 avg = cfs_rq->avg_vruntime;
 	long load = cfs_rq->avg_load;
+	s64 a = entity_key(cfs_rq, se);
+	s64 b, d;
 
 	if (curr && curr->on_rq) {
 		unsigned long weight = scale_load_down(curr->load.weight);
@@ -754,7 +756,11 @@ int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se)
 		load += weight;
 	}
 
-	return avg >= entity_key(cfs_rq, se) * load;
+	b = (s64)load;
+	if (check_mul_overflow(a, b, &d))
+		return 1;
+
+	return avg - d >= 0;
 }
 
 static u64 __update_min_vruntime(struct cfs_rq *cfs_rq, u64 vruntime)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread
* [PATCH] sched/fair: Fix overflow in vruntime_eligible() causing NULL return
@ 2025-07-09  9:38 Zicheng Qu
  2025-07-09 11:53 ` Peter Zijlstra
  2025-08-07  3:06 ` Fang Xiang
  0 siblings, 2 replies; 7+ messages in thread
From: Zicheng Qu @ 2025-07-09  9:38 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli, vincent.guittot, dietmar.eggemann,
	rostedt, bsegall, mgorman, vschneid, linux-kernel
  Cc: tanghui20, zhangqiao22, judy.chenhui, quzicheng

In the vruntime_eligible() function, the original comparison:
	return avg >= (s64)(vruntime - cfs_rq->min_vruntime) * load;
could produce incorrect results due to integer overflow in the 'avg' or
s64 part.

This overflow causes the comparison to return false even when the
mathematical result should be true, leading all tasks to be falsely
deemed ineligible. Consequently, pick_eevdf() returns NULL, triggering a
kernel crash.

The best approach should be to dig deep into why overflow occurs, which
attributes lead to the overflow, whether it is normal, and how to avoid
it. Not pretty sure if this part of the modification will introduce new
issues, but it may be incorrect to simply spot a potentially
overflowing integer type and directly use the >= sign for comparison.
Therefore, drawing on the enqueue method in the rb tree, to avoid the
impact of overflow, the method of first performing subtraction and then
comparing with 0 is also adopted.

The following are the relevant attributes that cause the return of NULL:
crash> struct cfs_rq.avg_vruntime,avg_load,min_vruntime
ffff9ea77ecb4280
  avg_vruntime = -4392414779907141680,
  avg_load = 28644,
  min_vruntime = 239776551994501,

sched_entity: curr
crash> struct sched_entity.deadline,min_vruntime,vruntime,load,vlag
0xffff9ea74510d800
  deadline = 86432035728535,
  min_vruntime = 86431486021988,
  vruntime = 86431531134184,
  load = {
    weight = 6066,
    inv_weight = 0
  },
  vlag = 86431823023195,

the sched_entity mapping to the root node in the cfs_rq
crash> struct sched_entity.deadline,min_vruntime,vruntime,load,vlag -l
sched_entity.run_node 0xffff9ea849fbc350
  deadline = 18446615868453340281,
  min_vruntime = 18446615868452621390,
  vruntime = 18446615868453018539,
  load = {
    weight = 9777152,
    inv_weight = 449829
  },
  vlag = -5599,

the sched_entity mapping to the leftmost node in the csf_rq, also the
left child of root node.
crash> struct sched_entity.deadline,min_vruntime,vruntime,load,vlag -l
sched_entity.run_node 0xffff9ea78d0280d0
  deadline = 18446615868452943132,
  min_vruntime = 18446615868452621390,
  vruntime = 18446615868452621390,
  load = {
    weight = 9777152,
    inv_weight = 449829
  },
  vlag = 444143,

the sched_entity mapping to the rightmost node in the csf_rq, also the
right child of root node.
crash> struct sched_entity.deadline,min_vruntime,vruntime,load,vlag -l
sched_entity.run_node 0xffff9ea783144350
  deadline = 515705106937888,
  min_vruntime = 515705106616146,
  vruntime = 515705106616146,
  load = {
    weight = 9777152,
    inv_weight = 449829
  },
  vlag = -260493,

Fixes: 147f3efaa241 ("sched/fair: Implement an EEVDF-like scheduling policy")
Signed-off-by: Zicheng Qu <quzicheng@huawei.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 7a14da5396fb..bfa4090cef93 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -734,7 +734,7 @@ static int vruntime_eligible(struct cfs_rq *cfs_rq, u64 vruntime)
 		load += weight;
 	}
 
-	return avg >= (s64)(vruntime - cfs_rq->min_vruntime) * load;
+	return avg - (s64)(vruntime - cfs_rq->min_vruntime) * load >= 0;
 }
 
 int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-01-06 11:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-08  9:21 [PATCH] sched/fair: Fix overflow in vruntime_eligible() causing NULL return quzicheng
  -- strict thread matches above, loose matches on Subject: below --
2026-01-06 11:34 Fang Xiang
2025-07-09  9:38 Zicheng Qu
2025-07-09 11:53 ` Peter Zijlstra
2025-07-10  2:01   ` Zicheng Qu
2025-07-10 11:13     ` Peter Zijlstra
2025-08-07  3:06 ` Fang Xiang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.