reset: Add KUnit test cases
PATCH v2 1/1 reset: Add KUnit test cases
Since commit 9035073d0ef1 ("reset: convert reset core to using firmware nodes"), reset suppliers and consumers can be wired up using software nodes without requiring Device Tree or hardware devices. Leverage this to add KUnit test cases for the reset controller framework.
The test cases cover the following scenarios:
- Basic: Verifies that a consumer can acquire a reset control via fwnode and perform reset, assert, deassert, status, release, and acquire operations.
- Use After Unregister: Verifies that after the supplier unregisters the reset_controller_dev while a consumer still holds a reset_control handle, subsequent consumer operations fail safely with -ENODEV.
- Concurrent Unregister: Verifies that unregistering the reset_controller_dev concurrently with an in-flight reset operation waits for the active operation to complete before returning.
---
Test:
$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_RESET_CONTROLLER=y \
--kconfig_add CONFIG_RESET_KUNIT_TEST=y \
reset_test
---
v2:
- Separate out from the revocable series.
- Add reset_test_exit() to clean up rstc and rcdev on test exit or
assertion failure.
- Clean up kthreads on error paths in reset_test_concurrent_unregister().v1: https://lore.kernel.org/all/[email protected]
--- drivers/reset/Kconfig | 5 + drivers/reset/Makefile | 2 + drivers/reset/core-test.c | 351 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 358 insertions(+) create mode 100644 drivers/reset/core-test.c
drivers/reset/Kconfig +5 -0
@@ -457,4 +457,9 @@ source "drivers/reset/starfive/Kconfig" source "drivers/reset/sti/Kconfig" source "drivers/reset/tegra/Kconfig" +config RESET_KUNIT_TEST + tristate "KUnit tests for reset controller" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + endif
drivers/reset/Makefile +2 -0
@@ -55,3 +55,5 @@ obj-$(CONFIG_RESET_UNIPHIER) += reset-uniphier.o obj-$(CONFIG_RESET_UNIPHIER_GLUE) += reset-uniphier-glue.o obj-$(CONFIG_RESET_ZYNQ) += reset-zynq.o obj-$(CONFIG_RESET_ZYNQMP) += reset-zynqmp.o + +obj-$(CONFIG_RESET_KUNIT_TEST) += core-test.o
drivers/reset/core-test.c new file +351 -0
351 lines not on this page · raw message
Signature · 1 line
2.53.0
Philipp Zabel
maintainerQuoted 39 lines · Tzung-Bi Shih
Since commit
9035073d0ef1("reset: convert reset core to using firmware nodes"), reset suppliers and consumers can be wired up using software nodes without requiring Device Tree or hardware devices. Leverage this to add KUnit test cases for the reset controller framework.The test cases cover the following scenarios:
- Basic: Verifies that a consumer can acquire a reset control via fwnode and perform reset, assert, deassert, status, release, and acquire operations.
- Use After Unregister: Verifies that after the supplier unregisters thereset_controller_devwhile a consumer still holds areset_controlhandle, subsequent consumer operations fail safely with-ENODEV.
- Concurrent Unregister: Verifies that unregistering thereset_controller_devconcurrently with an in-flight reset operation waits for the active operation to complete before returning.Signed-off-by Tzung-Bi Shih <[email protected]>---
Test:$ ./tools/testing/kunit/kunit.py run \ --kconfig_add CONFIG_RESET_CONTROLLER=y \ --kconfig_add CONFIG_RESET_KUNIT_TEST=y \ reset_test --- v2: - Separate out from the revocable series. - Add reset_test_exit() to clean up rstc and rcdev on test exit or assertion failure. - Clean up kthreads on error paths in reset_test_concurrent_unregister().v1: https://lore.kernel.org/all/[email protected]
--- drivers/reset/Kconfig | 5 + drivers/reset/Makefile | 2 + drivers/reset/core-test.c | 351 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 358 insertions(+) create mode 100644 drivers/reset/core-test.c
[...]
Quoted 6 lines
drivers/reset/core-test.c new file +0
-0@@ -0,0 +1,351 @@
[...]
Quoted 47 lines
drivers/reset/core-test.c new file +47
-0+static void reset_test_concurrent_unregister (struct kunit *test) +{ +struct reset_test_context *rtc = test->priv; +struct test_concurrent_unregister_context *ctx; +int i, ret, val; + + rtc->rcdev.ops = &reset_test_concurrent_unregister_ops; + + ctx =kunit_kmalloc_array (test,3 ,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, ctx); + +for (i =0 ; i <3 ; ++i) { + ctx[i].test = test; +init_completion (&ctx[i].started); +init_completion (&ctx[i].enter); + +if (i ==0 ) { + ctx[i].thread =kthread_run ( + test_concurrent_unregister_supplier, ctx + i, +"supplier%d" , i); + }else { +init_completion (&ctx[i].entered); +init_completion (&ctx[i].exit); + + ctx[i].thread =kthread_run ( + test_concurrent_unregister_consumer, ctx + i, +"consumer%d" , i); + } +if (IS_ERR (ctx[i].thread)) { + ret =PTR_ERR (ctx[i].thread); +while (i--) +kthread_stop_put (ctx[i].thread); +KUNIT_ASSERT_EQ (test, ret,0 ); + } +get_task_struct (ctx[i].thread); + +wait_for_completion (&ctx[i].started); + } + + rtc->priv = &ctx[1 ]; + ctx[1 ].expected_ret =0x12345678 ; +/* consumer1 enters reset(). */ +KUNIT_EXPECT_EQ (test, rtc->reset_count,0 ); +complete (&ctx[1 ].enter); +wait_for_completion (&ctx[1 ].entered); +KUNIT_EXPECT_EQ (test, rtc->reset_count,1 ); +
drivers/reset/core-test.c line 317
Quoted 10 lines
drivers/reset/core-test.c new file +10
-0+/* supplier0 unregisters. */ +complete (&ctx[0 ].enter); + ret =read_poll_timeout (reset_control_status, val, val == -ENODEV , +1000 ,100000 , false, rtc->rstc); +if (ret) { +for (i =2 ; i >=0 ; --i) +kthread_stop_put (ctx[i].thread); +KUNIT_ASSERT_EQ (test, ret,0 ); + } +/* supplier0 can't exit. It's waiting for all consumers. */
This comment looks like an untested assertion. For all we know, reset_controller_unregister() may have already returned by now.
How about signalling a completion "ctx->exited" after reset_controller_unregister() returns, calling wait_for_completion_timeout() here, and asserting it times out?
regards
Philipp
Quoted 15 lines · Philipp Zabel
drivers/reset/core-test.c line 317
Quoted 10 lines · Tzung-Bi Shih
drivers/reset/core-test.c new file +10
-0+/* supplier0 unregisters. */ +complete (&ctx[0 ].enter); + ret =read_poll_timeout (reset_control_status, val, val == -ENODEV , +1000 ,100000 , false, rtc->rstc); +if (ret) { +for (i =2 ; i >=0 ; --i) +kthread_stop_put (ctx[i].thread); +KUNIT_ASSERT_EQ (test, ret,0 ); + } +/* supplier0 can't exit. It's waiting for all consumers. */ This comment looks like an untested assertion. For all we know,
reset_controller_unregister()may have already returned by now.
Quoted 3 lines
How about signalling a completion "
ctx->exited" afterreset_controller_unregister()returns, callingwait_for_completion_timeout()here, and asserting it times out?
Fixed in v3[1].