From: Andrey Ryabinin <arbn@yandex-team.com>
To: linux-kernel@vger.kernel.org
Cc: Alexander Graf <graf@amazon.com>,
James Gowans <jgowans@amazon.com>,
Mike Rapoport <rppt@kernel.org>,
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>,
Eric Biederman <ebiederm@xmission.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>,
Andrey Ryabinin <arbn@yandex-team.com>
Subject: [PATCH v2 7/7] kstate, test: add test module for testing kstate subsystem.
Date: Mon, 10 Mar 2025 13:03:18 +0100 [thread overview]
Message-ID: <20250310120318.2124-8-arbn@yandex-team.com> (raw)
In-Reply-To: <20250310120318.2124-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>
---
include/linux/kstate.h | 3 ++
kernel/kstate.c | 5 +++
lib/Makefile | 2 +
lib/test_kstate.c | 86 ++++++++++++++++++++++++++++++++++++++++++
4 files changed, 96 insertions(+)
create mode 100644 lib/test_kstate.c
diff --git a/include/linux/kstate.h b/include/linux/kstate.h
index 36cfefd87572..0bde76aa4d8f 100644
--- a/include/linux/kstate.h
+++ b/include/linux/kstate.h
@@ -90,6 +90,7 @@ struct kstate_field {
enum kstate_ids {
KSTATE_RSVD_MEM_ID = 1,
KSTATE_STRUCT_PAGE_ID,
+ KSTATE_TEST_ID,
KSTATE_LAST_ID = -1,
};
@@ -132,6 +133,8 @@ extern struct kstate_description page_state;
void kstate_init(void);
+bool is_kstate_kernel(void);
+
int kstate_save_state(void);
void free_kstate_stream(void);
diff --git a/kernel/kstate.c b/kernel/kstate.c
index 68a1272abceb..3d9b786da72a 100644
--- a/kernel/kstate.c
+++ b/kernel/kstate.c
@@ -287,6 +287,11 @@ static void restore_migrate_state(unsigned long kstate_data,
static unsigned long kstate_stream_addr = -1;
static unsigned long kstate_size;
+bool is_kstate_kernel(void)
+{
+ return kstate_stream_addr != -1;
+}
+
static void __kstate_register(struct kstate_description *state, void *obj,
struct state_entry *se)
{
diff --git a/lib/Makefile b/lib/Makefile
index d5cfc7afbbb8..1395b852b58d 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -356,6 +356,8 @@ obj-$(CONFIG_PARMAN) += parman.o
obj-y += group_cpus.o
+obj-$(CONFIG_KSTATE) += 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..1d9feb017415
--- /dev/null
+++ b/lib/test_kstate.c
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/io.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 page *page;
+};
+
+struct kstate_description test_state = {
+ .name = "test",
+ .version_id = 1,
+ .id = KSTATE_TEST_ID,
+ .state_list = LIST_HEAD_INIT(test_state.state_list),
+ .fields = (const struct kstate_field[]) {
+ KSTATE_BASE_TYPE(i, struct kstate_test_data, int),
+ KSTATE_BASE_TYPE(s, struct kstate_test_data, char [10]),
+ KSTATE_POINTER(p_ulong, struct kstate_test_data),
+ KSTATE_PAGE(page, struct kstate_test_data),
+ KSTATE_END_OF_LIST()
+ },
+};
+
+static struct kstate_test_data test_data;
+
+static int init_test_data(void)
+{
+ struct page *page;
+ int i;
+
+ test_data.i = 10;
+ ulong_val = 20;
+ memcpy(test_data.s, "abcdefghk", sizeof(test_data.s));
+ page = alloc_page(GFP_KERNEL);
+ if (!page)
+ return -ENOMEM;
+
+ for (i = 0; i < PAGE_SIZE/4; i += 4)
+ *((u32 *)page_address(page) + i) = 0xdeadbeef;
+ test_data.page = page;
+ 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 < PAGE_SIZE/4; i += 4) {
+ u32 val = *((u32 *)page_address(test_data.page) + i);
+
+ WARN_ON(val != 0xdeadbeef);
+ }
+}
+
+static int __init test_kstate_init(void)
+{
+ int ret = 0;
+
+ test_data.p_ulong = &ulong_val;
+
+ if (!is_kstate_kernel()) {
+ ret = init_test_data();
+ if (ret)
+ goto out;
+ }
+
+ kstate_register(&test_state, &test_data);
+
+ validate_test_data();
+
+out:
+ return ret;
+}
+__initcall(test_kstate_init);
--
2.45.3
next prev parent reply other threads:[~2025-03-10 12:04 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-10 12:03 [PATCH v2 0/7] KSTATE: a mechanism to migrate some part of the kernel state across kexec Andrey Ryabinin
2025-03-10 12:03 ` [PATCH v2 1/7] kstate: Add kstate - a mechanism to describe and migrate " Andrey Ryabinin
2025-03-10 12:03 ` [PATCH v2 2/7] kstate, kexec, x86: transfer kstate data " Andrey Ryabinin
2025-03-10 12:03 ` [PATCH v2 3/7] kexec: exclude control pages from the destination addresses Andrey Ryabinin
2025-03-10 12:03 ` [PATCH v2 4/7] kexec, kstate: delay loading of kexec segments Andrey Ryabinin
2025-03-11 11:31 ` kernel test robot
2025-03-11 12:25 ` kernel test robot
2025-03-10 12:03 ` [PATCH v2 5/7] x86, kstate: Add the ability to preserve memory pages across kexec Andrey Ryabinin
2025-03-10 12:03 ` [PATCH v2 6/7] kexec, kstate: save kstate data before kexec'ing Andrey Ryabinin
2025-03-10 12:03 ` Andrey Ryabinin [this message]
2025-03-11 2:27 ` [PATCH v2 0/7] KSTATE: a mechanism to migrate some part of the kernel state across kexec Cong Wang
2025-03-11 12:19 ` Andrey Ryabinin
2025-04-28 23:01 ` Chris Li
2025-04-28 23:01 ` Chris Li
2025-05-05 14:35 ` Andrey Ryabinin
2025-05-07 6:11 ` Chris Li
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=20250310120318.2124-8-arbn@yandex-team.com \
--to=arbn@yandex-team.com \
--cc=akpm@linux-foundation.org \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=ebiederm@xmission.com \
--cc=graf@amazon.com \
--cc=hpa@zytor.com \
--cc=jgg@nvidia.com \
--cc=jgowans@amazon.com \
--cc=kexec@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mingo@redhat.com \
--cc=pasha.tatashin@soleen.com \
--cc=ptyadav@amazon.de \
--cc=rientjes@google.com \
--cc=rppt@kernel.org \
--cc=tglx@linutronix.de \
--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