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 a new commit never
gets past drm_atomic_helper_setup_commit() before the previous one has
reached hw_done. Check that directly rather than by counting the
commits in flight, which would depend on how quickly the commit work
items get to run.

The commit work items are always drained before the test returns, also
when it bails out early, so that none of them can touch the device
after it has been torn down.

v2: Make sure everything is torn down before we bail (sashiko)
    Don't count the in flight commits as that is non-deterministic (sashiko)

Cc: Maarten Lankhorst <[email protected]>
Signed-off-by: Ville Syrjälä <[email protected]>
---
 .../gpu/drm/tests/drm_atomic_commit_test.c    | 135 ++++++++++++++++++
 1 file changed, 135 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..7c51ea84718f 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)
@@ -36,6 +37,11 @@ struct drm_atomic_test_priv {
        struct drm_crtc *crtc;
        struct drm_encoder encoders[3];
        struct drm_connector connectors[2];
+
+       /* for drm_test_fake_commit_flood() */
+       atomic_t flood_inflight;
+       struct completion flood_drained;
+       bool flood_released;
 };
 
 static int modeset_counter;
@@ -495,6 +501,134 @@ static void drm_test_fake_commit_not_busy(struct kunit 
*test)
        drm_modeset_acquire_fini(&ctx);
 }
 
+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_atomic_test_priv *priv =
+               container_of(state->dev, struct drm_atomic_test_priv, drm);
+
+       drm_atomic_helper_wait_for_dependencies(state);
+
+       /* Pretend to do some work */
+       msleep(2);
+
+       drm_atomic_helper_commit_hw_done(state);
+       drm_atomic_helper_commit_cleanup_done(state);
+
+       /* The commit must be gone before the test can tear down the device */
+       drm_atomic_commit_put(state);
+
+       if (atomic_dec_and_test(&priv->flood_inflight))
+               complete_all(&priv->flood_drained);
+}
+
+/*
+ * Wait for all the commit work items to finish. The submitter holds one
+ * reference to flood_inflight until it is done submitting, so that the
+ * completion can not fire early. Also used as a cleanup action, to make sure
+ * no work item outlives the test if it bails out.
+ */
+static void drm_test_flood_drain(void *data)
+{
+       struct drm_atomic_test_priv *priv = data;
+
+       if (!xchg(&priv->flood_released, true) &&
+           atomic_dec_and_test(&priv->flood_inflight))
+               complete_all(&priv->flood_drained);
+
+       wait_for_completion(&priv->flood_drained);
+}
+
+static int drm_test_flood_submit(struct kunit *test,
+                                struct drm_atomic_test_priv *priv)
+{
+       struct drm_modeset_acquire_ctx ctx;
+       struct drm_atomic_commit *state;
+       struct drm_plane_state *plane_state;
+       struct drm_crtc_commit *prev;
+       int ret;
+
+       drm_modeset_acquire_init(&ctx, 0);
+
+       state = drm_atomic_commit_alloc(&priv->drm);
+       if (!state) {
+               ret = -ENOMEM;
+               goto out;
+       }
+       state->acquire_ctx = &ctx;
+
+       plane_state = drm_atomic_get_plane_state(state, priv->plane);
+       if (IS_ERR(plane_state)) {
+               ret = PTR_ERR(plane_state);
+               goto err_put;
+       }
+
+       /* Stable, the plane is locked */
+       prev = priv->plane->state->commit;
+
+       ret = drm_atomic_helper_setup_commit(state, true);
+       if (ret)
+               goto err_put;
+
+       /*
+        * Userspace must not get ahead of the previous commit's hw_done,
+        * whatever the commit work is up to.
+        */
+       if (prev)
+               KUNIT_EXPECT_TRUE(test, 
try_wait_for_completion(&prev->hw_done));
+
+       ret = drm_atomic_helper_swap_state(state, false);
+       if (ret)
+               goto err_put;
+
+       /* The ctx goes away, but the state lives on in the work item */
+       state->acquire_ctx = NULL;
+
+       atomic_inc(&priv->flood_inflight);
+       INIT_WORK(&state->commit_work, drm_test_flood_commit_work);
+       queue_work(system_dfl_wq, &state->commit_work);
+
+       ret = 0;
+       goto out;
+
+err_put:
+       drm_atomic_commit_put(state);
+out:
+       drm_modeset_drop_locks(&ctx);
+       drm_modeset_acquire_fini(&ctx);
+
+       return ret;
+}
+
+/*
+ * 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, ret;
+
+       priv = drm_atomic_test_init_drm_components(test, false);
+       KUNIT_ASSERT_NOT_NULL(test, priv);
+
+       atomic_set(&priv->flood_inflight, 1);
+       init_completion(&priv->flood_drained);
+
+       ret = kunit_add_action_or_reset(test, drm_test_flood_drain, priv);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+
+       for (i = 0; i < 100; i++) {
+               ret = drm_test_flood_submit(test, priv);
+               KUNIT_ASSERT_EQ(test, ret, 0);
+       }
+
+       drm_test_flood_drain(priv);
+}
+
 /*
  * 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 +667,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