Skip to contentSkip to conversation

sched/fair: Task placement biasing using uclamp

PATCH Applied Valentin Schneider 4 participants 13 messages Dec 11, 2019 – Dec 25, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover sched/fair: Task placement biasing using uclamp

Hi,

While uclamp restrictions currently only impact schedutil's frequency selection, it would make sense to also let them impact CPU selection in asymmetric topologies. This would let us steer specific tasks towards certain CPU capacities regardless of their actual utilization - I give a few examples in patch 4.

The first three patches are mainly cleanups, the meat of the thing is in patches 4 and 5.

Note that this is in the same spirit as what Patrick had proposed for EAS on Android [1]

[1]: https://android.googlesource.com/kernel/common/+/b61876ed122f816660fe49e0de1b7ee4891deaa2%5E%21

Revisions
=========

Changed in v3:

- Collect Reviewed-by
- (new patch) Remove uclamp_util() (Dietmar)
- Make uclamp_eff_value()'s return type unsigned long (Vincent)
- Reword find_energy_efficient_cpu() tweak changelog (Dietmar)

Changed in v2:

- Collect Reviewed-by

- Make uclamp_task_util() unconditionally use util_est (Quentin)
- Because of the above, move uclamp_task_util() to fair.c

- Split v1's 3/3 into
- task_fits_capacity() tweak (v2's 3/4)
- find_energy_efficient_cpu() tweak (v2's 4/4).

Cheers,
Valentin

Valentin Schneider (5):
sched/uclamp: Remove uclamp_util()
sched/uclamp: Make uclamp util helpers use and return UL values sched/uclamp: Rename uclamp_util_with() into uclamp_rq_util_with() sched/fair: Make task_fits_capacity() consider uclamp restrictions sched/fair: Make EAS wakeup placement consider uclamp restrictions

 kernel/sched/core.c              |  6 +++---
 kernel/sched/cpufreq_schedutil.c |  2 +-
 kernel/sched/fair.c              | 28 +++++++++++++++++++++++++---
 kernel/sched/sched.h             | 24 ++++++++----------------
 4 files changed, 37 insertions(+), 23 deletions(-)

--
2.24.0

#1 lore raw

PATCH v3 1/5 sched/uclamp: Remove uclamp_util()

The sole user of uclamp_util(), schedutil_cpu_util(), was made to use uclamp_util_with() instead in commit

af24bde8df20 ("sched/uclamp: Add uclamp support to energy_compute()")

From then on, uclamp_util() has remained unused. Being a simple wrapper around uclamp_util_with(), we can get rid of it and win back a few lines.

---
 kernel/sched/sched.h | 9 ---------
 1 file changed, 9 deletions(-)
kernel/sched/sched.h +0 -9
index 280a3c735935..d9b24513d71d 100644
@@ -2324,21 +2324,12 @@ unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
 
 	return clamp(util, min_util, max_util);
 }
-
-static inline unsigned int uclamp_util(struct rq *rq, unsigned int util)
-{
-	return uclamp_util_with(rq, util, NULL);
-}
 #else /* CONFIG_UCLAMP_TASK */
 static inline unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
 					    struct task_struct *p)
 {
 	return util;
 }
-static inline unsigned int uclamp_util(struct rq *rq, unsigned int util)
-{
-	return util;
-}
 #endif /* CONFIG_UCLAMP_TASK */
 
 #ifdef arch_scale_freq_capacity
Signature · 1 line
2.24.0
#2 lore raw

[tip: sched/core] sched/uclamp: Remove uclamp_util()

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     59fe675248ffc37d4167e9ec6920a2f3d5ec67bb
Gitweb:        https://git.kernel.org/tip/59fe675248ffc37d4167e9ec6920a2f3d5ec67bb
Author:        Valentin Schneider <[email protected]>
AuthorDate:    Wed, 11 Dec 2019 11:38:47 
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 25 Dec 2019 10:42:07 +01:00

