[PATCH v2 1/4] drm/tests: helpers: Add helper for drm_display_mode_from_cea_vic()

Jinjie Ruan posted 4 patches 1 month, 1 week ago
[PATCH v2 1/4] drm/tests: helpers: Add helper for drm_display_mode_from_cea_vic()
Posted by Jinjie Ruan 1 month, 1 week ago
As Maxime suggested, add a new helper
drm_kunit_helper_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.

Suggested-by: Maxime Ripard <mripard@kernel.org>
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 drivers/gpu/drm/tests/drm_kunit_helpers.c | 40 +++++++++++++++++++++++
 include/drm/drm_kunit_helpers.h           |  6 ++++
 2 files changed, 46 insertions(+)

diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
index aa62719dab0e..dc70bafcd394 100644
--- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
+++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
@@ -311,6 +311,46 @@ 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_helper_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_helper_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_helper_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..1e7fd4be550c 100644
--- a/include/drm/drm_kunit_helpers.h
+++ b/include/drm/drm_kunit_helpers.h
@@ -4,6 +4,7 @@
 #define DRM_KUNIT_HELPERS_H_
 
 #include <drm/drm_drv.h>
+#include <drm/drm_edid.h>
 
 #include <linux/device.h>
 
@@ -120,4 +121,9 @@ 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_helper_display_mode_from_cea_vic(struct kunit *test,
+					   struct drm_device *dev,
+					   u8 video_code);
+
 #endif // DRM_KUNIT_HELPERS_H_
-- 
2.34.1
Re: [PATCH v2 1/4] drm/tests: helpers: Add helper for drm_display_mode_from_cea_vic()
Posted by Maxime Ripard 1 month, 1 week ago
On Mon, Oct 14, 2024 at 08:52:01PM GMT, Jinjie Ruan wrote:
> As Maxime suggested, add a new helper
> drm_kunit_helper_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.
> 
> Suggested-by: Maxime Ripard <mripard@kernel.org>
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> ---
>  drivers/gpu/drm/tests/drm_kunit_helpers.c | 40 +++++++++++++++++++++++
>  include/drm/drm_kunit_helpers.h           |  6 ++++
>  2 files changed, 46 insertions(+)
> 
> diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
> index aa62719dab0e..dc70bafcd394 100644
> --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
> +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
> @@ -311,6 +311,46 @@ 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_helper_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_helper_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_helper_display_mode_from_cea_vic);
> +

I think you can drop the "helper" name there, it's usually reserved for
blanket implementation of DRM hooks. This one isn't a hook, so just
calling it drm_kunit_display_mode_from_cea_vic makes a bit more sense to
me.

>  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..1e7fd4be550c 100644
> --- a/include/drm/drm_kunit_helpers.h
> +++ b/include/drm/drm_kunit_helpers.h
> @@ -4,6 +4,7 @@
>  #define DRM_KUNIT_HELPERS_H_
>  
>  #include <drm/drm_drv.h>
> +#include <drm/drm_edid.h>
>
>  #include <linux/device.h>
>  
> @@ -120,4 +121,9 @@ 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_helper_display_mode_from_cea_vic(struct kunit *test,
> +					   struct drm_device *dev,
> +					   u8 video_code);

It's not clear to me what you need the drm_edid header, you just return
a drm_display_mode pointer so you can just forward declare the structure

Once fixed
Acked-by: Maxime Ripard <mripard@kernel.org>

Maxime
Re: [PATCH v2 1/4] drm/tests: helpers: Add helper for drm_display_mode_from_cea_vic()
Posted by Jinjie Ruan 1 month, 1 week ago

On 2024/10/16 17:35, Maxime Ripard wrote:
> On Mon, Oct 14, 2024 at 08:52:01PM GMT, Jinjie Ruan wrote:
>> As Maxime suggested, add a new helper
>> drm_kunit_helper_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.
>>
>> Suggested-by: Maxime Ripard <mripard@kernel.org>
>> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
>> ---
>>  drivers/gpu/drm/tests/drm_kunit_helpers.c | 40 +++++++++++++++++++++++
>>  include/drm/drm_kunit_helpers.h           |  6 ++++
>>  2 files changed, 46 insertions(+)
>>
>> diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
>> index aa62719dab0e..dc70bafcd394 100644
>> --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
>> +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
>> @@ -311,6 +311,46 @@ 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_helper_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_helper_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_helper_display_mode_from_cea_vic);
>> +
> 
> I think you can drop the "helper" name there, it's usually reserved for
> blanket implementation of DRM hooks. This one isn't a hook, so just
> calling it drm_kunit_display_mode_from_cea_vic makes a bit more sense to
> me.
> 
>>  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..1e7fd4be550c 100644
>> --- a/include/drm/drm_kunit_helpers.h
>> +++ b/include/drm/drm_kunit_helpers.h
>> @@ -4,6 +4,7 @@
>>  #define DRM_KUNIT_HELPERS_H_
>>  
>>  #include <drm/drm_drv.h>
>> +#include <drm/drm_edid.h>
>>
>>  #include <linux/device.h>
>>  
>> @@ -120,4 +121,9 @@ 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_helper_display_mode_from_cea_vic(struct kunit *test,
>> +					   struct drm_device *dev,
>> +					   u8 video_code);
> 
> It's not clear to me what you need the drm_edid header, you just return
> a drm_display_mode pointer so you can just forward declare the structure


