Add scope-based cleanup capability to eventfd_ctx
Subsystems (3)
v3 cover Add scope-based cleanup capability to eventfd_ctx
Add __free() support to eventfd and update drm_syncobj to use it as an example consumer.
A minor discrepancy I encountered is whether to put a semicolon after DEFINE_FREE(). It seems some in-tree code does, some doesn't. In the end, I followed the example set in core-api/cleanup.rst, and omitted it.
---
Changes in v3:
- IS_ERR_OR_NULL() moved into DEFINE_FREE() body
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- IS_ERR_OR_NULL() check, instead of NULLing out pointer on IS_ERR
- Link to v1: https://patch.msgid.link/[email protected]
To: Alexander Viro <[email protected]>
To: Christian Brauner <[email protected]>
To: Jan Kara <[email protected]>
To: Maarten Lankhorst <[email protected]>
To: Maxime Ripard <[email protected]>
To: Thomas Zimmermann <[email protected]>
To: David Airlie <[email protected]>
To: Simona Vetter <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Bence Csokas (2):
eventfd: Add scope-based cleanup capability drm/syncobj: Use scope-based cleanup for eventfd_ctx
drivers/gpu/drm/drm_syncobj.c | 8 +++----- include/linux/eventfd.h | 3 +++ 2 files changed, 6 insertions(+), 5 deletions(-)
---
base-commit: d135ab453a7400a34930bdda1ef4bdc8acfac80e
change-id: 20260826-efd-autofree-766608e8d8aa
Best regards,
Signature · 1 line
Bence Csokas <[email protected]>
PATCH v3 1/2 eventfd: Add scope-based cleanup capability
Allow eventfd_ctx_put() to be used with the RAII-inspired __free() API.
--- include/linux/eventfd.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/eventfd.h +3 -0
@@ -10,6 +10,7 @@#define _LINUX_EVENTFD_H#include <linux/wait.h> +#include <linux/cleanup.h> #include <linux/err.h> #include <linux/percpu-defs.h> #include <linux/percpu.h>
@@ -89,5 +90,7 @@ static inline void eventfd_signal(struct eventfd_ctx *ctx)eventfd_signal_mask (ctx,0 ); } +DEFINE_FREE(eventfd, struct eventfd_ctx *, if (!IS_ERR_OR_NULL(_T)) eventfd_ctx_put(_T)) +#endif /* _LINUX_EVENTFD_H */
Signature · 1 line
2.56.0
PATCH v3 2/2 drm/syncobj: Use scope-based cleanup for eventfd_ctx
In reply to Bence CsokasNow that eventfd supports __free(), use it in drm_syncobj_eventfd_ioctl().
--- drivers/gpu/drm/drm_syncobj.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpu/drm/drm_syncobj.c +3 -5
@@ -1429,9 +1429,9 @@ int drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) { + struct eventfd_ctx *ev_fd_ctx __free(eventfd) = NULL;struct drm_syncobj_eventfd *args = data;struct drm_syncobj *syncobj; -struct eventfd_ctx *ev_fd_ctx;struct syncobj_eventfd_entry *entry;int ret;
@@ -1457,10 +1457,10 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data, entry =kzalloc_obj (*entry);if (!entry) { ret = -ENOMEM ; - goto err_kzalloc; + goto err_fdget; } entry->syncobj = syncobj; - entry->ev_fd_ctx = ev_fd_ctx; + entry->ev_fd_ctx =no_free_ptr (ev_fd_ctx); entry->point = args->point; entry->flags = args->flags;
@@ -1469,8 +1469,6 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,return 0 ; -err_kzalloc: -eventfd_ctx_put (ev_fd_ctx); err_fdget:drm_syncobj_put (syncobj);return ret;
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v3 2/2] drm/syncobj: Use scope-based cleanup for eventfd_ctx
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] drm/syncobj: mixing __free() and goto cleanup in drm_syncobj_eventfd_ioctl() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] drm/syncobj: mixing __free() and goto cleanup in drm_syncobj_eventfd_ioctl() --
commit 235e12bb4f2858e1c0f1b573b50d4958dd229203
Author: Bence Csokas <[email protected]>
drm/syncobj: Use scope-based cleanup for eventfd_ctx
This commit converts ev_fd_ctx in drm_syncobj_eventfd_ioctl() to use __free(eventfd) and no_free_ptr(), removing the err_kzalloc error label.
Quoted 29 lines
drivers/gpu/drm/drm_syncobj.c +3
-3@@ -1429,9 +1429,9 @@ int drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) { + struct eventfd_ctx *ev_fd_ctx __free(eventfd) = NULL;struct drm_syncobj_eventfd *args = data;struct drm_syncobj *syncobj; -struct eventfd_ctx *ev_fd_ctx;struct syncobj_eventfd_entry *entry;int ret;@@ -1457,10 +1457,10 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data, entry =kzalloc_obj (*entry);if (!entry) { ret = -ENOMEM ; - goto err_kzalloc; + goto err_fdget; } entry->syncobj = syncobj; - entry->ev_fd_ctx = ev_fd_ctx; + entry->ev_fd_ctx =no_free_ptr (ev_fd_ctx); entry->point = args->point; entry->flags = args->flags;
drivers/gpu/drm/drm_syncobj.c drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data, · line 1474
Quoted 8 lines
drivers/gpu/drm/drm_syncobj.c +0
-2@@ -1469,8 +1469,6 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,return 0 ; -err_kzalloc: -eventfd_ctx_put (ev_fd_ctx); err_fdget:drm_syncobj_put (syncobj);return ret;
[Severity: Low]
In drivers/gpu/drm/drm_syncobj.c, drm_syncobj_eventfd_ioctl() declares ev_fd_ctx with __free(eventfd) = NULL at the top of the function while retaining goto err_fdget to clean up syncobj.
When kzalloc_obj(*entry) fails, jumping to err_fdget runs drm_syncobj_put(syncobj) before scope exit calls eventfd_ctx_put(ev_fd_ctx), which reverses the teardown order relative to acquisition:
syncobj = drm_syncobj_find(file_private, args->handle);
if (!syncobj)
return -ENOENT;
ev_fd_ctx = eventfd_ctx_fdget(args->fd);
if (IS_ERR(ev_fd_ctx)) {
ret = PTR_ERR(ev_fd_ctx);
goto err_fdget;
}
entry = kzalloc_obj(*entry);
if (!entry) {
ret = -ENOMEM;
goto err_fdget;
}The documentation in include/linux/cleanup.h notes that __free() variables should be defined and initialized in one statement rather than grouped at the top with = NULL, and states that goto cleanup and scope-based cleanup helpers should not be mixed in the same function:
* Given that the "__free(...) = NULL" pattern for variables defined at
* the top of the function poses this potential interdependency problem
* the recommendation is to always define and assign variables in one
* 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.
Should drm_syncobj_eventfd_ioctl() either convert syncobj to scope-based cleanup as well (or restructure the acquisition order so goto err_fdget is no longer needed), and define ev_fd_ctx at the point where eventfd_ctx_fdget() is called?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Christian Brauner
maintainerQuoted 9 lines · Bence Csokas
Add
__free()support to eventfd and updatedrm_syncobjto use it as an example consumer.A minor discrepancy I encountered is whether to put a semicolon after
DEFINE_FREE(). It seems some in-tree code does, some doesn't. In the end, I followed the example set incore-api/cleanup.rst, and omitted it.Signed-off-by Bence Csokas <[email protected]>---
I'm really having trouble seeing how this improves anything significantly. Probably others have the same impression which is why this doesn't get a lot of review.
Hi,
Quoted 8 lines · Christian Brauner
Quoted 7 lines · Bence Csokas
Add
__free()support to eventfd and updatedrm_syncobjto use it as an example consumer.A minor discrepancy I encountered is whether to put a semicolon after
DEFINE_FREE(). It seems some in-tree code does, some doesn't. In the end, I followed the example set incore-api/cleanup.rst, and omitted it.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Bence Csokas <[email protected]>---
I'm really having trouble seeing how this improves anything significantly. Probably others have the same impression which is why this doesn't get a lot of review.
The goal of the __free() API is to reduce the number of goto-based error paths. Normally, you'd have to keep the state of every local in your head for every early return. (Has it been initialized? Has it been consumed? Or does it need to be freed?) Then you need to arrange your "goto tree" (error labels) in a way that frees everything, and then for every early exit, jump to the correct one. This can be quite complex for some functions. Look at KVM or HyperV eventfd code, for example.
By contrast, you just mark the local var with _free() and an appropriate destructor snippet, and the consumption point (the point after which we don't need to free it on a return) with no_free_ptr(), and the compiler instruments every early return accordingly.
Bence
Jan Kara
reviewerHi!
Quoted 24 lines · Bence Csókás
Quoted 8 lines · Christian Brauner
Quoted 7 lines · Bence Csokas
Add
__free()support to eventfd and updatedrm_syncobjto use it as an example consumer.A minor discrepancy I encountered is whether to put a semicolon after
DEFINE_FREE(). It seems some in-tree code does, some doesn't. In the end, I followed the example set incore-api/cleanup.rst, and omitted it.Quoted 6 lines
Quoted 2 lines
Signed-off-by Bence Csokas <[email protected]>---
I'm really having trouble seeing how this improves anything significantly. Probably others have the same impression which is why this doesn't get a lot of review.
The goal of the
__free()API is to reduce the number of goto-based error paths. Normally, you'd have to keep the state of every local in your head for every early return. (Has it been initialized? Has it been consumed? Or does it need to be freed?) Then you need to arrange your "goto tree" (error labels) in a way that frees everything, and then for every early exit, jump to the correct one. This can be quite complex for some functions. Look at KVM or HyperV eventfd code, for example.
Quoted 4 lines
By contrast, you just mark the local var with
_free()and an appropriate destructor snippet, and the consumption point (the point after which we don't need to free it on a return) withno_free_ptr(), and the compiler instruments every early return accordingly.
Sure, but there's also the other side about which you don't speak: unless the destructor is trivial (like kfree()), there are implications about locking, when resources (such as references held by the object) get released etc. So it can also be more complex to wrap your head around __free usage than around standard cleanup code.
So unless __free is providing a noticeable simplification of some goto hell, cleanup in many functions or similar, I generally don't find it worth it...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Hi,
Quoted 32 lines · Jan Kara
Hi!
Quoted 24 lines · Bence Csókás
Quoted 8 lines · Christian Brauner
Quoted 7 lines · Bence Csokas
Add
__free()support to eventfd and updatedrm_syncobjto use it as an example consumer.A minor discrepancy I encountered is whether to put a semicolon after
DEFINE_FREE(). It seems some in-tree code does, some doesn't. In the end, I followed the example set incore-api/cleanup.rst, and omitted it.Quoted 6 lines
Quoted 2 lines
Signed-off-by Bence Csokas <[email protected]>---
I'm really having trouble seeing how this improves anything significantly. Probably others have the same impression which is why this doesn't get a lot of review.
The goal of the
__free()API is to reduce the number of goto-based error paths. Normally, you'd have to keep the state of every local in your head for every early return. (Has it been initialized? Has it been consumed? Or does it need to be freed?) Then you need to arrange your "goto tree" (error labels) in a way that frees everything, and then for every early exit, jump to the correct one. This can be quite complex for some functions. Look at KVM or HyperV eventfd code, for example.Quoted 4 lines
By contrast, you just mark the local var with
_free()and an appropriate destructor snippet, and the consumption point (the point after which we don't need to free it on a return) withno_free_ptr(), and the compiler instruments every early return accordingly.
Quoted 5 lines
Sure, but there's also the other side about which you don't speak: unless the destructor is trivial (like
kfree()), there are implications about locking, when resources (such as references held by the object) get released etc. So it can also be more complex to wrap your head around__freeusage than around standard cleanup code.
Sure. Thankfully eventfd_ctx_put() is one of the pretty trivial ones, basically just calling kfree() on itself when the kref drops to 0.
Quoted 5 lines
So unless
__freeis providing a noticeable simplification of some goto hell, cleanup in many functions or similar, I generally don't find it worth it...Honza
Look into for instance mshv_irqfd_assign() and weep... And then imagine how much simpler it'd be if it were using __free() instead.
For the example in 2/2, I deliberately chose one callsite where it's trivial to deduce that behaviour is unchanged.
Bence
Jan Kara
reviewerHi!
Quoted 33 lines · Bence Csókás
Quoted 32 lines · Jan Kara
Hi!
Quoted 28 lines · Bence Csókás
Quoted 8 lines · Christian Brauner
Quoted 7 lines · Bence Csokas
Add
__free()support to eventfd and updatedrm_syncobjto use it as an example consumer.A minor discrepancy I encountered is whether to put a semicolon after
DEFINE_FREE(). It seems some in-tree code does, some doesn't. In the end, I followed the example set incore-api/cleanup.rst, and omitted it.Quoted 6 lines
Quoted 2 lines
Signed-off-by Bence Csokas <[email protected]>---
I'm really having trouble seeing how this improves anything significantly. Probably others have the same impression which is why this doesn't get a lot of review.
The goal of the
__free()API is to reduce the number of goto-based error paths. Normally, you'd have to keep the state of every local in your head for every early return. (Has it been initialized? Has it been consumed? Or does it need to be freed?) Then you need to arrange your "goto tree" (error labels) in a way that frees everything, and then for every early exit, jump to the correct one. This can be quite complex for some functions. Look at KVM or HyperV eventfd code, for example.By contrast, you just mark the local var with
_free()and an appropriate destructor snippet, and the consumption point (the point after which we don't need to free it on a return) withno_free_ptr(), and the compiler instruments every early return accordingly.
Quoted 8 lines
Quoted 5 lines
Sure, but there's also the other side about which you don't speak: unless the destructor is trivial (like
kfree()), there are implications about locking, when resources (such as references held by the object) get released etc. So it can also be more complex to wrap your head around__freeusage than around standard cleanup code.Sure. Thankfully
eventfd_ctx_put()is one of the pretty trivial ones, basically just callingkfree()on itself when the kref drops to 0.
Ok, fair.
Quoted 6 lines
Quoted 3 lines
So unless
__freeis providing a noticeable simplification of some goto hell, cleanup in many functions or similar, I generally don't find it worth it...Look into for instance
mshv_irqfd_assign()and weep... And then imagine how much simpler it'd be if it were using__free()instead.
Well, over the years I've seen much worse things :) But I agree for that function using __free would be a simplification.
Quoted 2 lines
For the example in 2/2, I deliberately chose one callsite where it's trivial to deduce that behaviour is unchanged.
Well, the change is kind of "meh, I don't care much" for me. So I guess if you include some conversion that actually makes code simpler, I guess Christian might be open to merging this.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerQuoted 3 lines · Jan Kara
Well, the change is kind of "meh, I don't care much" for me. So I guess if you include some conversion that actually makes code simpler, I guess Christian might be open to merging this.
No, I agree with you. I think if at all it should go together with a conversion that actually gets rid of a goto mess, e.g. mshv_irqfd_assign() or sm.