[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20250821140833.24791-1-rongqianfeng@vivo.com>
Date: Thu, 21 Aug 2025 22:08:32 +0800
From: Qianfeng Rong <rongqianfeng@...o.com>
To: Ingo Molnar <mingo@...hat.com>,
Peter Zijlstra <peterz@...radead.org>,
Juri Lelli <juri.lelli@...hat.com>,
Vincent Guittot <vincent.guittot@...aro.org>,
Dietmar Eggemann <dietmar.eggemann@....com>,
Steven Rostedt <rostedt@...dmis.org>,
Ben Segall <bsegall@...gle.com>,
Mel Gorman <mgorman@...e.de>,
Valentin Schneider <vschneid@...hat.com>,
linux-kernel@...r.kernel.org
Cc: Qianfeng Rong <rongqianfeng@...o.com>
Subject: [PATCH] sched/topology: Use kcalloc() in sched_init_numa()
Replace kzalloc() with kcalloc() in sched_init_numa(). As noted in
the kernel documentation [1], open-coded multiplication in allocator
arguments is discouraged because it can lead to integer overflow.
Use kcalloc() to gain built-in overflow protection, making memory
allocation safer when calculating allocation size compared to explicit
multiplication. Similarly, use size_add() instead of explicit addition
for 'i + nr_levels + 1'.
Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments #1
Signed-off-by: Qianfeng Rong <rongqianfeng@...o.com>
---
kernel/sched/topology.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index 977e133bb8a4..0500146f9c1f 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -1956,7 +1956,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;
@@ -1965,7 +1965,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;
@@ -1994,8 +1994,8 @@ void sched_init_numa(int offline_node)
/* Compute default topology size */
for (i = 0; sched_domain_topology[i].mask; i++);
- tl = kzalloc((i + nr_levels + 1) *
- sizeof(struct sched_domain_topology_level), GFP_KERNEL);
+ tl = kcalloc(size_add(size_add(i, nr_levels), 1),
+ sizeof(struct sched_domain_topology_level), GFP_KERNEL);
if (!tl)
return;
--
2.34.1
Powered by blists - more mailing lists