Skip to contentSkip to conversation

scsi: ses: Move a label in ses_enclosure_data_process()

PATCH In review Markus Elfring 3 participants 9 messages Dec 28, 2023 – Dec 31, 2023

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

PATCH 1/1 scsi: ses: Move a label in ses_enclosure_data_process()

From: Markus Elfring <[email protected]>
Date: Thu, 28 Dec 2023 15:38:09 +0100

The kfree() function was called in up to three cases by the ses_enclosure_data_process() function during error handling even if the passed variable contained a null pointer. This issue was detected by using the Coccinelle software.

* Thus move the label “simple_populate” behind this kfree() call.

* Delete an initialisation (for the variable “buf”) which became unnecessary with this refactoring.

---
 drivers/scsi/ses.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/scsi/ses.c +2 -2
index d7d0c35c58b8..e98f47d8206f 100644
@@ -528,7 +528,7 @@ static void ses_enclosure_data_process(struct enclosure_device *edev,
 				       int create)
 {
 	u32 result;
-	unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
+	unsigned char *buf, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
 	int i, j, page7_len, len, components;
 	struct ses_device *ses_dev = edev->scratch;
 	int types = ses_dev->page1_num_types;
@@ -552,8 +552,8 @@ static void ses_enclosure_data_process(struct enclosure_device *edev,
 		goto simple_populate;
 	result = ses_recv_diag(sdev, 7, buf, len);
 	if (result) {
- simple_populate:
 		kfree(buf);
+simple_populate:
 		buf = NULL;
 		desc_ptr = NULL;
 		len = 0;

--
2.43.0

#1 lore raw
Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 28 Dec 2023 15:38:09 +0100

The kfree() function was called in up to three cases by the ses_enclosure_data_process() function during error handling even if the passed variable contained a null pointer. This issue was detected by using the Coccinelle software.

Why is this an issue? The whole point of having kfree(NULL) be a nop is so we don't have to special case the free path. The reason we do that is because multiple special case paths through code leads to more complex control flows and more potential bugs. If coccinelle suddenly thinks this is a problem, it's coccinelle that needs fixing.

James

#2 lore raw
Quoted 9 lines · James Bottomley
Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 28 Dec 2023 15:38:09 +0100

The kfree() function was called in up to three cases by the ses_enclosure_data_process() function during error handling even if the passed variable contained a null pointer. This issue was detected by using the Coccinelle software.

Quoted 5 lines

Why is this an issue? The whole point of having kfree(NULL) be a nop is so we don't have to special case the free path. The reason we do that is because multiple special case paths through code leads to more complex control flows and more potential bugs. If coccinelle suddenly thinks this is a problem, it's coccinelle that needs fixing.

Coccinelle doesn't think anything. Markus for some reason thinks it's a problem and uses Coccinelle to find occurrences of it.

julia

#3 lore raw
In reply to James Bottomley
Quoted 6 lines
Quoted 4 lines

The kfree() function was called in up to three cases by the ses_enclosure_data_process() function during error handling even if the passed variable contained a null pointer. This issue was detected by using the Coccinelle software.

Why is this an issue? The whole point of having kfree(NULL) be a nop

Such “a nop” can trigger the allocation of extra data processing resources, can't it?

Quoted 1 line

is so we don't have to special case the free path.

A bit more development attention can hopefully connect the mentioned label with a more appropriate jump target directly.

Quoted 3 lines

The reason we do
that is because multiple special case paths through code leads to more complex control flows and more potential bugs.

You probably know some advices from another information source.

https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources

Quoted 2 lines

If coccinelle suddenly
thinks this is a problem, it's coccinelle that needs fixing.

This software tool can help to point source code places out for further considerations. The search patterns are evolving accordingly.

Regards,
Markus

#4 lore raw
Quoted 10 lines · Markus Elfring
Quoted 7 lines
Quoted 4 lines

The kfree() function was called in up to three cases by the ses_enclosure_data_process() function during error handling even if the passed variable contained a null pointer. This issue was detected by using the Coccinelle software.

Why is this an issue?  The whole point of having kfree(NULL) be a nop

Such “a nop” can trigger the allocation of extra data processing resources, can't it?

No.

Quoted 4 lines
Quoted 1 line

is so we don't have to special case the free path.

A bit more development attention can hopefully connect the mentioned label with a more appropriate jump target directly.

That's making the flow more complex as I pointed out in my initial email.

Quoted 8 lines
Quoted 3 lines

                                                    The reason we do that is because multiple special case paths through code leads to more complex control flows and more potential bugs.

You probably know some advices from another information source.

https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources

Yes, but it's about using staged deallocation at the end of the function instead of in the if loops. That's to *simplify* the exit chain and make the error legs less error prone because the teardown isn't repeated in if bodies. It has no bearing on what you just tried to do.

Quoted 6 lines
Quoted 3 lines

                                              If coccinelle suddenly thinks this is a problem, it's coccinelle that needs fixing.

This software tool can help to point source code places out for further considerations. The search patterns are evolving accordingly.

The pattern is wrong because kfree(NULL) exists as a teardown simplification.

James

#5 lore raw
Quoted 10 lines
Quoted 4 lines

You probably know some advices from another information source.

https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources

Yes, but it's about using staged deallocation at the end of the function instead of in the if loops. That's to *simplify* the exit chain and make the error legs less error prone because the teardown isn't repeated in if bodies. It has no bearing on what you just tried to do.

I got the impression that there is a general conflict involved according to different programming styles.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

                                              If coccinelle suddenly thinks this is a problem, it's coccinelle that needs fixing.

This software tool can help to point source code places out for further considerations. The search patterns are evolving accordingly.

The pattern is wrong because kfree(NULL) exists as a teardown simplification.

It might be convenient to view in this way.

If you would dare to follow advice from goto chains in a strict way, I imagine that you can tend to stress the attention for more useful data processing a bit more than such a redundant function call.

Regards,
Markus

#6 lore raw
Quoted 5 lines · Markus Elfring
Quoted 5 lines
Quoted 4 lines

You probably know some advices from another information source.

https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources

Quoted 8 lines
Quoted 5 lines

Yes, but it's about using staged deallocation at the end of the function instead of in the if loops.  That's to *simplify* the exit chain and make the error legs less error prone because the teardown isn't repeated in if bodies.  It has no bearing on what you just tried to do.

I got the impression that there is a general conflict involved according to different programming styles.

There isn't; you simply seem to have misunderstood what the above resource is actually saying.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

                                              If coccinelle suddenly thinks this is a problem, it's coccinelle that needs fixing.

This software tool can help to point source code places out for further considerations. The search patterns are evolving accordingly.

Quoted 8 lines
Quoted 2 lines

The pattern is wrong because kfree(NULL) exists as a teardown simplification.

It might be convenient to view in this way.

If you would dare to follow advice from goto chains in a strict way, I imagine that you can tend to stress the attention for more useful data processing a bit more than such a redundant function call.

It's about maintainability and simplicity. Eliminating kfree(NULL) doesn't simplify most code, it just makes the exit paths more complex as I've said for the third time now. It could possibly be done in extremely performance critical situations for good and well documented reason, but that's only a tiny fraction of the kernel and it certainly doesn't apply here.

James

#7 lore raw
Quoted 6 lines
Quoted 3 lines

If you would dare to follow advice from goto chains in a strict way, I imagine that you can tend to stress the attention for more useful data processing a bit more than such a redundant function call.

It's about maintainability and simplicity. Eliminating kfree(NULL) doesn't simplify most code,

I find it easy to avoid such a call in the affected and concrete function implementation.

Quoted 1 line

it just makes the exit paths more complex

Where is undesirable software complexity here in the repositioning of the label “simple_populate” before the statement “buf = NULL;”?

Regards,
Markus

#8 lore raw
Quoted 10 lines · Markus Elfring
Quoted 7 lines
Quoted 4 lines

If you would dare to follow advice from goto chains in a strict way, I imagine that you can tend to stress the attention for more useful data processing a bit more than such a redundant function call.

It's about maintainability and simplicity.  Eliminating kfree(NULL) doesn't simplify most code,

I find it easy to avoid such a call in the affected and concrete function implementation.

I find it easy to fall down stairs nowadays; that doesn't make it a necessary or even desirable thing to do.

Quoted 5 lines
Quoted 2 lines

                            it just makes the exit paths more complex

Where is undesirable software complexity here in the repositioning of the label “simple_populate” before the statement “buf = NULL;”?

We don't just apply patches because we can: code churn is inimical to software maintenance and backporting, so every patch has an application cost. The value provided by any patch has to be greater than that cost. kfree(NULL) is an expected operation so there's little value in avoiding it and certainly not enough to overcome the patch application cost.

James