Skip to contentSkip to conversation

KUnit tests for drm_format_helper

PATCH Superseded José Expósito 5 participants 13 messages Jun 6, 2022 – Jun 7, 2022

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover KUnit tests for drm_format_helper

Hello everyone,

Recently Javier added a new task in the ToDo list [1] to create KUnit tests for the functions present in "drm_format_helper".

This patch includes the changes suggested in the RFC version [2].

Best wishes,
José Expósito

[1] https://cgit.freedesktop.org/drm/drm-misc/commit/?id=596c35b1440e
[2] https://lore.kernel.org/dri-devel/[email protected]/T/

José Expósito (1):
drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

 drivers/gpu/drm/.kunitconfig             |   3 +
 drivers/gpu/drm/Kconfig                  |  16 +++
 drivers/gpu/drm/Makefile                 |   2 +
 drivers/gpu/drm/drm_format_helper_test.c | 166 +++++++++++++++++++++++
 4 files changed, 187 insertions(+)
 create mode 100644 drivers/gpu/drm/.kunitconfig
 create mode 100644 drivers/gpu/drm/drm_format_helper_test.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

Test the conversion from XRGB8888 to RGB332.

What is tested?

- Different values for the X in XRGB8888 to make sure it is ignored
- Different clip values: Single pixel and full and partial buffer
- Well known colors: White, black, red, green, blue, magenta, yellow and cyan
- Other colors: Randomly picked
- Destination pitch

How to run the tests?

 $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
         --kconfig_add CONFIG_VIRTIO_UML=y \
         --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

---

RFC -> v1: https://lore.kernel.org/dri-devel/[email protected]/T/

- Add .kunitconfig (Maxime Ripard)
- Fix memory leak (Daniel Latypov)
- Make config option generic (Javier Martinez Canillas):
DRM_FORMAR_HELPER_TEST -> DRM_KUNIT_TEST
- Remove DISABLE_STRUCTLEAK_PLUGIN (Daniel Latypov)

---
 drivers/gpu/drm/.kunitconfig             |   3 +
 drivers/gpu/drm/Kconfig                  |  16 +++
 drivers/gpu/drm/Makefile                 |   2 +
 drivers/gpu/drm/drm_format_helper_test.c | 166 +++++++++++++++++++++++
 4 files changed, 187 insertions(+)
 create mode 100644 drivers/gpu/drm/.kunitconfig
 create mode 100644 drivers/gpu/drm/drm_format_helper_test.c
drivers/gpu/drm/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..6ec04b4c979d
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_DRM=y
+CONFIG_DRM_KUNIT_TEST=y
drivers/gpu/drm/Kconfig +16 -0
index e88c497fa010..3c0b1faba439 100644
@@ -70,6 +70,22 @@ config DRM_DEBUG_SELFTEST
 
 	  If in doubt, say "N".
 
+config DRM_KUNIT_TEST
+	tristate "KUnit tests for DRM" if !KUNIT_ALL_TESTS
+	depends on DRM && KUNIT=y
+	select DRM_KMS_HELPER
+	default KUNIT_ALL_TESTS
+	help
+	  This builds unit tests for DRM. This option is not useful for
+	  distributions or general kernels, but only for kernel
+	  developers working on DRM and associated drivers.
+
+	  For more information on KUnit and unit tests in general,
+	  please refer to the KUnit documentation in
+	  Documentation/dev-tools/kunit/.
+
+	  If in doubt, say "N".
+
 config DRM_KMS_HELPER
 	tristate
 	depends on DRM
drivers/gpu/drm/Makefile +2 -0
index 15fe3163f822..6549471f09c7 100644
@@ -76,6 +76,8 @@ obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o
 #
 
 obj-$(CONFIG_DRM_DEBUG_SELFTEST) += selftests/
+obj-$(CONFIG_DRM_KUNIT_TEST) += drm_kms_helper.o \
+		drm_format_helper_test.o
 
 obj-$(CONFIG_DRM_MIPI_DBI) += drm_mipi_dbi.o
 obj-$(CONFIG_DRM_MIPI_DSI) += drm_mipi_dsi.o
drivers/gpu/drm/drm_format_helper_test.c new file +166 -0

166 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#2 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

Hello José,

Quoted 13 lines · José Expósito

Test the conversion from XRGB8888 to RGB332.

What is tested?

- Different values for the X in XRGB8888 to make sure it is ignored
- Different clip values: Single pixel and full and partial buffer
- Well known colors: White, black, red, green, blue, magenta, yellow and cyan
- Other colors: Randomly picked
- Destination pitch

How to run the tests?

Quoted 8 lines
 $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
         --kconfig_add CONFIG_VIRTIO_UML=y \
         --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

---

Thanks for addressing the issues pointed out. Patch looks good to me now.

By the way, I think you should request an account [0], so that you can push patches to drm-misc directly. Specially since AFAIU the plan is to add more KUnit tests in future patch series.

[0]: https://www.freedesktop.org/wiki/AccountRequests/

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Linux Engineering
Red Hat
#3 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

Hi!

Quoted 20 lines · Javier Martinez Canillas

Hello José,

Quoted 17 lines · José Expósito

