Skip to contentSkip to conversation

sched_ext: Fix incorrect time delta calculation in time_delta()

PATCH Applied Changwoo Min 3 participants 3 messages Feb 2, 2025 Scheduler - sched_ext

lore
Share & tools
lore

Changwoo Min

author reviewer
#0 lore raw

PATCH 1/1 sched_ext: Fix incorrect time delta calculation in time_delta()

When (s64)(after - before) > 0, the code returns the result of (s64)(after - before) > 0 while the intended result should be (s64)(after - before). That happens because the middle operand of the ternary operator was omitted incorrectly, returning the result of (s64)(after - before) > 0. Thus, add the middle operand
-- (s64)(after - before) -- to return the correct time calculation.

Fixes d07be814fc71 ("sched_ext: Add time helpers for BPF schedulers")
---
 tools/sched_ext/include/scx/common.bpf.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/sched_ext/include/scx/common.bpf.h +1 -1
index f254a39b86a5..d72b60a0c582 100644
@@ -432,7 +432,7 @@ void bpf_rcu_read_unlock(void) __ksym;
  */
 static inline s64 time_delta(u64 after, u64 before)
 {
-	return (s64)(after - before) > 0 ? : 0;
+	return (s64)(after - before) > 0 ? (s64)(after - before) : 0;
 }
 
 /**
Signature · 1 line
2.48.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 18 lines · Changwoo Min

When (s64)(after - before) > 0, the code returns the result of (s64)(after - before) > 0 while the intended result should be (s64)(after - before). That happens because the middle operand of the ternary operator was omitted incorrectly, returning the result of (s64)(after - before) > 0. Thus, add the middle operand
-- (s64)(after - before) -- to return the corRECT time calculation.

Fixes d07be814fc71 ("sched_ext: Add time helpers for BPF schedulers")
---
 tools/sched_ext/include/scx/common.bpf.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/sched_ext/include/scx/common.bpf.h +0 -0
index f254a39b86a5..d72b60a0c582 100644

tools/sched_ext/include/scx/common.bpf.h void bpf_rcu_read_unlock(void) __ksym; · line 435

Quoted 5 lines
tools/sched_ext/include/scx/common.bpf.h +1 -1
@@ -432,7 +432,7 @@ void bpf_rcu_read_unlock(void) __ksym;
  */
 static inline s64 time_delta(u64 after, u64 before)
 {
-	return (s64)(after - before) > 0 ? : 0;
+	return (s64)(after - before) > 0 ? (s64)(after - before) : 0;

Ugh.. how did we miss this? Thanks for noticing.

-Andrea

reviewer Applied
#2 lore raw
In reply to Changwoo Min
Quoted 9 lines · Changwoo Min

When (s64)(after - before) > 0, the code returns the result of (s64)(after - before) > 0 while the intended result should be (s64)(after - before). That happens because the middle operand of the ternary operator was omitted incorrectly, returning the result of (s64)(after - before) > 0. Thus, add the middle operand
-- (s64)(after - before) -- to return the correct time calculation.

Fixes d07be814fc71 ("sched_ext: Add time helpers for BPF schedulers")

Applied to sched_ext/for-6.14-fixes.

Thanks.

Signature · 1 line
tejun