sched/uclamp: Remove uclamp_util()

The sole user of uclamp_util(), schedutil_cpu_util(), was made to use uclamp_util_with() instead in commit:

af24bde8df20 ("sched/uclamp: Add uclamp support to energy_compute()")

Quoted 1 line

From then on, uclamp_util() has remained unused. Being a simple wrapper

around uclamp_util_with(), we can get rid of it and win back a few lines.

---
 kernel/sched/sched.h |  9 ---------
 1 file changed, 9 deletions(-)
kernel/sched/sched.h +0 -9
index 280a3c7..d9b2451 100644
@@ -2324,21 +2324,12 @@ unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
 
 	return clamp(util, min_util, max_util);
 }
-
-static inline unsigned int uclamp_util(struct rq *rq, unsigned int util)
-{
-	return uclamp_util_with(rq, util, NULL);
-}
 #else /* CONFIG_UCLAMP_TASK */
 static inline unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
 					    struct task_struct *p)
 {
 	return util;
 }
-static inline unsigned int uclamp_util(struct rq *rq, unsigned int util)
-{
-	return util;
-}
 #endif /* CONFIG_UCLAMP_TASK */
 
 #ifdef arch_scale_freq_capacity
#3 lore raw

PATCH v3 2/5 sched/uclamp: Make uclamp util helpers use and return UL values

In reply to Valentin Schneider

Vincent pointed out recently that the canonical type for utilization values is 'unsigned long'. Internally uclamp uses 'unsigned int' values for cache optimization, but this doesn't have to be exported to its users.

Make the uclamp helpers that deal with utilization use and return unsigned long values.

---
 kernel/sched/core.c  |  6 +++---
 kernel/sched/sched.h | 14 +++++++-------
 2 files changed, 10 insertions(+), 10 deletions(-)
kernel/sched/core.c +3 -3
index 90e4b00ace89..9c41b6551bc9 100644
@@ -919,17 +919,17 @@ uclamp_eff_get(struct task_struct *p, enum uclamp_id clamp_id)
 	return uc_req;
 }
 
-unsigned int uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
+unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
 {
 	struct uclamp_se uc_eff;
 
 	/* Task currently refcounted: use back-annotated (effective) value */
 	if (p->uclamp[clamp_id].active)
-		return p->uclamp[clamp_id].value;
+		return (unsigned long)p->uclamp[clamp_id].value;
 
 	uc_eff = uclamp_eff_get(p, clamp_id);
 
-	return uc_eff.value;
+	return (unsigned long)uc_eff.value;
 }
 
 /*
kernel/sched/sched.h +7 -7
index d9b24513d71d..b478474ea847 100644
@@ -2300,14 +2300,14 @@ static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
 #endif /* CONFIG_CPU_FREQ */
 
 #ifdef CONFIG_UCLAMP_TASK
-unsigned int uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
+unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
 
 static __always_inline
-unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
-			      struct task_struct *p)
+unsigned long uclamp_util_with(struct rq *rq, unsigned long util,
+			       struct task_struct *p)
 {
-	unsigned int min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value);
-	unsigned int max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
+	unsigned long min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value);
+	unsigned long max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
 
 	if (p) {
 		min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN));
@@ -2325,8 +2325,8 @@ unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
 	return clamp(util, min_util, max_util);
 }
 #else /* CONFIG_UCLAMP_TASK */
-static inline unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
-					    struct task_struct *p)
+static inline unsigned long uclamp_util_with(struct rq *rq, unsigned long util,
+					     struct task_struct *p)
 {
 	return util;
 }
Signature · 1 line
2.24.0
#4 lore raw

[tip: sched/core] sched/uclamp: Make uclamp util helpers use and return UL values

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     686516b55e98edf18c2a02d36aaaa6f4c0f6c39c
Gitweb:        https://git.kernel.org/tip/686516b55e98edf18c2a02d36aaaa6f4c0f6c39c
Author:        Valentin Schneider <[email protected]>
AuthorDate:    Wed, 11 Dec 2019 11:38:48 
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 25 Dec 2019 10:42:08 +01:00

