Skip to contentSkip to conversation

Add scope-based cleanup capability to eventfd_ctx

PATCH Superseded Bence Csokas 2 participants 4 messages Sep 15

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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 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 +++-----
 fs/eventfd.c                  | 3 +++
 include/linux/eventfd.h       | 3 +++
 3 files changed, 9 insertions(+), 5 deletions(-)

---
base-commit: 587858367581b9c55c3690f4e63382ad622719d4
change-id: 20260826-efd-autofree-766608e8d8aa

Best regards,

Signature · 1 line
Bence Csokas <[email protected]>
#1 lore raw

PATCH v2 1/2 eventfd: Add scope-based cleanup capability

Allow eventfd_ctx_put() to be used with the RAII-inspired __free() API.

---
 fs/eventfd.c            | 3 +++
 include/linux/eventfd.h | 3 +++
 2 files changed, 6 insertions(+)
fs/eventfd.c +3 -0
index 9d33a02757d5..1cd1741b81b1 100644
@@ -102,6 +102,9 @@ static void eventfd_free(struct kref *kref)
  */
 void eventfd_ctx_put(struct eventfd_ctx *ctx)
 {
+	if (IS_ERR_OR_NULL(ctx))
+		return;
+
 	kref_put(&ctx->kref, eventfd_free);
 }
 EXPORT_SYMBOL_GPL(eventfd_ctx_put);
include/linux/eventfd.h +3 -0
index e32bee4345fb..689358c2b0ae 100644
@@ -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 *, eventfd_ctx_put(_T))
+
 #endif /* _LINUX_EVENTFD_H */
 
Signature · 1 line
2.55.0
#2 lore raw

PATCH v2 2/2 drm/syncobj: Use scope-based cleanup for eventfd_ctx

In reply to Bence Csokas

Now 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
index c23a5de27eff..efc4e8af220c 100644
@@ -1462,9 +1462,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;
 
@@ -1490,10 +1490,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;
 
@@ -1502,8 +1502,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.55.0
#3 lore raw

Re: [PATCH v2 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: - [Medium] cleanup: mixing goto and scope-based cleanup in drm_syncobj_eventfd_ioctl --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] cleanup: mixing goto and scope-based cleanup in drm_syncobj_eventfd_ioctl --

commit ff6fd047e86cd28041b0ce12099546c46df110b2
Author: Bence Csokas <[email protected]>

drm/syncobj: Use scope-based cleanup for eventfd_ctx

This patch converts the eventfd context in drm_syncobj_eventfd_ioctl() to use scope-based cleanup now that eventfd supports it.

Quoted 11 lines
drivers/gpu/drm/drm_syncobj.c +1 -0
index c23a5de27effa..efc4e8af220c8 100644
@@ -1462,9 +1462,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;

[ ... ]

drivers/gpu/drm/drm_syncobj.c drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data, · line 1493

Quoted 6 lines
drivers/gpu/drm/drm_syncobj.c +1 -1
@@ -1490,10 +1490,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;

[Severity: Medium]
Does this mix goto-based error handling and scope-based cleanup within drm_syncobj_eventfd_ioctl()?

According to include/linux/cleanup.h, the expectation is that usage of goto and cleanup helpers is never mixed in the same function. All resources that need cleanup should either be converted to scope-based cleanup, or none of them.

Leaving the err_fdget label to manually call drm_syncobj_put(syncobj) while using __free(eventfd) for ev_fd_ctx can lead to confusing resource ownership and break the expected LIFO teardown ordering.

Could syncobj be converted to use scope-based cleanup as well?

Quoted 6 lines
drivers/gpu/drm/drm_syncobj.c +1 -1
 	}
 	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;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2