[PATCH 2/9] tools/sched_ext: scx_flatcg: Validate the stats interval

From: Tao Cui

Date: Tue Nov 10 2026 - 07:57:38 EST


From: Tao Cui <cuitao@xxxxxxxxxx>

The interval is parsed with strtod() and split into a timespec with a
float cast in the middle: a negative value produces a negative tv_sec
or tv_nsec (for -0.5 the seconds truncate to zero and the nanoseconds
go negative), and any value that doesn't survive the float round-trip
(e.g. 2^24 + 1) produces tv_nsec >= 1e9. Either way nanosleep()
returns EINVAL, which is never checked, and the stats loop starts
busy-spinning at 100% CPU instead of sampling at the requested
interval.

Reject negative intervals up front and compute the nsec part from the
double rather than through a float so the rounding hole is gone.

Fixes: a4103eacc2ab ("sched_ext: Add a cgroup scheduler which uses flattened hierarchy")
Signed-off-by: Tao Cui <cuitao@xxxxxxxxxx>
---
tools/sched_ext/scx_flatcg.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/tools/sched_ext/scx_flatcg.c b/tools/sched_ext/scx_flatcg.c
index 4c3c2cca2b85..bac6535fa2f0 100644
--- a/tools/sched_ext/scx_flatcg.c
+++ b/tools/sched_ext/scx_flatcg.c
@@ -158,8 +158,12 @@ int main(int argc, char **argv)
break;
case 'i':
v = strtod(optarg, NULL);
+ if (!(v >= 0) || v >= 1e18) {
+ fprintf(stderr, "Invalid interval %f\n", v);
+ exit(1);
+ }
intv_ts.tv_sec = v;
- intv_ts.tv_nsec = (v - (float)intv_ts.tv_sec) * 1000000000;
+ intv_ts.tv_nsec = (v - intv_ts.tv_sec) * 1000000000;
break;
case 'f':
skel->rodata->fifo_sched = true;
--
2.53.0

base-commit: 5b740a72b55c4267ba4d8010f17dde26b26f52d3