Skip to contentSkip to conversation

sched/pelt: Fix truncation of the PELT period count

PATCH Superseded Hui Su 4 participants 7 messages Oct 22 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hui Su

author
#0 lore raw

PATCH 1/1 sched/pelt: Fix truncation of the PELT period count

accumulate_sum() computes the number of elapsed PELT periods in a u64 but returns u32. When the count is a multiple of 2^32, it is truncated to zero even though the PELT sums have been updated.

The caller interprets zero as no period boundary crossed and skips recomputing the averages, leaving *_avg stale relative to *_sum.

Return only whether any period elapsed, which is all the caller needs.

---
 kernel/sched/pelt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/pelt.c +1 -1
index 779eb58a4261..519bc9c40043 100644
@@ -146,7 +146,7 @@ accumulate_sum(u64 delta, struct sched_avg *sa,
 	if (running)
 		sa->util_sum += contrib << SCHED_CAPACITY_SHIFT;
 
-	return periods;
+	return !!periods;
 }
 
 /*
Signature · 1 line
2.55.0
#1 lore raw

Hello Hui,

Quoted 2 lines · Hui Su

accumulate_sum() computes the number of elapsed PELT periods in a u64 but returns u32. When the count is a multiple of 2^32, it is

So at least an hour between PELT updates! I suppose it is possible with nohz so not too far fetched.

Quoted 8 lines

truncated to zero even though the PELT sums have been updated.

The caller interprets zero as no period boundary crossed and skips recomputing the averages, leaving *_avg stale relative to *_sum.

Return only whether any period elapsed, which is all the caller needs.

Probably needs a:

Fixes a481db34b9be ("sched/fair: Optimize ___update_sched_avg()") # c079629862b2
Quoted 10 lines
---
 kernel/sched/pelt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/pelt.c +0 -0
index 779eb58a4261..519bc9c40043 100644

kernel/sched/pelt.c accumulate_sum(u64 delta, struct sched_avg *sa, · line 149

Quoted 5 lines
kernel/sched/pelt.c +1 -1
@@ -146,7 +146,7 @@ accumulate_sum(u64 delta, struct sched_avg *sa,
 	if (running)
 		sa->util_sum += contrib << SCHED_CAPACITY_SHIFT;
 
-	return periods;
+	return !!periods;

I would actually favor changing return type to u64. Since accumulate_sum() is only called in one place, and that too within an 'if' condition, I'm sure compiler can optimize it similarly.

Quoted 3 lines
kernel/sched/pelt.c +0 -0
 }
 
 /*
Signature · 2 lines
Thanks and Regards,
Prateek
maintainer
#2 lore raw
Quoted 9 lines · K Prateek Nayak

Hello Hui,

Quoted 2 lines · Hui Su

accumulate_sum() computes the number of elapsed PELT periods in a u64 but returns u32. When the count is a multiple of 2^32, it is

So at least an hour between PELT updates! I suppose it is possible with nohz so not too far fetched.

Period is 1ms so it's more in the range of 50 days

Quoted 42 lines
Quoted 9 lines

truncated to zero even though the PELT sums have been updated.

The caller interprets zero as no period boundary crossed and skips recomputing the averages, leaving *_avg stale relative to *_sum.

Return only whether any period elapsed, which is all the caller needs.

Probably needs a:

Fixes a481db34b9be ("sched/fair: Optimize ___update_sched_avg()") # c079629862b2
Quoted 10 lines
---
 kernel/sched/pelt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/pelt.c +0 -0
index 779eb58a4261..519bc9c40043 100644

kernel/sched/pelt.c accumulate_sum(u64 delta, struct sched_avg *sa, · line 149

Quoted 5 lines
kernel/sched/pelt.c +1 -1
@@ -146,7 +146,7 @@ accumulate_sum(u64 delta, struct sched_avg *sa,
      if (running)
              sa->util_sum += contrib << SCHED_CAPACITY_SHIFT;
 
-     return periods;
+     return !!periods;

I would actually favor changing return type to u64. Since accumulate_sum() is only called in one place, and that too within an 'if' condition, I'm sure compiler can optimize it similarly.

kernel/sched/pelt.c accumulate_sum(u64 delta, struct sched_avg *sa, · line 152

Quoted 3 lines
kernel/sched/pelt.c +0 -0
 }
 
 /*

--
Thanks and Regards,
Prateek

author
#3 lore raw
In reply to K Prateek Nayak
Quoted 7 lines

Probably needs a:

Fixes a481db34b9be ("sched/fair: Optimize ___update_sched_avg()") # c079629862b2

I would actually favor changing return type to u64. Since accumulate_sum() is only called in one place, and that too within an 'if' condition, I'm sure compiler can optimize it similarly.

Thanks for the review.

I will use a481db34b9be in v2's Fixes tag and change accumulate_sum() to return u64.

Thanks,
Hui

maintainer Reviewed-by on v1 1/1
#4 lore raw
In reply to Hui Su
Quoted 4 lines · Hui Su

accumulate_sum() computes the number of elapsed PELT periods in a u64 but returns u32. When the count is a multiple of 2^32, it is truncated to zero even though the PELT sums have been updated.

Did you face this problem, or did you find it only during a code review?

This would happen every multiple of exactly 2^32 ms (~50days) without updating pelt whereas the latter is supposed to be updated far more often (within second) until it reaches 0. Then such a large period would keep it at 0 anyway

Quoted 8 lines

The caller interprets zero as no period boundary crossed and skips recomputing the averages, leaving *_avg stale relative to *_sum.

Return only whether any period elapsed, which is all the caller needs.

Even if I doubt it can happen, it's better to fix it anyway

Quoted 20 lines
---
 kernel/sched/pelt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/pelt.c +1 -2
index 779eb58a4261..519bc9c40043 100644
@@ -146,7 +146,7 @@ accumulate_sum(u64 delta, struct sched_avg *sa,
        if (running)
                sa->util_sum += contrib << SCHED_CAPACITY_SHIFT;
 
-       return periods;
+       return !!periods;
 }
 
 /*
--

2.55.0

author
#5 lore raw
Quoted 1 line · Vincent Guittot

Did you face this problem, or did you find it only during a code review?

I found it during code review, not from a production failure.

I reproduced it with a KUnit test that advances PELT by exactly 2^32 periods. Before the fix, util_avg remained 43 instead of 1024; after the fix, the test passed. A standalone harness confirmed the same result.

Thanks for the review.

Thanks,
Hui

Reviewed-by on v1 1/1
#6 lore raw
In reply to Hui Su
Quoted 20 lines · Hui Su

accumulate_sum() computes the number of elapsed PELT periods in a u64 but returns u32. When the count is a multiple of 2^32, it is truncated to zero even though the PELT sums have been updated.

The caller interprets zero as no period boundary crossed and skips recomputing the averages, leaving *_avg stale relative to *_sum.

Return only whether any period elapsed, which is all the caller needs.

---
 kernel/sched/pelt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/pelt.c +0 -0
index 779eb58a4261..519bc9c40043 100644

kernel/sched/pelt.c accumulate_sum(u64 delta, struct sched_avg *sa, · line 150

Quoted 6 lines
kernel/sched/pelt.c +1 -1
@@ -146,7 +146,7 @@ accumulate_sum(u64 delta, struct sched_avg *sa,
 	if (running)
 		sa->util_sum += contrib << SCHED_CAPACITY_SHIFT;
 
-	return periods;
+	return !!periods;
 }

I think making this return value u64 is cleaner (As Prateek says). Both works for me tho.

Quoted 5 lines
kernel/sched/pelt.c +0 -0
 /*
Signature · 1 line
2.55.0