diff mbox series

ARM, sched/topology: Check return value of kcalloc()

Message ID 20240628194350.542376-2-thorsten.blum@toblux.com (mailing list archive)
State New, archived
Headers show
Series ARM, sched/topology: Check return value of kcalloc() | expand

Commit Message

Thorsten Blum June 28, 2024, 7:43 p.m. UTC
Check the return value of kcalloc() and return early if memory
allocation fails.

Compile-tested only.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
 arch/arm/kernel/topology.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Russell King (Oracle) June 28, 2024, 9:55 p.m. UTC | #1
On Fri, Jun 28, 2024 at 09:43:51PM +0200, Thorsten Blum wrote:
> Check the return value of kcalloc() and return early if memory
> allocation fails.

If we fail this allocation even with your fix, how likely is it that the
system will boot and manage to run userspace?
Thorsten Blum June 29, 2024, 4:09 a.m. UTC | #2
On 28. Jun 2024, at 14:55, Russell King (Oracle) <linux@armlinux.org.uk> wrote:
> On Fri, Jun 28, 2024 at 09:43:51PM +0200, Thorsten Blum wrote:
>> Check the return value of kcalloc() and return early if memory
>> allocation fails.
> 
> If we fail this allocation even with your fix, how likely is it that the
> system will boot and manage to run userspace?

I thought about panicking, but wasn't sure if it's justified.

But since it would just crash anyway when accessing a NULL pointer
later on, it's probably best to panic() if the memory allocation fails.

I'll submit a v2 shortly.

Thanks,
Thorsten
diff mbox series

Patch

diff --git a/arch/arm/kernel/topology.c b/arch/arm/kernel/topology.c
index 2336ee2aa44a..b77d1838e5a3 100644
--- a/arch/arm/kernel/topology.c
+++ b/arch/arm/kernel/topology.c
@@ -93,6 +93,10 @@  static void __init parse_dt_topology(void)
 
 	__cpu_capacity = kcalloc(nr_cpu_ids, sizeof(*__cpu_capacity),
 				 GFP_NOWAIT);
+	if (unlikely(!__cpu_capacity)) {
+		pr_crit("Failed to allocate memory for __cpu_capacity\n");
+		return;
+	}
 
 	for_each_possible_cpu(cpu) {
 		const __be32 *rate;