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>,
	linux-kernel@vger.kernel.org
Subject: [PATCH V8 20/44] mm/pkeys: Add PKS test for context switching
Date: Thu, 27 Jan 2022 09:54:41 -0800	[thread overview]
Message-ID: <20220127175505.851391-21-ira.weiny@intel.com> (raw)
In-Reply-To: <20220127175505.851391-1-ira.weiny@intel.com>

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

PKS software must maintain the PKRS value for each thread in the system.
It must then restore this value whenever a thread is scheduled in.

Create a user space test to test this.  The test runs 2 processes
simultaneously on the same CPU.  One sets up a known PKS value for the
test pkey and sleeps while the other runs through all the protections
using the same pkey.  The first process is then allowed to run and it
checks that its PKRS value was properly restored.

On the kernel side 2 additional commands are added.  One is a mechanism
to arm a context and the other checks that context.  The kernel
maintains this context while the char device remains open.  The context
is cleaned up with the fd is closed.

Signed-off-by: Ira Weiny <ira.weiny@intel.com>

---
Changes for V8
	Split this off from the main testing patch
	Remove unneeded prints
---
 lib/pks/pks_test.c                     |  74 +++++++++++
 tools/testing/selftests/x86/Makefile   |   2 +-
 tools/testing/selftests/x86/test_pks.c | 168 +++++++++++++++++++++++++
 3 files changed, 243 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/x86/test_pks.c

diff --git a/lib/pks/pks_test.c b/lib/pks/pks_test.c
index fad9b996562a..933f1bed4820 100644
--- a/lib/pks/pks_test.c
+++ b/lib/pks/pks_test.c
@@ -15,6 +15,8 @@
  *
  * * 0  Loop through all CPUs, report the msr, and check against the default.
  * * 1  Allocate a single key and check all 3 permissions on a page.
+ * * 2  'arm context' for context switch test
+ * * 3  Check the context armed in '2' to ensure the MSR value was preserved
  * * 8  Loop through all CPUs, report the msr, and check against the default.
  * * 9  Set up and fault on a PKS protected page.
  *
@@ -24,6 +26,11 @@
  * $ cat /sys/kernel/debug/x86/run_pks
  *
  * Will print the result of the last test.
+ *
+ * To automate context switch testing a user space program is provided in:
+ *
+ *	.../tools/testing/selftests/x86/test_pks.c
+ *
  */
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -33,6 +40,9 @@
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/pkeys.h>
+#include <uapi/asm-generic/mman-common.h>
+
+#include <asm/pks.h>
 
 #include <asm/pks.h>
 
@@ -40,6 +50,8 @@
 
 #define CHECK_DEFAULTS		0
 #define RUN_SINGLE		1
+#define ARM_CTX_SWITCH		2
+#define CHECK_CTX_SWITCH	3
 #define RUN_CRASH_TEST		9
 
 static struct dentry *pks_test_dentry;
@@ -309,6 +321,55 @@ static void arm_or_run_crash_test(void)
 	crash_armed = false;
 }
 
