[PATCH] EDAC/amd64: Avoid clang -Wsometimes-uninitialized in hw_info_get()

From: Nathan Chancellor
Date: Mon Feb 13 2023 - 13:18:25 EST


Clang warns:

drivers/edac/amd64_edac.c:3936:7: error: variable 'pci_id1' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
if (!pvt->umc)
^~~~~~~~~
drivers/edac/amd64_edac.c:3943:37: note: uninitialized use occurs here
ret = reserve_mc_sibling_devs(pvt, pci_id1, pci_id2);
^~~~~~~
...
drivers/edac/amd64_edac.c:3936:7: error: variable 'pci_id2' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
if (!pvt->umc)
^~~~~~~~~
drivers/edac/amd64_edac.c:3943:46: note: uninitialized use occurs here
ret = reserve_mc_sibling_devs(pvt, pci_id1, pci_id2);
^~~~~~~

This is technically a false postive, as it is not possible for pci_id1
or pci_id2 to be used in reserve_mc_sibling_devs() when pvt->umc is not
NULL, since it returns right away. However, clang does not perform
interprodecural analysis for this warning, so it cannot tell that from
the way the code is currently written.

To silence the warning, reduce the scope of the local variables and the
call to reserve_mc_sibling_devs() to the else branch, which will not
functionally change anything.

Link: https://github.com/ClangBuiltLinux/linux/issues/1803
Signed-off-by: Nathan Chancellor <nathan@xxxxxxxxxx>
---
I left the check for 'pvt->umc' alone in reserve_mc_sibling_devs() in
case it was ever called from a different path but if I should remove, I
am happy to do so in another revision.

Since this is technically a false positive, I did not include fixes
tags. If they are so desired:

Fixes: 6229235f7c66 ("EDAC/amd64: Remove PCI Function 6")
Fixes: cf981562e627 ("EDAC/amd64: Remove PCI Function 0")
---
drivers/edac/amd64_edac.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/edac/amd64_edac.c b/drivers/edac/amd64_edac.c
index 1c4bef1cdf28..a9dd66988b1d 100644
--- a/drivers/edac/amd64_edac.c
+++ b/drivers/edac/amd64_edac.c
@@ -3928,21 +3928,21 @@ static const struct attribute_group *amd64_edac_attr_groups[] = {

static int hw_info_get(struct amd64_pvt *pvt)
{
- u16 pci_id1, pci_id2;
- int ret;
-
if (pvt->fam >= 0x17) {
pvt->umc = kcalloc(fam_type->max_mcs, sizeof(struct amd64_umc), GFP_KERNEL);
if (!pvt->umc)
return -ENOMEM;
} else {
+ u16 pci_id1, pci_id2;
+ int ret;
+
pci_id1 = fam_type->f1_id;
pci_id2 = fam_type->f2_id;
- }

- ret = reserve_mc_sibling_devs(pvt, pci_id1, pci_id2);
- if (ret)
- return ret;
+ ret = reserve_mc_sibling_devs(pvt, pci_id1, pci_id2);
+ if (ret)
+ return ret;
+ }

read_mc_regs(pvt);


---
base-commit: c4605bde334367b22bbf43cbbef0d1b7c75433dc
change-id: 20230213-amd64_edac-wsometimes-uninitialized-9bdbdf40996e

Best regards,
--
Nathan Chancellor <nathan@xxxxxxxxxx>