mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] cleanup: clarify scope-based resource management and goto guidance
@ 2026-11-25 16:12 Przemek Kitszel
  2026-11-25 16:44 ` Peter Zijlstra
  0 siblings, 1 reply; 5+ messages in thread
From: Przemek Kitszel @ 2026-11-25 16:12 UTC (permalink / raw)
  To: Peter Zijlstra, linux-kernel
  Cc: Dan Williams, Jonathan Cameron, Kevin Tian, Dmitry Ilvokhin,
	Christian Brauner, Ira Weiny, 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.

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 <przemyslaw.kitszel@intel.com>
---
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(-)

diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 1e09aead4b08..3b852198e969 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -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.
  */
 
 /*
-- 
2.51.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cleanup: clarify scope-based resource management and goto guidance
  2026-11-25 16:12 [PATCH] cleanup: clarify scope-based resource management and goto guidance Przemek Kitszel
@ 2026-11-25 16:44 ` Peter Zijlstra
  2026-11-26  4:29   ` Przemek Kitszel
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Zijlstra @ 2026-11-25 16:44 UTC (permalink / raw)
  To: Przemek Kitszel
  Cc: linux-kernel, Dan Williams, Jonathan Cameron, Kevin Tian,
	Dmitry Ilvokhin, Christian Brauner, Ira Weiny

On Wed, Nov 25, 2026 at 05:12:03PM +0100, Przemek Kitszel wrote:
> 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.

> 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 <przemyslaw.kitszel@intel.com>
> ---
> 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(-)
> 
> diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
> index 1e09aead4b08..3b852198e969 100644
> --- a/include/linux/cleanup.h
> +++ b/include/linux/cleanup.h
> @@ -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.
>   */

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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cleanup: clarify scope-based resource management and goto guidance
  2026-11-25 16:44 ` Peter Zijlstra
@ 2026-11-26  4:29   ` Przemek Kitszel
  2026-11-26  9:50     ` Peter Zijlstra
  0 siblings, 1 reply; 5+ messages in thread
From: Przemek Kitszel @ 2026-11-26  4:29 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: linux-kernel, Kevin Tian, Dmitry Ilvokhin, Christian Brauner

On 11/25/26 5:44 PM, Peter Zijlstra wrote:
> On Wed, Nov 25, 2026 at 05:12:03PM +0100, Przemek Kitszel wrote:
>> 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.
> 
>> 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 <przemyslaw.kitszel@intel.com>
>> ---
>> 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(-)
>>
>> diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
>> index 1e09aead4b08..3b852198e969 100644
>> --- a/include/linux/cleanup.h
>> +++ b/include/linux/cleanup.h
>> @@ -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.
>>    */
> 
> 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();

ah, I see :(

would it be fine to relax the doc and stop discouraging against
scoped_guard() and goto in the same function (but keep the "don't jump
into/out of its scope")?

> 
> 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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cleanup: clarify scope-based resource management and goto guidance
  2026-11-26  4:29   ` Przemek Kitszel
@ 2026-11-26  9:50     ` Peter Zijlstra
  2026-11-26 14:28       ` Przemek Kitszel
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Zijlstra @ 2026-11-26  9:50 UTC (permalink / raw)
  To: Przemek Kitszel
  Cc: linux-kernel, Kevin Tian, Dmitry Ilvokhin, Christian Brauner

On Thu, Nov 26, 2026 at 05:29:33AM +0100, Przemek Kitszel wrote:

> > Then undo_pre_bar() is inside guard(); and that is very much not the
> > intention. The same will be true with ACQUIRE();
> 
> ah, I see :(
> 
> would it be fine to relax the doc and stop discouraging against
> scoped_guard() and goto in the same function (but keep the "don't jump
> into/out of its scope")?

So I don't really understand the problem we're trying to solve here. Is
this really about AI being stupid? Just wait a few months, I'll catch up
or something.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cleanup: clarify scope-based resource management and goto guidance
  2026-11-26  9:50     ` Peter Zijlstra
@ 2026-11-26 14:28       ` Przemek Kitszel
  0 siblings, 0 replies; 5+ messages in thread
From: Przemek Kitszel @ 2026-11-26 14:28 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: linux-kernel, Kevin Tian, Dmitry Ilvokhin, Christian Brauner

On 11/26/26 10:50 AM, Peter Zijlstra wrote:
> On Thu, Nov 26, 2026 at 05:29:33AM +0100, Przemek Kitszel wrote:
> 
>>> Then undo_pre_bar() is inside guard(); and that is very much not the
>>> intention. The same will be true with ACQUIRE();
>>
>> ah, I see :(
>>
>> would it be fine to relax the doc and stop discouraging against
>> scoped_guard() and goto in the same function (but keep the "don't jump
>> into/out of its scope")?
> 
> So I don't really understand the problem we're trying to solve here. Is
> this really about AI being stupid? Just wait a few months, I'll catch up
> or something.

AI is smart enough to follow our (human) written instructions, and right
now, those discourage scoped_guard() mixed with goto.
I would like to reword to fix that :)

Of course it will also be good for those of us who read docs, and also
have the additional benefit of not popping up scoped_guard() usage
(I don't believe, but there are still maintainers that prefer old style
locks ;P)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-26 14:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 16:12 [PATCH] cleanup: clarify scope-based resource management and goto guidance Przemek Kitszel
2026-11-25 16:44 ` Peter Zijlstra
2026-11-26  4:29   ` Przemek Kitszel
2026-11-26  9:50     ` Peter Zijlstra
2026-11-26 14:28       ` Przemek Kitszel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®