Skip to contentSkip to conversation

x86: Fix return value in frequency invariance setup for XEON_PHI_KNL/KNM

PATCH In review Giovanni Gherdovich 3 participants 6 messages May 20, 2022 – May 23, 2022 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86: Fix return value in frequency invariance setup for XEON_PHI_KNL/KNM

knl_set_max_freq_ratio() should return true on success and false otherwise. If the last line of the function body is reached, it means no appropriate value for turbo_freq was found: the setup is unsuccessful and it should return false.

Fixes 8bea0dfb4a82 ("x86, sched: Add support for frequency invariance on XEON_PHI_KNL/KNM")
---
 arch/x86/kernel/smpboot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/smpboot.c +1 -1
index 2ef14772dc04..225a3c31297c 100644
@@ -1985,7 +1985,7 @@ static bool knl_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq,
 		i += 8;
 	} while (i < 64);
 
-	return true;
+	return false;
 }
 
 static bool skx_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq, int size)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 2/2 x86: fix platform info detection in frequency invariance

Once the microarchitecture is recognized (via x86_match_cpu()), a failure in setting base_freq/turbo_freq should result in bailing out from frequency invariance, not in trying the next microarchitecture. This is because the call to core_set_max_freq_ratio() isn't guarded by x86_match_cpu(). The call to core_set_max_freq_ratio() should happen if no more specific microarch matched, but not in case of prior errors.

Initializing base_freq=0 and turbo_freq=0 gives a mean for later code to check if setup failed.

Fixes db441bd9f630 ("x86, sched: Move check for CPU type to caller function")
---
 arch/x86/kernel/smpboot.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
arch/x86/kernel/smpboot.c +10 -7
index 225a3c31297c..d0a692ea8294 100644
@@ -2044,23 +2044,26 @@ static bool core_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq)
 
 static bool intel_set_max_freq_ratio(void)
 {
-	u64 base_freq, turbo_freq;
+	u64 base_freq = 0, turbo_freq = 0;
 	u64 turbo_ratio;
 
 	if (slv_set_max_freq_ratio(&base_freq, &turbo_freq))
 		goto out;
 
-	if (x86_match_cpu(has_glm_turbo_ratio_limits) &&
-	    skx_set_max_freq_ratio(&base_freq, &turbo_freq, 1))
+	if (x86_match_cpu(has_glm_turbo_ratio_limits)) {
+		skx_set_max_freq_ratio(&base_freq, &turbo_freq, 1);
 		goto out;
+	}
 
-	if (x86_match_cpu(has_knl_turbo_ratio_limits) &&
-	    knl_set_max_freq_ratio(&base_freq, &turbo_freq, 1))
+	if (x86_match_cpu(has_knl_turbo_ratio_limits)) {
+		knl_set_max_freq_ratio(&base_freq, &turbo_freq, 1);
 		goto out;
+	}
 
-	if (x86_match_cpu(has_skx_turbo_ratio_limits) &&
-	    skx_set_max_freq_ratio(&base_freq, &turbo_freq, 4))
+	if (x86_match_cpu(has_skx_turbo_ratio_limits)) {
+		skx_set_max_freq_ratio(&base_freq, &turbo_freq, 4);
 		goto out;
+	}
 
 	if (core_set_max_freq_ratio(&base_freq, &turbo_freq))
 		goto out;
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH 2/2] x86: fix platform info detection in frequency invariance

Quoted 16 lines · Giovanni Gherdovich
arch/x86/kernel/smpboot.c +6 -4
 	if (slv_set_max_freq_ratio(&base_freq, &turbo_freq))
 		goto out;
 
-	if (x86_match_cpu(has_glm_turbo_ratio_limits) &&
-	    skx_set_max_freq_ratio(&base_freq, &turbo_freq, 1))
+	if (x86_match_cpu(has_glm_turbo_ratio_limits)) {
+		skx_set_max_freq_ratio(&base_freq, &turbo_freq, 1);
 		goto out;
+	}
 
