All of lore.kernel.org
 help / color / mirror / Atom feed
From: ira.weiny@intel.com
To: Dave Hansen <dave.hansen@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Dan Williams <dan.j.williams@intel.com>
Cc: Ira Weiny <ira.weiny@intel.com>,
	Fenghua Yu <fenghua.yu@intel.com>,
	Rick Edgecombe <rick.p.edgecombe@intel.com>,
	"Shankar, Ravi V" <ravi.v.shankar@intel.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH V9 14/45] x86/selftests: Add test_pks
Date: Thu, 10 Mar 2022 09:19:48 -0800	[thread overview]
Message-ID: <20220310172019.850939-15-ira.weiny@intel.com> (raw)
In-Reply-To: <20220310172019.850939-1-ira.weiny@intel.com>

From: Ira Weiny <ira.weiny@intel.com>

The PKS kernel tests are clumsy to run using debugfs directly.  It is
much nicer to have a user space application trigger the execution of
those tests.

Create test_pks as a selftest.

Output is as follows.

$ ./test_pks_64 -h
Usage: ./test_pks_64 [-h,-d] [test]
	--help,-h   This help
	--debug,-d  Output kernel debug via dynamic debug if available

        Run all PKS tests or the [test] specified.

	[test] can be one of:
	       'check_defaults'
	       'create_fault' (Not included in run all)

$ ./test_pks_64
[RUN]	check_defaults
[OK]

Suggested-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Ira Weiny <ira.weiny@intel.com>

---
Changes for V9:
	New Patch
---
 Documentation/core-api/protection-keys.rst |   3 +
 tools/testing/selftests/x86/Makefile       |   2 +-
 tools/testing/selftests/x86/test_pks.c     | 353 +++++++++++++++++++++
 3 files changed, 357 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/x86/test_pks.c

diff --git a/Documentation/core-api/protection-keys.rst b/Documentation/core-api/protection-keys.rst
index 4d99ca41c914..23330a7d53eb 100644
--- a/Documentation/core-api/protection-keys.rst
+++ b/Documentation/core-api/protection-keys.rst
@@ -148,3 +148,6 @@ Testing
 
 .. kernel-doc:: lib/pks/pks_test.c
         :doc: PKS_TEST
+
+.. kernel-doc:: tools/testing/selftests/x86/test_pks.c
+        :doc: PKS_TEST_USER
diff --git a/tools/testing/selftests/x86/Makefile b/tools/testing/selftests/x86/Makefile
index 8a1f62ab3c8e..e08670596c14 100644
--- a/tools/testing/selftests/x86/Makefile
+++ b/tools/testing/selftests/x86/Makefile
@@ -13,7 +13,7 @@ CAN_BUILD_WITH_NOPIE := $(shell ./check_cc.sh $(CC) trivial_program.c -no-pie)
 TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt test_mremap_vdso \
 			check_initial_reg_state sigreturn iopl ioperm \
 			test_vsyscall mov_ss_trap \
-			syscall_arg_fault fsgsbase_restore sigaltstack
+			syscall_arg_fault fsgsbase_restore sigaltstack test_pks
 TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
 			test_FCMOV test_FCOMI test_FISTTP \
 			vdso_restorer
diff --git a/tools/testing/selftests/x86/test_pks.c b/tools/testing/selftests/x86/test_pks.c
new file mode 100644
index 000000000000..df5bde9bfdbe
--- /dev/null
+++ b/tools/testing/selftests/x86/test_pks.c
@@ -0,0 +1,353 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright(c) 2022 Intel Corporation. All rights reserved.
+ */
+
+/**
+ * DOC: PKS_TEST_USER
+ *
+ * To assist in executing the tests 'test_pks' can be built from the
+ * tools/testing directory.  See the help output for details.
+ *
+ * .. code-block:: sh
+ *
+ *	$ cd tools/testing/selftests/x86
+ *	$ make test_pks
+ *	$ ./test_pks_64 -h
+ *	...
+ */
+#define _GNU_SOURCE
+#include <unistd.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define DYN_DBG_CNT_FILE "/sys/kernel/debug/dynamic_debug/control"
+#define PKS_TEST_FILE "/sys/kernel/debug/x86/run_pks"
+
+/* Values from the kernel */
+#define CHECK_DEFAULTS		"0"
+#define RUN_CRASH_TEST		"9"
+
+time_t g_start_time;
+int g_debug;
+
+#define PRINT_DEBUG(fmt, ...) \
+	do { \
+		if (g_debug) \
+			printf("%s: " fmt, __func__, ##__VA_ARGS__); \
+	} while (0)
+
+#define PRINT_ERROR(fmt, ...) \
+	fprintf(stderr, "%s: " fmt, __func__, ##__VA_ARGS__)
+
+static int do_simple_test(const char *debugfs_str);
+
+/*
+ * The crash test is a special case which is not included in the run all
+ * option.  Do not add it here.
+ */
+enum {
+	TEST_DEFAULTS = 0,
+	MAX_TESTS,
+} tests;
+
+/* Special */
+#define CREATE_FAULT_TEST_NAME "create_fault"
+
+struct test_item {
+	char *name;
+	const char *debugfs_str;
+	int (*test_fn)(const char *debugfs_str);
+} test_list[] = {
+	{ "check_defaults", CHECK_DEFAULTS, do_simple_test }
+};
+
+static char *get_test_name(int test_num)
+{
+	if (test_num > MAX_TESTS)
+		return "<UNKNOWN>";
+	/* Special: not in run all */
+	if (test_num == MAX_TESTS)
+		return CREATE_FAULT_TEST_NAME;
+	return test_list[test_num].name;
+}
+
+static int get_test_num(char *test_name)
+{
+	int i;
+
+	/* Special: not in run all */
+	if (strcmp(test_name, CREATE_FAULT_TEST_NAME) == 0)
+		return MAX_TESTS;
+
+	for (i = 0; i < MAX_TESTS; i++)
+		if (strcmp(test_name, test_list[i].name) == 0)
+			return i;
+	return -1;
+}
+
+static void print_help_and_exit(char *argv0)
+{
+	int i;
+
+	printf("Usage: %s [-h,-d] [test]\n", argv0);
+	printf("	--help,-h   This help\n");
+	printf("	--debug,-d  Output kernel debug via dynamic debug if available\n");
+	printf("\n");
+	printf("        Run all PKS tests or the [test] specified.\n");
+	printf("\n");
+	printf("	[test] can be one of:\n");
+
+	for (i = 0; i < MAX_TESTS; i++)
+		printf("	       '%s'\n", get_test_name(i));
+
+	/* Special: not in run all */
+	printf("	       '%s' (Not included in run all)\n",
+		CREATE_FAULT_TEST_NAME);
+
+	printf("\n");
+}
+
+/*
+ * Do a simple test of writing the debugfs value and reading back for 'PASS'
+ */
+static int do_simple_test(const char *debugfs_str)
+{
+	char str[16];
+	int fd, rc = 0;
+
+	fd = open(PKS_TEST_FILE, O_RDWR);
+	if (fd < 0) {
+		PRINT_DEBUG("Failed to open test file : %s\n", PKS_TEST_FILE);
+		return -ENOENT;
+	}
+
+	rc = write(fd, debugfs_str, strlen(debugfs_str));
+	if (rc < 0) {
+		rc = -errno;
+		goto close_file;
+	}
+
+	rc = read(fd, str, 16);
+	if (rc < 0)
+		goto close_file;
+
+	str[15] = '\0';
+
+	if (strncmp(str, "PASS", 4)) {
+		PRINT_ERROR("result: %s\n", str);
+		rc = -EFAULT;
+		goto close_file;
+	}
+
+	rc = 0;
+
+close_file:
+	close(fd);
+	return rc;
+}
+
+/*
+ * This test is special in that it requires the option to be written 2 times.
+ * In addition because it creates a fault it is not included in the run all
+ * test suite.
+ */
+static int create_fault(void)
+{
+	char str[16];
+	int fd, rc = 0;
+
+	fd = open(PKS_TEST_FILE, O_RDWR);
+	if (fd < 0) {
+		PRINT_DEBUG("Failed to open test file : %s\n", PKS_TEST_FILE);
+		return -ENOENT;
+	}
+
+	rc = write(fd, "9", 1);
+	if (rc < 0) {
+		rc = -errno;
+		goto close_file;
+	}
+
+	rc = write(fd, "9", 1);
+	if (rc < 0)
+		goto close_file;
+
+	rc = read(fd, str, 16);
+	if (rc < 0)
+		goto close_file;
+
+	str[15] = '\0';
+
+	if (strncmp(str, "PASS", 4)) {
+		PRINT_ERROR("result: %s\n", str);
+		rc = -EFAULT;
+		goto close_file;
+	}
+
+	rc = 0;
+
+close_file:
+	close(fd);
+	return rc;
+}
+
+static int run_one(int test_num)
+{
+	int ret;
+
+	printf("[RUN]\t%s\n", get_test_name(test_num));
+
+	if (test_num == MAX_TESTS)
+		/* Special: not in run all */
+		ret = create_fault();
+	else
+		ret = test_list[test_num].test_fn(test_list[test_num].debugfs_str);
+
+	if (ret == -ENOENT) {
+		printf("[SKIP] Test not supported\n");
+		return 0;
+	} else if (ret) {
+		printf("[FAIL]\n");
+		return 1;
+	}
+
+	printf("[OK]\n");
+	return 0;
+}
+
+static int run_all(void)
+{
+	int i, rc = 0;
+
+	for (i = 0; i < MAX_TESTS; i++) {
+		int ret = run_one(i);
+
+		/* sticky fail */
+		if (ret)
+			rc = ret;
+	}
+
+	return rc;
+}
+
+#define STR_LEN 256
+
+/* Debug output in the kernel is through dynamic debug */
+static void setup_debug(void)
+{
+	char str[STR_LEN];
+	int fd, rc;
+
+	g_start_time = time(NULL);
+
+	fd = open(DYN_DBG_CNT_FILE, O_RDWR);
+	if (fd < 0) {
+		PRINT_ERROR("Dynamic debug not available: Failed to open: %s\n",
+			DYN_DBG_CNT_FILE);
+		return;
+	}
+
+	snprintf(str, STR_LEN, "file pks_test.c +pflm");
+
+	rc = write(fd, str, strlen(str));
+	if (rc != strlen(str))
+		PRINT_ERROR("ERROR: Failed to set up dynamic debug...\n");
+
+	close(fd);
+}
+
+static void print_debug(void)
+{
+	char str[STR_LEN];
+	struct tm *tm;
+	int fd, rc;
+
+	fd = open(DYN_DBG_CNT_FILE, O_RDWR);
+	if (fd < 0)
+		return;
+
+	snprintf(str, STR_LEN, "file pks_test.c -p");
+
+	rc = write(fd, str, strlen(str));
+	if (rc != strlen(str))
+		PRINT_ERROR("ERROR: Failed to turn off dynamic debug...\n");
+
+	close(fd);
+
+	/*
+	 * dmesg is not accurate with time stamps so back up the start time a
+	 * bit to ensure all the output from this run is dumped.
+	 */
+	g_start_time -= 5;
+	tm = localtime(&g_start_time);
+
+	snprintf(str, STR_LEN,
+		 "dmesg -H --since '%d-%d-%d %d:%d:%d' | grep pks_test",
+		 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+		 tm->tm_hour, tm->tm_min, tm->tm_sec);
+	system(str);
+	printf("\tDebug output command (approximate start time):\n\t\t%s\n",
+		str);
+}
+
+int main(int argc, char *argv[])
+{
+	int flag_all = 1;
+	int test_num = 0;
+	int rc;
+
+	while (1) {
+		static struct option long_options[] = {
+			{"help",	no_argument,	0,	'h' },
+			{"debug",	no_argument,	0,	'd' },
+			{0,		0,		0,	0 }
+		};
+		int option_index = 0;
+		int c;
+
+		c = getopt_long(argc, argv, "hd", long_options, &option_index);
+		if (c == -1)
+			break;
+
+		switch (c) {
+		case 'h':
+			print_help_and_exit(argv[0]);
+			return 0;
+		case 'd':
+			g_debug++;
+			break;
+		default:
+			print_help_and_exit(argv[0]);
+			exit(-1);
+		}
+	}
+
+	if (optind < argc) {
+		test_num = get_test_num(argv[optind]);
+		if (test_num < 0) {
+			printf("[RUN]\t'%s'\n[SKIP]\tInvalid test\n", argv[optind]);
+			return 1;
+		}
+
+		flag_all = 0;
+	}
+
+	if (g_debug)
+		setup_debug();
+
+	if (flag_all)
+		rc = run_all();
+	else
+		rc = run_one(test_num);
+
+	if (g_debug)
+		print_debug();
+
+	return rc;
+}
-- 
2.35.1


  parent reply	other threads:[~2022-03-10 17:24 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-10 17:19 [PATCH V9 00/45] PKS/PMEM: Add Stray Write Protection ira.weiny
2022-03-10 17:19 ` [PATCH V9 01/45] entry: Create an internal irqentry_exit_cond_resched() call ira.weiny
2022-04-07  2:48   ` Ira Weiny
2022-03-10 17:19 ` [PATCH V9 02/45] Documentation/protection-keys: Clean up documentation for User Space pkeys ira.weiny
2022-03-10 17:19 ` [PATCH V9 03/45] x86/pkeys: Clarify PKRU_AD_KEY macro ira.weiny
2022-03-10 17:19 ` [PATCH V9 04/45] x86/pkeys: Make PKRU macros generic ira.weiny
2022-03-10 17:19 ` [PATCH V9 05/45] x86/fpu: Refactor arch_set_user_pkey_access() ira.weiny
2022-03-10 17:19 ` [PATCH V9 06/45] mm/pkeys: Add Kconfig options for PKS ira.weiny
2022-03-10 17:19 ` [PATCH V9 07/45] x86/pkeys: Add PKS CPU feature bit ira.weiny
2022-03-10 17:19 ` [PATCH V9 08/45] x86/fault: Adjust WARN_ON for pkey fault ira.weiny
2022-03-10 17:19 ` [PATCH V9 09/45] Documentation/pkeys: Add initial PKS documentation ira.weiny
2022-03-10 17:19 ` [PATCH V9 10/45] mm/pkeys: Provide for PKS key allocation ira.weiny
2022-03-10 17:19 ` [PATCH V9 11/45] x86/pkeys: Enable PKS on cpus which support it ira.weiny
2022-03-10 17:19 ` [PATCH V9 12/45] mm/pkeys: Define PKS page table macros ira.weiny
2022-03-10 17:19 ` [PATCH V9 13/45] mm/pkeys: PKS testing, add initial test code ira.weiny
2022-03-10 17:19 ` ira.weiny [this message]
2022-03-10 17:19 ` [PATCH V9 15/45] x86/pkeys: Introduce pks_write_pkrs() ira.weiny
2022-03-10 17:19 ` [PATCH V9 16/45] x86/pkeys: Preserve the PKS MSR on context switch ira.weiny
2022-03-10 17:19 ` [PATCH V9 17/45] mm/pkeys: Introduce pks_set_readwrite() ira.weiny
2022-03-10 17:19 ` [PATCH V9 18/45] mm/pkeys: Introduce pks_set_noaccess() ira.weiny
2022-03-10 17:19 ` [PATCH V9 19/45] mm/pkeys: Introduce PKS fault callbacks ira.weiny
2022-03-10 17:19 ` [PATCH V9 20/45] mm/pkeys: PKS testing, add a fault call back ira.weiny
2022-03-10 17:19 ` [PATCH V9 21/45] mm/pkeys: PKS testing, add pks_set_*() tests ira.weiny
2022-03-10 17:19 ` [PATCH V9 22/45] mm/pkeys: PKS testing, test context switching ira.weiny
2022-03-10 17:19 ` [PATCH V9 23/45] x86/entry: Add auxiliary pt_regs space ira.weiny
2022-03-10 17:19 ` [PATCH V9 24/45] entry: Split up irqentry_exit_cond_resched() ira.weiny
2022-04-07  2:50   ` Ira Weiny
2022-03-10 17:19 ` [PATCH V9 25/45] entry: Add calls for save/restore auxiliary pt_regs ira.weiny
2022-03-10 17:20 ` [PATCH V9 26/45] x86/entry: Define arch_{save|restore}_auxiliary_pt_regs() ira.weiny
2022-03-10 17:20 ` [PATCH V9 27/45] x86/pkeys: Preserve PKRS MSR across exceptions ira.weiny
2022-03-10 17:20 ` [PATCH V9 28/45] x86/fault: Print PKS MSR on fault ira.weiny
2022-03-10 17:20 ` [PATCH V9 29/45] mm/pkeys: PKS testing, Add exception test ira.weiny
2022-03-10 17:20 ` [PATCH V9 30/45] mm/pkeys: Introduce pks_update_exception() ira.weiny
2022-03-10 17:20 ` [PATCH V9 31/45] mm/pkeys: PKS testing, test pks_update_exception() ira.weiny
2022-03-10 17:20 ` [PATCH V9 32/45] mm/pkeys: PKS testing, add test for all keys ira.weiny
2022-03-10 17:20 ` [PATCH V9 33/45] mm/pkeys: Add pks_available() ira.weiny
2022-03-10 17:20 ` [PATCH V9 34/45] memremap_pages: Add Kconfig for DEVMAP_ACCESS_PROTECTION ira.weiny
2022-03-10 17:20 ` [PATCH V9 35/45] memremap_pages: Introduce pgmap_protection_available() ira.weiny
2022-03-10 17:20 ` [PATCH V9 36/45] memremap_pages: Introduce a PGMAP_PROTECTION flag ira.weiny
2022-03-10 17:20 ` [PATCH V9 37/45] memremap_pages: Introduce devmap_protected() ira.weiny
2022-03-10 17:20 ` [PATCH V9 38/45] memremap_pages: Reserve a PKS pkey for eventual use by PMEM ira.weiny
2022-03-10 17:20 ` [PATCH V9 39/45] memremap_pages: Set PKS pkey in PTEs if requested ira.weiny
2022-03-10 17:20 ` [PATCH V9 40/45] memremap_pages: Define pgmap_set_{readwrite|noaccess}() calls ira.weiny
2022-03-10 17:20 ` [PATCH V9 41/45] memremap_pages: Add memremap.pks_fault_mode ira.weiny
2022-03-10 17:20 ` [PATCH V9 42/45] kmap: Make kmap work for devmap protected pages ira.weiny
2022-03-10 17:20 ` [PATCH V9 43/45] dax: Stray access protection for dax_direct_access() ira.weiny
2022-03-10 17:20 ` [PATCH V9 44/45] nvdimm/pmem: Enable stray access protection ira.weiny
2022-03-10 17:20 ` [PATCH V9 45/45] devdax: " ira.weiny
2022-03-31 17:13 ` [PATCH V9 00/45] PKS/PMEM: Add Stray Write Protection Ira Weiny

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=20220310172019.850939-15-ira.weiny@intel.com \
    --to=ira.weiny@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=fenghua.yu@intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ravi.v.shankar@intel.com \
    --cc=rick.p.edgecombe@intel.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.