diff mbox series

[v2,02/33] drm/tests: Add Kunit Helpers

Message ID 20220728-rpi-analog-tv-properties-v2-2-f733a0ed9f90@cerno.tech (mailing list archive)
State New, archived
Headers show
Series drm: Analog TV Improvements | expand

Commit Message

Maxime Ripard Sept. 22, 2022, 2:25 p.m. UTC
As the number of kunit tests in KMS grows further, we start to have
multiple test suites that, for example, need to register a mock DRM
driver to interact with the KMS function they are supposed to test.

Let's add a file meant to provide those kind of helpers to avoid
duplication.

Signed-off-by: Maxime Ripard <maxime@cerno.tech>

Comments

Noralf Trønnes Sept. 24, 2022, 5:56 p.m. UTC | #1
Den 22.09.2022 16.25, skrev Maxime Ripard:
> As the number of kunit tests in KMS grows further, we start to have
> multiple test suites that, for example, need to register a mock DRM
> driver to interact with the KMS function they are supposed to test.
> 
> Let's add a file meant to provide those kind of helpers to avoid
> duplication.
> 
> Signed-off-by: Maxime Ripard <maxime@cerno.tech>
> 
> diff --git a/drivers/gpu/drm/tests/Makefile b/drivers/gpu/drm/tests/Makefile
> index 2d9f49b62ecb..b29ef1085cad 100644
> --- a/drivers/gpu/drm/tests/Makefile
> +++ b/drivers/gpu/drm/tests/Makefile
> @@ -8,6 +8,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
>  	drm_format_helper_test.o \
>  	drm_format_test.o \
>  	drm_framebuffer_test.o \
> +	drm_kunit_helpers.o \
>  	drm_mm_test.o \
>  	drm_plane_helper_test.o \
>  	drm_rect_test.o
> diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
> new file mode 100644
> index 000000000000..7ebd620481c1
> --- /dev/null
> +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
> @@ -0,0 +1,54 @@
> +#include <drm/drm_drv.h>
> +#include <drm/drm_managed.h>
> +
> +#include <linux/device.h>
> +
> +static const struct drm_mode_config_funcs drm_mode_config_funcs = {
> +};
> +
> +static const struct drm_driver drm_mode_driver = {
> +};
> +
> +static void drm_kunit_free_device(struct drm_device *drm, void *ptr)
> +{
> +	struct device *dev = ptr;
> +
> +	root_device_unregister(dev);
> +}
> +
> +struct drm_device *drm_kunit_device_init(const char *name)
> +{
> +	struct drm_device *drm;
> +	struct device *dev;
> +	int ret;
> +
> +	dev = root_device_register(name);
> +	if (IS_ERR(dev))
> +		return ERR_CAST(dev);
> +
> +	drm = drm_dev_alloc(&drm_mode_driver, dev);

I can't find drm being freed anywhere?
Maybe you could assign it to drm->managed.final_kfree.

Noralf.

> +	if (IS_ERR(drm)) {
> +		root_device_unregister(dev);
> +		return ERR_CAST(drm);
> +	}
> +	drm->mode_config.funcs = &drm_mode_config_funcs;
> +
> +	ret = drmm_add_action_or_reset(drm, drm_kunit_free_device, dev);
> +	if (ret)
> +		goto err_put_device;
> +
> +	ret = drmm_mode_config_init(drm);
> +	if (ret)
> +		return ERR_PTR(ret);
> +
> +	return drm;
> +
> +err_put_device:
> +	drm_dev_put(drm);
> +	return ERR_PTR(ret);
> +}
> +
> +void drm_kunit_device_exit(struct drm_device *drm)
> +{
> +	drm_dev_put(drm);
> +}
> diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.h b/drivers/gpu/drm/tests/drm_kunit_helpers.h
> new file mode 100644
> index 000000000000..5015a327a8c1
> --- /dev/null
> +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.h
> @@ -0,0 +1,9 @@
> +#ifndef DRM_KUNIT_HELPERS_H_
> +#define DRM_KUNIT_HELPERS_H_
> +
> +struct drm_device;
> +
> +struct drm_device *drm_kunit_device_init(const char *name);
> +void drm_kunit_device_exit(struct drm_device *drm);
> +
> +#endif // DRM_KUNIT_HELPERS_H_
>
Noralf Trønnes Sept. 24, 2022, 6:06 p.m. UTC | #2
Den 24.09.2022 19.56, skrev Noralf Trønnes:
> 
> 
> Den 22.09.2022 16.25, skrev Maxime Ripard:
>> As the number of kunit tests in KMS grows further, we start to have
>> multiple test suites that, for example, need to register a mock DRM
>> driver to interact with the KMS function they are supposed to test.
>>
>> Let's add a file meant to provide those kind of helpers to avoid
>> duplication.
>>
>> Signed-off-by: Maxime Ripard <maxime@cerno.tech>
>>
>> diff --git a/drivers/gpu/drm/tests/Makefile b/drivers/gpu/drm/tests/Makefile
>> index 2d9f49b62ecb..b29ef1085cad 100644
>> --- a/drivers/gpu/drm/tests/Makefile
>> +++ b/drivers/gpu/drm/tests/Makefile
>> @@ -8,6 +8,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
>>  	drm_format_helper_test.o \
>>  	drm_format_test.o \
>>  	drm_framebuffer_test.o \
>> +	drm_kunit_helpers.o \
>>  	drm_mm_test.o \
>>  	drm_plane_helper_test.o \
>>  	drm_rect_test.o
>> diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
>> new file mode 100644
>> index 000000000000..7ebd620481c1
>> --- /dev/null
>> +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
>> @@ -0,0 +1,54 @@
>> +#include <drm/drm_drv.h>
>> +#include <drm/drm_managed.h>
>> +
>> +#include <linux/device.h>
>> +
>> +static const struct drm_mode_config_funcs drm_mode_config_funcs = {
>> +};
>> +
>> +static const struct drm_driver drm_mode_driver = {
>> +};
>> +
>> +static void drm_kunit_free_device(struct drm_device *drm, void *ptr)
>> +{
>> +	struct device *dev = ptr;
>> +
>> +	root_device_unregister(dev);
>> +}
>> +
>> +struct drm_device *drm_kunit_device_init(const char *name)
>> +{
>> +	struct drm_device *drm;
>> +	struct device *dev;
>> +	int ret;
>> +
>> +	dev = root_device_register(name);
>> +	if (IS_ERR(dev))
>> +		return ERR_CAST(dev);
>> +
>> +	drm = drm_dev_alloc(&drm_mode_driver, dev);
> 
> I can't find drm being freed anywhere?
> Maybe you could assign it to drm->managed.final_kfree.
> 

Perhaps a better solution would be to use devm_drm_dev_alloc() and
unregister the root device on exit. That avoids reaching into the drm
managed internals and it looks more like a regular driver.

> Noralf.
> 
>> +	if (IS_ERR(drm)) {
>> +		root_device_unregister(dev);
>> +		return ERR_CAST(drm);
>> +	}
>> +	drm->mode_config.funcs = &drm_mode_config_funcs;
>> +
>> +	ret = drmm_add_action_or_reset(drm, drm_kunit_free_device, dev);
>> +	if (ret)
>> +		goto err_put_device;
>> +
>> +	ret = drmm_mode_config_init(drm);
>> +	if (ret)
>> +		return ERR_PTR(ret);
>> +
>> +	return drm;
>> +
>> +err_put_device:
>> +	drm_dev_put(drm);
>> +	return ERR_PTR(ret);
>> +}
>> +
>> +void drm_kunit_device_exit(struct drm_device *drm)
>> +{
>> +	drm_dev_put(drm);
>> +}
>> diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.h b/drivers/gpu/drm/tests/drm_kunit_helpers.h
>> new file mode 100644
>> index 000000000000..5015a327a8c1
>> --- /dev/null
>> +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.h
>> @@ -0,0 +1,9 @@
>> +#ifndef DRM_KUNIT_HELPERS_H_
>> +#define DRM_KUNIT_HELPERS_H_
>> +
>> +struct drm_device;
>> +
>> +struct drm_device *drm_kunit_device_init(const char *name);
>> +void drm_kunit_device_exit(struct drm_device *drm);
>> +
>> +#endif // DRM_KUNIT_HELPERS_H_
>>
Maxime Ripard Sept. 26, 2022, 9:36 a.m. UTC | #3
Hi Noralf,

On Sat, Sep 24, 2022 at 08:06:17PM +0200, Noralf Trønnes wrote:
> Den 24.09.2022 19.56, skrev Noralf Trønnes:
> > 
> > 
> > Den 22.09.2022 16.25, skrev Maxime Ripard:
> >> As the number of kunit tests in KMS grows further, we start to have
> >> multiple test suites that, for example, need to register a mock DRM
> >> driver to interact with the KMS function they are supposed to test.
> >>
> >> Let's add a file meant to provide those kind of helpers to avoid
> >> duplication.
> >>
> >> Signed-off-by: Maxime Ripard <maxime@cerno.tech>
> >>
> >> diff --git a/drivers/gpu/drm/tests/Makefile b/drivers/gpu/drm/tests/Makefile
> >> index 2d9f49b62ecb..b29ef1085cad 100644
> >> --- a/drivers/gpu/drm/tests/Makefile
> >> +++ b/drivers/gpu/drm/tests/Makefile
> >> @@ -8,6 +8,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
> >>  	drm_format_helper_test.o \
> >>  	drm_format_test.o \
> >>  	drm_framebuffer_test.o \
> >> +	drm_kunit_helpers.o \
> >>  	drm_mm_test.o \
> >>  	drm_plane_helper_test.o \
> >>  	drm_rect_test.o
> >> diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
> >> new file mode 100644
> >> index 000000000000..7ebd620481c1
> >> --- /dev/null
> >> +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
> >> @@ -0,0 +1,54 @@
> >> +#include <drm/drm_drv.h>
> >> +#include <drm/drm_managed.h>
> >> +
> >> +#include <linux/device.h>
> >> +
> >> +static const struct drm_mode_config_funcs drm_mode_config_funcs = {
> >> +};
> >> +
> >> +static const struct drm_driver drm_mode_driver = {
> >> +};
> >> +
> >> +static void drm_kunit_free_device(struct drm_device *drm, void *ptr)
> >> +{
> >> +	struct device *dev = ptr;
> >> +
> >> +	root_device_unregister(dev);
> >> +}
> >> +
> >> +struct drm_device *drm_kunit_device_init(const char *name)
> >> +{
> >> +	struct drm_device *drm;
> >> +	struct device *dev;
> >> +	int ret;
> >> +
> >> +	dev = root_device_register(name);
> >> +	if (IS_ERR(dev))
> >> +		return ERR_CAST(dev);
> >> +
> >> +	drm = drm_dev_alloc(&drm_mode_driver, dev);
> > 
> > I can't find drm being freed anywhere?
> > Maybe you could assign it to drm->managed.final_kfree.

There's a drm_dev_put in the test_exit hook which should free it.

> Perhaps a better solution would be to use devm_drm_dev_alloc() and
> unregister the root device on exit. That avoids reaching into the drm
> managed internals and it looks more like a regular driver.

But yeah, this is a good idea, I'll do it.

Maxime
Noralf Trønnes Sept. 26, 2022, 12:15 p.m. UTC | #4
Den 26.09.2022 11.36, skrev Maxime Ripard:
> Hi Noralf,
> 
> On Sat, Sep 24, 2022 at 08:06:17PM +0200, Noralf Trønnes wrote:
>> Den 24.09.2022 19.56, skrev Noralf Trønnes:
>>>
>>>
>>> Den 22.09.2022 16.25, skrev Maxime Ripard:
>>>> As the number of kunit tests in KMS grows further, we start to have
>>>> multiple test suites that, for example, need to register a mock DRM
>>>> driver to interact with the KMS function they are supposed to test.
>>>>
>>>> Let's add a file meant to provide those kind of helpers to avoid
>>>> duplication.
>>>>
>>>> Signed-off-by: Maxime Ripard <maxime@cerno.tech>
>>>>
>>>> diff --git a/drivers/gpu/drm/tests/Makefile b/drivers/gpu/drm/tests/Makefile
>>>> index 2d9f49b62ecb..b29ef1085cad 100644
>>>> --- a/drivers/gpu/drm/tests/Makefile
>>>> +++ b/drivers/gpu/drm/tests/Makefile
>>>> @@ -8,6 +8,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
>>>>  	drm_format_helper_test.o \
>>>>  	drm_format_test.o \
>>>>  	drm_framebuffer_test.o \
>>>> +	drm_kunit_helpers.o \
>>>>  	drm_mm_test.o \
>>>>  	drm_plane_helper_test.o \
>>>>  	drm_rect_test.o
>>>> diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
>>>> new file mode 100644
>>>> index 000000000000..7ebd620481c1
>>>> --- /dev/null
>>>> +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
>>>> @@ -0,0 +1,54 @@
>>>> +#include <drm/drm_drv.h>
>>>> +#include <drm/drm_managed.h>
>>>> +
>>>> +#include <linux/device.h>
>>>> +
>>>> +static const struct drm_mode_config_funcs drm_mode_config_funcs = {
>>>> +};
>>>> +
>>>> +static const struct drm_driver drm_mode_driver = {
>>>> +};
>>>> +
>>>> +static void drm_kunit_free_device(struct drm_device *drm, void *ptr)
>>>> +{
>>>> +	struct device *dev = ptr;
>>>> +
>>>> +	root_device_unregister(dev);
>>>> +}
>>>> +
>>>> +struct drm_device *drm_kunit_device_init(const char *name)
>>>> +{
>>>> +	struct drm_device *drm;
>>>> +	struct device *dev;
>>>> +	int ret;
>>>> +
>>>> +	dev = root_device_register(name);
>>>> +	if (IS_ERR(dev))
>>>> +		return ERR_CAST(dev);
>>>> +
>>>> +	drm = drm_dev_alloc(&drm_mode_driver, dev);
>>>
>>> I can't find drm being freed anywhere?
>>> Maybe you could assign it to drm->managed.final_kfree.
> 
> There's a drm_dev_put in the test_exit hook which should free it.
> 

I see now, there's a drmm_add_final_kfree() in drm_dev_alloc().

Noralf.

>> Perhaps a better solution would be to use devm_drm_dev_alloc() and
>> unregister the root device on exit. That avoids reaching into the drm
>> managed internals and it looks more like a regular driver.
> 
> But yeah, this is a good idea, I'll do it.
> 
> Maxime
diff mbox series

Patch

diff --git a/drivers/gpu/drm/tests/Makefile b/drivers/gpu/drm/tests/Makefile
index 2d9f49b62ecb..b29ef1085cad 100644
--- a/drivers/gpu/drm/tests/Makefile
+++ b/drivers/gpu/drm/tests/Makefile
@@ -8,6 +8,7 @@  obj-$(CONFIG_DRM_KUNIT_TEST) += \
 	drm_format_helper_test.o \
 	drm_format_test.o \
 	drm_framebuffer_test.o \
+	drm_kunit_helpers.o \
 	drm_mm_test.o \
 	drm_plane_helper_test.o \
 	drm_rect_test.o
diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
new file mode 100644
index 000000000000..7ebd620481c1
--- /dev/null
+++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
@@ -0,0 +1,54 @@ 
+#include <drm/drm_drv.h>
+#include <drm/drm_managed.h>
+
+#include <linux/device.h>
+
+static const struct drm_mode_config_funcs drm_mode_config_funcs = {
+};
+
+static const struct drm_driver drm_mode_driver = {
+};
+
+static void drm_kunit_free_device(struct drm_device *drm, void *ptr)
+{
+	struct device *dev = ptr;
+
+	root_device_unregister(dev);
+}
+
+struct drm_device *drm_kunit_device_init(const char *name)
+{
+	struct drm_device *drm;
+	struct device *dev;
+	int ret;
+
+	dev = root_device_register(name);
+	if (IS_ERR(dev))
+		return ERR_CAST(dev);
+
+	drm = drm_dev_alloc(&drm_mode_driver, dev);
+	if (IS_ERR(drm)) {
+		root_device_unregister(dev);
+		return ERR_CAST(drm);
+	}
+	drm->mode_config.funcs = &drm_mode_config_funcs;
+
+	ret = drmm_add_action_or_reset(drm, drm_kunit_free_device, dev);
+	if (ret)
+		goto err_put_device;
+
+	ret = drmm_mode_config_init(drm);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return drm;
+
+err_put_device:
+	drm_dev_put(drm);
+	return ERR_PTR(ret);
+}
+
+void drm_kunit_device_exit(struct drm_device *drm)
+{
+	drm_dev_put(drm);
+}
diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.h b/drivers/gpu/drm/tests/drm_kunit_helpers.h
new file mode 100644
index 000000000000..5015a327a8c1
--- /dev/null
+++ b/drivers/gpu/drm/tests/drm_kunit_helpers.h
@@ -0,0 +1,9 @@ 
+#ifndef DRM_KUNIT_HELPERS_H_
+#define DRM_KUNIT_HELPERS_H_
+
+struct drm_device;
+
+struct drm_device *drm_kunit_device_init(const char *name);
+void drm_kunit_device_exit(struct drm_device *drm);
+
+#endif // DRM_KUNIT_HELPERS_H_