[2/2] x86, sched: Bail out of frequency invariance if turbo frequency is unknown
diff mbox series

Message ID 20200428132450.24901-3-ggherdovich@suse.cz
State New, archived
Headers show
Series
  • More frequency invariance fixes for x86
Related show

Commit Message

Giovanni Gherdovich April 28, 2020, 1:24 p.m. UTC
There may be CPUs that support turbo boost but don't declare any turbo
ratio, i.e. their MSR_TURBO_RATIO_LIMIT is all zeroes. In that condition
scale-invariant calculations can't be performed.

Signed-off-by: Giovanni Gherdovich <ggherdovich@suse.cz>
Suggested-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
Fixes: 1567c3e3467c ("x86, sched: Add support for frequency invariance")
---
 arch/x86/kernel/smpboot.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Rafael J. Wysocki April 29, 2020, 11:30 a.m. UTC | #1
On Tue, Apr 28, 2020 at 3:25 PM Giovanni Gherdovich <ggherdovich@suse.cz> wrote:
>
> There may be CPUs that support turbo boost but don't declare any turbo
> ratio, i.e. their MSR_TURBO_RATIO_LIMIT is all zeroes. In that condition
> scale-invariant calculations can't be performed.
>
> Signed-off-by: Giovanni Gherdovich <ggherdovich@suse.cz>
> Suggested-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
> Fixes: 1567c3e3467c ("x86, sched: Add support for frequency invariance")

Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

