diff mbox series

[v3,1/4] drm/tests: helpers: Add helper for drm_display_mode_from_cea_vic()

Message ID 20241017063125.3080347-2-ruanjinjie@huawei.com (mailing list archive)
State New, archived
Headers show
Series drm/tests: Fix some memory leaks | expand

Commit Message

Jinjie Ruan Oct. 17, 2024, 6:31 a.m. UTC
As Maxime suggested, add a new helper
drm_kunit_display_mode_from_cea_vic(), it can replace the direct call
of drm_display_mode_from_cea_vic(), and it will help solving
the `mode` memory leaks.

Acked-by: Maxime Ripard <mripard@kernel.org>
Suggested-by: Maxime Ripard <mripard@kernel.org>
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
v3:
- Adjust drm/drm_edid.h header to drm_kunit_helpers.c.
- Drop the "helper" in the helper name.
- Add Acked-by.
---
 drivers/gpu/drm/tests/drm_kunit_helpers.c | 40 +++++++++++++++++++++++
 include/drm/drm_kunit_helpers.h           |  4 +++
 2 files changed, 44 insertions(+)

Comments

Maxime Ripard Oct. 29, 2024, 8:16 a.m. UTC | #1
On Thu, Oct 17, 2024 at 02:31:22PM +0800, Jinjie Ruan wrote:
> As Maxime suggested, add a new helper
> drm_kunit_display_mode_from_cea_vic(), it can replace the direct call
> of drm_display_mode_from_cea_vic(), and it will help solving
> the `mode` memory leaks.
> 
> Acked-by: Maxime Ripard <mripard@kernel.org>
> Suggested-by: Maxime Ripard <mripard@kernel.org>
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> ---
> v3:
> - Adjust drm/drm_edid.h header to drm_kunit_helpers.c.
> - Drop the "helper" in the helper name.
> - Add Acked-by.
> ---
>  drivers/gpu/drm/tests/drm_kunit_helpers.c | 40 +++++++++++++++++++++++
>  include/drm/drm_kunit_helpers.h           |  4 +++
>  2 files changed, 44 insertions(+)
> 
> diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
> index aa62719dab0e..565172990044 100644
> --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
> +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
> @@ -3,6 +3,7 @@
>  #include <drm/drm_atomic.h>
>  #include <drm/drm_atomic_helper.h>
>  #include <drm/drm_drv.h>
> +#include <drm/drm_edid.h>
>  #include <drm/drm_fourcc.h>
>  #include <drm/drm_kunit_helpers.h>
>  #include <drm/drm_managed.h>
> @@ -311,6 +312,45 @@ drm_kunit_helper_create_crtc(struct kunit *test,
>  }
>  EXPORT_SYMBOL_GPL(drm_kunit_helper_create_crtc);
>  
> +static void kunit_action_drm_mode_destroy(void *ptr)
> +{
> +	struct drm_display_mode *mode = ptr;
> +
> +	drm_mode_destroy(NULL, mode);
> +}
> +
> +/**
> + * drm_kunit_display_mode_from_cea_vic() - return a mode for CEA VIC
> +					   for a KUnit test
> + * @test: The test context object
> + * @dev: DRM device
> + * @video_code: CEA VIC of the mode
> + *
> + * Creates a new mode matching the specified CEA VIC for a KUnit test.
> + *
> + * Resources will be cleaned up automatically.
> + *
> + * Returns: A new drm_display_mode on success or NULL on failure
> + */
> +struct drm_display_mode *
> +drm_kunit_display_mode_from_cea_vic(struct kunit *test, struct drm_device *dev,
> +				    u8 video_code)
> +{
> +	struct drm_display_mode *mode;
> +	int ret;
> +
> +	mode = drm_display_mode_from_cea_vic(dev, video_code);

I'd rather return directly if mode is NULL here...

> +	ret = kunit_add_action_or_reset(test,
> +					kunit_action_drm_mode_destroy,
> +					mode);
> +	if (ret)
> +		return NULL;

Because it doesn't really make much sense to register a cleanup action
if we know that it's going to be useless, and possibly be executed right
away.

Maxime
Jinjie Ruan Oct. 30, 2024, 1:48 a.m. UTC | #2
On 2024/10/29 16:16, Maxime Ripard wrote:
> On Thu, Oct 17, 2024 at 02:31:22PM +0800, Jinjie Ruan wrote:
>> As Maxime suggested, add a new helper
>> drm_kunit_display_mode_from_cea_vic(), it can replace the direct call
>> of drm_display_mode_from_cea_vic(), and it will help solving
>> the `mode` memory leaks.
>>
>> Acked-by: Maxime Ripard <mripard@kernel.org>
>> Suggested-by: Maxime Ripard <mripard@kernel.org>
>> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
>> ---
>> v3:
>> - Adjust drm/drm_edid.h header to drm_kunit_helpers.c.
>> - Drop the "helper" in the helper name.
>> - Add Acked-by.
>> ---
>>  drivers/gpu/drm/tests/drm_kunit_helpers.c | 40 +++++++++++++++++++++++
>>  include/drm/drm_kunit_helpers.h           |  4 +++
>>  2 files changed, 44 insertions(+)
>>
>> diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
>> index aa62719dab0e..565172990044 100644
>> --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
>> +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
>> @@ -3,6 +3,7 @@
>>  #include <drm/drm_atomic.h>
>>  #include <drm/drm_atomic_helper.h>
>>  #include <drm/drm_drv.h>
>> +#include <drm/drm_edid.h>
>>  #include <drm/drm_fourcc.h>
>>  #include <drm/drm_kunit_helpers.h>
>>  #include <drm/drm_managed.h>
>> @@ -311,6 +312,45 @@ drm_kunit_helper_create_crtc(struct kunit *test,
>>  }
>>  EXPORT_SYMBOL_GPL(drm_kunit_helper_create_crtc);
>>  
>> +static void kunit_action_drm_mode_destroy(void *ptr)
>> +{
>> +	struct drm_display_mode *mode = ptr;
>> +
>> +	drm_mode_destroy(NULL, mode);
>> +}
>> +
>> +/**
>> + * drm_kunit_display_mode_from_cea_vic() - return a mode for CEA VIC
>> +					   for a KUnit test
>> + * @test: The test context object
>> + * @dev: DRM device
>> + * @video_code: CEA VIC of the mode
>> + *
>> + * Creates a new mode matching the specified CEA VIC for a KUnit test.
>> + *
>> + * Resources will be cleaned up automatically.
>> + *
>> + * Returns: A new drm_display_mode on success or NULL on failure
>> + */
>> +struct drm_display_mode *
>> +drm_kunit_display_mode_from_cea_vic(struct kunit *test, struct drm_device *dev,
>> +				    u8 video_code)
>> +{
>> +	struct drm_display_mode *mode;
>> +	int ret;
>> +
>> +	mode = drm_display_mode_from_cea_vic(dev, video_code);
> 
> I'd rather return directly if mode is NULL here...
> 
>> +	ret = kunit_add_action_or_reset(test,
>> +					kunit_action_drm_mode_destroy,
>> +					mode);
>> +	if (ret)
>> +		return NULL;
> 
> Because it doesn't really make much sense to register a cleanup action
> if we know that it's going to be useless, and possibly be executed right
> away.

Sure, will udpate it.

> 
> Maxime
diff mbox series

Patch

diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
index aa62719dab0e..565172990044 100644
--- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
+++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
@@ -3,6 +3,7 @@ 
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_drv.h>
+#include <drm/drm_edid.h>
 #include <drm/drm_fourcc.h>
 #include <drm/drm_kunit_helpers.h>
 #include <drm/drm_managed.h>
@@ -311,6 +312,45 @@  drm_kunit_helper_create_crtc(struct kunit *test,
 }
 EXPORT_SYMBOL_GPL(drm_kunit_helper_create_crtc);
 
+static void kunit_action_drm_mode_destroy(void *ptr)
+{
+	struct drm_display_mode *mode = ptr;
+
+	drm_mode_destroy(NULL, mode);
+}
+
+/**
+ * drm_kunit_display_mode_from_cea_vic() - return a mode for CEA VIC
+					   for a KUnit test
+ * @test: The test context object
+ * @dev: DRM device
+ * @video_code: CEA VIC of the mode
+ *
+ * Creates a new mode matching the specified CEA VIC for a KUnit test.
+ *
+ * Resources will be cleaned up automatically.
+ *
+ * Returns: A new drm_display_mode on success or NULL on failure
+ */
+struct drm_display_mode *
+drm_kunit_display_mode_from_cea_vic(struct kunit *test, struct drm_device *dev,
+				    u8 video_code)
+{
+	struct drm_display_mode *mode;
+	int ret;
+
+	mode = drm_display_mode_from_cea_vic(dev, video_code);
+
+	ret = kunit_add_action_or_reset(test,
+					kunit_action_drm_mode_destroy,
+					mode);
+	if (ret)
+		return NULL;
+
+	return mode;
+}
+EXPORT_SYMBOL_GPL(drm_kunit_display_mode_from_cea_vic);
+
 MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
 MODULE_DESCRIPTION("KUnit test suite helper functions");
 MODULE_LICENSE("GPL");
diff --git a/include/drm/drm_kunit_helpers.h b/include/drm/drm_kunit_helpers.h
index e7cc17ee4934..afdd46ef04f7 100644
--- a/include/drm/drm_kunit_helpers.h
+++ b/include/drm/drm_kunit_helpers.h
@@ -120,4 +120,8 @@  drm_kunit_helper_create_crtc(struct kunit *test,
 			     const struct drm_crtc_funcs *funcs,
 			     const struct drm_crtc_helper_funcs *helper_funcs);
 
+struct drm_display_mode *
+drm_kunit_display_mode_from_cea_vic(struct kunit *test, struct drm_device *dev,
+				    u8 video_code);
+
 #endif // DRM_KUNIT_HELPERS_H_