Skip to contentSkip to conversation

drm: Fix drm_crtc_commit leak if signaled when PAGE_FLIP_EVENT is used

PATCH Applied Thadeu Lima de Souza Cascardo 3 participants 4 messages Jul 27 – Aug 26

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm: Fix drm_crtc_commit leak if signaled when PAGE_FLIP_EVENT is used

Commit 1c6ceeee6ebb ("drm/atomic: Fix memleak on ERESTARTSYS during non-blocking commits") fixed a very similar issue when the event was allocated by drm_atomic_helper_setup_commit() itself.

However, if the event is allocated in prepare_signaling(), it will also be set to NULL in complete_signaling(), which prevents drm_crtc_commit from being put in __drm_atomic_helper_crtc_destroy_state().

Dropping the reference when the event is set to NULL at complete_signaling() fixes the leak.

The leak can be reproduced by sending a signal to the thread using DRM_MODE_PAGE_FLIP_EVENT and using a sw_sync fence to cause the atomic ioctl to block at drm_atomic_helper_wait_for_fences(). It happened both with amdgpu and vkms.

Fixes 24835e442f28 ("drm: reference count event->completion")
---
 drivers/gpu/drm/drm_atomic_uapi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/drm_atomic_uapi.c +2 -0
index e997917819e8..7b951af5ab32 100644
@@ -1563,6 +1563,8 @@ static void complete_signaling(struct drm_device *dev,
 		 * to prevent a double free in drm_atomic_commit_clear.
 		 */
 		if (event && (event->base.fence || event->base.file_priv)) {
+			if (crtc_state->commit && crtc_state->commit->abort_completion)
+				drm_crtc_commit_put(crtc_state->commit);
 			drm_event_cancel_free(dev, &event->base);
 			crtc_state->event = NULL;
 		}

---
base-commit: d5efb1e1b1d7cd27c642654042f80d19a7ba3caf
change-id: 20260727-drm_crtc_atomic_commit_leak-4de5e75162b3

Best regards,

Signature · 1 line
Thadeu Lima de Souza Cascardo <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Memory leak of drm_pending_vblank_event in prepare_signaling() error path. --

--- Patch [1]: [PATCH] drm: Fix drm_crtc_commit leak if signaled when PAGE_FLIP_EVENT is used --- commit 7e124359c795a6d6965d1810c1067f7ff33a7631
Author: Thadeu Lima de Souza Cascardo <[email protected]>

drm: Fix drm_crtc_commit leak if signaled when PAGE_FLIP_EVENT is used

This commit fixes a memory leak in the atomic commit path when the ioctl is signaled while waiting for fences. It ensures the reference to the drm_crtc_commit is properly dropped if the event allocated in prepare_signaling() is cleared in complete_signaling().

drivers/gpu/drm/drm_atomic_uapi.c static void complete_signaling(struct drm_device *dev, · line 1565

Quoted 8 lines
drivers/gpu/drm/drm_atomic_uapi.c +0 -0
index e997917819e8e..7b951af5ab320 100644
@@ -1563,6 +1563,8 @@ static void complete_signaling(struct drm_device *dev,
 		 * to prevent a double free in drm_atomic_commit_clear.
 		 */
 		if (event && (event->base.fence || event->base.file_priv)) {

[Severity: High]
This is a pre-existing issue, but does this conditional inadvertently leak the event under certain error conditions?

If userspace calls DRM_IOCTL_MODE_ATOMIC with an OUT_FENCE_PTR but without the DRM_MODE_PAGE_FLIP_EVENT flag, prepare_signaling() allocates the event:

    e = create_vblank_event(crtc, arg->user_data);
    if (!e)
        return -ENOMEM;
    crtc_state->event = e;

Since PAGE_FLIP_EVENT is not set, drm_event_reserve_init() is skipped, leaving event->base.file_priv as NULL. If setup_out_fence() then fails (for example, due to file descriptor exhaustion), prepare_signaling() returns an error before event->base.fence is set:

    ret = setup_out_fence(&f[(*num_fences)++], fence);
    if (ret) {
        dma_fence_put(fence);
        return ret;
    }

When complete_signaling() cleans up the error path, both event->base.fence and event->base.file_priv are NULL, causing this block to be skipped.

The event is left in crtc_state->event, but __drm_atomic_helper_crtc_destroy_state() does not free it (it only frees state->commit->event), leading to a memory leak. Could this be triggered by untrusted userspace to reliably exhaust kernel memory?

Quoted 5 lines
drivers/gpu/drm/drm_atomic_uapi.c +2 -0
+			if (crtc_state->commit && crtc_state->commit->abort_completion)
+				drm_crtc_commit_put(crtc_state->commit);
 			drm_event_cancel_free(dev, &event->base);
 			crtc_state->event = NULL;
 		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260727-drm_crtc_atomic_commit_leak-v1-1-23d9948a9d7c@igalia.com?part=1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Thadeu Lima de Souza Cascardo
Quoted 8 lines · Thadeu Lima de Souza Cascardo

Commit 1c6ceeee6ebb ("drm/atomic: Fix memleak on ERESTARTSYS during non-blocking commits") fixed a very similar issue when the event was allocated by drm_atomic_helper_setup_commit() itself.

However, if the event is allocated in prepare_signaling(), it will also be set to NULL in complete_signaling(), which prevents drm_crtc_commit from being put in __drm_atomic_helper_crtc_destroy_state().

Quoted 7 lines

Dropping the reference when the event is set to NULL at complete_signaling() fixes the leak.

The leak can be reproduced by sending a signal to the thread using DRM_MODE_PAGE_FLIP_EVENT and using a sw_sync fence to cause the atomic ioctl to block at drm_atomic_helper_wait_for_fences(). It happened both with amdgpu and vkms.

Nice catch!

- Do you have a reproducer or is there an IGT test that triggers this issue?

- cc'ing Leo and Harry as well because they worked on the similar issue and must be interested in it.

- I think it should point to stable # v4.11+ as in commit 1c6ceeee6ebb

Thanks!

Quoted 30 lines
Fixes 24835e442f28 ("drm: reference count event->completion")
---
  drivers/gpu/drm/drm_atomic_uapi.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/gpu/drm/drm_atomic_uapi.c +2 -1
index e997917819e8..7b951af5ab32 100644
@@ -1563,6 +1563,8 @@ static void complete_signaling(struct drm_device *dev,
  		 * to prevent a double free in drm_atomic_commit_clear.
  		 */
  		if (event && (event->base.fence || event->base.file_priv)) {
+			if (crtc_state->commit && crtc_state->commit->abort_completion)
+				drm_crtc_commit_put(crtc_state->commit);
  			drm_event_cancel_free(dev, &event->base);
  			crtc_state->event = NULL;
  		}
 
---

base-commit: d5efb1e1b1d7cd27c642654042f80d19a7ba3caf
change-id: 20260727-drm_crtc_atomic_commit_leak-4de5e75162b3

Best regards,
--
Thadeu Lima de Souza Cascardo <[email protected]>

Applied
#3 lore raw
Quoted 24 lines · Melissa Wen
Quoted 8 lines · Thadeu Lima de Souza Cascardo

Commit 1c6ceeee6ebb ("drm/atomic: Fix memleak on ERESTARTSYS during non-blocking commits") fixed a very similar issue when the event was allocated by drm_atomic_helper_setup_commit() itself.

However, if the event is allocated in prepare_signaling(), it will also be set to NULL in complete_signaling(), which prevents drm_crtc_commit from being put in __drm_atomic_helper_crtc_destroy_state().

Quoted 7 lines

Dropping the reference when the event is set to NULL at complete_signaling() fixes the leak.

The leak can be reproduced by sending a signal to the thread using DRM_MODE_PAGE_FLIP_EVENT and using a sw_sync fence to cause the atomic ioctl to block at drm_atomic_helper_wait_for_fences(). It happened both with amdgpu and vkms.

Nice catch!

- Do you have a reproducer or is there an IGT test that triggers this issue?

Quoted 6 lines

- cc'ing Leo and Harry as well because they worked on the similar issue and must be interested in it.

- I think it should point to stable # v4.11+ as in commit 1c6ceeee6ebb

Applied to drm-misc-fixes.
commit: 4d4be202165e832d74849b4a68e289a2a377039c

Melissa

Quoted 36 lines

Thanks!

Quoted 32 lines
Fixes 24835e442f28 ("drm: reference count event->completion")

---
  drivers/gpu/drm/drm_atomic_uapi.c | 2 ++

  1 file changed, 2 insertions(+)
drivers/gpu/drm/drm_atomic_uapi.c +2 -0
index e997917819e8..7b951af5ab32 100644
@@ -1563,6 +1563,8 @@ static void complete_signaling(struct 
 drm_device *dev,
            * to prevent a double free in drm_atomic_commit_clear.
            */
           if (event && (event->base.fence || event->base.file_priv)) {
+            if (crtc_state->commit && 
 crtc_state->commit->abort_completion)
+                drm_crtc_commit_put(crtc_state->commit);
               drm_event_cancel_free(dev, &event->base);

              crtc_state->event = NULL;
          }

---
base-commit: d5efb1e1b1d7cd27c642654042f80d19a7ba3caf
change-id: 20260727-drm_crtc_atomic_commit_leak-4de5e75162b3

Best regards,

Signature · 1 line
Thadeu Lima de Souza Cascardo <[email protected]>