diff mbox series

powercap: Avoid explicit cpumask allocation on stack

Message ID 20240415094821.3060892-1-dawei.li@shingroup.cn (mailing list archive)
State In Next
Delegated to: Rafael Wysocki
Headers show
Series powercap: Avoid explicit cpumask allocation on stack | expand

Commit Message

Dawei Li April 15, 2024, 9:48 a.m. UTC
In general it's preferable to avoid placing cpumasks on the stack, as
for large values of NR_CPUS these can consume significant amounts of
stack space and make stack overflows more likely.

Use cpumask_weight_and() to avoid the need for a temporary cpumask on
the stack.

Signed-off-by: Dawei Li <dawei.li@shingroup.cn>
---
 drivers/powercap/dtpm_cpu.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

Comments

Rafael J. Wysocki April 16, 2024, 11:34 a.m. UTC | #1
On Mon, Apr 15, 2024 at 11:48 AM Dawei Li <dawei.li@shingroup.cn> wrote:
>
> In general it's preferable to avoid placing cpumasks on the stack, as
> for large values of NR_CPUS these can consume significant amounts of
> stack space and make stack overflows more likely.
>
> Use cpumask_weight_and() to avoid the need for a temporary cpumask on
> the stack.
>
> Signed-off-by: Dawei Li <dawei.li@shingroup.cn>
> ---
>  drivers/powercap/dtpm_cpu.c | 8 ++------
>  1 file changed, 2 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/powercap/dtpm_cpu.c b/drivers/powercap/dtpm_cpu.c
> index bc90126f1b5f..6b6f51b21550 100644
> --- a/drivers/powercap/dtpm_cpu.c
> +++ b/drivers/powercap/dtpm_cpu.c
> @@ -43,13 +43,11 @@ static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit)
>         struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
>         struct em_perf_domain *pd = em_cpu_get(dtpm_cpu->cpu);
>         struct em_perf_state *table;
> -       struct cpumask cpus;
>         unsigned long freq;
>         u64 power;
>         int i, nr_cpus;
>
> -       cpumask_and(&cpus, cpu_online_mask, to_cpumask(pd->cpus));
> -       nr_cpus = cpumask_weight(&cpus);
> +       nr_cpus = cpumask_weight_and(cpu_online_mask, to_cpumask(pd->cpus));
>
>         rcu_read_lock();
>         table = em_perf_state_from_pd(pd);
> @@ -123,11 +121,9 @@ static int update_pd_power_uw(struct dtpm *dtpm)
>         struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
>         struct em_perf_domain *em = em_cpu_get(dtpm_cpu->cpu);
>         struct em_perf_state *table;
> -       struct cpumask cpus;
>         int nr_cpus;
>
> -       cpumask_and(&cpus, cpu_online_mask, to_cpumask(em->cpus));
> -       nr_cpus = cpumask_weight(&cpus);
> +       nr_cpus = cpumask_weight_and(cpu_online_mask, to_cpumask(em->cpus));
>
>         rcu_read_lock();
>         table = em_perf_state_from_pd(em);
> --

Applied as 6.10 material, thanks!
diff mbox series

Patch

diff --git a/drivers/powercap/dtpm_cpu.c b/drivers/powercap/dtpm_cpu.c
index bc90126f1b5f..6b6f51b21550 100644
--- a/drivers/powercap/dtpm_cpu.c
+++ b/drivers/powercap/dtpm_cpu.c
@@ -43,13 +43,11 @@  static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit)
 	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
 	struct em_perf_domain *pd = em_cpu_get(dtpm_cpu->cpu);
 	struct em_perf_state *table;
-	struct cpumask cpus;
 	unsigned long freq;
 	u64 power;
 	int i, nr_cpus;
 
-	cpumask_and(&cpus, cpu_online_mask, to_cpumask(pd->cpus));
-	nr_cpus = cpumask_weight(&cpus);
+	nr_cpus = cpumask_weight_and(cpu_online_mask, to_cpumask(pd->cpus));
 
 	rcu_read_lock();
 	table = em_perf_state_from_pd(pd);
@@ -123,11 +121,9 @@  static int update_pd_power_uw(struct dtpm *dtpm)
 	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
 	struct em_perf_domain *em = em_cpu_get(dtpm_cpu->cpu);
 	struct em_perf_state *table;
-	struct cpumask cpus;
 	int nr_cpus;
 
-	cpumask_and(&cpus, cpu_online_mask, to_cpumask(em->cpus));
-	nr_cpus = cpumask_weight(&cpus);
+	nr_cpus = cpumask_weight_and(cpu_online_mask, to_cpumask(em->cpus));
 
 	rcu_read_lock();
 	table = em_perf_state_from_pd(em);