percpu: initialize best_upa variable
Subsystems (2)
PATCH 1/1 percpu: initialize best_upa variable
From: Tom Rix <[email protected]>
Static analysis reports this problem
percpu.c:2945:6: warning: Assigned value is garbage or undefined
upa = best_upa;
^ ~~~~~~~~best_upa may not be set, so initialize it.
--- mm/percpu.c | 1 + 1 file changed, 1 insertion(+)
mm/percpu.c +1 -0
@@ -2916,6 +2916,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 = max_upa;for (upa = max_upa; upa; upa--) {int allocs =0 , wasted =0 ;
Signature · 1 line
2.26.3
Quoted 5 lines · On Sun, May 16, 2021 at 3:08 AM <[email protected]> wrote:
Static analysis reports this problem
percpu.c:2945:6: warning: Assigned value is garbage or undefinedupa = best_upa; ^ ~~~~~~~~
best_upamay not be set, so initialize it.
Hi,
Actually, best_upa is always set in the for loop below. when upa is 1, It will always satisfy all conditions.
mm/percpu.c static struct pcpu_alloc_info * __init __flatten pcpu_build_alloc_info( · line 2921
Quoted 11 lines
mm/percpu.c +1
-0@@ -2916,6 +2916,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 = max_upa;for (upa = max_upa; upa; upa--) {int allocs =0 , wasted =0 ;
It doesn't seem to be a problem. But, how about this?
best_upa = 1;
for (upa = max_upa; upa>1; upa--)
Dennis Zhou
maintainerHello,
Quoted 28 lines · On Sat, May 15, 2021 at 11:08:17AM -0700, [email protected] wrote:
From: Tom Rix <[email protected]>
Static analysis reports this problem
percpu.c:2945:6: warning: Assigned value is garbage or undefinedupa = best_upa; ^ ~~~~~~~~
best_upamay not be set, so initialize it.Signed-off-by Tom Rix <[email protected]>--- mm/percpu.c | 1 + 1 file changed, 1 insertion(+)mm/percpu.c +1
-0@@ -2916,6 +2916,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 = max_upa;for (upa = max_upa; upa; upa--) {int allocs =0 , wasted =0 ;Signature · 1 line
2.26.3
I think the proper fix would be:
best_upa = 0;
for (...) { }BUG_ON(!best_upa);
upa = best_upa;
If you're fine with this I'll make the changes and apply it to for-5.13-fixes.
Can you also tell me what static analysis tool produced this? I'm just a little curious because this code hasn't changed in several years so I'd have expected some static analyzer to have caught this by now.
Thanks,
Dennis
Quoted 5 lines · Dennis Zhou
Can you also tell me what static analysis tool produced this? I'm just a little curious because this code hasn't changed in several years so I'd have expected some static analyzer to have caught this by now.
It's because uninitialize_var() was gone. It was introduced commit :
3f649ab728cda8("treewide: Remove uninitialized_var() usage")
Quoted 28 lines · Dennis Zhou
Hello,
Quoted 25 lines · On Sat, May 15, 2021 at 11:08:17AM -0700, [email protected] wrote:
From: Tom Rix <[email protected]>
Static analysis reports this problem
percpu.c:2945:6: warning: Assigned value is garbage or undefinedupa = best_upa; ^ ~~~~~~~~
best_upamay not be set, so initialize it.Signed-off-by Tom Rix <[email protected]>--- mm/percpu.c | 1 + 1 file changed, 1 insertion(+)mm/percpu.c +1
-0@@ -2916,6 +2916,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 = max_upa;for (upa = max_upa; upa; upa--) {int allocs =0 , wasted =0 ;
Quoted 6 lines
Quoted 3 lines
Signature · 1 line
2.26.3I think the proper fix would be:
best_upa= 0;
I was looking for initializing with something that would work.
Quoted 2 lines
for (...) { }
BUG_ON(!best_upa);
WARN_ON instead?
Quoted 8 lines
upa =
best_upa;If you're fine with this I'll make the changes and apply it to for-5.13-fixes.
Can you also tell me what static analysis tool produced this? I'm just a little curious because this code hasn't changed in several years so I'd have expected some static analyzer to have caught this by now.
Clang 10
Tom
Quoted 4 lines
Thanks,
Dennis
Dennis Zhou
maintainerQuoted 34 lines · Tom Rix
Quoted 32 lines · Dennis Zhou
Hello,
Quoted 27 lines · On Sat, May 15, 2021 at 11:08:17AM -0700, [email protected] wrote:
From: Tom Rix <[email protected]>
Static analysis reports this problem
percpu.c:2945:6: warning: Assigned value is garbage or undefinedupa = best_upa; ^ ~~~~~~~~
best_upamay not be set, so initialize it.Signed-off-by Tom Rix <[email protected]>--- mm/percpu.c | 1 + 1 file changed, 1 insertion(+)mm/percpu.c +1
-0@@ -2916,6 +2916,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 = max_upa;for (upa = max_upa; upa; upa--) {int allocs =0 , wasted =0 ;Signature · 1 line
2.26.3I think the proper fix would be:
Quoted 4 lines
Quoted 1 line
best_upa= 0;I was looking for initializing with something that would work.
I think I prefer setting it to 0 because it forces the loop to have succeeded vs being able to bypass it if the for loop logic was changed.
Quoted 3 lines
Quoted 2 lines
for (...) { }
BUG_ON(!best_upa);
WARN_ONinstead?
This is initialization code. So if upa == 0, it really is a problem. Having 0 units per allocation is bogus.
Quoted 5 lines
Quoted 5 lines
upa =
best_upa;If you're fine with this I'll make the changes and apply it to for-5.13-fixes.
Quoted 8 lines
Quoted 3 lines
Can you also tell me what static analysis tool produced this? I'm just a little curious because this code hasn't changed in several years so I'd have expected some static analyzer to have caught this by now.
Clang 10
Tom
Thanks,
Dennis
Dennis Zhou
maintainerHello,
Quoted 59 lines · Dennis Zhou
Quoted 34 lines · Tom Rix
Quoted 32 lines · Dennis Zhou
Hello,
Quoted 27 lines · On Sat, May 15, 2021 at 11:08:17AM -0700, [email protected] wrote:
From: Tom Rix <[email protected]>
Static analysis reports this problem
percpu.c:2945:6: warning: Assigned value is garbage or undefinedupa = best_upa; ^ ~~~~~~~~
best_upamay not be set, so initialize it.Signed-off-by Tom Rix <[email protected]>--- mm/percpu.c | 1 + 1 file changed, 1 insertion(+)mm/percpu.c +1
-0@@ -2916,6 +2916,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 = max_upa;for (upa = max_upa; upa; upa--) {int allocs =0 , wasted =0 ;Signature · 1 line
2.26.3I think the proper fix would be:
Quoted 4 lines
Quoted 1 line
best_upa= 0;I was looking for initializing with something that would work.
I think I prefer setting it to 0 because it forces the loop to have succeeded vs being able to bypass it if the for loop logic was changed.
Quoted 3 lines
Quoted 2 lines
for (...) { }
BUG_ON(!best_upa);
WARN_ONinstead?This is initialization code. So if upa == 0, it really is a problem. Having 0 units per allocation is bogus.
Quoted 9 lines
Quoted 8 lines
upa =
best_upa;If you're fine with this I'll make the changes and apply it to for-5.13-fixes.
Can you also tell me what static analysis tool produced this? I'm just a little curious because this code hasn't changed in several years so I'd have expected some static analyzer to have caught this by now.
Quoted 7 lines
Quoted 4 lines
Clang 10
Tom
Thanks,
Dennis
Following up here. Are you find with me making the changes and attributing it to you? Otherwise I can just spin another patch real quick.
At this point I've already sent my PR for-5.13-fixes. So I'll queue some fix for-5.14.
Thanks,
Dennis
Quoted 59 lines · Dennis Zhou
Hello,
Quoted 56 lines · Dennis Zhou
Quoted 33 lines · Tom Rix
Quoted 32 lines · Dennis Zhou
Hello,
Quoted 27 lines · On Sat, May 15, 2021 at 11:08:17AM -0700, [email protected] wrote:
From: Tom Rix <[email protected]>
Static analysis reports this problem
percpu.c:2945:6: warning: Assigned value is garbage or undefinedupa = best_upa; ^ ~~~~~~~~
best_upamay not be set, so initialize it.Signed-off-by Tom Rix <[email protected]>--- mm/percpu.c | 1 + 1 file changed, 1 insertion(+)mm/percpu.c +1
-0@@ -2916,6 +2916,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 = max_upa;for (upa = max_upa; upa; upa--) {int allocs =0 , wasted =0 ;Signature · 1 line
2.26.3I think the proper fix would be:
Quoted 3 lines
Quoted 1 line
best_upa= 0;I was looking for initializing with something that would work.
I think I prefer setting it to 0 because it forces the loop to have succeeded vs being able to bypass it if the for loop logic was changed.
Quoted 3 lines
Quoted 2 lines
for (...) { }
BUG_ON(!best_upa);
WARN_ONinstead?This is initialization code. So if upa == 0, it really is a problem. Having 0 units per allocation is bogus.
Quoted 10 lines
Quoted 8 lines
upa =
best_upa;If you're fine with this I'll make the changes and apply it to for-5.13-fixes.
Can you also tell me what static analysis tool produced this? I'm just a little curious because this code hasn't changed in several years so I'd have expected some static analyzer to have caught this by now.
Clang 10
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Tom
Thanks,
DennisFollowing up here. Are you find with me making the changes and attributing it to you? Otherwise I can just spin another patch real quick.
I am fine with you respinning, no need to attribute the change to me.
If you would like a review, include me on the cc.
Thanks!
Tom
Quoted 6 lines
At this point I've already sent my PR for-5.13-fixes. So I'll queue some fix for-5.14.
Thanks,
Dennis