Skip to contentSkip to conversation

sched/rt: use u64 for RT group bandwidth accounting

PATCH In review Joseph Salisbury 2 participants 3 messages Mar 17 – Apr 3 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/rt: use u64 for RT group bandwidth accounting

tg_rt_schedulable() sums the bandwidth ratios of all child RT groups and compares the result against the bandwidth available to the parent.

On 32-bit builds, the running sum is stored in unsigned long and can overflow when enough child groups are present. In that case, an overcommitted hierarchy may incorrectly pass the schedulability check.

Use u64 for the local bandwidth totals so the accumulated child sum and the parent limit are compared without 32-bit overflow.

Fixes b40b2e8eb521 ("sched: rt: multi level group constraints")
Assisted-by Codex:GPT-5
---
 kernel/sched/rt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/rt.c +1 -1
index f69e1f16d923..906f6c656c2e 100644
@@ -2652,7 +2652,7 @@ static int tg_rt_schedulable(struct task_group *tg, void *data)
 {
 	struct rt_schedulable_data *d = data;
 	struct task_group *child;
-	unsigned long total, sum = 0;
+	u64 total, sum = 0;
 	u64 period, runtime;
 
 	period = ktime_to_ns(tg->rt_bandwidth.rt_period);
Signature · 1 line
2.47.3
#1 lore raw

Hi.

Quoted 5 lines · Joseph Salisbury

tg_rt_schedulable() sums the bandwidth ratios of all child RT groups and compares the result against the bandwidth available to the parent.

On 32-bit builds, the running sum is stored in unsigned long and can overflow when enough child groups are present.

As per my calculation this'd require at least 4096 sibling RT groups (see to_ratio() and BW_SHIFT)...

Quoted 2 lines

In that case, an overcommitted hierarchy may incorrectly pass the schedulability check.

...and I expect there'd other issues with so many so little RT groups than allowing theoretically unschedulabile configuration.

Quoted 3 lines

Use u64 for the local bandwidth totals so the accumulated child sum and the parent limit are compared without 32-bit overflow.

You may want to fix this also in to_ratio() return type as there are other consumers besides this check. (Although, some conditions would still be similarly exotic, e.g. normalize_cfs_quota(), like CFQ quota of more than 4096 (32b!) CPUs.)

Thanks,
Michal

#2 lore raw

Thanks for the feedback, Michal!

Quoted 8 lines · Michal Koutný

Hi.

Quoted 5 lines · Joseph Salisbury

tg_rt_schedulable() sums the bandwidth ratios of all child RT groups and compares the result against the bandwidth available to the parent.

On 32-bit builds, the running sum is stored in unsigned long and can overflow when enough child groups are present.

Quoted 7 lines

As per my calculation this'd require at least 4096 sibling RT groups (see to_ratio() and BW_SHIFT)...

Quoted 2 lines

In that case, an overcommitted hierarchy may incorrectly pass the schedulability check.

...and I expect there'd other issues with so many so little RT groups than allowing theoretically unschedulabile configuration.

You are right.  This is mostly a correctness fix for a corner case. This patch primarily addresses the case where the arithmetic can silently truncate on 32-bit.

Quoted 10 lines
Quoted 3 lines

Use u64 for the local bandwidth totals so the accumulated child sum and the parent limit are compared without 32-bit overflow.

You may want to fix this also in to_ratio() return type as there are other consumers besides this check. (Although, some conditions would still be similarly exotic, e.g. normalize_cfs_quota(), like CFQ quota of more than 4096 (32b!) CPUs.)

Thanks,
Michal

That's a good point.   I can send a v2 that returns u64 from to_ratio() and keeps the RT totals in u64 as well.