lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241002160722.20025-7-arbn@yandex-team.com>
Date: Wed,  2 Oct 2024 18:07:21 +0200
From: Andrey Ryabinin <arbn@...dex-team.com>
To: linux-kernel@...r.kernel.org
Cc: Alexander Graf <graf@...zon.com>,
	James Gowans <jgowans@...zon.com>,
	Mike Rapoport <rppt@...nel.org>,
	Andrew Morton <akpm@...ux-foundation.org>,
	linux-mm@...ck.org,
	Thomas Gleixner <tglx@...utronix.de>,
	Ingo Molnar <mingo@...hat.com>,
	Borislav Petkov <bp@...en8.de>,
	Dave Hansen <dave.hansen@...ux.intel.com>,
	x86@...nel.org,
	"H. Peter Anvin" <hpa@...or.com>,
	Eric Biederman <ebiederm@...ssion.com>,
	kexec@...ts.infradead.org,
	Steven Rostedt <rostedt@...dmis.org>,
	Masami Hiramatsu <mhiramat@...nel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@...icios.com>,
	linux-trace-kernel@...r.kernel.org,
	valesini@...dex-team.com,
	Andrey Ryabinin <arbn@...dex-team.com>
Subject: [RFC PATCH 6/7] kstate, test: add test module for testing kstate subsystem.

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@...dex-team.com>
---
 include/linux/kstate.h |  1 +
 lib/Makefile           |  2 +
 lib/test_kstate.c      | 89 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+)
 create mode 100644 lib/test_kstate.c

diff --git a/include/linux/kstate.h b/include/linux/kstate.h
index 855acb339d5d7..2ddbe41a1f171 100644
--- a/include/linux/kstate.h
+++ b/include/linux/kstate.h
@@ -31,6 +31,7 @@ struct kstate_field {
 enum kstate_ids {
 	KSTATE_PAGE_ID,
 	KSTATE_RSVD_MEM_ID,
+	KSTATE_TEST_ID,
 	KSTATE_LAST_ID = -1,
 };
 
diff --git a/lib/Makefile b/lib/Makefile
index 773adf88af416..2432e47664c35 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -354,6 +354,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 0000000000000..e95e3110f8949
--- /dev/null
+++ b/lib/test_kstate.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/io.h>
+#include <linux/kstate.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+
+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_SIMPLE(i, struct kstate_test_data),
+		KSTATE_SIMPLE(s, struct kstate_test_data),
+		KSTATE_POINTER(p_ulong, struct kstate_test_data),
+		{
+			.name = "page",
+			.flags = KS_CUSTOM,
+			.offset = offsetof(struct kstate_test_data, page),
+			.save = kstate_page_save,
+		},
+		KSTATE_SIMPLE(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;
+
+	WARN_ON(test_data.i != 10);
+	WARN_ON(*test_data.p_ulong != 20);
+	WARN_ON(strcmp(test_data.s, "abcdefghk") != 0);
+
+	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_migrate_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.2


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