mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] sched/topology: replace kzalloc() with kcalloc() in sched_init_numa()
@ 2025-02-22 19:46 Ethan Carter Edwards
  2025-02-23  7:19 ` Madadi Vineeth Reddy
  2025-02-23 10:30 ` [PATCH v3?] " Markus Elfring
  0 siblings, 2 replies; 3+ messages in thread
From: Ethan Carter Edwards @ 2025-02-22 19:46 UTC (permalink / raw)
  To: Markus Elfring
  Cc: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, linux-kernel, linux-hardening,
	Ethan Carter Edwards

We are trying to get rid of all multiplications from allocation
functions to prevent integer overflows[1]. Here the multiplication is
obviously safe, but using kcalloc() is more appropriate and improves
readability. This patch has no effect on runtime behavior.

Link: https://github.com/KSPP/linux/issues/162 [1]
Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

Signed-off-by: Ethan Carter Edwards <ethan@ethancedwards.com>
---
 kernel/sched/topology.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index c49aea8c10254dec985bba47b18f61be954d23f6..b4539b29fb36f6b2f0c5ca310620ebda29755e5c 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -1918,7 +1918,7 @@ void sched_init_numa(int offline_node)
 	 */
 	sched_domains_numa_levels = 0;
 
-	masks = kzalloc(sizeof(void *) * nr_levels, GFP_KERNEL);
+	masks = kcalloc(nr_levels, sizeof(void *), GFP_KERNEL);
 	if (!masks)
 		return;
 
@@ -1927,7 +1927,7 @@ void sched_init_numa(int offline_node)
 	 * CPUs of nodes that are that many hops away from us.
 	 */
 	for (i = 0; i < nr_levels; i++) {
-		masks[i] = kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
+		masks[i] = kcalloc(nr_node_ids, sizeof(void *), GFP_KERNEL);
 		if (!masks[i])
 			return;
 

---
base-commit: 5cf80612d3f72c46ad53ef5042b4c609c393122f
change-id: 20250222-sched-kcalloc-1d5acea7249b

Best regards,
-- 
Ethan Carter Edwards <ethan@ethancedwards.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] sched/topology: replace kzalloc() with kcalloc() in sched_init_numa()
  2025-02-22 19:46 [PATCH] sched/topology: replace kzalloc() with kcalloc() in sched_init_numa() Ethan Carter Edwards
@ 2025-02-23  7:19 ` Madadi Vineeth Reddy
  2025-02-23 10:30 ` [PATCH v3?] " Markus Elfring
  1 sibling, 0 replies; 3+ messages in thread
From: Madadi Vineeth Reddy @ 2025-02-23  7:19 UTC (permalink / raw)
  To: Ethan Carter Edwards
  Cc: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, linux-kernel, linux-hardening,
	Markus Elfring, Madadi Vineeth Reddy

Hi Ethan,

On 23/02/25 01:16, Ethan Carter Edwards wrote:
> We are trying to get rid of all multiplications from allocation
> functions to prevent integer overflows[1]. Here the multiplication is
> obviously safe, but using kcalloc() is more appropriate and improves
> readability. This patch has no effect on runtime behavior.
> 
> Link: https://github.com/KSPP/linux/issues/162 [1]
> Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments
> 
> Signed-off-by: Ethan Carter Edwards <ethan@ethancedwards.com>
> ---
>  kernel/sched/topology.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> index c49aea8c10254dec985bba47b18f61be954d23f6..b4539b29fb36f6b2f0c5ca310620ebda29755e5c 100644
> --- a/kernel/sched/topology.c
> +++ b/kernel/sched/topology.c
> @@ -1918,7 +1918,7 @@ void sched_init_numa(int offline_node)
>  	 */
>  	sched_domains_numa_levels = 0;
>  
> -	masks = kzalloc(sizeof(void *) * nr_levels, GFP_KERNEL);
> +	masks = kcalloc(nr_levels, sizeof(void *), GFP_KERNEL);

Even though an overflow is very unlikely here, using kcalloc() improves
readability and adds a safeguard since its built-in overflow check has
minimal overhead.

Reviewed-by: Madadi Vineeth Reddy <vineethr@linux.ibm.com>

Thanks,
Madadi Vineeth Reddy

>  	if (!masks)
>  		return;
>  
> @@ -1927,7 +1927,7 @@ void sched_init_numa(int offline_node)
>  	 * CPUs of nodes that are that many hops away from us.
>  	 */
>  	for (i = 0; i < nr_levels; i++) {
> -		masks[i] = kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
> +		masks[i] = kcalloc(nr_node_ids, sizeof(void *), GFP_KERNEL);
>  		if (!masks[i])
>  			return;
>  
> 
> ---
> base-commit: 5cf80612d3f72c46ad53ef5042b4c609c393122f
> change-id: 20250222-sched-kcalloc-1d5acea7249b
> 
> Best regards,


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3?] sched/topology: replace kzalloc() with kcalloc() in sched_init_numa()
  2025-02-22 19:46 [PATCH] sched/topology: replace kzalloc() with kcalloc() in sched_init_numa() Ethan Carter Edwards
  2025-02-23  7:19 ` Madadi Vineeth Reddy
@ 2025-02-23 10:30 ` Markus Elfring
  1 sibling, 0 replies; 3+ messages in thread
From: Markus Elfring @ 2025-02-23 10:30 UTC (permalink / raw)
  To: Ethan Carter Edwards, linux-hardening
  Cc: LKML, kernel-janitors, Ben Segall, Dietmar Eggemann, Ingo Molnar,
	Juri Lelli, Madadi Vineeth Reddy, Mel Gorman, Peter Zijlstra,
	Steven Rostedt, Valentin Schneider, Vincent Guittot

…
> We are trying to get rid of all multiplications from allocation
> functions to prevent integer overflows[1]. …

Is an imperative wording more desirable for such a change description?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14-rc3#n94


…
> ---
>  kernel/sched/topology.c | 4 ++--
…

How do you think about to improve your version management?
https://lore.kernel.org/all/?q=%22This+looks+like+a+new+version+of+a+previously+submitted+patch%22
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14-rc3#n780


…
> +++ b/kernel/sched/topology.c
> @@ -1918,7 +1918,7 @@ void sched_init_numa(int offline_node)
>  	 */
>  	sched_domains_numa_levels = 0;
>
> -	masks = kzalloc(sizeof(void *) * nr_levels, GFP_KERNEL);
> +	masks = kcalloc(nr_levels, sizeof(void *), GFP_KERNEL);
…

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.14-rc3#n941

Regards,
Markus

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-02-23 10:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-22 19:46 [PATCH] sched/topology: replace kzalloc() with kcalloc() in sched_init_numa() Ethan Carter Edwards
2025-02-23  7:19 ` Madadi Vineeth Reddy
2025-02-23 10:30 ` [PATCH v3?] " Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®