From: Andrey Ryabinin <arbn@yandex-team.com>
To: linux-kernel@vger.kernel.org
Cc: Alexander Graf <graf@amazon.com>, Mike Rapoport <rppt@kernel.org>,
James Gowans <jgowans@amazon.com>,
Andrew Morton <akpm@linux-foundation.org>,
linux-mm@kvack.org, Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Baoquan He <bhe@redhat.com>,
kexec@lists.infradead.org, Pratyush Yadav <ptyadav@amazon.de>,
Jason Gunthorpe <jgg@nvidia.com>,
Pasha Tatashin <pasha.tatashin@soleen.com>,
David Rientjes <rientjes@google.com>,
Pratyush Yadav <pratyush@kernel.org>,
Changyuan Lyu <changyuanl@google.com>,
Jonathan Corbet <corbet@lwn.net>,
linux-doc@vger.kernel.org,
Andrey Ryabinin <ryabinin.a.a@gmail.com>,
Chris Li <chrisl@kernel.org>,
Ashish.Kalra@amd.com, William Tu <witu@nvidia.com>,
David Matlack <dmatlack@google.com>,
Andrey Ryabinin <arbn@yandex-team.com>
Subject: [PATCH v3 5/7] kstate, test: add test module for testing kstate subsystem.
Date: Tue, 9 Sep 2025 22:14:40 +0200 [thread overview]
Message-ID: <20250909201446.13138-6-arbn@yandex-team.com> (raw)
In-Reply-To: <20250909201446.13138-1-arbn@yandex-team.com>
This is simple test and playground useful kstate subsystem development.
It contains some structure with different kind of data which migrated
across kexec to the new kernel using kstate.
Signed-off-by: Andrey Ryabinin <arbn@yandex-team.com>
---
MAINTAINERS | 1 +
include/linux/kstate.h | 2 +
kernel/liveupdate/Kconfig | 8 +++
lib/Makefile | 2 +
lib/test_kstate.c | 116 ++++++++++++++++++++++++++++++++++++++
5 files changed, 129 insertions(+)
create mode 100644 lib/test_kstate.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 2cd9e49abee5..e96da6d97e75 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13723,6 +13723,7 @@ M: Andrey Ryabinin <ryabinin.a.a@gmail.com>
S: Maintained
F: include/linux/kstate.h
F: kernel/livupdate/kstate.c
+F: lib/test_kstate.c
KTD253 BACKLIGHT DRIVER
M: Linus Walleij <linus.walleij@linaro.org>
diff --git a/include/linux/kstate.h b/include/linux/kstate.h
index 5a95960e5b03..0ced0da37c8f 100644
--- a/include/linux/kstate.h
+++ b/include/linux/kstate.h
@@ -95,6 +95,8 @@ struct kstate_field {
enum kstate_ids {
KSTATE_FOLIO_ID = 1,
KSTATE_KHO_FDT_ID,
+ KSTATE_TEST_ID,
+ KSTATE_TEST_ID_V2,
KSTATE_LAST_ID = -1,
};
diff --git a/kernel/liveupdate/Kconfig b/kernel/liveupdate/Kconfig
index b6ea861006bf..af9a25bdcd6e 100644
--- a/kernel/liveupdate/Kconfig
+++ b/kernel/liveupdate/Kconfig
@@ -69,6 +69,14 @@ config KSTATE
state, save it into the memory and restore the state after kexec
in new kernel.
+config KSTATE_TEST
+ bool "KSTATE test code"
+ help
+ Build a simple test/playground code that is useful for kstate
+ subsystem development. It contains some structure with different
+ kind of data which migrated across kexec to the new kernel
+ using KSTATE.
+
config KEXEC_HANDOVER
bool "kexec handover"
depends on ARCH_SUPPORTS_KEXEC_HANDOVER && ARCH_SUPPORTS_KEXEC_FILE
diff --git a/lib/Makefile b/lib/Makefile
index 392ff808c9b9..46616577caf3 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -316,6 +316,8 @@ obj-$(CONFIG_PARMAN) += parman.o
obj-y += group_cpus.o
+obj-$(CONFIG_KSTATE_TEST) += test_kstate.o
+
# GCC library routines
obj-$(CONFIG_GENERIC_LIB_ASHLDI3) += ashldi3.o
obj-$(CONFIG_GENERIC_LIB_ASHRDI3) += ashrdi3.o
diff --git a/lib/test_kstate.c b/lib/test_kstate.c
new file mode 100644
index 000000000000..70534e8c718f
--- /dev/null
+++ b/lib/test_kstate.c
@@ -0,0 +1,116 @@
+// SPDX-License-Identifier: GPL-2.0
+#define pr_fmt(fmt) "kstate test: " fmt
+#include <linux/io.h>
+#include <linux/kexec_handover.h>
+#include <linux/kstate.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+
+static unsigned long ulong_val;
+struct kstate_test_data {
+ int i;
+ unsigned long *p_ulong;
+ char s[10];
+ struct folio *folio;
+};
+
+#define KSTATE_TEST_DATA_ID 123
+
+struct kstate_description test_state_v2 = {
+ .name = "test_v2",
+ .version_id = 1,
+ .id = KSTATE_TEST_ID_V2,
+ .fields = (const struct kstate_field[]) {
+ KSTATE_BASE_TYPE(i, struct kstate_test_data, int),
+ KSTATE_END_OF_LIST()
+ },
+};
+
+struct kstate_description test_state = {
+ .name = "test",
+ .version_id = 2,
+ .id = KSTATE_TEST_ID,
+ .fields = (const struct kstate_field[]) {
+ KSTATE_BASE_TYPE(s, struct kstate_test_data, char [10]),
+ KSTATE_POINTER(p_ulong, struct kstate_test_data),
+ KSTATE_FOLIO(folio, struct kstate_test_data),
+ KSTATE_BASE_TYPE_DEPRECATED(k, u16, 1),
+ KSTATE_END_OF_LIST()
+ },
+ .subsections = (const struct kstate_description *[]){
+ &test_state_v2,
+ NULL
+ },
+};
+
+static struct kstate_test_data test_data;
+
+static int init_test_data(void)
+{
+ struct folio *folio;
+ int i;
+
+ test_data.i = 10;
+ ulong_val = 20;
+ memcpy(test_data.s, "abcdefghk", sizeof(test_data.s));
+ folio = folio_alloc(GFP_KERNEL, 0);
+ if (!folio)
+ return -ENOMEM;
+
+ for (i = 0; i < folio_size(folio)/sizeof(u32); i += 4)
+ *((u32 *)folio_address(folio) + i) = 0xdeadbeef;
+ test_data.folio = folio;
+ return 0;
+}
+
+static void validate_test_data(void)
+{
+ int i;
+
+ if (WARN_ON(test_data.i != 10))
+ return;
+ if (WARN_ON(*test_data.p_ulong != 20))
+ return;
+ if (WARN_ON(strcmp(test_data.s, "abcdefghk") != 0))
+ return;
+
+ for (i = 0; i < folio_size(test_data.folio)/4; i += 4) {
+ u32 val = *((u32 *)folio_address(test_data.folio) + i);
+
+ if (WARN_ON_ONCE(val != 0xdeadbeef))
+ return;
+ }
+}
+
+static int __init test_kstate_init(void)
+{
+ int ret = 0;
+
+ test_data.p_ulong = &ulong_val;
+
+ ret = kstate_register(&test_state, &test_data, KSTATE_TEST_DATA_ID);
+ if (ret) {
+ pr_err("register failed %d\n", ret);
+ goto out;
+ }
+
+ if (!is_kho_boot()) {
+ ret = init_test_data();
+ if (ret)
+ goto out;
+ } else {
+ pr_info("restoring data\n");
+ ret = kstate_restore(&test_state, &test_data, KSTATE_TEST_DATA_ID);
+ if (ret) {
+ pr_err("restore failed %d\n", ret);
+ goto out;
+ }
+
+ }
+
+ validate_test_data();
+
+out:
+ return ret;
+}
+late_initcall(test_kstate_init);
--
2.49.1
next prev parent reply other threads:[~2025-09-09 20:15 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-09 20:14 [PATCH v3 0/7] KSTATE: [de]serialization framework for KHO Andrey Ryabinin
2025-09-09 20:14 ` [PATCH v3 1/7] kho: move fdt setup in separate helper Andrey Ryabinin
2025-09-09 20:14 ` [PATCH v3 2/7] kho: move scratch memory " Andrey Ryabinin
2025-09-09 20:14 ` [PATCH v3 3/7] kstate: Add KSTATE - [de]serialization framework for KHO Andrey Ryabinin
2025-09-09 20:14 ` [PATCH v3 4/7] kho: replace KHO FDT with kstate metadata Andrey Ryabinin
2025-09-10 16:50 ` Rob Herring
2025-09-11 16:54 ` Andrey Ryabinin
2025-09-09 20:14 ` Andrey Ryabinin [this message]
2025-09-10 0:33 ` [PATCH v3 5/7] kstate, test: add test module for testing kstate subsystem Randy Dunlap
2025-09-11 17:00 ` Andrey Ryabinin
2025-09-09 20:14 ` [PATCH v3 6/7] mm/memblock: Use KSTATE instead of kho to preserve preserved_mem_table Andrey Ryabinin
2025-09-15 11:47 ` Jason Gunthorpe
2025-09-18 19:00 ` Andrey Ryabinin
2025-09-18 23:14 ` Jason Gunthorpe
2025-09-09 20:14 ` [PATCH v3 7/7] Documentation, kstate: Add KSTATE documentation Andrey Ryabinin
2025-09-10 0:53 ` Bagas Sanjaya
2025-09-11 17:07 ` Andrey Ryabinin
2025-09-10 1:00 ` Randy Dunlap
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250909201446.13138-6-arbn@yandex-team.com \
--to=arbn@yandex-team.com \
--cc=Ashish.Kalra@amd.com \
--cc=akpm@linux-foundation.org \
--cc=bhe@redhat.com \
--cc=bp@alien8.de \
--cc=changyuanl@google.com \
--cc=chrisl@kernel.org \
--cc=corbet@lwn.net \
--cc=dave.hansen@linux.intel.com \
--cc=dmatlack@google.com \
--cc=graf@amazon.com \
--cc=hpa@zytor.com \
--cc=jgg@nvidia.com \
--cc=jgowans@amazon.com \
--cc=kexec@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mingo@redhat.com \
--cc=pasha.tatashin@soleen.com \
--cc=pratyush@kernel.org \
--cc=ptyadav@amazon.de \
--cc=rientjes@google.com \
--cc=rppt@kernel.org \
--cc=ryabinin.a.a@gmail.com \
--cc=tglx@linutronix.de \
--cc=witu@nvidia.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox