Skip to contentSkip to conversation

Fix nohz_full vs cfs bandwidth

PATCH Superseded Phil Auld 3 participants 12 messages Jul 7, 2023 – Jul 11, 2023 Scheduler

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Phil Auld

author
#0 lore raw

v5 cover Fix nohz_full vs cfs bandwidth

This is v5 of patch 2/2 which is adding code to prevent the tick from being stopped when the single running task has bandwidth limits. Discussions had led to the idea of adding a bit to task_struct to help make this decision.

There was some complexity with doing it in the task which is avoided by using something in the cfs_rq. Looking into that lead me to the hierarchical_quota field in the cfs_bandwith struct. We spend a good deal of effort updating (or trying to, see patch 1/2) that value for the whole task_group tree when a quota is set/changed.

This new version first fixes that value to be meaningful for cgroupv2 and then leverages it to make the decisions about blocking the tick_stop.

Phil Auld (2):
sched, cgroup: Restore meaning to hierarchical_quota Sched/fair: Block nohz tick_stop when cfs bandwidth in use

 kernel/sched/core.c     | 23 ++++++++++++++---
 kernel/sched/fair.c     | 56 ++++++++++++++++++++++++++++++++++++++---
 kernel/sched/features.h |  2 ++
 kernel/sched/sched.h    |  3 ++-
 4 files changed, 76 insertions(+), 8 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v5 1/2 sched, cgroup: Restore meaning to hierarchical_quota

In cgroupv2 cfs_b->hierarchical_quota is set to -1 for all task groups due to the previous fix simply taking the min. It should reflect a limit imposed at that level or by an ancestor. Even though cgroupv2 does not require child quota to be less than or equal to that of its ancestors the task group will still be constrained by such a quota so this should be shown here. Cgroupv1 continues to set this correctly.

In both cases, add initialization when a new task group is created based on the current parent's value (or RUNTIME_INF in the case of root_task_group). Otherwise, the field is wrong until a quota is changed after creation and __cfs_schedulable() is called.

Fixes c53593e5cb69 ("sched, cgroup: Don't reject lower cpu.max on ancestors")
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Valentin Schneider <[email protected]>
Cc Ben Segall <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Tejun Heo <[email protected]>
---
 kernel/sched/core.c  | 11 +++++++----
 kernel/sched/fair.c  |  7 ++++---
 kernel/sched/sched.h |  2 +-
 3 files changed, 12 insertions(+), 8 deletions(-)
kernel/sched/core.c +7 -4
index a68d1276bab0..1b214e10c25d 100644
@@ -9904,7 +9904,7 @@ void __init sched_init(void)
 		ptr += nr_cpu_ids * sizeof(void **);
 
 		root_task_group.shares = ROOT_TASK_GROUP_LOAD;
-		init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
+		init_cfs_bandwidth(&root_task_group.cfs_bandwidth, NULL);
 #endif /* CONFIG_FAIR_GROUP_SCHED */
 #ifdef CONFIG_RT_GROUP_SCHED
 		root_task_group.rt_se = (struct sched_rt_entity **)ptr;
@@ -11038,11 +11038,14 @@ static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
 
 		/*
 		 * Ensure max(child_quota) <= parent_quota.  On cgroup2,
-		 * always take the min.  On cgroup1, only inherit when no
-		 * limit is set:
+		 * always take the non-RUNTIME_INF min.  On cgroup1, only
+		 * inherit when no limit is set:
 		 */
 		if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) {
-			quota = min(quota, parent_quota);
+			if (quota == RUNTIME_INF)
+				quota = parent_quota;
+			else if (parent_quota != RUNTIME_INF)
+				quota = min(quota, parent_quota);
 		} else {
 			if (quota == RUNTIME_INF)
 				quota = parent_quota;
kernel/sched/fair.c +4 -3
index 373ff5f55884..92381f9ecf37 100644
@@ -6005,13 +6005,14 @@ static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
 	return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
 }
 
-void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
+void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent)
 {
 	raw_spin_lock_init(&cfs_b->lock);
 	cfs_b->runtime = 0;
 	cfs_b->quota = RUNTIME_INF;
 	cfs_b->period = ns_to_ktime(default_cfs_period());
 	cfs_b->burst = 0;
+	cfs_b->hierarchical_quota = ((parent) ? parent->hierarchical_quota : RUNTIME_INF);
 
 	INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
 	hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
@@ -6168,7 +6169,7 @@ static inline int throttled_lb_pair(struct task_group *tg,
 	return 0;
 }
 
-void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
+void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent) {}
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
 static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
@@ -12373,7 +12374,7 @@ int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
 
 	tg->shares = NICE_0_LOAD;
 
-	init_cfs_bandwidth(tg_cfs_bandwidth(tg));
+	init_cfs_bandwidth(tg_cfs_bandwidth(tg), tg_cfs_bandwidth(parent));
 
 	for_each_possible_cpu(i) {
 		cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
kernel/sched/sched.h +1 -1
index ec7b3e0a2b20..63822c9238cc 100644
@@ -460,7 +460,7 @@ extern void unregister_fair_sched_group(struct task_group *tg);
 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
 			struct sched_entity *se, int cpu,
 			struct sched_entity *parent);
-extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
+extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent);
 
 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH v5 1/2] sched, cgroup: Restore meaning to hierarchical_quota

Hello,

Quoted 5 lines · Phil Auld
kernel/sched/core.c +0 -1
@@ -11038,11 +11038,14 @@ static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
 
 		/*
 		 * Ensure max(child_quota) <= parent_quota.  On cgroup2,
-		 * always take the min.  On cgroup1, only inherit when no

kernel/sched/core.c static int tg_cfs_schedulable_down(struct task_group *tg, void *data) · line 11048

Quoted 10 lines
kernel/sched/core.c +6 -2
-		 * limit is set:
+		 * always take the non-RUNTIME_INF min.  On cgroup1, only
+		 * inherit when no limit is set:
 		 */
 		if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) {
-			quota = min(quota, parent_quota);
+			if (quota == RUNTIME_INF)
+				quota = parent_quota;
+			else if (parent_quota != RUNTIME_INF)
+				quota = min(quota, parent_quota);

Can you please add a comment explaining how hierarchical_quota is used in cgroup2? It seems like you're using it to detect whether bw limit is enforced for a given cgroup, which is fine, but the above code in isolation looks a bit curious because it doesn't serve any purpose by itself.

Thanks.

Signature · 1 line
tejun
author
#3 lore raw

Re: [PATCH v5 1/2] sched, cgroup: Restore meaning to hierarchical_quota

Quoted 15 lines · On Mon, Jul 10, 2023 at 10:17:08AM -1000 Tejun Heo wrote:

Hello,

Quoted 12 lines · Phil Auld
kernel/sched/core.c +3 -3
@@ -11038,11 +11038,14 @@ static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
 
 		/*
 		 * Ensure max(child_quota) <= parent_quota.  On cgroup2,
-		 * always take the min.  On cgroup1, only inherit when no
-		 * limit is set:
+		 * always take the non-RUNTIME_INF min.  On cgroup1, only
+		 * inherit when no limit is set:
 		 */
 		if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) {
-			quota = min(quota, parent_quota);
+			if (quota == RUNTIME_INF)
Quoted 8 lines

kernel/sched/core.c static int tg_cfs_schedulable_down(struct task_group *tg, void *data) · line 11048

Quoted 3 lines
kernel/sched/core.c +3 -0
+				quota = parent_quota;
+			else if (parent_quota != RUNTIME_INF)
+				quota = min(quota, parent_quota);

Can you please add a comment explaining how hierarchical_quota is used in cgroup2? It seems like you're using it to detect whether bw limit is enforced for a given cgroup, which is fine, but the above code in isolation looks a bit curious because it doesn't serve any purpose by itself.

Sure, I can do that.

Cheers,
Phil

Quoted 6 lines

Thanks.

Signature · 1 line
tejun
reviewer Reviewed-by on v5 1/2
#4 lore raw

Re: [PATCH v5 1/2] sched, cgroup: Restore meaning to hierarchical_quota

In reply to Phil Auld
Quoted 8 lines · Phil Auld

In cgroupv2 cfs_b->hierarchical_quota is set to -1 for all task groups due to the previous fix simply taking the min. It should reflect a limit imposed at that level or by an ancestor. Even though cgroupv2 does not require child quota to be less than or equal to that of its ancestors the task group will still be constrained by such a quota so this should be shown here. Cgroupv1 continues to set this correctly.

Quoted 4 lines

In both cases, add initialization when a new task group is created based on the current parent's value (or RUNTIME_INF in the case of root_task_group). Otherwise, the field is wrong until a quota is changed after creation and __cfs_schedulable() is called.

Quoted 12 lines
kernel/sched/core.c +2 -2
index a68d1276bab0..1b214e10c25d 100644
@@ -11038,11 +11038,14 @@ static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
 
 		/*
 		 * Ensure max(child_quota) <= parent_quota.  On cgroup2,
-		 * always take the min.  On cgroup1, only inherit when no
-		 * limit is set:
+		 * always take the non-RUNTIME_INF min.  On cgroup1, only
+		 * inherit when no limit is set:

kernel/sched/core.c static int tg_cfs_schedulable_down(struct task_group *tg, void *data) · line 11051

Quoted 10 lines
kernel/sched/core.c +4 -1
 		 */
 		if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) {
-			quota = min(quota, parent_quota);
+			if (quota == RUNTIME_INF)
+				quota = parent_quota;
+			else if (parent_quota != RUNTIME_INF)
+				quota = min(quota, parent_quota);
 		} else {
 			if (quota == RUNTIME_INF)
 				quota = parent_quota;

I suppose you could also set RUNTIME_INF to be a positive value or better yet just compare at unsigned, but it's not like config needs to be fast, so no need to mess with that.

Quoted 7 lines
kernel/sched/fair.c +0 -0
index 373ff5f55884..92381f9ecf37 100644
@@ -6005,13 +6005,14 @@ static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
 	return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
 }