-	if (x86_match_cpu(has_knl_turbo_ratio_limits) &&
-	    knl_set_max_freq_ratio(&base_freq, &turbo_freq, 1))
+	if (x86_match_cpu(has_knl_turbo_ratio_limits)) {
+		knl_set_max_freq_ratio(&base_freq, &turbo_freq, 1);
 		goto out;
+	}

arch/x86/kernel/smpboot.c static bool core_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq) · line 2069

Quoted 10 lines
arch/x86/kernel/smpboot.c +3 -2
 
-	if (x86_match_cpu(has_skx_turbo_ratio_limits) &&
-	    skx_set_max_freq_ratio(&base_freq, &turbo_freq, 4))
+	if (x86_match_cpu(has_skx_turbo_ratio_limits)) {
+		skx_set_max_freq_ratio(&base_freq, &turbo_freq, 4);
 		goto out;
+	}
 
 	if (core_set_max_freq_ratio(&base_freq, &turbo_freq))
 		goto out;

But didn't the last patch in the series carefully change the return value for knl_set_max_freq_ratio()? Now, the only call site is ignoring the return value? That seems odd.

Also, this is a mess. These constructs:

static const struct x86_cpu_id has_knl_turbo_ratio_limits[] = {
        X86_MATCH(XEON_PHI_KNL),
        X86_MATCH(XEON_PHI_KNM),
        {}
};
static const struct x86_cpu_id has_skx_turbo_ratio_limits[] = {
        X86_MATCH(SKYLAKE_X),
        {}
};
static const struct x86_cpu_id has_glm_turbo_ratio_limits[] = {
        X86_MATCH(ATOM_GOLDMONT),
        X86_MATCH(ATOM_GOLDMONT_D),
        X86_MATCH(ATOM_GOLDMONT_PLUS),
        {}
};

are rather goofy. A single array like rapl_ids[] that points to the handler function would do us a lot more good here, say:

static const struct x86_cpu_id has_knl_turbo_ratio_limits[] = {
        X86_MATCH(XEON_PHI_KNL, 	&knl_set_max_freq_ratio),
        X86_MATCH(XEON_PHI_KNM, 	&knl_set_max_freq_ratio),
        X86_MATCH(SKYLAKE_X,		&skx_set_max_freq_ratio),
        X86_MATCH(ATOM_GOLDMONT, 	&skx_set_max_freq_ratio),
        X86_MATCH(ATOM_GOLDMONT_D,	&skx_set_max_freq_ratio),
        X86_MATCH(ATOM_GOLDMONT_PLUS,	&skx_set_max_freq_ratio),
	X86_MATCH(ANY,			&core_set_max_freq_ratio),
        {}
};

That would get rid of all the goofy gotos and actually puts all the logic in one place. BTW, I'm not 100% sure about the 'ANY' line. I think that's how those work, but please double-check me on it.

While it's generally best to keep bug fixes to a minimum, I think this one is worth a bit of a cleanup because it will remove a bunch of spaghetti.

#3 lore raw

Re: [PATCH 2/2] x86: fix platform info detection in frequency invariance

Quoted 25 lines · Dave Hansen
Quoted 24 lines · Giovanni Gherdovich
arch/x86/kernel/smpboot.c +9 -6
 	if (slv_set_max_freq_ratio(&base_freq, &turbo_freq))
 		goto out;
 
-	if (x86_match_cpu(has_glm_turbo_ratio_limits) &&
-	    skx_set_max_freq_ratio(&base_freq, &turbo_freq, 1))
+	if (x86_match_cpu(has_glm_turbo_ratio_limits)) {
+		skx_set_max_freq_ratio(&base_freq, &turbo_freq, 1);
 		goto out;
+	}
 
-	if (x86_match_cpu(has_knl_turbo_ratio_limits) &&
-	    knl_set_max_freq_ratio(&base_freq, &turbo_freq, 1))
+	if (x86_match_cpu(has_knl_turbo_ratio_limits)) {
+		knl_set_max_freq_ratio(&base_freq, &turbo_freq, 1);
 		goto out;
+	}
 
