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

From: Dawei Li
Date: Tue Apr 02 2024 - 07:00:05 EST


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@xxxxxxxxxxxx>
---
drivers/perf/arm_dsu_pmu.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/drivers/perf/arm_dsu_pmu.c b/drivers/perf/arm_dsu_pmu.c
index bae3ca37f846..87efdca05807 100644
--- a/drivers/perf/arm_dsu_pmu.c
+++ b/drivers/perf/arm_dsu_pmu.c
@@ -230,13 +230,21 @@ static const struct attribute_group *dsu_pmu_attr_groups[] = {
NULL,
};

-static int dsu_pmu_get_online_cpu_any_but(struct dsu_pmu *dsu_pmu, int cpu)
+static unsigned int dsu_pmu_get_online_cpu_any_but(struct dsu_pmu *dsu_pmu, int cpu)
{
- struct cpumask online_supported;
+ cpumask_var_t online_supported;
+ unsigned int ret;

- cpumask_and(&online_supported,
- &dsu_pmu->associated_cpus, cpu_online_mask);
- return cpumask_any_but(&online_supported, cpu);
+ if (!alloc_cpumask_var(&online_supported, GFP_KERNEL))
+ return -ENOMEM;
+
+ cpumask_and(online_supported,
+ &dsu_pmu->associated_cpus, cpu_online_mask);
+ ret = cpumask_any_but(&online_supported, cpu);
+
+ free_cpumask_var(online_supported);
+
+ return ret;
}

static inline bool dsu_pmu_counter_valid(struct dsu_pmu *dsu_pmu, u32 idx)
--
2.27.0