diff mbox series

[v3,2/2] sched/fair: Fixes for capacity inversion detection

Message ID 20230112122708.330667-3-qyousef@layalina.io
State Accepted
Commit da07d2f9c153e457e845d4dcfdd13568d71d18a4
Headers show
Series Fixes for uclamp and capacity inversion detection | expand

Commit Message

Qais Yousef Jan. 12, 2023, 12:27 p.m. UTC
Traversing the Perf Domains requires rcu_read_lock() to be held and is
conditional on sched_energy_enabled(). Ensure right protections applied.

Also skip capacity inversion detection for our own pd; which was an
error.

Fixes: 44c7b80bffc3 ("sched/fair: Detect capacity inversion")
Reported-by: Dietmar Eggemann <dietmar.eggemann@arm.com>
Signed-off-by: Qais Yousef (Google) <qyousef@layalina.io>
---
 kernel/sched/fair.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Comments

Vincent Guittot Jan. 13, 2023, 8:14 a.m. UTC | #1
On Thu, 12 Jan 2023 at 13:27, Qais Yousef <qyousef@layalina.io> wrote:
>
> Traversing the Perf Domains requires rcu_read_lock() to be held and is
> conditional on sched_energy_enabled(). Ensure right protections applied.
>
> Also skip capacity inversion detection for our own pd; which was an
> error.
>
> Fixes: 44c7b80bffc3 ("sched/fair: Detect capacity inversion")
> Reported-by: Dietmar Eggemann <dietmar.eggemann@arm.com>
> Signed-off-by: Qais Yousef (Google) <qyousef@layalina.io>

Reviewed-by: Vincent Guittot <vincent.guittot@linaro.org>

> ---
>  kernel/sched/fair.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 5a8e75d4a17b..34239d3118f0 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -8992,16 +8992,23 @@ static void update_cpu_capacity(struct sched_domain *sd, int cpu)
>          *   * Thermal pressure will impact all cpus in this perf domain
>          *     equally.
>          */
> -       if (static_branch_unlikely(&sched_asym_cpucapacity)) {
> +       if (sched_energy_enabled()) {
>                 unsigned long inv_cap = capacity_orig - thermal_load_avg(rq);
> -               struct perf_domain *pd = rcu_dereference(rq->rd->pd);
> +               struct perf_domain *pd;
> +
> +               rcu_read_lock();
>
> +               pd = rcu_dereference(rq->rd->pd);
>                 rq->cpu_capacity_inverted = 0;
>
>                 for (; pd; pd = pd->next) {
>                         struct cpumask *pd_span = perf_domain_span(pd);
>                         unsigned long pd_cap_orig, pd_cap;
>
> +                       /* We can't be inverted against our own pd */
> +                       if (cpumask_test_cpu(cpu_of(rq), pd_span))
> +                               continue;
> +
>                         cpu = cpumask_any(pd_span);
>                         pd_cap_orig = arch_scale_cpu_capacity(cpu);
>
> @@ -9026,6 +9033,8 @@ static void update_cpu_capacity(struct sched_domain *sd, int cpu)
>                                 break;
>                         }
>                 }
> +
> +               rcu_read_unlock();
>         }
>
>         trace_sched_cpu_capacity_tp(rq);
> --
> 2.25.1
>
diff mbox series

Patch

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 5a8e75d4a17b..34239d3118f0 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -8992,16 +8992,23 @@  static void update_cpu_capacity(struct sched_domain *sd, int cpu)
 	 *   * Thermal pressure will impact all cpus in this perf domain
 	 *     equally.
 	 */
-	if (static_branch_unlikely(&sched_asym_cpucapacity)) {
+	if (sched_energy_enabled()) {
 		unsigned long inv_cap = capacity_orig - thermal_load_avg(rq);
-		struct perf_domain *pd = rcu_dereference(rq->rd->pd);
+		struct perf_domain *pd;
+
+		rcu_read_lock();
 
+		pd = rcu_dereference(rq->rd->pd);
 		rq->cpu_capacity_inverted = 0;
 
 		for (; pd; pd = pd->next) {
 			struct cpumask *pd_span = perf_domain_span(pd);
 			unsigned long pd_cap_orig, pd_cap;
 
+			/* We can't be inverted against our own pd */
+			if (cpumask_test_cpu(cpu_of(rq), pd_span))
+				continue;
+
 			cpu = cpumask_any(pd_span);
 			pd_cap_orig = arch_scale_cpu_capacity(cpu);
 
@@ -9026,6 +9033,8 @@  static void update_cpu_capacity(struct sched_domain *sd, int cpu)
 				break;
 			}
 		}
+
+		rcu_read_unlock();
 	}
 
 	trace_sched_cpu_capacity_tp(rq);