sched/uclamp: Make uclamp util helpers use and return UL values

Vincent pointed out recently that the canonical type for utilization values is 'unsigned long'. Internally uclamp uses 'unsigned int' values for cache optimization, but this doesn't have to be exported to its users.

Make the uclamp helpers that deal with utilization use and return unsigned long values.

---
 kernel/sched/core.c  |  6 +++---
 kernel/sched/sched.h | 14 +++++++-------
 2 files changed, 10 insertions(+), 10 deletions(-)
kernel/sched/core.c +3 -3
index 1f6c094..e7b08d5 100644
@@ -919,17 +919,17 @@ uclamp_eff_get(struct task_struct *p, enum uclamp_id clamp_id)
 	return uc_req;
 }
 
-unsigned int uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
+unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
 {
 	struct uclamp_se uc_eff;
 
 	/* Task currently refcounted: use back-annotated (effective) value */
 	if (p->uclamp[clamp_id].active)
-		return p->uclamp[clamp_id].value;
+		return (unsigned long)p->uclamp[clamp_id].value;
 
 	uc_eff = uclamp_eff_get(p, clamp_id);
 
-	return uc_eff.value;
+	return (unsigned long)uc_eff.value;
 }
 
 /*
kernel/sched/sched.h +7 -7
index d9b2451..b478474 100644
@@ -2300,14 +2300,14 @@ static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
 #endif /* CONFIG_CPU_FREQ */
 
 #ifdef CONFIG_UCLAMP_TASK
-unsigned int uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
+unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
 
 static __always_inline
-unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
-			      struct task_struct *p)
+unsigned long uclamp_util_with(struct rq *rq, unsigned long util,
+			       struct task_struct *p)
 {
-	unsigned int min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value);
-	unsigned int max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
+	unsigned long min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value);
+	unsigned long max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
 
 	if (p) {
 		min_util = max(min_util, uclamp_eff_value(p, UCLAMP_MIN));
@@ -2325,8 +2325,8 @@ unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
 	return clamp(util, min_util, max_util);
 }
 #else /* CONFIG_UCLAMP_TASK */
-static inline unsigned int uclamp_util_with(struct rq *rq, unsigned int util,
-					    struct task_struct *p)
+static inline unsigned long uclamp_util_with(struct rq *rq, unsigned long util,
+					     struct task_struct *p)
 {
 	return util;
 }
#5 lore raw

PATCH v3 3/5 sched/uclamp: Rename uclamp_util_with() into uclamp_rq_util_with()

In reply to Valentin Schneider

The current helper returns (CPU) rq utilization with uclamp restrictions taken into account. A uclamp task utilization helper would be quite helpful, but this requires some renaming.

Prepare the code for the introduction of a uclamp_task_util() by renaming the existing uclamp_util_with() to uclamp_rq_util_with().

---
 kernel/sched/cpufreq_schedutil.c | 2 +-
 kernel/sched/sched.h             | 9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)
kernel/sched/cpufreq_schedutil.c +1 -1
index 86800b4d5453..9deb3a13416d 100644
@@ -240,7 +240,7 @@ unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
 	 */
 	util = util_cfs + cpu_util_rt(rq);
 	if (type == FREQUENCY_UTIL)
-		util = uclamp_util_with(rq, util, p);
+		util = uclamp_rq_util_with(rq, util, p);
 
 	dl_util = cpu_util_dl(rq);
 
kernel/sched/sched.h +5 -4
index b478474ea847..1a88dc8ad11b 100644
@@ -2303,8 +2303,8 @@ static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
 unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
 
 static __always_inline
-unsigned long uclamp_util_with(struct rq *rq, unsigned long util,
-			       struct task_struct *p)
+unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
+				  struct task_struct *p)
 {
 	unsigned long min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value);
 	unsigned long max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
