[PATCH] powerpc/smp: Add check for kcalloc() in parse_thread_groups()

Guangshuo Li posted 1 patch 1 week, 6 days ago
There is a newer version of this series
arch/powerpc/kernel/smp.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[PATCH] powerpc/smp: Add check for kcalloc() in parse_thread_groups()
Posted by Guangshuo Li 1 week, 6 days ago
As kcalloc() may fail, check its return value to avoid a NULL pointer
dereference when passing it to of_property_read_u32_array().

Fixes: 790a1662d3a26 ("powerpc/smp: Parse ibm,thread-groups with multiple properties")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 arch/powerpc/kernel/smp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/kernel/smp.c b/arch/powerpc/kernel/smp.c
index dac45694a9c9..34de27d75b1b 100644
--- a/arch/powerpc/kernel/smp.c
+++ b/arch/powerpc/kernel/smp.c
@@ -822,9 +822,9 @@ static int parse_thread_groups(struct device_node *dn,
 
 	count = of_property_count_u32_elems(dn, "ibm,thread-groups");
 	thread_group_array = kcalloc(count, sizeof(u32), GFP_KERNEL);
-	if (!thread_group_array) {
-	ret = -ENOMEM;
-	goto out_free;
+	if (!thread_group_array) {	/* check kcalloc() to avoid NULL deref */
+		ret = -ENOMEM;
+		goto out_free;
 	}
 	ret = of_property_read_u32_array(dn, "ibm,thread-groups",
 					 thread_group_array, count);
-- 
2.43.0
Re: [PATCH] powerpc/smp: Add check for kcalloc() in parse_thread_groups()
Posted by Thomas Weißschuh 1 week, 6 days ago
On Thu, Sep 18, 2025 at 05:34:15PM +0800, Guangshuo Li wrote:
> As kcalloc() may fail, check its return value to avoid a NULL pointer
> dereference when passing it to of_property_read_u32_array().
> 
> Fixes: 790a1662d3a26 ("powerpc/smp: Parse ibm,thread-groups with multiple properties")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> ---
>  arch/powerpc/kernel/smp.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/powerpc/kernel/smp.c b/arch/powerpc/kernel/smp.c
> index dac45694a9c9..34de27d75b1b 100644
> --- a/arch/powerpc/kernel/smp.c
> +++ b/arch/powerpc/kernel/smp.c
> @@ -822,9 +822,9 @@ static int parse_thread_groups(struct device_node *dn,
>  
>  	count = of_property_count_u32_elems(dn, "ibm,thread-groups");
>  	thread_group_array = kcalloc(count, sizeof(u32), GFP_KERNEL);
> -	if (!thread_group_array) {
> -	ret = -ENOMEM;
> -	goto out_free;
> +	if (!thread_group_array) {	/* check kcalloc() to avoid NULL deref */
> +		ret = -ENOMEM;
> +		goto out_free;

Thanks for the patch!

This diff looks wrong, did you forget to squash two commits?
The comment is unnecessary.
It is enough to just 'return -ENOMEM'.
The return value documentation needs an update, too.

>  	}
>  	ret = of_property_read_u32_array(dn, "ibm,thread-groups",
>  					 thread_group_array, count);
> -- 
> 2.43.0
>