[PATCH 1/3] drm/connector: hdmi: Fix memory leak in drm_display_mode_from_cea_vic()

From: Jinjie Ruan
Date: Mon Oct 14 2024 - 03:17:53 EST


modprobe drm_connector_test and then rmmod drm_connector_test,
the following memory leak occurs.

The `mode` allocated in drm_mode_duplicate() called by
drm_display_mode_from_cea_vic() is not freed, which cause the memory leak:

unreferenced object 0xffffff80cb0ee400 (size 128):
comm "kunit_try_catch", pid 1948, jiffies 4294950339
hex dump (first 32 bytes):
14 44 02 00 80 07 d8 07 04 08 98 08 00 00 38 04 .D............8.
3c 04 41 04 65 04 00 00 05 00 00 00 00 00 00 00 <.A.e...........
backtrace (crc 90e9585c):
[<00000000ec42e3d7>] kmemleak_alloc+0x34/0x40
[<00000000d0ef055a>] __kmalloc_cache_noprof+0x26c/0x2f4
[<00000000c2062161>] drm_mode_duplicate+0x44/0x19c
[<00000000f96c74aa>] drm_display_mode_from_cea_vic+0x88/0x98
[<00000000d8f2c8b4>] 0xffffffdc982a4868
[<000000005d164dbc>] kunit_try_run_case+0x13c/0x3ac
[<000000006fb23398>] kunit_generic_run_threadfn_adapter+0x80/0xec
[<000000006ea56ca0>] kthread+0x2e8/0x374
[<000000000676063f>] ret_from_fork+0x10/0x20
......

Free `mode` by calling drm_mode_destroy() to fix it.

Cc: stable@xxxxxxxxxxxxxxx
Fixes: abb6f74973e2 ("drm/tests: Add HDMI TDMS character rate tests")
Signed-off-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
---
drivers/gpu/drm/tests/drm_connector_test.c | 24 ++++++++++++++++++++++
1 file changed, 24 insertions(+)

diff --git a/drivers/gpu/drm/tests/drm_connector_test.c b/drivers/gpu/drm/tests/drm_connector_test.c
index 15e36a8db685..9c94d26b3486 100644
--- a/drivers/gpu/drm/tests/drm_connector_test.c
+++ b/drivers/gpu/drm/tests/drm_connector_test.c
@@ -1004,6 +1004,8 @@ static void drm_test_drm_hdmi_compute_mode_clock_rgb(struct kunit *test)
rate = drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_RGB);
KUNIT_ASSERT_GT(test, rate, 0);
KUNIT_EXPECT_EQ(test, mode->clock * 1000ULL, rate);
+
+ drm_mode_destroy(drm, mode);
}

/*
@@ -1025,6 +1027,8 @@ static void drm_test_drm_hdmi_compute_mode_clock_rgb_10bpc(struct kunit *test)
rate = drm_hdmi_compute_mode_clock(mode, 10, HDMI_COLORSPACE_RGB);
KUNIT_ASSERT_GT(test, rate, 0);
KUNIT_EXPECT_EQ(test, mode->clock * 1250, rate);
+
+ drm_mode_destroy(drm, mode);
}

/*
@@ -1043,6 +1047,8 @@ static void drm_test_drm_hdmi_compute_mode_clock_rgb_10bpc_vic_1(struct kunit *t

rate = drm_hdmi_compute_mode_clock(mode, 10, HDMI_COLORSPACE_RGB);
KUNIT_EXPECT_EQ(test, rate, 0);
+
+ drm_mode_destroy(drm, mode);
}

/*
@@ -1064,6 +1070,8 @@ static void drm_test_drm_hdmi_compute_mode_clock_rgb_12bpc(struct kunit *test)
rate = drm_hdmi_compute_mode_clock(mode, 12, HDMI_COLORSPACE_RGB);
KUNIT_ASSERT_GT(test, rate, 0);
KUNIT_EXPECT_EQ(test, mode->clock * 1500, rate);
+
+ drm_mode_destroy(drm, mode);
}

/*
@@ -1082,6 +1090,8 @@ static void drm_test_drm_hdmi_compute_mode_clock_rgb_12bpc_vic_1(struct kunit *t

rate = drm_hdmi_compute_mode_clock(mode, 12, HDMI_COLORSPACE_RGB);
KUNIT_EXPECT_EQ(test, rate, 0);
+
+ drm_mode_destroy(drm, mode);
}

/*
@@ -1103,6 +1113,8 @@ static void drm_test_drm_hdmi_compute_mode_clock_rgb_double(struct kunit *test)
rate = drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_RGB);
KUNIT_ASSERT_GT(test, rate, 0);
KUNIT_EXPECT_EQ(test, (mode->clock * 1000ULL) * 2, rate);
+
+ drm_mode_destroy(drm, mode);
}

/*
@@ -1126,6 +1138,8 @@ static void drm_test_connector_hdmi_compute_mode_clock_yuv420_valid(struct kunit
rate = drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_YUV420);
KUNIT_ASSERT_GT(test, rate, 0);
KUNIT_EXPECT_EQ(test, (mode->clock * 1000ULL) / 2, rate);
+
+ drm_mode_destroy(drm, mode);
}

static const unsigned int drm_hdmi_compute_mode_clock_yuv420_vic_valid_tests[] = {
@@ -1164,6 +1178,8 @@ static void drm_test_connector_hdmi_compute_mode_clock_yuv420_10_bpc(struct kuni
KUNIT_ASSERT_GT(test, rate, 0);

KUNIT_EXPECT_EQ(test, mode->clock * 625, rate);
+
+ drm_mode_destroy(drm, mode);
}

/*
@@ -1189,6 +1205,8 @@ static void drm_test_connector_hdmi_compute_mode_clock_yuv420_12_bpc(struct kuni
KUNIT_ASSERT_GT(test, rate, 0);

KUNIT_EXPECT_EQ(test, mode->clock * 750, rate);
+
+ drm_mode_destroy(drm, mode);
}

/*
@@ -1211,6 +1229,8 @@ static void drm_test_connector_hdmi_compute_mode_clock_yuv422_8_bpc(struct kunit
rate = drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_YUV422);
KUNIT_ASSERT_GT(test, rate, 0);
KUNIT_EXPECT_EQ(test, mode->clock * 1000, rate);
+
+ drm_mode_destroy(drm, mode);
}

/*
@@ -1233,6 +1253,8 @@ static void drm_test_connector_hdmi_compute_mode_clock_yuv422_10_bpc(struct kuni
rate = drm_hdmi_compute_mode_clock(mode, 10, HDMI_COLORSPACE_YUV422);
KUNIT_ASSERT_GT(test, rate, 0);
KUNIT_EXPECT_EQ(test, mode->clock * 1000, rate);
+
+ drm_mode_destroy(drm, mode);
}

/*
@@ -1255,6 +1277,8 @@ static void drm_test_connector_hdmi_compute_mode_clock_yuv422_12_bpc(struct kuni
rate = drm_hdmi_compute_mode_clock(mode, 12, HDMI_COLORSPACE_YUV422);
KUNIT_ASSERT_GT(test, rate, 0);
KUNIT_EXPECT_EQ(test, mode->clock * 1000, rate);
+
+ drm_mode_destroy(drm, mode);
}

static struct kunit_case drm_hdmi_compute_mode_clock_tests[] = {
--
2.34.1