Skip to contentSkip to conversation

kho: Add support for kunit mocking KHO restore API

PATCH Superseded Samiullah Khawaja 2 participants 6 messages May 12 – May 18

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

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      |  61 ++++++++++-
 kernel/liveupdate/kexec_handover_test.c | 130 ++++++++++++++++++++++++
 5 files changed, 206 insertions(+), 1 deletion(-)
 create mode 100644 kernel/liveupdate/kexec_handover_test.c

base-commit: 9974969c14031a097d6b45bcb7a06bb4aa525c40

Signature · 1 line
2.54.0.563.g4f69b47b94-goog
#1 lore raw

PATCH 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 | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/liveupdate/kexec_handover.c +8 -1
index d5718bef6d4d..1375291d9b07 100644
@@ -11,6 +11,7 @@
 #define pr_fmt(fmt) "KHO: " fmt
 
 #include <linux/cleanup.h>
+#include <kunit/static_stub.h>
 #include <linux/cma.h>
 #include <linux/kmemleak.h>
 #include <linux/count_zeros.h>
@@ -437,7 +438,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;
 }
@@ -459,6 +464,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.563.g4f69b47b94-goog
maintainer
#2 lore raw

Re: [PATCH 1/3] kho: Add kunit static stubs

Hi Sami,

Quoted 13 lines · Samiullah Khawaja

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 | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/liveupdate/kexec_handover.c +0 -0
index d5718bef6d4d..1375291d9b07 100644

kernel/liveupdate/kexec_handover.c line 14

Quoted 4 lines
kernel/liveupdate/kexec_handover.c +1 -0
@@ -11,6 +11,7 @@
 #define pr_fmt(fmt) "KHO: " fmt
 
 #include <linux/cleanup.h>
+#include <kunit/static_stub.h>

Can we include kunit headers before linux headers?

Quoted 28 lines
kernel/liveupdate/kexec_handover.c +7 -1
 #include <linux/cma.h>
 #include <linux/kmemleak.h>
 #include <linux/count_zeros.h>
@@ -437,7 +438,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;
 }
@@ -459,6 +464,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.563.g4f69b47b94-goog
Signature · 2 lines
Sincerely yours,
Mike.
#3 lore raw

Re: [PATCH 1/3] kho: Add kunit static stubs

Hi Mike,

Quoted 18 lines · Mike Rapoport

Hi Sami,

Quoted 15 lines · Samiullah Khawaja

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 | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/liveupdate/kexec_handover.c +0 -0
index d5718bef6d4d..1375291d9b07 100644
@@ -11,6 +11,7 @@
 #define pr_fmt(fmt) "KHO: " fmt
 
Quoted 4 lines

kernel/liveupdate/kexec_handover.c line 14

Quoted 2 lines
kernel/liveupdate/kexec_handover.c +1 -0
 #include <linux/cleanup.h>
+#include <kunit/static_stub.h>

Can we include kunit headers before linux headers?

Agreed. I will update this in next revision.

Quoted 21 lines
Quoted 21 lines
kernel/liveupdate/kexec_handover.c +5 -1
 #include <linux/cma.h>
 #include <linux/kmemleak.h>
 #include <linux/count_zeros.h>
@@ -437,7 +438,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;
 }
@@ -459,6 +464,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;
 
Quoted 12 lines
Quoted 8 lines
kernel/liveupdate/kexec_handover.c +2 -1
+	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));
--

2.54.0.563.g4f69b47b94-goog

Signature · 2 lines
Sincerely yours,
Mike.

Thanks,
Sami

#4 lore raw

PATCH 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 | 52 ++++++++++++++++++++++++++++++
 2 files changed, 57 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 +52 -0

61 lines not on this page · raw message

Signature · 1 line
2.54.0.563.g4f69b47b94-goog
#5 lore raw

PATCH 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 | 130 ++++++++++++++++++++++++
 3 files changed, 141 insertions(+)
 create mode 100644 kernel/liveupdate/kexec_handover_test.c
kernel/liveupdate/Kconfig +10 -0
index c13af38ba23a..ffdfdc4080ef 100644
@@ -49,6 +49,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 +130 -0

130 lines not on this page · raw message

Signature · 1 line
2.54.0.563.g4f69b47b94-goog