[PATCH] ARM, sched/topology: fix possible memory allocation failure

From: huhai
Date: Tue Jul 12 2022 - 22:14:31 EST


From: huhai <huhai@xxxxxxxxxx>

When kcalloc in parse_dt_topology() fails and it will lead to a null
pointer access.

Considering update_cpu_capacity() also uses __cpu_capacity, and refer to
the implementation of parse_dt_topology() in drivers/base/arch_topology.c,
use global variables to store capacity in this patch.

Signed-off-by: huhai <huhai@xxxxxxxxxx>
---
arch/arm/kernel/topology.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/arch/arm/kernel/topology.c b/arch/arm/kernel/topology.c
index ef0058de432b..383eaac1dbd2 100644
--- a/arch/arm/kernel/topology.c
+++ b/arch/arm/kernel/topology.c
@@ -68,8 +68,7 @@ static const struct cpu_efficiency table_efficiency[] = {
{NULL, },
};

-static unsigned long *__cpu_capacity;
-#define cpu_capacity(cpu) __cpu_capacity[cpu]
+static unsigned long cpu_capacity[NR_CPUS];

static unsigned long middle_capacity = 1;
static bool cap_from_dt = true;
@@ -91,9 +90,6 @@ static void __init parse_dt_topology(void)
unsigned long capacity = 0;
int cpu = 0;

- __cpu_capacity = kcalloc(nr_cpu_ids, sizeof(*__cpu_capacity),
- GFP_NOWAIT);
-
for_each_possible_cpu(cpu) {
const __be32 *rate;
int len;
@@ -135,7 +131,7 @@ static void __init parse_dt_topology(void)
if (capacity > max_capacity)
max_capacity = capacity;

- cpu_capacity(cpu) = capacity;
+ cpu_capacity[cpu] = capacity;
}

/* If min and max capacities are equals, we bypass the update of the
@@ -163,10 +159,10 @@ static void __init parse_dt_topology(void)
*/
static void update_cpu_capacity(unsigned int cpu)
{
- if (!cpu_capacity(cpu) || cap_from_dt)
+ if (!cpu_capacity[cpu] || cap_from_dt)
return;

- topology_set_cpu_scale(cpu, cpu_capacity(cpu) / middle_capacity);
+ topology_set_cpu_scale(cpu, cpu_capacity[cpu] / middle_capacity);

pr_info("CPU%u: update cpu_capacity %lu\n",
cpu, topology_get_cpu_scale(cpu));
--
2.27.0