Skip to contentSkip to conversation

drm/amd/display: avoid flip_done waits on TEST_ONLY commits

RFC PATCH In review Jakub Okoński 3 participants 7 messages Nov 24 – Nov 26

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

cover drm/amd/display: avoid flip_done waits on TEST_ONLY commits

(bear with me because it's the first patch I've ever sent over email)

I tested overlays on KWin 6.8 and noticed a big latency tail for DRM_MODE_ATOMIC_TEST_ONLY | NONBLOCK checks that KWin does to plan the frame and decide whether it needs composition or direct scanout. The testing happened on an RX 6900 XT RDNA2 GPU. The stalls happen whenever planes move on or off an overlay or change geometry with the previous commit still pending.

The stall happens in amdgpu_dm_atomic_check(): dm_update_plane_state() sets needs_dc_state_realloc, which calls do_aquire_global_lock() and waits for both hw_done and flip_done on the CRTC's newest commit, even though TEST_ONLY publishes no state and mode-only DC validation takes <= ~0.1 ms.

This RFC adds a test_only bit to struct drm_atomic_commit (1/2) and skips the flip_done wait in do_aquire_global_lock() for non-modeset TEST_ONLY checks (2/2), keeping all locks, the hw_done wait, and the full drain for modeset tests and real commits.

I tested my 7.3.4 kernel at stock and with this series. A recorded desktop session lasting ~26 seconds and replayed with libinput replay. A browser window navigating to a page and interacting with it causing redraws in response to input. farnoy.dev/tools/mouse-latency.

Captured stalls from this scenario break down like this:

+--------------------------+-----------+----------+
|                   Metric | Unpatched |  Patched |
+--------------------------+-----------+----------+
|              Replay runs |        10 |        5 |
|         Presented frames |      5943 |     3019 |
|         TEST_ONLY ioctls |     51305 |    25627 |
| TEST_ONLY stalls >= 2 ms |        13 |        2 |
|          flip_done waits |         9 |        0 |
|            hw_done waits |         4 |        2 |
+--------------------------+-----------+----------+

No flip timeouts or atomic commit failures were observed.

The remaining two long waits were on hw_done: tracing the previous commit worker showed 99.3% of each ~7 ms wait inside dc_update_planes_and_stream(), returning 9-19 us after a late flip.

Patches written by an LLM, reviewed and tested by me. Compile-tested on amd-staging-drm-next 8dd68a79583a.

I was told Harry is working on this specific area but I'd already tested this change by then, so I'm pushing in case this is useful. I have no idea whether this works on <=gfx9 or >gfx10, and I have not attempted to improve the hw_done waits.

Jakub Okoński (2):
drm/atomic: record TEST_ONLY ioctl intent in drm_atomic_commit drm/amd/display: Skip flip_done wait for non-modeset TEST_ONLY checks

 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 15 ++++++++-------
 drivers/gpu/drm/drm_atomic_uapi.c                 |  1 +
 include/drm/drm_atomic.h                          |  7 +++++++
 3 files changed, 16 insertions(+), 7 deletions(-)

base-commit: 8dd68a79583aaf6df6fe4feea3ee9e9f26b02993

Signature · 1 line
2.56.0
#1 lore raw

RFC PATCH 1/2 drm/atomic: record TEST_ONLY ioctl intent in drm_atomic_commit

Real commits also run drm_atomic_check_only(), and NONBLOCK is not a dry-run flag, so atomic_check cannot tell a TEST_ONLY ioctl from a real commit.

Set a test_only bit from DRM_MODE_ATOMIC_TEST_ONLY in drm_mode_atomic_ioctl(). Core behaviour is unchanged.

---
 drivers/gpu/drm/drm_atomic_uapi.c | 1 +
 include/drm/drm_atomic.h          | 7 +++++++
 2 files changed, 8 insertions(+)