@@ -2325,8 +2325,9 @@ unsigned long uclamp_util_with(struct rq *rq, unsigned long util,
 	return clamp(util, min_util, max_util);
 }
 #else /* CONFIG_UCLAMP_TASK */
-static inline unsigned long uclamp_util_with(struct rq *rq, unsigned long util,
-					     struct task_struct *p)
+static inline
+unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
+				  struct task_struct *p)
 {
 	return util;
 }
Signature · 1 line
2.24.0
#6 lore raw

[tip: sched/core] sched/uclamp: Rename uclamp_util_with() into uclamp_rq_util_with()

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     d2b58a286e89824900d501db0be1d4f6aed474fc
Gitweb:        https://git.kernel.org/tip/d2b58a286e89824900d501db0be1d4f6aed474fc
Author:        Valentin Schneider <[email protected]>
AuthorDate:    Wed, 11 Dec 2019 11:38:49 
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 25 Dec 2019 10:42:08 +01:00

sched/uclamp: Rename uclamp_util_with() into uclamp_rq_util_with()

The current helper returns (CPU) rq utilization with uclamp restrictions taken into account. A uclamp task utilization helper would be quite helpful, but this requires some renaming.

Prepare the code for the introduction of a uclamp_task_util() by renaming the existing uclamp_util_with() to uclamp_rq_util_with().

---
 kernel/sched/cpufreq_schedutil.c |  2 +-
 kernel/sched/sched.h             |  9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)
kernel/sched/cpufreq_schedutil.c +1 -1
index 9b8916f..7fbaee2 100644
@@ -238,7 +238,7 @@ unsigned long schedutil_cpu_util(int cpu, unsigned long util_cfs,
 	 */
 	util = util_cfs + cpu_util_rt(rq);
 	if (type == FREQUENCY_UTIL)
-		util = uclamp_util_with(rq, util, p);
+		util = uclamp_rq_util_with(rq, util, p);
 
 	dl_util = cpu_util_dl(rq);
 
kernel/sched/sched.h +5 -4
index b478474..1a88dc8 100644
@@ -2303,8 +2303,8 @@ static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
 unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
 
 static __always_inline
-unsigned long uclamp_util_with(struct rq *rq, unsigned long util,
-			       struct task_struct *p)
+unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
+				  struct task_struct *p)
 {
 	unsigned long min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value);
 	unsigned long max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
@@ -2325,8 +2325,9 @@ unsigned long uclamp_util_with(struct rq *rq, unsigned long util,
 	return clamp(util, min_util, max_util);
 }
 #else /* CONFIG_UCLAMP_TASK */
-static inline unsigned long uclamp_util_with(struct rq *rq, unsigned long util,
-					     struct task_struct *p)
+static inline
+unsigned long uclamp_rq_util_with(struct rq *rq, unsigned long util,
+				  struct task_struct *p)
 {
 	return util;
 }
#7 lore raw

PATCH v3 4/5 sched/fair: Make task_fits_capacity() consider uclamp restrictions

In reply to Valentin Schneider

task_fits_capacity() drives CPU selection at wakeup time, and is also used to detect misfit tasks. Right now it does so by comparing task_util_est() with a CPU's capacity, but doesn't take into account uclamp restrictions.

There's a few interesting uses that can come out of doing this. For instance, a low uclamp.max value could prevent certain tasks from being flagged as misfit tasks, so they could merrily remain on low-capacity CPUs. Similarly, a high uclamp.min value would steer tasks towards high capacity CPUs at wakeup (and, should that fail, later steered via misfit balancing), so such "boosted" tasks would favor CPUs of higher capacity.

Introduce uclamp_task_util() and make task_fits_capacity() use it.

---
 kernel/sched/fair.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
kernel/sched/fair.c +15 -1
index 08a233e97a01..a9c93c5427bf 100644
@@ -3711,6 +3711,20 @@ static inline unsigned long task_util_est(struct task_struct *p)
 	return max(task_util(p), _task_util_est(p));
 }
 
