Skip to contentSkip to conversation

Add scope-based cleanup capability to eventfd_ctx

PATCH Superseded Bence Csokas 2 participants 5 messages Sep 9

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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.

---
Bence Csokas (2):
eventfd: Add scope-based cleanup capability drm/syncobj: Use scope-based cleanup for eventfd_ctx

 drivers/gpu/drm/drm_syncobj.c | 9 ++++-----
 fs/eventfd.c                  | 3 +++
 include/linux/eventfd.h       | 3 +++
 3 files changed, 10 insertions(+), 5 deletions(-)

---
base-commit: 893e11787f78e43b534e252249ac3fff4d1333f8
change-id: 20260826-efd-autofree-766608e8d8aa

Best regards,

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

PATCH 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..24111a16284f 100644
@@ -102,6 +102,9 @@ static void eventfd_free(struct kref *kref)
  */
 void eventfd_ctx_put(struct eventfd_ctx *ctx)
 {
+	if (!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

Re: [PATCH 1/2] eventfd: Add scope-based cleanup capability

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The DEFINE_FREE wrapper for eventfd lacks an IS_ERR_OR_NULL check. When used with…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The DEFINE_FREE wrapper for eventfd lacks an IS_ERR_OR_NULL check. When used with eventfd_ctx_fdget() or eventfd_ctx_fileget() (which return ERR_PTR on failure), the cleanup routine will pass the error pointer to eventfd_ctx_put(), bypassing its NULL check and causing a kernel panic when dereferenced. --

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

eventfd: Add scope-based cleanup capability

This commit introduces a scope-based cleanup capability for eventfd contexts, allowing eventfd_ctx_put() to be used with the RAII-inspired __free() API.

fs/eventfd.c static void eventfd_free(struct kref *kref) · line 108

Quoted 12 lines
fs/eventfd.c +3 -0
index 9d33a02757d52..24111a16284f1 100644
@@ -102,6 +102,9 @@ static void eventfd_free(struct kref *kref)
  */
 void eventfd_ctx_put(struct eventfd_ctx *ctx)
 {
+	if (!ctx)
+		return;
+
 	kref_put(&ctx->kref, eventfd_free);

[Severity: High]
Does this release function adequately protect against error pointers?

It appears to only guard against NULL, so if the __free wrapper invokes this function with an ERR_PTR, it seems like it will bypass this check and cause a kernel panic when dereferencing ctx->kref.

Quoted 6 lines
fs/eventfd.c +0 -0
 }
 EXPORT_SYMBOL_GPL(eventfd_ctx_put);
include/linux/eventfd.h +0 -0
index e32bee4345fb9..689358c2b0ae7 100644

[ ... ]

include/linux/eventfd.h static inline void eventfd_signal(struct eventfd_ctx *ctx) · line 93

Quoted 5 lines
include/linux/eventfd.h +1 -0
@@ -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))

[Severity: High]
Is this cleanup wrapper safe to use with functions like eventfd_ctx_fdget() or eventfd_ctx_fileget() that return ERR_PTR on failure?

Because this wrapper lacks an IS_ERR_OR_NULL check, if a getter fails and returns an ERR_PTR, the early return path will pass the error pointer to eventfd_ctx_put(), bypassing its NULL check. This seems to violate the cleanup function compatibility guidelines and could lead to a crash.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH 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 | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/gpu/drm/drm_syncobj.c +4 -5
index c23a5de27eff..261d5606e8bd 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;
 
@@ -1484,16 +1484,17 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,
 	ev_fd_ctx = eventfd_ctx_fdget(args->fd);
 	if (IS_ERR(ev_fd_ctx)) {
 		ret = PTR_ERR(ev_fd_ctx);
+		ev_fd_ctx = NULL;
 		goto err_fdget;
 	}
 
 	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 +1503,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
#4 lore raw

Re: [PATCH 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] Mixing goto-based error handling with scope-based cleanup (__free) in the same function. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Mixing goto-based error handling with scope-based cleanup (__free) in the same function. --

commit 991d574653fb7465f62bf99703d5af0d6fe21920
Author: Bence Csokas <[email protected]>

drm/syncobj: Use scope-based cleanup for eventfd_ctx

This patch modifies drm_syncobj_eventfd_ioctl() to use the scope-based cleanup helper for eventfd_ctx, removing the explicit put calls.

Quoted 11 lines
drivers/gpu/drm/drm_syncobj.c +1 -0
index c23a5de27effa..261d5606e8bd8 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 1508

Quoted 9 lines
drivers/gpu/drm/drm_syncobj.c +0 -2
@@ -1502,8 +1503,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: Medium]
Does this code in drivers/gpu/drm/drm_syncobj.c:drm_syncobj_eventfd_ioctl() mix goto-based error handling and scope-based cleanup in the same function?

The cleanup subsystem documentation in include/linux/cleanup.h states that for a given routine, all resources that need a "goto" cleanup should be converted to scope-based cleanup, or none of them should be. Mixing the two creates confusing ownership semantics.

Could the syncobj cleanup also be converted to use __free(), or should we stick to goto-based cleanup for both resources in this function?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2