Skip to contentSkip to conversation

percpu: initialize best_upa variable

PATCH New Dennis Zhou 1 participant 1 message Jun 14, 2021

Subsystems (2)
lore
Share & tools
lore

Dennis Zhou

author maintainer
#0 lore raw

PATCH 1/1 percpu: initialize best_upa variable

Tom reported this finding from clang 10's static analysis [1].

Due to the way the code is written, it will always see a successful loop iteration. Instead of setting an initial value, check that it was set instead with BUG_ON() because 0 units per allocation is bogus.

[1] https://lore.kernel.org/lkml/[email protected]/

---
Sorry this hasn't been my highest priority. I'll apply this shortly to for-5.14.

 mm/percpu.c | 2 ++
 1 file changed, 2 insertions(+)
mm/percpu.c +2 -0
index e7b9ca82e9aa..6667dfa0fcec 100644
@@ -2888,6 +2888,7 @@ static struct pcpu_alloc_info * __init __flatten pcpu_build_alloc_info(
 	 * Related to atom_size, which could be much larger than the unit_size.
 	 */
 	last_allocs = INT_MAX;
+	best_upa = 0;
 	for (upa = max_upa; upa; upa--) {
 		int allocs = 0, wasted = 0;
 
@@ -2914,6 +2915,7 @@ static struct pcpu_alloc_info * __init __flatten pcpu_build_alloc_info(
 		last_allocs = allocs;
 		best_upa = upa;
 	}
+	BUG_ON(!best_upa);
 	upa = best_upa;
 
 	/* allocate and fill alloc_info */
Signature · 1 line
2.32.0.272.g935e593368-goog