kernel/sched/fair.c static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) · line 6015

Quoted 10 lines
kernel/sched/fair.c +2 -1
 
-void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
+void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent)
 {
 	raw_spin_lock_init(&cfs_b->lock);
 	cfs_b->runtime = 0;
 	cfs_b->quota = RUNTIME_INF;
 	cfs_b->period = ns_to_ktime(default_cfs_period());
 	cfs_b->burst = 0;
+	cfs_b->hierarchical_quota = ((parent) ? parent->hierarchical_quota : RUNTIME_INF);

Minor style nit: don't need any of these parens here.

author
#5 lore raw

Re: [PATCH v5 1/2] sched, cgroup: Restore meaning to hierarchical_quota

Quoted 10 lines · On Mon, Jul 10, 2023 at 05:00:11PM -0700 Benjamin Segall wrote:
Quoted 8 lines · Phil Auld

In cgroupv2 cfs_b->hierarchical_quota is set to -1 for all task groups due to the previous fix simply taking the min. It should reflect a limit imposed at that level or by an ancestor. Even though cgroupv2 does not require child quota to be less than or equal to that of its ancestors the task group will still be constrained by such a quota so this should be shown here. Cgroupv1 continues to set this correctly.

Quoted 7 lines
Quoted 4 lines

In both cases, add initialization when a new task group is created based on the current parent's value (or RUNTIME_INF in the case of root_task_group). Otherwise, the field is wrong until a quota is changed after creation and __cfs_schedulable() is called.

Thanks, I'll hold on to this for the next version where I update the comment if that's okay. I was just going to send that but based on your comment on patch 2 may just do a v6 of the whole thing.

Cheers,
Phil

Quoted 47 lines
Quoted 12 lines
kernel/sched/core.c +2 -2
index a68d1276bab0..1b214e10c25d 100644
@@ -11038,11 +11038,14 @@ static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
 
 		/*
 		 * Ensure max(child_quota) <= parent_quota.  On cgroup2,
-		 * always take the min.  On cgroup1, only inherit when no
-		 * limit is set:
+		 * always take the non-RUNTIME_INF min.  On cgroup1, only
+		 * inherit when no limit is set:

kernel/sched/core.c static int tg_cfs_schedulable_down(struct task_group *tg, void *data) · line 11051

Quoted 10 lines
kernel/sched/core.c +4 -1
 		 */
 		if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) {
-			quota = min(quota, parent_quota);
+			if (quota == RUNTIME_INF)
+				quota = parent_quota;
+			else if (parent_quota != RUNTIME_INF)
+				quota = min(quota, parent_quota);
 		} else {
 			if (quota == RUNTIME_INF)
 				quota = parent_quota;

I suppose you could also set RUNTIME_INF to be a positive value or better yet just compare at unsigned, but it's not like config needs to be fast, so no need to mess with that.

Quoted 7 lines
kernel/sched/fair.c +0 -0
index 373ff5f55884..92381f9ecf37 100644
@@ -6005,13 +6005,14 @@ static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
 	return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
 }

kernel/sched/fair.c static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) · line 6015

Quoted 10 lines
kernel/sched/fair.c +2 -1
 
-void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
+void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent)
 {
 	raw_spin_lock_init(&cfs_b->lock);
 	cfs_b->runtime = 0;
 	cfs_b->quota = RUNTIME_INF;
 	cfs_b->period = ns_to_ktime(default_cfs_period());
 	cfs_b->burst = 0;
+	cfs_b->hierarchical_quota = ((parent) ? parent->hierarchical_quota : RUNTIME_INF);

Minor style nit: don't need any of these parens here.

author
#6 lore raw

PATCH v5 2/2 Sched/fair: Block nohz tick_stop when cfs bandwidth in use

In reply to Phil Auld

CFS bandwidth limits and NOHZ full don't play well together. Tasks can easily run well past their quotas before a remote tick does accounting. This leads to long, multi-period stalls before such tasks can run again. Currently, when presented with these conflicting requirements the scheduler is favoring nohz_full and letting the tick be stopped. However, nohz tick stopping is already best-effort, there are a number of conditions that can prevent it, whereas cfs runtime bandwidth is expected to be enforced.

Make the scheduler favor bandwidth over stopping the tick by setting TICK_DEP_BIT_SCHED when the only running task is a cfs task with runtime limit enabled. We use cfs_b->hierarchical_quota to determine if the task requires the tick.

Add check in pick_next_task_fair() as well since that is where we have a handle on the task that is actually going to be running.

Add check in sched_can_stop_tick() to cover some edge cases such as nr_running going from 2->1 and the 1 remains the running task.

Add sched_feat HZ_BW (off by default) to control the tick_stop behavior.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Valentin Schneider <[email protected]>
Cc Ben Segall <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 kernel/sched/core.c     | 12 ++++++++++
 kernel/sched/fair.c     | 49 +++++++++++++++++++++++++++++++++++++++++
 kernel/sched/features.h |  2 ++
 kernel/sched/sched.h    |  1 +
 4 files changed, 64 insertions(+)
kernel/sched/core.c +12 -0
index 1b214e10c25d..4b8534abdf4f 100644
@@ -1229,6 +1229,18 @@ bool sched_can_stop_tick(struct rq *rq)
 	if (rq->nr_running > 1)
 		return false;
 
+	/*
+	 * If there is one task and it has CFS runtime bandwidth constraints
+	 * and it's on the cpu now we don't want to stop the tick.
+	 * This check prevents clearing the bit if a newly enqueued task here is
+	 * dequeued by migrating while the constrained task continues to run.
+	 * E.g. going from 2->1 without going through pick_next_task().
+	 */
+	if (sched_feat(HZ_BW) && rq->nr_running == 1 && task_on_rq_queued(rq->curr)) {
+		if (cfs_task_bw_constrained(rq->curr))
+			return false;
+	}
+
 	return true;
 }
 #endif /* CONFIG_NO_HZ_FULL */
kernel/sched/fair.c +49 -0

70 lines not on this page · raw message

kernel/sched/features.h +2 -0
index ee7f23c76bd3..6fdf1fdf6b17 100644
@@ -101,3 +101,5 @@ SCHED_FEAT(LATENCY_WARN, false)
 
 SCHED_FEAT(ALT_PERIOD, true)
 SCHED_FEAT(BASE_SLICE, true)
+
+SCHED_FEAT(HZ_BW, false)
kernel/sched/sched.h +1 -0
index 63822c9238cc..d6d346bc78aa 100644
@@ -465,6 +465,7 @@ extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth
 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
+extern bool cfs_task_bw_constrained(struct task_struct *p);
 
 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
 		struct sched_rt_entity *rt_se, int cpu,
Signature · 1 line
2.31.1
#7 lore raw

Re: [PATCH v5 2/2] Sched/fair: Block nohz tick_stop when cfs bandwidth in use

Quoted 49 lines · Phil Auld

CFS bandwidth limits and NOHZ full don't play well together. Tasks can easily run well past their quotas before a remote tick does accounting. This leads to long, multi-period stalls before such tasks can run again. Currently, when presented with these conflicting requirements the scheduler is favoring nohz_full and letting the tick be stopped. However, nohz tick stopping is already best-effort, there are a number of conditions that can prevent it, whereas cfs runtime bandwidth is expected to be enforced.

Make the scheduler favor bandwidth over stopping the tick by setting TICK_DEP_BIT_SCHED when the only running task is a cfs task with runtime limit enabled. We use cfs_b->hierarchical_quota to determine if the task requires the tick.

Add check in pick_next_task_fair() as well since that is where we have a handle on the task that is actually going to be running.

Add check in sched_can_stop_tick() to cover some edge cases such as nr_running going from 2->1 and the 1 remains the running task.

Add sched_feat HZ_BW (off by default) to control the tick_stop behavior.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Valentin Schneider <[email protected]>
Cc Ben Segall <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 kernel/sched/core.c     | 12 ++++++++++
 kernel/sched/fair.c     | 49 +++++++++++++++++++++++++++++++++++++++++
 kernel/sched/features.h |  2 ++
 kernel/sched/sched.h    |  1 +
 4 files changed, 64 insertions(+)
kernel/sched/core.c +2 -0
index 1b214e10c25d..4b8534abdf4f 100644
@@ -1229,6 +1229,18 @@ bool sched_can_stop_tick(struct rq *rq)
 	if (rq->nr_running > 1)
 		return false;
 
+	/*
+	 * If there is one task and it has CFS runtime bandwidth constraints

kernel/sched/core.c bool sched_can_stop_tick(struct rq *rq) · line 1243

Quoted 10 lines
kernel/sched/core.c +10 -0
+	 * and it's on the cpu now we don't want to stop the tick.
+	 * This check prevents clearing the bit if a newly enqueued task here is
+	 * dequeued by migrating while the constrained task continues to run.
+	 * E.g. going from 2->1 without going through pick_next_task().
+	 */
+	if (sched_feat(HZ_BW) && rq->nr_running == 1 && task_on_rq_queued(rq->curr)) {
+		if (cfs_task_bw_constrained(rq->curr))
+			return false;
+	}
+

I think we still need the fair_sched_class check with the bit being on cfs_rq/tg rather than task.

author
#8 lore raw

Re: [PATCH v5 2/2] Sched/fair: Block nohz tick_stop when cfs bandwidth in use

Quoted 49 lines · On Mon, Jul 10, 2023 at 04:54:58PM -0700 Benjamin Segall wrote:
Quoted 47 lines · Phil Auld

CFS bandwidth limits and NOHZ full don't play well together. Tasks can easily run well past their quotas before a remote tick does accounting. This leads to long, multi-period stalls before such tasks can run again. Currently, when presented with these conflicting requirements the scheduler is favoring nohz_full and letting the tick be stopped. However, nohz tick stopping is already best-effort, there are a number of conditions that can prevent it, whereas cfs runtime bandwidth is expected to be enforced.

Make the scheduler favor bandwidth over stopping the tick by setting TICK_DEP_BIT_SCHED when the only running task is a cfs task with runtime limit enabled. We use cfs_b->hierarchical_quota to determine if the task requires the tick.

Add check in pick_next_task_fair() as well since that is where we have a handle on the task that is actually going to be running.

Add check in sched_can_stop_tick() to cover some edge cases such as nr_running going from 2->1 and the 1 remains the running task.

Add sched_feat HZ_BW (off by default) to control the tick_stop behavior.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Valentin Schneider <[email protected]>
Cc Ben Segall <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 kernel/sched/core.c     | 12 ++++++++++
 kernel/sched/fair.c     | 49 +++++++++++++++++++++++++++++++++++++++++
 kernel/sched/features.h |  2 ++
 kernel/sched/sched.h    |  1 +
 4 files changed, 64 insertions(+)
kernel/sched/core.c +0 -0
index 1b214e10c25d..4b8534abdf4f 100644
@@ -1229,6 +1229,18 @@ bool sched_can_stop_tick(struct rq *rq)
 	if (rq->nr_running > 1)
 		return false;
 
Quoted 16 lines

kernel/sched/core.c bool sched_can_stop_tick(struct rq *rq) · line 1243

Quoted 12 lines
kernel/sched/core.c +12 -0
+	/*
+	 * If there is one task and it has CFS runtime bandwidth constraints
+	 * and it's on the cpu now we don't want to stop the tick.
+	 * This check prevents clearing the bit if a newly enqueued task here is
+	 * dequeued by migrating while the constrained task continues to run.
+	 * E.g. going from 2->1 without going through pick_next_task().
+	 */
+	if (sched_feat(HZ_BW) && rq->nr_running == 1 && task_on_rq_queued(rq->curr)) {
+		if (cfs_task_bw_constrained(rq->curr))
+			return false;
+	}
+

I think we still need the fair_sched_class check with the bit being on cfs_rq/tg rather than task.

Is there a way a non-fair_sched_class task will be in a cfs_rq with cfs_rq->runtime_enabled and/or cfs_b->hierarchical_quota set to non RUNTIME_INF? I suppose if they are stale and it's had its class changed?

That makes the condition pretty ugly but I can add that back if needed.

Thanks,
Phil

author
#9 lore raw

Re: [PATCH v5 2/2] Sched/fair: Block nohz tick_stop when cfs bandwidth in use

Quoted 67 lines · On Tue, Jul 11, 2023 at 09:10:24AM -0400 Phil Auld wrote:
Quoted 49 lines · On Mon, Jul 10, 2023 at 04:54:58PM -0700 Benjamin Segall wrote:
Quoted 47 lines · Phil Auld

CFS bandwidth limits and NOHZ full don't play well together. Tasks can easily run well past their quotas before a remote tick does accounting. This leads to long, multi-period stalls before such tasks can run again. Currently, when presented with these conflicting requirements the scheduler is favoring nohz_full and letting the tick be stopped. However, nohz tick stopping is already best-effort, there are a number of conditions that can prevent it, whereas cfs runtime bandwidth is expected to be enforced.

Make the scheduler favor bandwidth over stopping the tick by setting TICK_DEP_BIT_SCHED when the only running task is a cfs task with runtime limit enabled. We use cfs_b->hierarchical_quota to determine if the task requires the tick.

Add check in pick_next_task_fair() as well since that is where we have a handle on the task that is actually going to be running.

Add check in sched_can_stop_tick() to cover some edge cases such as nr_running going from 2->1 and the 1 remains the running task.

Add sched_feat HZ_BW (off by default) to control the tick_stop behavior.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Valentin Schneider <[email protected]>
Cc Ben Segall <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 kernel/sched/core.c     | 12 ++++++++++
 kernel/sched/fair.c     | 49 +++++++++++++++++++++++++++++++++++++++++
 kernel/sched/features.h |  2 ++
 kernel/sched/sched.h    |  1 +
 4 files changed, 64 insertions(+)
kernel/sched/core.c +0 -0
index 1b214e10c25d..4b8534abdf4f 100644
@@ -1229,6 +1229,18 @@ bool sched_can_stop_tick(struct rq *rq)
 	if (rq->nr_running > 1)
 		return false;
 
Quoted 16 lines

kernel/sched/core.c bool sched_can_stop_tick(struct rq *rq) · line 1243

