Skip to content

Commit 1d3b43c

Browse files
author
Xuewei Zhang
committed
sched/fair: Scale bandwidth quota and period without losing quota/period ratio precision
commit 4929a4e upstream. The quota/period ratio is used to ensure a child task group won't get more bandwidth than the parent task group, and is calculated as: normalized_cfs_quota() = [(quota_us << 20) / period_us] If the quota/period ratio was changed during this scaling due to precision loss, it will cause inconsistency between parent and child task groups. See below example: A userspace container manager (kubelet) does three operations: 1) Create a parent cgroup, set quota to 1,000us and period to 10,000us. 2) Create a few children cgroups. 3) Set quota to 1,000us and period to 10,000us on a child cgroup. These operations are expected to succeed. However, if the scaling of 147/128 happens before step 3, quota and period of the parent cgroup will be changed: new_quota: 1148437ns, 1148us new_period: 11484375ns, 11484us And when step 3 comes in, the ratio of the child cgroup will be 104857, which will be larger than the parent cgroup ratio (104821), and will fail. Scaling them by a factor of 2 will fix the problem. Tested-by: Phil Auld <pauld@redhat.com> Signed-off-by: Xuewei Zhang <xueweiz@google.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Phil Auld <pauld@redhat.com> Cc: Anton Blanchard <anton@ozlabs.org> Cc: Ben Segall <bsegall@google.com> Cc: Dietmar Eggemann <dietmar.eggemann@arm.com> Cc: Juri Lelli <juri.lelli@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mel Gorman <mgorman@suse.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vincent Guittot <vincent.guittot@linaro.org> Fixes: 2e8e192 ("sched/fair: Limit sched_cfs_period_timer() loop to avoid hard lockup") Link: https://lkml.kernel.org/r/20191004001243.140897-1-xueweiz@google.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent dc824ef commit 1d3b43c

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

kernel/sched/fair.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4055,20 +4055,28 @@ static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
40554055
if (++count > 3) {
40564056
u64 new, old = ktime_to_ns(cfs_b->period);
40574057

4058-
new = (old * 147) / 128; /* ~115% */
4059-
new = min(new, max_cfs_quota_period);
4060-
4061-
cfs_b->period = ns_to_ktime(new);
4062-
4063-
/* since max is 1s, this is limited to 1e9^2, which fits in u64 */
4064-
cfs_b->quota *= new;
4065-
cfs_b->quota = div64_u64(cfs_b->quota, old);
4066-
4067-
pr_warn_ratelimited(
4068-
"cfs_period_timer[cpu%d]: period too short, scaling up (new cfs_period_us %lld, cfs_quota_us = %lld)\n",
4069-
smp_processor_id(),
4070-
div_u64(new, NSEC_PER_USEC),
4071-
div_u64(cfs_b->quota, NSEC_PER_USEC));
4058+
/*
4059+
* Grow period by a factor of 2 to avoid losing precision.
4060+
* Precision loss in the quota/period ratio can cause __cfs_schedulable
4061+
* to fail.
4062+
*/
4063+
new = old * 2;
4064+
if (new < max_cfs_quota_period) {
4065+
cfs_b->period = ns_to_ktime(new);
4066+
cfs_b->quota *= 2;
4067+
4068+
pr_warn_ratelimited(
4069+
"cfs_period_timer[cpu%d]: period too short, scaling up (new cfs_period_us = %lld, cfs_quota_us = %lld)\n",
4070+
smp_processor_id(),
4071+
div_u64(new, NSEC_PER_USEC),
4072+
div_u64(cfs_b->quota, NSEC_PER_USEC));
4073+
} else {
4074+
pr_warn_ratelimited(
4075+
"cfs_period_timer[cpu%d]: period too short, but cannot scale up without losing precision (cfs_period_us = %lld, cfs_quota_us = %lld)\n",
4076+
smp_processor_id(),
4077+
div_u64(old, NSEC_PER_USEC),
4078+
div_u64(cfs_b->quota, NSEC_PER_USEC));
4079+
}
40724080

40734081
/* reset count so we don't come right back in here */
40744082
count = 0;

0 commit comments

Comments
 (0)