Test the conversion from XRGB8888 to RGB332.

What is tested?

- Different values for the X in XRGB8888 to make sure it is ignored
- Different clip values: Single pixel and full and partial buffer
- Well known colors: White, black, red, green, blue, magenta, yellow and cyan
- Other colors: Randomly picked
- Destination pitch

How to run the tests?

 $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
         --kconfig_add CONFIG_VIRTIO_UML=y \
         --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y
Quoted 8 lines
Quoted 4 lines

---

Thanks for addressing the issues pointed out. Patch looks good to me now.

Thanks for the quick review Javier :)

Quoted 5 lines · Javier Martinez Canillas

By the way, I think you should request an account [0], so that you can push patches to drm-misc directly. Specially since AFAIU the plan is to add more KUnit tests in future patch series.

[0]: https://www.freedesktop.org/wiki/AccountRequests/

I'll request one, thanks for the tip.

-------

Quoted 11 lines · Maxime Ripard
Quoted 11 lines
Quoted 6 lines

The following works correctly but it won't use User Mode Linux:

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig --arch=x86_64

But then, can't we add them to .kunitconfig?

That's what I asked in the previous RFC too. Daniel mentioned that it shouldn't go there because is platform specific (AFAIU, one might want to test it on x86, aarch64, etc) but then I asked why we couldn't have a arch/um/.kunitconfig.

Quoted 8 lines
Quoted 3 lines

The answer was that's not that simple and some agreement on how to do it is needed:

https://lists.freedesktop.org/archives/dri-devel/2022-June/357617.html

We should probably just document it somewhere in KMS then? It doesn't have to be in this patch series, but I have the feeling that we will end up with that discussion a lot from people frustrated to have spent too much time figuring it out :)

My understanding from Daniel's comment [1] is also that at the moment it is not easy to support this use case, so yes, at least copy and pasting the command in the docs should help everyone figure out how to run the tests.

Documentation/gpu/drm-internals.rst seems like a good place to add some information about how to run and add tests.
I'll send a patch with the docs ASAP.

Jose

[1] https://lore.kernel.org/dri-devel/CAGS_qxqpiCim_sy1LDK7PLwVgWf-LKW+uNFTGM=T7ydk-dYcEw@mail.gmail.com/

#4 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

In reply to José Expósito

Hi,

Quoted 11 lines · José Expósito

Test the conversion from XRGB8888 to RGB332.

What is tested?

- Different values for the X in XRGB8888 to make sure it is ignored
- Different clip values: Single pixel and full and partial buffer
- Well known colors: White, black, red, green, blue, magenta, yellow and cyan
- Other colors: Randomly picked
- Destination pitch

Quoted 5 lines

How to run the tests?

 $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
         --kconfig_add CONFIG_VIRTIO_UML=y \
         --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

It's not clear to me why you would need VIRTIO here? The Kunit config file should be enough to run the tests properly

Maxime

#5 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

Hello Maxime,

Quoted 14 lines · Maxime Ripard

Hi,

Quoted 11 lines · José Expósito

Test the conversion from XRGB8888 to RGB332.

What is tested?

- Different values for the X in XRGB8888 to make sure it is ignored
- Different clip values: Single pixel and full and partial buffer
- Well known colors: White, black, red, green, blue, magenta, yellow and cyan
- Other colors: Randomly picked
- Destination pitch

Quoted 9 lines
Quoted 5 lines

How to run the tests?

 $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
         --kconfig_add CONFIG_VIRTIO_UML=y \
         --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

It's not clear to me why you would need VIRTIO here? The Kunit config file should be enough to run the tests properly

It's needed or otherwise KUnit will complain with:

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig [15:47:31] Configuring KUnit Kernel ...
Regenerating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
ERROR:root:Not all Kconfig options selected in kunitconfig were in the generated .config.
This is probably due to unsatisfied dependencies.
Missing: CONFIG_DRM=y, CONFIG_DRM_KUNIT_TEST=y
Note: many Kconfig options aren't available on UML. You can try running on a different architecture with something like "--arch=x86_64".

The following works correctly but it won't use User Mode Linux:

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig --arch=x86_64

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Linux Engineering
Red Hat
#6 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

hi,

Quoted 39 lines · Javier Martinez Canillas

Hello Maxime,

Quoted 14 lines · Maxime Ripard

Hi,

Quoted 11 lines · José Expósito

Test the conversion from XRGB8888 to RGB332.

What is tested?

- Different values for the X in XRGB8888 to make sure it is ignored
- Different clip values: Single pixel and full and partial buffer
- Well known colors: White, black, red, green, blue, magenta, yellow and cyan
- Other colors: Randomly picked
- Destination pitch

Quoted 9 lines
Quoted 5 lines

How to run the tests?

 $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
         --kconfig_add CONFIG_VIRTIO_UML=y \
         --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

It's not clear to me why you would need VIRTIO here? The Kunit config file should be enough to run the tests properly

It's needed or otherwise KUnit will complain with:

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig [15:47:31] Configuring KUnit Kernel ...
Regenerating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
ERROR:root:Not all Kconfig options selected in kunitconfig were in the generated .config.
This is probably due to unsatisfied dependencies.
Missing: CONFIG_DRM=y, CONFIG_DRM_KUNIT_TEST=y
Note: many Kconfig options aren't available on UML. You can try running on a different architecture with something like "--arch=x86_64".
Quoted 3 lines

