Skip to contentSkip to conversation

gpio: add kunit tests for GPIO core

PATCH Superseded Bartosz Golaszewski 2 participants 5 messages May 20 – May 25

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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]>
#1 lore raw

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
index f8236a8536f7ebcee6b0e00a7bd799a14b345c1b..8cad6e1c3e7efba862862b579089f2f317784a73 100644
@@ -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
index 0b518de26065d65dac3bd49dd94a4b3e7ea0634b..583b50b538c79599ebbf33e261fe2e9ced35efa9 100644
@@ -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
#2 lore raw

PATCH v2 2/3 kunit: provide kunit_platform_device_unregister()

In reply to Bartosz Golaszewski

Tests 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
index 8cad6e1c3e7efba862862b579089f2f317784a73..eee565d5d1d35c1d1bc82b45eb91d21d00c68428 100644
@@ -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
index 583b50b538c79599ebbf33e261fe2e9ced35efa9..ccfbc70b4e12817f60490e8146f7f7773c9a4dbb 100644
@@ -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
#3 lore raw

PATCH v2 3/3 gpio: add kunit test cases for the GPIO subsystem

In reply to Bartosz Golaszewski

Add 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
index 00fcab5d09a4294ed778cea78af5867a0f6e481b..0306005fb7d65ae85905e967b9065fd74db753db 100644
@@ -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
index 2ea47d9d3dca948e1cdc46965e83b0e1b6de5f70..c66b6dd659b16b80b6bb6b15fac3e3f462dec596 100644
@@ -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
maintainer Reviewed-by on the v2 series
#4 lore raw
In reply to Bartosz Golaszewski
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.

Pretty cool stuff! I wish I had the energy and grit to do the same for pin control...

Yours,
Linus Walleij