drivers/gpu/drm/drm_atomic_uapi.c +1 -0
index 1050dddadb17..17881994f737 100644
@@ -1667,6 +1667,7 @@ int drm_mode_atomic_ioctl(struct drm_device *dev,
 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
 	state->acquire_ctx = &ctx;
 	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
+	state->test_only = !!(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY);
 	state->plane_color_pipeline = file_priv->plane_color_pipeline;
 
 retry:
include/drm/drm_atomic.h +7 -0
index 88087910ab1a..456b194d220a 100644
@@ -521,6 +521,13 @@ struct drm_atomic_commit {
 	 *   for the normal composition loop at reasonable cost.
 	 */
 	bool allow_modeset : 1;
+	/**
+	 * @test_only:
+	 *
+	 * This transaction is built for a DRM_MODE_ATOMIC_TEST_ONLY ioctl
+	 * and will not be passed to the driver's atomic commit hook.
+	 */
+	bool test_only : 1;
 	/**
 	 * @legacy_cursor_update:
 	 *
Signature · 1 line
2.56.0
#2 lore raw

RFC PATCH 2/2 drm/amd/display: Skip flip_done wait for non-modeset TEST_ONLY checks

In reply to Jakub Okoński

needs_dc_state_realloc makes amdgpu_dm_atomic_check() call do_aquire_global_lock(), which waits for hw_done and flip_done of each CRTC's newest commit. A TEST_ONLY check issued while a nonblocking commit is pending therefore blocks for up to one refresh (6.9 ms seen on DCN 3.0 at 120 Hz) for about 0.1 ms of validation.

A TEST_ONLY transaction publishes no state and does not reconfigure IRQs through the commit path, so a non-modeset test has nothing to order against the pending flip. Skip only its flip_done wait.

Kept: the modeset and private object locks, the hw_done wait (DC validation reads the programmed topology in dc->current_state), the same validation, and the full drain for modeset tests and real commits.

---
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +8 -7
index 3d2201285888..25dcbee0c7df 100644
@@ -5594,8 +5594,8 @@ static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_commit *state)
 }
 
 /*
- * Grabs all modesetting locks to serialize against any blocking commits,
- * Waits for completion of all non blocking commits.
+ * Grabs all modesetting locks to serialize against blocking commits, and
+ * waits for pending programming and, where needed, flip completion.
  */
 STATIC_IFN_KUNIT int do_aquire_global_lock(struct drm_device *dev,
 					   struct drm_atomic_commit *state)
@@ -5624,13 +5624,14 @@ STATIC_IFN_KUNIT int do_aquire_global_lock(struct drm_device *dev,
 		if (!commit)
 			continue;
 
-		/*
-		 * Make sure all pending HW programming completed and
-		 * page flips done
-		 */
+		/* DC validation can inspect the programmed pipe topology. */
 		ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
 
-		if (ret > 0)
+		/*
+		 * Tests don't publish state or reconfigure interrupts. Limit
+		 * overlap with pending flips to tests that disallow modesets.
+		 */
+		if (ret > 0 && (!state->test_only || state->allow_modeset))
 			ret = wait_for_completion_interruptible_timeout(
 					&commit->flip_done, 10*HZ);
 
Signature · 1 line
2.56.0
#3 lore raw

Re: [RFC PATCH 2/2] drm/amd/display: Skip flip_done wait for non-modeset TEST_ONLY checks

Quoted 45 lines · Jakub Okoński

needs_dc_state_realloc makes amdgpu_dm_atomic_check() call do_aquire_global_lock(), which waits for hw_done and flip_done of each CRTC's newest commit. A TEST_ONLY check issued while a nonblocking commit is pending therefore blocks for up to one refresh (6.9 ms seen on DCN 3.0 at 120 Hz) for about 0.1 ms of validation.

A TEST_ONLY transaction publishes no state and does not reconfigure IRQs through the commit path, so a non-modeset test has nothing to order against the pending flip. Skip only its flip_done wait.

Kept: the modeset and private object locks, the hw_done wait (DC validation reads the programmed topology in dc->current_state), the same validation, and the full drain for modeset tests and real commits.

---
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +3 -6
index 3d2201285888..25dcbee0c7df 100644
@@ -5594,8 +5594,8 @@ static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_commit *state)
 }
 
 /*
- * Grabs all modesetting locks to serialize against any blocking commits,
- * Waits for completion of all non blocking commits.
+ * Grabs all modesetting locks to serialize against blocking commits, and
+ * waits for pending programming and, where needed, flip completion.
  */
 STATIC_IFN_KUNIT int do_aquire_global_lock(struct drm_device *dev,
 					   struct drm_atomic_commit *state)
@@ -5624,13 +5624,14 @@ STATIC_IFN_KUNIT int do_aquire_global_lock(struct drm_device *dev,
 		if (!commit)
 			continue;
 
-		/*
-		 * Make sure all pending HW programming completed and
-		 * page flips done
-		 */
+		/* DC validation can inspect the programmed pipe topology. */
 		ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c STATIC_IFN_KUNIT int do_aquire_global_lock(struct drm_device *dev, · line 5637

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +5 -1
 
-		if (ret > 0)
+		/*
+		 * Tests don't publish state or reconfigure interrupts. Limit
+		 * overlap with pending flips to tests that disallow modesets.
+		 */
+		if (ret > 0 && (!state->test_only || state->allow_modeset))
 			ret = wait_for_completion_interruptible_timeout(
 					&commit->flip_done, 10*HZ);
 

Does a commit with both DRM_MODE_ATOMIC_ALLOW_MODESET & DRM_MODE_ATOMIC_TEST_ONLY set really need to wait for pending flips to complete? Why?

Signature · 2 lines
Earthling Michel Dänzer       \        GNOME / Xwayland / Mesa developer
https://redhat.com             \               Libre software enthusiast
#4 lore raw

Re: [RFC PATCH 2/2] drm/amd/display: Skip flip_done wait for non-modeset TEST_ONLY checks

Quoted 1 line · Michel Dänzer

Does a commit with both DRM_MODE_ATOMIC_ALLOW_MODESET & DRM_MODE_ATOMIC_TEST_ONLY set really need to wait for pending flips to complete? Why?

I was not confident at all and tried to keep this change narrow. It's possible this could be relaxed further but I have not tried it.

maintainer
#5 lore raw

Re: [RFC PATCH 2/2] drm/amd/display: Skip flip_done wait for non-modeset TEST_ONLY checks

Quoted 7 lines · Jakub Okoński

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 1 line · Michel Dänzer

Does a commit with both DRM_MODE_ATOMIC_ALLOW_MODESET & DRM_MODE_ATOMIC_TEST_ONLY set really need to wait for pending flips to complete? Why?

I was not confident at all and tried to keep this change narrow. It's possible this could be relaxed further but I have not tried it.

I have a branch that removes the need for that stall in atomic check and passed the work on to someone else for validation and review.

The problem with the existing code is that IRQ handlers reference in-flight state, especially dc_stream_state. The solution is to decouple the state the IRQ handlers need from anything in flight. But its liable to break things, hence I'd rather be a bit more thorough with validation.

I hope you'll see patches for it soon-ish.

Harry

#6 lore raw

Re: [RFC PATCH 2/2] drm/amd/display: Skip flip_done wait for non-modeset TEST_ONLY checks

Quoted 14 lines · Harry Wentland
Quoted 7 lines · Jakub Okoński

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 1 line · Michel Dänzer

Does a commit with both DRM_MODE_ATOMIC_ALLOW_MODESET & DRM_MODE_ATOMIC_TEST_ONLY set really need to wait for pending flips to complete? Why?

I was not confident at all and tried to keep this change narrow. It's possible this could be relaxed further but I have not tried it.

I have a branch that removes the need for that stall in atomic check and passed the work on to someone else for validation and review.

Quoted 8 lines

The problem with the existing code is that IRQ handlers reference in-flight state, especially dc_stream_state. The solution is to decouple the state the IRQ handlers need from anything in flight. But its liable to break things, hence I'd rather be a bit more thorough with validation.

I hope you'll see patches for it soon-ish.

Harry

Great! Does that mean you'll be going further and superseding my patch to cover the hw_done waits and potentially other sources of stalls too?