gpio: add kunit tests for GPIO core
v2 cover gpio: add kunit tests for GPIO core
This series adds a first batch of kunit tests for GPIO core. I intend to gradually add more coverage for functionalities that can't really be tested from user-space with the existing kernel selftests or libgpiod tests.
Merging strategy: with an Ack from kunit maintainers, this can go through the GPIO tree for v7.2.
---
Changes in v2:
- Remove cases of kunit killing a thread on test failure with device lock
taken
- Fix a platform device leak on test failure
- New patch: provide kunit_platform_device_unregister()
- Link to v1: https://patch.msgid.link/[email protected]
---
Bartosz Golaszewski (3):
kunit: provide kunit_platform_device_register_full()
kunit: provide kunit_platform_device_unregister()
gpio: add kunit test cases for the GPIO subsystem
drivers/gpio/Kconfig | 8 + drivers/gpio/Makefile | 1 + drivers/gpio/gpiolib-kunit.c | 358 ++++++++++++++++++++++++++++++++++++++++ include/kunit/platform_device.h | 6 + lib/kunit/platform.c | 46 ++++++ 5 files changed, 419 insertions(+)
---
base-commit: 80dd246accce631c328ea43294e53b2b2dd2aa32
change-id: 20260326-gpiolib-kunit-d7f1b5541ffa
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>
PATCH v2 1/3 kunit: provide kunit_platform_device_register_full()
Provide a kunit-managed variant of platform_device_register_full().
--- include/kunit/platform_device.h | 4 ++++ lib/kunit/platform.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+)
include/kunit/platform_device.h +4 -0
@@ -6,10 +6,14 @@ struct completion;struct kunit ;struct platform_device ;struct platform_driver ; +struct platform_device_info ;struct platform_device *kunit_platform_device_alloc (struct kunit *test,const char *name,int id);int kunit_platform_device_add (struct kunit *test,struct platform_device *pdev); +struct platform_device * +kunit_platform_device_register_full (struct kunit *test, +const struct platform_device_info *pdevinfo); int kunit_platform_device_prepare_wait_for_probe(struct kunit *test, struct platform_device *pdev,
lib/kunit/platform.c +31 -0
@@ -6,6 +6,7 @@#include <linux/completion.h> #include <linux/device/bus.h> #include <linux/device/driver.h> +#include <linux/err.h> #include <linux/platform_device.h> #include <kunit/platform_device.h>
@@ -130,6 +131,36 @@ int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev) }EXPORT_SYMBOL_GPL (kunit_platform_device_add); +/** +* kunit_platform_device_register_full() - Register a KUnit test-managed platform +* device described by platform device info +* @test: test context +* @pdevinfo: platform device information describing the new device +* +* Register a test-managed platform device. The device is unregistered when the +* test completes. +* +* Return: New platform device on success, IS_ERR() on error. +*/ +struct platform_device * +kunit_platform_device_register_full (struct kunit *test, +const struct platform_device_info *pdevinfo) +{ +struct platform_device *pdev; +int ret; + + pdev =platform_device_register_full (pdevinfo); +if (IS_ERR (pdev)) +return pdev; + + ret =kunit_add_action_or_reset (test, platform_device_unregister_wrapper, pdev); +if (ret) +return ERR_PTR (ret); + +return pdev; +} +EXPORT_SYMBOL_GPL (kunit_platform_device_register_full); +struct kunit_platform_device_probe_nb {struct completion *x;struct device *dev;
Signature · 1 line
2.47.3
PATCH v2 2/3 kunit: provide kunit_platform_device_unregister()
In reply to Bartosz GolaszewskiTests may want to unregister a platform device as part of the test case logic. Using the regular platform_device_register() with kunit assertions may result in a platform device leak or otherwise requires cumbersome error handling. Provide a function that unregisters a kunit-managed platform device and drops the release action from the test's list.
--- include/kunit/platform_device.h | 2 ++ lib/kunit/platform.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+)
include/kunit/platform_device.h +2 -0
@@ -14,6 +14,8 @@ int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev);struct platform_device *kunit_platform_device_register_full (struct kunit *test,const struct platform_device_info *pdevinfo); +void kunit_platform_device_unregister (struct kunit *test, +struct platform_device *pdev); int kunit_platform_device_prepare_wait_for_probe(struct kunit *test, struct platform_device *pdev,
lib/kunit/platform.c +15 -0
@@ -161,6 +161,21 @@ kunit_platform_device_register_full(struct kunit *test, }EXPORT_SYMBOL_GPL (kunit_platform_device_register_full); +/** +* kunit_platform_device_unregister() - Unregister a KUnit-managed platform device +* @test: test context +* @pdev: platform device to unregister +* +* Unregister a test-managed platform device and cancel its release action. +*/ +void kunit_platform_device_unregister (struct kunit *test, +struct platform_device *pdev) +{ +kunit_remove_action (test, platform_device_unregister_wrapper, pdev); +platform_device_unregister (pdev); +} +EXPORT_SYMBOL_GPL (kunit_platform_device_unregister); +struct kunit_platform_device_probe_nb {struct completion *x;struct device *dev;
Signature · 1 line
2.47.3
PATCH v2 3/3 gpio: add kunit test cases for the GPIO subsystem
In reply to Bartosz GolaszewskiAdd a module containing kunit test cases for GPIO core. The idea is to use it to test functionalities that can't easily be tested from user-space with kernel selftests or GPIO character device test suites provided by the libgpiod package.
For now add test cases that verify software node based lookup and ensure that a GPIO provider unbinding with active consumers does not cause a crash.
--- drivers/gpio/Kconfig | 8 + drivers/gpio/Makefile | 1 + drivers/gpio/gpiolib-kunit.c | 358 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 367 insertions(+)
drivers/gpio/Kconfig +8 -0
@@ -102,6 +102,14 @@ config GPIO_CDEV_V1 This ABI version is deprecated. Please use the latest ABI for new developments. +config GPIO_KUNIT + tristate "Build GPIO Kunit test cases" + depends on KUNIT + default KUNIT_ALL_TESTS + help + Say Y here to build the module containing Kunit test cases verifying + the functionality of the GPIO subsystem. + config GPIO_GENERIC depends on HAS_IOMEM # Only for IOMEM drivers tristate
drivers/gpio/Makefile +1 -0
@@ -13,6 +13,7 @@ obj-$(CONFIG_GPIO_ACPI) += gpiolib-acpi.o gpiolib-acpi-y := gpiolib-acpi-core.o gpiolib-acpi-quirks.o obj-$(CONFIG_GPIOLIB) += gpiolib-swnode.o obj-$(CONFIG_GPIO_SHARED) += gpiolib-shared.o +obj-$(CONFIG_GPIO_KUNIT) += gpiolib-kunit.o # Device drivers. Generally keep list sorted alphabetically obj-$(CONFIG_GPIO_REGMAP) += gpio-regmap.o
drivers/gpio/gpiolib-kunit.c new file +358 -0
358 lines not on this page · raw message
Signature · 1 line
2.47.3
Quoted 9 lines · Bartosz Golaszewski
This series adds a first batch of kunit tests for GPIO core. I intend to gradually add more coverage for functionalities that can't really be tested from user-space with the existing kernel selftests or libgpiod tests.
Merging strategy: with an Ack from kunit maintainers, this can go through the GPIO tree for v7.2.
Signed-off-by Bartosz Golaszewski <[email protected]>
Pretty cool stuff! I wish I had the energy and grit to do the same for pin control...
Yours,
Linus Walleij