* [PATCH v3 0/2] drm/drm_crtc: dma_fence_ops fixes
@ 2026-07-21 8:21 André Draszik
2026-07-21 8:21 ` [PATCH v3 1/2] drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind André Draszik
2026-07-21 8:21 ` [PATCH v3 2/2] drm/drm_crtc: fix race with dma_fence_signal() in ::get_driver_name() André Draszik
0 siblings, 2 replies; 5+ messages in thread
From: André Draszik @ 2026-07-21 8:21 UTC (permalink / raw)
To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Sumit Semwal, Christian König, Tvrtko Ursulin,
Boris Brezillon, Philipp Stanner, Danilo Krummrich, Sean Paul,
Gustavo Padovan
Cc: dri-devel, linux-kernel, linux-media, linaro-mm-sig,
Peter Griffin, Tudor Ambarus, Juan Yescas, kernel-team,
Simona Vetter, André Draszik, stable
Hi,
These patches fix two issues in the drm/drm_crtc driver. Initially I
was hitting the BUG_ON() in a scenario as explained in the commit
message of what is now the second patch in this series.
After posting, sashiko.dev noticed another issue, that was previously
masked by the now-removed BUG_ON(). Since we can't have a loud BUG() be
replaced with silent data corruption or worse, I've also added a patch
to address this issue highlighted by sashiko.dev. I believe its
observation and analysis to be correct.
Cheers,
Andre'
Signed-off-by: André Draszik <andre.draszik@linaro.org>
---
Changes in v3:
- Philipp:
- patch 1: update kerneldoc, add Fixes:
- patch 2: shorten commit message
- explicitly Cc: stable
- collect tag
- Link to v2: https://lore.kernel.org/r/20260708-linux-drm_crtc_fix2-v2-0-cf72be75d75a@linaro.org
Changes in v2:
- add new patch 1 to address sashiko observation
- original patch 1 becomes patch 2
- patch 2:
- don't turn fence_to_crtc() into macro (Jani, Philipp)
- update commit message to include reference to deprecated use of BUG
- Link to v1: https://lore.kernel.org/r/20260618-linux-drm_crtc_fix2-v1-1-c03e77b36f34@linaro.org
---
André Draszik (2):
drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind
drm/drm_crtc: fix race with dma_fence_signal() in ::get_driver_name()
drivers/gpu/drm/drm_crtc.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
---
base-commit: 0718283ab28bc3907e10b61a6b4be6fefa1cbb2f
change-id: 20260618-linux-drm_crtc_fix2-23a7c354a412
Best regards,
--
André Draszik <andre.draszik@linaro.org>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v3 1/2] drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind
2026-07-21 8:21 [PATCH v3 0/2] drm/drm_crtc: dma_fence_ops fixes André Draszik
@ 2026-07-21 8:21 ` André Draszik
2026-07-21 8:43 ` sashiko-bot
2026-07-21 11:20 ` Philipp Stanner
2026-07-21 8:21 ` [PATCH v3 2/2] drm/drm_crtc: fix race with dma_fence_signal() in ::get_driver_name() André Draszik
1 sibling, 2 replies; 5+ messages in thread
From: André Draszik @ 2026-07-21 8:21 UTC (permalink / raw)
To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Sumit Semwal, Christian König, Tvrtko Ursulin,
Boris Brezillon, Philipp Stanner, Danilo Krummrich, Sean Paul,
Gustavo Padovan
Cc: dri-devel, linux-kernel, linux-media, linaro-mm-sig,
Peter Griffin, Tudor Ambarus, Juan Yescas, kernel-team,
Simona Vetter, André Draszik, stable
In [1], sashiko reported the following issue:
=== snip ===
Looking at how these fences are managed, drm_crtc_create_fence()
creates a dma_fence without taking a reference to the drm_device or
drm_crtc. Because the sync_file framework exposes this fence to
userspace, the fence can outlive the CRTC.
The dma_fence contract requires that data accessed by dma_fence_ops
(like get_driver_name) must remain valid for an RCU grace period after
the fence is signaled. However, drm_crtc_cleanup() and the subsequent
freeing of the device do not wait for an RCU grace period via
synchronize_rcu().
If userspace calls ioctl(SYNC_IOC_FILE_INFO) concurrently with a device
hot-unplug:
CPU1 (Userspace)
sync_file_get_name()
ops = rcu_dereference(fence->ops);
if (!dma_fence_test_signaled_flag())
// Preempted or delayed here
CPU2 (Driver Teardown)
Signals the fence (setting fence->ops = NULL)
Destroys and frees the CRTC without waiting for an RCU grace period
CPU1 (Resumes)
ops->get_driver_name(fence) -> drm_crtc_fence_get_driver_name()
crtc = fence_to_crtc(fence); // Casts to the freed CRTC
return crtc->dev->driver->name; // Use-after-free
...
Does the CRTC or DRM device need to be kept alive for the RCU grace
period, or should the fence hold a proper reference to prevent the
use-after-free when get_driver_name() and get_timeline_name() access
the freed CRTC structure?
=== snap ===
I believe this to be a correct observation and this patch implements
the suggestion of waiting for an RCU grace period before proceeding
with destruction of the drm_crtc, so that get_driver_name() and
get_timeline_name() can still work.
Link: https://sashiko.dev/#/patchset/20260618-linux-drm_crtc_fix2-v1-1-c03e77b36f34@linaro.org?part=1
Fixes: 6d6003c4b613 ("drm/fence: add fence timeline to drm_crtc")
Cc: stable@vger.kernel.org
Signed-off-by: André Draszik <andre.draszik@linaro.org>
---
v3:
- Philipp: update kerneldoc, add Fixes:
v2: new patch
---
drivers/gpu/drm/drm_crtc.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c
index 63ead8ba6756..e8e80c936852 100644
--- a/drivers/gpu/drm/drm_crtc.c
+++ b/drivers/gpu/drm/drm_crtc.c
@@ -493,14 +493,23 @@ EXPORT_SYMBOL(__drmm_crtc_alloc_with_planes);
* drm_crtc_cleanup - Clean up the core crtc usage
* @crtc: CRTC to cleanup
*
- * This function cleans up @crtc and removes it from the DRM mode setting
- * core. Note that the function does *not* free the crtc structure itself,
- * this is the responsibility of the caller.
+ * This function cleans up @crtc and removes it from the DRM mode setting core,
+ * after first waiting an RCU grace period to ensure @crtc->dev can safely be
+ * dereferenced by our dma_fence_ops.
+ *
+ * Note that the function does *not* free the crtc structure itself, this is the
+ * responsibility of the caller.
*/
void drm_crtc_cleanup(struct drm_crtc *crtc)
{
struct drm_device *dev = crtc->dev;
+ /* Ensure our dma_fence_ops remain valid for an RCU grace period after
+ * the fence is signaled. This is necessary because our dma_fence_ops
+ * dereference crtc->dev.
+ */
+ synchronize_rcu();
+
/* Note that the crtc_list is considered to be static; should we
* remove the drm_crtc at runtime we would have to decrement all
* the indices on the drm_crtc after us in the crtc_list.
--
2.55.0.229.g6434b31f56-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v3 2/2] drm/drm_crtc: fix race with dma_fence_signal() in ::get_driver_name()
2026-07-21 8:21 [PATCH v3 0/2] drm/drm_crtc: dma_fence_ops fixes André Draszik
2026-07-21 8:21 ` [PATCH v3 1/2] drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind André Draszik
@ 2026-07-21 8:21 ` André Draszik
1 sibling, 0 replies; 5+ messages in thread
From: André Draszik @ 2026-07-21 8:21 UTC (permalink / raw)
To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
Simona Vetter, Sumit Semwal, Christian König, Tvrtko Ursulin,
Boris Brezillon, Philipp Stanner, Danilo Krummrich, Sean Paul,
Gustavo Padovan
Cc: dri-devel, linux-kernel, linux-media, linaro-mm-sig,
Peter Griffin, Tudor Ambarus, Juan Yescas, kernel-team,
Simona Vetter, André Draszik, stable
Since commit 541c8f2468b9 ("dma-buf: detach fence ops on signal v3"),
I'm seeing the BUG_ON() triggering in drm_crtc's fence_to_crtc() via
drm_crtc_fence_get_driver_name() regularly:
Call trace:
panic+0x58/0x5c
die+0x160/0x178
bug_brk_handler+0x70/0xa4
call_el1_break_hook+0x3c/0x1a0
do_el1_brk64+0x24/0x74
el1_brk64+0x34/0x54
el1h_64_sync_handler+0x80/0xfc
el1h_64_sync+0x84/0x88
drm_crtc_fence_get_driver_name+0x60/0x68 (P)
sync_file_get_name+0x184/0x45c
sync_file_ioctl+0x404/0xf70
__arm64_sys_ioctl+0x124/0x1dc
This looks to be caused by a code flow similar to the following:
+++ snip +++
thread A thread B
ioctl(SYNC_IOC_FILE_INFO)
sync_file_ioctl()
sync_file_get_name()
dma_fence_signal_timestamp_locked() dma_fence_driver_name()
ops = rcu_dereference(fence->ops)
if (!dma_fence_test_signaled_flag())
ops->get_driver_name(fence) i.e.
drm_crtc_fence_get_driver_name()
test_and_set_bit(SIGNALED)
RCU_INIT_POINTER(fence->ops, NULL)
drm_crtc_fence_get_driver_name()
BUG_ON(rcu_access_pointer(fence->ops)
!= &drm_crtc_fence_ops)
+++ snap +++
In other words, a fence's ops pointer can now change to NULL on signal,
which wasn't the case before the culprit commit and hence the BUG_ON()
triggers.
Simply drop the BUG_ON() as it's usage of fence ops is invalid now, and
because using BUG_ON() and friends to take down the system is an
unacceptable way to handle a failure.
Note that the adjacent drm_crtc_fence_get_timeline_name() has the same
problem and is fixed by this patch as well.
Fixes: 541c8f2468b9 ("dma-buf: detach fence ops on signal v3")
Cc: stable@vger.kernel.org
Reviewed-by: Philipp Stanner <phasta@kernel.org>
Signed-off-by: André Draszik <andre.draszik@linaro.org>
---
v3:
- Philipp:
- shorten commit message
- explicitly Cc: stable
- collect tag
v2:
- don't turn fence_to_crtc() into macro
- update commit message to include reference to unacceptable use of BUG
---
drivers/gpu/drm/drm_crtc.c | 3 ---
1 file changed, 3 deletions(-)
diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c
index e8e80c936852..5295ec122e0d 100644
--- a/drivers/gpu/drm/drm_crtc.c
+++ b/drivers/gpu/drm/drm_crtc.c
@@ -154,11 +154,8 @@ static void drm_crtc_crc_fini(struct drm_crtc *crtc)
#endif
}
-static const struct dma_fence_ops drm_crtc_fence_ops;
-
static struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
{
- BUG_ON(rcu_access_pointer(fence->ops) != &drm_crtc_fence_ops);
return container_of(fence->extern_lock, struct drm_crtc, fence_lock);
}
--
2.55.0.229.g6434b31f56-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v3 1/2] drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind
2026-07-21 8:21 ` [PATCH v3 1/2] drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind André Draszik
@ 2026-07-21 8:43 ` sashiko-bot
2026-07-21 11:20 ` Philipp Stanner
1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-07-21 8:43 UTC (permalink / raw)
To: André Draszik; +Cc: dri-devel, media-ci
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch fails to address a critical use-after-free of `crtc->fence_lock` because `synchronize_rcu()` does not protect external spinlocks.
- [High] The synchronize_rcu() fix violates the RCU-001 'Remove Before Reclaim' rule, failing to prevent UAF of the CRTC when new readers query the fence.
--
--- Patch [1]: [PATCH v3 1/2] drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind ---
commit 31b9417fd75463986e2e186faa6c672078a471fd
Author: André Draszik <andre.draszik@linaro.org>
drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind
This commit adds a synchronize_rcu() call to drm_crtc_cleanup() before removing
the CRTC from the DRM mode setting core. This is intended to wait for an RCU
grace period to ensure that dma_fence_ops can safely dereference crtc->dev
if userspace queries a sync_file concurrently with device hot-unplug.
Link: https://sashiko.dev/#/patchset/20260618-linux-drm_crtc_fix2-v1-1-c03e77b36f34@linaro.org?part=1
> diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c
> index 63ead8ba67564..e8e80c9368529 100644
> --- a/drivers/gpu/drm/drm_crtc.c
> +++ b/drivers/gpu/drm/drm_crtc.c
> @@ -493,14 +493,23 @@ EXPORT_SYMBOL(__drmm_crtc_alloc_with_planes);
[ ... ]
> void drm_crtc_cleanup(struct drm_crtc *crtc)
> {
> struct drm_device *dev = crtc->dev;
>
> + /* Ensure our dma_fence_ops remain valid for an RCU grace period after
> + * the fence is signaled. This is necessary because our dma_fence_ops
> + * dereference crtc->dev.
> + */
> + synchronize_rcu();
[Severity: High]
This is a pre-existing issue, but does this actually protect the
crtc->fence_lock?
In drm_crtc_create_fence(), the dma_fence is initialized with a pointer to
the CRTC's embedded spinlock:
drivers/gpu/drm/drm_crtc.c:drm_crtc_create_fence() {
...
dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
crtc->fence_context, ++crtc->fence_seqno);
...
}
Because the dma_fence does not hold a reference to the CRTC, the fence can
outlive the CRTC.
The synchronize_rcu() call added here provides no protection for the direct
spinlock acquisition in the dma_fence API.
If userspace calls ioctl(SYNC_IOC_FILE_INFO) after the CRTC is freed, the
kernel calls dma_fence_get_status(), which executes spin_lock_irqsave() on the
fence's external lock (pointing to the freed crtc->fence_lock).
Can this still lead to a use-after-free of the crtc->fence_lock?
[Severity: High]
This isn't a bug introduced by this patch, but does calling synchronize_rcu()
here violate the RCU "Remove Before Reclaim" rule?
Because the fence is exposed to userspace via sync_file, it is not removed
from RCU-accessible paths before synchronize_rcu() is called.
synchronize_rcu() only waits for existing readers.
New userspace calls to ioctl(SYNC_IOC_FILE_INFO) after the CRTC is destroyed
will start a new rcu_read_lock(), call ops->get_driver_name(), cast the
fence to the freed CRTC, and dereference crtc->dev.
Does this leave the CRTC structure vulnerable to a use-after-free when new
readers query the fence?
> +
> /* Note that the crtc_list is considered to be static; should we
> * remove the drm_crtc at runtime we would have to decrement all
> * the indices on the drm_crtc after us in the crtc_list.
> */
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260721-linux-drm_crtc_fix2-v3-0-afa8c71506e6@linaro.org?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3 1/2] drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind
2026-07-21 8:21 ` [PATCH v3 1/2] drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind André Draszik
2026-07-21 8:43 ` sashiko-bot
@ 2026-07-21 11:20 ` Philipp Stanner
1 sibling, 0 replies; 5+ messages in thread
From: Philipp Stanner @ 2026-07-21 11:20 UTC (permalink / raw)
To: André Draszik, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann, David Airlie, Simona Vetter, Sumit Semwal,
Christian König, Tvrtko Ursulin, Boris Brezillon,
Philipp Stanner, Danilo Krummrich, Sean Paul, Gustavo Padovan
Cc: dri-devel, linux-kernel, linux-media, linaro-mm-sig,
Peter Griffin, Tudor Ambarus, Juan Yescas, kernel-team,
Simona Vetter, stable
On Tue, 2026-07-21 at 09:21 +0100, André Draszik wrote:
> In [1], sashiko reported the following issue:
>
> === snip ===
> Looking at how these fences are managed, drm_crtc_create_fence()
> creates a dma_fence without taking a reference to the drm_device or
> drm_crtc. Because the sync_file framework exposes this fence to
> userspace, the fence can outlive the CRTC.
>
> The dma_fence contract requires that data accessed by dma_fence_ops
> (like get_driver_name) must remain valid for an RCU grace period after
> the fence is signaled. However, drm_crtc_cleanup() and the subsequent
> freeing of the device do not wait for an RCU grace period via
> synchronize_rcu().
>
> If userspace calls ioctl(SYNC_IOC_FILE_INFO) concurrently with a device
> hot-unplug:
>
> CPU1 (Userspace)
> sync_file_get_name()
> ops = rcu_dereference(fence->ops);
> if (!dma_fence_test_signaled_flag())
> // Preempted or delayed here
nit: no one will be preempted here since the RCU read lock must be
held. The Sashiko tool misses the point, which is simply that someone
illegally frees up stuff that might be still in use, with or without
delay or preemption, that's all irrelevant for the issue.
Anyways, thanks for fixing this:
>
>
[…]
> Link: https://sashiko.dev/#/patchset/20260618-linux-drm_crtc_fix2-v1-1-c03e77b36f34@linaro.org?part=1
> Fixes: 6d6003c4b613 ("drm/fence: add fence timeline to drm_crtc")
> Cc: stable@vger.kernel.org
> Signed-off-by: André Draszik <andre.draszik@linaro.org>
Reviewed-by: Philipp Stanner <phasta@kernel.org>
>
> ---
> v3:
> - Philipp: update kerneldoc, add Fixes:
>
> v2: new patch
> ---
> drivers/gpu/drm/drm_crtc.c | 15 ++++++++++++---
> 1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c
> index 63ead8ba6756..e8e80c936852 100644
> --- a/drivers/gpu/drm/drm_crtc.c
> +++ b/drivers/gpu/drm/drm_crtc.c
> @@ -493,14 +493,23 @@ EXPORT_SYMBOL(__drmm_crtc_alloc_with_planes);
> * drm_crtc_cleanup - Clean up the core crtc usage
> * @crtc: CRTC to cleanup
> *
> - * This function cleans up @crtc and removes it from the DRM mode setting
> - * core. Note that the function does *not* free the crtc structure itself,
> - * this is the responsibility of the caller.
> + * This function cleans up @crtc and removes it from the DRM mode setting core,
> + * after first waiting an RCU grace period to ensure @crtc->dev can safely be
> + * dereferenced by our dma_fence_ops.
> + *
> + * Note that the function does *not* free the crtc structure itself, this is the
> + * responsibility of the caller.
> */
> void drm_crtc_cleanup(struct drm_crtc *crtc)
> {
> struct drm_device *dev = crtc->dev;
>
> + /* Ensure our dma_fence_ops remain valid for an RCU grace period after
> + * the fence is signaled. This is necessary because our dma_fence_ops
> + * dereference crtc->dev.
> + */
> + synchronize_rcu();
> +
> /* Note that the crtc_list is considered to be static; should we
> * remove the drm_crtc at runtime we would have to decrement all
> * the indices on the drm_crtc after us in the crtc_list.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-07-21 11:20 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-21 8:21 [PATCH v3 0/2] drm/drm_crtc: dma_fence_ops fixes André Draszik
2026-07-21 8:21 ` [PATCH v3 1/2] drm/drm_crtc: ensure dma_fence_ops remain valid during device unbind André Draszik
2026-07-21 8:43 ` sashiko-bot
2026-07-21 11:20 ` Philipp Stanner
2026-07-21 8:21 ` [PATCH v3 2/2] drm/drm_crtc: fix race with dma_fence_signal() in ::get_driver_name() André Draszik
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.