Skip to contentSkip to conversation

EDAC/amd64: Adjustments for two function implementations

PATCH New Markus Elfring 1 participant 8 messages Dec 25, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover EDAC/amd64: Adjustments for two function implementations

From: Markus Elfring <[email protected]>
Date: Mon, 25 Dec 2023 18:19:45 +0100

Some update suggestions were taken into account
from static source code analysis.

Markus Elfring (7):
Improve error handling in amd64_edac_init()
Delete an unnecessary variable initialisation in amd64_edac_init() Merge two if statements into one in amd64_edac_init() Adjust error handling in probe_one_instance()
Improve two size determinations in probe_one_instance() Delete an unnecessary variable initialisation in probe_one_instance() Move an assignment for the variable “F3” in probe_one_instance()

 drivers/edac/amd64_edac.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)

--
2.43.0

#1 lore raw

PATCH 1/7 EDAC/amd64: Improve error handling in amd64_edac_init()

From: Markus Elfring <[email protected]>
Date: Mon, 25 Dec 2023 16:21:34 +0100

The kfree() function was called in one case by the amd64_edac_init() function during error handling even if the passed variable contained a null pointer. This issue was detected by using the Coccinelle software.

1. Return directly after a call of the function “kcalloc” failed.

2. Move an error code assignment into an if branch.

---
 drivers/edac/amd64_edac.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/edac/amd64_edac.c +4 -3
index 537b9987a431..f912cbadefa4 100644
@@ -4414,14 +4414,15 @@ static int __init amd64_edac_init(void)
 
 	opstate_init();
 
-	err = -ENOMEM;
 	ecc_stngs = kcalloc(amd_nb_num(), sizeof(ecc_stngs[0]), GFP_KERNEL);
 	if (!ecc_stngs)
-		goto err_free;
+		return -ENOMEM;
 
 	msrs = msrs_alloc();
-	if (!msrs)
+	if (!msrs) {
+		err = -ENOMEM;
 		goto err_free;
+	}
 
 	for (i = 0; i < amd_nb_num(); i++) {
 		err = probe_one_instance(i);

--
2.43.0

#2 lore raw

PATCH 2/7 EDAC/amd64: Delete an unnecessary variable initialisation in amd64_edac_init()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 25 Dec 2023 16:33:22 +0100

The variable “err” will eventually be set to an appropriate value a bit later. Thus omit the explicit initialisation at the beginning.

---
 drivers/edac/amd64_edac.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/edac/amd64_edac.c +1 -1
index f912cbadefa4..8a27a4af7121 100644
@@ -4396,7 +4396,7 @@ MODULE_DEVICE_TABLE(x86cpu, amd64_cpuids);
 static int __init amd64_edac_init(void)
 {
 	const char *owner;
-	int err = -ENODEV;
+	int err;
 	int i;
 
 	if (ghes_get_devices())

--
2.43.0

#3 lore raw

PATCH 3/7 EDAC/amd64: Merge two if statements into one in amd64_edac_init()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 25 Dec 2023 16:45:37 +0100

Two if statements were connected with the same return statements. Thus merge their condition checks into one statement.

---
 drivers/edac/amd64_edac.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/edac/amd64_edac.c +1 -4
index 8a27a4af7121..49f3d9b54902 100644
@@ -4406,10 +4406,7 @@ static int __init amd64_edac_init(void)
 	if (owner && strncmp(owner, EDAC_MOD_STR, sizeof(EDAC_MOD_STR)))
 		return -EBUSY;
 
-	if (!x86_match_cpu(amd64_cpuids))
-		return -ENODEV;
-
-	if (!amd_nb_num())
+	if (!x86_match_cpu(amd64_cpuids) || !amd_nb_num())
 		return -ENODEV;
 
 	opstate_init();

--
2.43.0

#4 lore raw

PATCH 4/7 EDAC/amd64: Adjust error handling in probe_one_instance()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 25 Dec 2023 17:18:14 +0100

1. Return directly after a call of the function “kzalloc” failed at the beginning.

2. Delete the label “err_out” which became unnecessary with this refactoring.

3. Move an error code assignment into an if branch.

---
 drivers/edac/amd64_edac.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/edac/amd64_edac.c +4 -5
index 49f3d9b54902..64680de47cab 100644
@@ -4268,16 +4268,17 @@ static int probe_one_instance(unsigned int nid)
 	struct ecc_settings *s;
 	int ret;
 
-	ret = -ENOMEM;
 	s = kzalloc(sizeof(struct ecc_settings), GFP_KERNEL);
 	if (!s)
-		goto err_out;
+		return -ENOMEM;
 
 	ecc_stngs[nid] = s;
 
 	pvt = kzalloc(sizeof(struct amd64_pvt), GFP_KERNEL);
-	if (!pvt)
+	if (!pvt) {
+		ret = -ENOMEM;
 		goto err_settings;
+	}
 
 	pvt->mc_node_id	= nid;
 	pvt->F3 = F3;
@@ -4336,8 +4337,6 @@ static int probe_one_instance(unsigned int nid)
 err_settings:
 	kfree(s);
 	ecc_stngs[nid] = NULL;
-
-err_out:
 	return ret;
 }
 

--
2.43.0

#5 lore raw

PATCH 5/7 EDAC/amd64: Improve two size determinations in probe_one_instance()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 25 Dec 2023 17:24:37 +0100

Replace the specification of data structures by pointer dereferences as the parameter for the operator “sizeof” to make the corresponding size determination a bit safer according to the Linux coding style convention.

---
 drivers/edac/amd64_edac.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/edac/amd64_edac.c +2 -2
index 64680de47cab..7536236df542 100644
@@ -4268,13 +4268,13 @@ static int probe_one_instance(unsigned int nid)
 	struct ecc_settings *s;
 	int ret;
 
-	s = kzalloc(sizeof(struct ecc_settings), GFP_KERNEL);
+	s = kzalloc(sizeof(*s), GFP_KERNEL);
 	if (!s)
 		return -ENOMEM;
 
 	ecc_stngs[nid] = s;
 
-	pvt = kzalloc(sizeof(struct amd64_pvt), GFP_KERNEL);
+	pvt = kzalloc(sizeof(*pvt), GFP_KERNEL);
 	if (!pvt) {
 		ret = -ENOMEM;
 		goto err_settings;

--
2.43.0

#6 lore raw

PATCH 6/7 EDAC/amd64: Delete an unnecessary variable initialisation in probe_one_instance()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 25 Dec 2023 17:34:35 +0100

The variable “pvt” will eventually be set to an appropriate pointer a bit later. Thus omit the explicit initialisation at the beginning.

---
 drivers/edac/amd64_edac.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/edac/amd64_edac.c +1 -1
index 7536236df542..698cde573847 100644
@@ -4264,7 +4264,7 @@ static bool instance_has_memory(struct amd64_pvt *pvt)
 static int probe_one_instance(unsigned int nid)
 {
 	struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
-	struct amd64_pvt *pvt = NULL;
+	struct amd64_pvt *pvt;
 	struct ecc_settings *s;
 	int ret;
 

--
2.43.0

#7 lore raw

PATCH 7/7 EDAC/amd64: Move an assignment for the variable “F3” in probe_one_instance()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 25 Dec 2023 17:54:33 +0100

Move one assignment for a local variable so that its setting will only be performed immediately before this pointer is used.

---
 drivers/edac/amd64_edac.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/edac/amd64_edac.c +2 -1
index 698cde573847..91e0abdf762f 100644
@@ -4263,7 +4263,7 @@ static bool instance_has_memory(struct amd64_pvt *pvt)
 
 static int probe_one_instance(unsigned int nid)
 {
-	struct pci_dev *F3 = node_to_amd_nb(nid)->misc;
+	struct pci_dev *F3;
 	struct amd64_pvt *pvt;
 	struct ecc_settings *s;
 	int ret;
@@ -4281,6 +4281,7 @@ static int probe_one_instance(unsigned int nid)
 	}
 
 	pvt->mc_node_id	= nid;
+	F3 = node_to_amd_nb(nid)->misc;
 	pvt->F3 = F3;
 
 	ret = per_family_init(pvt);

--
2.43.0