sched/fair: fix broken bandwidth control with nohz_full
PATCH 1/1 sched/fair: fix broken bandwidth control with nohz_full
With nohz_full enabled on cpu, the scheduler_tick() will be stopped when only one CFS task left on rq.
scheduler_tick()
task_tick_fair()
entity_tick()
update_curr()
account_cfs_rq_runtime(cfs_rq, delta_exec) --> stoppedSo that running task can't account its runtime periodically, but the cfs_bandwidth hrtimer still __refill_cfs_bandwidth_runtime() periodically. Later in one period, the task would account very big delta_exec, which cause the cfs_rq to be throttled for a long time.
There are two solutions for the problem, the first is that we can check in sched_can_stop_tick() if current task's cfs_rq have runtime_enabled, in which case we don't stop tick. But it will make nohz_full almost useless in cloud environment that every container has the cpu bandwidth control setting.
The other is what this patch implemented, cfs_bandwidth hrtimer would sync unaccounted runtime from all running cfs_rqs with tick stopped, just before __refill_cfs_bandwidth_runtime(). Also do the same thing in tg_set_cfs_bandwidth().
A testcase to reproduce:
``` cd /sys/fs/cgroup echo "+cpu" > cgroup.subtree_control mkdir test echo "105000 100000" > test/cpu.max echo $$ > test/cgroup.procs taskset -c 1 bash -c "while true; do let i++; done" ```
Ctrl-C and cat test/cpu.stat to see if nr_throttled > 0.
The above testcase uses period 100ms and quota 105ms, would only see nr_throttled > 0 on nohz_full system. The problem is gone in test with this patch.
--- kernel/sched/core.c | 4 ++++ kernel/sched/fair.c | 30 ++++++++++++++++++++++++++++++ kernel/sched/sched.h | 3 +++ 3 files changed, 37 insertions(+)
kernel/sched/core.c +4 -0
@@ -10443,6 +10443,10 @@ static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota,*/ if (runtime_enabled && !runtime_was_enabled)cfs_bandwidth_usage_inc (); + +if (runtime_was_enabled) +sync_cfs_bandwidth_runtime (cfs_b); +raw_spin_lock_irq (&cfs_b->lock); cfs_b->period =ns_to_ktime (period); cfs_b->quota = quota;
kernel/sched/fair.c +30 -0
@@ -5339,6 +5339,34 @@ static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)return HRTIMER_NORESTART ; } +#ifdef CONFIG_NO_HZ_FULL +void sync_cfs_bandwidth_runtime (struct cfs_bandwidth *cfs_b) +{ +unsigned int cpu; +struct rq *rq; +struct rq_flags rf; +struct cfs_rq *cfs_rq; +struct task_group *tg; + + tg = container_of(cfs_b, struct task_group, cfs_bandwidth); + +for_each_online_cpu (cpu) { +if (!tick_nohz_tick_stopped_cpu (cpu)) +continue ; + + rq =cpu_rq (cpu); + cfs_rq = tg->cfs_rq[cpu]; + +rq_lock_irqsave (rq, &rf); +if (cfs_rq->curr) { +update_rq_clock (rq); +update_curr (cfs_rq); + } +rq_unlock_irqrestore (rq, &rf); + } +} +#endif +extern const u64 max_cfs_quota_period;static enum hrtimer_restart sched_cfs_period_timer (struct hrtimer *timer)
@@ -5350,6 +5378,8 @@ static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)int idle =0 ;int count =0 ; +sync_cfs_bandwidth_runtime (cfs_b); +raw_spin_lock_irqsave (&cfs_b->lock, flags);for (;;) { overrun =hrtimer_forward_now (timer, cfs_b->period);
kernel/sched/sched.h +3 -0
@@ -2351,9 +2351,12 @@ static inline void sched_update_tick_dependency(struct rq *rq) else tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED); } + +extern void sync_cfs_bandwidth_runtime (struct cfs_bandwidth *cfs_b);#else static inline int sched_tick_offload_init (void ) {return 0 ; }static inline void sched_update_tick_dependency (struct rq *rq) { } +static inline void sync_cfs_bandwidth_runtime (struct cfs_bandwidth *cfs_b) {}#endif static inline void add_nr_running (struct rq *rq,unsigned count)
Signature · 1 line
2.35.1
Peter Zijlstra
maintainerQuoted 15 lines · Chengming Zhou
With
nohz_fullenabled on cpu, thescheduler_tick()will be stopped when only one CFS task left on rq.
scheduler_tick()
task_tick_fair()entity_tick() update_curr() account_cfs_rq_runtime(cfs_rq, delta_exec) --> stoppedSo that running task can't account its runtime periodically, but the
cfs_bandwidthhrtimer still__refill_cfs_bandwidth_runtime()periodically. Later in one period, the task would account very bigdelta_exec, which cause thecfs_rqto be throttled for a long time.
Quoted 5 lines
There are two solutions for the problem, the first is that we can check in
sched_can_stop_tick()if current task'scfs_rqhaveruntime_enabled, in which case we don't stop tick. But it will makenohz_fullalmost useless in cloud environment that every container has the cpu bandwidth control setting.
How is NOHZ_FULL useful in that environment to begin with? If you set bandwidth crap, the expectation is that there is overcommit, which more or less assumes lots of scheduling, presumably VMs or somesuch crud.
So how does NOHZ_FULL make sense?
Quoted 22 lines · Peter Zijlstra
Quoted 15 lines · Chengming Zhou
With
nohz_fullenabled on cpu, thescheduler_tick()will be stopped when only one CFS task left on rq.
scheduler_tick()
task_tick_fair()entity_tick() update_curr() account_cfs_rq_runtime(cfs_rq, delta_exec) --> stoppedSo that running task can't account its runtime periodically, but the
cfs_bandwidthhrtimer still__refill_cfs_bandwidth_runtime()periodically. Later in one period, the task would account very bigdelta_exec, which cause thecfs_rqto be throttled for a long time.Quoted 5 lines
There are two solutions for the problem, the first is that we can check in
sched_can_stop_tick()if current task'scfs_rqhaveruntime_enabled, in which case we don't stop tick. But it will makenohz_fullalmost useless in cloud environment that every container has the cpu bandwidth control setting.
Quoted 5 lines
How is
NOHZ_FULLuseful in that environment to begin with? If you set bandwidth crap, the expectation is that there is overcommit, which more or less assumes lots of scheduling, presumably VMs or somesuch crud.So how does
NOHZ_FULLmake sense?
Yes, we have scheduled some VMs in cgroups on the host, which enabled NOHZ_FULL to reduce the interference of tick to vcpu task if it's the only task running on cpu.
This problem will however throttle it wrongly, even if it hasn't used up its quota.
Do you suggest that we shouldn't stop tick when the current task's cfs_rq has runtime_enabled ?
Thanks.
Peter Zijlstra
maintainerQuoted 33 lines · Chengming Zhou
Quoted 22 lines · Peter Zijlstra
Quoted 15 lines · Chengming Zhou
With
nohz_fullenabled on cpu, thescheduler_tick()will be stopped when only one CFS task left on rq.
scheduler_tick()
task_tick_fair()entity_tick() update_curr() account_cfs_rq_runtime(cfs_rq, delta_exec) --> stoppedSo that running task can't account its runtime periodically, but the
cfs_bandwidthhrtimer still__refill_cfs_bandwidth_runtime()periodically. Later in one period, the task would account very bigdelta_exec, which cause thecfs_rqto be throttled for a long time.Quoted 5 lines
There are two solutions for the problem, the first is that we can check in
sched_can_stop_tick()if current task'scfs_rqhaveruntime_enabled, in which case we don't stop tick. But it will makenohz_fullalmost useless in cloud environment that every container has the cpu bandwidth control setting.Quoted 5 lines
How is
NOHZ_FULLuseful in that environment to begin with? If you set bandwidth crap, the expectation is that there is overcommit, which more or less assumes lots of scheduling, presumably VMs or somesuch crud.So how does
NOHZ_FULLmake sense?Yes, we have scheduled some VMs in cgroups on the host, which enabled
NOHZ_FULLto reduce the interference of tick to vcpu task if it's the only task running on cpu.
Quoted 5 lines
This problem will however throttle it wrongly, even if it hasn't used up its quota.
Do you suggest that we shouldn't stop tick when the current task's
cfs_rqhasruntime_enabled?
I'm not suggesting anything just yet as I'm not sure I understand things well enough. I'm just wondering if NOHZ_FULL makes sense for you since NOHZ_FULL makes system entry/exit so much more expensive.
NOHZ_FULL is for use-cases that 'never' intend to go into the kernel, your use-case actively relies on going into the kernel. Hence the confusion.
Quoted 37 lines · Peter Zijlstra
Quoted 36 lines · Chengming Zhou
Quoted 22 lines · Peter Zijlstra
Quoted 15 lines · Chengming Zhou
With
nohz_fullenabled on cpu, thescheduler_tick()will be stopped when only one CFS task left on rq.
scheduler_tick()
task_tick_fair()entity_tick() update_curr() account_cfs_rq_runtime(cfs_rq, delta_exec) --> stoppedSo that running task can't account its runtime periodically, but the
cfs_bandwidthhrtimer still__refill_cfs_bandwidth_runtime()periodically. Later in one period, the task would account very bigdelta_exec, which cause thecfs_rqto be throttled for a long time.Quoted 5 lines
There are two solutions for the problem, the first is that we can check in
sched_can_stop_tick()if current task'scfs_rqhaveruntime_enabled, in which case we don't stop tick. But it will makenohz_fullalmost useless in cloud environment that every container has the cpu bandwidth control setting.Quoted 5 lines
How is
NOHZ_FULLuseful in that environment to begin with? If you set bandwidth crap, the expectation is that there is overcommit, which more or less assumes lots of scheduling, presumably VMs or somesuch crud.So how does
NOHZ_FULLmake sense?Yes, we have scheduled some VMs in cgroups on the host, which enabled
NOHZ_FULLto reduce the interference of tick to vcpu task if it's the only task running on cpu.This problem will however throttle it wrongly, even if it hasn't used up its quota.
Quoted 6 lines
Quoted 2 lines
Do you suggest that we shouldn't stop tick when the current task's
cfs_rqhasruntime_enabled?I'm not suggesting anything just yet as I'm not sure I understand things well enough. I'm just wondering if
NOHZ_FULLmakes sense for you sinceNOHZ_FULLmakes system entry/exit so much more expensive.
Ok, I see. It seems a normal use-case that run VMs on system with NOHZ_FULL enabled, and set bandwidth for overcommit. At some times, the cpu will stop tick when low load.
Quoted 4 lines
NOHZ_FULLis for use-cases that 'never' intend to go into the kernel, your use-case actively relies on going into the kernel. Hence the confusion.
In fact, I put a testcase at the end of git message, in which only run a userspace loop workload:
cd /sys/fs/cgroup
echo "+cpu" > cgroup.subtree_control
mkdir test
echo "105000 100000" > test/cpu.max
echo $$ > test/cgroup.procs
taskset -c 1 bash -c "while true; do let i++; done" --> will be throttled
Thanks.
Peter Zijlstra
maintainerQuoted 7 lines · Chengming Zhou
Quoted 3 lines
NOHZ_FULLis for use-cases that 'never' intend to go into the kernel, your use-case actively relies on going into the kernel. Hence the confusion.In fact, I put a testcase at the end of git message, in which only run a userspace loop workload:
Quoted 8 lines
cd
/sys/fs/cgroup
echo "+cpu" > cgroup.subtree_controlmkdir test
echo "105000 100000" > test/cpu.maxecho $$ > test/cgroup.procs
taskset -c 1 bash -c "while true; do let i++; done" --> will be throttled
Ofcourse.. I'm arguing that bandiwdth control and NOHZ_FULL are somewhat mutually exclusive, use-case wise. So I really don't get why you'd want them both.
NOHZ_FULL says, "I 'never' intend to go to the kernel"
bandwidth control says: "I expect to be sharing the system and must be interrupted to not consume too much time", which very much implies: "I will go into the kernel".
The trade-off we make to make NOHZ_FULL work, makes system enter/exit *far* more expensive. There's also people asking to outright kill a task that causes entry under NOHZ_FULL.
So yes, you can configure it, but why does it make sense?
Quoted 15 lines · Peter Zijlstra
Quoted 13 lines · Chengming Zhou
Quoted 3 lines
NOHZ_FULLis for use-cases that 'never' intend to go into the kernel, your use-case actively relies on going into the kernel. Hence the confusion.In fact, I put a testcase at the end of git message, in which only run a userspace loop workload:
cd
/sys/fs/cgroup
echo "+cpu" > cgroup.subtree_controlmkdir test
echo "105000 100000" > test/cpu.max
Quoted 6 lines
Quoted 2 lines
echo $$ > test/cgroup.procs
taskset -c 1 bash -c "while true; do let i++; done" --> will be throttledOfcourse.. I'm arguing that bandiwdth control and
NOHZ_FULLare somewhat mutually exclusive, use-case wise. So I really don't get why you'd want them both.
This problem is found by our VM team, they use bandwidth for overcommit, share CPUs between two VMs.
Quoted 2 lines
NOHZ_FULLsays, "I 'never' intend to go to the kernel"
Like VCPU seldom kvm_exit to the kernel, stop tick is helpful for performance, since kvm_exit is more expensive.
Quoted 4 lines
bandwidth control says: "I expect to be sharing the system and must be interrupted to not consume too much time", which very much implies: "I will go into the kernel".
Yes, agree. If the tasks in the task_group used up quota, they have to go into the kernel to resched out.
Quoted 4 lines
The trade-off we make to make
NOHZ_FULLwork, makes system enter/exit *far* more expensive. There's also people asking to outright kill a task that causes entry underNOHZ_FULL.
It's correct that the task under NOHZ_FULL shouldn't often enter/exit.
Quoted 2 lines
So yes, you can configure it, but why does it make sense?
I don't know if other people have the same use-case, or is there other better way to do VMs overcommit and bandwidth...
Thanks.
Steven Rostedt
reviewerQuoted 6 lines · On Mon, 28 Mar 2022 17:56:07 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 2 lines
echo $$ > test/cgroup.procs
taskset -c 1 bash -c "while true; do let i++; done" --> will be throttledOfcourse.. I'm arguing that bandiwdth control and
NOHZ_FULLare somewhat mutually exclusive, use-case wise. So I really don't get why you'd want them both.
Is it?
One use case I can see for having both is for having a deadline task that needs to get something done in a tight deadline. NOHZ_FULL means "do not interrupt this task when it is the top priority task on the CPU and is running in user space".
Why is it mutually exclusive to have a deadline task that does not want to be interrupted by timer interrupts?
Just because the biggest pushers of NOHZ_FULL is for those that are running RT tasks completely in user space and event want to fault if it ever goes into the kernel, doesn't mean that's the only use case.
Chengming brought up VMs. That's a case to want to control the bandwidth, but also not interrupt them with timer interrupts when they are running as the top priority task on a CPU.
-- Steve
Quoted 10 lines · Steven Rostedt
Quoted 6 lines · On Mon, 28 Mar 2022 17:56:07 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 2 lines
echo $$ > test/cgroup.procs
taskset -c 1 bash -c "while true; do let i++; done" --> will be throttledOfcourse.. I'm arguing that bandiwdth control and
NOHZ_FULLare somewhat mutually exclusive, use-case wise. So I really don't get why you'd want them both.
Quoted 6 lines
Is it?
One use case I can see for having both is for having a deadline task that needs to get something done in a tight deadline.
NOHZ_FULLmeans "do not interrupt this task when it is the top priority task on the CPU and is running in user space".
Yes, this is similar with our use-case.
Quoted 11 lines
Why is it mutually exclusive to have a deadline task that does not want to be interrupted by timer interrupts?
Just because the biggest pushers of
NOHZ_FULLis for those that are running RT tasks completely in user space and event want to fault if it ever goes into the kernel, doesn't mean that's the only use case.Chengming brought up VMs. That's a case to want to control the bandwidth, but also not interrupt them with timer interrupts when they are running as the top priority task on a CPU.
Agree. NOHZ_FULL means don't want timer interrupts when running mostly in user space or guest mode, bandwidth control just means need to go into kernel to schedule out only when its quota used up. We shouldn't make them mutually exclusive.
Thanks.
Quoted 2 lines
-- Steve
Peter Zijlstra
maintainerQuoted 10 lines · Steven Rostedt
Quoted 6 lines · On Mon, 28 Mar 2022 17:56:07 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 2 lines
echo $$ > test/cgroup.procs
taskset -c 1 bash -c "while true; do let i++; done" --> will be throttledOfcourse.. I'm arguing that bandiwdth control and
NOHZ_FULLare somewhat mutually exclusive, use-case wise. So I really don't get why you'd want them both.
Quoted 6 lines
Is it?
One use case I can see for having both is for having a deadline task that needs to get something done in a tight deadline.
NOHZ_FULLmeans "do not interrupt this task when it is the top priority task on the CPU and is running in user space".
This is absolute batshit.. It means no such thing. We'll happily wake another task to this CPU and re-enable the tick any instant.
Worse; the use-case at hand pertains to cfs bandwidth control, which pretty much guarantees there *will* be an interrupt.
Quoted 2 lines
Why is it mutually exclusive to have a deadline task that does not want to be interrupted by timer interrupts?
This has absolutely nothing to do with deadline tasks, nada, noppes.
Quoted 3 lines
Just because the biggest pushers of
NOHZ_FULLis for those that are running RT tasks completely in user space and event want to fault if it ever goes into the kernel, doesn't mean that's the only use case.
Because there's costs associated with the whole thing. system entry/exit get far more expensive. It just doesn't make much sense to use NOHZ_FULL if you're not absoultely limiting system entry.
Quoted 3 lines
Chengming brought up VMs. That's a case to want to control the bandwidth, but also not interrupt them with timer interrupts when they are running as the top priority task on a CPU.
It's CFS, there is nothing top priority about that.
Steven Rostedt
reviewerQuoted 5 lines · On Wed, 30 Mar 2022 20:23:27 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 3 lines
Chengming brought up VMs. That's a case to want to control the bandwidth, but also not interrupt them with timer interrupts when they are running as the top priority task on a CPU.
It's CFS, there is nothing top priority about that.
If there's only one task running on a CPU, even with CFS, why do we need a tick?
Look, we have a host that is doing nothing but running VMs. Could be a single VM. The host doesn't even have disk, it just runs on from initrd, and the user is actually just logging into a guest.
Why should the host be triggering a tick when running this VM, which is the only thing that is running on the host? Yeah, it's CFS, or does CFS have to have a tick? even when there's nothing else on the CPU?
-- Steve
Hi Peter,
Quoted 18 lines · On Wed, Mar 30, 2022 at 08:23:27PM +0200 Peter Zijlstra wrote:
Quoted 10 lines · Steven Rostedt
Quoted 6 lines · On Mon, 28 Mar 2022 17:56:07 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 2 lines
echo $$ > test/cgroup.procs
taskset -c 1 bash -c "while true; do let i++; done" --> will be throttledOfcourse.. I'm arguing that bandiwdth control and
NOHZ_FULLare somewhat mutually exclusive, use-case wise. So I really don't get why you'd want them both.Quoted 6 lines
Is it?
One use case I can see for having both is for having a deadline task that needs to get something done in a tight deadline.
NOHZ_FULLmeans "do not interrupt this task when it is the top priority task on the CPU and is running in user space".
Quoted 5 lines
This is absolute batshit.. It means no such thing. We'll happily wake another task to this CPU and re-enable the tick any instant.
Worse; the use-case at hand pertains to cfs bandwidth control, which pretty much guarantees there *will* be an interrupt.
The problem is (at least in some cases) that container orchestration userspace code allocates a whole CPU by setting quota == period. Or 3 cpus as 3*period etc.
In cases where an isolated task is expected to run uninterrupted (only task in the system affined to that cpu, nohz_full, nocbs etc) you can end up with it getting throttled even though it theoritically has enough bandwidth for the full cpu and therefore should never get throttled.
There are radio network setups where the packet processing is isolated like this but the system as a whole is managed by container orchestration so everything has cfs bandwidth quotas set.
I don't think generally the bandwidth controls in these cases are used for CPU sharing (quota < period). I agree that doesn't make much sense with NOHZ_FULL and won't work right.
It's doled out as full cpu(s) in these cases.
Thats not a VM case so is likely different from the one that started this thread but I thought I should mention it.
Cheers,
Phil
Quoted 20 lines
Quoted 3 lines
Why is it mutually exclusive to have a deadline task that does not want to be interrupted by timer interrupts?
This has absolutely nothing to do with deadline tasks, nada, noppes.
Quoted 3 lines
Just because the biggest pushers of
NOHZ_FULLis for those that are running RT tasks completely in user space and event want to fault if it ever goes into the kernel, doesn't mean that's the only use case.Because there's costs associated with the whole thing. system entry/exit get far more expensive. It just doesn't make much sense to use
NOHZ_FULLif you're not absoultely limiting system entry.Quoted 3 lines
Chengming brought up VMs. That's a case to want to control the bandwidth, but also not interrupt them with timer interrupts when they are running as the top priority task on a CPU.
It's CFS, there is nothing top priority about that.
Hi,
Quoted 39 lines · Phil Auld
Hi Peter,
Quoted 18 lines · On Wed, Mar 30, 2022 at 08:23:27PM +0200 Peter Zijlstra wrote:
Quoted 10 lines · Steven Rostedt
Quoted 6 lines · On Mon, 28 Mar 2022 17:56:07 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 2 lines
echo $$ > test/cgroup.procs
taskset -c 1 bash -c "while true; do let i++; done" --> will be throttledOfcourse.. I'm arguing that bandiwdth control and
NOHZ_FULLare somewhat mutually exclusive, use-case wise. So I really don't get why you'd want them both.Quoted 6 lines
Is it?
One use case I can see for having both is for having a deadline task that needs to get something done in a tight deadline.
NOHZ_FULLmeans "do not interrupt this task when it is the top priority task on the CPU and is running in user space".Quoted 5 lines
This is absolute batshit.. It means no such thing. We'll happily wake another task to this CPU and re-enable the tick any instant.
Worse; the use-case at hand pertains to cfs bandwidth control, which pretty much guarantees there *will* be an interrupt.
The problem is (at least in some cases) that container orchestration userspace code allocates a whole CPU by setting quota == period. Or 3 cpus as 3*period etc.
In cases where an isolated task is expected to run uninterrupted (only task in the system affined to that cpu,
nohz_full, nocbs etc) you can end up with it getting throttled even though it theoritically has enough bandwidth for the full cpu and therefore should never get throttled.There are radio network setups where the packet processing is isolated like this but the system as a whole is managed by container orchestration so everything has cfs bandwidth quotas set.
Quoted 8 lines
I don't think generally the bandwidth controls in these cases are used for CPU sharing (quota < period). I agree that doesn't make much sense with
NOHZ_FULLand won't work right.It's doled out as full cpu(s) in these cases.
Thats not a VM case so is likely different from the one that started this thread but I thought I should mention it.
Yes, it's a different use-case from ours. Thanks for sharing with us. I should put these in the patch log and send an updated version.
Thanks.
Quoted 26 lines
Cheers,
PhilQuoted 20 lines
Quoted 3 lines
Why is it mutually exclusive to have a deadline task that does not want to be interrupted by timer interrupts?
This has absolutely nothing to do with deadline tasks, nada, noppes.
Quoted 3 lines
Just because the biggest pushers of
NOHZ_FULLis for those that are running RT tasks completely in user space and event want to fault if it ever goes into the kernel, doesn't mean that's the only use case.Because there's costs associated with the whole thing. system entry/exit get far more expensive. It just doesn't make much sense to use
NOHZ_FULLif you're not absoultely limiting system entry.Quoted 3 lines
Chengming brought up VMs. That's a case to want to control the bandwidth, but also not interrupt them with timer interrupts when they are running as the top priority task on a CPU.
It's CFS, there is nothing top priority about that.
Benjamin Segall
reviewerQuoted 27 lines · Chengming Zhou
With
nohz_fullenabled on cpu, thescheduler_tick()will be stopped when only one CFS task left on rq.
scheduler_tick()
task_tick_fair()entity_tick() update_curr() account_cfs_rq_runtime(cfs_rq, delta_exec) --> stoppedSo that running task can't account its runtime periodically, but the
cfs_bandwidthhrtimer still__refill_cfs_bandwidth_runtime()periodically. Later in one period, the task would account very bigdelta_exec, which cause thecfs_rqto be throttled for a long time.There are two solutions for the problem, the first is that we can check in
sched_can_stop_tick()if current task'scfs_rqhaveruntime_enabled, in which case we don't stop tick. But it will makenohz_fullalmost useless in cloud environment that every container has the cpu bandwidth control setting.The other is what this patch implemented,
cfs_bandwidthhrtimer would sync unaccounted runtime from all runningcfs_rqswith tick stopped, just before__refill_cfs_bandwidth_runtime(). Also do the same thing intg_set_cfs_bandwidth().
Quoted 7 lines
A testcase to reproduce:
```
cd/sys/fs/cgroup
echo "+cpu" > cgroup.subtree_controlmkdir test
echo "105000 100000" > test/cpu.max
The other half of this problem I think would be that it also won't throttle when it /is/ out of bandwidth. That is, 'echo "50000 100000" > test/cpu.max' would not stop after 50ms of runtime is spent, it would only stop (with this patch) after 100ms. It would then correctly need to repay that debt, so the (very) long-run ratio should be correct, but a misbehaving task could be use practically arbitrarily more than their supposed quota over the ~100s of milliseconds timescales cfsb is generally supposed to work on.
However, I don't really see a way for cfsb's current design to avoid that on nohz_full (without vetoing sched_can_stop_tick). It might be possible to try and guess when quota will run out by tracking the number of running or runnable threads, but that would be a nontrivial undertaking and involve a bunch of hrtimer reprogramming (particularly in cases when some cgroups actually have tasks that wake/sleep frequently).
Quoted 49 lines
echo $$ > test/cgroup.procs
taskset -c 1 bash -c "while true; do let i++; done" ```
Ctrl-C and cat test/cpu.stat to see ifnr_throttled> 0.The above testcase uses period 100ms and quota 105ms, would only see
nr_throttled> 0 onnohz_fullsystem. The problem is gone in test with this patch.Signed-off-by Chengming Zhou <[email protected]>--- kernel/sched/core.c | 4 ++++ kernel/sched/fair.c | 30 ++++++++++++++++++++++++++++++ kernel/sched/sched.h | 3 +++ 3 files changed, 37 insertions(+)kernel/sched/core.c +4
-0@@ -10443,6 +10443,10 @@ static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota,*/ if (runtime_enabled && !runtime_was_enabled)cfs_bandwidth_usage_inc (); + +if (runtime_was_enabled) +sync_cfs_bandwidth_runtime (cfs_b); +raw_spin_lock_irq (&cfs_b->lock); cfs_b->period =ns_to_ktime (period); cfs_b->quota = quota;kernel/sched/fair.c +9
-0@@ -5339,6 +5339,34 @@ static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)return HRTIMER_NORESTART ; } +#ifdef CONFIG_NO_HZ_FULL +void sync_cfs_bandwidth_runtime (struct cfs_bandwidth *cfs_b) +{ +unsigned int cpu; +struct rq *rq; +struct rq_flags rf; +struct cfs_rq *cfs_rq; +struct task_group *tg; +
kernel/sched/fair.c static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) · line 5360
Quoted 10 lines
kernel/sched/fair.c +10
-0+ tg = container_of(cfs_b, struct task_group, cfs_bandwidth); + +for_each_online_cpu (cpu) { +if (!tick_nohz_tick_stopped_cpu (cpu)) +continue ; + + rq =cpu_rq (cpu); + cfs_rq = tg->cfs_rq[cpu]; + +rq_lock_irqsave (rq, &rf);
A for-every-nohz-cpu rqlock is definitely not great when the cgroup is likely to be running on much fewer cpus. I'm not sure what the current opinion would be on doing some sort of optimistic locking like
if (!READ_ONCE(cfs_rq->curr)) continue;rq_lock_irqsave(...);
if (cfs_rq->curr) { ... }
but solutions to the other problem I mentioned earlier might provide help here anyways.
Quoted 38 lines
kernel/sched/fair.c +11
-0+if (cfs_rq->curr) { +update_rq_clock (rq); +update_curr (cfs_rq); + } + rq_unlock_irqrestore(rq, &rf); + } +} +#endif +extern const u64 max_cfs_quota_period;static enum hrtimer_restart sched_cfs_period_timer (struct hrtimer *timer)@@ -5350,6 +5378,8 @@ static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)int idle =0 ;int count =0 ; +sync_cfs_bandwidth_runtime (cfs_b); +raw_spin_lock_irqsave (&cfs_b->lock, flags);for (;;) { overrun =hrtimer_forward_now (timer, cfs_b->period);kernel/sched/sched.h +3
-0@@ -2351,9 +2351,12 @@ static inline void sched_update_tick_dependency(struct rq *rq) else tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED); } + +extern void sync_cfs_bandwidth_runtime (struct cfs_bandwidth *cfs_b);#else static inline int sched_tick_offload_init (void ) {return 0 ; }static inline void sched_update_tick_dependency (struct rq *rq) { } +static inline void sync_cfs_bandwidth_runtime (struct cfs_bandwidth *cfs_b) {}#endif static inline void add_nr_running (struct rq *rq,unsigned count)
Hi,
Quoted 38 lines · Benjamin Segall
Quoted 27 lines · Chengming Zhou
With
nohz_fullenabled on cpu, thescheduler_tick()will be stopped when only one CFS task left on rq.
scheduler_tick()
task_tick_fair()entity_tick() update_curr() account_cfs_rq_runtime(cfs_rq, delta_exec) --> stoppedSo that running task can't account its runtime periodically, but the
cfs_bandwidthhrtimer still__refill_cfs_bandwidth_runtime()periodically. Later in one period, the task would account very bigdelta_exec, which cause thecfs_rqto be throttled for a long time.There are two solutions for the problem, the first is that we can check in
sched_can_stop_tick()if current task'scfs_rqhaveruntime_enabled, in which case we don't stop tick. But it will makenohz_fullalmost useless in cloud environment that every container has the cpu bandwidth control setting.The other is what this patch implemented,
cfs_bandwidthhrtimer would sync unaccounted runtime from all runningcfs_rqswith tick stopped, just before__refill_cfs_bandwidth_runtime(). Also do the same thing intg_set_cfs_bandwidth().Quoted 7 lines
A testcase to reproduce:
```
cd/sys/fs/cgroup
echo "+cpu" > cgroup.subtree_controlmkdir test
echo "105000 100000" > test/cpu.max
Quoted 8 lines
The other half of this problem I think would be that it also won't throttle when it /is/ out of bandwidth. That is, 'echo "50000 100000" > test/cpu.max' would not stop after 50ms of runtime is spent, it would only stop (with this patch) after 100ms. It would then correctly need to repay that debt, so the (very) long-run ratio should be correct, but a misbehaving task could be use practically arbitrarily more than their supposed quota over the ~100s of milliseconds timescales cfsb is generally supposed to work on.
Right, it's a problem with this patch's solution. I can't think of a better way to make NOHZ_FULL work with bandwidth control. But it should be no problem for most use-case? If it used over quota, it would need to repay that debt before it can run again, so it just misbehave in that period.
The main problem this patch solved is that task accumulated a very long delta_exec accounted in one period, then cause very long time throttled.
Quoted 73 lines
However, I don't really see a way for cfsb's current design to avoid that on
nohz_full(without vetoingsched_can_stop_tick). It might be possible to try and guess when quota will run out by tracking the number of running or runnable threads, but that would be a nontrivial undertaking and involve a bunch of hrtimer reprogramming (particularly in cases when some cgroups actually have tasks that wake/sleep frequently).Quoted 49 lines
echo $$ > test/cgroup.procs
taskset -c 1 bash -c "while true; do let i++; done" ```
Ctrl-C and cat test/cpu.stat to see ifnr_throttled> 0.The above testcase uses period 100ms and quota 105ms, would only see
nr_throttled> 0 onnohz_fullsystem. The problem is gone in test with this patch.Signed-off-by Chengming Zhou <[email protected]>--- kernel/sched/core.c | 4 ++++ kernel/sched/fair.c | 30 ++++++++++++++++++++++++++++++ kernel/sched/sched.h | 3 +++ 3 files changed, 37 insertions(+)kernel/sched/core.c +4
-0@@ -10443,6 +10443,10 @@ static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota,*/ if (runtime_enabled && !runtime_was_enabled)cfs_bandwidth_usage_inc (); + +if (runtime_was_enabled) +sync_cfs_bandwidth_runtime (cfs_b); +raw_spin_lock_irq (&cfs_b->lock); cfs_b->period =ns_to_ktime (period); cfs_b->quota = quota;kernel/sched/fair.c +9
-0@@ -5339,6 +5339,34 @@ static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)return HRTIMER_NORESTART ; } +#ifdef CONFIG_NO_HZ_FULL +void sync_cfs_bandwidth_runtime (struct cfs_bandwidth *cfs_b) +{ +unsigned int cpu; +struct rq *rq; +struct rq_flags rf; +struct cfs_rq *cfs_rq; +struct task_group *tg; +kernel/sched/fair.c static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) · line 5360
Quoted 10 lines
kernel/sched/fair.c +10
-0+ tg = container_of(cfs_b, struct task_group, cfs_bandwidth); + +for_each_online_cpu (cpu) { +if (!tick_nohz_tick_stopped_cpu (cpu)) +continue ; + + rq =cpu_rq (cpu); + cfs_rq = tg->cfs_rq[cpu]; + +rq_lock_irqsave (rq, &rf);A for-every-nohz-cpu rqlock is definitely not great when the cgroup is likely to be running on much fewer cpus. I'm not sure what the current opinion would be on doing some sort of optimistic locking like
Quoted 3 lines
if (!READ_ONCE(cfs_rq->curr)) continue;
rq_lock_irqsave(...);
if (cfs_rq->curr) { ... }
I think it's better, will changed to this if Peter is ok with this patch.
Thanks.
Quoted 43 lines
but solutions to the other problem I mentioned earlier might provide help here anyways.
Quoted 38 lines
kernel/sched/fair.c +11
-0+if (cfs_rq->curr) { +update_rq_clock (rq); +update_curr (cfs_rq); + } + rq_unlock_irqrestore(rq, &rf); + } +} +#endif +extern const u64 max_cfs_quota_period;static enum hrtimer_restart sched_cfs_period_timer (struct hrtimer *timer)@@ -5350,6 +5378,8 @@ static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)int idle =0 ;int count =0 ; +sync_cfs_bandwidth_runtime (cfs_b); +raw_spin_lock_irqsave (&cfs_b->lock, flags);for (;;) { overrun =hrtimer_forward_now (timer, cfs_b->period);kernel/sched/sched.h +3
-0@@ -2351,9 +2351,12 @@ static inline void sched_update_tick_dependency(struct rq *rq) else tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED); } + +extern void sync_cfs_bandwidth_runtime (struct cfs_bandwidth *cfs_b);#else static inline int sched_tick_offload_init (void ) {return 0 ; }static inline void sched_update_tick_dependency (struct rq *rq) { } +static inline void sync_cfs_bandwidth_runtime (struct cfs_bandwidth *cfs_b) {}#endif static inline void add_nr_running (struct rq *rq,unsigned count)