-	if (x86_match_cpu(has_skx_turbo_ratio_limits) &&
-	    skx_set_max_freq_ratio(&base_freq, &turbo_freq, 4))
+	if (x86_match_cpu(has_skx_turbo_ratio_limits)) {
+		skx_set_max_freq_ratio(&base_freq, &turbo_freq, 4);
 		goto out;
+	}
Quoted 6 lines

arch/x86/kernel/smpboot.c static bool core_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq) · line 2069

Quoted 2 lines
arch/x86/kernel/smpboot.c +0 -0
 	if (core_set_max_freq_ratio(&base_freq, &turbo_freq))
 		goto out;

But didn't the last patch in the series carefully change the return value for knl_set_max_freq_ratio()? Now, the only call site is ignoring the return value? That seems odd.

Thanks for having a look! You're right. I need to either check these return values, or not have them at all.

Quoted 35 lines

Also, this is a mess. These constructs:

static const struct x86_cpu_id has_knl_turbo_ratio_limits[] = {
        X86_MATCH(XEON_PHI_KNL),
        X86_MATCH(XEON_PHI_KNM),
        {}
};
static const struct x86_cpu_id has_skx_turbo_ratio_limits[] = {
        X86_MATCH(SKYLAKE_X),
        {}
};
static const struct x86_cpu_id has_glm_turbo_ratio_limits[] = {
        X86_MATCH(ATOM_GOLDMONT),
        X86_MATCH(ATOM_GOLDMONT_D),
        X86_MATCH(ATOM_GOLDMONT_PLUS),
        {}
};

are rather goofy. A single array like rapl_ids[] that points to the handler function would do us a lot more good here, say:

static const struct x86_cpu_id has_knl_turbo_ratio_limits[] = {
        X86_MATCH(XEON_PHI_KNL, 	&knl_set_max_freq_ratio),
        X86_MATCH(XEON_PHI_KNM, 	&knl_set_max_freq_ratio),
        X86_MATCH(SKYLAKE_X,		&skx_set_max_freq_ratio),
        X86_MATCH(ATOM_GOLDMONT, 	&skx_set_max_freq_ratio),
        X86_MATCH(ATOM_GOLDMONT_D,	&skx_set_max_freq_ratio),
        X86_MATCH(ATOM_GOLDMONT_PLUS,	&skx_set_max_freq_ratio),
	X86_MATCH(ANY,			&core_set_max_freq_ratio),
        {}
};
Quoted 3 lines

That would get rid of all the goofy gotos and actually puts all the logic in one place. BTW, I'm not 100% sure about the 'ANY' line. I think that's how those work, but please double-check me on it.

That's good advice. I'll do that consolidation.

Quoted 3 lines

While it's generally best to keep bug fixes to a minimum, I think this one is worth a bit of a cleanup because it will remove a bunch of spaghetti.

Thanks,
Giovanni

#4 lore raw
In reply to Giovanni Gherdovich
Quoted 10 lines · Giovanni Gherdovich

knl_set_max_freq_ratio() should return true on success and false otherwise. If the last line of the function body is reached, it means no appropriate value for turbo_freq was found: the setup is unsuccessful and it should return false.

Fixes 8bea0dfb4a82 ("x86, sched: Add support for frequency invariance on XEON_PHI_KNL/KNM")
---
 arch/x86/kernel/smpboot.c | 2 +-

You seems to have missed all that code moved in tip. This no longer applies.

#5 lore raw
Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Giovanni Gherdovich

knl_set_max_freq_ratio() should return true on success and false otherwise. If the last line of the function body is reached, it means no appropriate value for turbo_freq was found: the setup is unsuccessful and it should return false.

Quoted 8 lines
Quoted 5 lines
Fixes 8bea0dfb4a82 ("x86, sched: Add support for frequency invariance on XEON_PHI_KNL/KNM")
---
 arch/x86/kernel/smpboot.c | 2 +-

You seems to have missed all that code moved in tip. This no longer applies.

Right. I'll rebase and resend. Plus, as per Dave Hansen's comments on the other patch, I need to have a second look at it.

Giovanni