* [PATCH 1/4] drm: Add drm_work_fence helper
@ 2026-09-03 5:29 Srinivasan Shanmugam
2026-09-03 5:29 ` [PATCH 2/4] drm: Add drm_user_fence helper Srinivasan Shanmugam
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-03 5:29 UTC (permalink / raw)
To: linux-kernel
GPU drivers often need to queue work when a dma-fence signals
because certain operations (copy_to_user, eventfd_signal, memory
allocation) cannot run in IRQ context. This pattern is currently
open-coded in multiple drivers.
Introduce drm_work_fence — an embeddable base structure that handles the
dma-fence-callback-to-workqueue pattern in one place. Drivers embed this
in their own structure and implement ops->writeback() for the deferred
work and ops->destroy() for cleanup.
The helper manages:
- kref lifetime
- dma-fence callback registration
- workqueue dispatch on fence signal
- safe cancellation before driver teardown
For work that additionally requires borrowing the process MM via
kthread_use_mm(), see drm_user_fence which builds on top of this.
Suggested-by: Matthew Brost <matthew.brost@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
---
drivers/gpu/drm/Makefile | 1 +
drivers/gpu/drm/drm_work_fence.c | 184 +++++++++++++++++++++++++++++++
include/drm/drm_work_fence.h | 69 ++++++++++++
3 files changed, 254 insertions(+)
create mode 100644 drivers/gpu/drm/drm_work_fence.c
create mode 100644 include/drm/drm_work_fence.h
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index e97faabcd783..c5be8e80d0c8 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -72,6 +72,7 @@ drm-y := \
drm_vblank.o \
drm_vblank_work.o \
drm_vma_manager.o \
+ drm_work_fence.o \
drm_writeback.o
drm-$(CONFIG_DRM_CLIENT) += \
drm_client.o \
diff --git a/drivers/gpu/drm/drm_work_fence.c b/drivers/gpu/drm/drm_work_fence.c
new file mode 100644
index 000000000000..482eb3d4475f
--- /dev/null
+++ b/drivers/gpu/drm/drm_work_fence.c
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 The Linux Foundation
+ *
+ * Common DRM work fence helper.
+ *
+ * When a GPU dma-fence signals, drivers often need to perform work that
+ * cannot run in IRQ context (e.g., memory allocation, copy_to_user,
+ * eventfd_signal). This helper queues a work item when a dma-fence
+ * signals, allowing that work to run safely in a workqueue context.
+ *
+ * NOTE: This helper consumes dma_fences but CANNOT implement
+ * dma_fence_ops. Work items queued here may sleep; dma_fence_ops
+ * callbacks are called under the fence spinlock and must not sleep.
+ *
+ * For work that additionally requires accessing userspace memory via
+ * kthread_use_mm(), see drm_user_fence which builds on top of this.
+ */
+
+#include <linux/workqueue.h>
+
+#include <drm/drm_work_fence.h>
+
+static void drm_work_fence_destroy(struct kref *kref)
+{
+ struct drm_work_fence *wfence =
+ container_of(kref, struct drm_work_fence, refcount);
+ struct dma_fence *fence = wfence->fence;
+
+ wfence->ops->destroy(wfence);
+ dma_fence_put(fence); /* NULL-safe */
+}
+
+/**
+ * drm_work_fence_get - Acquire a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_get(struct drm_work_fence *wfence)
+{
+ kref_get(&wfence->refcount);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_get);
+
+/**
+ * drm_work_fence_put - Release a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_put(struct drm_work_fence *wfence)
+{
+ kref_put(&wfence->refcount, drm_work_fence_destroy);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_put);
+
+static void drm_work_fence_work(struct work_struct *w)
+{
+ struct drm_work_fence *wfence =
+ container_of(w, struct drm_work_fence, work);
+
+ wfence->ops->writeback(wfence);
+ drm_work_fence_put(wfence);
+}
+
+static void drm_work_fence_queue(struct drm_work_fence *wfence)
+{
+ queue_work(wfence->wq, &wfence->work);
+}
+
+static void drm_work_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
+{
+ struct drm_work_fence *wfence =
+ container_of(cb, struct drm_work_fence, cb);
+
+ drm_work_fence_queue(wfence);
+ /* Single ref: wfence->fence released in drm_work_fence_destroy(). */
+}
+
+/**
+ * drm_work_fence_init - Initialize a work fence
+ * @wfence: work fence to initialize
+ * @wq: workqueue to run the worker on (must be ordered if sequencing matters)
+ * @ops: driver operations
+ */
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops)
+{
+ kref_init(&wfence->refcount);
+ wfence->wq = wq;
+ wfence->ops = ops;
+ wfence->fence = NULL;
+ INIT_WORK(&wfence->work, drm_work_fence_work);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_init);
+
+/**
+ * drm_work_fence_add_callback - Attach a work fence to a dma-fence
+ * @wfence: work fence; caller retains their reference and must release
+ * it via drm_work_fence_put() when no longer needed
+ * @fence: dma-fence to watch; one reference is consumed on any return value
+ *
+ * When @fence signals, a work item is queued that calls ops->writeback().
+ * If @fence has already signaled, the work item is queued immediately.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence)
+{
+ int err;
+
+ drm_work_fence_get(wfence);
+ wfence->fence = fence; /* transfer caller's ref — single ref, no get */
+
+ err = dma_fence_add_callback(fence, &wfence->cb, drm_work_fence_cb);
+ if (err == -ENOENT) {
+ drm_work_fence_queue(wfence);
+ err = 0;
+ } else if (err) {
+ wfence->fence = NULL;
+ dma_fence_put(fence);
+ drm_work_fence_put(wfence);
+ }
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_add_callback);
+
+/**
+ * drm_work_fence_cancel - Cancel a pending work fence callback
+ * @wfence: work fence
+ *
+ * Attempts to remove the pending callback before driver context teardown.
+ * The caller must hold a reference to @wfence across this call.
+ *
+ * If the callback has already fired this returns false and all cleanup
+ * has been handled internally.
+ *
+ * If removal succeeds the callback reference is released internally.
+ * The caller must still release its own reference via drm_work_fence_put().
+ *
+ * This function is safe to call from atomic context as it only acquires
+ * the dma-fence spinlock internally. If the caller also needs to wait
+ * for the worker to finish, use drm_work_fence_cancel_sync() instead,
+ * which may sleep.
+ *
+ * Return: true if callback was removed, false if it had already fired.
+ */
+bool drm_work_fence_cancel(struct drm_work_fence *wfence)
+{
+ struct dma_fence *fence = wfence->fence;
+
+ if (!fence)
+ return false;
+
+ if (dma_fence_remove_callback(fence, &wfence->cb)) {
+ drm_work_fence_put(wfence); /* drop ref from add_callback */
+ return true;
+ }
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel);
+
+/**
+ * drm_work_fence_cancel_sync - Cancel callback and wait for worker to finish
+ * @wfence: work fence
+ *
+ * Calls drm_work_fence_cancel() then cancel_work_sync() to guarantee
+ * the worker has fully completed before returning.
+ *
+ * This function may sleep. Must not be called from atomic or interrupt
+ * context. Use drm_work_fence_cancel() instead when sleeping is not allowed.
+ *
+ * Drivers must call this during teardown before freeing any resources
+ * accessed by ops->writeback().
+ */
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence)
+{
+ if (drm_work_fence_cancel(wfence))
+ return;
+ if (cancel_work_sync(&wfence->work))
+ drm_work_fence_put(wfence);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel_sync);
diff --git a/include/drm/drm_work_fence.h b/include/drm/drm_work_fence.h
new file mode 100644
index 000000000000..c8e3c5b9f0c4
--- /dev/null
+++ b/include/drm/drm_work_fence.h
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 The Linux Foundation
+ */
+
+#ifndef __DRM_WORK_FENCE_H__
+#define __DRM_WORK_FENCE_H__
+
+#include <linux/dma-fence.h>
+#include <linux/kref.h>
+#include <linux/workqueue.h>
+
+struct drm_work_fence;
+
+/**
+ * struct drm_work_fence_ops - driver callbacks for a DRM work fence
+ */
+struct drm_work_fence_ops {
+ /**
+ * @writeback: Called from workqueue context when the dma-fence signals.
+ *
+ * Perform the deferred work here (copy_to_user, eventfd_signal, etc.).
+ * May sleep. Must not requeue the fence.
+ */
+ void (*writeback)(struct drm_work_fence *wfence);
+
+ /**
+ * @destroy: Called when the last reference is dropped.
+ * Free the containing structure here.
+ */
+ void (*destroy)(struct drm_work_fence *wfence);
+};
+
+/**
+ * struct drm_work_fence - DRM dma-fence-to-workqueue helper
+ *
+ * Embeddable base structure that queues a work item when a dma-fence signals.
+ * Drivers embed this in their own structure and implement ops->writeback()
+ * for the deferred work and ops->destroy() for cleanup.
+ *
+ * Call drm_work_fence_init() at creation and drm_work_fence_add_callback()
+ * to arm on a dma-fence. Call drm_work_fence_cancel_sync() before teardown.
+ */
+struct drm_work_fence {
+ /** @refcount: Reference count. */
+ struct kref refcount;
+ /** @wq: Workqueue on which to run the worker. */
+ struct workqueue_struct *wq;
+ /** @ops: Driver operations. */
+ const struct drm_work_fence_ops *ops;
+ /** @fence: The watched dma-fence; holds a single reference. */
+ struct dma_fence *fence;
+ /** @work: Work item queued when the fence signals. */
+ struct work_struct work;
+ /** @cb: Callback registered on the dma-fence. */
+ struct dma_fence_cb cb;
+};
+
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops);
+void drm_work_fence_get(struct drm_work_fence *wfence);
+void drm_work_fence_put(struct drm_work_fence *wfence);
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence);
+bool drm_work_fence_cancel(struct drm_work_fence *wfence);
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence);
+
+#endif /* __DRM_WORK_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH 2/4] drm: Add drm_user_fence helper
2026-09-03 5:29 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
@ 2026-09-03 5:29 ` Srinivasan Shanmugam
2026-09-03 5:29 ` [PATCH 3/4] drm/xe: Convert xe_user_fence to drm_user_fence Srinivasan Shanmugam
2026-09-03 5:29 ` [PATCH 4/4] drm: Add per-signal compare functionality " Srinivasan Shanmugam
2 siblings, 0 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-03 5:29 UTC (permalink / raw)
To: linux-kernel
Introduce a common DRM user fence helper providing the kref-managed,
MM-borrowing dma-fence-callback-to-workqueue pattern used by drivers
that must access userspace memory from a kthread context when a GPU
fence signals.
XE uses this pattern (xe_sync.c) to write a fence completion value
to a userspace VA. AMDGPU will use the same pattern to signal a
per-queue eventfd from a user-queue EOP fence callback.
The helper provides:
- struct drm_user_fence: embeddable base structure
- struct drm_user_fence_ops: worker/destroy callbacks
- drm_user_fence_init(): initialize and grab the process MM
- drm_user_fence_get/put(): reference counting
- drm_user_fence_add_callback(): attach to a dma-fence
The worker callback receives a bool indicating whether the process
MM was successfully obtained, allowing drivers to handle the
unavailable-MM case (log, skip the userspace write, etc.) without
duplicating the mmget/kthread_use_mm/mmput boilerplate.
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
Change-Id: I09da42c688392326ed78235b302fba893e570eff
---
drivers/gpu/drm/Makefile | 1 +
drivers/gpu/drm/drm_user_fence.c | 70 ++++++++++++++++++
include/drm/drm_user_fence.h | 122 +++++++++++++++++++++++++++++++
3 files changed, 193 insertions(+)
create mode 100644 drivers/gpu/drm/drm_user_fence.c
create mode 100644 include/drm/drm_user_fence.h
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index c5be8e80d0c8..ddb770738992 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -69,6 +69,7 @@ drm-y := \
drm_syncobj.o \
drm_sysfs.o \
drm_trace_points.o \
+ drm_user_fence.o \
drm_vblank.o \
drm_vblank_work.o \
drm_vma_manager.o \
diff --git a/drivers/gpu/drm/drm_user_fence.c b/drivers/gpu/drm/drm_user_fence.c
new file mode 100644
index 000000000000..0f229b7210a9
--- /dev/null
+++ b/drivers/gpu/drm/drm_user_fence.c
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 The Linux Foundation
+ *
+ * DRM user fence — extends drm_work_fence with kthread_use_mm() support.
+ *
+ * Use this when a GPU fence signals and work needs to access userspace
+ * memory (copy_to_user, fault-able operations) from a kthread context.
+ * For work that does not require userspace memory access, use
+ * drm_work_fence directly.
+ */
+
+#include <linux/kthread.h>
+#include <linux/sched/mm.h>
+
+#include <drm/drm_user_fence.h>
+
+static void drm_user_fence_do_destroy(struct drm_work_fence *wfence)
+{
+ struct drm_user_fence *ufence =
+ container_of(wfence, struct drm_user_fence, base);
+ struct mm_struct *mm = ufence->mm;
+
+ ufence->ops->destroy(ufence);
+ mmdrop(mm);
+}
+
+static void drm_user_fence_do_work(struct drm_work_fence *wfence)
+{
+ struct drm_user_fence *ufence =
+ container_of(wfence, struct drm_user_fence, base);
+ struct mm_struct *mm = NULL;
+
+ if (mmget_not_zero(ufence->mm)) {
+ mm = ufence->mm;
+ kthread_use_mm(mm);
+ }
+
+ ufence->ops->worker(ufence, !!mm);
+
+ if (mm) {
+ kthread_unuse_mm(mm);
+ mmput_async(mm);
+ }
+}
+
+static const struct drm_work_fence_ops drm_user_fence_wfence_ops = {
+ .writeback = drm_user_fence_do_work,
+ .destroy = drm_user_fence_do_destroy,
+};
+
+/**
+ * drm_user_fence_init - Initialize a user fence
+ * @ufence: user fence to initialize
+ * @wq: workqueue on which to run the worker
+ * @ops: driver operations
+ *
+ * Must be called from process context with a valid current->mm.
+ * Grabs a reference to current->mm via mmgrab().
+ */
+void drm_user_fence_init(struct drm_user_fence *ufence,
+ struct workqueue_struct *wq,
+ const struct drm_user_fence_ops *ops)
+{
+ drm_work_fence_init(&ufence->base, wq, &drm_user_fence_wfence_ops);
+ ufence->mm = current->mm;
+ mmgrab(ufence->mm);
+ ufence->ops = ops;
+}
+EXPORT_SYMBOL_GPL(drm_user_fence_init);
diff --git a/include/drm/drm_user_fence.h b/include/drm/drm_user_fence.h
new file mode 100644
index 000000000000..d35438eaa9e2
--- /dev/null
+++ b/include/drm/drm_user_fence.h
@@ -0,0 +1,122 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 The Linux Foundation
+ */
+
+#ifndef __DRM_USER_FENCE_H__
+#define __DRM_USER_FENCE_H__
+
+#include <linux/dma-fence.h>
+
+#include <drm/drm_work_fence.h>
+
+struct drm_user_fence;
+
+/**
+ * struct drm_user_fence_ops - driver callbacks for a DRM user fence
+ */
+struct drm_user_fence_ops {
+ /**
+ * @worker: Called from workqueue context with the process MM active.
+ *
+ * If @mm_ok is true, kthread_use_mm() is active and userspace memory
+ * (copy_to_user, etc.) may be accessed safely.
+ * If @mm_ok is false, the process MM was already gone; skip the
+ * userspace write.
+ *
+ * wake_up() or other post-signal housekeeping should also happen here.
+ *
+ * WARNING: Fault-able operations such as copy_to_user() may block
+ * indefinitely if userspace registers the target address with
+ * userfaultfd or backs it with a FUSE mount. Drivers that cannot
+ * tolerate blocking should use copy_to_user_nofault() instead.
+ */
+ void (*worker)(struct drm_user_fence *ufence, bool mm_ok);
+
+ /**
+ * @destroy: Called when the last reference is dropped.
+ * Free the containing structure here.
+ */
+ void (*destroy)(struct drm_user_fence *ufence);
+};
+
+/**
+ * struct drm_user_fence - DRM user fence with MM borrowing
+ *
+ * Extends drm_work_fence with kthread_use_mm() support for drivers
+ * that need to access userspace memory when a GPU fence signals.
+ *
+ * Call drm_user_fence_init() at creation and drm_user_fence_add_callback()
+ * to arm on a dma-fence. Call drm_user_fence_cancel_sync() before teardown.
+ */
+struct drm_user_fence {
+ /** @base: Base work fence. Must be first. */
+ struct drm_work_fence base;
+ /** @mm: Process MM grabbed at init time. */
+ struct mm_struct *mm;
+ /** @ops: Driver operations. */
+ const struct drm_user_fence_ops *ops;
+};
+
+void drm_user_fence_init(struct drm_user_fence *ufence,
+ struct workqueue_struct *wq,
+ const struct drm_user_fence_ops *ops);
+
+/**
+ * drm_user_fence_get - Acquire a reference to a user fence
+ * @ufence: user fence
+ */
+static inline void drm_user_fence_get(struct drm_user_fence *ufence)
+{
+ drm_work_fence_get(&ufence->base);
+}
+
+/**
+ * drm_user_fence_put - Release a reference to a user fence
+ * @ufence: user fence
+ */
+static inline void drm_user_fence_put(struct drm_user_fence *ufence)
+{
+ drm_work_fence_put(&ufence->base);
+}
+
+/**
+ * drm_user_fence_add_callback - Attach a user fence to a dma-fence
+ * @ufence: user fence; caller retains their reference and must release
+ * it via drm_user_fence_put() when no longer needed
+ * @fence: dma-fence to watch; one reference is consumed on any return value
+ *
+ * When @fence signals, ops->worker() is called from workqueue context.
+ * If @fence has already signaled, the worker is queued immediately.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+static inline int drm_user_fence_add_callback(struct drm_user_fence *ufence,
+ struct dma_fence *fence)
+{
+ return drm_work_fence_add_callback(&ufence->base, fence);
+}
+
+/**
+ * drm_user_fence_cancel - Cancel a pending user fence callback
+ * @ufence: user fence
+ *
+ * Return: true if callback was removed, false if it had already fired.
+ */
+static inline bool drm_user_fence_cancel(struct drm_user_fence *ufence)
+{
+ return drm_work_fence_cancel(&ufence->base);
+}
+
+/**
+ * drm_user_fence_cancel_sync - Cancel callback and wait for worker to finish
+ * @ufence: user fence
+ *
+ * Must be called during teardown before freeing resources. May sleep.
+ */
+static inline void drm_user_fence_cancel_sync(struct drm_user_fence *ufence)
+{
+ drm_work_fence_cancel_sync(&ufence->base);
+}
+
+#endif /* __DRM_USER_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH 3/4] drm/xe: Convert xe_user_fence to drm_user_fence
2026-09-03 5:29 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-09-03 5:29 ` [PATCH 2/4] drm: Add drm_user_fence helper Srinivasan Shanmugam
@ 2026-09-03 5:29 ` Srinivasan Shanmugam
2026-09-03 5:29 ` [PATCH 4/4] drm: Add per-signal compare functionality " Srinivasan Shanmugam
2 siblings, 0 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-03 5:29 UTC (permalink / raw)
To: linux-kernel
Replace the open-coded user fence implementation in xe_sync.c with the
new common drm_user_fence helper.
struct xe_user_fence now embeds struct drm_user_fence as its base.
XE-specific fields (xe_device pointer for the ufence_wq wake-up,
userspace VA, expected value, signalled flag) remain in the wrapper.
The local user_fence_destroy/get/put/worker/kick_ufence/user_fence_cb
functions are removed. Their logic moves to xe_ufence_ops.worker and
xe_ufence_ops.destroy, which are called by the drm_user_fence helper.
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Mika Kuoppala <mika.kuoppala@linux.intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
---
drivers/gpu/drm/xe/xe_sync.c | 147 ++++++++++++++++-------------
drivers/gpu/drm/xe/xe_sync.h | 2 +
drivers/gpu/drm/xe/xe_sync_types.h | 1 -
drivers/gpu/drm/xe/xe_vm.c | 1 +
4 files changed, 82 insertions(+), 69 deletions(-)
diff --git a/drivers/gpu/drm/xe/xe_sync.c b/drivers/gpu/drm/xe/xe_sync.c
index 37866768d64c..a67ace940931 100644
--- a/drivers/gpu/drm/xe/xe_sync.c
+++ b/drivers/gpu/drm/xe/xe_sync.c
@@ -6,12 +6,11 @@
#include "xe_sync.h"
#include <linux/dma-fence-array.h>
-#include <linux/kthread.h>
-#include <linux/sched/mm.h>
#include <linux/uaccess.h>
#include <drm/drm_print.h>
#include <drm/drm_syncobj.h>
+#include <drm/drm_user_fence.h>
#include <uapi/drm/xe_drm.h>
#include "xe_device.h"
@@ -19,36 +18,58 @@
#include "xe_macros.h"
#include "xe_sched_job_types.h"
+/*
+ * xe_user_fence wraps drm_user_fence with XE-specific fields.
+ * The drm_user_fence base handles MM borrowing and work-item lifetime.
+ */
struct xe_user_fence {
- struct xe_device *xe;
- struct kref refcount;
- struct dma_fence_cb cb;
- struct work_struct worker;
- struct mm_struct *mm;
- u64 __user *addr;
- u64 value;
- int signalled;
+ struct drm_user_fence base;
+ struct xe_device *xe;
+ u64 __user *addr;
+ u64 value;
+ int signalled;
};
-static void user_fence_destroy(struct kref *kref)
+static void xe_ufence_worker(struct drm_user_fence *base, bool mm_ok)
{
- struct xe_user_fence *ufence = container_of(kref, struct xe_user_fence,
- refcount);
+ struct xe_user_fence *ufence = container_of(base, struct xe_user_fence, base);
- mmdrop(ufence->mm);
- kfree(ufence);
-}
+ /*
+ * Mark signalled before the userspace write so that if userspace
+ * observes the written value and immediately issues a VM unmap,
+ * check_ufence() will see signalled=1 and not return -EBUSY.
+ *
+ * NOTE: copy_to_user() may block if userspace backs the address with
+ * userfaultfd or FUSE. Drivers requiring non-blocking semantics should
+ * use a dedicated workqueue instead of xe->ordered_wq.
+ */
+ WRITE_ONCE(ufence->signalled, 1);
-static void user_fence_get(struct xe_user_fence *ufence)
-{
- kref_get(&ufence->refcount);
+ if (mm_ok) {
+ if (copy_to_user(ufence->addr, &ufence->value,
+ sizeof(ufence->value)))
+ drm_dbg(&ufence->xe->drm,
+ "copy_to_user failed, user fence wasn't signaled\n");
+ } else {
+ drm_dbg(&ufence->xe->drm,
+ "mmget_not_zero() failed, ufence wasn't signaled\n");
+ }
+
+ wake_up_all(&ufence->xe->ufence_wq);
}
-static void user_fence_put(struct xe_user_fence *ufence)
+static void xe_ufence_destroy(struct drm_user_fence *base)
{
- kref_put(&ufence->refcount, user_fence_destroy);
+ struct xe_user_fence *ufence = container_of(base, struct xe_user_fence, base);
+
+ kfree(ufence);
}
+static const struct drm_user_fence_ops xe_ufence_ops = {
+ .worker = xe_ufence_worker,
+ .destroy = xe_ufence_destroy,
+};
+
static struct xe_user_fence *user_fence_create(struct xe_device *xe, u64 addr,
u64 value)
{
@@ -63,51 +84,22 @@ static struct xe_user_fence *user_fence_create(struct xe_device *xe, u64 addr,
if (!ufence)
return ERR_PTR(-ENOMEM);
- ufence->xe = xe;
- kref_init(&ufence->refcount);
- ufence->addr = ptr;
+ ufence->xe = xe;
+ ufence->addr = ptr;
ufence->value = value;
- ufence->mm = current->mm;
- mmgrab(ufence->mm);
+ drm_user_fence_init(&ufence->base, xe->ordered_wq, &xe_ufence_ops);
return ufence;
}
-static void user_fence_worker(struct work_struct *w)
-{
- struct xe_user_fence *ufence = container_of(w, struct xe_user_fence, worker);
-
- WRITE_ONCE(ufence->signalled, 1);
- if (mmget_not_zero(ufence->mm)) {
- kthread_use_mm(ufence->mm);
- if (copy_to_user(ufence->addr, &ufence->value, sizeof(ufence->value)))
- XE_WARN_ON("Copy to user failed");
- kthread_unuse_mm(ufence->mm);
- mmput(ufence->mm);
- } else {
- drm_dbg(&ufence->xe->drm, "mmget_not_zero() failed, ufence wasn't signaled\n");
- }
-
- /*
- * Wake up waiters only after updating the ufence state, allowing the UMD
- * to safely reuse the same ufence without encountering -EBUSY errors.
- */
- wake_up_all(&ufence->xe->ufence_wq);
- user_fence_put(ufence);
-}
-
-static void kick_ufence(struct xe_user_fence *ufence, struct dma_fence *fence)
+static void user_fence_get(struct xe_user_fence *ufence)
{
- INIT_WORK(&ufence->worker, user_fence_worker);
- queue_work(ufence->xe->ordered_wq, &ufence->worker);
- dma_fence_put(fence);
+ drm_user_fence_get(&ufence->base);
}
-static void user_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
+static void user_fence_put(struct xe_user_fence *ufence)
{
- struct xe_user_fence *ufence = container_of(cb, struct xe_user_fence, cb);
-
- kick_ufence(ufence, fence);
+ drm_user_fence_put(&ufence->base);
}
int xe_sync_entry_parse(struct xe_device *xe, struct xe_file *xef,
@@ -282,24 +274,15 @@ void xe_sync_entry_signal(struct xe_sync_entry *sync, struct dma_fence *fence)
} else if (sync->syncobj) {
drm_syncobj_replace_fence(sync->syncobj, fence);
} else if (sync->ufence) {
- int err;
-
drm_syncobj_add_point(sync->ufence_syncobj,
sync->ufence_chain_fence,
fence, sync->ufence_timeline_value);
sync->ufence_chain_fence = NULL;
fence = drm_syncobj_fence_get(sync->ufence_syncobj);
- user_fence_get(sync->ufence);
- err = dma_fence_add_callback(fence, &sync->ufence->cb,
- user_fence_cb);
- if (err == -ENOENT) {
- kick_ufence(sync->ufence, fence);
- } else if (err) {
+ if (drm_user_fence_add_callback(&sync->ufence->base, fence))
XE_WARN_ON("failed to add user fence");
- user_fence_put(sync->ufence);
- dma_fence_put(fence);
- }
+ /* fence ref consumed by drm_user_fence_add_callback */
}
}
@@ -434,6 +417,34 @@ void xe_sync_ufence_put(struct xe_user_fence *ufence)
user_fence_put(ufence);
}
+/**
+ * xe_sync_ufence_cancel() - Non-blocking cancel of user fence callback
+ * @ufence: user fence reference
+ *
+ * Attempts to cancel the pending callback without waiting for the worker.
+ * Safe to call while holding dma_resv_lock or vm->lock. If the callback
+ * has already fired, the worker runs independently — xe_ufence_worker
+ * only accesses device-level and userspace resources, both safe after
+ * VMA teardown.
+ */
+void xe_sync_ufence_cancel(struct xe_user_fence *ufence)
+{
+ drm_user_fence_cancel(&ufence->base);
+}
+
+/**
+ * xe_sync_ufence_cancel_sync() - Cancel user fence callback and wait for worker
+ * @ufence: user fence reference
+ *
+ * Cancels any pending dma-fence callback and waits for the worker to fully
+ * complete before returning. Must be called during teardown before freeing
+ * any resources accessed by the worker.
+ */
+void xe_sync_ufence_cancel_sync(struct xe_user_fence *ufence)
+{
+ drm_user_fence_cancel_sync(&ufence->base);
+}
+
/**
* xe_sync_ufence_get_status() - Get user fence status
* @ufence: user fence
diff --git a/drivers/gpu/drm/xe/xe_sync.h b/drivers/gpu/drm/xe/xe_sync.h
index 6b949194acff..b5ff3387c592 100644
--- a/drivers/gpu/drm/xe/xe_sync.h
+++ b/drivers/gpu/drm/xe/xe_sync.h
@@ -44,6 +44,8 @@ static inline bool xe_sync_is_ufence(struct xe_sync_entry *sync)
struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence);
struct xe_user_fence *xe_sync_ufence_get(struct xe_sync_entry *sync);
void xe_sync_ufence_put(struct xe_user_fence *ufence);
+void xe_sync_ufence_cancel(struct xe_user_fence *ufence);
+void xe_sync_ufence_cancel_sync(struct xe_user_fence *ufence);
int xe_sync_ufence_get_status(struct xe_user_fence *ufence);
#endif
diff --git a/drivers/gpu/drm/xe/xe_sync_types.h b/drivers/gpu/drm/xe/xe_sync_types.h
index b88f1833e28c..67048199b070 100644
--- a/drivers/gpu/drm/xe/xe_sync_types.h
+++ b/drivers/gpu/drm/xe/xe_sync_types.h
@@ -12,7 +12,6 @@ struct drm_syncobj;
struct dma_fence;
struct dma_fence_chain;
struct drm_xe_sync;
-struct user_fence;
struct xe_sync_entry {
struct drm_syncobj *syncobj;
diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c
index b01f31ed4417..fbd2c28de8a6 100644
--- a/drivers/gpu/drm/xe/xe_vm.c
+++ b/drivers/gpu/drm/xe/xe_vm.c
@@ -1181,6 +1181,7 @@ static void xe_vma_destroy_late(struct xe_vma *vma)
struct xe_bo *bo = xe_vma_bo(vma);
if (vma->ufence) {
+ xe_sync_ufence_cancel(vma->ufence);
xe_sync_ufence_put(vma->ufence);
vma->ufence = NULL;
}
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence
2026-09-03 5:29 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-09-03 5:29 ` [PATCH 2/4] drm: Add drm_user_fence helper Srinivasan Shanmugam
2026-09-03 5:29 ` [PATCH 3/4] drm/xe: Convert xe_user_fence to drm_user_fence Srinivasan Shanmugam
@ 2026-09-03 5:29 ` Srinivasan Shanmugam
2 siblings, 0 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-03 5:29 UTC (permalink / raw)
To: linux-kernel
GPU drivers sometimes need to read a value from a userspace VA when a
dma-fence signals, compare it with an expected value, and only perform
the deferred work (e.g. eventfd_signal) if the comparison passes. This
is the per-signal filtering pattern used in AMDGPU's EOP eventfd path.
Add optional compare fields to drm_user_fence and a new helper
drm_user_fence_set_compare() to configure them. Supported operators are
==, !=, >=.
When cmp_op is set, drm_user_fence reads the value from userspace via
copy_from_user_nofault() and calls ops->worker() with mm_ok=true only if
the comparison passes. If the process MM is gone or the read fails, the
worker is called with mm_ok=false to allow mandatory housekeeping (e.g.
wake_up()).
Drivers that do not need filtering (e.g. XE) leave cmp_op unset and the
worker is called unconditionally — no behavioral change.
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: dri-devel@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
Change-Id: I200dd4bf32286ba9bdd2ecb68fffb3c205f9fd52
---
drivers/gpu/drm/drm_user_fence.c | 104 ++++++++++++++++++++++++++++---
include/drm/drm_user_fence.h | 47 +++++++++++++-
2 files changed, 142 insertions(+), 9 deletions(-)
diff --git a/drivers/gpu/drm/drm_user_fence.c b/drivers/gpu/drm/drm_user_fence.c
index 0f229b7210a9..6dc57f556aba 100644
--- a/drivers/gpu/drm/drm_user_fence.c
+++ b/drivers/gpu/drm/drm_user_fence.c
@@ -2,16 +2,25 @@
/*
* Copyright © 2024 The Linux Foundation
*
- * DRM user fence — extends drm_work_fence with kthread_use_mm() support.
+ * DRM user fence helper.
*
- * Use this when a GPU fence signals and work needs to access userspace
- * memory (copy_to_user, fault-able operations) from a kthread context.
- * For work that does not require userspace memory access, use
- * drm_work_fence directly.
+ * Extends drm_work_fence with the ability to access userspace memory
+ * from workqueue context by borrowing the process MM via kthread_use_mm().
+ *
+ * Drivers that need to write completion status to userspace (e.g., user
+ * fences, signaling eventfds) embed drm_user_fence and implement
+ * ops->worker() to do the actual write.
+ *
+ * Optionally, drivers may configure a per-signal compare via
+ * drm_user_fence_set_compare(): work is skipped unless the value at a
+ * userspace address matches the expected value at signal time.
*/
#include <linux/kthread.h>
+#include <linux/mm.h>
#include <linux/sched/mm.h>
+#include <linux/uaccess.h>
+#include <linux/workqueue.h>
#include <drm/drm_user_fence.h>
@@ -30,13 +39,50 @@ static void drm_user_fence_do_work(struct drm_work_fence *wfence)
struct drm_user_fence *ufence =
container_of(wfence, struct drm_user_fence, base);
struct mm_struct *mm = NULL;
+ bool call_worker = true;
if (mmget_not_zero(ufence->mm)) {
mm = ufence->mm;
kthread_use_mm(mm);
}
- ufence->ops->worker(ufence, !!mm);
+ if (ufence->cmp_op != DRM_USER_FENCE_CMP_NONE &&
+ !(wfence->fence && wfence->fence->error)) {
+ if (!mm) {
+ call_worker = false;
+ } else {
+ __le64 raw;
+
+ /*
+ * Use copy_from_user_nofault() to prevent a
+ * userfaultfd-registered page from blocking this
+ * workqueue thread indefinitely (DoS).
+ *
+ * If the page is not resident, skip the worker to
+ * preserve compare filter semantics — the caller must
+ * ensure the compare address remains resident.
+ */
+ if (copy_from_user_nofault(&raw, ufence->cmp_addr,
+ sizeof(raw))) {
+ call_worker = false;
+ } else {
+ /* GPU writes LE; convert before comparing. */
+ u64 cur_val = le64_to_cpu(raw);
+
+ if (!drm_user_fence_cmp_match(cur_val,
+ ufence->cmp_value,
+ ufence->cmp_op))
+ call_worker = false;
+ }
+ }
+ }
+
+ /*
+ * Always invoke the worker so drivers can perform mandatory
+ * housekeeping (e.g. wake_up()). Pass false if the compare
+ * filter suppressed the write.
+ */
+ ufence->ops->worker(ufence, call_worker ? !!mm : false);
if (mm) {
kthread_unuse_mm(mm);
@@ -63,8 +109,50 @@ void drm_user_fence_init(struct drm_user_fence *ufence,
const struct drm_user_fence_ops *ops)
{
drm_work_fence_init(&ufence->base, wq, &drm_user_fence_wfence_ops);
- ufence->mm = current->mm;
+ ufence->mm = current->mm;
mmgrab(ufence->mm);
- ufence->ops = ops;
+ ufence->ops = ops;
+ ufence->cmp_op = DRM_USER_FENCE_CMP_NONE;
+ ufence->cmp_addr = NULL;
+ ufence->cmp_value = 0;
}
EXPORT_SYMBOL_GPL(drm_user_fence_init);
+
+/**
+ * drm_user_fence_set_compare - Set per-signal compare filter
+ * @ufence: user fence
+ * @addr: 8-byte-aligned userspace address to read from at signal time
+ * @value: expected value to compare against
+ * @op: comparison operator; pass %DRM_USER_FENCE_CMP_NONE to disable
+ *
+ * When @op is not %DRM_USER_FENCE_CMP_NONE, the worker is only called
+ * with mm_ok=true if the value at @addr matches @value according to @op.
+ * The worker is always called for mandatory housekeeping.
+ * If the MM is gone or the read fails, mm_ok is passed as false.
+ *
+ * Must only be called before drm_work_fence_add_callback().
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op)
+{
+ /*
+ * get_user() of u64 is not atomic on 32-bit — caller should not
+ * reach here on non-64-bit kernels.
+ */
+ if (!IS_ENABLED(CONFIG_64BIT))
+ return -EOPNOTSUPP;
+
+ if (op != DRM_USER_FENCE_CMP_NONE) {
+ if (!addr || !IS_ALIGNED((unsigned long)addr, sizeof(u64)))
+ return -EINVAL;
+ }
+
+ ufence->cmp_addr = addr;
+ ufence->cmp_value = value;
+ ufence->cmp_op = op;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(drm_user_fence_set_compare);
diff --git a/include/drm/drm_user_fence.h b/include/drm/drm_user_fence.h
index d35438eaa9e2..94bc2fd59d73 100644
--- a/include/drm/drm_user_fence.h
+++ b/include/drm/drm_user_fence.h
@@ -40,11 +40,23 @@ struct drm_user_fence_ops {
void (*destroy)(struct drm_user_fence *ufence);
};
+/**
+ * enum drm_user_fence_cmp - compare operator for per-signal filtering
+ */
+enum drm_user_fence_cmp {
+ DRM_USER_FENCE_CMP_NONE = 0,
+ DRM_USER_FENCE_CMP_EQ,
+ DRM_USER_FENCE_CMP_NEQ,
+ DRM_USER_FENCE_CMP_GTE,
+};
+
/**
* struct drm_user_fence - DRM user fence with MM borrowing
*
* Extends drm_work_fence with kthread_use_mm() support for drivers
* that need to access userspace memory when a GPU fence signals.
+ * For work that does not need userspace memory access, use
+ * drm_work_fence directly.
*
* Call drm_user_fence_init() at creation and drm_user_fence_add_callback()
* to arm on a dma-fence. Call drm_user_fence_cancel_sync() before teardown.
@@ -56,11 +68,20 @@ struct drm_user_fence {
struct mm_struct *mm;
/** @ops: Driver operations. */
const struct drm_user_fence_ops *ops;
+ /** @cmp_addr: Userspace address to read for per-signal compare. */
+ u64 __user *cmp_addr;
+ /** @cmp_value: Expected value for per-signal compare. */
+ u64 cmp_value;
+ /** @cmp_op: Compare operator; DRM_USER_FENCE_CMP_NONE disables. */
+ enum drm_user_fence_cmp cmp_op;
};
void drm_user_fence_init(struct drm_user_fence *ufence,
struct workqueue_struct *wq,
const struct drm_user_fence_ops *ops);
+int drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op);
/**
* drm_user_fence_get - Acquire a reference to a user fence
@@ -112,11 +133,35 @@ static inline bool drm_user_fence_cancel(struct drm_user_fence *ufence)
* drm_user_fence_cancel_sync - Cancel callback and wait for worker to finish
* @ufence: user fence
*
- * Must be called during teardown before freeing resources. May sleep.
+ * Must be called during teardown before freeing any resources accessed
+ * by ops->worker(). May sleep.
*/
static inline void drm_user_fence_cancel_sync(struct drm_user_fence *ufence)
{
drm_work_fence_cancel_sync(&ufence->base);
}
+/**
+ * drm_user_fence_cmp_match - Test a value against the compare filter
+ * @cur_val: value read from userspace (already converted from LE)
+ * @cmp_value: expected value
+ * @op: comparison operator
+ *
+ * Return: true if the comparison passes, false otherwise.
+ */
+static inline bool drm_user_fence_cmp_match(u64 cur_val, u64 cmp_value,
+ enum drm_user_fence_cmp op)
+{
+ switch (op) {
+ case DRM_USER_FENCE_CMP_EQ:
+ return cur_val == cmp_value;
+ case DRM_USER_FENCE_CMP_NEQ:
+ return cur_val != cmp_value;
+ case DRM_USER_FENCE_CMP_GTE:
+ return cur_val >= cmp_value;
+ default:
+ return false;
+ }
+}
+
#endif /* __DRM_USER_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 1/4] drm: Add drm_work_fence helper
@ 2026-09-03 5:32 Srinivasan Shanmugam
2026-09-03 5:32 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
0 siblings, 1 reply; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-03 5:32 UTC (permalink / raw)
To: linux-kernel
GPU drivers often need to queue work when a dma-fence signals
because certain operations (copy_to_user, eventfd_signal, memory
allocation) cannot run in IRQ context. This pattern is currently
open-coded in multiple drivers.
Introduce drm_work_fence — an embeddable base structure that handles the
dma-fence-callback-to-workqueue pattern in one place. Drivers embed this
in their own structure and implement ops->writeback() for the deferred
work and ops->destroy() for cleanup.
The helper manages:
- kref lifetime
- dma-fence callback registration
- workqueue dispatch on fence signal
- safe cancellation before driver teardown
For work that additionally requires borrowing the process MM via
kthread_use_mm(), see drm_user_fence which builds on top of this.
Suggested-by: Matthew Brost <matthew.brost@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
---
drivers/gpu/drm/Makefile | 1 +
drivers/gpu/drm/drm_work_fence.c | 184 +++++++++++++++++++++++++++++++
include/drm/drm_work_fence.h | 69 ++++++++++++
3 files changed, 254 insertions(+)
create mode 100644 drivers/gpu/drm/drm_work_fence.c
create mode 100644 include/drm/drm_work_fence.h
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index e97faabcd783..c5be8e80d0c8 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -72,6 +72,7 @@ drm-y := \
drm_vblank.o \
drm_vblank_work.o \
drm_vma_manager.o \
+ drm_work_fence.o \
drm_writeback.o
drm-$(CONFIG_DRM_CLIENT) += \
drm_client.o \
diff --git a/drivers/gpu/drm/drm_work_fence.c b/drivers/gpu/drm/drm_work_fence.c
new file mode 100644
index 000000000000..482eb3d4475f
--- /dev/null
+++ b/drivers/gpu/drm/drm_work_fence.c
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 The Linux Foundation
+ *
+ * Common DRM work fence helper.
+ *
+ * When a GPU dma-fence signals, drivers often need to perform work that
+ * cannot run in IRQ context (e.g., memory allocation, copy_to_user,
+ * eventfd_signal). This helper queues a work item when a dma-fence
+ * signals, allowing that work to run safely in a workqueue context.
+ *
+ * NOTE: This helper consumes dma_fences but CANNOT implement
+ * dma_fence_ops. Work items queued here may sleep; dma_fence_ops
+ * callbacks are called under the fence spinlock and must not sleep.
+ *
+ * For work that additionally requires accessing userspace memory via
+ * kthread_use_mm(), see drm_user_fence which builds on top of this.
+ */
+
+#include <linux/workqueue.h>
+
+#include <drm/drm_work_fence.h>
+
+static void drm_work_fence_destroy(struct kref *kref)
+{
+ struct drm_work_fence *wfence =
+ container_of(kref, struct drm_work_fence, refcount);
+ struct dma_fence *fence = wfence->fence;
+
+ wfence->ops->destroy(wfence);
+ dma_fence_put(fence); /* NULL-safe */
+}
+
+/**
+ * drm_work_fence_get - Acquire a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_get(struct drm_work_fence *wfence)
+{
+ kref_get(&wfence->refcount);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_get);
+
+/**
+ * drm_work_fence_put - Release a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_put(struct drm_work_fence *wfence)
+{
+ kref_put(&wfence->refcount, drm_work_fence_destroy);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_put);
+
+static void drm_work_fence_work(struct work_struct *w)
+{
+ struct drm_work_fence *wfence =
+ container_of(w, struct drm_work_fence, work);
+
+ wfence->ops->writeback(wfence);
+ drm_work_fence_put(wfence);
+}
+
+static void drm_work_fence_queue(struct drm_work_fence *wfence)
+{
+ queue_work(wfence->wq, &wfence->work);
+}
+
+static void drm_work_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
+{
+ struct drm_work_fence *wfence =
+ container_of(cb, struct drm_work_fence, cb);
+
+ drm_work_fence_queue(wfence);
+ /* Single ref: wfence->fence released in drm_work_fence_destroy(). */
+}
+
+/**
+ * drm_work_fence_init - Initialize a work fence
+ * @wfence: work fence to initialize
+ * @wq: workqueue to run the worker on (must be ordered if sequencing matters)
+ * @ops: driver operations
+ */
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops)
+{
+ kref_init(&wfence->refcount);
+ wfence->wq = wq;
+ wfence->ops = ops;
+ wfence->fence = NULL;
+ INIT_WORK(&wfence->work, drm_work_fence_work);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_init);
+
+/**
+ * drm_work_fence_add_callback - Attach a work fence to a dma-fence
+ * @wfence: work fence; caller retains their reference and must release
+ * it via drm_work_fence_put() when no longer needed
+ * @fence: dma-fence to watch; one reference is consumed on any return value
+ *
+ * When @fence signals, a work item is queued that calls ops->writeback().
+ * If @fence has already signaled, the work item is queued immediately.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence)
+{
+ int err;
+
+ drm_work_fence_get(wfence);
+ wfence->fence = fence; /* transfer caller's ref — single ref, no get */
+
+ err = dma_fence_add_callback(fence, &wfence->cb, drm_work_fence_cb);
+ if (err == -ENOENT) {
+ drm_work_fence_queue(wfence);
+ err = 0;
+ } else if (err) {
+ wfence->fence = NULL;
+ dma_fence_put(fence);
+ drm_work_fence_put(wfence);
+ }
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_add_callback);
+
+/**
+ * drm_work_fence_cancel - Cancel a pending work fence callback
+ * @wfence: work fence
+ *
+ * Attempts to remove the pending callback before driver context teardown.
+ * The caller must hold a reference to @wfence across this call.
+ *
+ * If the callback has already fired this returns false and all cleanup
+ * has been handled internally.
+ *
+ * If removal succeeds the callback reference is released internally.
+ * The caller must still release its own reference via drm_work_fence_put().
+ *
+ * This function is safe to call from atomic context as it only acquires
+ * the dma-fence spinlock internally. If the caller also needs to wait
+ * for the worker to finish, use drm_work_fence_cancel_sync() instead,
+ * which may sleep.
+ *
+ * Return: true if callback was removed, false if it had already fired.
+ */
+bool drm_work_fence_cancel(struct drm_work_fence *wfence)
+{
+ struct dma_fence *fence = wfence->fence;
+
+ if (!fence)
+ return false;
+
+ if (dma_fence_remove_callback(fence, &wfence->cb)) {
+ drm_work_fence_put(wfence); /* drop ref from add_callback */
+ return true;
+ }
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel);
+
+/**
+ * drm_work_fence_cancel_sync - Cancel callback and wait for worker to finish
+ * @wfence: work fence
+ *
+ * Calls drm_work_fence_cancel() then cancel_work_sync() to guarantee
+ * the worker has fully completed before returning.
+ *
+ * This function may sleep. Must not be called from atomic or interrupt
+ * context. Use drm_work_fence_cancel() instead when sleeping is not allowed.
+ *
+ * Drivers must call this during teardown before freeing any resources
+ * accessed by ops->writeback().
+ */
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence)
+{
+ if (drm_work_fence_cancel(wfence))
+ return;
+ if (cancel_work_sync(&wfence->work))
+ drm_work_fence_put(wfence);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel_sync);
diff --git a/include/drm/drm_work_fence.h b/include/drm/drm_work_fence.h
new file mode 100644
index 000000000000..c8e3c5b9f0c4
--- /dev/null
+++ b/include/drm/drm_work_fence.h
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 The Linux Foundation
+ */
+
+#ifndef __DRM_WORK_FENCE_H__
+#define __DRM_WORK_FENCE_H__
+
+#include <linux/dma-fence.h>
+#include <linux/kref.h>
+#include <linux/workqueue.h>
+
+struct drm_work_fence;
+
+/**
+ * struct drm_work_fence_ops - driver callbacks for a DRM work fence
+ */
+struct drm_work_fence_ops {
+ /**
+ * @writeback: Called from workqueue context when the dma-fence signals.
+ *
+ * Perform the deferred work here (copy_to_user, eventfd_signal, etc.).
+ * May sleep. Must not requeue the fence.
+ */
+ void (*writeback)(struct drm_work_fence *wfence);
+
+ /**
+ * @destroy: Called when the last reference is dropped.
+ * Free the containing structure here.
+ */
+ void (*destroy)(struct drm_work_fence *wfence);
+};
+
+/**
+ * struct drm_work_fence - DRM dma-fence-to-workqueue helper
+ *
+ * Embeddable base structure that queues a work item when a dma-fence signals.
+ * Drivers embed this in their own structure and implement ops->writeback()
+ * for the deferred work and ops->destroy() for cleanup.
+ *
+ * Call drm_work_fence_init() at creation and drm_work_fence_add_callback()
+ * to arm on a dma-fence. Call drm_work_fence_cancel_sync() before teardown.
+ */
+struct drm_work_fence {
+ /** @refcount: Reference count. */
+ struct kref refcount;
+ /** @wq: Workqueue on which to run the worker. */
+ struct workqueue_struct *wq;
+ /** @ops: Driver operations. */
+ const struct drm_work_fence_ops *ops;
+ /** @fence: The watched dma-fence; holds a single reference. */
+ struct dma_fence *fence;
+ /** @work: Work item queued when the fence signals. */
+ struct work_struct work;
+ /** @cb: Callback registered on the dma-fence. */
+ struct dma_fence_cb cb;
+};
+
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops);
+void drm_work_fence_get(struct drm_work_fence *wfence);
+void drm_work_fence_put(struct drm_work_fence *wfence);
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence);
+bool drm_work_fence_cancel(struct drm_work_fence *wfence);
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence);
+
+#endif /* __DRM_WORK_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence
2026-09-03 5:32 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
@ 2026-09-03 5:32 ` Srinivasan Shanmugam
0 siblings, 0 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-03 5:32 UTC (permalink / raw)
To: linux-kernel
GPU drivers sometimes need to read a value from a userspace VA when a
dma-fence signals, compare it with an expected value, and only perform
the deferred work (e.g. eventfd_signal) if the comparison passes. This
is the per-signal filtering pattern used in AMDGPU's EOP eventfd path.
Add optional compare fields to drm_user_fence and a new helper
drm_user_fence_set_compare() to configure them. Supported operators are
==, !=, >=.
When cmp_op is set, drm_user_fence reads the value from userspace via
copy_from_user_nofault() and calls ops->worker() with mm_ok=true only if
the comparison passes. If the process MM is gone or the read fails, the
worker is called with mm_ok=false to allow mandatory housekeeping (e.g.
wake_up()).
Drivers that do not need filtering (e.g. XE) leave cmp_op unset and the
worker is called unconditionally — no behavioral change.
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: dri-devel@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
Change-Id: I200dd4bf32286ba9bdd2ecb68fffb3c205f9fd52
---
drivers/gpu/drm/drm_user_fence.c | 104 ++++++++++++++++++++++++++++---
include/drm/drm_user_fence.h | 47 +++++++++++++-
2 files changed, 142 insertions(+), 9 deletions(-)
diff --git a/drivers/gpu/drm/drm_user_fence.c b/drivers/gpu/drm/drm_user_fence.c
index 0f229b7210a9..6dc57f556aba 100644
--- a/drivers/gpu/drm/drm_user_fence.c
+++ b/drivers/gpu/drm/drm_user_fence.c
@@ -2,16 +2,25 @@
/*
* Copyright © 2024 The Linux Foundation
*
- * DRM user fence — extends drm_work_fence with kthread_use_mm() support.
+ * DRM user fence helper.
*
- * Use this when a GPU fence signals and work needs to access userspace
- * memory (copy_to_user, fault-able operations) from a kthread context.
- * For work that does not require userspace memory access, use
- * drm_work_fence directly.
+ * Extends drm_work_fence with the ability to access userspace memory
+ * from workqueue context by borrowing the process MM via kthread_use_mm().
+ *
+ * Drivers that need to write completion status to userspace (e.g., user
+ * fences, signaling eventfds) embed drm_user_fence and implement
+ * ops->worker() to do the actual write.
+ *
+ * Optionally, drivers may configure a per-signal compare via
+ * drm_user_fence_set_compare(): work is skipped unless the value at a
+ * userspace address matches the expected value at signal time.
*/
#include <linux/kthread.h>
+#include <linux/mm.h>
#include <linux/sched/mm.h>
+#include <linux/uaccess.h>
+#include <linux/workqueue.h>
#include <drm/drm_user_fence.h>
@@ -30,13 +39,50 @@ static void drm_user_fence_do_work(struct drm_work_fence *wfence)
struct drm_user_fence *ufence =
container_of(wfence, struct drm_user_fence, base);
struct mm_struct *mm = NULL;
+ bool call_worker = true;
if (mmget_not_zero(ufence->mm)) {
mm = ufence->mm;
kthread_use_mm(mm);
}
- ufence->ops->worker(ufence, !!mm);
+ if (ufence->cmp_op != DRM_USER_FENCE_CMP_NONE &&
+ !(wfence->fence && wfence->fence->error)) {
+ if (!mm) {
+ call_worker = false;
+ } else {
+ __le64 raw;
+
+ /*
+ * Use copy_from_user_nofault() to prevent a
+ * userfaultfd-registered page from blocking this
+ * workqueue thread indefinitely (DoS).
+ *
+ * If the page is not resident, skip the worker to
+ * preserve compare filter semantics — the caller must
+ * ensure the compare address remains resident.
+ */
+ if (copy_from_user_nofault(&raw, ufence->cmp_addr,
+ sizeof(raw))) {
+ call_worker = false;
+ } else {
+ /* GPU writes LE; convert before comparing. */
+ u64 cur_val = le64_to_cpu(raw);
+
+ if (!drm_user_fence_cmp_match(cur_val,
+ ufence->cmp_value,
+ ufence->cmp_op))
+ call_worker = false;
+ }
+ }
+ }
+
+ /*
+ * Always invoke the worker so drivers can perform mandatory
+ * housekeeping (e.g. wake_up()). Pass false if the compare
+ * filter suppressed the write.
+ */
+ ufence->ops->worker(ufence, call_worker ? !!mm : false);
if (mm) {
kthread_unuse_mm(mm);
@@ -63,8 +109,50 @@ void drm_user_fence_init(struct drm_user_fence *ufence,
const struct drm_user_fence_ops *ops)
{
drm_work_fence_init(&ufence->base, wq, &drm_user_fence_wfence_ops);
- ufence->mm = current->mm;
+ ufence->mm = current->mm;
mmgrab(ufence->mm);
- ufence->ops = ops;
+ ufence->ops = ops;
+ ufence->cmp_op = DRM_USER_FENCE_CMP_NONE;
+ ufence->cmp_addr = NULL;
+ ufence->cmp_value = 0;
}
EXPORT_SYMBOL_GPL(drm_user_fence_init);
+
+/**
+ * drm_user_fence_set_compare - Set per-signal compare filter
+ * @ufence: user fence
+ * @addr: 8-byte-aligned userspace address to read from at signal time
+ * @value: expected value to compare against
+ * @op: comparison operator; pass %DRM_USER_FENCE_CMP_NONE to disable
+ *
+ * When @op is not %DRM_USER_FENCE_CMP_NONE, the worker is only called
+ * with mm_ok=true if the value at @addr matches @value according to @op.
+ * The worker is always called for mandatory housekeeping.
+ * If the MM is gone or the read fails, mm_ok is passed as false.
+ *
+ * Must only be called before drm_work_fence_add_callback().
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op)
+{
+ /*
+ * get_user() of u64 is not atomic on 32-bit — caller should not
+ * reach here on non-64-bit kernels.
+ */
+ if (!IS_ENABLED(CONFIG_64BIT))
+ return -EOPNOTSUPP;
+
+ if (op != DRM_USER_FENCE_CMP_NONE) {
+ if (!addr || !IS_ALIGNED((unsigned long)addr, sizeof(u64)))
+ return -EINVAL;
+ }
+
+ ufence->cmp_addr = addr;
+ ufence->cmp_value = value;
+ ufence->cmp_op = op;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(drm_user_fence_set_compare);
diff --git a/include/drm/drm_user_fence.h b/include/drm/drm_user_fence.h
index d35438eaa9e2..94bc2fd59d73 100644
--- a/include/drm/drm_user_fence.h
+++ b/include/drm/drm_user_fence.h
@@ -40,11 +40,23 @@ struct drm_user_fence_ops {
void (*destroy)(struct drm_user_fence *ufence);
};
+/**
+ * enum drm_user_fence_cmp - compare operator for per-signal filtering
+ */
+enum drm_user_fence_cmp {
+ DRM_USER_FENCE_CMP_NONE = 0,
+ DRM_USER_FENCE_CMP_EQ,
+ DRM_USER_FENCE_CMP_NEQ,
+ DRM_USER_FENCE_CMP_GTE,
+};
+
/**
* struct drm_user_fence - DRM user fence with MM borrowing
*
* Extends drm_work_fence with kthread_use_mm() support for drivers
* that need to access userspace memory when a GPU fence signals.
+ * For work that does not need userspace memory access, use
+ * drm_work_fence directly.
*
* Call drm_user_fence_init() at creation and drm_user_fence_add_callback()
* to arm on a dma-fence. Call drm_user_fence_cancel_sync() before teardown.
@@ -56,11 +68,20 @@ struct drm_user_fence {
struct mm_struct *mm;
/** @ops: Driver operations. */
const struct drm_user_fence_ops *ops;
+ /** @cmp_addr: Userspace address to read for per-signal compare. */
+ u64 __user *cmp_addr;
+ /** @cmp_value: Expected value for per-signal compare. */
+ u64 cmp_value;
+ /** @cmp_op: Compare operator; DRM_USER_FENCE_CMP_NONE disables. */
+ enum drm_user_fence_cmp cmp_op;
};
void drm_user_fence_init(struct drm_user_fence *ufence,
struct workqueue_struct *wq,
const struct drm_user_fence_ops *ops);
+int drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op);
/**
* drm_user_fence_get - Acquire a reference to a user fence
@@ -112,11 +133,35 @@ static inline bool drm_user_fence_cancel(struct drm_user_fence *ufence)
* drm_user_fence_cancel_sync - Cancel callback and wait for worker to finish
* @ufence: user fence
*
- * Must be called during teardown before freeing resources. May sleep.
+ * Must be called during teardown before freeing any resources accessed
+ * by ops->worker(). May sleep.
*/
static inline void drm_user_fence_cancel_sync(struct drm_user_fence *ufence)
{
drm_work_fence_cancel_sync(&ufence->base);
}
+/**
+ * drm_user_fence_cmp_match - Test a value against the compare filter
+ * @cur_val: value read from userspace (already converted from LE)
+ * @cmp_value: expected value
+ * @op: comparison operator
+ *
+ * Return: true if the comparison passes, false otherwise.
+ */
+static inline bool drm_user_fence_cmp_match(u64 cur_val, u64 cmp_value,
+ enum drm_user_fence_cmp op)
+{
+ switch (op) {
+ case DRM_USER_FENCE_CMP_EQ:
+ return cur_val == cmp_value;
+ case DRM_USER_FENCE_CMP_NEQ:
+ return cur_val != cmp_value;
+ case DRM_USER_FENCE_CMP_GTE:
+ return cur_val >= cmp_value;
+ default:
+ return false;
+ }
+}
+
#endif /* __DRM_USER_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 1/4] drm: Add drm_work_fence helper
@ 2026-09-02 15:48 Srinivasan Shanmugam
2026-09-02 15:48 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
0 siblings, 1 reply; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-02 15:48 UTC (permalink / raw)
To: linux-kernel
GPU drivers often need to queue work when a dma-fence signals
because certain operations (copy_to_user, eventfd_signal, memory
allocation) cannot run in IRQ context. This pattern is currently
open-coded in multiple drivers.
Introduce drm_work_fence — an embeddable base structure that handles the
dma-fence-callback-to-workqueue pattern in one place. Drivers embed this
in their own structure and implement ops->writeback() for the deferred
work and ops->destroy() for cleanup.
The helper manages:
- kref lifetime
- dma-fence callback registration
- workqueue dispatch on fence signal
- safe cancellation before driver teardown
For work that additionally requires borrowing the process MM via
kthread_use_mm(), see drm_user_fence which builds on top of this.
Suggested-by: Matthew Brost <matthew.brost@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
---
drivers/gpu/drm/Makefile | 1 +
drivers/gpu/drm/drm_work_fence.c | 184 +++++++++++++++++++++++++++++++
include/drm/drm_work_fence.h | 69 ++++++++++++
3 files changed, 254 insertions(+)
create mode 100644 drivers/gpu/drm/drm_work_fence.c
create mode 100644 include/drm/drm_work_fence.h
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index e97faabcd783..c5be8e80d0c8 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -72,6 +72,7 @@ drm-y := \
drm_vblank.o \
drm_vblank_work.o \
drm_vma_manager.o \
+ drm_work_fence.o \
drm_writeback.o
drm-$(CONFIG_DRM_CLIENT) += \
drm_client.o \
diff --git a/drivers/gpu/drm/drm_work_fence.c b/drivers/gpu/drm/drm_work_fence.c
new file mode 100644
index 000000000000..482eb3d4475f
--- /dev/null
+++ b/drivers/gpu/drm/drm_work_fence.c
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 The Linux Foundation
+ *
+ * Common DRM work fence helper.
+ *
+ * When a GPU dma-fence signals, drivers often need to perform work that
+ * cannot run in IRQ context (e.g., memory allocation, copy_to_user,
+ * eventfd_signal). This helper queues a work item when a dma-fence
+ * signals, allowing that work to run safely in a workqueue context.
+ *
+ * NOTE: This helper consumes dma_fences but CANNOT implement
+ * dma_fence_ops. Work items queued here may sleep; dma_fence_ops
+ * callbacks are called under the fence spinlock and must not sleep.
+ *
+ * For work that additionally requires accessing userspace memory via
+ * kthread_use_mm(), see drm_user_fence which builds on top of this.
+ */
+
+#include <linux/workqueue.h>
+
+#include <drm/drm_work_fence.h>
+
+static void drm_work_fence_destroy(struct kref *kref)
+{
+ struct drm_work_fence *wfence =
+ container_of(kref, struct drm_work_fence, refcount);
+ struct dma_fence *fence = wfence->fence;
+
+ wfence->ops->destroy(wfence);
+ dma_fence_put(fence); /* NULL-safe */
+}
+
+/**
+ * drm_work_fence_get - Acquire a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_get(struct drm_work_fence *wfence)
+{
+ kref_get(&wfence->refcount);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_get);
+
+/**
+ * drm_work_fence_put - Release a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_put(struct drm_work_fence *wfence)
+{
+ kref_put(&wfence->refcount, drm_work_fence_destroy);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_put);
+
+static void drm_work_fence_work(struct work_struct *w)
+{
+ struct drm_work_fence *wfence =
+ container_of(w, struct drm_work_fence, work);
+
+ wfence->ops->writeback(wfence);
+ drm_work_fence_put(wfence);
+}
+
+static void drm_work_fence_queue(struct drm_work_fence *wfence)
+{
+ queue_work(wfence->wq, &wfence->work);
+}
+
+static void drm_work_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
+{
+ struct drm_work_fence *wfence =
+ container_of(cb, struct drm_work_fence, cb);
+
+ drm_work_fence_queue(wfence);
+ /* Single ref: wfence->fence released in drm_work_fence_destroy(). */
+}
+
+/**
+ * drm_work_fence_init - Initialize a work fence
+ * @wfence: work fence to initialize
+ * @wq: workqueue to run the worker on (must be ordered if sequencing matters)
+ * @ops: driver operations
+ */
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops)
+{
+ kref_init(&wfence->refcount);
+ wfence->wq = wq;
+ wfence->ops = ops;
+ wfence->fence = NULL;
+ INIT_WORK(&wfence->work, drm_work_fence_work);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_init);
+
+/**
+ * drm_work_fence_add_callback - Attach a work fence to a dma-fence
+ * @wfence: work fence; caller retains their reference and must release
+ * it via drm_work_fence_put() when no longer needed
+ * @fence: dma-fence to watch; one reference is consumed on any return value
+ *
+ * When @fence signals, a work item is queued that calls ops->writeback().
+ * If @fence has already signaled, the work item is queued immediately.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence)
+{
+ int err;
+
+ drm_work_fence_get(wfence);
+ wfence->fence = fence; /* transfer caller's ref — single ref, no get */
+
+ err = dma_fence_add_callback(fence, &wfence->cb, drm_work_fence_cb);
+ if (err == -ENOENT) {
+ drm_work_fence_queue(wfence);
+ err = 0;
+ } else if (err) {
+ wfence->fence = NULL;
+ dma_fence_put(fence);
+ drm_work_fence_put(wfence);
+ }
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_add_callback);
+
+/**
+ * drm_work_fence_cancel - Cancel a pending work fence callback
+ * @wfence: work fence
+ *
+ * Attempts to remove the pending callback before driver context teardown.
+ * The caller must hold a reference to @wfence across this call.
+ *
+ * If the callback has already fired this returns false and all cleanup
+ * has been handled internally.
+ *
+ * If removal succeeds the callback reference is released internally.
+ * The caller must still release its own reference via drm_work_fence_put().
+ *
+ * This function is safe to call from atomic context as it only acquires
+ * the dma-fence spinlock internally. If the caller also needs to wait
+ * for the worker to finish, use drm_work_fence_cancel_sync() instead,
+ * which may sleep.
+ *
+ * Return: true if callback was removed, false if it had already fired.
+ */
+bool drm_work_fence_cancel(struct drm_work_fence *wfence)
+{
+ struct dma_fence *fence = wfence->fence;
+
+ if (!fence)
+ return false;
+
+ if (dma_fence_remove_callback(fence, &wfence->cb)) {
+ drm_work_fence_put(wfence); /* drop ref from add_callback */
+ return true;
+ }
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel);
+
+/**
+ * drm_work_fence_cancel_sync - Cancel callback and wait for worker to finish
+ * @wfence: work fence
+ *
+ * Calls drm_work_fence_cancel() then cancel_work_sync() to guarantee
+ * the worker has fully completed before returning.
+ *
+ * This function may sleep. Must not be called from atomic or interrupt
+ * context. Use drm_work_fence_cancel() instead when sleeping is not allowed.
+ *
+ * Drivers must call this during teardown before freeing any resources
+ * accessed by ops->writeback().
+ */
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence)
+{
+ if (drm_work_fence_cancel(wfence))
+ return;
+ if (cancel_work_sync(&wfence->work))
+ drm_work_fence_put(wfence);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel_sync);
diff --git a/include/drm/drm_work_fence.h b/include/drm/drm_work_fence.h
new file mode 100644
index 000000000000..c8e3c5b9f0c4
--- /dev/null
+++ b/include/drm/drm_work_fence.h
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 The Linux Foundation
+ */
+
+#ifndef __DRM_WORK_FENCE_H__
+#define __DRM_WORK_FENCE_H__
+
+#include <linux/dma-fence.h>
+#include <linux/kref.h>
+#include <linux/workqueue.h>
+
+struct drm_work_fence;
+
+/**
+ * struct drm_work_fence_ops - driver callbacks for a DRM work fence
+ */
+struct drm_work_fence_ops {
+ /**
+ * @writeback: Called from workqueue context when the dma-fence signals.
+ *
+ * Perform the deferred work here (copy_to_user, eventfd_signal, etc.).
+ * May sleep. Must not requeue the fence.
+ */
+ void (*writeback)(struct drm_work_fence *wfence);
+
+ /**
+ * @destroy: Called when the last reference is dropped.
+ * Free the containing structure here.
+ */
+ void (*destroy)(struct drm_work_fence *wfence);
+};
+
+/**
+ * struct drm_work_fence - DRM dma-fence-to-workqueue helper
+ *
+ * Embeddable base structure that queues a work item when a dma-fence signals.
+ * Drivers embed this in their own structure and implement ops->writeback()
+ * for the deferred work and ops->destroy() for cleanup.
+ *
+ * Call drm_work_fence_init() at creation and drm_work_fence_add_callback()
+ * to arm on a dma-fence. Call drm_work_fence_cancel_sync() before teardown.
+ */
+struct drm_work_fence {
+ /** @refcount: Reference count. */
+ struct kref refcount;
+ /** @wq: Workqueue on which to run the worker. */
+ struct workqueue_struct *wq;
+ /** @ops: Driver operations. */
+ const struct drm_work_fence_ops *ops;
+ /** @fence: The watched dma-fence; holds a single reference. */
+ struct dma_fence *fence;
+ /** @work: Work item queued when the fence signals. */
+ struct work_struct work;
+ /** @cb: Callback registered on the dma-fence. */
+ struct dma_fence_cb cb;
+};
+
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops);
+void drm_work_fence_get(struct drm_work_fence *wfence);
+void drm_work_fence_put(struct drm_work_fence *wfence);
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence);
+bool drm_work_fence_cancel(struct drm_work_fence *wfence);
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence);
+
+#endif /* __DRM_WORK_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence
2026-09-02 15:48 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
@ 2026-09-02 15:48 ` Srinivasan Shanmugam
0 siblings, 0 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-02 15:48 UTC (permalink / raw)
To: linux-kernel
GPU drivers sometimes need to read a value from a userspace VA when a
dma-fence signals, compare it with an expected value, and only perform
the deferred work (e.g. eventfd_signal) if the comparison passes. This
is the per-signal filtering pattern used in AMDGPU's EOP eventfd path.
Add optional compare fields to drm_user_fence and a new helper
drm_user_fence_set_compare() to configure them. Supported operators are
==, !=, >=.
When cmp_op is set, drm_user_fence reads the value from userspace via
copy_from_user_nofault() and calls ops->worker() with mm_ok=true only if
the comparison passes. If the process MM is gone or the read fails, the
worker is called with mm_ok=false to allow mandatory housekeeping (e.g.
wake_up()).
Drivers that do not need filtering (e.g. XE) leave cmp_op unset and the
worker is called unconditionally — no behavioral change.
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: dri-devel@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
Change-Id: I200dd4bf32286ba9bdd2ecb68fffb3c205f9fd52
---
drivers/gpu/drm/drm_user_fence.c | 104 ++++++++++++++++++++++++++++---
include/drm/drm_user_fence.h | 47 +++++++++++++-
2 files changed, 142 insertions(+), 9 deletions(-)
diff --git a/drivers/gpu/drm/drm_user_fence.c b/drivers/gpu/drm/drm_user_fence.c
index 0f229b7210a9..d9bd3128ff6c 100644
--- a/drivers/gpu/drm/drm_user_fence.c
+++ b/drivers/gpu/drm/drm_user_fence.c
@@ -2,16 +2,25 @@
/*
* Copyright © 2024 The Linux Foundation
*
- * DRM user fence — extends drm_work_fence with kthread_use_mm() support.
+ * DRM user fence helper.
*
- * Use this when a GPU fence signals and work needs to access userspace
- * memory (copy_to_user, fault-able operations) from a kthread context.
- * For work that does not require userspace memory access, use
- * drm_work_fence directly.
+ * Extends drm_work_fence with the ability to access userspace memory
+ * from workqueue context by borrowing the process MM via kthread_use_mm().
+ *
+ * Drivers that need to write completion status to userspace (e.g., user
+ * fences, signaling eventfds) embed drm_user_fence and implement
+ * ops->worker() to do the actual write.
+ *
+ * Optionally, drivers may configure a per-signal compare via
+ * drm_user_fence_set_compare(): work is skipped unless the value at a
+ * userspace address matches the expected value at signal time.
*/
#include <linux/kthread.h>
+#include <linux/mm.h>
#include <linux/sched/mm.h>
+#include <linux/uaccess.h>
+#include <linux/workqueue.h>
#include <drm/drm_user_fence.h>
@@ -30,13 +39,50 @@ static void drm_user_fence_do_work(struct drm_work_fence *wfence)
struct drm_user_fence *ufence =
container_of(wfence, struct drm_user_fence, base);
struct mm_struct *mm = NULL;
+ bool call_worker = true;
if (mmget_not_zero(ufence->mm)) {
mm = ufence->mm;
kthread_use_mm(mm);
}
- ufence->ops->worker(ufence, !!mm);
+ if (ufence->cmp_op != DRM_USER_FENCE_CMP_NONE &&
+ !(wfence->fence && wfence->fence->error)) {
+ if (!mm) {
+ call_worker = false;
+ } else {
+ __le64 raw;
+
+ /*
+ * Use copy_from_user_nofault() to prevent a
+ * userfaultfd-registered page from blocking this
+ * workqueue thread indefinitely (DoS).
+ *
+ * If the page is not resident (e.g. swapped out,
+ * NUMA-migrated), call the worker conservatively to
+ * avoid dropping legitimate completion events.
+ */
+ if (copy_from_user_nofault(&raw, ufence->cmp_addr,
+ sizeof(raw))) {
+ call_worker = true;
+ } else {
+ /* GPU writes LE; convert before comparing. */
+ u64 cur_val = le64_to_cpu(raw);
+
+ if (!drm_user_fence_cmp_match(cur_val,
+ ufence->cmp_value,
+ ufence->cmp_op))
+ call_worker = false;
+ }
+ }
+ }
+
+ /*
+ * Always invoke the worker so drivers can perform mandatory
+ * housekeeping (e.g. wake_up()). Pass false if the compare
+ * filter suppressed the write.
+ */
+ ufence->ops->worker(ufence, call_worker ? !!mm : false);
if (mm) {
kthread_unuse_mm(mm);
@@ -63,8 +109,50 @@ void drm_user_fence_init(struct drm_user_fence *ufence,
const struct drm_user_fence_ops *ops)
{
drm_work_fence_init(&ufence->base, wq, &drm_user_fence_wfence_ops);
- ufence->mm = current->mm;
+ ufence->mm = current->mm;
mmgrab(ufence->mm);
- ufence->ops = ops;
+ ufence->ops = ops;
+ ufence->cmp_op = DRM_USER_FENCE_CMP_NONE;
+ ufence->cmp_addr = NULL;
+ ufence->cmp_value = 0;
}
EXPORT_SYMBOL_GPL(drm_user_fence_init);
+
+/**
+ * drm_user_fence_set_compare - Set per-signal compare filter
+ * @ufence: user fence
+ * @addr: 8-byte-aligned userspace address to read from at signal time
+ * @value: expected value to compare against
+ * @op: comparison operator; pass %DRM_USER_FENCE_CMP_NONE to disable
+ *
+ * When @op is not %DRM_USER_FENCE_CMP_NONE, the worker is only called
+ * with mm_ok=true if the value at @addr matches @value according to @op.
+ * The worker is always called for mandatory housekeeping.
+ * If the MM is gone or the read fails, mm_ok is passed as false.
+ *
+ * Must only be called before drm_work_fence_add_callback().
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op)
+{
+ /*
+ * get_user() of u64 is not atomic on 32-bit — caller should not
+ * reach here on non-64-bit kernels.
+ */
+ if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_64BIT)))
+ return -EOPNOTSUPP;
+
+ if (op != DRM_USER_FENCE_CMP_NONE) {
+ if (!addr || !IS_ALIGNED((unsigned long)addr, sizeof(u64)))
+ return -EINVAL;
+ }
+
+ ufence->cmp_addr = addr;
+ ufence->cmp_value = value;
+ ufence->cmp_op = op;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(drm_user_fence_set_compare);
diff --git a/include/drm/drm_user_fence.h b/include/drm/drm_user_fence.h
index d35438eaa9e2..94bc2fd59d73 100644
--- a/include/drm/drm_user_fence.h
+++ b/include/drm/drm_user_fence.h
@@ -40,11 +40,23 @@ struct drm_user_fence_ops {
void (*destroy)(struct drm_user_fence *ufence);
};
+/**
+ * enum drm_user_fence_cmp - compare operator for per-signal filtering
+ */
+enum drm_user_fence_cmp {
+ DRM_USER_FENCE_CMP_NONE = 0,
+ DRM_USER_FENCE_CMP_EQ,
+ DRM_USER_FENCE_CMP_NEQ,
+ DRM_USER_FENCE_CMP_GTE,
+};
+
/**
* struct drm_user_fence - DRM user fence with MM borrowing
*
* Extends drm_work_fence with kthread_use_mm() support for drivers
* that need to access userspace memory when a GPU fence signals.
+ * For work that does not need userspace memory access, use
+ * drm_work_fence directly.
*
* Call drm_user_fence_init() at creation and drm_user_fence_add_callback()
* to arm on a dma-fence. Call drm_user_fence_cancel_sync() before teardown.
@@ -56,11 +68,20 @@ struct drm_user_fence {
struct mm_struct *mm;
/** @ops: Driver operations. */
const struct drm_user_fence_ops *ops;
+ /** @cmp_addr: Userspace address to read for per-signal compare. */
+ u64 __user *cmp_addr;
+ /** @cmp_value: Expected value for per-signal compare. */
+ u64 cmp_value;
+ /** @cmp_op: Compare operator; DRM_USER_FENCE_CMP_NONE disables. */
+ enum drm_user_fence_cmp cmp_op;
};
void drm_user_fence_init(struct drm_user_fence *ufence,
struct workqueue_struct *wq,
const struct drm_user_fence_ops *ops);
+int drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op);
/**
* drm_user_fence_get - Acquire a reference to a user fence
@@ -112,11 +133,35 @@ static inline bool drm_user_fence_cancel(struct drm_user_fence *ufence)
* drm_user_fence_cancel_sync - Cancel callback and wait for worker to finish
* @ufence: user fence
*
- * Must be called during teardown before freeing resources. May sleep.
+ * Must be called during teardown before freeing any resources accessed
+ * by ops->worker(). May sleep.
*/
static inline void drm_user_fence_cancel_sync(struct drm_user_fence *ufence)
{
drm_work_fence_cancel_sync(&ufence->base);
}
+/**
+ * drm_user_fence_cmp_match - Test a value against the compare filter
+ * @cur_val: value read from userspace (already converted from LE)
+ * @cmp_value: expected value
+ * @op: comparison operator
+ *
+ * Return: true if the comparison passes, false otherwise.
+ */
+static inline bool drm_user_fence_cmp_match(u64 cur_val, u64 cmp_value,
+ enum drm_user_fence_cmp op)
+{
+ switch (op) {
+ case DRM_USER_FENCE_CMP_EQ:
+ return cur_val == cmp_value;
+ case DRM_USER_FENCE_CMP_NEQ:
+ return cur_val != cmp_value;
+ case DRM_USER_FENCE_CMP_GTE:
+ return cur_val >= cmp_value;
+ default:
+ return false;
+ }
+}
+
#endif /* __DRM_USER_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 1/4] drm: Add drm_work_fence helper
@ 2026-09-02 14:56 Srinivasan Shanmugam
2026-09-02 14:56 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
0 siblings, 1 reply; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-02 14:56 UTC (permalink / raw)
To: linux-kernel
GPU drivers often need to queue work when a dma-fence signals
because certain operations (copy_to_user, eventfd_signal, memory
allocation) cannot run in IRQ context. This pattern is currently
open-coded in multiple drivers.
Introduce drm_work_fence — an embeddable base structure that handles the
dma-fence-callback-to-workqueue pattern in one place. Drivers embed this
in their own structure and implement ops->writeback() for the deferred
work and ops->destroy() for cleanup.
The helper manages:
- kref lifetime
- dma-fence callback registration
- workqueue dispatch on fence signal
- safe cancellation before driver teardown
For work that additionally requires borrowing the process MM via
kthread_use_mm(), see drm_user_fence which builds on top of this.
Suggested-by: Matthew Brost <matthew.brost@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
---
drivers/gpu/drm/Makefile | 1 +
drivers/gpu/drm/drm_work_fence.c | 184 +++++++++++++++++++++++++++++++
include/drm/drm_work_fence.h | 69 ++++++++++++
3 files changed, 254 insertions(+)
create mode 100644 drivers/gpu/drm/drm_work_fence.c
create mode 100644 include/drm/drm_work_fence.h
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index e97faabcd783..c5be8e80d0c8 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -72,6 +72,7 @@ drm-y := \
drm_vblank.o \
drm_vblank_work.o \
drm_vma_manager.o \
+ drm_work_fence.o \
drm_writeback.o
drm-$(CONFIG_DRM_CLIENT) += \
drm_client.o \
diff --git a/drivers/gpu/drm/drm_work_fence.c b/drivers/gpu/drm/drm_work_fence.c
new file mode 100644
index 000000000000..482eb3d4475f
--- /dev/null
+++ b/drivers/gpu/drm/drm_work_fence.c
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 The Linux Foundation
+ *
+ * Common DRM work fence helper.
+ *
+ * When a GPU dma-fence signals, drivers often need to perform work that
+ * cannot run in IRQ context (e.g., memory allocation, copy_to_user,
+ * eventfd_signal). This helper queues a work item when a dma-fence
+ * signals, allowing that work to run safely in a workqueue context.
+ *
+ * NOTE: This helper consumes dma_fences but CANNOT implement
+ * dma_fence_ops. Work items queued here may sleep; dma_fence_ops
+ * callbacks are called under the fence spinlock and must not sleep.
+ *
+ * For work that additionally requires accessing userspace memory via
+ * kthread_use_mm(), see drm_user_fence which builds on top of this.
+ */
+
+#include <linux/workqueue.h>
+
+#include <drm/drm_work_fence.h>
+
+static void drm_work_fence_destroy(struct kref *kref)
+{
+ struct drm_work_fence *wfence =
+ container_of(kref, struct drm_work_fence, refcount);
+ struct dma_fence *fence = wfence->fence;
+
+ wfence->ops->destroy(wfence);
+ dma_fence_put(fence); /* NULL-safe */
+}
+
+/**
+ * drm_work_fence_get - Acquire a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_get(struct drm_work_fence *wfence)
+{
+ kref_get(&wfence->refcount);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_get);
+
+/**
+ * drm_work_fence_put - Release a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_put(struct drm_work_fence *wfence)
+{
+ kref_put(&wfence->refcount, drm_work_fence_destroy);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_put);
+
+static void drm_work_fence_work(struct work_struct *w)
+{
+ struct drm_work_fence *wfence =
+ container_of(w, struct drm_work_fence, work);
+
+ wfence->ops->writeback(wfence);
+ drm_work_fence_put(wfence);
+}
+
+static void drm_work_fence_queue(struct drm_work_fence *wfence)
+{
+ queue_work(wfence->wq, &wfence->work);
+}
+
+static void drm_work_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
+{
+ struct drm_work_fence *wfence =
+ container_of(cb, struct drm_work_fence, cb);
+
+ drm_work_fence_queue(wfence);
+ /* Single ref: wfence->fence released in drm_work_fence_destroy(). */
+}
+
+/**
+ * drm_work_fence_init - Initialize a work fence
+ * @wfence: work fence to initialize
+ * @wq: workqueue to run the worker on (must be ordered if sequencing matters)
+ * @ops: driver operations
+ */
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops)
+{
+ kref_init(&wfence->refcount);
+ wfence->wq = wq;
+ wfence->ops = ops;
+ wfence->fence = NULL;
+ INIT_WORK(&wfence->work, drm_work_fence_work);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_init);
+
+/**
+ * drm_work_fence_add_callback - Attach a work fence to a dma-fence
+ * @wfence: work fence; caller retains their reference and must release
+ * it via drm_work_fence_put() when no longer needed
+ * @fence: dma-fence to watch; one reference is consumed on any return value
+ *
+ * When @fence signals, a work item is queued that calls ops->writeback().
+ * If @fence has already signaled, the work item is queued immediately.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence)
+{
+ int err;
+
+ drm_work_fence_get(wfence);
+ wfence->fence = fence; /* transfer caller's ref — single ref, no get */
+
+ err = dma_fence_add_callback(fence, &wfence->cb, drm_work_fence_cb);
+ if (err == -ENOENT) {
+ drm_work_fence_queue(wfence);
+ err = 0;
+ } else if (err) {
+ wfence->fence = NULL;
+ dma_fence_put(fence);
+ drm_work_fence_put(wfence);
+ }
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_add_callback);
+
+/**
+ * drm_work_fence_cancel - Cancel a pending work fence callback
+ * @wfence: work fence
+ *
+ * Attempts to remove the pending callback before driver context teardown.
+ * The caller must hold a reference to @wfence across this call.
+ *
+ * If the callback has already fired this returns false and all cleanup
+ * has been handled internally.
+ *
+ * If removal succeeds the callback reference is released internally.
+ * The caller must still release its own reference via drm_work_fence_put().
+ *
+ * This function is safe to call from atomic context as it only acquires
+ * the dma-fence spinlock internally. If the caller also needs to wait
+ * for the worker to finish, use drm_work_fence_cancel_sync() instead,
+ * which may sleep.
+ *
+ * Return: true if callback was removed, false if it had already fired.
+ */
+bool drm_work_fence_cancel(struct drm_work_fence *wfence)
+{
+ struct dma_fence *fence = wfence->fence;
+
+ if (!fence)
+ return false;
+
+ if (dma_fence_remove_callback(fence, &wfence->cb)) {
+ drm_work_fence_put(wfence); /* drop ref from add_callback */
+ return true;
+ }
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel);
+
+/**
+ * drm_work_fence_cancel_sync - Cancel callback and wait for worker to finish
+ * @wfence: work fence
+ *
+ * Calls drm_work_fence_cancel() then cancel_work_sync() to guarantee
+ * the worker has fully completed before returning.
+ *
+ * This function may sleep. Must not be called from atomic or interrupt
+ * context. Use drm_work_fence_cancel() instead when sleeping is not allowed.
+ *
+ * Drivers must call this during teardown before freeing any resources
+ * accessed by ops->writeback().
+ */
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence)
+{
+ if (drm_work_fence_cancel(wfence))
+ return;
+ if (cancel_work_sync(&wfence->work))
+ drm_work_fence_put(wfence);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel_sync);
diff --git a/include/drm/drm_work_fence.h b/include/drm/drm_work_fence.h
new file mode 100644
index 000000000000..c8e3c5b9f0c4
--- /dev/null
+++ b/include/drm/drm_work_fence.h
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 The Linux Foundation
+ */
+
+#ifndef __DRM_WORK_FENCE_H__
+#define __DRM_WORK_FENCE_H__
+
+#include <linux/dma-fence.h>
+#include <linux/kref.h>
+#include <linux/workqueue.h>
+
+struct drm_work_fence;
+
+/**
+ * struct drm_work_fence_ops - driver callbacks for a DRM work fence
+ */
+struct drm_work_fence_ops {
+ /**
+ * @writeback: Called from workqueue context when the dma-fence signals.
+ *
+ * Perform the deferred work here (copy_to_user, eventfd_signal, etc.).
+ * May sleep. Must not requeue the fence.
+ */
+ void (*writeback)(struct drm_work_fence *wfence);
+
+ /**
+ * @destroy: Called when the last reference is dropped.
+ * Free the containing structure here.
+ */
+ void (*destroy)(struct drm_work_fence *wfence);
+};
+
+/**
+ * struct drm_work_fence - DRM dma-fence-to-workqueue helper
+ *
+ * Embeddable base structure that queues a work item when a dma-fence signals.
+ * Drivers embed this in their own structure and implement ops->writeback()
+ * for the deferred work and ops->destroy() for cleanup.
+ *
+ * Call drm_work_fence_init() at creation and drm_work_fence_add_callback()
+ * to arm on a dma-fence. Call drm_work_fence_cancel_sync() before teardown.
+ */
+struct drm_work_fence {
+ /** @refcount: Reference count. */
+ struct kref refcount;
+ /** @wq: Workqueue on which to run the worker. */
+ struct workqueue_struct *wq;
+ /** @ops: Driver operations. */
+ const struct drm_work_fence_ops *ops;
+ /** @fence: The watched dma-fence; holds a single reference. */
+ struct dma_fence *fence;
+ /** @work: Work item queued when the fence signals. */
+ struct work_struct work;
+ /** @cb: Callback registered on the dma-fence. */
+ struct dma_fence_cb cb;
+};
+
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops);
+void drm_work_fence_get(struct drm_work_fence *wfence);
+void drm_work_fence_put(struct drm_work_fence *wfence);
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence);
+bool drm_work_fence_cancel(struct drm_work_fence *wfence);
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence);
+
+#endif /* __DRM_WORK_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence
2026-09-02 14:56 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
@ 2026-09-02 14:56 ` Srinivasan Shanmugam
0 siblings, 0 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-02 14:56 UTC (permalink / raw)
To: linux-kernel
GPU drivers sometimes need to read a value from a userspace VA when a
dma-fence signals, compare it with an expected value, and only perform
the deferred work (e.g. eventfd_signal) if the comparison passes. This
is the per-signal filtering pattern used in AMDGPU's EOP eventfd path.
Add optional compare fields to drm_user_fence and a new helper
drm_user_fence_set_compare() to configure them. Supported operators are
==, !=, >=.
When cmp_op is set, drm_user_fence reads the value from userspace via
copy_from_user_nofault() and calls ops->worker() with mm_ok=true only if
the comparison passes. If the process MM is gone or the read fails, the
worker is called with mm_ok=false to allow mandatory housekeeping (e.g.
wake_up()).
Drivers that do not need filtering (e.g. XE) leave cmp_op unset and the
worker is called unconditionally — no behavioral change.
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: dri-devel@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
Change-Id: I200dd4bf32286ba9bdd2ecb68fffb3c205f9fd52
---
drivers/gpu/drm/drm_user_fence.c | 97 +++++++++++++++++++++++++++++---
include/drm/drm_user_fence.h | 52 ++++++++++++++++-
2 files changed, 138 insertions(+), 11 deletions(-)
diff --git a/drivers/gpu/drm/drm_user_fence.c b/drivers/gpu/drm/drm_user_fence.c
index 0f229b7210a9..06a5683db10d 100644
--- a/drivers/gpu/drm/drm_user_fence.c
+++ b/drivers/gpu/drm/drm_user_fence.c
@@ -2,16 +2,25 @@
/*
* Copyright © 2024 The Linux Foundation
*
- * DRM user fence — extends drm_work_fence with kthread_use_mm() support.
+ * DRM user fence helper.
*
- * Use this when a GPU fence signals and work needs to access userspace
- * memory (copy_to_user, fault-able operations) from a kthread context.
- * For work that does not require userspace memory access, use
- * drm_work_fence directly.
+ * Extends drm_work_fence with the ability to access userspace memory
+ * from workqueue context by borrowing the process MM via kthread_use_mm().
+ *
+ * Drivers that need to write completion status to userspace (e.g., user
+ * fences, signaling eventfds) embed drm_user_fence and implement
+ * ops->worker() to do the actual write.
+ *
+ * Optionally, drivers may configure a per-signal compare via
+ * drm_user_fence_set_compare(): work is skipped unless the value at a
+ * userspace address matches the expected value at signal time.
*/
#include <linux/kthread.h>
+#include <linux/mm.h>
#include <linux/sched/mm.h>
+#include <linux/uaccess.h>
+#include <linux/workqueue.h>
#include <drm/drm_user_fence.h>
@@ -30,13 +39,46 @@ static void drm_user_fence_do_work(struct drm_work_fence *wfence)
struct drm_user_fence *ufence =
container_of(wfence, struct drm_user_fence, base);
struct mm_struct *mm = NULL;
+ bool call_worker = true;
if (mmget_not_zero(ufence->mm)) {
mm = ufence->mm;
kthread_use_mm(mm);
}
- ufence->ops->worker(ufence, !!mm);
+ if (ufence->cmp_op != DRM_USER_FENCE_CMP_NONE &&
+ !(wfence->fence && wfence->fence->error)) {
+ if (!mm) {
+ call_worker = false;
+ } else {
+ __le64 raw;
+
+ /*
+ * Use copy_from_user_nofault() to prevent a
+ * userfaultfd-registered page from blocking this
+ * workqueue thread indefinitely (DoS).
+ */
+ if (copy_from_user_nofault(&raw, ufence->cmp_addr,
+ sizeof(raw))) {
+ call_worker = false;
+ } else {
+ /* GPU writes LE; convert before comparing. */
+ u64 cur_val = le64_to_cpu(raw);
+
+ if (!drm_user_fence_cmp_match(cur_val,
+ ufence->cmp_value,
+ ufence->cmp_op))
+ call_worker = false;
+ }
+ }
+ }
+
+ /*
+ * Always invoke the worker so drivers can perform mandatory
+ * housekeeping (e.g. wake_up()). Pass false if the compare
+ * filter suppressed the write.
+ */
+ ufence->ops->worker(ufence, call_worker ? !!mm : false);
if (mm) {
kthread_unuse_mm(mm);
@@ -63,8 +105,47 @@ void drm_user_fence_init(struct drm_user_fence *ufence,
const struct drm_user_fence_ops *ops)
{
drm_work_fence_init(&ufence->base, wq, &drm_user_fence_wfence_ops);
- ufence->mm = current->mm;
+ ufence->mm = current->mm;
mmgrab(ufence->mm);
- ufence->ops = ops;
+ ufence->ops = ops;
+ ufence->cmp_op = DRM_USER_FENCE_CMP_NONE;
+ ufence->cmp_addr = NULL;
+ ufence->cmp_value = 0;
}
EXPORT_SYMBOL_GPL(drm_user_fence_init);
+
+/**
+ * drm_user_fence_set_compare - Set per-signal compare filter
+ * @ufence: user fence
+ * @addr: 8-byte-aligned userspace address to read from at signal time
+ * @value: expected value to compare against
+ * @op: comparison operator; pass %DRM_USER_FENCE_CMP_NONE to disable
+ *
+ * When @op is not %DRM_USER_FENCE_CMP_NONE, the worker is only called
+ * with mm_ok=true if the value at @addr matches @value according to @op.
+ * The worker is always called for mandatory housekeeping.
+ * If the MM is gone or the read fails, mm_ok is passed as false.
+ *
+ * Must only be called before drm_work_fence_add_callback().
+ */
+void drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op)
+{
+ /*
+ * get_user() of u64 is not atomic on 32-bit — caller should not
+ * reach here on non-64-bit kernels.
+ */
+ if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_64BIT)))
+ return;
+
+ if (op != DRM_USER_FENCE_CMP_NONE) {
+ if (!addr || !IS_ALIGNED((unsigned long)addr, sizeof(u64)))
+ return;
+ }
+
+ ufence->cmp_addr = addr;
+ ufence->cmp_value = value;
+ ufence->cmp_op = op;
+}
+EXPORT_SYMBOL_GPL(drm_user_fence_set_compare);
diff --git a/include/drm/drm_user_fence.h b/include/drm/drm_user_fence.h
index 36c61aa7d102..1e3b017cbc30 100644
--- a/include/drm/drm_user_fence.h
+++ b/include/drm/drm_user_fence.h
@@ -20,8 +20,9 @@ struct drm_user_fence_ops {
* @worker: Called from workqueue context with the process MM active.
*
* If @mm_ok is true, kthread_use_mm() is active and userspace memory
- * may be accessed safely. If @mm_ok is false, the process MM was
- * already gone; skip the userspace write.
+ * (copy_to_user, etc.) may be accessed safely.
+ * If @mm_ok is false, the process MM was already gone; skip the
+ * userspace write.
*
* wake_up() or other post-signal housekeeping should also happen here.
*/
@@ -34,11 +35,23 @@ struct drm_user_fence_ops {
void (*destroy)(struct drm_user_fence *ufence);
};
+/**
+ * enum drm_user_fence_cmp - compare operator for per-signal filtering
+ */
+enum drm_user_fence_cmp {
+ DRM_USER_FENCE_CMP_NONE = 0,
+ DRM_USER_FENCE_CMP_EQ,
+ DRM_USER_FENCE_CMP_NEQ,
+ DRM_USER_FENCE_CMP_GTE,
+};
+
/**
* struct drm_user_fence - DRM user fence with MM borrowing
*
* Extends drm_work_fence with kthread_use_mm() support for drivers
* that need to access userspace memory when a GPU fence signals.
+ * For work that does not need userspace memory access, use
+ * drm_work_fence directly.
*
* Call drm_user_fence_init() at creation and drm_user_fence_add_callback()
* to arm on a dma-fence. Call drm_user_fence_cancel_sync() before teardown.
@@ -50,11 +63,20 @@ struct drm_user_fence {
struct mm_struct *mm;
/** @ops: Driver operations. */
const struct drm_user_fence_ops *ops;
+ /** @cmp_addr: Userspace address to read for per-signal compare. */
+ u64 __user *cmp_addr;
+ /** @cmp_value: Expected value for per-signal compare. */
+ u64 cmp_value;
+ /** @cmp_op: Compare operator; DRM_USER_FENCE_CMP_NONE disables. */
+ enum drm_user_fence_cmp cmp_op;
};
void drm_user_fence_init(struct drm_user_fence *ufence,
struct workqueue_struct *wq,
const struct drm_user_fence_ops *ops);
+void drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op);
/**
* drm_user_fence_get - Acquire a reference to a user fence
@@ -106,11 +128,35 @@ static inline bool drm_user_fence_cancel(struct drm_user_fence *ufence)
* drm_user_fence_cancel_sync - Cancel callback and wait for worker to finish
* @ufence: user fence
*
- * Must be called during teardown before freeing resources. May sleep.
+ * Must be called during teardown before freeing any resources accessed
+ * by ops->worker(). May sleep.
*/
static inline void drm_user_fence_cancel_sync(struct drm_user_fence *ufence)
{
drm_work_fence_cancel_sync(&ufence->base);
}
+/**
+ * drm_user_fence_cmp_match - Test a value against the compare filter
+ * @cur_val: value read from userspace (already converted from LE)
+ * @cmp_value: expected value
+ * @op: comparison operator
+ *
+ * Return: true if the comparison passes, false otherwise.
+ */
+static inline bool drm_user_fence_cmp_match(u64 cur_val, u64 cmp_value,
+ enum drm_user_fence_cmp op)
+{
+ switch (op) {
+ case DRM_USER_FENCE_CMP_EQ:
+ return cur_val == cmp_value;
+ case DRM_USER_FENCE_CMP_NEQ:
+ return cur_val != cmp_value;
+ case DRM_USER_FENCE_CMP_GTE:
+ return cur_val >= cmp_value;
+ default:
+ return false;
+ }
+}
+
#endif /* __DRM_USER_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 1/4] drm: Add drm_work_fence helper
@ 2026-09-02 13:49 Srinivasan Shanmugam
2026-09-02 13:49 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
0 siblings, 1 reply; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-02 13:49 UTC (permalink / raw)
To: linux-kernel
GPU drivers often need to queue work when a dma-fence signals
because certain operations (copy_to_user, eventfd_signal, memory
allocation) cannot run in IRQ context. This pattern is currently
open-coded in multiple drivers.
Introduce drm_work_fence — an embeddable base structure that handles the
dma-fence-callback-to-workqueue pattern in one place. Drivers embed this
in their own structure and implement ops->writeback() for the deferred
work and ops->destroy() for cleanup.
The helper manages:
- kref lifetime
- dma-fence callback registration
- workqueue dispatch on fence signal
- safe cancellation before driver teardown
For work that additionally requires borrowing the process MM via
kthread_use_mm(), see drm_user_fence which builds on top of this.
Suggested-by: Matthew Brost <matthew.brost@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
---
drivers/gpu/drm/Makefile | 1 +
drivers/gpu/drm/drm_work_fence.c | 183 +++++++++++++++++++++++++++++++
include/drm/drm_work_fence.h | 69 ++++++++++++
3 files changed, 253 insertions(+)
create mode 100644 drivers/gpu/drm/drm_work_fence.c
create mode 100644 include/drm/drm_work_fence.h
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index e97faabcd783..c5be8e80d0c8 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -72,6 +72,7 @@ drm-y := \
drm_vblank.o \
drm_vblank_work.o \
drm_vma_manager.o \
+ drm_work_fence.o \
drm_writeback.o
drm-$(CONFIG_DRM_CLIENT) += \
drm_client.o \
diff --git a/drivers/gpu/drm/drm_work_fence.c b/drivers/gpu/drm/drm_work_fence.c
new file mode 100644
index 000000000000..74e319ba7f5b
--- /dev/null
+++ b/drivers/gpu/drm/drm_work_fence.c
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 The Linux Foundation
+ *
+ * Common DRM work fence helper.
+ *
+ * When a GPU dma-fence signals, drivers often need to perform work that
+ * cannot run in IRQ context (e.g., memory allocation, copy_to_user,
+ * eventfd_signal). This helper queues a work item when a dma-fence
+ * signals, allowing that work to run safely in a workqueue context.
+ *
+ * NOTE: This helper consumes dma_fences but CANNOT implement
+ * dma_fence_ops. Work items queued here may sleep; dma_fence_ops
+ * callbacks are called under the fence spinlock and must not sleep.
+ *
+ * For work that additionally requires accessing userspace memory via
+ * kthread_use_mm(), see drm_user_fence which builds on top of this.
+ */
+
+#include <linux/workqueue.h>
+
+#include <drm/drm_work_fence.h>
+
+static void drm_work_fence_destroy(struct kref *kref)
+{
+ struct drm_work_fence *wfence =
+ container_of(kref, struct drm_work_fence, refcount);
+ struct dma_fence *fence = wfence->fence;
+
+ wfence->ops->destroy(wfence);
+ dma_fence_put(fence); /* NULL-safe */
+}
+
+/**
+ * drm_work_fence_get - Acquire a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_get(struct drm_work_fence *wfence)
+{
+ kref_get(&wfence->refcount);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_get);
+
+/**
+ * drm_work_fence_put - Release a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_put(struct drm_work_fence *wfence)
+{
+ kref_put(&wfence->refcount, drm_work_fence_destroy);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_put);
+
+static void drm_work_fence_work(struct work_struct *w)
+{
+ struct drm_work_fence *wfence =
+ container_of(w, struct drm_work_fence, work);
+
+ wfence->ops->writeback(wfence);
+ drm_work_fence_put(wfence);
+}
+
+static void drm_work_fence_queue(struct drm_work_fence *wfence)
+{
+ queue_work(wfence->wq, &wfence->work);
+}
+
+static void drm_work_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
+{
+ struct drm_work_fence *wfence =
+ container_of(cb, struct drm_work_fence, cb);
+
+ drm_work_fence_queue(wfence);
+ /* Single ref: wfence->fence released in drm_work_fence_destroy(). */
+}
+
+/**
+ * drm_work_fence_init - Initialize a work fence
+ * @wfence: work fence to initialize
+ * @wq: workqueue to run the worker on (must be ordered if sequencing matters)
+ * @ops: driver operations
+ */
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops)
+{
+ kref_init(&wfence->refcount);
+ wfence->wq = wq;
+ wfence->ops = ops;
+ wfence->fence = NULL;
+ INIT_WORK(&wfence->work, drm_work_fence_work);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_init);
+
+/**
+ * drm_work_fence_add_callback - Attach a work fence to a dma-fence
+ * @wfence: work fence; one reference is consumed on any return value
+ * @fence: dma-fence to watch; one reference is consumed on any return value
+ *
+ * When @fence signals, a work item is queued that calls ops->writeback().
+ * If @fence has already signaled, the work item is queued immediately.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence)
+{
+ int err;
+
+ drm_work_fence_get(wfence);
+ wfence->fence = fence; /* transfer caller's ref — single ref, no get */
+
+ err = dma_fence_add_callback(fence, &wfence->cb, drm_work_fence_cb);
+ if (err == -ENOENT) {
+ drm_work_fence_queue(wfence);
+ err = 0;
+ } else if (err) {
+ wfence->fence = NULL;
+ dma_fence_put(fence);
+ drm_work_fence_put(wfence);
+ }
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_add_callback);
+
+/**
+ * drm_work_fence_cancel - Cancel a pending work fence callback
+ * @wfence: work fence
+ *
+ * Attempts to remove the pending callback before driver context teardown.
+ * The caller must hold a reference to @wfence across this call.
+ *
+ * If the callback has already fired this returns false and all cleanup
+ * has been handled internally.
+ *
+ * If removal succeeds the callback reference is released internally.
+ * The caller must still release its own reference via drm_work_fence_put().
+ *
+ * This function is safe to call from atomic context as it only acquires
+ * the dma-fence spinlock internally. If the caller also needs to wait
+ * for the worker to finish, use drm_work_fence_cancel_sync() instead,
+ * which may sleep.
+ *
+ * Return: true if callback was removed, false if it had already fired.
+ */
+bool drm_work_fence_cancel(struct drm_work_fence *wfence)
+{
+ struct dma_fence *fence = wfence->fence;
+
+ if (!fence)
+ return false;
+
+ if (dma_fence_remove_callback(fence, &wfence->cb)) {
+ drm_work_fence_put(wfence); /* drop ref from add_callback */
+ return true;
+ }
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel);
+
+/**
+ * drm_work_fence_cancel_sync - Cancel callback and wait for worker to finish
+ * @wfence: work fence
+ *
+ * Calls drm_work_fence_cancel() then cancel_work_sync() to guarantee
+ * the worker has fully completed before returning.
+ *
+ * This function may sleep. Must not be called from atomic or interrupt
+ * context. Use drm_work_fence_cancel() instead when sleeping is not allowed.
+ *
+ * Drivers must call this during teardown before freeing any resources
+ * accessed by ops->writeback().
+ */
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence)
+{
+ if (drm_work_fence_cancel(wfence))
+ return;
+ if (cancel_work_sync(&wfence->work))
+ drm_work_fence_put(wfence);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel_sync);
diff --git a/include/drm/drm_work_fence.h b/include/drm/drm_work_fence.h
new file mode 100644
index 000000000000..c8e3c5b9f0c4
--- /dev/null
+++ b/include/drm/drm_work_fence.h
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 The Linux Foundation
+ */
+
+#ifndef __DRM_WORK_FENCE_H__
+#define __DRM_WORK_FENCE_H__
+
+#include <linux/dma-fence.h>
+#include <linux/kref.h>
+#include <linux/workqueue.h>
+
+struct drm_work_fence;
+
+/**
+ * struct drm_work_fence_ops - driver callbacks for a DRM work fence
+ */
+struct drm_work_fence_ops {
+ /**
+ * @writeback: Called from workqueue context when the dma-fence signals.
+ *
+ * Perform the deferred work here (copy_to_user, eventfd_signal, etc.).
+ * May sleep. Must not requeue the fence.
+ */
+ void (*writeback)(struct drm_work_fence *wfence);
+
+ /**
+ * @destroy: Called when the last reference is dropped.
+ * Free the containing structure here.
+ */
+ void (*destroy)(struct drm_work_fence *wfence);
+};
+
+/**
+ * struct drm_work_fence - DRM dma-fence-to-workqueue helper
+ *
+ * Embeddable base structure that queues a work item when a dma-fence signals.
+ * Drivers embed this in their own structure and implement ops->writeback()
+ * for the deferred work and ops->destroy() for cleanup.
+ *
+ * Call drm_work_fence_init() at creation and drm_work_fence_add_callback()
+ * to arm on a dma-fence. Call drm_work_fence_cancel_sync() before teardown.
+ */
+struct drm_work_fence {
+ /** @refcount: Reference count. */
+ struct kref refcount;
+ /** @wq: Workqueue on which to run the worker. */
+ struct workqueue_struct *wq;
+ /** @ops: Driver operations. */
+ const struct drm_work_fence_ops *ops;
+ /** @fence: The watched dma-fence; holds a single reference. */
+ struct dma_fence *fence;
+ /** @work: Work item queued when the fence signals. */
+ struct work_struct work;
+ /** @cb: Callback registered on the dma-fence. */
+ struct dma_fence_cb cb;
+};
+
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops);
+void drm_work_fence_get(struct drm_work_fence *wfence);
+void drm_work_fence_put(struct drm_work_fence *wfence);
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence);
+bool drm_work_fence_cancel(struct drm_work_fence *wfence);
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence);
+
+#endif /* __DRM_WORK_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence
2026-09-02 13:49 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
@ 2026-09-02 13:49 ` Srinivasan Shanmugam
0 siblings, 0 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-09-02 13:49 UTC (permalink / raw)
To: linux-kernel
GPU drivers sometimes need to read a value from a userspace VA when a
dma-fence signals, compare it with an expected value, and only perform
the deferred work (e.g. eventfd_signal) if the comparison passes. This
is the per-signal filtering pattern used in AMDGPU's EOP eventfd path.
Add optional compare fields to drm_user_fence and a new helper
drm_user_fence_set_compare() to configure them. Supported operators are
==, !=, >=.
When cmp_op is set, drm_user_fence reads the value from userspace via
copy_from_user_nofault() and calls ops->worker() only if the comparison
passes. If the process MM is gone or the read fails, the worker is
skipped since the comparison cannot be performed.
Drivers that do not need filtering (e.g. XE) leave cmp_op unset and the
worker is called unconditionally — no behavioral change.
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: dri-devel@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
Change-Id: Ie9620ccccdb894512e7a11e7fb5e4544e5951ec4
---
drivers/gpu/drm/drm_user_fence.c | 93 +++++++++++++++++++++++++++++---
include/drm/drm_user_fence.h | 52 ++++++++++++++++--
2 files changed, 134 insertions(+), 11 deletions(-)
diff --git a/drivers/gpu/drm/drm_user_fence.c b/drivers/gpu/drm/drm_user_fence.c
index 0f229b7210a9..fcbdf40d1758 100644
--- a/drivers/gpu/drm/drm_user_fence.c
+++ b/drivers/gpu/drm/drm_user_fence.c
@@ -2,16 +2,25 @@
/*
* Copyright © 2024 The Linux Foundation
*
- * DRM user fence — extends drm_work_fence with kthread_use_mm() support.
+ * DRM user fence helper.
*
- * Use this when a GPU fence signals and work needs to access userspace
- * memory (copy_to_user, fault-able operations) from a kthread context.
- * For work that does not require userspace memory access, use
- * drm_work_fence directly.
+ * Extends drm_work_fence with the ability to access userspace memory
+ * from workqueue context by borrowing the process MM via kthread_use_mm().
+ *
+ * Drivers that need to write completion status to userspace (e.g., user
+ * fences, signaling eventfds) embed drm_user_fence and implement
+ * ops->worker() to do the actual write.
+ *
+ * Optionally, drivers may configure a per-signal compare via
+ * drm_user_fence_set_compare(): work is skipped unless the value at a
+ * userspace address matches the expected value at signal time.
*/
#include <linux/kthread.h>
+#include <linux/mm.h>
#include <linux/sched/mm.h>
+#include <linux/uaccess.h>
+#include <linux/workqueue.h>
#include <drm/drm_user_fence.h>
@@ -30,13 +39,42 @@ static void drm_user_fence_do_work(struct drm_work_fence *wfence)
struct drm_user_fence *ufence =
container_of(wfence, struct drm_user_fence, base);
struct mm_struct *mm = NULL;
+ bool call_worker = true;
if (mmget_not_zero(ufence->mm)) {
mm = ufence->mm;
kthread_use_mm(mm);
}
- ufence->ops->worker(ufence, !!mm);
+ if (ufence->cmp_op != DRM_USER_FENCE_CMP_NONE &&
+ !(wfence->fence && wfence->fence->error)) {
+ if (!mm) {
+ call_worker = false;
+ } else {
+ __le64 raw;
+
+ /*
+ * Use copy_from_user_nofault() to prevent a
+ * userfaultfd-registered page from blocking this
+ * workqueue thread indefinitely (DoS).
+ */
+ if (copy_from_user_nofault(&raw, ufence->cmp_addr,
+ sizeof(raw))) {
+ call_worker = false;
+ } else {
+ /* GPU writes LE; convert before comparing. */
+ u64 cur_val = le64_to_cpu(raw);
+
+ if (!drm_user_fence_cmp_match(cur_val,
+ ufence->cmp_value,
+ ufence->cmp_op))
+ call_worker = false;
+ }
+ }
+ }
+
+ if (call_worker)
+ ufence->ops->worker(ufence, !!mm);
if (mm) {
kthread_unuse_mm(mm);
@@ -63,8 +101,47 @@ void drm_user_fence_init(struct drm_user_fence *ufence,
const struct drm_user_fence_ops *ops)
{
drm_work_fence_init(&ufence->base, wq, &drm_user_fence_wfence_ops);
- ufence->mm = current->mm;
+ ufence->mm = current->mm;
mmgrab(ufence->mm);
- ufence->ops = ops;
+ ufence->ops = ops;
+ ufence->cmp_op = DRM_USER_FENCE_CMP_NONE;
+ ufence->cmp_addr = NULL;
+ ufence->cmp_value = 0;
}
EXPORT_SYMBOL_GPL(drm_user_fence_init);
+
+/**
+ * drm_user_fence_set_compare - Set per-signal compare filter
+ * @ufence: user fence
+ * @addr: 8-byte-aligned userspace address to read from at signal time
+ * @value: expected value to compare against
+ * @op: comparison operator; pass %DRM_USER_FENCE_CMP_NONE to disable
+ *
+ * When @op is not %DRM_USER_FENCE_CMP_NONE, the worker is only called
+ * if the value at @addr matches @value according to @op. If the MM is
+ * gone or the read fails, the worker is suppressed.
+ *
+ * Must only be called before drm_work_fence_add_callback().
+ */
+void drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op)
+{
+ /*
+ * get_user() of u64 is not atomic on 32-bit — caller should not
+ * reach here on non-64-bit kernels.
+ */
+ if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_64BIT)))
+ return;
+
+ if (op != DRM_USER_FENCE_CMP_NONE) {
+ if (WARN_ON_ONCE(!addr ||
+ !IS_ALIGNED((unsigned long)addr, sizeof(u64))))
+ return;
+ }
+
+ ufence->cmp_addr = addr;
+ ufence->cmp_value = value;
+ ufence->cmp_op = op;
+}
+EXPORT_SYMBOL_GPL(drm_user_fence_set_compare);
diff --git a/include/drm/drm_user_fence.h b/include/drm/drm_user_fence.h
index 9b92498e6009..56918928bb4f 100644
--- a/include/drm/drm_user_fence.h
+++ b/include/drm/drm_user_fence.h
@@ -20,8 +20,9 @@ struct drm_user_fence_ops {
* @worker: Called from workqueue context with the process MM active.
*
* If @mm_ok is true, kthread_use_mm() is active and userspace memory
- * may be accessed safely. If @mm_ok is false, the process MM was
- * already gone; skip the userspace write.
+ * (copy_to_user, etc.) may be accessed safely.
+ * If @mm_ok is false, the process MM was already gone; skip the
+ * userspace write.
*
* wake_up() or other post-signal housekeeping should also happen here.
*/
@@ -34,11 +35,23 @@ struct drm_user_fence_ops {
void (*destroy)(struct drm_user_fence *ufence);
};
+/**
+ * enum drm_user_fence_cmp - compare operator for per-signal filtering
+ */
+enum drm_user_fence_cmp {
+ DRM_USER_FENCE_CMP_NONE = 0,
+ DRM_USER_FENCE_CMP_EQ,
+ DRM_USER_FENCE_CMP_NEQ,
+ DRM_USER_FENCE_CMP_GTE,
+};
+
/**
* struct drm_user_fence - DRM user fence with MM borrowing
*
* Extends drm_work_fence with kthread_use_mm() support for drivers
* that need to access userspace memory when a GPU fence signals.
+ * For work that does not need userspace memory access, use
+ * drm_work_fence directly.
*
* Call drm_user_fence_init() at creation and drm_user_fence_add_callback()
* to arm on a dma-fence. Call drm_user_fence_cancel_sync() before teardown.
@@ -50,11 +63,20 @@ struct drm_user_fence {
struct mm_struct *mm;
/** @ops: Driver operations. */
const struct drm_user_fence_ops *ops;
+ /** @cmp_addr: Userspace address to read for per-signal compare. */
+ u64 __user *cmp_addr;
+ /** @cmp_value: Expected value for per-signal compare. */
+ u64 cmp_value;
+ /** @cmp_op: Compare operator; DRM_USER_FENCE_CMP_NONE disables. */
+ enum drm_user_fence_cmp cmp_op;
};
void drm_user_fence_init(struct drm_user_fence *ufence,
struct workqueue_struct *wq,
const struct drm_user_fence_ops *ops);
+void drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op);
/**
* drm_user_fence_get - Acquire a reference to a user fence
@@ -105,11 +127,35 @@ static inline bool drm_user_fence_cancel(struct drm_user_fence *ufence)
* drm_user_fence_cancel_sync - Cancel callback and wait for worker to finish
* @ufence: user fence
*
- * Must be called during teardown before freeing resources. May sleep.
+ * Must be called during teardown before freeing any resources accessed
+ * by ops->worker(). May sleep.
*/
static inline void drm_user_fence_cancel_sync(struct drm_user_fence *ufence)
{
drm_work_fence_cancel_sync(&ufence->base);
}
+/**
+ * drm_user_fence_cmp_match - Test a value against the compare filter
+ * @cur_val: value read from userspace (already converted from LE)
+ * @cmp_value: expected value
+ * @op: comparison operator
+ *
+ * Return: true if the comparison passes, false otherwise.
+ */
+static inline bool drm_user_fence_cmp_match(u64 cur_val, u64 cmp_value,
+ enum drm_user_fence_cmp op)
+{
+ switch (op) {
+ case DRM_USER_FENCE_CMP_EQ:
+ return cur_val == cmp_value;
+ case DRM_USER_FENCE_CMP_NEQ:
+ return cur_val != cmp_value;
+ case DRM_USER_FENCE_CMP_GTE:
+ return cur_val >= cmp_value;
+ default:
+ return false;
+ }
+}
+
#endif /* __DRM_USER_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 1/4] drm: Add drm_work_fence helper
@ 2026-08-31 14:56 Srinivasan Shanmugam
2026-08-31 14:56 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
0 siblings, 1 reply; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-08-31 14:56 UTC (permalink / raw)
To: linux-kernel
GPU drivers often need to queue work when a dma-fence signals
because certain operations (copy_to_user, eventfd_signal, memory
allocation) cannot run in IRQ context. This pattern is currently
open-coded in multiple drivers.
Introduce drm_work_fence — an embeddable base structure that handles
the dma-fence-callback-to-workqueue pattern in one place. Drivers
embed this in their own structure and implement ops->work() for the
deferred work and ops->destroy() for cleanup.
The helper manages:
- kref lifetime
- dma-fence callback registration
- workqueue dispatch on fence signal
- safe cancellation before driver teardown
For work that additionally requires borrowing the process MM via
kthread_use_mm(), see drm_user_fence which builds on top of this.
Suggested-by: Matthew Brost <matthew.brost@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
---
drivers/gpu/drm/Makefile | 1 +
drivers/gpu/drm/drm_work_fence.c | 195 +++++++++++++++++++++++++++++++
include/drm/drm_work_fence.h | 76 ++++++++++++
3 files changed, 272 insertions(+)
create mode 100644 drivers/gpu/drm/drm_work_fence.c
create mode 100644 include/drm/drm_work_fence.h
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index e97faabcd783..c5be8e80d0c8 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -72,6 +72,7 @@ drm-y := \
drm_vblank.o \
drm_vblank_work.o \
drm_vma_manager.o \
+ drm_work_fence.o \
drm_writeback.o
drm-$(CONFIG_DRM_CLIENT) += \
drm_client.o \
diff --git a/drivers/gpu/drm/drm_work_fence.c b/drivers/gpu/drm/drm_work_fence.c
new file mode 100644
index 000000000000..9f6b779d0fe9
--- /dev/null
+++ b/drivers/gpu/drm/drm_work_fence.c
@@ -0,0 +1,195 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 The Linux Foundation
+ *
+ * Common DRM work fence helper.
+ *
+ * When a GPU dma-fence signals, drivers often need to perform work that
+ * cannot run in IRQ context (e.g., memory allocation, copy_to_user,
+ * eventfd_signal). This helper queues a work item when a dma-fence
+ * signals, allowing that work to run safely in a workqueue context.
+ *
+ * NOTE: This helper consumes dma_fences but CANNOT implement
+ * dma_fence_ops. Work items queued here may sleep; dma_fence_ops
+ * callbacks are called under the fence spinlock and must not sleep.
+ *
+ * For work that additionally requires accessing userspace memory via
+ * kthread_use_mm(), see drm_user_fence which builds on top of this.
+ */
+
+#include <linux/workqueue.h>
+
+#include <drm/drm_work_fence.h>
+
+static void drm_work_fence_destroy(struct kref *kref)
+{
+ struct drm_work_fence *wfence =
+ container_of(kref, struct drm_work_fence, refcount);
+
+ if (wfence->fence)
+ dma_fence_put(wfence->fence);
+
+ wfence->ops->destroy(wfence);
+}
+
+/**
+ * drm_work_fence_get - Acquire a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_get(struct drm_work_fence *wfence)
+{
+ kref_get(&wfence->refcount);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_get);
+
+/**
+ * drm_work_fence_put - Release a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_put(struct drm_work_fence *wfence)
+{
+ kref_put(&wfence->refcount, drm_work_fence_destroy);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_put);
+
+static void drm_work_fence_work(struct work_struct *w)
+{
+ struct drm_work_fence *wfence =
+ container_of(w, struct drm_work_fence, work);
+
+ wfence->ops->work(wfence);
+ drm_work_fence_put(wfence);
+}
+
+static void drm_work_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
+{
+ struct drm_work_fence *wfence =
+ container_of(cb, struct drm_work_fence, cb);
+
+ queue_work(wfence->wq, &wfence->work);
+ /*
+ * Put the transferred reference from add_callback. The stored
+ * reference in wfence->fence is released in drm_work_fence_destroy().
+ */
+ dma_fence_put(fence);
+}
+
+/**
+ * drm_work_fence_init - Initialize a work fence
+ * @wfence: work fence to initialize
+ * @wq: workqueue to run the worker on (must be ordered if sequencing matters)
+ * @ops: driver operations
+ */
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops)
+{
+ kref_init(&wfence->refcount);
+ wfence->wq = wq;
+ wfence->ops = ops;
+ wfence->fence = NULL;
+ INIT_WORK(&wfence->work, drm_work_fence_work);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_init);
+
+/**
+ * drm_work_fence_add_callback - Attach a work fence to a dma-fence
+ * @wfence: work fence
+ * @fence: dma-fence to watch; ownership of this reference is transferred
+ * to the callback — caller must NOT put it afterward.
+ *
+ * When @fence signals, a work item is queued that calls ops->work().
+ * If @fence has already signaled, the work item is queued immediately.
+ *
+ * An additional reference to @fence is stored internally in @wfence to
+ * allow drm_work_fence_cancel() to be called safely without the caller
+ * needing to hold a separate fence reference.
+ *
+ * On any return value the caller's fence reference is consumed.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence)
+{
+ int err;
+
+ drm_work_fence_get(wfence);
+ wfence->fence = dma_fence_get(fence);
+
+ err = dma_fence_add_callback(fence, &wfence->cb, drm_work_fence_cb);
+ if (err == -ENOENT) {
+ queue_work(wfence->wq, &wfence->work);
+ dma_fence_put(fence);
+ err = 0;
+ } else if (err) {
+ dma_fence_put(wfence->fence);
+ wfence->fence = NULL;
+ drm_work_fence_put(wfence);
+ dma_fence_put(fence);
+ }
+ /* on success: transferred ref goes to drm_work_fence_cb */
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_add_callback);
+
+/**
+ * drm_work_fence_cancel - Cancel a pending work fence callback
+ * @wfence: work fence
+ *
+ * Attempts to remove the pending callback before driver context teardown.
+ * The caller must hold a reference to @wfence across this call.
+ *
+ * If the callback has already fired this returns false and all cleanup
+ * has been handled internally.
+ *
+ * If removal succeeds the callback reference is released internally.
+ * The caller must still release its own reference via drm_work_fence_put().
+ *
+ * This function is safe to call from atomic context as it only acquires
+ * the dma-fence spinlock internally. If the caller also needs to wait
+ * for the worker to finish, use drm_work_fence_cancel_sync() instead,
+ * which may sleep.
+ *
+ * Return: true if callback was removed, false if it had already fired.
+ */
+bool drm_work_fence_cancel(struct drm_work_fence *wfence)
+{
+ struct dma_fence *fence = wfence->fence;
+
+ if (!fence)
+ return false;
+
+ if (dma_fence_remove_callback(fence, &wfence->cb)) {
+ wfence->fence = NULL;
+ dma_fence_put(fence); /* callback ref */
+ dma_fence_put(fence); /* stored ref */
+ drm_work_fence_put(wfence);
+ return true;
+ }
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel);
+
+/**
+ * drm_work_fence_cancel_sync - Cancel callback and wait for worker to finish
+ * @wfence: work fence
+ *
+ * Calls drm_work_fence_cancel() then cancel_work_sync() to guarantee
+ * the worker has fully completed before returning.
+ *
+ * This function may sleep. Must not be called from atomic or interrupt
+ * context. Use drm_work_fence_cancel() instead when sleeping is not allowed.
+ *
+ * Drivers must call this during teardown before freeing any resources
+ * accessed by ops->work().
+ */
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence)
+{
+ drm_work_fence_cancel(wfence);
+ if (cancel_work_sync(&wfence->work))
+ drm_work_fence_put(wfence);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel_sync);
diff --git a/include/drm/drm_work_fence.h b/include/drm/drm_work_fence.h
new file mode 100644
index 000000000000..4fa369f937d7
--- /dev/null
+++ b/include/drm/drm_work_fence.h
@@ -0,0 +1,76 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 The Linux Foundation
+ */
+
+#ifndef __DRM_WORK_FENCE_H__
+#define __DRM_WORK_FENCE_H__
+
+#include <linux/dma-fence.h>
+#include <linux/kref.h>
+#include <linux/workqueue.h>
+
+struct drm_work_fence;
+
+/**
+ * struct drm_work_fence_ops - driver callbacks for a DRM work fence
+ */
+struct drm_work_fence_ops {
+ /**
+ * @work: Called from workqueue context when the dma-fence signals.
+ * Perform any work that cannot run in IRQ context here.
+ */
+ void (*work)(struct drm_work_fence *wfence);
+
+ /**
+ * @destroy: Called when the last reference is dropped.
+ * Free the containing structure here.
+ */
+ void (*destroy)(struct drm_work_fence *wfence);
+};
+
+/**
+ * struct drm_work_fence - embeddable DRM work fence
+ *
+ * Provides a dma-fence callback that queues a work item when the fence
+ * signals, allowing work that cannot run in IRQ context to be deferred
+ * to a workqueue. Drivers embed this in their own structure.
+ *
+ * NOTE: This helper is a *consumer* of dma_fences only. It CANNOT be
+ * used to implement dma_fence_ops. dma_fence callbacks are invoked
+ * while holding the fence spinlock; work queued here may sleep
+ * (copy_to_user, kthread_use_mm, eventfd_signal) and must not be
+ * called under that spinlock.
+ *
+ * Call drm_work_fence_init() at creation and drm_work_fence_add_callback()
+ * to arm. Call drm_work_fence_cancel_sync() before driver teardown.
+ */
+struct drm_work_fence {
+ /** @refcount: Reference count. */
+ struct kref refcount;
+ /** @work: Work item queued when the dma-fence signals. */
+ struct work_struct work;
+ /** @cb: dma-fence callback. */
+ struct dma_fence_cb cb;
+ /**
+ * @fence: Extra reference held for safe cancel(). Set during
+ * add_callback, released in destroy().
+ */
+ struct dma_fence *fence;
+ /** @wq: Workqueue to run @work on. */
+ struct workqueue_struct *wq;
+ /** @ops: Driver operations. */
+ const struct drm_work_fence_ops *ops;
+};
+
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops);
+void drm_work_fence_get(struct drm_work_fence *wfence);
+void drm_work_fence_put(struct drm_work_fence *wfence);
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence);
+bool drm_work_fence_cancel(struct drm_work_fence *wfence);
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence);
+
+#endif /* __DRM_WORK_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence
2026-08-31 14:56 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
@ 2026-08-31 14:56 ` Srinivasan Shanmugam
0 siblings, 0 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-08-31 14:56 UTC (permalink / raw)
To: linux-kernel
GPU drivers sometimes need to read a value from a userspace VA when a
dma-fence signals, compare it with an expected value, and only perform
the deferred work (e.g. eventfd_signal) if the comparison passes. This
is the per-signal filtering pattern used in AMDGPU's EOP eventfd path.
Add optional compare fields to drm_user_fence and a new helper
drm_user_fence_set_compare() to configure them. Supported operators
are ==, !=, >, >=, <, <=.
When cmp_addr is set, drm_user_fence reads the value from userspace
via get_user() and calls ops->worker() only if the comparison passes.
If the process MM is gone and cmp_addr is set, the worker is skipped
since the comparison cannot be performed.
Drivers that do not need filtering (e.g. XE) leave cmp_addr NULL and
the worker is called unconditionally — no behavioral change.
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: dri-devel@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
Change-Id: I03a726d5368674d06bd40fe7d11effd88c492741
---
drivers/gpu/drm/drm_user_fence.c | 91 +++++++++++++++++++++++++++++++-
include/drm/drm_user_fence.h | 29 ++++++++++
2 files changed, 119 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/drm_user_fence.c b/drivers/gpu/drm/drm_user_fence.c
index 664178e2d74c..e543b9349067 100644
--- a/drivers/gpu/drm/drm_user_fence.c
+++ b/drivers/gpu/drm/drm_user_fence.c
@@ -12,21 +12,74 @@
#include <linux/kthread.h>
#include <linux/sched/mm.h>
+#include <linux/uaccess.h>
#include <drm/drm_user_fence.h>
+static bool drm_user_fence_cmp_match(u64 cur_val, u64 expected,
+ enum drm_user_fence_cmp op)
+{
+ switch (op) {
+ case DRM_USER_FENCE_CMP_EQ:
+ return cur_val == expected;
+ case DRM_USER_FENCE_CMP_NE:
+ return cur_val != expected;
+ case DRM_USER_FENCE_CMP_GT:
+ return cur_val > expected;
+ case DRM_USER_FENCE_CMP_GE:
+ return cur_val >= expected;
+ case DRM_USER_FENCE_CMP_LT:
+ return cur_val < expected;
+ case DRM_USER_FENCE_CMP_LE:
+ return cur_val <= expected;
+ default:
+ return true;
+ }
+}
+
static void drm_user_fence_do_work(struct drm_work_fence *wfence)
{
struct drm_user_fence *ufence =
container_of(wfence, struct drm_user_fence, base);
bool mm_ok = false;
+ bool call_worker = true;
if (mmget_not_zero(ufence->mm)) {
kthread_use_mm(ufence->mm);
mm_ok = true;
}
- ufence->ops->worker(ufence, mm_ok);
+ /*
+ * Per-signal comparison: read a value from userspace and compare
+ * with the expected value. Skip ops->worker if the condition is
+ * not met. Drivers that do not need filtering leave cmp_op as
+ * DRM_USER_FENCE_CMP_NONE.
+ *
+ * If the fence signaled with an error (e.g. GPU hang), bypass the
+ * comparison entirely and always call the worker so userspace is
+ * notified rather than hanging indefinitely on a stale value.
+ *
+ * If the MM is gone and cmp_op is set we cannot perform the
+ * comparison, so skip the worker rather than calling it without
+ * having verified the condition.
+ */
+ if (ufence->cmp_op != DRM_USER_FENCE_CMP_NONE &&
+ !(wfence->fence && wfence->fence->error)) {
+ if (!mm_ok) {
+ call_worker = false;
+ } else {
+ u64 cur_val;
+
+ if (get_user(cur_val, ufence->cmp_addr) ||
+ !drm_user_fence_cmp_match(cur_val,
+ ufence->cmp_value,
+ ufence->cmp_op))
+ call_worker = false;
+ }
+ }
+
+ if (call_worker)
+ ufence->ops->worker(ufence, mm_ok);
if (mm_ok) {
kthread_unuse_mm(ufence->mm);
@@ -65,5 +118,41 @@ void drm_user_fence_init(struct drm_user_fence *ufence,
ufence->mm = current->mm;
mmgrab(ufence->mm);
ufence->ops = ops;
+ ufence->cmp_addr = NULL;
+ ufence->cmp_value = 0;
+ ufence->cmp_op = DRM_USER_FENCE_CMP_NONE;
}
EXPORT_SYMBOL_GPL(drm_user_fence_init);
+
+/**
+ * drm_user_fence_set_compare - Configure per-signal value comparison
+ * @ufence: user fence
+ * @addr: userspace VA to read when the fence signals
+ * @value: expected value to compare against
+ * @op: comparison operator (see &enum drm_user_fence_cmp)
+ *
+ * When set, drm_user_fence reads @addr via get_user() each time the
+ * fence signals and calls ops->worker() only if the comparison passes.
+ * If the fence signals with an error (e.g. GPU hang), the comparison
+ * is bypassed and ops->worker() is always called.
+ *
+ * Must be called after drm_user_fence_init() and before
+ * drm_user_fence_add_callback().
+ *
+ * Note: get_user() of a 64-bit value is not atomic on 32-bit systems.
+ * This function is only safe on 64-bit architectures.
+ */
+void drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op)
+{
+ WARN_ON_ONCE(!IS_ENABLED(CONFIG_64BIT));
+
+ if (op != DRM_USER_FENCE_CMP_NONE && !addr)
+ return;
+
+ ufence->cmp_addr = addr;
+ ufence->cmp_value = value;
+ ufence->cmp_op = op;
+}
+EXPORT_SYMBOL_GPL(drm_user_fence_set_compare);
diff --git a/include/drm/drm_user_fence.h b/include/drm/drm_user_fence.h
index 2b2b640f510f..764dd55ed3a2 100644
--- a/include/drm/drm_user_fence.h
+++ b/include/drm/drm_user_fence.h
@@ -33,6 +33,22 @@ struct drm_user_fence_ops {
void (*destroy)(struct drm_user_fence *ufence);
};
+/**
+ * enum drm_user_fence_cmp - comparison operators for per-signal filtering
+ *
+ * Used with drm_user_fence_set_compare() to control when ops->worker is
+ * called based on a value read from userspace.
+ */
+enum drm_user_fence_cmp {
+ DRM_USER_FENCE_CMP_NONE = 0, /* always call worker — default */
+ DRM_USER_FENCE_CMP_EQ, /* call worker if *addr == value */
+ DRM_USER_FENCE_CMP_NE, /* call worker if *addr != value */
+ DRM_USER_FENCE_CMP_GT, /* call worker if *addr > value */
+ DRM_USER_FENCE_CMP_GE, /* call worker if *addr >= value */
+ DRM_USER_FENCE_CMP_LT, /* call worker if *addr < value */
+ DRM_USER_FENCE_CMP_LE, /* call worker if *addr <= value */
+};
+
/**
* struct drm_user_fence - DRM user fence with MM borrowing
*
@@ -51,11 +67,24 @@ struct drm_user_fence {
struct mm_struct *mm;
/** @ops: Driver operations. */
const struct drm_user_fence_ops *ops;
+ /**
+ * @cmp_addr: Userspace VA to read for per-signal comparison.
+ * NULL means always call ops->worker (default XE behavior).
+ * Set via drm_user_fence_set_compare().
+ */
+ u64 __user *cmp_addr;
+ /** @cmp_value: Expected value for comparison. */
+ u64 cmp_value;
+ /** @cmp_op: Comparison operator. */
+ enum drm_user_fence_cmp cmp_op;
};
void drm_user_fence_init(struct drm_user_fence *ufence,
struct workqueue_struct *wq,
const struct drm_user_fence_ops *ops);
+void drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op);
static inline void drm_user_fence_get(struct drm_user_fence *ufence)
{
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 1/4] drm: Add drm_work_fence helper
@ 2026-08-30 5:56 Srinivasan Shanmugam
2026-08-30 5:56 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
0 siblings, 1 reply; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-08-30 5:56 UTC (permalink / raw)
To: linux-kernel
GPU drivers often need to queue work when a dma-fence signals
because certain operations (copy_to_user, eventfd_signal, memory
allocation) cannot run in IRQ context. This pattern is currently
open-coded in multiple drivers.
Introduce drm_work_fence — an embeddable base structure that handles
the dma-fence-callback-to-workqueue pattern in one place. Drivers
embed this in their own structure and implement ops->work() for the
deferred work and ops->destroy() for cleanup.
The helper manages:
- kref lifetime
- dma-fence callback registration
- workqueue dispatch on fence signal
- safe cancellation before driver teardown
For work that additionally requires borrowing the process MM via
kthread_use_mm(), see drm_user_fence which builds on top of this.
Suggested-by: Matthew Brost <matthew.brost@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
---
drivers/gpu/drm/Makefile | 1 +
drivers/gpu/drm/drm_work_fence.c | 195 +++++++++++++++++++++++++++++++
include/drm/drm_work_fence.h | 76 ++++++++++++
3 files changed, 272 insertions(+)
create mode 100644 drivers/gpu/drm/drm_work_fence.c
create mode 100644 include/drm/drm_work_fence.h
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index e97faabcd783..c5be8e80d0c8 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -72,6 +72,7 @@ drm-y := \
drm_vblank.o \
drm_vblank_work.o \
drm_vma_manager.o \
+ drm_work_fence.o \
drm_writeback.o
drm-$(CONFIG_DRM_CLIENT) += \
drm_client.o \
diff --git a/drivers/gpu/drm/drm_work_fence.c b/drivers/gpu/drm/drm_work_fence.c
new file mode 100644
index 000000000000..9f6b779d0fe9
--- /dev/null
+++ b/drivers/gpu/drm/drm_work_fence.c
@@ -0,0 +1,195 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 The Linux Foundation
+ *
+ * Common DRM work fence helper.
+ *
+ * When a GPU dma-fence signals, drivers often need to perform work that
+ * cannot run in IRQ context (e.g., memory allocation, copy_to_user,
+ * eventfd_signal). This helper queues a work item when a dma-fence
+ * signals, allowing that work to run safely in a workqueue context.
+ *
+ * NOTE: This helper consumes dma_fences but CANNOT implement
+ * dma_fence_ops. Work items queued here may sleep; dma_fence_ops
+ * callbacks are called under the fence spinlock and must not sleep.
+ *
+ * For work that additionally requires accessing userspace memory via
+ * kthread_use_mm(), see drm_user_fence which builds on top of this.
+ */
+
+#include <linux/workqueue.h>
+
+#include <drm/drm_work_fence.h>
+
+static void drm_work_fence_destroy(struct kref *kref)
+{
+ struct drm_work_fence *wfence =
+ container_of(kref, struct drm_work_fence, refcount);
+
+ if (wfence->fence)
+ dma_fence_put(wfence->fence);
+
+ wfence->ops->destroy(wfence);
+}
+
+/**
+ * drm_work_fence_get - Acquire a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_get(struct drm_work_fence *wfence)
+{
+ kref_get(&wfence->refcount);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_get);
+
+/**
+ * drm_work_fence_put - Release a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_put(struct drm_work_fence *wfence)
+{
+ kref_put(&wfence->refcount, drm_work_fence_destroy);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_put);
+
+static void drm_work_fence_work(struct work_struct *w)
+{
+ struct drm_work_fence *wfence =
+ container_of(w, struct drm_work_fence, work);
+
+ wfence->ops->work(wfence);
+ drm_work_fence_put(wfence);
+}
+
+static void drm_work_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
+{
+ struct drm_work_fence *wfence =
+ container_of(cb, struct drm_work_fence, cb);
+
+ queue_work(wfence->wq, &wfence->work);
+ /*
+ * Put the transferred reference from add_callback. The stored
+ * reference in wfence->fence is released in drm_work_fence_destroy().
+ */
+ dma_fence_put(fence);
+}
+
+/**
+ * drm_work_fence_init - Initialize a work fence
+ * @wfence: work fence to initialize
+ * @wq: workqueue to run the worker on (must be ordered if sequencing matters)
+ * @ops: driver operations
+ */
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops)
+{
+ kref_init(&wfence->refcount);
+ wfence->wq = wq;
+ wfence->ops = ops;
+ wfence->fence = NULL;
+ INIT_WORK(&wfence->work, drm_work_fence_work);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_init);
+
+/**
+ * drm_work_fence_add_callback - Attach a work fence to a dma-fence
+ * @wfence: work fence
+ * @fence: dma-fence to watch; ownership of this reference is transferred
+ * to the callback — caller must NOT put it afterward.
+ *
+ * When @fence signals, a work item is queued that calls ops->work().
+ * If @fence has already signaled, the work item is queued immediately.
+ *
+ * An additional reference to @fence is stored internally in @wfence to
+ * allow drm_work_fence_cancel() to be called safely without the caller
+ * needing to hold a separate fence reference.
+ *
+ * On any return value the caller's fence reference is consumed.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence)
+{
+ int err;
+
+ drm_work_fence_get(wfence);
+ wfence->fence = dma_fence_get(fence);
+
+ err = dma_fence_add_callback(fence, &wfence->cb, drm_work_fence_cb);
+ if (err == -ENOENT) {
+ queue_work(wfence->wq, &wfence->work);
+ dma_fence_put(fence);
+ err = 0;
+ } else if (err) {
+ dma_fence_put(wfence->fence);
+ wfence->fence = NULL;
+ drm_work_fence_put(wfence);
+ dma_fence_put(fence);
+ }
+ /* on success: transferred ref goes to drm_work_fence_cb */
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_add_callback);
+
+/**
+ * drm_work_fence_cancel - Cancel a pending work fence callback
+ * @wfence: work fence
+ *
+ * Attempts to remove the pending callback before driver context teardown.
+ * The caller must hold a reference to @wfence across this call.
+ *
+ * If the callback has already fired this returns false and all cleanup
+ * has been handled internally.
+ *
+ * If removal succeeds the callback reference is released internally.
+ * The caller must still release its own reference via drm_work_fence_put().
+ *
+ * This function is safe to call from atomic context as it only acquires
+ * the dma-fence spinlock internally. If the caller also needs to wait
+ * for the worker to finish, use drm_work_fence_cancel_sync() instead,
+ * which may sleep.
+ *
+ * Return: true if callback was removed, false if it had already fired.
+ */
+bool drm_work_fence_cancel(struct drm_work_fence *wfence)
+{
+ struct dma_fence *fence = wfence->fence;
+
+ if (!fence)
+ return false;
+
+ if (dma_fence_remove_callback(fence, &wfence->cb)) {
+ wfence->fence = NULL;
+ dma_fence_put(fence); /* callback ref */
+ dma_fence_put(fence); /* stored ref */
+ drm_work_fence_put(wfence);
+ return true;
+ }
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel);
+
+/**
+ * drm_work_fence_cancel_sync - Cancel callback and wait for worker to finish
+ * @wfence: work fence
+ *
+ * Calls drm_work_fence_cancel() then cancel_work_sync() to guarantee
+ * the worker has fully completed before returning.
+ *
+ * This function may sleep. Must not be called from atomic or interrupt
+ * context. Use drm_work_fence_cancel() instead when sleeping is not allowed.
+ *
+ * Drivers must call this during teardown before freeing any resources
+ * accessed by ops->work().
+ */
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence)
+{
+ drm_work_fence_cancel(wfence);
+ if (cancel_work_sync(&wfence->work))
+ drm_work_fence_put(wfence);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel_sync);
diff --git a/include/drm/drm_work_fence.h b/include/drm/drm_work_fence.h
new file mode 100644
index 000000000000..4fa369f937d7
--- /dev/null
+++ b/include/drm/drm_work_fence.h
@@ -0,0 +1,76 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 The Linux Foundation
+ */
+
+#ifndef __DRM_WORK_FENCE_H__
+#define __DRM_WORK_FENCE_H__
+
+#include <linux/dma-fence.h>
+#include <linux/kref.h>
+#include <linux/workqueue.h>
+
+struct drm_work_fence;
+
+/**
+ * struct drm_work_fence_ops - driver callbacks for a DRM work fence
+ */
+struct drm_work_fence_ops {
+ /**
+ * @work: Called from workqueue context when the dma-fence signals.
+ * Perform any work that cannot run in IRQ context here.
+ */
+ void (*work)(struct drm_work_fence *wfence);
+
+ /**
+ * @destroy: Called when the last reference is dropped.
+ * Free the containing structure here.
+ */
+ void (*destroy)(struct drm_work_fence *wfence);
+};
+
+/**
+ * struct drm_work_fence - embeddable DRM work fence
+ *
+ * Provides a dma-fence callback that queues a work item when the fence
+ * signals, allowing work that cannot run in IRQ context to be deferred
+ * to a workqueue. Drivers embed this in their own structure.
+ *
+ * NOTE: This helper is a *consumer* of dma_fences only. It CANNOT be
+ * used to implement dma_fence_ops. dma_fence callbacks are invoked
+ * while holding the fence spinlock; work queued here may sleep
+ * (copy_to_user, kthread_use_mm, eventfd_signal) and must not be
+ * called under that spinlock.
+ *
+ * Call drm_work_fence_init() at creation and drm_work_fence_add_callback()
+ * to arm. Call drm_work_fence_cancel_sync() before driver teardown.
+ */
+struct drm_work_fence {
+ /** @refcount: Reference count. */
+ struct kref refcount;
+ /** @work: Work item queued when the dma-fence signals. */
+ struct work_struct work;
+ /** @cb: dma-fence callback. */
+ struct dma_fence_cb cb;
+ /**
+ * @fence: Extra reference held for safe cancel(). Set during
+ * add_callback, released in destroy().
+ */
+ struct dma_fence *fence;
+ /** @wq: Workqueue to run @work on. */
+ struct workqueue_struct *wq;
+ /** @ops: Driver operations. */
+ const struct drm_work_fence_ops *ops;
+};
+
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops);
+void drm_work_fence_get(struct drm_work_fence *wfence);
+void drm_work_fence_put(struct drm_work_fence *wfence);
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence);
+bool drm_work_fence_cancel(struct drm_work_fence *wfence);
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence);
+
+#endif /* __DRM_WORK_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence
2026-08-30 5:56 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
@ 2026-08-30 5:56 ` Srinivasan Shanmugam
0 siblings, 0 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-08-30 5:56 UTC (permalink / raw)
To: linux-kernel
GPU drivers sometimes need to read a value from a userspace VA when a
dma-fence signals, compare it with an expected value, and only perform
the deferred work (e.g. eventfd_signal) if the comparison passes. This
is the per-signal filtering pattern used in AMDGPU's EOP eventfd path.
Add optional compare fields to drm_user_fence and a new helper
drm_user_fence_set_compare() to configure them. Supported operators
are ==, !=, >, >=, <, <=.
When cmp_addr is set, drm_user_fence reads the value from userspace
via get_user() and calls ops->worker() only if the comparison passes.
If the process MM is gone and cmp_addr is set, the worker is skipped
since the comparison cannot be performed.
Drivers that do not need filtering (e.g. XE) leave cmp_addr NULL and
the worker is called unconditionally — no behavioral change.
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: dri-devel@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
Change-Id: I44fe8ec50babaa140dd875c00e249e0f6edc0551
---
drivers/gpu/drm/drm_user_fence.c | 80 +++++++++++++++++++++++++++++++-
include/drm/drm_user_fence.h | 29 ++++++++++++
2 files changed, 108 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/drm_user_fence.c b/drivers/gpu/drm/drm_user_fence.c
index 664178e2d74c..9e141533622e 100644
--- a/drivers/gpu/drm/drm_user_fence.c
+++ b/drivers/gpu/drm/drm_user_fence.c
@@ -12,21 +12,68 @@
#include <linux/kthread.h>
#include <linux/sched/mm.h>
+#include <linux/uaccess.h>
#include <drm/drm_user_fence.h>
+static bool drm_user_fence_cmp_match(u64 cur_val, u64 expected,
+ enum drm_user_fence_cmp op)
+{
+ switch (op) {
+ case DRM_USER_FENCE_CMP_EQ:
+ return cur_val == expected;
+ case DRM_USER_FENCE_CMP_NE:
+ return cur_val != expected;
+ case DRM_USER_FENCE_CMP_GT:
+ return cur_val > expected;
+ case DRM_USER_FENCE_CMP_GE:
+ return cur_val >= expected;
+ case DRM_USER_FENCE_CMP_LT:
+ return cur_val < expected;
+ case DRM_USER_FENCE_CMP_LE:
+ return cur_val <= expected;
+ default:
+ return true;
+ }
+}
+
static void drm_user_fence_do_work(struct drm_work_fence *wfence)
{
struct drm_user_fence *ufence =
container_of(wfence, struct drm_user_fence, base);
bool mm_ok = false;
+ bool call_worker = true;
if (mmget_not_zero(ufence->mm)) {
kthread_use_mm(ufence->mm);
mm_ok = true;
}
- ufence->ops->worker(ufence, mm_ok);
+ /*
+ * Per-signal comparison: read a value from userspace and compare
+ * with the expected value. Skip ops->worker if the condition is
+ * not met. Drivers that do not need filtering leave cmp_addr NULL.
+ *
+ * If the MM is gone and cmp_addr is set we cannot perform the
+ * comparison, so skip the worker rather than calling it without
+ * having verified the condition.
+ */
+ if (ufence->cmp_op != DRM_USER_FENCE_CMP_NONE) {
+ if (!mm_ok) {
+ call_worker = false;
+ } else {
+ u64 cur_val;
+
+ if (get_user(cur_val, ufence->cmp_addr) ||
+ !drm_user_fence_cmp_match(cur_val,
+ ufence->cmp_value,
+ ufence->cmp_op))
+ call_worker = false;
+ }
+ }
+
+ if (call_worker)
+ ufence->ops->worker(ufence, mm_ok);
if (mm_ok) {
kthread_unuse_mm(ufence->mm);
@@ -65,5 +112,36 @@ void drm_user_fence_init(struct drm_user_fence *ufence,
ufence->mm = current->mm;
mmgrab(ufence->mm);
ufence->ops = ops;
+ ufence->cmp_addr = NULL;
+ ufence->cmp_value = 0;
+ ufence->cmp_op = DRM_USER_FENCE_CMP_NONE;
}
EXPORT_SYMBOL_GPL(drm_user_fence_init);
+
+/**
+ * drm_user_fence_set_compare - Configure per-signal value comparison
+ * @ufence: user fence
+ * @addr: userspace VA to read when the fence signals
+ * @value: expected value to compare against
+ * @op: comparison operator (see &enum drm_user_fence_cmp)
+ *
+ * When set, drm_user_fence reads @addr via get_user() each time the
+ * fence signals and calls ops->worker() only if the comparison passes.
+ * This enables per-signal filtering without open-coding the read+compare
+ * pattern in each driver.
+ *
+ * Must be called after drm_user_fence_init() and before
+ * drm_user_fence_add_callback().
+ */
+void drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op)
+{
+ if (WARN_ON(op != DRM_USER_FENCE_CMP_NONE && !addr))
+ return;
+
+ ufence->cmp_addr = addr;
+ ufence->cmp_value = value;
+ ufence->cmp_op = op;
+}
+EXPORT_SYMBOL_GPL(drm_user_fence_set_compare);
diff --git a/include/drm/drm_user_fence.h b/include/drm/drm_user_fence.h
index 2b2b640f510f..764dd55ed3a2 100644
--- a/include/drm/drm_user_fence.h
+++ b/include/drm/drm_user_fence.h
@@ -33,6 +33,22 @@ struct drm_user_fence_ops {
void (*destroy)(struct drm_user_fence *ufence);
};
+/**
+ * enum drm_user_fence_cmp - comparison operators for per-signal filtering
+ *
+ * Used with drm_user_fence_set_compare() to control when ops->worker is
+ * called based on a value read from userspace.
+ */
+enum drm_user_fence_cmp {
+ DRM_USER_FENCE_CMP_NONE = 0, /* always call worker — default */
+ DRM_USER_FENCE_CMP_EQ, /* call worker if *addr == value */
+ DRM_USER_FENCE_CMP_NE, /* call worker if *addr != value */
+ DRM_USER_FENCE_CMP_GT, /* call worker if *addr > value */
+ DRM_USER_FENCE_CMP_GE, /* call worker if *addr >= value */
+ DRM_USER_FENCE_CMP_LT, /* call worker if *addr < value */
+ DRM_USER_FENCE_CMP_LE, /* call worker if *addr <= value */
+};
+
/**
* struct drm_user_fence - DRM user fence with MM borrowing
*
@@ -51,11 +67,24 @@ struct drm_user_fence {
struct mm_struct *mm;
/** @ops: Driver operations. */
const struct drm_user_fence_ops *ops;
+ /**
+ * @cmp_addr: Userspace VA to read for per-signal comparison.
+ * NULL means always call ops->worker (default XE behavior).
+ * Set via drm_user_fence_set_compare().
+ */
+ u64 __user *cmp_addr;
+ /** @cmp_value: Expected value for comparison. */
+ u64 cmp_value;
+ /** @cmp_op: Comparison operator. */
+ enum drm_user_fence_cmp cmp_op;
};
void drm_user_fence_init(struct drm_user_fence *ufence,
struct workqueue_struct *wq,
const struct drm_user_fence_ops *ops);
+void drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op);
static inline void drm_user_fence_get(struct drm_user_fence *ufence)
{
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 1/4] drm: Add drm_work_fence helper
@ 2026-08-30 3:12 Srinivasan Shanmugam
2026-08-30 3:12 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
0 siblings, 1 reply; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-08-30 3:12 UTC (permalink / raw)
To: linux-kernel
GPU drivers often need to queue work when a dma-fence signals
because certain operations (copy_to_user, eventfd_signal, memory
allocation) cannot run in IRQ context. This pattern is currently
open-coded in multiple drivers.
Introduce drm_work_fence — an embeddable base structure that handles
the dma-fence-callback-to-workqueue pattern in one place. Drivers
embed this in their own structure and implement ops->work() for the
deferred work and ops->destroy() for cleanup.
The helper manages:
- kref lifetime
- dma-fence callback registration
- workqueue dispatch on fence signal
- safe cancellation before driver teardown
For work that additionally requires borrowing the process MM via
kthread_use_mm(), see drm_user_fence which builds on top of this.
Suggested-by: Matthew Brost <matthew.brost@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
---
drivers/gpu/drm/Makefile | 1 +
drivers/gpu/drm/drm_work_fence.c | 195 +++++++++++++++++++++++++++++++
include/drm/drm_work_fence.h | 76 ++++++++++++
3 files changed, 272 insertions(+)
create mode 100644 drivers/gpu/drm/drm_work_fence.c
create mode 100644 include/drm/drm_work_fence.h
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index e97faabcd783..c5be8e80d0c8 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -72,6 +72,7 @@ drm-y := \
drm_vblank.o \
drm_vblank_work.o \
drm_vma_manager.o \
+ drm_work_fence.o \
drm_writeback.o
drm-$(CONFIG_DRM_CLIENT) += \
drm_client.o \
diff --git a/drivers/gpu/drm/drm_work_fence.c b/drivers/gpu/drm/drm_work_fence.c
new file mode 100644
index 000000000000..9f6b779d0fe9
--- /dev/null
+++ b/drivers/gpu/drm/drm_work_fence.c
@@ -0,0 +1,195 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 The Linux Foundation
+ *
+ * Common DRM work fence helper.
+ *
+ * When a GPU dma-fence signals, drivers often need to perform work that
+ * cannot run in IRQ context (e.g., memory allocation, copy_to_user,
+ * eventfd_signal). This helper queues a work item when a dma-fence
+ * signals, allowing that work to run safely in a workqueue context.
+ *
+ * NOTE: This helper consumes dma_fences but CANNOT implement
+ * dma_fence_ops. Work items queued here may sleep; dma_fence_ops
+ * callbacks are called under the fence spinlock and must not sleep.
+ *
+ * For work that additionally requires accessing userspace memory via
+ * kthread_use_mm(), see drm_user_fence which builds on top of this.
+ */
+
+#include <linux/workqueue.h>
+
+#include <drm/drm_work_fence.h>
+
+static void drm_work_fence_destroy(struct kref *kref)
+{
+ struct drm_work_fence *wfence =
+ container_of(kref, struct drm_work_fence, refcount);
+
+ if (wfence->fence)
+ dma_fence_put(wfence->fence);
+
+ wfence->ops->destroy(wfence);
+}
+
+/**
+ * drm_work_fence_get - Acquire a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_get(struct drm_work_fence *wfence)
+{
+ kref_get(&wfence->refcount);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_get);
+
+/**
+ * drm_work_fence_put - Release a reference to a work fence
+ * @wfence: work fence
+ */
+void drm_work_fence_put(struct drm_work_fence *wfence)
+{
+ kref_put(&wfence->refcount, drm_work_fence_destroy);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_put);
+
+static void drm_work_fence_work(struct work_struct *w)
+{
+ struct drm_work_fence *wfence =
+ container_of(w, struct drm_work_fence, work);
+
+ wfence->ops->work(wfence);
+ drm_work_fence_put(wfence);
+}
+
+static void drm_work_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
+{
+ struct drm_work_fence *wfence =
+ container_of(cb, struct drm_work_fence, cb);
+
+ queue_work(wfence->wq, &wfence->work);
+ /*
+ * Put the transferred reference from add_callback. The stored
+ * reference in wfence->fence is released in drm_work_fence_destroy().
+ */
+ dma_fence_put(fence);
+}
+
+/**
+ * drm_work_fence_init - Initialize a work fence
+ * @wfence: work fence to initialize
+ * @wq: workqueue to run the worker on (must be ordered if sequencing matters)
+ * @ops: driver operations
+ */
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops)
+{
+ kref_init(&wfence->refcount);
+ wfence->wq = wq;
+ wfence->ops = ops;
+ wfence->fence = NULL;
+ INIT_WORK(&wfence->work, drm_work_fence_work);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_init);
+
+/**
+ * drm_work_fence_add_callback - Attach a work fence to a dma-fence
+ * @wfence: work fence
+ * @fence: dma-fence to watch; ownership of this reference is transferred
+ * to the callback — caller must NOT put it afterward.
+ *
+ * When @fence signals, a work item is queued that calls ops->work().
+ * If @fence has already signaled, the work item is queued immediately.
+ *
+ * An additional reference to @fence is stored internally in @wfence to
+ * allow drm_work_fence_cancel() to be called safely without the caller
+ * needing to hold a separate fence reference.
+ *
+ * On any return value the caller's fence reference is consumed.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence)
+{
+ int err;
+
+ drm_work_fence_get(wfence);
+ wfence->fence = dma_fence_get(fence);
+
+ err = dma_fence_add_callback(fence, &wfence->cb, drm_work_fence_cb);
+ if (err == -ENOENT) {
+ queue_work(wfence->wq, &wfence->work);
+ dma_fence_put(fence);
+ err = 0;
+ } else if (err) {
+ dma_fence_put(wfence->fence);
+ wfence->fence = NULL;
+ drm_work_fence_put(wfence);
+ dma_fence_put(fence);
+ }
+ /* on success: transferred ref goes to drm_work_fence_cb */
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_add_callback);
+
+/**
+ * drm_work_fence_cancel - Cancel a pending work fence callback
+ * @wfence: work fence
+ *
+ * Attempts to remove the pending callback before driver context teardown.
+ * The caller must hold a reference to @wfence across this call.
+ *
+ * If the callback has already fired this returns false and all cleanup
+ * has been handled internally.
+ *
+ * If removal succeeds the callback reference is released internally.
+ * The caller must still release its own reference via drm_work_fence_put().
+ *
+ * This function is safe to call from atomic context as it only acquires
+ * the dma-fence spinlock internally. If the caller also needs to wait
+ * for the worker to finish, use drm_work_fence_cancel_sync() instead,
+ * which may sleep.
+ *
+ * Return: true if callback was removed, false if it had already fired.
+ */
+bool drm_work_fence_cancel(struct drm_work_fence *wfence)
+{
+ struct dma_fence *fence = wfence->fence;
+
+ if (!fence)
+ return false;
+
+ if (dma_fence_remove_callback(fence, &wfence->cb)) {
+ wfence->fence = NULL;
+ dma_fence_put(fence); /* callback ref */
+ dma_fence_put(fence); /* stored ref */
+ drm_work_fence_put(wfence);
+ return true;
+ }
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel);
+
+/**
+ * drm_work_fence_cancel_sync - Cancel callback and wait for worker to finish
+ * @wfence: work fence
+ *
+ * Calls drm_work_fence_cancel() then cancel_work_sync() to guarantee
+ * the worker has fully completed before returning.
+ *
+ * This function may sleep. Must not be called from atomic or interrupt
+ * context. Use drm_work_fence_cancel() instead when sleeping is not allowed.
+ *
+ * Drivers must call this during teardown before freeing any resources
+ * accessed by ops->work().
+ */
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence)
+{
+ drm_work_fence_cancel(wfence);
+ if (cancel_work_sync(&wfence->work))
+ drm_work_fence_put(wfence);
+}
+EXPORT_SYMBOL_GPL(drm_work_fence_cancel_sync);
diff --git a/include/drm/drm_work_fence.h b/include/drm/drm_work_fence.h
new file mode 100644
index 000000000000..4fa369f937d7
--- /dev/null
+++ b/include/drm/drm_work_fence.h
@@ -0,0 +1,76 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 The Linux Foundation
+ */
+
+#ifndef __DRM_WORK_FENCE_H__
+#define __DRM_WORK_FENCE_H__
+
+#include <linux/dma-fence.h>
+#include <linux/kref.h>
+#include <linux/workqueue.h>
+
+struct drm_work_fence;
+
+/**
+ * struct drm_work_fence_ops - driver callbacks for a DRM work fence
+ */
+struct drm_work_fence_ops {
+ /**
+ * @work: Called from workqueue context when the dma-fence signals.
+ * Perform any work that cannot run in IRQ context here.
+ */
+ void (*work)(struct drm_work_fence *wfence);
+
+ /**
+ * @destroy: Called when the last reference is dropped.
+ * Free the containing structure here.
+ */
+ void (*destroy)(struct drm_work_fence *wfence);
+};
+
+/**
+ * struct drm_work_fence - embeddable DRM work fence
+ *
+ * Provides a dma-fence callback that queues a work item when the fence
+ * signals, allowing work that cannot run in IRQ context to be deferred
+ * to a workqueue. Drivers embed this in their own structure.
+ *
+ * NOTE: This helper is a *consumer* of dma_fences only. It CANNOT be
+ * used to implement dma_fence_ops. dma_fence callbacks are invoked
+ * while holding the fence spinlock; work queued here may sleep
+ * (copy_to_user, kthread_use_mm, eventfd_signal) and must not be
+ * called under that spinlock.
+ *
+ * Call drm_work_fence_init() at creation and drm_work_fence_add_callback()
+ * to arm. Call drm_work_fence_cancel_sync() before driver teardown.
+ */
+struct drm_work_fence {
+ /** @refcount: Reference count. */
+ struct kref refcount;
+ /** @work: Work item queued when the dma-fence signals. */
+ struct work_struct work;
+ /** @cb: dma-fence callback. */
+ struct dma_fence_cb cb;
+ /**
+ * @fence: Extra reference held for safe cancel(). Set during
+ * add_callback, released in destroy().
+ */
+ struct dma_fence *fence;
+ /** @wq: Workqueue to run @work on. */
+ struct workqueue_struct *wq;
+ /** @ops: Driver operations. */
+ const struct drm_work_fence_ops *ops;
+};
+
+void drm_work_fence_init(struct drm_work_fence *wfence,
+ struct workqueue_struct *wq,
+ const struct drm_work_fence_ops *ops);
+void drm_work_fence_get(struct drm_work_fence *wfence);
+void drm_work_fence_put(struct drm_work_fence *wfence);
+int drm_work_fence_add_callback(struct drm_work_fence *wfence,
+ struct dma_fence *fence);
+bool drm_work_fence_cancel(struct drm_work_fence *wfence);
+void drm_work_fence_cancel_sync(struct drm_work_fence *wfence);
+
+#endif /* __DRM_WORK_FENCE_H__ */
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence
2026-08-30 3:12 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
@ 2026-08-30 3:12 ` Srinivasan Shanmugam
0 siblings, 0 replies; 11+ messages in thread
From: Srinivasan Shanmugam @ 2026-08-30 3:12 UTC (permalink / raw)
To: linux-kernel
GPU drivers sometimes need to read a value from a userspace VA when a
dma-fence signals, compare it with an expected value, and only perform
the deferred work (e.g. eventfd_signal) if the comparison passes. This
is the per-signal filtering pattern used in AMDGPU's EOP eventfd path.
Add optional compare fields to drm_user_fence and a new helper
drm_user_fence_set_compare() to configure them. Supported operators
are ==, !=, >, >=, <, <=.
When cmp_addr is set, drm_user_fence reads the value from userspace
via get_user() and calls ops->worker() only if the comparison passes.
If the process MM is gone and cmp_addr is set, the worker is skipped
since the comparison cannot be performed.
Drivers that do not need filtering (e.g. XE) leave cmp_addr NULL and
the worker is called unconditionally — no behavioral change.
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: dri-devel@lists.freedesktop.org
Cc: amd-gfx@lists.freedesktop.org
Signed-off-by: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>
---
drivers/gpu/drm/drm_user_fence.c | 77 +++++++++++++++++++++++++++++++-
include/drm/drm_user_fence.h | 29 ++++++++++++
2 files changed, 105 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/drm_user_fence.c b/drivers/gpu/drm/drm_user_fence.c
index 9f6327406d3e..3e429eab7d99 100644
--- a/drivers/gpu/drm/drm_user_fence.c
+++ b/drivers/gpu/drm/drm_user_fence.c
@@ -12,21 +12,68 @@
#include <linux/kthread.h>
#include <linux/sched/mm.h>
+#include <linux/uaccess.h>
#include <drm/drm_user_fence.h>
+static bool drm_user_fence_cmp_match(u64 cur_val, u64 expected,
+ enum drm_user_fence_cmp op)
+{
+ switch (op) {
+ case DRM_USER_FENCE_CMP_EQ:
+ return cur_val == expected;
+ case DRM_USER_FENCE_CMP_NE:
+ return cur_val != expected;
+ case DRM_USER_FENCE_CMP_GT:
+ return cur_val > expected;
+ case DRM_USER_FENCE_CMP_GE:
+ return cur_val >= expected;
+ case DRM_USER_FENCE_CMP_LT:
+ return cur_val < expected;
+ case DRM_USER_FENCE_CMP_LE:
+ return cur_val <= expected;
+ default:
+ return true;
+ }
+}
+
static void drm_user_fence_do_work(struct drm_work_fence *wfence)
{
struct drm_user_fence *ufence =
container_of(wfence, struct drm_user_fence, base);
bool mm_ok = false;
+ bool call_worker = true;
if (mmget_not_zero(ufence->mm)) {
kthread_use_mm(ufence->mm);
mm_ok = true;
}
- ufence->ops->worker(ufence, mm_ok);
+ /*
+ * Per-signal comparison: read a value from userspace and compare
+ * with the expected value. Skip ops->worker if the condition is
+ * not met. Drivers that do not need filtering leave cmp_addr NULL.
+ *
+ * If the MM is gone and cmp_addr is set we cannot perform the
+ * comparison, so skip the worker rather than calling it without
+ * having verified the condition.
+ */
+ if (ufence->cmp_addr) {
+ if (!mm_ok) {
+ call_worker = false;
+ } else {
+ u64 cur_val;
+
+ if (get_user(cur_val, ufence->cmp_addr) ||
+ !drm_user_fence_cmp_match(cur_val,
+ ufence->cmp_value,
+ ufence->cmp_op))
+ call_worker = false;
+ }
+ }
+
+ if (call_worker)
+ ufence->ops->worker(ufence, mm_ok);
if (mm_ok) {
kthread_unuse_mm(ufence->mm);
@@ -65,5 +112,33 @@ void drm_user_fence_init(struct drm_user_fence *ufence,
ufence->mm = current->mm;
mmgrab(ufence->mm);
ufence->ops = ops;
+ ufence->cmp_addr = NULL;
+ ufence->cmp_value = 0;
+ ufence->cmp_op = DRM_USER_FENCE_CMP_NONE;
}
EXPORT_SYMBOL_GPL(drm_user_fence_init);
+
+/**
+ * drm_user_fence_set_compare - Configure per-signal value comparison
+ * @ufence: user fence
+ * @addr: userspace VA to read when the fence signals
+ * @value: expected value to compare against
+ * @op: comparison operator (see &enum drm_user_fence_cmp)
+ *
+ * When set, drm_user_fence reads @addr via get_user() each time the
+ * fence signals and calls ops->worker() only if the comparison passes.
+ * This enables per-signal filtering without open-coding the read+compare
+ * pattern in each driver.
+ *
+ * Must be called after drm_user_fence_init() and before
+ * drm_user_fence_add_callback().
+ */
+void drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op)
+{
+ ufence->cmp_addr = addr;
+ ufence->cmp_value = value;
+ ufence->cmp_op = op;
+}
+EXPORT_SYMBOL_GPL(drm_user_fence_set_compare);
diff --git a/include/drm/drm_user_fence.h b/include/drm/drm_user_fence.h
index 2b2b640f510f..764dd55ed3a2 100644
--- a/include/drm/drm_user_fence.h
+++ b/include/drm/drm_user_fence.h
@@ -33,6 +33,22 @@ struct drm_user_fence_ops {
void (*destroy)(struct drm_user_fence *ufence);
};
+/**
+ * enum drm_user_fence_cmp - comparison operators for per-signal filtering
+ *
+ * Used with drm_user_fence_set_compare() to control when ops->worker is
+ * called based on a value read from userspace.
+ */
+enum drm_user_fence_cmp {
+ DRM_USER_FENCE_CMP_NONE = 0, /* always call worker — default */
+ DRM_USER_FENCE_CMP_EQ, /* call worker if *addr == value */
+ DRM_USER_FENCE_CMP_NE, /* call worker if *addr != value */
+ DRM_USER_FENCE_CMP_GT, /* call worker if *addr > value */
+ DRM_USER_FENCE_CMP_GE, /* call worker if *addr >= value */
+ DRM_USER_FENCE_CMP_LT, /* call worker if *addr < value */
+ DRM_USER_FENCE_CMP_LE, /* call worker if *addr <= value */
+};
+
/**
* struct drm_user_fence - DRM user fence with MM borrowing
*
@@ -51,11 +67,24 @@ struct drm_user_fence {
struct mm_struct *mm;
/** @ops: Driver operations. */
const struct drm_user_fence_ops *ops;
+ /**
+ * @cmp_addr: Userspace VA to read for per-signal comparison.
+ * NULL means always call ops->worker (default XE behavior).
+ * Set via drm_user_fence_set_compare().
+ */
+ u64 __user *cmp_addr;
+ /** @cmp_value: Expected value for comparison. */
+ u64 cmp_value;
+ /** @cmp_op: Comparison operator. */
+ enum drm_user_fence_cmp cmp_op;
};
void drm_user_fence_init(struct drm_user_fence *ufence,
struct workqueue_struct *wq,
const struct drm_user_fence_ops *ops);
+void drm_user_fence_set_compare(struct drm_user_fence *ufence,
+ u64 __user *addr, u64 value,
+ enum drm_user_fence_cmp op);
static inline void drm_user_fence_get(struct drm_user_fence *ufence)
{
--
2.34.1
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-09-03 5:33 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-03 5:29 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-09-03 5:29 ` [PATCH 2/4] drm: Add drm_user_fence helper Srinivasan Shanmugam
2026-09-03 5:29 ` [PATCH 3/4] drm/xe: Convert xe_user_fence to drm_user_fence Srinivasan Shanmugam
2026-09-03 5:29 ` [PATCH 4/4] drm: Add per-signal compare functionality " Srinivasan Shanmugam
-- strict thread matches above, loose matches on Subject: below --
2026-09-03 5:32 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-09-03 5:32 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
2026-09-02 15:48 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-09-02 15:48 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
2026-09-02 14:56 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-09-02 14:56 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
2026-09-02 13:49 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-09-02 13:49 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
2026-08-31 14:56 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-08-31 14:56 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
2026-08-30 5:56 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-08-30 5:56 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
2026-08-30 3:12 [PATCH 1/4] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-08-30 3:12 ` [PATCH 4/4] drm: Add per-signal compare functionality to drm_user_fence Srinivasan Shanmugam
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®