The following works correctly but it won't use User Mode Linux:

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig --arch=x86_64

But then, can't we add them to .kunitconfig?

We should avoid that gotcha entirely

Maxime

#7 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

Hello Maxime,

Quoted 42 lines · Maxime Ripard

hi,

Quoted 39 lines · Javier Martinez Canillas

Hello Maxime,

Quoted 14 lines · Maxime Ripard

Hi,

Quoted 11 lines · José Expósito

Test the conversion from XRGB8888 to RGB332.

What is tested?

- Different values for the X in XRGB8888 to make sure it is ignored
- Different clip values: Single pixel and full and partial buffer
- Well known colors: White, black, red, green, blue, magenta, yellow and cyan
- Other colors: Randomly picked
- Destination pitch

Quoted 9 lines
Quoted 5 lines

How to run the tests?

 $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
         --kconfig_add CONFIG_VIRTIO_UML=y \
         --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

It's not clear to me why you would need VIRTIO here? The Kunit config file should be enough to run the tests properly

It's needed or otherwise KUnit will complain with:

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig [15:47:31] Configuring KUnit Kernel ...
Regenerating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
ERROR:root:Not all Kconfig options selected in kunitconfig were in the generated .config.
This is probably due to unsatisfied dependencies.
Missing: CONFIG_DRM=y, CONFIG_DRM_KUNIT_TEST=y
Note: many Kconfig options aren't available on UML. You can try running on a different architecture with something like "--arch=x86_64".
Quoted 6 lines
Quoted 3 lines

The following works correctly but it won't use User Mode Linux:

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig --arch=x86_64

But then, can't we add them to .kunitconfig?

That's what I asked in the previous RFC too. Daniel mentioned that it shouldn't go there because is platform specific (AFAIU, one might want to test it on x86, aarch64, etc) but then I asked why we couldn't have a arch/um/.kunitconfig.

The answer was that's not that simple and some agreement on how to do it is needed:

https://lists.freedesktop.org/archives/dri-devel/2022-June/357617.html

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Linux Engineering
Red Hat
#8 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

Quoted 52 lines · Javier Martinez Canillas

Hello Maxime,

Quoted 42 lines · Maxime Ripard

hi,

Quoted 39 lines · Javier Martinez Canillas

Hello Maxime,

Quoted 14 lines · Maxime Ripard

Hi,

Quoted 11 lines · José Expósito

Test the conversion from XRGB8888 to RGB332.

What is tested?

- Different values for the X in XRGB8888 to make sure it is ignored
- Different clip values: Single pixel and full and partial buffer
- Well known colors: White, black, red, green, blue, magenta, yellow and cyan
- Other colors: Randomly picked
- Destination pitch

Quoted 9 lines
Quoted 5 lines

How to run the tests?

 $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
         --kconfig_add CONFIG_VIRTIO_UML=y \
         --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

It's not clear to me why you would need VIRTIO here? The Kunit config file should be enough to run the tests properly

It's needed or otherwise KUnit will complain with:

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig [15:47:31] Configuring KUnit Kernel ...
Regenerating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
ERROR:root:Not all Kconfig options selected in kunitconfig were in the generated .config.
This is probably due to unsatisfied dependencies.
Missing: CONFIG_DRM=y, CONFIG_DRM_KUNIT_TEST=y
Note: many Kconfig options aren't available on UML. You can try running on a different architecture with something like "--arch=x86_64".
Quoted 6 lines
Quoted 3 lines

The following works correctly but it won't use User Mode Linux:

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig --arch=x86_64

But then, can't we add them to .kunitconfig?

Quoted 7 lines

That's what I asked in the previous RFC too. Daniel mentioned that it shouldn't go there because is platform specific (AFAIU, one might want to test it on x86, aarch64, etc) but then I asked why we couldn't have a arch/um/.kunitconfig.

The answer was that's not that simple and some agreement on how to do it is needed:

https://lists.freedesktop.org/archives/dri-devel/2022-June/357617.html

Thanks for pointing this out. So yeah, it's indeed not very optimal

We should probably just document it somewhere in KMS then? It doesn't have to be in this patch series, but I have the feeling that we will end up with that discussion a lot from people frustrated to have spent too much time figuring it out :)

Maxime

#9 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

In reply to Javier Martinez Canillas
Quoted 1 line · Javier Martinez Canillas

<snip>

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 7 lines
Quoted 3 lines
 $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
         --kconfig_add CONFIG_VIRTIO_UML=y \
         --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

It's not clear to me why you would need VIRTIO here? The Kunit config file should be enough to run the tests properly

It's needed or otherwise KUnit will complain with:

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig [15:47:31] Configuring KUnit Kernel ...
Regenerating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
ERROR:root:Not all Kconfig options selected in kunitconfig were in the generated .config.
This is probably due to unsatisfied dependencies.
Missing: CONFIG_DRM=y, CONFIG_DRM_KUNIT_TEST=y
Note: many Kconfig options aren't available on UML. You can try running on a different architecture with something like "--arch=x86_64".