+#ifdef CONFIG_UCLAMP_TASK
+static inline unsigned long uclamp_task_util(struct task_struct *p)
+{
+	return clamp(task_util_est(p),
+		     uclamp_eff_value(p, UCLAMP_MIN),
+		     uclamp_eff_value(p, UCLAMP_MAX));
+}
+#else
+static inline unsigned long uclamp_task_util(struct task_struct *p)
+{
+	return task_util_est(p);
+}
+#endif
+
 static inline void util_est_enqueue(struct cfs_rq *cfs_rq,
 				    struct task_struct *p)
 {
@@ -3822,7 +3836,7 @@ util_est_dequeue(struct cfs_rq *cfs_rq, struct task_struct *p, bool task_sleep)
 
 static inline int task_fits_capacity(struct task_struct *p, long capacity)
 {
-	return fits_capacity(task_util_est(p), capacity);
+	return fits_capacity(uclamp_task_util(p), capacity);
 }
 
 static inline void update_misfit_status(struct task_struct *p, struct rq *rq)
Signature · 1 line
2.24.0
#8 lore raw

[tip: sched/core] sched/fair: Make task_fits_capacity() consider uclamp restrictions

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     a7008c07a568278ed2763436404752a98004c7ff
Gitweb:        https://git.kernel.org/tip/a7008c07a568278ed2763436404752a98004c7ff
Author:        Valentin Schneider <[email protected]>
AuthorDate:    Wed, 11 Dec 2019 11:38:50 
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 25 Dec 2019 10:42:09 +01:00

sched/fair: Make task_fits_capacity() consider uclamp restrictions

task_fits_capacity() drives CPU selection at wakeup time, and is also used to detect misfit tasks. Right now it does so by comparing task_util_est() with a CPU's capacity, but doesn't take into account uclamp restrictions.

There's a few interesting uses that can come out of doing this. For instance, a low uclamp.max value could prevent certain tasks from being flagged as misfit tasks, so they could merrily remain on low-capacity CPUs. Similarly, a high uclamp.min value would steer tasks towards high capacity CPUs at wakeup (and, should that fail, later steered via misfit balancing), so such "boosted" tasks would favor CPUs of higher capacity.

Introduce uclamp_task_util() and make task_fits_capacity() use it.

---
 kernel/sched/fair.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
kernel/sched/fair.c +15 -1
index 1f34fa9..26c59bc 100644
@@ -3711,6 +3711,20 @@ static inline unsigned long task_util_est(struct task_struct *p)
 	return max(task_util(p), _task_util_est(p));
 }
 
