Skip to contentSkip to conversation

cpufreq/cppc: fix perf_to_khz conversion exception

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

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

liwei

author
#0 lore raw

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

When the nominal_freq recorded by the kernel is equal to lowest_freq and the frequency reduction operation is triggered externally, there is a conversion logic error in cppc_perf_to_khz(), causing the kernel to be unable to feedback the true frequency.

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

Fixes ec1c7ad47664 ("cpufreq: CPPC: Fix performance/frequency conversion")
---
 drivers/acpi/cppc_acpi.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/acpi/cppc_acpi.c +8 -2
index a40b6f3946ef..92aac6974e0e 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 the 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 {
Signature · 1 line
2.25.1
#1 lore raw

Hello Liwei,

Thanks for the fix. I think there might be a similar issue in cppc_khz_to_perf(),

Regards,
Pierre

Quoted 36 lines · liwei

When the nominal_freq recorded by the kernel is equal to lowest_freq and the frequency reduction operation is triggered externally, there is a conversion logic error in cppc_perf_to_khz(), causing the kernel to be unable to feedback the true frequency.

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

Fixes ec1c7ad47664 ("cpufreq: CPPC: Fix performance/frequency conversion")
---
  drivers/acpi/cppc_acpi.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
drivers/acpi/cppc_acpi.c +8 -2
index a40b6f3946ef..92aac6974e0e 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 the 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 {
author
#2 lore raw

Hello Pierre,

Thanks for your reminder.

Quoted 8 lines · Pierre Gondois

Hello Liwei,

Thanks for the fix. I think there might be a similar issue in cppc_khz_to_perf(),

Regards,
Pierre

cppc_khz_to_perf() also has similar issue, I will modify the patch again.

Liwei

Quoted 38 lines · liwei
Quoted 37 lines

When the nominal_freq recorded by the kernel is equal to lowest_freq and the frequency reduction operation is triggered externally, there is a conversion logic error in cppc_perf_to_khz(), causing the kernel to be unable to feedback the true frequency.

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

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

---
  drivers/acpi/cppc_acpi.c | 10 ++++++++--

  1 file changed, 8 insertions(+), 2 deletions(-)
drivers/acpi/cppc_acpi.c +1 -1
index a40b6f3946ef..92aac6974e0e 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 the 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 {