+static void arm_ctx_switch(struct file *file)
+{
+	struct pks_test_ctx *ctx;
+
+	ctx = alloc_ctx(PKS_KEY_TEST);
+	if (IS_ERR(ctx)) {
+		pr_err("Failed to allocate a context\n");
+		last_test_pass = false;
+		return;
+	}
+
+	/* Store context for later checks */
+	if (file->private_data) {
+		pr_warn("Context already armed\n");
+		free_ctx(file->private_data);
+	}
+	file->private_data = ctx;
+
+	/* Ensure a known state to test context switch */
+	pks_mk_readwrite(ctx->pkey);
+}
+
+static void check_ctx_switch(struct file *file)
+{
+	struct pks_test_ctx *ctx;
+	unsigned long reg_pkrs;
+	int access;
+
+	last_test_pass = true;
+
+	if (!file->private_data) {
+		pr_err("No Context switch configured\n");
+		last_test_pass = false;
+		return;
+	}
+
+	ctx = file->private_data;
+
+	rdmsrl(MSR_IA32_PKRS, reg_pkrs);
+
+	access = (reg_pkrs >> PKR_PKEY_SHIFT(ctx->pkey)) &
+		  PKEY_ACCESS_MASK;
+	if (access != 0) {
+		last_test_pass = false;
+		pr_err("Context switch check failed: pkey %d: 0x%x reg: 0x%lx\n",
+			ctx->pkey, access, reg_pkrs);
+	}
+}
+
 static ssize_t pks_read_file(struct file *file, char __user *user_buf,
 			     size_t count, loff_t *ppos)
 {
@@ -351,6 +412,14 @@ static ssize_t pks_write_file(struct file *file, const char __user *user_buf,
 	case RUN_SINGLE:
 		last_test_pass = run_single();
 		break;
+	case ARM_CTX_SWITCH:
+		/* start of context switch test */
+		arm_ctx_switch(file);
+		break;
+	case CHECK_CTX_SWITCH:
+		/* After context switch MSR should be restored */
+		check_ctx_switch(file);
+		break;
 	default:
 		last_test_pass = false;
 		break;
@@ -365,6 +434,11 @@ static ssize_t pks_write_file(struct file *file, const char __user *user_buf,
 
 static int pks_release_file(struct inode *inode, struct file *file)
 {
+	struct pks_test_ctx *ctx = file->private_data;
+
+	if (ctx)
+		free_ctx(ctx);
+
 	return 0;
 }
 
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..9a24a4a61f28
--- /dev/null
+++ b/tools/testing/selftests/x86/test_pks.c
@@ -0,0 +1,168 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright(c) 2021 Intel Corporation. All rights reserved.
+ *
+ * User space tool to test PKS operations.  Accesses test code through
+ * <debugfs>/x86/run_pks when CONFIG_PKS_TEST is enabled.
+ */
+
+#define _GNU_SOURCE
+#include <sched.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <assert.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdbool.h>
+
+#define PKS_TEST_FILE "/sys/kernel/debug/x86/run_pks"
+
+#define RUN_SINGLE		"1"
+#define ARM_CTX_SWITCH		"2"
+#define CHECK_CTX_SWITCH	"3"
+
+void print_help_and_exit(char *argv0)
+{
+	printf("Usage: %s [-h] <cpu>\n", argv0);
+	printf("	--help,-h  This help\n");
+	printf("\n");
+	printf("	Run a context switch test on <cpu> (Default: 0)\n");
+}
+
+int check_context_switch(int cpu)
+{
+	int switch_done[2];
+	int setup_done[2];
+	cpu_set_t cpuset;
+	char result[32];
+	int rc = 0;
+	pid_t pid;
+	int fd;
+
+	CPU_ZERO(&cpuset);
+	CPU_SET(cpu, &cpuset);
+	/*
+	 * Ensure the two processes run on the same CPU so that they go through
+	 * a context switch.
+	 */
+	sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset);
+
+	if (pipe(setup_done)) {
+		printf("ERROR: Failed to create pipe\n");
+		return -1;
+	}
+	if (pipe(switch_done)) {
+		printf("ERROR: Failed to create pipe\n");
+		return -1;
+	}
+
+	pid = fork();
+	if (pid == 0) {
+		char done = 'y';
+
+		fd = open(PKS_TEST_FILE, O_RDWR);
+		if (fd < 0) {
+			printf("ERROR: cannot open %s\n", PKS_TEST_FILE);
+			return -1;
+		}
+
+		cpu = sched_getcpu();
+		printf("Child running on cpu %d...\n", cpu);
+
+		/* Allocate and run test. */
+		write(fd, RUN_SINGLE, 1);
+
+		/* Arm for context switch test */
+		write(fd, ARM_CTX_SWITCH, 1);
+
+		printf("   tell parent to go\n");
+		write(setup_done[1], &done, sizeof(done));
+
+		/* Context switch out... */
+		printf("   Waiting for parent...\n");
+		read(switch_done[0], &done, sizeof(done));
+
+		/* Check msr restored */
+		printf("Checking result\n");
+		write(fd, CHECK_CTX_SWITCH, 1);
+
+		read(fd, result, 10);
+		printf("   #PF, context switch, pkey allocation and free tests: %s\n", result);
+		if (!strncmp(result, "PASS", 10)) {
+			rc = -1;
+			done = 'F';
+		}
+
+		/* Signal result */
+		write(setup_done[1], &done, sizeof(done));
+	} else {
+		char done = 'y';
+
+		read(setup_done[0], &done, sizeof(done));
+		cpu = sched_getcpu();
+		printf("Parent running on cpu %d\n", cpu);
+
+		fd = open(PKS_TEST_FILE, O_RDWR);
+		if (fd < 0) {
+			printf("ERROR: cannot open %s\n", PKS_TEST_FILE);
+			return -1;
+		}
+
+		/* run test with the same pkey */
+		write(fd, RUN_SINGLE, 1);
+
+		printf("   Signaling child.\n");
+		write(switch_done[1], &done, sizeof(done));
+
+		/* Wait for result */
+		read(setup_done[0], &done, sizeof(done));
+		if (done == 'F')
+			rc = -1;
+	}
+
+	close(fd);
+
+	return rc;
+}
+
+int main(int argc, char *argv[])
+{
+	int cpu = 0;
+	int rc;
+	int c;
+
+	while (1) {
+		int option_index = 0;
+		static struct option long_options[] = {
+			{"help",	no_argument,	0,	'h' },
+			{0,		0,		0,	0 }
+		};
+
+		c = getopt_long(argc, argv, "h", long_options, &option_index);
+		if (c == -1)
+			break;
+
+		switch (c) {
+		case 'h':
+			print_help_and_exit(argv[0]);
+			break;
+		}
+	}
+
+	if (optind < argc)
+		cpu = strtoul(argv[optind], NULL, 0);
+
+	if (cpu >= sysconf(_SC_NPROCESSORS_ONLN)) {
+		printf("CPU %d is invalid\n", cpu);
+		cpu = sysconf(_SC_NPROCESSORS_ONLN) - 1;
+		printf("   running on max CPU: %d\n", cpu);
+	}
+
+	rc = check_context_switch(cpu);
+
+	return rc;
+}
-- 
2.31.1


  parent reply	other threads:[~2022-01-27 17:55 UTC|newest]

Thread overview: 147+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-27 17:54 [PATCH V8 00/44] PKS/PMEM: Add Stray Write Protection ira.weiny
2022-01-27 17:54 ` [PATCH V8 01/44] entry: Create an internal irqentry_exit_cond_resched() call ira.weiny
2022-01-27 17:54 ` [PATCH V8 02/44] Documentation/protection-keys: Clean up documentation for User Space pkeys ira.weiny
2022-01-28 22:39   ` Dave Hansen
2022-02-01 23:49     ` Ira Weiny
2022-02-01 23:54       ` Dave Hansen
2022-01-27 17:54 ` [PATCH V8 03/44] x86/pkeys: Create pkeys_common.h ira.weiny
2022-01-28 22:43   ` Dave Hansen
2022-02-02  1:00     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 04/44] x86/pkeys: Add additional PKEY helper macros ira.weiny
2022-01-28 22:47   ` Dave Hansen
2022-02-02 20:21     ` Ira Weiny
2022-02-02 20:26       ` Dave Hansen
2022-02-02 20:28         ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 05/44] x86/fpu: Refactor arch_set_user_pkey_access() ira.weiny
2022-01-28 22:50   ` Dave Hansen
2022-02-02 20:22     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 06/44] mm/pkeys: Add Kconfig options for PKS ira.weiny
2022-01-28 22:54   ` Dave Hansen
2022-01-28 23:10     ` Ira Weiny
2022-01-28 23:51       ` Dave Hansen
2022-02-04 19:08         ` Ira Weiny
2022-02-09  5:34           ` Ira Weiny
2022-02-14 19:20             ` Dave Hansen
2022-02-14 23:03               ` Ira Weiny
2022-01-29  0:06   ` Dave Hansen
2022-02-04 19:14     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 07/44] x86/pkeys: Add PKS CPU feature bit ira.weiny
2022-01-28 23:05   ` Dave Hansen
2022-02-04 19:21     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 08/44] x86/fault: Adjust WARN_ON for PKey fault ira.weiny
2022-01-28 23:10   ` Dave Hansen
2022-02-04 20:06     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 09/44] x86/pkeys: Enable PKS on cpus which support it ira.weiny
2022-01-28 23:18   ` Dave Hansen
2022-01-28 23:41     ` Ira Weiny
2022-01-28 23:53       ` Dave Hansen
2022-01-27 17:54 ` [PATCH V8 10/44] Documentation/pkeys: Add initial PKS documentation ira.weiny
2022-01-28 23:57   ` Dave Hansen
2022-01-27 17:54 ` [PATCH V8 11/44] mm/pkeys: Define static PKS key array and default values ira.weiny
2022-01-29  0:02   ` Dave Hansen
2022-02-04 23:54     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 12/44] mm/pkeys: Define PKS page table macros ira.weiny
2022-01-27 17:54 ` [PATCH V8 13/44] mm/pkeys: Add initial PKS Test code ira.weiny
2022-01-31 19:30   ` Edgecombe, Rick P
2022-02-09 23:44     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 14/44] x86/pkeys: Introduce pks_write_pkrs() ira.weiny
2022-01-29  0:12   ` Dave Hansen
2022-01-29  0:16     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 15/44] x86/pkeys: Preserve the PKS MSR on context switch ira.weiny
2022-01-29  0:22   ` Dave Hansen
2022-02-11  6:10     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 16/44] mm/pkeys: Introduce pks_mk_readwrite() ira.weiny
2022-01-28 14:13   ` kernel test robot
2022-01-31 23:10   ` Edgecombe, Rick P
2022-02-18  2:22     ` Ira Weiny
2022-02-01 17:40   ` Dave Hansen
2022-02-18  4:39     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 17/44] mm/pkeys: Introduce pks_mk_noaccess() ira.weiny
2022-01-29  3:42   ` kernel test robot
2022-01-27 17:54 ` [PATCH V8 18/44] x86/fault: Add a PKS test fault hook ira.weiny
2022-01-31 19:56   ` Edgecombe, Rick P
2022-02-11 20:40     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 19/44] mm/pkeys: PKS Testing, add pks_mk_*() tests ira.weiny
2022-02-01 17:45   ` Dave Hansen
2022-02-18  5:34     ` Ira Weiny
2022-02-18 15:28       ` Dave Hansen
2022-02-18 17:25         ` Ira Weiny
2022-01-27 17:54 ` ira.weiny [this message]
2022-02-01 17:43   ` [PATCH V8 20/44] mm/pkeys: Add PKS test for context switching Edgecombe, Rick P
2022-02-22 21:42     ` Ira Weiny
2022-02-01 17:47   ` Edgecombe, Rick P
2022-02-01 19:52     ` Edgecombe, Rick P
2022-02-18  6:03       ` Ira Weiny
2022-02-18  6:02     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 21/44] x86/entry: Add auxiliary pt_regs space ira.weiny
2022-01-27 17:54 ` [PATCH V8 22/44] entry: Pass pt_regs to irqentry_exit_cond_resched() ira.weiny
2022-01-27 17:54 ` [PATCH V8 23/44] entry: Add architecture auxiliary pt_regs save/restore calls ira.weiny
2022-01-27 17:54 ` [PATCH V8 24/44] x86/entry: Define arch_{save|restore}_auxiliary_pt_regs() ira.weiny
2022-01-27 17:54 ` [PATCH V8 25/44] x86/pkeys: Preserve PKRS MSR across exceptions ira.weiny
2022-01-27 17:54 ` [PATCH V8 26/44] x86/fault: Print PKS MSR on fault ira.weiny
2022-02-01 18:13   ` Edgecombe, Rick P
2022-02-18  6:01     ` Ira Weiny
2022-02-18 17:28       ` Edgecombe, Rick P
2022-02-18 20:20         ` Dave Hansen
2022-02-18 20:54           ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 27/44] mm/pkeys: Add PKS exception test ira.weiny
2022-01-27 17:54 ` [PATCH V8 28/44] mm/pkeys: Introduce pks_update_exception() ira.weiny
2022-01-27 17:54 ` [PATCH V8 29/44] mm/pkeys: Introduce PKS fault callbacks ira.weiny
2022-01-27 17:54 ` [PATCH V8 30/44] mm/pkeys: Test setting a PKS key in a custom fault callback ira.weiny
2022-02-01  0:55   ` Edgecombe, Rick P
2022-03-01 15:39     ` Ira Weiny
2022-02-01 17:42   ` Edgecombe, Rick P
2022-02-11 20:44     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 31/44] mm/pkeys: Add pks_available() ira.weiny
2022-01-27 17:54 ` [PATCH V8 32/44] memremap_pages: Add Kconfig for DEVMAP_ACCESS_PROTECTION ira.weiny
2022-02-04 15:49   ` Dan Williams
2022-01-27 17:54 ` [PATCH V8 33/44] memremap_pages: Introduce pgmap_protection_available() ira.weiny
2022-02-04 16:19   ` Dan Williams
2022-02-28 16:59     ` Ira Weiny
2022-03-01 15:56       ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 34/44] memremap_pages: Introduce a PGMAP_PROTECTION flag ira.weiny
2022-01-27 17:54 ` [PATCH V8 35/44] memremap_pages: Introduce devmap_protected() ira.weiny
2022-01-27 17:54 ` [PATCH V8 36/44] memremap_pages: Reserve a PKS PKey for eventual use by PMEM ira.weiny
2022-02-01 18:35   ` Edgecombe, Rick P
2022-02-04 17:12     ` Dan Williams
2022-02-05  5:40       ` Ira Weiny
2022-02-05  8:19         ` Dan Williams
2022-02-06 18:14           ` Dan Williams
2022-02-08 22:48           ` Ira Weiny
2022-02-08 23:22             ` Dan Williams
2022-02-08 23:42               ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 37/44] memremap_pages: Set PKS PKey in PTEs if PGMAP_PROTECTIONS is requested ira.weiny
2022-02-04 17:41   ` Dan Williams
2022-03-01 18:15     ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 38/44] memremap_pages: Define pgmap_mk_{readwrite|noaccess}() calls ira.weiny
2022-02-04 18:35   ` Dan Williams
2022-02-05  0:09     ` Ira Weiny
2022-02-05  0:19       ` Dan Williams
2022-02-05  0:25         ` Dan Williams
2022-02-05  0:27           ` Dan Williams
2022-02-05  5:55             ` Ira Weiny
2022-02-05  6:28               ` Dan Williams
2022-02-22 22:05     ` Ira Weiny
2022-01-27 17:55 ` [PATCH V8 39/44] memremap_pages: Add memremap.pks_fault_mode ira.weiny
2022-02-01  1:16   ` Edgecombe, Rick P
2022-03-02  0:20     ` Ira Weiny
2022-02-04 19:01   ` Dan Williams
2022-03-02  2:00     ` Ira Weiny
2022-01-27 17:55 ` [PATCH V8 40/44] memremap_pages: Add pgmap_protection_flag_invalid() ira.weiny
2022-02-01  1:37   ` Edgecombe, Rick P
2022-03-02  2:01     ` Ira Weiny
2022-02-04 19:18   ` Dan Williams
2022-01-27 17:55 ` [PATCH V8 41/44] kmap: Ensure kmap works for devmap pages ira.weiny
2022-02-04 21:07   ` Dan Williams
2022-03-01 19:45     ` Ira Weiny
2022-03-01 19:50       ` Ira Weiny
2022-03-01 20:05       ` Dan Williams
2022-03-01 23:03         ` Ira Weiny
2022-01-27 17:55 ` [PATCH V8 42/44] dax: Stray access protection for dax_direct_access() ira.weiny
2022-02-04  5:19   ` Dan Williams
2022-03-01 18:13     ` Ira Weiny
2022-01-27 17:55 ` [PATCH V8 43/44] nvdimm/pmem: Enable stray access protection ira.weiny
2022-02-04 21:10   ` Dan Williams
2022-03-01 18:18     ` Ira Weiny
2022-01-27 17:55 ` [PATCH V8 44/44] devdax: " ira.weiny
2022-02-04 21:12   ` Dan Williams

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=20220127175505.851391-21-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=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.