> ---
>  arch/x86/kernel/smpboot.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
> index 4718f29a3065..ab2a0df7d1fb 100644
> --- a/arch/x86/kernel/smpboot.c
> +++ b/arch/x86/kernel/smpboot.c
> @@ -1991,9 +1991,11 @@ static bool intel_set_max_freq_ratio(void)
>         /*
>          * Some hypervisors advertise X86_FEATURE_APERFMPERF
>          * but then fill all MSR's with zeroes.
> +        * Some CPUs have turbo boost but don't declare any turbo ratio
> +        * in MSR_TURBO_RATIO_LIMIT.
>          */
> -       if (!base_freq) {
> -               pr_debug("Couldn't determine cpu base frequency, necessary for scale-invariant accounting.\n");
> +       if (!base_freq || !turbo_freq) {
> +               pr_debug("Couldn't determine cpu base or turbo frequency, necessary for scale-invariant accounting.\n");
>                 return false;
>         }
>
> --
> 2.16.4
>
Peter Zijlstra May 1, 2020, 1:04 p.m. UTC | #2
On Tue, Apr 28, 2020 at 03:24:50PM +0200, Giovanni Gherdovich wrote:
> There may be CPUs that support turbo boost but don't declare any turbo
> ratio, i.e. their MSR_TURBO_RATIO_LIMIT is all zeroes. In that condition
> scale-invariant calculations can't be performed.
> 
> Signed-off-by: Giovanni Gherdovich <ggherdovich@suse.cz>
> Suggested-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
> Fixes: 1567c3e3467c ("x86, sched: Add support for frequency invariance")
> ---
>  arch/x86/kernel/smpboot.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
> index 4718f29a3065..ab2a0df7d1fb 100644
> --- a/arch/x86/kernel/smpboot.c
> +++ b/arch/x86/kernel/smpboot.c
> @@ -1991,9 +1991,11 @@ static bool intel_set_max_freq_ratio(void)
>  	/*
>  	 * Some hypervisors advertise X86_FEATURE_APERFMPERF
>  	 * but then fill all MSR's with zeroes.
> +	 * Some CPUs have turbo boost but don't declare any turbo ratio
> +	 * in MSR_TURBO_RATIO_LIMIT.
>  	 */
> -	if (!base_freq) {
> -		pr_debug("Couldn't determine cpu base frequency, necessary for scale-invariant accounting.\n");
> +	if (!base_freq || !turbo_freq) {
> +		pr_debug("Couldn't determine cpu base or turbo frequency, necessary for scale-invariant accounting.\n");
>  		return false;
>  	}

I've added the below, imagine base_freq > turbo_freq *
SCHED_CAPACITY_SCALE.

--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -1975,6 +1975,7 @@ static bool core_set_max_freq_ratio(u64
 static bool intel_set_max_freq_ratio(void)
 {
 	u64 base_freq, turbo_freq;
+	u64 turbo_ratio;
 
 	if (slv_set_max_freq_ratio(&base_freq, &turbo_freq))
 		goto out;
@@ -2008,9 +2009,15 @@ static bool intel_set_max_freq_ratio(voi
 		return false;
 	}
 
-	arch_turbo_freq_ratio = div_u64(turbo_freq * SCHED_CAPACITY_SCALE,
-					base_freq);
+	turbo_ratio = div_u64(turbo_freq * SCHED_CAPACITY_SCALE, base_freq);
+	if (!turbo_ratio) {
+		pr_debug("Non-zero turbo and base frequencies led to a 0 ratio.\n");
+		return false;
+	}
+
+	arch_turbo_freq_ratio = turbo_ratio;
 	arch_set_max_freq_ratio(turbo_disabled());
+
 	return true;
 }
Ricardo Neri May 2, 2020, 12:04 a.m. UTC | #3
On Tue, Apr 28, 2020 at 03:24:50PM +0200, Giovanni Gherdovich wrote:
> There may be CPUs that support turbo boost but don't declare any turbo
> ratio, i.e. their MSR_TURBO_RATIO_LIMIT is all zeroes. In that condition
> scale-invariant calculations can't be performed.
> 
> Signed-off-by: Giovanni Gherdovich <ggherdovich@suse.cz>
> Suggested-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>

Thanks for implementing this, Giovanni!

Tested-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
Ricardo Neri May 2, 2020, 12:06 a.m. UTC | #4
On Fri, May 01, 2020 at 03:04:27PM +0200, Peter Zijlstra wrote:
> On Tue, Apr 28, 2020 at 03:24:50PM +0200, Giovanni Gherdovich wrote:
> > There may be CPUs that support turbo boost but don't declare any turbo
> > ratio, i.e. their MSR_TURBO_RATIO_LIMIT is all zeroes. In that condition
> > scale-invariant calculations can't be performed.
> > 
> > Signed-off-by: Giovanni Gherdovich <ggherdovich@suse.cz>
> > Suggested-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
> > Fixes: 1567c3e3467c ("x86, sched: Add support for frequency invariance")
> > ---
> >  arch/x86/kernel/smpboot.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
> > index 4718f29a3065..ab2a0df7d1fb 100644
> > --- a/arch/x86/kernel/smpboot.c
> > +++ b/arch/x86/kernel/smpboot.c
> > @@ -1991,9 +1991,11 @@ static bool intel_set_max_freq_ratio(void)
> >  	/*
> >  	 * Some hypervisors advertise X86_FEATURE_APERFMPERF
> >  	 * but then fill all MSR's with zeroes.
> > +	 * Some CPUs have turbo boost but don't declare any turbo ratio
> > +	 * in MSR_TURBO_RATIO_LIMIT.
> >  	 */
> > -	if (!base_freq) {
> > -		pr_debug("Couldn't determine cpu base frequency, necessary for scale-invariant accounting.\n");
> > +	if (!base_freq || !turbo_freq) {
> > +		pr_debug("Couldn't determine cpu base or turbo frequency, necessary for scale-invariant accounting.\n");
> >  		return false;
> >  	}
> 
> I've added the below, imagine base_freq > turbo_freq *
> SCHED_CAPACITY_SCALE.
> 
> --- a/arch/x86/kernel/smpboot.c
> +++ b/arch/x86/kernel/smpboot.c
> @@ -1975,6 +1975,7 @@ static bool core_set_max_freq_ratio(u64
>  static bool intel_set_max_freq_ratio(void)
>  {
>  	u64 base_freq, turbo_freq;
> +	u64 turbo_ratio;
>  
>  	if (slv_set_max_freq_ratio(&base_freq, &turbo_freq))
>  		goto out;
> @@ -2008,9 +2009,15 @@ static bool intel_set_max_freq_ratio(voi
>  		return false;
>  	}
>  
> -	arch_turbo_freq_ratio = div_u64(turbo_freq * SCHED_CAPACITY_SCALE,
> -					base_freq);
> +	turbo_ratio = div_u64(turbo_freq * SCHED_CAPACITY_SCALE, base_freq);
> +	if (!turbo_ratio) {
> +		pr_debug("Non-zero turbo and base frequencies led to a 0 ratio.\n");
> +		return false;
> +	}
> +
> +	arch_turbo_freq_ratio = turbo_ratio;

I guess this covers more cases in which turbo_ratio can be zero.

Also, FWIW

Tested-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>

Thanks and BR,
Ricardo
Giovanni Gherdovich May 2, 2020, 2:26 p.m. UTC | #5
On Fri, 2020-05-01 at 15:04 +0200, Peter Zijlstra wrote:
> On Tue, Apr 28, 2020 at 03:24:50PM +0200, Giovanni Gherdovich wrote:
> > There may be CPUs that support turbo boost but don't declare any turbo
> > ratio, i.e. their MSR_TURBO_RATIO_LIMIT is all zeroes. In that condition
> > scale-invariant calculations can't be performed.
> > 
> > Signed-off-by: Giovanni Gherdovich <ggherdovich@suse.cz>
> > Suggested-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
> > Fixes: 1567c3e3467c ("x86, sched: Add support for frequency invariance")
> > ---
> >  arch/x86/kernel/smpboot.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
> > index 4718f29a3065..ab2a0df7d1fb 100644
> > --- a/arch/x86/kernel/smpboot.c
> > +++ b/arch/x86/kernel/smpboot.c
> > @@ -1991,9 +1991,11 @@ static bool intel_set_max_freq_ratio(void)
> >  	/*
> >  	 * Some hypervisors advertise X86_FEATURE_APERFMPERF
> >  	 * but then fill all MSR's with zeroes.
> > +	 * Some CPUs have turbo boost but don't declare any turbo ratio
> > +	 * in MSR_TURBO_RATIO_LIMIT.
> >  	 */
> > -	if (!base_freq) {
> > -		pr_debug("Couldn't determine cpu base frequency, necessary for scale-invariant accounting.\n");
> > +	if (!base_freq || !turbo_freq) {
> > +		pr_debug("Couldn't determine cpu base or turbo frequency, necessary for scale-invariant accounting.\n");
> >  		return false;
> >  	}
> 
> I've added the below, imagine base_freq > turbo_freq *
> SCHED_CAPACITY_SCALE.

Right, I didn't consider that case. It doesn't hurt to be defensive.

I understand you've already edited the patches in your tree, so I am not
resending, just confirming my

Signed-off-by: Giovanni Gherdovich <ggherdovich@suse.cz>

> 
> --- a/arch/x86/kernel/smpboot.c
> +++ b/arch/x86/kernel/smpboot.c
> @@ -1975,6 +1975,7 @@ static bool core_set_max_freq_ratio(u64
>  static bool intel_set_max_freq_ratio(void)
>  {
>  	u64 base_freq, turbo_freq;
> +	u64 turbo_ratio;
>  
>  	if (slv_set_max_freq_ratio(&base_freq, &turbo_freq))
>  		goto out;
> @@ -2008,9 +2009,15 @@ static bool intel_set_max_freq_ratio(voi
>  		return false;
>  	}
>  
> -	arch_turbo_freq_ratio = div_u64(turbo_freq * SCHED_CAPACITY_SCALE,
> -					base_freq);
> +	turbo_ratio = div_u64(turbo_freq * SCHED_CAPACITY_SCALE, base_freq);
> +	if (!turbo_ratio) {
> +		pr_debug("Non-zero turbo and base frequencies led to a 0 ratio.\n");
> +		return false;
> +	}
> +
> +	arch_turbo_freq_ratio = turbo_ratio;
>  	arch_set_max_freq_ratio(turbo_disabled());
> +
>  	return true;
>  }
>

Patch
diff mbox series

diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index 4718f29a3065..ab2a0df7d1fb 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -1991,9 +1991,11 @@  static bool intel_set_max_freq_ratio(void)
 	/*
 	 * Some hypervisors advertise X86_FEATURE_APERFMPERF
 	 * but then fill all MSR's with zeroes.
+	 * Some CPUs have turbo boost but don't declare any turbo ratio
+	 * in MSR_TURBO_RATIO_LIMIT.
 	 */
-	if (!base_freq) {
-		pr_debug("Couldn't determine cpu base frequency, necessary for scale-invariant accounting.\n");
+	if (!base_freq || !turbo_freq) {
+		pr_debug("Couldn't determine cpu base or turbo frequency, necessary for scale-invariant accounting.\n");
 		return false;
 	}