]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
drm/tests: Add test for drm_framebuffer_free()
authorCarlos Eduardo Gallo Filho <gcarlos@disroot.org>
Wed, 11 Sep 2024 00:15:34 +0000 (21:15 -0300)
committerMaxime Ripard <mripard@kernel.org>
Wed, 11 Sep 2024 12:17:11 +0000 (14:17 +0200)
Add a single KUnit test case for the drm_framebuffer_free function.

Signed-off-by: Carlos Eduardo Gallo Filho <gcarlos@disroot.org>
Acked-by: Maxime Ripard <mripard@kernel.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20240911001559.28284-10-gcarlos@disroot.org
Signed-off-by: Maxime Ripard <mripard@kernel.org>
drivers/gpu/drm/drm_mode_object.c
drivers/gpu/drm/tests/drm_framebuffer_test.c

index df4cc0e8e263d5887a799cf1a61d998234be7158..e943205a2394cd9c0d64b940a5e15ccff7f35246 100644 (file)
@@ -81,6 +81,7 @@ int drm_mode_object_add(struct drm_device *dev,
 {
        return __drm_mode_object_add(dev, obj, obj_type, true, NULL);
 }
+EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_mode_object_add);
 
 void drm_mode_object_register(struct drm_device *dev,
                              struct drm_mode_object *obj)
index 72314805839d8174bc2f0f43f760cd8e867ecbd1..6ea04cc8f3241ddf28cbfa5d4d25fe4976e09417 100644 (file)
@@ -358,6 +358,7 @@ static const struct drm_framebuffer_test drm_framebuffer_create_cases[] = {
 struct drm_framebuffer_test_priv {
        struct drm_device dev;
        bool buffer_created;
+       bool buffer_freed;
 };
 
 static struct drm_framebuffer *fb_create_mock(struct drm_device *dev,
@@ -649,10 +650,59 @@ static void drm_test_framebuffer_init_dev_mismatch(struct kunit *test)
        KUNIT_EXPECT_EQ(test, ret, -EINVAL);
 }
 
+static void destroy_free_mock(struct drm_framebuffer *fb)
+{
+       struct drm_framebuffer_test_priv *priv = container_of(fb->dev, typeof(*priv), dev);
+
+       priv->buffer_freed = true;
+}
+
+static struct drm_framebuffer_funcs framebuffer_funcs_free_mock = {
+       .destroy = destroy_free_mock,
+};
+
+/*
+ * In summary, the drm_framebuffer_free() function must implicitly call
+ * fb->funcs->destroy() and garantee that the framebufer object is unregistered
+ * from the drm_device idr pool.
+ */
+static void drm_test_framebuffer_free(struct kunit *test)
+{
+       struct drm_framebuffer_test_priv *priv = test->priv;
+       struct drm_device *dev = &priv->dev;
+       struct drm_mode_object *obj;
+       struct drm_framebuffer fb = {
+               .dev = dev,
+               .funcs = &framebuffer_funcs_free_mock,
+       };
+       int id, ret;
+
+       priv->buffer_freed = false;
+
+       /*
+        * Mock a framebuffer that was not unregistered at the moment of the
+        * drm_framebuffer_free() call.
+        */
+       ret = drm_mode_object_add(dev, &fb.base, DRM_MODE_OBJECT_FB);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+       id = fb.base.id;
+
+       drm_framebuffer_free(&fb.base.refcount);
+
+       /* The framebuffer object must be unregistered */
+       obj = drm_mode_object_find(dev, NULL, id, DRM_MODE_OBJECT_FB);
+       KUNIT_EXPECT_PTR_EQ(test, obj, NULL);
+       KUNIT_EXPECT_EQ(test, fb.base.id, 0);
+
+       /* Test if fb->funcs->destroy() was called */
+       KUNIT_EXPECT_EQ(test, priv->buffer_freed, true);
+}
+
 static struct kunit_case drm_framebuffer_tests[] = {
        KUNIT_CASE_PARAM(drm_test_framebuffer_check_src_coords, check_src_coords_gen_params),
        KUNIT_CASE(drm_test_framebuffer_cleanup),
        KUNIT_CASE_PARAM(drm_test_framebuffer_create, drm_framebuffer_create_gen_params),
+       KUNIT_CASE(drm_test_framebuffer_free),
        KUNIT_CASE(drm_test_framebuffer_init),
        KUNIT_CASE(drm_test_framebuffer_init_bad_format),
        KUNIT_CASE(drm_test_framebuffer_init_dev_mismatch),