Skip to contentSkip to conversation

cpufreq/cppc: fix perf_to_khz/khz_to_perf conversion exception

PATCH Superseded liwei 2 participants 3 messages May 6, 2024 – Jul 2, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

liwei

author
#0 lore raw

PATCH v2 1/1 cpufreq/cppc: fix perf_to_khz/khz_to_perf conversion exception

When the nominal_freq recorded by the kernel is equal to the lowest_freq, and the frequency adjustment operation is triggered externally, there is a logic error in cppc_perf_to_khz()/cppc_khz_to_perf(), resulting in perf and khz conversion errors.

Fix this by adding the branch processing logic when nominal_freq is equal to lowest_freq.

Fixes ec1c7ad47664 ("cpufreq: CPPC: Fix performance/frequency conversion")

---
v2:
- Fix similar issue in cppc_khz_to_perf()

 drivers/acpi/cppc_acpi.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
drivers/acpi/cppc_acpi.c +17 -5
index a40b6f3946ef..e3a0d6b46b24 100644
@@ -1869,9 +1869,15 @@ unsigned int cppc_perf_to_khz(struct cppc_perf_caps *caps, unsigned int perf)
 	u64 mul, div;
 
 	if (caps->lowest_freq && caps->nominal_freq) {
-		mul = caps->nominal_freq - caps->lowest_freq;
+		/* Avoid special case when nominal_freq is equal to lowest_freq */
+		if (caps->lowest_freq == caps->nominal_freq) {
+			mul = caps->nominal_freq;
+			div = caps->nominal_perf;
+		} else {
+			mul = caps->nominal_freq - caps->lowest_freq;
+			div = caps->nominal_perf - caps->lowest_perf;
+		}
 		mul *= KHZ_PER_MHZ;
-		div = caps->nominal_perf - caps->lowest_perf;
 		offset = caps->nominal_freq * KHZ_PER_MHZ -
 			 div64_u64(caps->nominal_perf * mul, div);
 	} else {
@@ -1892,11 +1898,17 @@ unsigned int cppc_khz_to_perf(struct cppc_perf_caps *caps, unsigned int freq)
 {
 	s64 retval, offset = 0;
 	static u64 max_khz;
-	u64  mul, div;
+	u64 mul, div;
 
 	if (caps->lowest_freq && caps->nominal_freq) {
-		mul = caps->nominal_perf - caps->lowest_perf;
-		div = caps->nominal_freq - caps->lowest_freq;
+		/* Avoid special case when nominal_freq is equal to lowest_freq */
+		if (caps->lowest_freq == caps->nominal_freq) {
+			mul = caps->nominal_perf;
+			div = caps->nominal_freq;
+		} else {
+			mul = caps->nominal_perf - caps->lowest_perf;
+			div = caps->nominal_freq - caps->lowest_freq;
+		}
 		/*
 		 * We don't need to convert to kHz for computing offset and can
 		 * directly use nominal_freq and lowest_freq as the div64_u64
Signature · 1 line
2.25.1
Acked-by on v2 1/1
#1 lore raw
Quoted 8 lines · liwei

When the nominal_freq recorded by the kernel is equal to the lowest_freq, and the frequency adjustment operation is triggered externally, there is a logic error in cppc_perf_to_khz()/cppc_khz_to_perf(), resulting in perf and khz conversion errors.

Fix this by adding the branch processing logic when nominal_freq is equal to lowest_freq.

Quoted 8 lines
Fixes ec1c7ad47664 ("cpufreq: CPPC: Fix performance/frequency conversion")

---
v2:
- Fix similar issue in cppc_khz_to_perf()

 drivers/acpi/cppc_acpi.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
Signature · 1 line
viresh
author
#2 lore raw

kindly ping

Quoted 20 lines · Viresh Kumar
Quoted 8 lines · liwei

When the nominal_freq recorded by the kernel is equal to the lowest_freq, and the frequency adjustment operation is triggered externally, there is a logic error in cppc_perf_to_khz()/cppc_khz_to_perf(), resulting in perf and khz conversion errors.

Fix this by adding the branch processing logic when nominal_freq is equal to lowest_freq.

Quoted 8 lines
Fixes ec1c7ad47664 ("cpufreq: CPPC: Fix performance/frequency conversion")

---
v2:
- Fix similar issue in cppc_khz_to_perf()

  drivers/acpi/cppc_acpi.c | 22 +++++++++++++++++-----
  1 file changed, 17 insertions(+), 5 deletions(-)