Skip to content

Commit d505b8a

Browse files
changhuaixinPeter Zijlstra
authored andcommitted
sched: Defend cfs and rt bandwidth quota against overflow
When users write some huge number into cpu.cfs_quota_us or cpu.rt_runtime_us, overflow might happen during to_ratio() shifts of schedulable checks. to_ratio() could be altered to avoid unnecessary internal overflow, but min_cfs_quota_period is less than 1 << BW_SHIFT, so a cutoff would still be needed. Set a cap MAX_BW for cfs_quota_us and rt_runtime_us to prevent overflow. Signed-off-by: Huaixin Chang <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Ben Segall <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent dbe9337 commit d505b8a

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

kernel/sched/core.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7379,6 +7379,8 @@ static DEFINE_MUTEX(cfs_constraints_mutex);
73797379

73807380
const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
73817381
static const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
7382+
/* More than 203 days if BW_SHIFT equals 20. */
7383+
static const u64 max_cfs_runtime = MAX_BW * NSEC_PER_USEC;
73827384

73837385
static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
73847386

@@ -7406,6 +7408,12 @@ static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
74067408
if (period > max_cfs_quota_period)
74077409
return -EINVAL;
74087410

7411+
/*
7412+
* Bound quota to defend quota against overflow during bandwidth shift.
7413+
*/
7414+
if (quota != RUNTIME_INF && quota > max_cfs_runtime)
7415+
return -EINVAL;
7416+
74097417
/*
74107418
* Prevent race between setting of cfs_rq->runtime_enabled and
74117419
* unthrottle_offline_cfs_rqs().

kernel/sched/rt.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
int sched_rr_timeslice = RR_TIMESLICE;
1111
int sysctl_sched_rr_timeslice = (MSEC_PER_SEC / HZ) * RR_TIMESLICE;
12+
/* More than 4 hours if BW_SHIFT equals 20. */
13+
static const u64 max_rt_runtime = MAX_BW;
1214

1315
static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
1416

@@ -2585,6 +2587,12 @@ static int tg_set_rt_bandwidth(struct task_group *tg,
25852587
if (rt_period == 0)
25862588
return -EINVAL;
25872589

2590+
/*
2591+
* Bound quota to defend quota against overflow during bandwidth shift.
2592+
*/
2593+
if (rt_runtime != RUNTIME_INF && rt_runtime > max_rt_runtime)
2594+
return -EINVAL;
2595+
25882596
mutex_lock(&rt_constraints_mutex);
25892597
err = __rt_schedulable(tg, rt_period, rt_runtime);
25902598
if (err)
@@ -2702,7 +2710,9 @@ static int sched_rt_global_validate(void)
27022710
return -EINVAL;
27032711

27042712
if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
2705-
(sysctl_sched_rt_runtime > sysctl_sched_rt_period))
2713+
((sysctl_sched_rt_runtime > sysctl_sched_rt_period) ||
2714+
((u64)sysctl_sched_rt_runtime *
2715+
NSEC_PER_USEC > max_rt_runtime)))
27062716
return -EINVAL;
27072717

27082718
return 0;

kernel/sched/sched.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,8 @@ extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
19151915
#define BW_SHIFT 20
19161916
#define BW_UNIT (1 << BW_SHIFT)
19171917
#define RATIO_SHIFT 8
1918+
#define MAX_BW_BITS (64 - BW_SHIFT)
1919+
#define MAX_BW ((1ULL << MAX_BW_BITS) - 1)
19181920
unsigned long to_ratio(u64 period, u64 runtime);
19191921

19201922
extern void init_entity_runnable_average(struct sched_entity *se);

0 commit comments

Comments
 (0)