Skip to contentSkip to conversation

kho: Add support for kunit mocking KHO restore API

PATCH In review Samiullah Khawaja 2 participants 7 messages May 21 – Jun 17

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover kho: Add support for kunit mocking KHO restore API

To write kunit tests for preservation and restoration of liveupdate state in various subsystems without triggering the actual kexec, the KHO restore API needs to be mocked by the test writer. The mocking is done to allow testing of the individual components or functions in isolation.

The patch series adds the following to support kunit testing when using the KHO API:

- Add static stub hooks to mock the KHO restore API so the restore path can be tested without triggering kexec.
- Add helper function that can be used by the test writer to check if memory is preserved in KHO tree.

Finally, it adds a KUnit test for the KHO API that verifies the allocation of preserved memory, and the preservation/restoration of pages and folios.

KHO Kunit test run:

KTAP version 1
1..1
KTAP version 1
# Subtest: kho_test
# module: kexec_handover_test
1..3
ok 1 kho_test_alloc_preserve
ok 2 kho_test_preserve_pages
ok 3 kho_test_preserve_folio
# kho_test: pass:3 fail:0 skip:0 total:3
# Totals: pass:3 fail:0 skip:0 total:3
ok 1 kho_test

v2:
- Move kunit header includes above linux header includes.
- Use the __kho_preserve_pages_order() to get the order of preserved pages instead of open order calculation math.

Samiullah Khawaja (3):
kho: Add kunit static stubs
kho: Add helper function to check if pages are preserved
kho: Add kunit test to verify preserve/restore pages and folio

 include/linux/kexec_handover.h          |   5 +
 kernel/liveupdate/Kconfig               |  10 ++
 kernel/liveupdate/Makefile              |   1 +
 kernel/liveupdate/kexec_handover.c      |  63 +++++++++++-
 kernel/liveupdate/kexec_handover_test.c | 131 ++++++++++++++++++++++++
 5 files changed, 209 insertions(+), 1 deletion(-)
 create mode 100644 kernel/liveupdate/kexec_handover_test.c

base-commit: ec4084bc445027a52f600e30a976928be1ba1950

Signature · 1 line
2.54.0.746.g67dd491aae-goog
#1 lore raw

PATCH v2 1/3 kho: Add kunit static stubs

Add kunit stubs in kho_restore_folio() and kho_restore_pages() so the users can mock these functions in kunit tests.

---
 kernel/liveupdate/kexec_handover.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
kernel/liveupdate/kexec_handover.c +9 -1
index 11855e275397..902019f36c24 100644
@@ -10,6 +10,8 @@
 
 #define pr_fmt(fmt) "KHO: " fmt
 
+#include <kunit/static_stub.h>
+
 #include <linux/cleanup.h>
 #include <linux/cma.h>
 #include <linux/kmemleak.h>
@@ -423,7 +425,11 @@ static struct page *kho_restore_page(phys_addr_t phys, bool is_folio)
  */
 struct folio *kho_restore_folio(phys_addr_t phys)
 {
-	struct page *page = kho_restore_page(phys, true);
+	struct page *page;
+
+	KUNIT_STATIC_STUB_REDIRECT(kho_restore_folio, phys);
+
+	page = kho_restore_page(phys, true);
 
 	return page ? page_folio(page) : NULL;
 }
@@ -445,6 +451,8 @@ struct page *kho_restore_pages(phys_addr_t phys, unsigned long nr_pages)
 	const unsigned long end_pfn = start_pfn + nr_pages;
 	unsigned long pfn = start_pfn;
 
