[PATCH v2] sched/fair: set burst to 0 when remove the restriction on cfs bandwidth

From: Cheng Yu
Date: Mon Jul 08 2024 - 08:13:23 EST


From: Zhao Wenhui <zhaowenhui8@xxxxxxxxxx>

In the cpu subsystem of cgroup v1 and v2, we set the restriction on cfs
bandwidth by setting the quota and burst value. Later, when we remove
the restriction by setting the quota to the default value, the burst
value should also be forced to the its default value of zero.

In the cgroup v1 cpu subsystem, assuming we have a cgroup named 'test',
and we set cpu.cfs_quota_us and cpu.cfs_burst_us:
# echo 100000 > cpu.cfs_quota_us
# echo 100000 > cpu.cfs_burst_us

Next we remove the restriction on cfs bandwidth:
# echo -1 > cpu.cfs_quota_us
# cat cpu.cfs_quota_us
-1
# cat cpu.cfs_burst_us
100000

Now we expect that the value of burst should be zero. When the burst is
zero, it means that the restriction on burst is removed.

The same situation exists for cgroup v2. The difference is that the
interface definition of the cpu subsystem and the default value of
quota. In v2, we remove the restriction on cfs bandwidth by setting max
to cpu.max.

Fixes: f4183717b370 ("sched/fair: Introduce the burstable CFS controller")
Reported-by: Zhao Gongyi <zhaogongyi@xxxxxxxxxx>
Reported-by: Qixin Liao <liaoqixin@xxxxxxxxxx>
Signed-off-by: Zhao Wenhui <zhaowenhui8@xxxxxxxxxx>
Signed-off-by: Cheng Yu <serein.chengyu@xxxxxxxxxx>
Tested-by: Vishal Chourasia <vishalc@xxxxxxxxxxxxx>
Reviewed-by: Tianchen Ding <dtcccc@xxxxxxxxxxxxxxxxx>
Reviewed-by: Ben Segall <bsegall@xxxxxxxxxx>
---
Change log:
----------
v2:
- Put the modifications to cgroup v1 and v2 in one patch
v1:
- patch for cgroup v1:
https://lore.kernel.org/all/20220809120320.19496-1-zhaowenhui8@xxxxxxxxxx/
- patchset for cgroup v1 and v2:
https://lore.kernel.org/all/20240522031007.643498-1-serein.chengyu@xxxxxxxxxx/
---
kernel/sched/core.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index bcf2c4cc0522..982d357b3983 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -10840,6 +10840,12 @@ static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota,
burst + quota > max_cfs_runtime))
return -EINVAL;

+ /*
+ * Ensure burst equals to zero when quota is -1.
+ */
+ if (quota == RUNTIME_INF && burst)
+ return -EINVAL;
+
/*
* Prevent race between setting of cfs_rq->runtime_enabled and
* unthrottle_offline_cfs_rqs().
@@ -10899,8 +10905,10 @@ static int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)

period = ktime_to_ns(tg->cfs_bandwidth.period);
burst = tg->cfs_bandwidth.burst;
- if (cfs_quota_us < 0)
+ if (cfs_quota_us < 0) {
quota = RUNTIME_INF;
+ burst = 0;
+ }
else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC)
quota = (u64)cfs_quota_us * NSEC_PER_USEC;
else
@@ -11406,8 +11414,11 @@ static ssize_t cpu_max_write(struct kernfs_open_file *of,
int ret;

ret = cpu_period_quota_parse(buf, &period, &quota);
- if (!ret)
+ if (!ret) {
+ if (quota == RUNTIME_INF)
+ burst = 0;
ret = tg_set_cfs_bandwidth(tg, period, quota, burst);
+ }
return ret ?: nbytes;
}
#endif
--
2.25.1