drm_atomic_helper_prepare_planes() prepares the framebuffers of the
colorops in a commit before those of the planes, and has to unwind
them on every failure path. Each prepared colorop state has to be
cleaned up exactly once, on the abort path through
drm_atomic_helper_unprepare_planes() or, after the state swap, through
drm_atomic_helper_cleanup_planes() on the old state.

Add KUnit tests with mock colorop and plane helpers that record their
calls and can fail on request. Check that:

- colorops are prepared once, before the planes;
- a failing colorop unwinds only the colorops prepared before it;
- a failing plane prepare_fb or begin_fb_access cleans up all
  colorops;
- unprepare_planes() cleans up the new colorop states;
- cleanup_planes() after the swap cleans up the old colorop states.

Signed-off-by: Harry Wentland <[email protected]>
---
 drivers/gpu/drm/tests/Makefile                |   1 +
 .../gpu/drm/tests/drm_colorop_helper_test.c   | 397 ++++++++++++++++++
 2 files changed, 398 insertions(+)
 create mode 100644 drivers/gpu/drm/tests/drm_colorop_helper_test.c

diff --git a/drivers/gpu/drm/tests/Makefile b/drivers/gpu/drm/tests/Makefile
index 42207df1d1c8..4bf8696fa49c 100644
--- a/drivers/gpu/drm/tests/Makefile
+++ b/drivers/gpu/drm/tests/Makefile
@@ -8,6 +8,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
        drm_atomic_commit_test.o \
        drm_bridge_test.o \
        drm_cmdline_parser_test.o \
+       drm_colorop_helper_test.o \
        drm_connector_test.o \
        drm_damage_helper_test.o \
        drm_dp_mst_helper_test.o \