+#ifdef CONFIG_UCLAMP_TASK
+static inline unsigned long uclamp_task_util(struct task_struct *p)
+{
+	return clamp(task_util_est(p),
+		     uclamp_eff_value(p, UCLAMP_MIN),
+		     uclamp_eff_value(p, UCLAMP_MAX));
+}
+#else
+static inline unsigned long uclamp_task_util(struct task_struct *p)
+{
+	return task_util_est(p);
+}
+#endif
+
 static inline void util_est_enqueue(struct cfs_rq *cfs_rq,
 				    struct task_struct *p)
 {
@@ -3822,7 +3836,7 @@ done:
 
 static inline int task_fits_capacity(struct task_struct *p, long capacity)
 {
-	return fits_capacity(task_util_est(p), capacity);
+	return fits_capacity(uclamp_task_util(p), capacity);
 }
 
 static inline void update_misfit_status(struct task_struct *p, struct rq *rq)
#9 lore raw

PATCH v3 5/5 sched/fair: Make EAS wakeup placement consider uclamp restrictions

In reply to Valentin Schneider

task_fits_capacity() has just been made uclamp-aware, and find_energy_efficient_cpu() needs to go through the same treatment.

Things are somewhat different here however - using the task max clamp isn't sufficient. Consider the following setup:

The target runqueue, rq:

    rq.cpu_capacity_orig = 512
    rq.cfs.avg.util_avg = 200
    rq.uclamp.max = 768 // the max p.uclamp.max of all enqueued p's is 768

The waking task, p (not yet enqueued on rq):

    p.util_est = 600
    p.uclamp.max = 100

Now, consider the following code which doesn't use the rq clamps:

  util = uclamp_task_util(p);
  // Does the task fit in the spare CPU capacity?
  cpu = cpu_of(rq);
  fits_capacity(util, cpu_capacity(cpu) - cpu_util(cpu))

This would lead to:

  util = 100;
  fits_capacity(100, 512 - 200)

fits_capacity() would return true. However, enqueuing p on that CPU *will* cause it to become overutilized since rq clamp values are max-aggregated, so we'd remain with

rq.uclamp.max = 768

which comes from the other tasks already enqueued on rq. Thus, we could select a high enough frequency to reach beyond 0.8 * 512 utilization (== overutilized) after enqueuing p on rq. What find_energy_efficient_cpu() needs here is uclamp_rq_util_with() which lets us peek at the future utilization landscape, including rq-wide uclamp values.

Make find_energy_efficient_cpu() use uclamp_rq_util_with() for its fits_capacity() check. This is in line with what compute_energy() ends up using for estimating utilization.

---
 kernel/sched/fair.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
kernel/sched/fair.c +10 -2
index a9c93c5427bf..956d9578935a 100644
@@ -6282,9 +6282,18 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu)
 			if (!cpumask_test_cpu(cpu, p->cpus_ptr))
 				continue;
 
-			/* Skip CPUs that will be overutilized. */
 			util = cpu_util_next(cpu, p, cpu);
 			cpu_cap = capacity_of(cpu);
+			spare_cap = cpu_cap - util;
+
+			/*
+			 * Skip CPUs that cannot satisfy the capacity request.
+			 * IOW, placing the task there would make the CPU
+			 * overutilized. Take uclamp into account to see how
+			 * much capacity we can get out of the CPU; this is
+			 * aligned with schedutil_cpu_util().
+			 */
+			util = uclamp_rq_util_with(cpu_rq(cpu), util, p);
 			if (!fits_capacity(util, cpu_cap))
 				continue;
 
@@ -6299,7 +6308,6 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu)
 			 * Find the CPU with the maximum spare capacity in
 			 * the performance domain
 			 */
-			spare_cap = cpu_cap - util;
 			if (spare_cap > max_spare_cap) {
 				max_spare_cap = spare_cap;
 				max_spare_cap_cpu = cpu;
Signature · 1 line
2.24.0
#10 lore raw

[tip: sched/core] sched/fair: Make EAS wakeup placement consider uclamp restrictions

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     1d42509e475cdc8542aa5b3e03a7e845244f4f57
Gitweb:        https://git.kernel.org/tip/1d42509e475cdc8542aa5b3e03a7e845244f4f57
Author:        Valentin Schneider <[email protected]>
AuthorDate:    Wed, 11 Dec 2019 11:38:51 
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 25 Dec 2019 10:42:09 +01:00

sched/fair: Make EAS wakeup placement consider uclamp restrictions

task_fits_capacity() has just been made uclamp-aware, and find_energy_efficient_cpu() needs to go through the same treatment.

Things are somewhat different here however - using the task max clamp isn't sufficient. Consider the following setup:

The target runqueue, rq:

    rq.cpu_capacity_orig = 512
    rq.cfs.avg.util_avg = 200
    rq.uclamp.max = 768 // the max p.uclamp.max of all enqueued p's is 768

The waking task, p (not yet enqueued on rq):

    p.util_est = 600
    p.uclamp.max = 100

Now, consider the following code which doesn't use the rq clamps:

  util = uclamp_task_util(p);
  // Does the task fit in the spare CPU capacity?
  cpu = cpu_of(rq);
  fits_capacity(util, cpu_capacity(cpu) - cpu_util(cpu))

This would lead to:

  util = 100;
  fits_capacity(100, 512 - 200)

fits_capacity() would return true. However, enqueuing p on that CPU *will* cause it to become overutilized since rq clamp values are max-aggregated, so we'd remain with

rq.uclamp.max = 768

which comes from the other tasks already enqueued on rq. Thus, we could select a high enough frequency to reach beyond 0.8 * 512 utilization (== overutilized) after enqueuing p on rq. What find_energy_efficient_cpu() needs here is uclamp_rq_util_with() which lets us peek at the future utilization landscape, including rq-wide uclamp values.

Make find_energy_efficient_cpu() use uclamp_rq_util_with() for its fits_capacity() check. This is in line with what compute_energy() ends up using for estimating utilization.

---
 kernel/sched/fair.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
kernel/sched/fair.c +10 -2
index 26c59bc..2d170b5 100644
@@ -6273,9 +6273,18 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu)
 			if (!cpumask_test_cpu(cpu, p->cpus_ptr))
 				continue;
 
-			/* Skip CPUs that will be overutilized. */
 			util = cpu_util_next(cpu, p, cpu);
 			cpu_cap = capacity_of(cpu);
+			spare_cap = cpu_cap - util;
+
+			/*
+			 * Skip CPUs that cannot satisfy the capacity request.
+			 * IOW, placing the task there would make the CPU
+			 * overutilized. Take uclamp into account to see how
+			 * much capacity we can get out of the CPU; this is
+			 * aligned with schedutil_cpu_util().
+			 */
+			util = uclamp_rq_util_with(cpu_rq(cpu), util, p);
 			if (!fits_capacity(util, cpu_cap))
 				continue;
 
@@ -6290,7 +6299,6 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu)
 			 * Find the CPU with the maximum spare capacity in
 			 * the performance domain
 			 */
-			spare_cap = cpu_cap - util;
 			if (spare_cap > max_spare_cap) {
 				max_spare_cap = spare_cap;
 				max_spare_cap_cpu = cpu;
reviewer Reviewed-by Tested-by on the v3 series
#11 lore raw
In reply to Valentin Schneider
Quoted 8 lines · Valentin Schneider

Hi,

While uclamp restrictions currently only impact schedutil's frequency selection, it would make sense to also let them impact CPU selection in asymmetric topologies. This would let us steer specific tasks towards certain CPU capacities regardless of their actual utilization - I give a few examples in patch 4.

Quoted 7 lines

The first three patches are mainly cleanups, the meat of the thing is in patches 4 and 5.

Note that this is in the same spirit as what Patrick had proposed for EAS on Android [1]

[1]: https://android.googlesource.com/kernel/common/+/b61876ed122f816660fe49e0de1b7ee4891deaa2%5E%21

Tested on Juno-r0 (Arm64) cpumask [0x3f] w/ big [0x06], LITTLE [0x39] [orig cpu capacity big,LITTLE: 1024,446] and rt-app

4 periodic tasks runtime/period [800/16000], per task uclamp_min/max [600,1024]

w/o uclamp: EAS puts the tasks on LITTLE CPUs [0x39] w/ uclamp: EAS puts the tasks on big CPUs [0x06]

maintainer
#12 lore raw
Quoted 12 lines · Dietmar Eggemann
Quoted 11 lines · Valentin Schneider

Hi,

While uclamp restrictions currently only impact schedutil's frequency selection, it would make sense to also let them impact CPU selection in asymmetric topologies. This would let us steer specific tasks towards certain CPU capacities regardless of their actual utilization - I give a few examples in patch 4.

The first three patches are mainly cleanups, the meat of the thing is in patches 4 and 5.

Quoted 7 lines
Quoted 4 lines

Note that this is in the same spirit as what Patrick had proposed for EAS on Android [1]

[1]: https://android.googlesource.com/kernel/common/+/b61876ed122f816660fe49e0de1b7ee4891deaa2%5E%21

Thanks!