The following works correctly but it won't use User Mode Linux:

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/.kunitconfig --arch=x86_64

But then, can't we add them to .kunitconfig?

That's what I asked in the previous RFC too. Daniel mentioned that it shouldn't go there because is platform specific (AFAIU, one might want to test it on x86,

Slight correction, it was David who explicitly suggested it shouldn't go in there.
https://lists.freedesktop.org/archives/dri-devel/2022-June/357611.html

Quoted 3 lines

aarch64, etc) but then I asked why we couldn't have a arch/um/.kunitconfig.

The answer was that's not that simple and some agreement on how to do it is needed:

I'm a bit more in favor of having UML-specific options in the drm .kunitconfig file, but I agree it's a bit unclear.
If people want to easily run with --arch=x86_64 or others, then they're indeed a liability.

Another option is to perhaps explicitly name the .kunitconfig file something like drm/uml.kunitconfig, which doesn't solve the problem but makes it less of a footgun.

Stepping back, I feel like perhaps a cleaner answer lies in adding a new Kconfig option that selects CONFIG_UML_PCI_OVER_VIRTIO under UML and just CONFIG_PCI otherwise.
But that's a bigger discussion still.

Daniel

#10 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

Hello Daniel,

On 6/6/22 16:19, Daniel Latypov wrote:

[snip]

Quoted 7 lines
Quoted 2 lines

