cleanup: clarify scope-based resource management and goto guidance
PATCH 1/1 cleanup: clarify scope-based resource management and goto guidance
Scope-based resource management helps manage resource lifetimes, but it does not solve rollback of a failed operation. Such rollback is often best handled with multiple goto-based unwind paths.
The current guidance can be read as discouraging any cleanup helper alongside goto, including guards. Automated reviewers repeatedly flag code that mixes the two, so clarify that the recommendation applies only to __free().
---
I develop mostly in the subsystem that bans __free(), which is also most low-level of all the helpers - so I get that we could discourage mixing __free() and goto. But for the rest it is counter productive.
--- include/linux/cleanup.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
include/linux/cleanup.h +4 -6
@@ -145,12 +145,10 @@* statement and not group variable definitions at the top of the * function when __free() is used. * -* Lastly, given that the benefit of cleanup helpers is removal of -* "goto", and that the "goto" statement can jump between scopes, the -* expectation is that usage of "goto" and cleanup helpers is never -* mixed in the same function. I.e. for a given routine, convert all -* resources that need a "goto" cleanup to scope-based cleanup, or -* convert none of them. +* Lastly, avoid mixing __free() with goto-based resource cleanup in +* the same function. This recommendation applies specifically to +* resources managed with __free(); it does not preclude using +* guard(), scoped_guard(), or ACQUIRE() in a function that uses goto. */ /*
Signature · 1 line
2.51.1
Quoted 3 lines · Przemek Kitszel
Scope-based resource management helps manage resource lifetimes, but it does not solve rollback of a failed operation. Such rollback is often best handled with multiple goto-based unwind paths.
Not sure I agree; you can fully convert to cleanup. Sometimes a little tedious but often less horrible than you might imagine.
Quoted 23 lines
The current guidance can be read as discouraging any cleanup helper alongside goto, including guards. Automated reviewers repeatedly flag code that mixes the two, so clarify that the recommendation applies only to
__free().Signed-off-by Przemek Kitszel <[email protected]>---
I develop mostly in the subsystem that bans__free(), which is also most low-level of all the helpers - so I get that we could discourage mixing__free()and goto. But for the rest it is counter productive.--- include/linux/cleanup.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)include/linux/cleanup.h +0
-1@@ -145,12 +145,10 @@* statement and not group variable definitions at the top of the * function when __free() is used. * -* Lastly, given that the benefit of cleanup helpers is removal of
include/linux/cleanup.h line 152
Quoted 10 lines
include/linux/cleanup.h +4
-5-* "goto", and that the "goto" statement can jump between scopes, the -* expectation is that usage of "goto" and cleanup helpers is never -* mixed in the same function. I.e. for a given routine, convert all -* resources that need a "goto" cleanup to scope-based cleanup, or -* convert none of them. +* Lastly, avoid mixing __free() with goto-based resource cleanup in +* the same function. This recommendation applies specifically to +* resources managed with __free(); it does not preclude using +* guard(), scoped_guard(), or ACQUIRE() in a function that uses goto. */
This isn't entirely accurate, guard() is likewise tricky.
Consider:
void foo()
{
...
if (!bar())
goto bar_failed;
guard(spinlock)(&my_lock);
...
return;bar_failed:
undo_pre_bar(); }
Then undo_pre_bar() is inside guard(); and that is very much not the intention. The same will be true with ACQUIRE();
The whole thing is all about scope. It really is a point to be careful about.
The scoped_guard() is safer, mostly because people typically will not goto into one.
Now, clang will actually warn if you mess up; but GCC will not and will happily mis-compile your code for you.
At all time; be very careful when mixing goto with cleanup. The general advice to not mix is safest. That is not to say it can't be done when you actually know what you're on about. But clearly the AI bots are clueless.