Skip to contentSkip to conversation

pstore/ram_core: Improve exception handling in persistent_ram_new()

PATCH In review Markus Elfring 2 participants 2 messages Jan 18, 2024 – Feb 1, 2024

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

PATCH 1/1 pstore/ram_core: Improve exception handling in persistent_ram_new()

From: Markus Elfring <[email protected]>
Date: Thu, 18 Jan 2024 14:57:21 +0100

* Omit an initialisation (for the variable “ret”) which became unnecessary with this refactoring because a memory allocation failure will be directly indicated by a corresponding return statement in an if branch.

* Move a call of the function “kstrdup” before two other statements.

---
 fs/pstore/ram_core.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
fs/pstore/ram_core.c +8 -6
index f1848cdd6d34..5047a8502e17 100644
@@ -586,21 +586,23 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
 			unsigned int memtype, u32 flags, char *label)
 {
 	struct persistent_ram_zone *prz;
-	int ret = -ENOMEM;
+	int ret;
 
 	prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
 	if (!prz) {
 		pr_err("failed to allocate persistent ram zone\n");
-		goto err;
+		return ERR_PTR(-ENOMEM);
+	}
+
+	prz->label = kstrdup(label, GFP_KERNEL);
+	if (!prz->label) {
+		kfree(prz);
+		return ERR_PTR(-ENOMEM);
 	}
 
 	/* Initialize general buffer state. */
 	raw_spin_lock_init(&prz->buffer_lock);
 	prz->flags = flags;
-	prz->label = kstrdup(label, GFP_KERNEL);
-	if (!prz->label)
-		goto err;
-
 	ret = persistent_ram_buffer_map(start, size, prz, memtype);
 	if (ret)
 		goto err;

--
2.43.0

#1 lore raw
Quoted 11 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 18 Jan 2024 14:57:21 +0100

* Omit an initialisation (for the variable “ret”) which became unnecessary with this refactoring because a memory allocation failure will be directly indicated by a corresponding return statement in an if branch.

* Move a call of the function “kstrdup” before two other statements.

Thanks for you patch!

I've decided not to apply it because I prefer having a single exit path for error handling, and it works as-is already.

-Kees

Quoted 41 lines
---
 fs/pstore/ram_core.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
fs/pstore/ram_core.c +8 -7
index f1848cdd6d34..5047a8502e17 100644
@@ -586,21 +586,23 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
 			unsigned int memtype, u32 flags, char *label)
 {
 	struct persistent_ram_zone *prz;
-	int ret = -ENOMEM;
+	int ret;
 
 	prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
 	if (!prz) {
 		pr_err("failed to allocate persistent ram zone\n");
-		goto err;
+		return ERR_PTR(-ENOMEM);
+	}
+
+	prz->label = kstrdup(label, GFP_KERNEL);
+	if (!prz->label) {
+		kfree(prz);
+		return ERR_PTR(-ENOMEM);
 	}
 
 	/* Initialize general buffer state. */
 	raw_spin_lock_init(&prz->buffer_lock);
 	prz->flags = flags;
-	prz->label = kstrdup(label, GFP_KERNEL);
-	if (!prz->label)
-		goto err;
-
 	ret = persistent_ram_buffer_map(start, size, prz, memtype);
 	if (ret)
 		goto err;
--

2.43.0

Signature · 1 line
Kees Cook