Skip to contentSkip to conversation

irqdomain: Refactor error path in __irq_domain_alloc_fwnode()

PATCH In review Andy Shevchenko 2 participants 5 messages Aug 4, 2023 – Aug 7, 2023

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

PATCH 1/1 irqdomain: Refactor error path in __irq_domain_alloc_fwnode()

First of all, there is no need to call kasprintf() if the previous allocation failed. Second, there is no need to call for kfree() when we know that its parameter is NULL. Refactor the code accordingly.

---
 kernel/irq/irqdomain.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/irq/irqdomain.c +3 -3
index 0bdef4fe925b..d2bbba46c808 100644
@@ -81,6 +81,8 @@ struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
 	char *n;
 
 	fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
+	if (!fwid)
+		return NULL;
 
 	switch (type) {
 	case IRQCHIP_FWNODE_NAMED:
@@ -93,10 +95,8 @@ struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
 		n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
 		break;
 	}
-
-	if (!fwid || !n) {
+	if (!n) {
 		kfree(fwid);
-		kfree(n);
 		return NULL;
 	}
 
Signature · 1 line
2.40.0.1.gaa8946217a0b
#1 lore raw
Quoted 25 lines · Andy Shevchenko

First of all, there is no need to call kasprintf() if the previous allocation failed. Second, there is no need to call for kfree() when we know that its parameter is NULL. Refactor the code accordingly.

---
 kernel/irq/irqdomain.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/irq/irqdomain.c +2 -0
index 0bdef4fe925b..d2bbba46c808 100644
@@ -81,6 +81,8 @@ struct fwnode_handle
 *__irq_domain_alloc_fwnode(unsigned int type, int id,
 	char *n;
 
 	fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
+	if (!fwid)
+		return NULL;
 
 	switch (type) {
kernel/irq/irqdomain.c +0 -0
 	case IRQCHIP_FWNODE_NAMED:
@@ -93,10 +95,8 @@ struct fwnode_handle
 *__irq_domain_alloc_fwnode(unsigned int type, int id,

kernel/irq/irqdomain.c struct fwnode_handle · line 102

Quoted 10 lines
kernel/irq/irqdomain.c +1 -3
 		n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
 		break;
 	}
-
-	if (!fwid || !n) {
+	if (!n) {
 		kfree(fwid);
-		kfree(n);
 		return NULL;
 	}

What are you trying to fix?

We have a common error handling path, which makes it easy to track the memory management. I don't think this sort of bike shedding adds much to the maintainability of this code.

Now if you have spotted an actual bug, I'm all ears.

  M.

Signature · 1 line
Jazz is not dead. It just smells funny...
#2 lore raw
Quoted 4 lines · Marc Zyngier
Quoted 3 lines · Andy Shevchenko

First of all, there is no need to call kasprintf() if the previous allocation failed. Second, there is no need to call for kfree() when we know that its parameter is NULL. Refactor the code accordingly.

...

Quoted 4 lines
Quoted 4 lines
kernel/irq/irqdomain.c +0 -1
 		n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
 		break;
 	}
-
Quoted 8 lines

kernel/irq/irqdomain.c struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id, · line 101

Quoted 6 lines
kernel/irq/irqdomain.c +1 -2
-	if (!fwid || !n) {
+	if (!n) {
 		kfree(fwid);
-		kfree(n);
 		return NULL;
 	}

What are you trying to fix?

I'm not trying to fix anything (there is no such statement from me), but I would think of some micro-optimization (speedup boot for unnoticeable time? Dunno.).

Quoted 3 lines

We have a common error handling path, which makes it easy to track the memory management. I don't think this sort of bike shedding adds much to the maintainability of this code.

Your call, of course, but I not often see in the kernel two or three attempts to allocate some memory and have grouped check for the failure.

Quoted 1 line

Now if you have spotted an actual bug, I'm all ears.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 22 lines · Andy Shevchenko
Quoted 4 lines · Marc Zyngier
Quoted 3 lines · Andy Shevchenko

First of all, there is no need to call kasprintf() if the previous allocation failed. Second, there is no need to call for kfree() when we know that its parameter is NULL. Refactor the code accordingly.

...

Quoted 4 lines
Quoted 4 lines
kernel/irq/irqdomain.c +0 -1
 		n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
 		break;
 	}
-
Quoted 8 lines

kernel/irq/irqdomain.c struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id, · line 101

Quoted 6 lines
kernel/irq/irqdomain.c +1 -2
-	if (!fwid || !n) {
+	if (!n) {
 		kfree(fwid);
-		kfree(n);
 		return NULL;
 	}

What are you trying to fix?

Quoted 3 lines

I'm not trying to fix anything (there is no such statement from me), but I would think of some micro-optimization (speedup boot for unnoticeable time? Dunno.).

Error handling paths rarely qualify as an optimisation.

Quoted 7 lines
Quoted 4 lines

We have a common error handling path, which makes it easy to track the memory management. I don't think this sort of bike shedding adds much to the maintainability of this code.

Your call, of course, but I not often see in the kernel two or three attempts to allocate some memory and have grouped check for the failure.

Things like this[1]? Well, this is a pattern I use often enough. Maybe it isn't everybody's taste, but it suits me.

	M.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/irqchip/irq-gic-v3-its.c#n3438

Signature · 1 line
Without deviation from the norm, progress is not possible.
#4 lore raw
Quoted 4 lines · Marc Zyngier
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Marc Zyngier

On 2023-08-04 17:49, Andy Shevchenko wrote:

...

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

kernel/irq/irqdomain.c struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id, · line 101

Quoted 10 lines
kernel/irq/irqdomain.c +1 -3
 		n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
 		break;
 	}
-
-	if (!fwid || !n) {
+	if (!n) {
 		kfree(fwid);
-		kfree(n);
 		return NULL;
 	}

What are you trying to fix?

Quoted 5 lines
Quoted 3 lines

I'm not trying to fix anything (there is no such statement from me), but I would think of some micro-optimization (speedup boot for unnoticeable time? Dunno.).

Error handling paths rarely qualify as an optimisation.

OK.

...

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

We have a common error handling path, which makes it easy to track the memory management. I don't think this sort of bike shedding adds much to the maintainability of this code.

Your call, of course, but I not often see in the kernel two or three attempts to allocate some memory and have grouped check for the failure.

Things like this[1]?

Yes.

Quoted 2 lines

Well, this is a pattern I use often enough. Maybe
it isn't everybody's taste, but it suits me.

Understand. Thanks for review!

Quoted 1 line

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/irqchip/irq-gic-v3-its.c#n3438

Signature · 2 lines
With Best Regards,
Andy Shevchenko