[PATCH 9/9] perf/thunder_x2: Avoid explicit cpumask var allocation from stack

Dawei Li dawei.li at shingroup.cn
Tue Apr 2 03:56:10 PDT 2024


For CONFIG_CPUMASK_OFFSTACK=y kernel, explicit allocation of cpumask
variable on stack is not recommended since it can cause potential stack
overflow.

Instead, kernel code should always use *cpumask_var API(s) to allocate
cpumask var in config- neutral way, leaving allocation strategy to
CONFIG_CPUMASK_OFFSTACK.

Use *cpumask_var API(s) to address it.

Signed-off-by: Dawei Li <dawei.li at shingroup.cn>
---
 drivers/perf/thunderx2_pmu.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/drivers/perf/thunderx2_pmu.c b/drivers/perf/thunderx2_pmu.c
index e16d10c763de..8a02a4533b32 100644
--- a/drivers/perf/thunderx2_pmu.c
+++ b/drivers/perf/thunderx2_pmu.c
@@ -932,9 +932,9 @@ static int tx2_uncore_pmu_online_cpu(unsigned int cpu,
 static int tx2_uncore_pmu_offline_cpu(unsigned int cpu,
 		struct hlist_node *hpnode)
 {
-	int new_cpu;
+	cpumask_var_t cpu_online_mask_temp;
 	struct tx2_uncore_pmu *tx2_pmu;
-	struct cpumask cpu_online_mask_temp;
+	int new_cpu;
 
 	tx2_pmu = hlist_entry_safe(hpnode,
 			struct tx2_uncore_pmu, hpnode);
@@ -945,17 +945,21 @@ static int tx2_uncore_pmu_offline_cpu(unsigned int cpu,
 	if (tx2_pmu->hrtimer_callback)
 		hrtimer_cancel(&tx2_pmu->hrtimer);
 
-	cpumask_copy(&cpu_online_mask_temp, cpu_online_mask);
-	cpumask_clear_cpu(cpu, &cpu_online_mask_temp);
-	new_cpu = cpumask_any_and(
-			cpumask_of_node(tx2_pmu->node),
-			&cpu_online_mask_temp);
+	if (!alloc_cpumask_var(&cpu_online_mask_temp, GFP_KERNEL))
+		return 0;
+
+	cpumask_copy(cpu_online_mask_temp, cpu_online_mask);
+	cpumask_clear_cpu(cpu, cpu_online_mask_temp);
+	new_cpu = cpumask_any_and(cpumask_of_node(tx2_pmu->node),
+				  cpu_online_mask_temp);
 
 	tx2_pmu->cpu = new_cpu;
 	if (new_cpu >= nr_cpu_ids)
-		return 0;
+		goto __free_cpumask;
 	perf_pmu_migrate_context(&tx2_pmu->pmu, cpu, new_cpu);
 
+__free_cpumask:
+	free_cpumask_var(cpu_online_mask_temp);
 	return 0;
 }
 
-- 
2.27.0




More information about the linux-arm-kernel mailing list