That's what I asked in the previous RFC too. Daniel mentioned that it shouldn't go there because is platform specific (AFAIU, one might want to test it on x86,

Slight correction, it was David who explicitly suggested it shouldn't go in there.
https://lists.freedesktop.org/archives/dri-devel/2022-June/357611.html

Ups, sorry for getting that wrong. I should had looked at the thread again before writing my answer.

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Linux Engineering
Red Hat
#11 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

In reply to José Expósito

Hi,

ading Kunit tests for the conversion helpers is pretty cool. Thanks for doing that.

Am 06.06.22 um 11:55 schrieb José Expósito:

Quoted 79 lines

Test the conversion from XRGB8888 to RGB332.

What is tested?

- Different values for the X in XRGB8888 to make sure it is ignored
- Different clip values: Single pixel and full and partial buffer
- Well known colors: White, black, red, green, blue, magenta, yellow and cyan
- Other colors: Randomly picked
- Destination pitch

How to run the tests?

  $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
          --kconfig_add CONFIG_VIRTIO_UML=y \
          --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

---

RFC -> v1: https://lore.kernel.org/dri-devel/[email protected]/T/

- Add .kunitconfig (Maxime Ripard)
- Fix memory leak (Daniel Latypov)
- Make config option generic (Javier Martinez Canillas):

    DRM_FORMAR_HELPER_TEST -> DRM_KUNIT_TEST
  - Remove DISABLE_STRUCTLEAK_PLUGIN (Daniel Latypov)
---
  drivers/gpu/drm/.kunitconfig             |   3 +
  drivers/gpu/drm/Kconfig                  |  16 +++
  drivers/gpu/drm/Makefile                 |   2 +
  drivers/gpu/drm/drm_format_helper_test.c | 166 +++++++++++++++++++++++
  4 files changed, 187 insertions(+)

create mode 100644 drivers/gpu/drm/.kunitconfig create mode 100644 drivers/gpu/drm/drm_format_helper_test.c

drivers/gpu/drm/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..6ec04b4c979d
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_DRM=y
+CONFIG_DRM_KUNIT_TEST=y
drivers/gpu/drm/Kconfig +16 -0
index e88c497fa010..3c0b1faba439 100644
@@ -70,6 +70,22 @@ config DRM_DEBUG_SELFTEST
 
  	  If in doubt, say "N".
 
+config DRM_KUNIT_TEST
+	tristate "KUnit tests for DRM" if !KUNIT_ALL_TESTS
+	depends on DRM && KUNIT=y
+	select DRM_KMS_HELPER
+	default KUNIT_ALL_TESTS
+	help
+	  This builds unit tests for DRM. This option is not useful for
+	  distributions or general kernels, but only for kernel
+	  developers working on DRM and associated drivers.
+
+	  For more information on KUnit and unit tests in general,
+	  please refer to the KUnit documentation in
+	  Documentation/dev-tools/kunit/.
+
+	  If in doubt, say "N".
+
  config DRM_KMS_HELPER
  	tristate
  	depends on DRM
drivers/gpu/drm/Makefile +0 -0
index 15fe3163f822..6549471f09c7 100644

drivers/gpu/drm/Makefile obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o · line 79

Quoted 4 lines
drivers/gpu/drm/Makefile +1 -0
@@ -76,6 +76,8 @@ obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o
  #
 
  obj-$(CONFIG_DRM_DEBUG_SELFTEST) += selftests/
+obj-$(CONFIG_DRM_KUNIT_TEST) += drm_kms_helper.o \

You already selected DRM_KMS_HELPER in Kconfig. Why do you need to list the module here?

drivers/gpu/drm/Makefile obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o · line 80

Quoted 1 line
drivers/gpu/drm/Makefile +1 -0
+		drm_format_helper_test.o

One comment about source-code organization:

There is potentially a long list of test files that will contain unit tests. I would prefer to put the unit tests into their own subdirectory (e.g., kunit).

Quoted 17 lines
drivers/gpu/drm/Makefile +0 -0
  obj-$(CONFIG_DRM_MIPI_DBI) += drm_mipi_dbi.o
  obj-$(CONFIG_DRM_MIPI_DSI) += drm_mipi_dsi.o
drivers/gpu/drm/drm_format_helper_test.c new file +8 -0
new file mode 100644
index 000000000000..e9302219f3f9
@@ -0,0 +1,166 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <kunit/test.h>
+
+#include <drm/drm_device.h>
+#include <drm/drm_file.h>
+#include <drm/drm_format_helper.h>
+#include <drm/drm_fourcc.h>

drivers/gpu/drm/drm_format_helper_test.c line 18

Quoted 10 lines
drivers/gpu/drm/drm_format_helper_test.c new file +10 -0
+#include <drm/drm_framebuffer.h>
+#include <drm/drm_gem_framebuffer_helper.h>
+#include <drm/drm_mode.h>
+#include <drm/drm_print.h>
+#include <drm/drm_rect.h>
+
+#include "drm_crtc_internal.h"
+
+#define TEST_BUF_SIZE 50
+#define CLIP(x, y, w, h) { (x), (y), (x) + (w), (y) + (h) }

I have long wished for an initializer macro for drm_rect.
Please rename that CLIP macro to DRM_RECT_INIT and put it into <drm/drm_rect.h> with docs.

drivers/gpu/drm/drm_format_helper_test.c line 29

Quoted 11 lines
drivers/gpu/drm/drm_format_helper_test.c new file +11 -0
+
+struct xrgb8888_to_rgb332_case {
+	const char *name;
+	unsigned int pitch;
+	unsigned int dst_pitch;
+	struct drm_rect clip;
+	const u32 xrgb8888[TEST_BUF_SIZE];
+	const u8 expected[4 * TEST_BUF_SIZE];
+};
+
+static struct xrgb8888_to_rgb332_case xrgb8888_to_rgb332_cases[] = {

The names of these tests are only mildly descriptive. Maybe add a single-line comment before each test case to describe what it does. Your commit description has a nice list of tests, which you can copy here almost as-is.

Quoted 57 lines
drivers/gpu/drm/drm_format_helper_test.c new file +57 -0
+	{
+		.name = "Single pixel source",
+		.pitch = 1 * 4,
+		.dst_pitch = 0,
+		.clip = CLIP(0, 0, 1, 1),
+		.xrgb8888 = { 0x01FF0000 },
+		.expected = { 0xE0 },
+	},
+	{
+		.name = "Single pixel clip",
+		.pitch = 2 * 4,
+		.dst_pitch = 0,
+		.clip = CLIP(1, 1, 1, 1),
+		.xrgb8888 = {
+			0x00000000, 0x00000000,
+			0x00000000, 0x10FF0000,
+		},
+		.expected = { 0xE0 },
+	},
+	{
+		.name = "White, black, red, green, blue, magenta, yellow, cyan",
+		.pitch = 4 * 4,
+		.dst_pitch = 0,
+		.clip = CLIP(1, 1, 2, 4),
+		.xrgb8888 = {
+			0x00000000, 0x00000000, 0x00000000, 0x00000000,
+			0x00000000, 0x11FFFFFF, 0x22000000, 0x00000000,
+			0x00000000, 0x33FF0000, 0x4400FF00, 0x00000000,
+			0x00000000, 0x550000FF, 0x66FF00FF, 0x00000000,
+			0x00000000, 0x77FFFF00, 0x8800FFFF, 0x00000000,
+		},
+		.expected = {
+			0xFF, 0x00,
+			0xE0, 0x1C,
+			0x03, 0xE3,
+			0xFC, 0x1F,
+		},
+	},
+	{
+		.name = "Destination pitch",
+		.pitch = 3 * 4,
+		.dst_pitch = 5,
+		.clip = CLIP(0, 0, 3, 3),
+		.xrgb8888 = {
+			0xA10E449C, 0xB1114D05, 0xC1A80303,
+			0xD16C7073, 0xA20E449C, 0xB2114D05,
+			0xC2A80303, 0xD26C7073, 0xA30E449C,
+		},
+		.expected = {
+			0x0A, 0x08, 0xA0, 0x00, 0x00,
+			0x6D, 0x0A, 0x08, 0x00, 0x00,
+			0xA0, 0x6D, 0x0A, 0x00, 0x00,
+		},
+	},
+};
+
+/**

drivers/gpu/drm/drm_format_helper_test.c line 96

Quoted 10 lines
drivers/gpu/drm/drm_format_helper_test.c new file +10 -0
+ * conversion_buf_size - Return the destination buffer size required to convert
+ * between formats.
+ * @src_format: source buffer pixel format (DRM_FORMAT_*)
+ * @dst_format: destination buffer pixel format (DRM_FORMAT_*)
+ * @dst_pitch: Number of bytes between two consecutive scanlines within dst
+ * @clip: Clip rectangle area to convert
+ *
+ * Returns:
+ * The size of the destination buffer or negative value on error.
+ */

You don't need to document internal functions with formatted comments. It will only confuse readers of the generated documentation. If you don't want to outright remove the comment, at least remove the /** at the top.

Best regards
Thomas

Quoted 70 lines
drivers/gpu/drm/drm_format_helper_test.c new file +70 -0
+static size_t conversion_buf_size(u32 src_format, u32 dst_format,
+				  unsigned int dst_pitch,
+				  const struct drm_rect *clip)
+{
+	const struct drm_format_info *src_fi = drm_format_info(src_format);
+	const struct drm_format_info *dst_fi = drm_format_info(dst_format);
+	size_t width = drm_rect_width(clip);
+	size_t src_nbytes;
+
+	if (!src_fi || !dst_fi)
+		return -EINVAL;
+
+	if (dst_pitch)
+		width = dst_pitch;
+
+	src_nbytes = width * drm_rect_height(clip) * src_fi->cpp[0];
+	if (!src_nbytes)
+		return 0;
+
+	return (src_nbytes * dst_fi->cpp[0]) / src_fi->cpp[0];
+}
+
+static void xrgb8888_to_rgb332_case_desc(struct xrgb8888_to_rgb332_case *t,
+					 char *desc)
+{
+	strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(xrgb8888_to_rgb332, xrgb8888_to_rgb332_cases,
+		  xrgb8888_to_rgb332_case_desc);
+
+static void xrgb8888_to_rgb332_test(struct kunit *test)
+{
+	const struct xrgb8888_to_rgb332_case *params = test->param_value;
+	size_t dst_size;
+	__u8 *dst = NULL;
+
+	struct drm_framebuffer fb = {
+		.format = drm_format_info(DRM_FORMAT_XRGB8888),
+		.pitches = { params->pitch, 0, 0 },
+	};
+
+	dst_size = conversion_buf_size(DRM_FORMAT_XRGB8888, DRM_FORMAT_RGB332,
+				       params->dst_pitch, &params->clip);
+	KUNIT_ASSERT_GT(test, dst_size, 0);
+
+	dst = kunit_kzalloc(test, dst_size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dst);
+
+	drm_fb_xrgb8888_to_rgb332(dst, params->dst_pitch, params->xrgb8888,
+				  &fb, &params->clip);
+	KUNIT_EXPECT_EQ(test, memcmp(dst, params->expected, dst_size), 0);
+}
+
+static struct kunit_case drm_format_helper_test_cases[] = {
+	KUNIT_CASE_PARAM(xrgb8888_to_rgb332_test,
+			 xrgb8888_to_rgb332_gen_params),
+	{}
+};
+
+static struct kunit_suite drm_format_helper_test_suite = {
+	.name = "drm-format-helper-test",
+	.test_cases = drm_format_helper_test_cases,
+};
+
+kunit_test_suite(drm_format_helper_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests for the drm_format_helper APIs");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("José Expósito <[email protected]>");
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#12 lore raw

Re: [PATCH 1/1] drm/format-helper: Add KUnit tests for drm_fb_xrgb8888_to_rgb332()

Hi Thomas,

Thanks a lot for your review.

Quoted 81 lines · Thomas Zimmermann

Hi,

ading Kunit tests for the conversion helpers is pretty cool. Thanks for doing that.

Am 06.06.22 um 11:55 schrieb José Expósito:

Quoted 75 lines

Test the conversion from XRGB8888 to RGB332.

What is tested?

- Different values for the X in XRGB8888 to make sure it is ignored
- Different clip values: Single pixel and full and partial buffer
- Well known colors: White, black, red, green, blue, magenta, yellow and cyan
- Other colors: Randomly picked
- Destination pitch

How to run the tests?

  $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm \
          --kconfig_add CONFIG_VIRTIO_UML=y \
          --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y

---

RFC -> v1: https://lore.kernel.org/dri-devel/[email protected]/T/

- Add .kunitconfig (Maxime Ripard)
- Fix memory leak (Daniel Latypov)
- Make config option generic (Javier Martinez Canillas):

    DRM_FORMAR_HELPER_TEST -> DRM_KUNIT_TEST
  - Remove DISABLE_STRUCTLEAK_PLUGIN (Daniel Latypov)
---
  drivers/gpu/drm/.kunitconfig             |   3 +
  drivers/gpu/drm/Kconfig                  |  16 +++
  drivers/gpu/drm/Makefile                 |   2 +
  drivers/gpu/drm/drm_format_helper_test.c | 166 +++++++++++++++++++++++
  4 files changed, 187 insertions(+)

create mode 100644 drivers/gpu/drm/.kunitconfig create mode 100644 drivers/gpu/drm/drm_format_helper_test.c

drivers/gpu/drm/.kunitconfig new file +3 -0
new file mode 100644
index 000000000000..6ec04b4c979d
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_DRM=y
+CONFIG_DRM_KUNIT_TEST=y
drivers/gpu/drm/Kconfig +16 -0
index e88c497fa010..3c0b1faba439 100644
@@ -70,6 +70,22 @@ config DRM_DEBUG_SELFTEST
  	  If in doubt, say "N".
+config DRM_KUNIT_TEST
+	tristate "KUnit tests for DRM" if !KUNIT_ALL_TESTS
+	depends on DRM && KUNIT=y
+	select DRM_KMS_HELPER
+	default KUNIT_ALL_TESTS
+	help
+	  This builds unit tests for DRM. This option is not useful for
+	  distributions or general kernels, but only for kernel
+	  developers working on DRM and associated drivers.
+
+	  For more information on KUnit and unit tests in general,
+	  please refer to the KUnit documentation in
+	  Documentation/dev-tools/kunit/.
+
+	  If in doubt, say "N".
+
  config DRM_KMS_HELPER
  	tristate
  	depends on DRM
drivers/gpu/drm/Makefile +0 -1
index 15fe3163f822..6549471f09c7 100644
--- a/drivers/gpu/drm/Makefile
Quoted 8 lines

drivers/gpu/drm/Makefile obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o · line 78

Quoted 5 lines

+++ b/drivers/gpu/drm/Makefile

drivers/gpu/drm/Makefile +1 -0
@@ -76,6 +76,8 @@ obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o
  #
  obj-$(CONFIG_DRM_DEBUG_SELFTEST) += selftests/
+obj-$(CONFIG_DRM_KUNIT_TEST) += drm_kms_helper.o \

You already selected DRM_KMS_HELPER in Kconfig. Why do you need to list the module here?

Actually, it is not required. I'll remove it in v2.

Quoted 7 lines

drivers/gpu/drm/Makefile obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o · line 80

Quoted 1 line
drivers/gpu/drm/Makefile +1 -0
+		drm_format_helper_test.o

One comment about source-code organization:

There is potentially a long list of test files that will contain unit tests. I would prefer to put the unit tests into their own subdirectory (e.g., kunit).

It makes sense, and it'd also be more consistent with selftest tests.

Quoted 22 lines
Quoted 22 lines
drivers/gpu/drm/Makefile +0 -0
  obj-$(CONFIG_DRM_MIPI_DBI) += drm_mipi_dbi.o
  obj-$(CONFIG_DRM_MIPI_DSI) += drm_mipi_dsi.o
drivers/gpu/drm/drm_format_helper_test.c new file +14 -0
new file mode 100644
index 000000000000..e9302219f3f9
@@ -0,0 +1,166 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <kunit/test.h>
+
+#include <drm/drm_device.h>
+#include <drm/drm_file.h>
+#include <drm/drm_format_helper.h>
+#include <drm/drm_fourcc.h>
+#include <drm/drm_framebuffer.h>
+#include <drm/drm_gem_framebuffer_helper.h>
+#include <drm/drm_mode.h>
+#include <drm/drm_print.h>
+#include <drm/drm_rect.h>
+
Quoted 8 lines

drivers/gpu/drm/drm_format_helper_test.c line 18

Quoted 4 lines
drivers/gpu/drm/drm_format_helper_test.c new file +4 -0
+#include "drm_crtc_internal.h"
+
+#define TEST_BUF_SIZE 50
+#define CLIP(x, y, w, h) { (x), (y), (x) + (w), (y) + (h) }

I have long wished for an initializer macro for drm_rect. Please rename that CLIP macro to DRM_RECT_INIT and put it into <drm/drm_rect.h> with docs.

I'll include an extra patch for it on v2.

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/drm_format_helper_test.c new file +8 -0
+
+struct xrgb8888_to_rgb332_case {
+	const char *name;
+	unsigned int pitch;
+	unsigned int dst_pitch;
+	struct drm_rect clip;
+	const u32 xrgb8888[TEST_BUF_SIZE];
+	const u8 expected[4 * TEST_BUF_SIZE];
Quoted 8 lines

drivers/gpu/drm/drm_format_helper_test.c line 29

Quoted 3 lines
drivers/gpu/drm/drm_format_helper_test.c new file +3 -0
+};
+
+static struct xrgb8888_to_rgb332_case xrgb8888_to_rgb332_cases[] = {

The names of these tests are only mildly descriptive. Maybe add a single-line comment before each test case to describe what it does. Your commit description has a nice list of tests, which you can copy here almost as-is.

Ok, written down for v2.

Quoted 63 lines
Quoted 63 lines
drivers/gpu/drm/drm_format_helper_test.c new file +63 -0
+	{
+		.name = "Single pixel source",
+		.pitch = 1 * 4,
+		.dst_pitch = 0,
+		.clip = CLIP(0, 0, 1, 1),
+		.xrgb8888 = { 0x01FF0000 },
+		.expected = { 0xE0 },
+	},
+	{
+		.name = "Single pixel clip",
+		.pitch = 2 * 4,
+		.dst_pitch = 0,
+		.clip = CLIP(1, 1, 1, 1),
+		.xrgb8888 = {
+			0x00000000, 0x00000000,
+			0x00000000, 0x10FF0000,
+		},
+		.expected = { 0xE0 },
+	},
+	{
+		.name = "White, black, red, green, blue, magenta, yellow, cyan",
+		.pitch = 4 * 4,
+		.dst_pitch = 0,
+		.clip = CLIP(1, 1, 2, 4),
+		.xrgb8888 = {
+			0x00000000, 0x00000000, 0x00000000, 0x00000000,
+			0x00000000, 0x11FFFFFF, 0x22000000, 0x00000000,
+			0x00000000, 0x33FF0000, 0x4400FF00, 0x00000000,
+			0x00000000, 0x550000FF, 0x66FF00FF, 0x00000000,
+			0x00000000, 0x77FFFF00, 0x8800FFFF, 0x00000000,
+		},
+		.expected = {
+			0xFF, 0x00,
+			0xE0, 0x1C,
+			0x03, 0xE3,
+			0xFC, 0x1F,
+		},
+	},
+	{
+		.name = "Destination pitch",
+		.pitch = 3 * 4,
+		.dst_pitch = 5,
+		.clip = CLIP(0, 0, 3, 3),
+		.xrgb8888 = {
+			0xA10E449C, 0xB1114D05, 0xC1A80303,
+			0xD16C7073, 0xA20E449C, 0xB2114D05,
+			0xC2A80303, 0xD26C7073, 0xA30E449C,
+		},
+		.expected = {
+			0x0A, 0x08, 0xA0, 0x00, 0x00,
+			0x6D, 0x0A, 0x08, 0x00, 0x00,
+			0xA0, 0x6D, 0x0A, 0x00, 0x00,
+		},
+	},
+};
+
+/**
+ * conversion_buf_size - Return the destination buffer size required to convert
+ * between formats.
+ * @src_format: source buffer pixel format (DRM_FORMAT_*)
+ * @dst_format: destination buffer pixel format (DRM_FORMAT_*)
+ * @dst_pitch: Number of bytes between two consecutive scanlines within dst
+ * @clip: Clip rectangle area to convert
Quoted 8 lines

drivers/gpu/drm/drm_format_helper_test.c line 96

Quoted 4 lines
drivers/gpu/drm/drm_format_helper_test.c new file +4 -0
+ *
+ * Returns:
+ * The size of the destination buffer or negative value on error.
+ */

You don't need to document internal functions with formatted comments. It will only confuse readers of the generated documentation. If you don't want to outright remove the comment, at least remove the /** at the top.

Cool, I'll remove the extra * on v2.

Speaking about documentation, I sent a patch explaining how to run the tests:

https://lore.kernel.org/dri-devel/[email protected]/T/

I'll send it as part of the v2 of this series.

Best wishes,
Jose

Quoted 81 lines

Best regards
Thomas

Quoted 70 lines
drivers/gpu/drm/drm_format_helper_test.c new file +70 -0
+static size_t conversion_buf_size(u32 src_format, u32 dst_format,
+				  unsigned int dst_pitch,
+				  const struct drm_rect *clip)
+{
+	const struct drm_format_info *src_fi = drm_format_info(src_format);
+	const struct drm_format_info *dst_fi = drm_format_info(dst_format);
+	size_t width = drm_rect_width(clip);
+	size_t src_nbytes;
+
+	if (!src_fi || !dst_fi)
+		return -EINVAL;
+
+	if (dst_pitch)
+		width = dst_pitch;
+
+	src_nbytes = width * drm_rect_height(clip) * src_fi->cpp[0];
+	if (!src_nbytes)
+		return 0;
+
+	return (src_nbytes * dst_fi->cpp[0]) / src_fi->cpp[0];
+}
+
+static void xrgb8888_to_rgb332_case_desc(struct xrgb8888_to_rgb332_case *t,
+					 char *desc)
+{
+	strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(xrgb8888_to_rgb332, xrgb8888_to_rgb332_cases,
+		  xrgb8888_to_rgb332_case_desc);
+
+static void xrgb8888_to_rgb332_test(struct kunit *test)
+{
+	const struct xrgb8888_to_rgb332_case *params = test->param_value;
+	size_t dst_size;
+	__u8 *dst = NULL;
+
+	struct drm_framebuffer fb = {
+		.format = drm_format_info(DRM_FORMAT_XRGB8888),
+		.pitches = { params->pitch, 0, 0 },
+	};
+
+	dst_size = conversion_buf_size(DRM_FORMAT_XRGB8888, DRM_FORMAT_RGB332,
+				       params->dst_pitch, &params->clip);
+	KUNIT_ASSERT_GT(test, dst_size, 0);
+
+	dst = kunit_kzalloc(test, dst_size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dst);
+
+	drm_fb_xrgb8888_to_rgb332(dst, params->dst_pitch, params->xrgb8888,
+				  &fb, &params->clip);
+	KUNIT_EXPECT_EQ(test, memcmp(dst, params->expected, dst_size), 0);
+}
+
+static struct kunit_case drm_format_helper_test_cases[] = {
+	KUNIT_CASE_PARAM(xrgb8888_to_rgb332_test,
+			 xrgb8888_to_rgb332_gen_params),
+	{}
+};
+
+static struct kunit_suite drm_format_helper_test_suite = {
+	.name = "drm-format-helper-test",
+	.test_cases = drm_format_helper_test_cases,
+};
+
+kunit_test_suite(drm_format_helper_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests for the drm_format_helper APIs");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("José Expósito <[email protected]>");
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev