Skip to contentSkip to conversation

powerpc/smp: Handle of_property_count_u32_elems() error in parse_thread_groups()

PATCH In review Nathan Chancellor 3 participants 4 messages Nov 9 – Nov 11 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/smp: Handle of_property_count_u32_elems() error in parse_thread_groups()

After commit 0af298e62e5c ("treewide: refresh kmalloc_obj() conversions"), there is a warning on boot for several PowerPC configurations in QEMU:

  WARNING: mm/slub.c:5459 at ___kmalloc_large_node+0x174/0x1b0, CPU#0: swapper/0/1
  Modules linked in:
  CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 7.4.0-rc1-00262-g0af298e62e5c #1 PREEMPT(lazy)
  NIP [c0000000006074e4] ___kmalloc_large_node+0x174/0x1b0
  LR [c00000000060782c] __kmalloc_large_node_noprof+0x3c/0x150
  Call Trace:
  ...
  [c00000000369fb60] [c000000000614ff0] __kmalloc_noprof+0x490/0x690
  [c00000000369fbe0] [c000000000051308] _kzalloc_noprof.constprop.0+0x1c/0x74
  [c00000000369fc00] [c00000000200b450] init_thread_group_cache_map+0xc0/0x2c4
  ...

In this case, of_property_count_u32_elems() returns -EINVAL because there is no "ibm,thread-groups" property. When kcalloc() was used to allocate thread_group_array, the multiplication overflow check in _kmalloc_array_noprof() caught that a really large allocation was requested (as -EINVAL was converted to size_t) and returned NULL. kzalloc_objs() has no such check, so the large allocation check in __kmalloc_large_noprof() triggers the visible warning above.

Check if of_property_count_u32_elems() has returned an error and bail out if so, clearing up the warning.

Fixes 790a1662d3a2 ("powerpc/smp: Parse ibm,thread-groups with multiple properties")
---
 arch/powerpc/kernel/smp.c | 2 ++
 1 file changed, 2 insertions(+)
arch/powerpc/kernel/smp.c +2 -0
index 7a25eaa59e62..58824f5f4c80 100644
@@ -824,6 +824,8 @@ static int parse_thread_groups(struct device_node *dn,
 	int i = 0;
 
 	count = of_property_count_u32_elems(dn, "ibm,thread-groups");
+	if (count < 0)
+		return count;
 	thread_group_array = kzalloc_objs(u32, count);
 	if (!thread_group_array)
 		return -ENOMEM;

---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
change-id: 20261109-powerpc-smp-handle-count-err-b48b9ffd6e90

Best regards,

Signature · 2 lines
Cheers,
Nathan
reviewer Reviewed-by on v1 1/1
#1 lore raw

Hi Nathan. Thanks for the fix.

Quoted 24 lines · Nathan Chancellor

After commit 0af298e62e5c ("treewide: refresh kmalloc_obj() conversions"), there is a warning on boot for several PowerPC configurations in QEMU:

   WARNING: mm/slub.c:5459 at ___kmalloc_large_node+0x174/0x1b0, CPU#0: swapper/0/1
   Modules linked in:
   CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 7.4.0-rc1-00262-g0af298e62e5c #1 PREEMPT(lazy)
   NIP [c0000000006074e4] ___kmalloc_large_node+0x174/0x1b0
   LR [c00000000060782c] __kmalloc_large_node_noprof+0x3c/0x150
   Call Trace:
   ...
   [c00000000369fb60] [c000000000614ff0] __kmalloc_noprof+0x490/0x690
   [c00000000369fbe0] [c000000000051308] _kzalloc_noprof.constprop.0+0x1c/0x74
   [c00000000369fc00] [c00000000200b450] init_thread_group_cache_map+0xc0/0x2c4
   ...

In this case, of_property_count_u32_elems() returns -EINVAL because there is no "ibm,thread-groups" property. When kcalloc() was used to allocate thread_group_array, the multiplication overflow check in _kmalloc_array_noprof() caught that a really large allocation was requested (as -EINVAL was converted to size_t) and returned NULL. kzalloc_objs() has no such check, so the large allocation check in __kmalloc_large_noprof() triggers the visible warning above.

Quoted 5 lines

Check if of_property_count_u32_elems() has returned an error and bail out if so, clearing up the warning.

Fixes 790a1662d3a2 ("powerpc/smp: Parse ibm,thread-groups with multiple properties")

Looks fine to me.

Quoted 23 lines
---
  arch/powerpc/kernel/smp.c | 2 ++
  1 file changed, 2 insertions(+)
arch/powerpc/kernel/smp.c +2 -1
index 7a25eaa59e62..58824f5f4c80 100644
@@ -824,6 +824,8 @@ static int parse_thread_groups(struct device_node *dn,
  	int i = 0;
 
  	count = of_property_count_u32_elems(dn, "ibm,thread-groups");
+	if (count < 0)
+		return count;
  	thread_group_array = kzalloc_objs(u32, count);
  	if (!thread_group_array)
  		return -ENOMEM;
 
---

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
change-id: 20261109-powerpc-smp-handle-count-err-b48b9ffd6e90

Quoted 5 lines

Best regards,
--
Cheers,
Nathan

Should the similar fix is needed here as well?

arch/powerpc/platforms/powernv/idle.c:

        nr_idle_states = of_property_count_u32_elems(np,
                                                "ibm,cpu-idle-state-flags");

        pnv_idle_states = kzalloc_objs(*pnv_idle_states, nr_idle_states);
        temp_u32 = kzalloc_objs(u32, nr_idle_states);
        temp_u64 = kzalloc_objs(u64, nr_idle_states);
        temp_string = kcalloc(nr_idle_states, sizeof(char *),  GFP_KERNEL);
#2 lore raw
Quoted 2 lines · Shrikanth Hegde

Looks fine to me.

Thanks a lot for taking a look!

Quoted 10 lines

Should the similar fix is needed here as well?

arch/powerpc/platforms/powernv/idle.c:

        nr_idle_states = of_property_count_u32_elems(np,
                                                "ibm,cpu-idle-state-flags");

        pnv_idle_states = kzalloc_objs(*pnv_idle_states, nr_idle_states);
        temp_u32 = kzalloc_objs(u32, nr_idle_states);
        temp_u64 = kzalloc_objs(u64, nr_idle_states);
        temp_string = kcalloc(nr_idle_states, sizeof(char *),  GFP_KERNEL);

More than likely. Maybe someone from IBM can validate and send a patch.

Signature · 2 lines
Cheers,
Nathan
reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Nathan Chancellor
Quoted 24 lines · Nathan Chancellor

After commit 0af298e62e5c ("treewide: refresh kmalloc_obj() conversions"), there is a warning on boot for several PowerPC configurations in QEMU:

  WARNING: mm/slub.c:5459 at ___kmalloc_large_node+0x174/0x1b0, CPU#0: swapper/0/1
  Modules linked in:
  CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 7.4.0-rc1-00262-g0af298e62e5c #1 PREEMPT(lazy)
  NIP [c0000000006074e4] ___kmalloc_large_node+0x174/0x1b0
  LR [c00000000060782c] __kmalloc_large_node_noprof+0x3c/0x150
  Call Trace:
  ...
  [c00000000369fb60] [c000000000614ff0] __kmalloc_noprof+0x490/0x690
  [c00000000369fbe0] [c000000000051308] _kzalloc_noprof.constprop.0+0x1c/0x74
  [c00000000369fc00] [c00000000200b450] init_thread_group_cache_map+0xc0/0x2c4
  ...

In this case, of_property_count_u32_elems() returns -EINVAL because there is no "ibm,thread-groups" property. When kcalloc() was used to allocate thread_group_array, the multiplication overflow check in _kmalloc_array_noprof() caught that a really large allocation was requested (as -EINVAL was converted to size_t) and returned NULL. kzalloc_objs() has no such check, so the large allocation check in __kmalloc_large_noprof() triggers the visible warning above.

Quoted 8 lines

Check if of_property_count_u32_elems() has returned an error and bail out if so, clearing up the warning.

Fixes 790a1662d3a2 ("powerpc/smp: Parse ibm,thread-groups with multiple properties")
---
 arch/powerpc/kernel/smp.c | 2 ++
 1 file changed, 2 insertions(+)

Thanks for fixing it. Looks good to me. Please feel free to add: