Skip to contentSkip to conversation

sched/deadline: Fix and optimize sched_dl_global_validate()

PATCH Superseded Peng Liu 2 participants 5 messages Oct 7, 2020 – Oct 8, 2020 Scheduler

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Peng Liu

author
#0 lore raw

v5 cover sched/deadline: Fix and optimize sched_dl_global_validate()

When change global rt bandwidth, we check to make sure that new settings could accommodate the allocated dl bandwidth.

Under SMP, the dl_bw is on a per root domain basis, currently we check and update the new settings one cpu by one cpu, but not in the unit of root domain, which is either overdoing or error.

patch 1 removed the superfluous checking and updating patch 2 fixed the error validation

For details, please see the corresponding patch.

----------------
v5 <-- v4:
- no functional changes, just split the v4 single patch to two to obey the "one patch do only one thing" rule;
- turn root_domain::visit_gen from u64 to u32;
both suggested by Juri.
- refine changelog;

v4 <-- v3:
- refine changelog;
- eliminate the ugly #ifdef guys with Peter's method;

v3 <-- v2:
- fix build error for !CONFIG_SMP, reported by kernel test robot;

v2 <-- v1:
- replace cpumask_weight(cpu_rq(cpu)->rd->span) with dl_bw_cpus(cpu), suggested by Juri;

Peng Liu (2):
sched/deadline: optimize sched_dl_global_validate() sched/deadline: Fix sched_dl_global_validate()

 kernel/sched/deadline.c | 46 +++++++++++++++++++++++++++++---------
 kernel/sched/sched.h    | 49 +++++++++++++++++++++--------------------
 kernel/sched/topology.c |  1 +
 3 files changed, 62 insertions(+), 34 deletions(-)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v5 1/2 sched/deadline: optimize sched_dl_global_validate()

Under CONFIG_SMP, dl_bw is per root domain, but not per CPU. When checking or updating dl_bw, currently iterating every CPU is overdoing, just need iterate each root domain once.

---
 kernel/sched/deadline.c | 43 ++++++++++++++++++++++++++++++++---------
 kernel/sched/sched.h    |  7 +++++++
 kernel/sched/topology.c |  1 +
 3 files changed, 42 insertions(+), 9 deletions(-)
kernel/sched/deadline.c +34 -9
index c19c1883d695..5200e185923f 100644
@@ -97,6 +97,17 @@ static inline unsigned long dl_bw_capacity(int i)
 		return __dl_bw_capacity(i);
 	}
 }
+
+static inline bool dl_bw_visited(int cpu, u32 gen)
+{
+	struct root_domain *rd = cpu_rq(cpu)->rd;
+
+	if (rd->visit_gen == gen)
+		return true;
+
+	rd->visit_gen = gen;
+	return false;
+}
 #else
 static inline struct dl_bw *dl_bw_of(int i)
 {
@@ -112,6 +123,11 @@ static inline unsigned long dl_bw_capacity(int i)
 {
 	return SCHED_CAPACITY_SCALE;
 }
+
+static inline bool dl_bw_visited(int cpu, u32 gen)
+{
+	return false;
+}
 #endif
 
 static inline
@@ -2514,26 +2530,30 @@ const struct sched_class dl_sched_class
 	.update_curr		= update_curr_dl,
 };
 
