From: Ville Syrjälä <[email protected]>

Add a test that floods nonblocking commits touching only an unbound
plane with swap_state(stall=false), and checks that the number of
commits in flight stays bounded and that no hw_done wait times out.

Cc: Maarten Lankhorst <[email protected]>
Signed-off-by: Ville Syrjälä <[email protected]>
---
 .../gpu/drm/tests/drm_atomic_commit_test.c    | 84 +++++++++++++++++++
 1 file changed, 84 insertions(+)

diff --git a/drivers/gpu/drm/tests/drm_atomic_commit_test.c 
b/drivers/gpu/drm/tests/drm_atomic_commit_test.c
index 593e1842fada..247b5a916b58 100644
--- a/drivers/gpu/drm/tests/drm_atomic_commit_test.c
+++ b/drivers/gpu/drm/tests/drm_atomic_commit_test.c
@@ -10,6 +10,7 @@
 #include <drm/drm_atomic_uapi.h>
 #include <drm/drm_kunit_helpers.h>
 #include <drm/drm_probe_helper.h>
+#include <linux/delay.h>
 #include <linux/workqueue.h>
 
 #define DRM_TEST_ENC_0 BIT(0)
@@ -495,6 +496,88 @@ static void drm_test_fake_commit_not_busy(struct kunit 
*test)
        drm_modeset_acquire_fini(&ctx);
 }
 
+static atomic_t drm_test_flood_inflight, drm_test_flood_max, 
drm_test_flood_timeouts;
+
+static void drm_test_flood_commit_work(struct work_struct *work)
+{
+       struct drm_atomic_commit *state =
+               container_of(work, struct drm_atomic_commit, commit_work);
+       struct drm_plane *plane;
+       struct drm_plane_state *old_plane_state;
+       int i;
+
+       for_each_old_plane_in_state(state, plane, old_plane_state, i)
+               if (drm_crtc_commit_wait(old_plane_state->commit))
+                       atomic_inc(&drm_test_flood_timeouts);
+
+       /* Pretend to do some work */
+       msleep(2);
+
+       drm_atomic_helper_commit_hw_done(state);
+       drm_atomic_helper_commit_cleanup_done(state);
+       atomic_dec(&drm_test_flood_inflight);
+       drm_atomic_commit_put(state);
+}
+
+/*
+ * Test that a flood of nonblocking commits that only touch an unbound plane
+ * can not queue up an unbounded number of commits, even if the driver does not
+ * ask swap_state() to stall. Otherwise the work items would all wait for the
+ * hw_done of their predecessor, and the waits would add up.
+ */
+static void drm_test_fake_commit_flood(struct kunit *test)
+{
+       struct drm_atomic_test_priv *priv;
+       int i;
+
+       priv = drm_atomic_test_init_drm_components(test, false);
+       KUNIT_ASSERT_NOT_NULL(test, priv);
+
+       atomic_set(&drm_test_flood_inflight, 0);
+       atomic_set(&drm_test_flood_max, 0);
+       atomic_set(&drm_test_flood_timeouts, 0);
+
+       for (i = 0; i < 100; i++) {
+               struct drm_modeset_acquire_ctx ctx;
+               struct drm_atomic_commit *state;
+               int cur, ret;
+
+               drm_modeset_acquire_init(&ctx, 0);
+
+               state = drm_atomic_commit_alloc(&priv->drm);
+               KUNIT_ASSERT_NOT_NULL(test, state);
+               state->acquire_ctx = &ctx;
+               KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
+                                            drm_atomic_get_plane_state(state, 
priv->plane));
+
+               ret = drm_atomic_helper_setup_commit(state, true);
+               KUNIT_ASSERT_EQ(test, ret, 0);
+
+               ret = drm_atomic_helper_swap_state(state, false);
+               KUNIT_ASSERT_EQ(test, ret, 0);
+
+               cur = atomic_inc_return(&drm_test_flood_inflight);
+               if (cur > atomic_read(&drm_test_flood_max))
+                       atomic_set(&drm_test_flood_max, cur);
+
+               INIT_WORK(&state->commit_work, drm_test_flood_commit_work);
+               queue_work(system_dfl_wq, &state->commit_work);
+
+               drm_modeset_drop_locks(&ctx);
+               drm_modeset_acquire_fini(&ctx);
+       }
+
+       while (atomic_read(&drm_test_flood_inflight))
+               msleep(10);
+
+       /*
+        * The previous commit may still be doing its cleanup when the next
+        * one is submitted, hence 2 rather than 1.
+        */
+       KUNIT_EXPECT_LE(test, atomic_read(&drm_test_flood_max), 2);
+       KUNIT_EXPECT_EQ(test, atomic_read(&drm_test_flood_timeouts), 0);
+}
+
 /*
  * Test that the commits know their device, and that only the one for a CRTC
  * knows a CRTC, so that the waits can report timeouts for either.
@@ -533,6 +616,7 @@ static void drm_test_commit_dev(struct kunit *test)
 static struct kunit_case drm_fake_commit_tests[] = {
        KUNIT_CASE(drm_test_fake_commit_flip_done),
        KUNIT_CASE(drm_test_fake_commit_not_busy),
+       KUNIT_CASE(drm_test_fake_commit_flood),
        KUNIT_CASE(drm_test_commit_dev),
        {}
 };
-- 
2.56.0

Reply via email to