+	KUNIT_STATIC_STUB_REDIRECT(kho_restore_pages, phys, nr_pages);
+
 	while (pfn < end_pfn) {
 		const unsigned int order =
 			min(count_trailing_zeros(pfn), ilog2(end_pfn - pfn));
Signature · 1 line
2.54.0.746.g67dd491aae-goog
#2 lore raw

PATCH v2 2/3 kho: Add helper function to check if pages are preserved

In reply to Samiullah Khawaja

Kunit test needs to check whether the pages are preserved after preservation or after unpreserve. The helper function added is only enabled when kunit is enabled.

---
 include/linux/kexec_handover.h     |  5 +++
 kernel/liveupdate/kexec_handover.c | 53 ++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
include/linux/kexec_handover.h +5 -0
index 8968c56d2d73..a3afc74cf41c 100644
@@ -32,6 +32,11 @@ void kho_restore_free(void *mem);
 struct folio *kho_restore_folio(phys_addr_t phys);
 struct page *kho_restore_pages(phys_addr_t phys, unsigned long nr_pages);
 void *kho_restore_vmalloc(const struct kho_vmalloc *preservation);
+
+#if IS_ENABLED(CONFIG_KUNIT)
+bool kho_test_pages_preserved(phys_addr_t phys, unsigned long nr_pages);
+#endif
+
 int kho_add_subtree(const char *name, void *blob, size_t size);
 void kho_remove_subtree(void *blob);
 int kho_retrieve_subtree(const char *name, phys_addr_t *phys, size_t *size);
kernel/liveupdate/kexec_handover.c +53 -0

62 lines not on this page · raw message

Signature · 1 line
2.54.0.746.g67dd491aae-goog
#3 lore raw

PATCH v2 3/3 kho: Add kunit test to verify preserve/restore pages and folio

In reply to Samiullah Khawaja

Add a kunit test to verify the preserve/unpreserve and restore of pages and folios by mocking the kho kunit restore stubs.

---
 kernel/liveupdate/Kconfig               |  10 ++
 kernel/liveupdate/Makefile              |   1 +
 kernel/liveupdate/kexec_handover_test.c | 131 ++++++++++++++++++++++++
 3 files changed, 142 insertions(+)
 create mode 100644 kernel/liveupdate/kexec_handover_test.c
kernel/liveupdate/Kconfig +10 -0
index 1a8513f16ef7..d2623f60c0dd 100644
@@ -51,6 +51,16 @@ config KEXEC_HANDOVER_ENABLE_DEFAULT
 	  The default behavior can still be overridden at boot time by
 	  passing 'kho=off'.
 
+config KEXEC_HANDOVER_KUNIT_TEST
+	tristate "KUnit test for Kexec Handover (KHO)"
+	depends on KEXEC_HANDOVER && KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  Enable Kunit tests for Kexec Handover to verify preservation and
+	  unpreservation of memory using KHO API without triggering kexec.
+
+	  If unsure, say N.
+
 config LIVEUPDATE
 	bool "Live Update Orchestrator"
 	depends on KEXEC_HANDOVER
kernel/liveupdate/Makefile +1 -0
index d2f779cbe279..ed798b2401a8 100644
@@ -9,5 +9,6 @@ luo-y :=								\
 obj-$(CONFIG_KEXEC_HANDOVER)		+= kexec_handover.o
 obj-$(CONFIG_KEXEC_HANDOVER_DEBUG)	+= kexec_handover_debug.o
 obj-$(CONFIG_KEXEC_HANDOVER_DEBUGFS)	+= kexec_handover_debugfs.o
+obj-$(CONFIG_KEXEC_HANDOVER_KUNIT_TEST) += kexec_handover_test.o
 
 obj-$(CONFIG_LIVEUPDATE)		+= luo.o
kernel/liveupdate/kexec_handover_test.c new file +131 -0

131 lines not on this page · raw message

Signature · 1 line
2.54.0.746.g67dd491aae-goog
maintainer
#4 lore raw
In reply to Samiullah Khawaja

Hi Samiullah,

Quoted 8 lines · Samiullah Khawaja

To write kunit tests for preservation and restoration of liveupdate state in various subsystems without triggering the actual kexec, the KHO restore API needs to be mocked by the test writer. The mocking is done to allow testing of the individual components or functions in isolation.

The patch series adds the following to support kunit testing when using the KHO API:

Quoted 7 lines

- Add static stub hooks to mock the KHO restore API so the restore path can be tested without triggering kexec.
- Add helper function that can be used by the test writer to check if memory is preserved in KHO tree.

Finally, it adds a KUnit test for the KHO API that verifies the allocation of preserved memory, and the preservation/restoration of pages and folios.

I looked at the tests for preservation and apparently they don't add coverage beyond the existing KHO selftest. How hard and/or intrusive would be adding tests for example for error paths?

Do you have an example of a kunit test for another subsystem that would benefit from mocking of KHO APIs?

Quoted 38 lines

KHO Kunit test run:

KTAP version 1
1..1
KTAP version 1
# Subtest: kho_test
# module: kexec_handover_test
1..3
ok 1 kho_test_alloc_preserve
ok 2 kho_test_preserve_pages
ok 3 kho_test_preserve_folio
# kho_test: pass:3 fail:0 skip:0 total:3
# Totals: pass:3 fail:0 skip:0 total:3
ok 1 kho_test

v2:
- Move kunit header includes above linux header includes.
- Use the __kho_preserve_pages_order() to get the order of preserved pages instead of open order calculation math.

Samiullah Khawaja (3):
kho: Add kunit static stubs
kho: Add helper function to check if pages are preserved
kho: Add kunit test to verify preserve/restore pages and folio

 include/linux/kexec_handover.h          |   5 +
 kernel/liveupdate/Kconfig               |  10 ++
 kernel/liveupdate/Makefile              |   1 +
 kernel/liveupdate/kexec_handover.c      |  63 +++++++++++-
 kernel/liveupdate/kexec_handover_test.c | 131 ++++++++++++++++++++++++
 5 files changed, 209 insertions(+), 1 deletion(-)
 create mode 100644 kernel/liveupdate/kexec_handover_test.c

base-commit: ec4084bc445027a52f600e30a976928be1ba1950

Signature · 1 line
2.54.0.746.g67dd491aae-goog
Signature · 2 lines
Sincerely yours,
Mike.
#5 lore raw

Hi Mike,

Sorry for the late reply, I got pulled into some other stuff.

Quoted 19 lines · Mike Rapoport

Hi Samiullah,

Quoted 8 lines · Samiullah Khawaja

To write kunit tests for preservation and restoration of liveupdate state in various subsystems without triggering the actual kexec, the KHO restore API needs to be mocked by the test writer. The mocking is done to allow testing of the individual components or functions in isolation.

The patch series adds the following to support kunit testing when using the KHO API:

Quoted 7 lines

- Add static stub hooks to mock the KHO restore API so the restore path can be tested without triggering kexec.
- Add helper function that can be used by the test writer to check if memory is preserved in KHO tree.

Finally, it adds a KUnit test for the KHO API that verifies the allocation of preserved memory, and the preservation/restoration of pages and folios.

Quoted 6 lines

I looked at the tests for preservation and apparently they don't add coverage beyond the existing KHO selftest. How hard and/or intrusive would be adding tests for example for error paths?

Do you have an example of a kunit test for another subsystem that would benefit from mocking of KHO APIs?

I think intrusive tests to get more coverage for KHO would probably not use the stubs added in this series, as these are meant to mock the KHO restore API itself.

My motivation was to allow downstream users of KHO to test their own preservation logic (making sure their ABI doesn't have bugs). LUO or the recently added KHO linked-block can probably be a start?

The KHO kunit test added here is just a minimum example of how to use the stubs, which is why it doesn't provide much extra coverage.

I am perfectly happy to drop this patch for now. We can get this in later when we have kunit tests for KHO users. WDYT?

Quoted 1 line

[snip]

Quoted 4 lines
Signature · 3 lines
-- 
Sincerely yours,
Mike.

Thanks,
Sami

maintainer
#6 lore raw

Hi Sami,

Quoted 35 lines · Samiullah Khawaja
Quoted 19 lines · Mike Rapoport

Hi Samiullah,

Quoted 8 lines · Samiullah Khawaja

To write kunit tests for preservation and restoration of liveupdate state in various subsystems without triggering the actual kexec, the KHO restore API needs to be mocked by the test writer. The mocking is done to allow testing of the individual components or functions in isolation.

The patch series adds the following to support kunit testing when using the KHO API:

Quoted 7 lines

- Add static stub hooks to mock the KHO restore API so the restore path can be tested without triggering kexec.
- Add helper function that can be used by the test writer to check if memory is preserved in KHO tree.

Finally, it adds a KUnit test for the KHO API that verifies the allocation of preserved memory, and the preservation/restoration of pages and folios.

Quoted 6 lines

I looked at the tests for preservation and apparently they don't add coverage beyond the existing KHO selftest. How hard and/or intrusive would be adding tests for example for error paths?

Do you have an example of a kunit test for another subsystem that would benefit from mocking of KHO APIs?

I think intrusive tests to get more coverage for KHO would probably not use the stubs added in this series, as these are meant to mock the KHO restore API itself.

My motivation was to allow downstream users of KHO to test their own preservation logic (making sure their ABI doesn't have bugs). LUO or the recently added KHO linked-block can probably be a start?

Quoted 5 lines

The KHO kunit test added here is just a minimum example of how to use the stubs, which is why it doesn't provide much extra coverage.

I am perfectly happy to drop this patch for now. We can get this in later when we have kunit tests for KHO users. WDYT?

I wouldn't want to merge this upstream before the actual users appear, but we can apply this to a branch in the liveupdate tree and keep it there so that subsystems that plan to use these stubs could base their work on that branch.

Let's revisit this after -rc1 is out.

Quoted 2 lines

Thanks,
Sami

Signature · 2 lines
Sincerely yours,
Mike.