+/* Used for dl_bw check and update. */
+static u32 dl_generation;
+
 int sched_dl_global_validate(void)
 {
 	u64 runtime = global_rt_runtime();
 	u64 period = global_rt_period();
 	u64 new_bw = to_ratio(period, runtime);
 	struct dl_bw *dl_b;
-	int cpu, ret = 0;
 	unsigned long flags;
+	int cpu, ret = 0;
+	u32 gen = ++dl_generation;
 
 	/*
 	 * Here we want to check the bandwidth not being set to some
 	 * value smaller than the currently allocated bandwidth in
 	 * any of the root_domains.
-	 *
-	 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
-	 * cycling on root_domains... Discussion on different/better
-	 * solutions is welcome!
 	 */
 	for_each_possible_cpu(cpu) {
 		rcu_read_lock_sched();
+
+		if (dl_bw_visited(cpu, gen))
+			goto next;
+
 		dl_b = dl_bw_of(cpu);
 
 		raw_spin_lock_irqsave(&dl_b->lock, flags);
@@ -2541,6 +2561,7 @@ int sched_dl_global_validate(void)
 			ret = -EBUSY;
 		raw_spin_unlock_irqrestore(&dl_b->lock, flags);
 
+next:
 		rcu_read_unlock_sched();
 
 		if (ret)
@@ -2567,8 +2588,9 @@ void sched_dl_do_global(void)
 {
 	u64 new_bw = -1;
 	struct dl_bw *dl_b;
-	int cpu;
 	unsigned long flags;
+	int cpu;
+	u32 gen = ++dl_generation;
 
 	def_dl_bandwidth.dl_period = global_rt_period();
 	def_dl_bandwidth.dl_runtime = global_rt_runtime();
@@ -2576,11 +2598,14 @@ void sched_dl_do_global(void)
 	if (global_rt_runtime() != RUNTIME_INF)
 		new_bw = to_ratio(global_rt_period(), global_rt_runtime());
 
-	/*
-	 * FIXME: As above...
-	 */
 	for_each_possible_cpu(cpu) {
 		rcu_read_lock_sched();
+
+		if (dl_bw_visited(cpu, gen)) {
+			rcu_read_unlock_sched();
+			continue;
+		}
+
 		dl_b = dl_bw_of(cpu);
 
 		raw_spin_lock_irqsave(&dl_b->lock, flags);
kernel/sched/sched.h +7 -0
index 28709f6b0975..53477e8b26b0 100644
@@ -798,6 +798,13 @@ struct root_domain {
 	 */
 	cpumask_var_t		dlo_mask;
 	atomic_t		dlo_count;
+
+	/*
+	 * Indicate whether a root_domain's dl_bw has been checked or
+	 * updated. It's monotonously increasing, then wrap around.
+	 */
+	u32 visit_gen;
+
 	struct dl_bw		dl_bw;
 	struct cpudl		cpudl;
 
kernel/sched/topology.c +1 -0
index dd7770226086..90f3e5558fa2 100644
@@ -516,6 +516,7 @@ static int init_rootdomain(struct root_domain *rd)
 	init_irq_work(&rd->rto_push_work, rto_push_irq_work_func);
 #endif
 
+	rd->visit_gen = 0;
 	init_dl_bw(&rd->dl_bw);
 	if (cpudl_init(&rd->cpudl) != 0)
 		goto free_rto_mask;
Signature · 1 line
2.20.1
author
#4 lore raw

PATCH v5 2/2 sched/deadline: Fix sched_dl_global_validate()

In reply to Peng Liu

When change sched_rt_{runtime, period}_us, we validate that the new settings should at least accommodate the currently allocated -dl bandwidth:

sched_rt_handler()

    -->	sched_dl_bandwidth_validate()
	{
		new_bw = global_rt_runtime()/global_rt_period();

		for_each_possible_cpu(cpu) {
			dl_b = dl_bw_of(cpu);
			if (new_bw < dl_b->total_bw)    <-------
				ret = -EBUSY;
		}
	}

But under CONFIG_SMP, dl_bw is per root domain , but not per CPU, dl_b->total_bw is the allocated bandwidth of the whole root domain. Instead, we should compare dl_b->total_bw against "cpus*new_bw", where 'cpus' is the number of CPUs of the root domain.

Also, below annotation(in kernel/sched/sched.h) implied implementation only appeared in SCHED_DEADLINE v2[1], then deadline scheduler kept evolving till got merged(v9), but the annotation remains unchanged, meaningless and misleading, update it.

* With respect to SMP, the bandwidth is given on a per-CPU basis,
* meaning that:
* - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
* - dl_total_bw array contains, in the i-eth element, the currently
* allocated bandwidth on the i-eth CPU.

[1]: https://lore.kernel.org/lkml/1267385230.13676.101.camel@Palantir/

Fixes 332ac17ef5bf ("sched/deadline: Add bandwidth management for SCHED_DEADLINE tasks")
---
 kernel/sched/deadline.c |  5 +++--
 kernel/sched/sched.h    | 42 ++++++++++++++++++-----------------------
 2 files changed, 21 insertions(+), 26 deletions(-)
kernel/sched/deadline.c +3 -2
index 5200e185923f..e32715db7119 100644
@@ -2540,7 +2540,7 @@ int sched_dl_global_validate(void)
 	u64 new_bw = to_ratio(period, runtime);
 	struct dl_bw *dl_b;
 	unsigned long flags;
-	int cpu, ret = 0;
+	int cpu, cpus, ret = 0;
 	u32 gen = ++dl_generation;
 
 	/*
@@ -2555,9 +2555,10 @@ int sched_dl_global_validate(void)
 			goto next;
 
 		dl_b = dl_bw_of(cpu);
+		cpus = dl_bw_cpus(cpu);
 
 		raw_spin_lock_irqsave(&dl_b->lock, flags);
-		if (new_bw < dl_b->total_bw)
+		if (new_bw * cpus < dl_b->total_bw)
 			ret = -EBUSY;
 		raw_spin_unlock_irqrestore(&dl_b->lock, flags);
 
kernel/sched/sched.h +18 -24

54 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#2 lore raw

Re: [PATCH v5 1/2] sched/deadline: optimize sched_dl_global_validate()

In reply to Peng Liu
Quoted 26 lines · Peng Liu
kernel/sched/deadline.c +2 -0
+/* Used for dl_bw check and update. */
+static u32 dl_generation;
kernel/sched/sched.h +7 -0
index 28709f6b0975..53477e8b26b0 100644
@@ -798,6 +798,13 @@ struct root_domain {
 	 */
 	cpumask_var_t		dlo_mask;
 	atomic_t		dlo_count;
+
+	/*
+	 * Indicate whether a root_domain's dl_bw has been checked or
+	 * updated. It's monotonously increasing, then wrap around.
+	 */
+	u32 visit_gen;
+
 	struct dl_bw		dl_bw;
 	struct cpudl		cpudl;
 
kernel/sched/topology.c +0 -0
index dd7770226086..90f3e5558fa2 100644

kernel/sched/topology.c static int init_rootdomain(struct root_domain *rd) · line 522

Quoted 7 lines
kernel/sched/topology.c +1 -0
@@ -516,6 +516,7 @@ static int init_rootdomain(struct root_domain *rd)
 	init_irq_work(&rd->rto_push_work, rto_push_irq_work_func);
 #endif
 
+	rd->visit_gen = 0;
 	init_dl_bw(&rd->dl_bw);
 	if (cpudl_init(&rd->cpudl) != 0)
 		goto free_rto_mask;

I'm fairly sure I made the generation a u64, the above is susceptible to a false positive due to wrap-around.

Increase the generation to -1, create a new root domain, then the next generation is 0 and we'll skip the new domain, even though it should be updated.

author
#3 lore raw

Re: [PATCH v5 1/2] sched/deadline: optimize sched_dl_global_validate()

Quoted 38 lines · Peter Zijlstra
Quoted 26 lines · Peng Liu
kernel/sched/deadline.c +2 -0
+/* Used for dl_bw check and update. */
+static u32 dl_generation;
kernel/sched/sched.h +7 -0
index 28709f6b0975..53477e8b26b0 100644
@@ -798,6 +798,13 @@ struct root_domain {
 	 */
 	cpumask_var_t		dlo_mask;
 	atomic_t		dlo_count;
+
+	/*
+	 * Indicate whether a root_domain's dl_bw has been checked or
+	 * updated. It's monotonously increasing, then wrap around.
+	 */
+	u32 visit_gen;
+
 	struct dl_bw		dl_bw;
 	struct cpudl		cpudl;
 
kernel/sched/topology.c +0 -0
index dd7770226086..90f3e5558fa2 100644

kernel/sched/topology.c static int init_rootdomain(struct root_domain *rd) · line 522

Quoted 7 lines
kernel/sched/topology.c +1 -0
@@ -516,6 +516,7 @@ static int init_rootdomain(struct root_domain *rd)
 	init_irq_work(&rd->rto_push_work, rto_push_irq_work_func);
 #endif
 
+	rd->visit_gen = 0;
 	init_dl_bw(&rd->dl_bw);
 	if (cpudl_init(&rd->cpudl) != 0)
 		goto free_rto_mask;

I'm fairly sure I made the generation a u64, the above is susceptible to a false positive due to wrap-around.

Quoted 3 lines

Increase the generation to -1, create a new root domain, then the next generation is 0 and we'll skip the new domain, even though it should be updated.

Ah... at first, I also thought that u32 is "big enough" given that no one would frequently change the settings, 'wrap-around' shouldn't be a concern.

So...OK, I will revert it back to u64. What a big circle! :)

Thanks for your time!