Skip to contentSkip to conversation

cpufreq: acpi-cpufreq: Optimize SMP calls in drv_write() function

PATCH In review Kaushlendra Kumar 2 participants 4 messages Sep 4, 2025 – Sep 8, 2025 CPU frequency scaling framework

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpufreq: acpi-cpufreq: Optimize SMP calls in drv_write() function

The current drv_write() implementation performs redundant work by calling do_drv_write() on the current CPU and then issuing smp_call_function_many() to all CPUs in the mask, including the current CPU again. This results in duplicate MSR writes on the current CPU and unnecessary IPI overhead.

Optimize the function by:
- Executing do_drv_write() locally when current CPU is in the target mask
- Use a temporary mask to exclude the current CPU from smp_call_function_many.
- Falling back to smp_call_function_many() for the target CPUs when current CPU is not in the target mask

---
 drivers/cpufreq/acpi-cpufreq.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/cpufreq/acpi-cpufreq.c +7 -2
index 4f7f9201598d..081d2d8a13da 100644
@@ -330,6 +330,7 @@ static void drv_write(struct acpi_cpufreq_data *data,
 		      const struct cpumask *mask, u32 val)
 {
 	struct acpi_processor_performance *perf = to_perf_data(data);
+	struct cpumask tmp_mask;
 	struct drv_cmd cmd = {
 		.reg = &perf->control_register,
 		.val = val,
@@ -338,10 +339,14 @@ static void drv_write(struct acpi_cpufreq_data *data,
 	int this_cpu;
 
 	this_cpu = get_cpu();
-	if (cpumask_test_cpu(this_cpu, mask))
+	if (cpumask_test_cpu(this_cpu, mask)) {
 		do_drv_write(&cmd);
+		cpumask_andnot(&tmp_mask, mask, cpumask_of(this_cpu));
+		smp_call_function_many(&tmp_mask, do_drv_write, &cmd, 1);
+	} else {
+		smp_call_function_many(mask, do_drv_write, &cmd, 1);
+	}
 
-	smp_call_function_many(mask, do_drv_write, &cmd, 1);
 	put_cpu();
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · Kaushlendra Kumar

The current drv_write() implementation performs redundant work by calling do_drv_write() on the current CPU and then issuing smp_call_function_many() to all CPUs in the mask, including the current CPU again. This results in duplicate MSR writes on the current CPU and unnecessary IPI overhead.

But smp_call_function_many() doesn't run func() on the local CPU, does it?

So what do you really do in this patch?

Quoted 43 lines

Optimize the function by:
- Executing do_drv_write() locally when current CPU is in the target mask
- Use a temporary mask to exclude the current CPU from smp_call_function_many.
- Falling back to smp_call_function_many() for the target CPUs when current CPU is not in the target mask

---
 drivers/cpufreq/acpi-cpufreq.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/cpufreq/acpi-cpufreq.c +7 -3
index 4f7f9201598d..081d2d8a13da 100644
@@ -330,6 +330,7 @@ static void drv_write(struct acpi_cpufreq_data *data,
                      const struct cpumask *mask, u32 val)
 {
        struct acpi_processor_performance *perf = to_perf_data(data);
+       struct cpumask tmp_mask;
        struct drv_cmd cmd = {
                .reg = &perf->control_register,
                .val = val,
@@ -338,10 +339,14 @@ static void drv_write(struct acpi_cpufreq_data *data,
        int this_cpu;
 
        this_cpu = get_cpu();
-       if (cpumask_test_cpu(this_cpu, mask))
+       if (cpumask_test_cpu(this_cpu, mask)) {
                do_drv_write(&cmd);
+               cpumask_andnot(&tmp_mask, mask, cpumask_of(this_cpu));
+               smp_call_function_many(&tmp_mask, do_drv_write, &cmd, 1);
+       } else {
+               smp_call_function_many(mask, do_drv_write, &cmd, 1);
+       }
 
-       smp_call_function_many(mask, do_drv_write, &cmd, 1);
        put_cpu();
 }
 
--
#2 lore raw
Quoted 3 lines

But smp_call_function_many() doesn't run func() on the local CPU, does it?

So what do you really do in this patch?

Thanks for the review feedback. I've examined the function documentation for smp_call_function_many():

/**
* smp_call_function_many(): Run a function on a set of CPUs.
* @mask: The set of cpus to run on (only runs on online subset).
* @func: The function to run. This must be fast and non-blocking.
* @info: An arbitrary pointer to pass to the function.
* @wait: Bitmask that controls the operation. If %SCF_WAIT is set, wait

 *        (atomically) until function has completed on other CPUs. If
 *        %SCF_RUN_LOCAL is set, the function will also be run locally
 *        if the local CPU is set in the @cpumask.

*
* If @wait is true, then returns once @func has returned.
*
* You must not call this function with disabled interrupts or from a
* hardware interrupt handler or from a bottom half handler. Preemption
* must be disabled when calling this function.
*/

Based on this documentation, when the SCF_RUN_LOCAL flag is there and the current CPU is in the mask, it will get called on local CPU also.

#3 lore raw
Quoted 13 lines · Kaushlendra
Quoted 4 lines

But smp_call_function_many() doesn't run func() on the local CPU, does it?

So what do you really do in this patch?

Thanks for the review feedback. I've examined the function documentation for smp_call_function_many():

/**
* smp_call_function_many(): Run a function on a set of CPUs.
* @mask: The set of cpus to run on (only runs on online subset).
* @func: The function to run. This must be fast and non-blocking.
* @info: An arbitrary pointer to pass to the function.
* @wait: Bitmask that controls the operation. If %SCF_WAIT is set, wait

Quoted 3 lines
 *        (atomically) until function has completed on other CPUs. If
 *        %SCF_RUN_LOCAL is set, the function will also be run locally
 *        if the local CPU is set in the @cpumask.

@wait is not a bitmask, so the above description is not accurate.

Quoted 9 lines

*
* If @wait is true, then returns once @func has returned.
*
* You must not call this function with disabled interrupts or from a
* hardware interrupt handler or from a bottom half handler. Preemption
* must be disabled when calling this function.
*/

Based on this documentation, when the SCF_RUN_LOCAL flag is there and the current CPU is in the mask, it will get called on local CPU also.

This applies to smp_call_function_many_cond() which is called by smp_call_function_many() with the mask of either SCF_WAIT or 0.

In any case, the $subject patch isn't quite right.