Quoted 12 lines
kernel/sched/core.c +12 -0
+	/*
+	 * If there is one task and it has CFS runtime bandwidth constraints
+	 * and it's on the cpu now we don't want to stop the tick.
+	 * This check prevents clearing the bit if a newly enqueued task here is
+	 * dequeued by migrating while the constrained task continues to run.
+	 * E.g. going from 2->1 without going through pick_next_task().
+	 */
+	if (sched_feat(HZ_BW) && rq->nr_running == 1 && task_on_rq_queued(rq->curr)) {
+		if (cfs_task_bw_constrained(rq->curr))
+			return false;
+	}
+

I think we still need the fair_sched_class check with the bit being on cfs_rq/tg rather than task.

Quoted 6 lines

Is there a way a non-fair_sched_class task will be in a cfs_rq with cfs_rq->runtime_enabled and/or cfs_b->hierarchical_quota set to non RUNTIME_INF? I suppose if they are stale and it's had its class changed?

That makes the condition pretty ugly but I can add that back if needed.

Sigh, yeah. I took that out when I had the bit in the task. I'll put it back in...

Cheers,
Phil

Quoted 8 lines

Thanks,
Phil

#10 lore raw

Re: [PATCH v5 2/2] Sched/fair: Block nohz tick_stop when cfs bandwidth in use

Quoted 72 lines · Phil Auld
Quoted 71 lines · On Tue, Jul 11, 2023 at 09:10:24AM -0400 Phil Auld wrote:
Quoted 49 lines · On Mon, Jul 10, 2023 at 04:54:58PM -0700 Benjamin Segall wrote:
Quoted 47 lines · Phil Auld

CFS bandwidth limits and NOHZ full don't play well together. Tasks can easily run well past their quotas before a remote tick does accounting. This leads to long, multi-period stalls before such tasks can run again. Currently, when presented with these conflicting requirements the scheduler is favoring nohz_full and letting the tick be stopped. However, nohz tick stopping is already best-effort, there are a number of conditions that can prevent it, whereas cfs runtime bandwidth is expected to be enforced.

Make the scheduler favor bandwidth over stopping the tick by setting TICK_DEP_BIT_SCHED when the only running task is a cfs task with runtime limit enabled. We use cfs_b->hierarchical_quota to determine if the task requires the tick.

Add check in pick_next_task_fair() as well since that is where we have a handle on the task that is actually going to be running.

Add check in sched_can_stop_tick() to cover some edge cases such as nr_running going from 2->1 and the 1 remains the running task.

Add sched_feat HZ_BW (off by default) to control the tick_stop behavior.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Valentin Schneider <[email protected]>
Cc Ben Segall <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 kernel/sched/core.c     | 12 ++++++++++
 kernel/sched/fair.c     | 49 +++++++++++++++++++++++++++++++++++++++++
 kernel/sched/features.h |  2 ++
 kernel/sched/sched.h    |  1 +
 4 files changed, 64 insertions(+)
kernel/sched/core.c +0 -0
index 1b214e10c25d..4b8534abdf4f 100644
@@ -1229,6 +1229,18 @@ bool sched_can_stop_tick(struct rq *rq)
 	if (rq->nr_running > 1)
 		return false;
 
Quoted 16 lines

kernel/sched/core.c bool sched_can_stop_tick(struct rq *rq) · line 1243

Quoted 12 lines
kernel/sched/core.c +12 -0
+	/*
+	 * If there is one task and it has CFS runtime bandwidth constraints
+	 * and it's on the cpu now we don't want to stop the tick.
+	 * This check prevents clearing the bit if a newly enqueued task here is
+	 * dequeued by migrating while the constrained task continues to run.
+	 * E.g. going from 2->1 without going through pick_next_task().
+	 */
+	if (sched_feat(HZ_BW) && rq->nr_running == 1 && task_on_rq_queued(rq->curr)) {
+		if (cfs_task_bw_constrained(rq->curr))
+			return false;
+	}
+

I think we still need the fair_sched_class check with the bit being on cfs_rq/tg rather than task.

Is there a way a non-fair_sched_class task will be in a cfs_rq with cfs_rq->runtime_enabled and/or cfs_b->hierarchical_quota set to non RUNTIME_INF? I suppose if they are stale and it's had its class changed?