diff --git a/drivers/gpu/drm/tests/drm_colorop_helper_test.c 
b/drivers/gpu/drm/tests/drm_colorop_helper_test.c
new file mode 100644
index 000000000000..2a650500dd60
--- /dev/null
+++ b/drivers/gpu/drm/tests/drm_colorop_helper_test.c
@@ -0,0 +1,397 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests for the colorop prepare_fb and cleanup_fb atomic helper hooks
+ *
+ * Copyright 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <drm/drm_atomic.h>
+#include <drm/drm_atomic_helper.h>
+#include <drm/drm_colorop.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_kunit_helpers.h>
+#include <drm/drm_modeset_helper_vtables.h>
+#include <drm/drm_modeset_lock.h>
+
+#include <kunit/test.h>
+#include <kunit/test-bug.h>
+
+#define TEST_NUM_COLOROPS      3
+
+struct colorop_hook_calls {
+       int prepared;
+       int cleaned;
+       int prepare_seq;
+       struct drm_colorop_state *prepared_state;
+       struct drm_colorop_state *cleaned_state;
+};
+
+struct colorop_test_priv {
+       struct drm_device *drm;
+       struct drm_plane *plane;
+       struct drm_colorop *colorops[TEST_NUM_COLOROPS];
+       struct drm_atomic_commit *state;
+
+       /* Fault injection */
+       int colorop_fail_at;
+       int plane_prepare_ret;
+       int plane_begin_access_ret;
+
+       /* Recorded calls */
+       int seq;
+       struct colorop_hook_calls colorop[TEST_NUM_COLOROPS];
+       int plane_prepared;
+       int plane_prepare_seq;
+       int plane_cleaned;
+       int plane_end_access;
+};
+
+static struct colorop_test_priv *colorop_test_priv(void)
+{
+       return kunit_get_current_test()->priv;
+}
+
+static int colorop_test_prepare_fb(struct drm_colorop *colorop,
+                                  struct drm_colorop_state *new_state)
+{
+       struct colorop_test_priv *priv = colorop_test_priv();
+       struct colorop_hook_calls *calls = &priv->colorop[colorop->index];
+
+       calls->prepared++;
+       calls->prepare_seq = priv->seq++;
+       calls->prepared_state = new_state;
+
+       return (int)colorop->index == priv->colorop_fail_at ? -EIO : 0;
+}
+
+static void colorop_test_cleanup_fb(struct drm_colorop *colorop,
+                                   struct drm_colorop_state *state)
+{
+       struct colorop_test_priv *priv = colorop_test_priv();
+       struct colorop_hook_calls *calls = &priv->colorop[colorop->index];
+
+       calls->cleaned++;
+       calls->cleaned_state = state;
+}
+
+static const struct drm_colorop_helper_funcs colorop_test_helper_funcs = {
+       .prepare_fb = colorop_test_prepare_fb,
+       .cleanup_fb = colorop_test_cleanup_fb,
+};
+
+static const struct drm_colorop_funcs colorop_test_funcs = {
+       .destroy = drm_colorop_destroy,
+};
+
+static int plane_test_prepare_fb(struct drm_plane *plane,
+                                struct drm_plane_state *new_state)
+{
+       struct colorop_test_priv *priv = colorop_test_priv();
+
+       priv->plane_prepared++;
+       priv->plane_prepare_seq = priv->seq++;
+
+       return priv->plane_prepare_ret;
+}
+
+static void plane_test_cleanup_fb(struct drm_plane *plane,
+                                 struct drm_plane_state *state)
+{
+       colorop_test_priv()->plane_cleaned++;
+}
+
+static int plane_test_begin_fb_access(struct drm_plane *plane,
+                                     struct drm_plane_state *new_state)
+{
+       return colorop_test_priv()->plane_begin_access_ret;
+}
+
+static void plane_test_end_fb_access(struct drm_plane *plane,
+                                    struct drm_plane_state *new_state)
+{
+       colorop_test_priv()->plane_end_access++;
+}
+
+static const struct drm_plane_helper_funcs plane_test_helper_funcs = {
+       .prepare_fb = plane_test_prepare_fb,
+       .cleanup_fb = plane_test_cleanup_fb,
+       .begin_fb_access = plane_test_begin_fb_access,
+       .end_fb_access = plane_test_end_fb_access,
+};
+
+static void colorop_test_pipeline_destroy(void *drm)
+{
+       drm_colorop_pipeline_destroy(drm);
+}
+
+static void colorop_test_acquire_fini(void *ptr)
+{
+       struct drm_modeset_acquire_ctx *ctx = ptr;
+
+       drm_modeset_drop_locks(ctx);
+       drm_modeset_acquire_fini(ctx);
+}
+
+/*
+ * Create a plane with a chain of colorops, and an atomic state containing the
+ * plane and all colorops.
+ */
+static int colorop_test_init(struct kunit *test)
+{
+       struct drm_modeset_acquire_ctx *ctx;
+       struct colorop_test_priv *priv;
+       struct drm_plane_state *plane_state;
+       struct device *dev;
+       int i, ret;
+
+       priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_NULL(test, priv);
+       priv->colorop_fail_at = -1;
+       test->priv = priv;
+
+       dev = drm_kunit_helper_alloc_device(test);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+       priv->drm = __drm_kunit_helper_alloc_drm_device(test, dev,
+                                                       sizeof(*priv->drm), 0,
+                                                       DRIVER_MODESET | 
DRIVER_ATOMIC);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->drm);
+
+       priv->plane = drm_kunit_helper_create_primary_plane(test, priv->drm, 
NULL,
+                                                           
&plane_test_helper_funcs,
+                                                           NULL, 0, NULL);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->plane);
+
+       /* Destroy the colorops after the atomic state that references them. */
+       ret = kunit_add_action_or_reset(test, colorop_test_pipeline_destroy,
+                                       priv->drm);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+
+       for (i = 0; i < TEST_NUM_COLOROPS; i++) {
+               struct drm_colorop *colorop;
+
+               colorop = kzalloc_obj(*colorop);
+               KUNIT_ASSERT_NOT_NULL(test, colorop);
+
+               ret = drm_plane_colorop_mult_init(priv->drm, colorop, 
priv->plane,
+                                                 &colorop_test_funcs,
+                                                 
DRM_COLOROP_FLAG_ALLOW_BYPASS);
+               if (ret)
+                       kfree(colorop);
+               KUNIT_ASSERT_EQ(test, ret, 0);
+
+               drm_colorop_helper_add(colorop, &colorop_test_helper_funcs);
+               if (i)
+                       drm_colorop_set_next_property(priv->colorops[i - 1], 
colorop);
+               priv->colorops[i] = colorop;
+       }
+
+       drm_mode_config_reset(priv->drm);
+
+       ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_NULL(test, ctx);
+       drm_modeset_acquire_init(ctx, 0);
+       ret = kunit_add_action_or_reset(test, colorop_test_acquire_fini, ctx);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+
+       priv->state = drm_kunit_helper_atomic_state_alloc(test, priv->drm, ctx);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->state);
+
+       plane_state = drm_atomic_get_plane_state(priv->state, priv->plane);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, plane_state);
+
+       for (i = 0; i < TEST_NUM_COLOROPS; i++) {
+               struct drm_colorop_state *colorop_state;
+
+               colorop_state = drm_atomic_get_colorop_state(priv->state,
+                                                            priv->colorops[i]);
+               KUNIT_ASSERT_NOT_ERR_OR_NULL(test, colorop_state);
+       }
+
+       return 0;
+}
+
+static struct drm_colorop_state *
+colorop_test_new_state(struct colorop_test_priv *priv, int i)
+{
+       return drm_atomic_get_new_colorop_state(priv->state, priv->colorops[i]);
+}
+
+/*
+ * A successful prepare_planes() prepares every colorop in the commit once,
+ * before the planes, without cleaning anything up. Aborting the commit with
+ * unprepare_planes() then cleans up every new colorop state once.
+ */
+static void drm_test_colorop_prepare_then_unprepare(struct kunit *test)
+{
+       struct colorop_test_priv *priv = test->priv;
+       int i, ret;
+
+       ret = drm_atomic_helper_prepare_planes(priv->drm, priv->state);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+
+       KUNIT_EXPECT_EQ(test, priv->plane_prepared, 1);
+       for (i = 0; i < TEST_NUM_COLOROPS; i++) {
+               struct colorop_hook_calls *calls = &priv->colorop[i];
+
+               KUNIT_EXPECT_EQ(test, calls->prepared, 1);
+               KUNIT_EXPECT_PTR_EQ(test, calls->prepared_state,
+                                   colorop_test_new_state(priv, i));
+               KUNIT_EXPECT_LT(test, calls->prepare_seq, 
priv->plane_prepare_seq);
+               KUNIT_EXPECT_EQ(test, calls->cleaned, 0);
+       }
+
+       drm_atomic_helper_unprepare_planes(priv->drm, priv->state);
+
+       KUNIT_EXPECT_EQ(test, priv->plane_cleaned, 1);
+       KUNIT_EXPECT_EQ(test, priv->plane_end_access, 1);
+       for (i = 0; i < TEST_NUM_COLOROPS; i++) {
+               struct colorop_hook_calls *calls = &priv->colorop[i];
+
+               KUNIT_EXPECT_EQ(test, calls->cleaned, 1);
+               KUNIT_EXPECT_PTR_EQ(test, calls->cleaned_state,
+                                   colorop_test_new_state(priv, i));
+       }
+}
+
+/*
+ * After the state swap, cleanup_planes() cleans up the old colorop states,
+ * which were current before the commit, and leaves the new ones prepared.
+ */
+static void drm_test_colorop_cleanup_after_swap(struct kunit *test)
+{
+       struct colorop_test_priv *priv = test->priv;
+       struct drm_colorop_state *old_state[TEST_NUM_COLOROPS];
+       int i, ret;
+
+       for (i = 0; i < TEST_NUM_COLOROPS; i++)
+               old_state[i] = priv->colorops[i]->state;
+
+       ret = drm_atomic_helper_prepare_planes(priv->drm, priv->state);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+
+       ret = drm_atomic_helper_swap_state(priv->state, false);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+
+       drm_atomic_helper_cleanup_planes(priv->drm, priv->state);
+
+       KUNIT_EXPECT_EQ(test, priv->plane_cleaned, 1);
+       for (i = 0; i < TEST_NUM_COLOROPS; i++) {
+               struct colorop_hook_calls *calls = &priv->colorop[i];
+
+               KUNIT_EXPECT_PTR_EQ(test, priv->colorops[i]->state,
+                                   calls->prepared_state);
+               KUNIT_EXPECT_EQ(test, calls->cleaned, 1);
+               KUNIT_EXPECT_PTR_EQ(test, calls->cleaned_state, old_state[i]);
+       }
+}
+
+static const int colorop_fail_at_params[] = { 0, 1, TEST_NUM_COLOROPS - 1 };
+
+static void colorop_fail_at_desc(const int *fail_at, char *desc)
+{
+       snprintf(desc, KUNIT_PARAM_DESC_SIZE, "colorop %d", *fail_at);
+}
+
+KUNIT_ARRAY_PARAM(colorop_fail_at, colorop_fail_at_params, 
colorop_fail_at_desc);
+
+/*
+ * When a colorop fails to prepare, only the colorops prepared before it are
+ * cleaned up, and the planes are not prepared.
+ */
+static void drm_test_colorop_prepare_fail(struct kunit *test)
+{
+       struct colorop_test_priv *priv = test->priv;
+       int fail_at = *(const int *)test->param_value;
+       int i, ret;
+
+       priv->colorop_fail_at = fail_at;
+
+       ret = drm_atomic_helper_prepare_planes(priv->drm, priv->state);
+       KUNIT_EXPECT_EQ(test, ret, -EIO);
+
+       KUNIT_EXPECT_EQ(test, priv->plane_prepared, 0);
+       KUNIT_EXPECT_EQ(test, priv->plane_cleaned, 0);
+       for (i = 0; i < TEST_NUM_COLOROPS; i++) {
+               struct colorop_hook_calls *calls = &priv->colorop[i];
+
+               KUNIT_EXPECT_EQ(test, calls->prepared, i <= fail_at);
+               KUNIT_EXPECT_EQ(test, calls->cleaned, i < fail_at);
+               if (i < fail_at)
+                       KUNIT_EXPECT_PTR_EQ(test, calls->cleaned_state,
+                                           colorop_test_new_state(priv, i));
+       }
+}
+
+/*
+ * When a plane fails to prepare after the colorops, every colorop is cleaned
+ * up once. The failing plane is not.
+ */
+static void drm_test_colorop_plane_prepare_fail(struct kunit *test)
+{
+       struct colorop_test_priv *priv = test->priv;
+       int i, ret;
+
+       priv->plane_prepare_ret = -EIO;
+
+       ret = drm_atomic_helper_prepare_planes(priv->drm, priv->state);
+       KUNIT_EXPECT_EQ(test, ret, -EIO);
+
+       KUNIT_EXPECT_EQ(test, priv->plane_prepared, 1);
+       KUNIT_EXPECT_EQ(test, priv->plane_cleaned, 0);
+       for (i = 0; i < TEST_NUM_COLOROPS; i++) {
+               struct colorop_hook_calls *calls = &priv->colorop[i];
+
+               KUNIT_EXPECT_EQ(test, calls->prepared, 1);
+               KUNIT_EXPECT_EQ(test, calls->cleaned, 1);
+               KUNIT_EXPECT_PTR_EQ(test, calls->cleaned_state,
+                                   colorop_test_new_state(priv, i));
+       }
+}
+
+/*
+ * When a plane fails begin_fb_access, the prepared plane and every colorop
+ * are cleaned up once.
+ */
+static void drm_test_colorop_plane_begin_access_fail(struct kunit *test)
+{
+       struct colorop_test_priv *priv = test->priv;
+       int i, ret;
+
+       priv->plane_begin_access_ret = -EIO;
+
+       ret = drm_atomic_helper_prepare_planes(priv->drm, priv->state);
+       KUNIT_EXPECT_EQ(test, ret, -EIO);
+
+       KUNIT_EXPECT_EQ(test, priv->plane_prepared, 1);
+       KUNIT_EXPECT_EQ(test, priv->plane_cleaned, 1);
+       KUNIT_EXPECT_EQ(test, priv->plane_end_access, 0);
+       for (i = 0; i < TEST_NUM_COLOROPS; i++) {
+               struct colorop_hook_calls *calls = &priv->colorop[i];
+
+               KUNIT_EXPECT_EQ(test, calls->prepared, 1);
+               KUNIT_EXPECT_EQ(test, calls->cleaned, 1);
+               KUNIT_EXPECT_PTR_EQ(test, calls->cleaned_state,
+                                   colorop_test_new_state(priv, i));
+       }
+}
+
+static struct kunit_case drm_colorop_helper_tests[] = {
+       KUNIT_CASE(drm_test_colorop_prepare_then_unprepare),
+       KUNIT_CASE(drm_test_colorop_cleanup_after_swap),
+       KUNIT_CASE_PARAM(drm_test_colorop_prepare_fail, 
colorop_fail_at_gen_params),
+       KUNIT_CASE(drm_test_colorop_plane_prepare_fail),
+       KUNIT_CASE(drm_test_colorop_plane_begin_access_fail),
+       { }
+};
+
+static struct kunit_suite drm_colorop_helper_test_suite = {
+       .name = "drm_colorop_helper",
+       .init = colorop_test_init,
+       .test_cases = drm_colorop_helper_tests,
+};
+
+kunit_test_suite(drm_colorop_helper_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests for the colorop atomic helper hooks");
+MODULE_LICENSE("GPL");
-- 
2.56.0

Reply via email to