There is a compile error without the header,because there is no
"drm_display_mode_from_cea_vic()" declare.

drivers/gpu/drm/tests/drm_kunit_helpers.c:341:16: error: implicit
declaration of function ‘drm_display_mode_from_cea_vic’; did you mean
‘drm_kunit_display_mode_from_cea_vic’?
[-Werror=implicit-function-declaration]
  341 |         mode = drm_display_mode_from_cea_vic(dev, video_code);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                drm_kunit_display_mode_from_cea_vic
drivers/gpu/drm/tests/drm_kunit_helpers.c:341:14: warning: assignment to
‘struct drm_display_mode *’ from ‘int’ makes pointer from integer
without a cast [-Wint-conversion]
  341 |         mode = drm_display_mode_from_cea_vic(dev, video_code);
      |              ^


> 
> Once fixed
> Acked-by: Maxime Ripard <mripard@kernel.org>
> 
> Maxime
Re: [PATCH v2 1/4] drm/tests: helpers: Add helper for drm_display_mode_from_cea_vic()
Posted by Maxime Ripard 1 month, 1 week ago
On Thu, Oct 17, 2024 at 09:33:07AM GMT, Jinjie Ruan wrote:
> >> diff --git a/include/drm/drm_kunit_helpers.h b/include/drm/drm_kunit_helpers.h
> >> index e7cc17ee4934..1e7fd4be550c 100644
> >> --- a/include/drm/drm_kunit_helpers.h
> >> +++ b/include/drm/drm_kunit_helpers.h
> >> @@ -4,6 +4,7 @@
> >>  #define DRM_KUNIT_HELPERS_H_
> >>  
> >>  #include <drm/drm_drv.h>
> >> +#include <drm/drm_edid.h>
> >>
> >>  #include <linux/device.h>
> >>  
> >> @@ -120,4 +121,9 @@ 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_helper_display_mode_from_cea_vic(struct kunit *test,
> >> +					   struct drm_device *dev,
> >> +					   u8 video_code);
> > 
> > It's not clear to me what you need the drm_edid header, you just return
> > a drm_display_mode pointer so you can just forward declare the structure
> 
> 
> There is a compile error without the header,because there is no
> "drm_display_mode_from_cea_vic()" declare.
> 
> drivers/gpu/drm/tests/drm_kunit_helpers.c:341:16: error: implicit
> declaration of function ‘drm_display_mode_from_cea_vic’; did you mean
> ‘drm_kunit_display_mode_from_cea_vic’?
> [-Werror=implicit-function-declaration]
>   341 |         mode = drm_display_mode_from_cea_vic(dev, video_code);
>       |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>       |                drm_kunit_display_mode_from_cea_vic
> drivers/gpu/drm/tests/drm_kunit_helpers.c:341:14: warning: assignment to
> ‘struct drm_display_mode *’ from ‘int’ makes pointer from integer
> without a cast [-Wint-conversion]
>   341 |         mode = drm_display_mode_from_cea_vic(dev, video_code);
>       |              ^

Right, but the error is in the C file, not the header.

Maxime
Re: [PATCH v2 1/4] drm/tests: helpers: Add helper for drm_display_mode_from_cea_vic()
Posted by Jinjie Ruan 1 month, 1 week ago

On 2024/10/17 20:13, Maxime Ripard wrote:
> On Thu, Oct 17, 2024 at 09:33:07AM GMT, Jinjie Ruan wrote:
>>>> diff --git a/include/drm/drm_kunit_helpers.h b/include/drm/drm_kunit_helpers.h
>>>> index e7cc17ee4934..1e7fd4be550c 100644
>>>> --- a/include/drm/drm_kunit_helpers.h
>>>> +++ b/include/drm/drm_kunit_helpers.h
>>>> @@ -4,6 +4,7 @@
>>>>  #define DRM_KUNIT_HELPERS_H_
>>>>  
>>>>  #include <drm/drm_drv.h>
>>>> +#include <drm/drm_edid.h>
>>>>
>>>>  #include <linux/device.h>
>>>>  
>>>> @@ -120,4 +121,9 @@ 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_helper_display_mode_from_cea_vic(struct kunit *test,
>>>> +					   struct drm_device *dev,
>>>> +					   u8 video_code);
>>>
>>> It's not clear to me what you need the drm_edid header, you just return
>>> a drm_display_mode pointer so you can just forward declare the structure
>>
>>
>> There is a compile error without the header,because there is no
>> "drm_display_mode_from_cea_vic()" declare.
>>
>> drivers/gpu/drm/tests/drm_kunit_helpers.c:341:16: error: implicit
>> declaration of function ‘drm_display_mode_from_cea_vic’; did you mean
>> ‘drm_kunit_display_mode_from_cea_vic’?
>> [-Werror=implicit-function-declaration]
>>   341 |         mode = drm_display_mode_from_cea_vic(dev, video_code);
>>       |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>       |                drm_kunit_display_mode_from_cea_vic
>> drivers/gpu/drm/tests/drm_kunit_helpers.c:341:14: warning: assignment to
>> ‘struct drm_display_mode *’ from ‘int’ makes pointer from integer
>> without a cast [-Wint-conversion]
>>   341 |         mode = drm_display_mode_from_cea_vic(dev, video_code);
>>       |              ^
> > Right, but the error is in the C file, not the header.

Yes, I have updated it to C file in V3, thank you!

> 
> Maxime