[PATCH v2] sched: use u64 for bandwidth ratio calculations

Joseph Salisbury posted 1 patch 2 months, 1 week ago
kernel/sched/core.c  | 2 +-
kernel/sched/rt.c    | 2 +-
kernel/sched/sched.h | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
[PATCH v2] sched: use u64 for bandwidth ratio calculations
Posted by Joseph Salisbury 2 months, 1 week ago
to_ratio() computes BW_SHIFT-scaled bandwidth ratios from u64 period and
runtime values, but it returns unsigned long.  tg_rt_schedulable() also
stores the current group limit and the accumulated child sum in unsigned
long.

On 32-bit builds, large bandwidth ratios can be truncated and the RT
group sum can wrap when enough siblings are present.  That can let an
overcommitted RT hierarchy pass the schedulability check, and it also
narrows the helper result for other callers.

Return u64 from to_ratio() and use u64 for the RT group totals so
bandwidth ratios are preserved and compared at full width on both 32-bit
and 64-bit builds.

Fixes: b40b2e8eb521 ("sched: rt: multi level group constraints")
Cc: stable@vger.kernel.org
Assisted-by: Codex:GPT-5
Signed-off-by: Joseph Salisbury <joseph.salisbury@oracle.com>
---
v2:
- Change to_ratio() to return u64 and update its declaration.
- Keep tg_rt_schedulable() bandwidth totals in u64.
- Drop the extra Fixes: tag and add Michal Koutny to Cc:.

 kernel/sched/core.c  | 2 +-
 kernel/sched/rt.c    | 2 +-
 kernel/sched/sched.h | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 496dff740dca..14e947bcb3e5 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -4738,7 +4738,7 @@ void sched_post_fork(struct task_struct *p)
 	scx_post_fork(p);
 }
 
-unsigned long to_ratio(u64 period, u64 runtime)
+u64 to_ratio(u64 period, u64 runtime)
 {
 	if (runtime == RUNTIME_INF)
 		return BW_UNIT;
diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index f69e1f16d923..906f6c656c2e 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -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);
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 43bbf0693cca..8d1ff8d950d3 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2896,7 +2896,7 @@ extern void init_cfs_throttle_work(struct task_struct *p);
 #define MAX_BW_BITS		(64 - BW_SHIFT)
 #define MAX_BW			((1ULL << MAX_BW_BITS) - 1)
 
-extern unsigned long to_ratio(u64 period, u64 runtime);
+extern u64 to_ratio(u64 period, u64 runtime);
 
 extern void init_entity_runnable_average(struct sched_entity *se);
 extern void post_init_entity_util_avg(struct task_struct *p);
-- 
2.47.3
Re: [PATCH v2] sched: use u64 for bandwidth ratio calculations
Posted by Michal Koutný 2 months, 1 week ago
On Fri, Apr 03, 2026 at 05:00:14PM -0400, Joseph Salisbury <joseph.salisbury@oracle.com> wrote:
> to_ratio() computes BW_SHIFT-scaled bandwidth ratios from u64 period and
> runtime values, but it returns unsigned long.  tg_rt_schedulable() also
> stores the current group limit and the accumulated child sum in unsigned
> long.
> 
> On 32-bit builds, large bandwidth ratios can be truncated and the RT
> group sum can wrap when enough siblings are present.  That can let an
> overcommitted RT hierarchy pass the schedulability check, and it also
> narrows the helper result for other callers.
> 
> Return u64 from to_ratio() and use u64 for the RT group totals so
> bandwidth ratios are preserved and compared at full width on both 32-bit
> and 64-bit builds.

Thanks.
 
> Fixes: b40b2e8eb521 ("sched: rt: multi level group constraints")
> Cc: stable@vger.kernel.org
> Assisted-by: Codex:GPT-5
> Signed-off-by: Joseph Salisbury <joseph.salisbury@oracle.com>
> ---
> v2:
> - Change to_ratio() to return u64 and update its declaration.
> - Keep tg_rt_schedulable() bandwidth totals in u64.
> - Drop the extra Fixes: tag and add Michal Koutny to Cc:.
> 
>  kernel/sched/core.c  | 2 +-
>  kernel/sched/rt.c    | 2 +-
>  kernel/sched/sched.h | 2 +-
>  3 files changed, 3 insertions(+), 3 deletions(-)

Reviewed-by: Michal Koutný <mkoutny@suse.com>
[tip: sched/core] sched: Use u64 for bandwidth ratio calculations
Posted by tip-bot2 for Joseph Salisbury 2 months, 1 week ago
The following commit has been merged into the sched/core branch of tip:

Commit-ID:     c6e80201e057dfb7253385e60bf541121bf5dc33
Gitweb:        https://git.kernel.org/tip/c6e80201e057dfb7253385e60bf541121bf5dc33
Author:        Joseph Salisbury <joseph.salisbury@oracle.com>
AuthorDate:    Fri, 03 Apr 2026 17:00:14 -04:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Tue, 07 Apr 2026 09:23:52 +02:00

sched: Use u64 for bandwidth ratio calculations

to_ratio() computes BW_SHIFT-scaled bandwidth ratios from u64 period and
runtime values, but it returns unsigned long.  tg_rt_schedulable() also
stores the current group limit and the accumulated child sum in unsigned
long.

On 32-bit builds, large bandwidth ratios can be truncated and the RT
group sum can wrap when enough siblings are present.  That can let an
overcommitted RT hierarchy pass the schedulability check, and it also
narrows the helper result for other callers.

Return u64 from to_ratio() and use u64 for the RT group totals so
bandwidth ratios are preserved and compared at full width on both 32-bit
and 64-bit builds.

Fixes: b40b2e8eb521 ("sched: rt: multi level group constraints")
Assisted-by: Codex:GPT-5
Signed-off-by: Joseph Salisbury <joseph.salisbury@oracle.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20260403210014.2713404-1-joseph.salisbury@oracle.com
---
 kernel/sched/core.c  | 2 +-
 kernel/sched/rt.c    | 2 +-
 kernel/sched/sched.h | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index c15c986..49cd5d2 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -4735,7 +4735,7 @@ void sched_post_fork(struct task_struct *p)
 	scx_post_fork(p);
 }
 
-unsigned long to_ratio(u64 period, u64 runtime)
+u64 to_ratio(u64 period, u64 runtime)
 {
 	if (runtime == RUNTIME_INF)
 		return BW_UNIT;
diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index 4e5f195..a48e867 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -2666,7 +2666,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);
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 9594355..c955841 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2907,7 +2907,7 @@ extern void init_cfs_throttle_work(struct task_struct *p);
 #define MAX_BW_BITS		(64 - BW_SHIFT)
 #define MAX_BW			((1ULL << MAX_BW_BITS) - 1)
 
-extern unsigned long to_ratio(u64 period, u64 runtime);
+extern u64 to_ratio(u64 period, u64 runtime);
 
 extern void init_entity_runnable_average(struct sched_entity *se);
 extern void post_init_entity_util_avg(struct task_struct *p);