Quoted 6 lines
Quoted 2 lines

That makes the condition pretty ugly but I can add that back if needed.

Sigh, yeah. I took that out when I had the bit in the task. I'll put it back in...

Yeah, cfs_rq (and rt_rq) are set unconditionally, and a cgroup can have a mix of fair and RT tasks (whether or not that's a good idea from a sysadmin perspective).

author
#11 lore raw

Re: [PATCH v5 2/2] Sched/fair: Block nohz tick_stop when cfs bandwidth in use

Quoted 77 lines · On Tue, Jul 11, 2023 at 03:07:12PM -0700 Benjamin Segall wrote:
Quoted 75 lines · Phil Auld
Quoted 73 lines · On Tue, Jul 11, 2023 at 09:10:24AM -0400 Phil Auld wrote:
Quoted 49 lines · On Mon, Jul 10, 2023 at 04:54:58PM -0700 Benjamin Segall wrote:
Quoted 47 lines · Phil Auld

CFS bandwidth limits and NOHZ full don't play well together. Tasks can easily run well past their quotas before a remote tick does accounting. This leads to long, multi-period stalls before such tasks can run again. Currently, when presented with these conflicting requirements the scheduler is favoring nohz_full and letting the tick be stopped. However, nohz tick stopping is already best-effort, there are a number of conditions that can prevent it, whereas cfs runtime bandwidth is expected to be enforced.

Make the scheduler favor bandwidth over stopping the tick by setting TICK_DEP_BIT_SCHED when the only running task is a cfs task with runtime limit enabled. We use cfs_b->hierarchical_quota to determine if the task requires the tick.

Add check in pick_next_task_fair() as well since that is where we have a handle on the task that is actually going to be running.

Add check in sched_can_stop_tick() to cover some edge cases such as nr_running going from 2->1 and the 1 remains the running task.

Add sched_feat HZ_BW (off by default) to control the tick_stop behavior.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Valentin Schneider <[email protected]>
Cc Ben Segall <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 kernel/sched/core.c     | 12 ++++++++++
 kernel/sched/fair.c     | 49 +++++++++++++++++++++++++++++++++++++++++
 kernel/sched/features.h |  2 ++
 kernel/sched/sched.h    |  1 +
 4 files changed, 64 insertions(+)
kernel/sched/core.c +0 -0
index 1b214e10c25d..4b8534abdf4f 100644
@@ -1229,6 +1229,18 @@ bool sched_can_stop_tick(struct rq *rq)
 	if (rq->nr_running > 1)
 		return false;
 
Quoted 16 lines

kernel/sched/core.c bool sched_can_stop_tick(struct rq *rq) · line 1243

Quoted 12 lines
kernel/sched/core.c +12 -0
+	/*
+	 * If there is one task and it has CFS runtime bandwidth constraints
+	 * and it's on the cpu now we don't want to stop the tick.
+	 * This check prevents clearing the bit if a newly enqueued task here is
+	 * dequeued by migrating while the constrained task continues to run.
+	 * E.g. going from 2->1 without going through pick_next_task().
+	 */
+	if (sched_feat(HZ_BW) && rq->nr_running == 1 && task_on_rq_queued(rq->curr)) {
+		if (cfs_task_bw_constrained(rq->curr))
+			return false;
+	}
+

I think we still need the fair_sched_class check with the bit being on cfs_rq/tg rather than task.

Is there a way a non-fair_sched_class task will be in a cfs_rq with cfs_rq->runtime_enabled and/or cfs_b->hierarchical_quota set to non RUNTIME_INF? I suppose if they are stale and it's had its class changed?

That makes the condition pretty ugly but I can add that back if needed.

Quoted 8 lines
Quoted 3 lines

Sigh, yeah. I took that out when I had the bit in the task. I'll put it back in...

Yeah, cfs_rq (and rt_rq) are set unconditionally, and a cgroup can have a mix of fair and RT tasks (whether or not that's a good idea from a sysadmin perspective).

Thanks. I think I'll try the condition as a single-use static inline function. The generated code seems the same but it is a bit nicer to read.

Cheers,
Phil