Skip to contentSkip to conversation

s390x: test storage keys during migration

PATCH Superseded Nico Boehr 4 participants 20 messages Dec 1, 2022 – Dec 9, 2022 KVM

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Nico Boehr

author
#0 lore raw

cover s390x: test storage keys during migration

Add a test which changes storage keys while VM is being migrated.

This series bases on Claudio's new PSW macros ("[PATCH v3 0/2] lib:
s390x: add PSW and PSW_WITH_CUR_MASK macros").

Nico Boehr (3):
s390x: add library for skey-related functions
lib: s390x: skey: add seed value for storage keys
s390x: add storage key test during migration

 lib/s390x/skey.c              |  94 +++++++++++++++++++++++++++++++
 lib/s390x/skey.h              |  42 ++++++++++++++
 s390x/Makefile                |   2 +
 s390x/migration-during-skey.c | 103 ++++++++++++++++++++++++++++++++++
 s390x/migration-skey.c        |  44 ++-------------
 s390x/unittests.cfg           |   5 ++
 6 files changed, 252 insertions(+), 38 deletions(-)
 create mode 100644 lib/s390x/skey.c
 create mode 100644 lib/s390x/skey.h
 create mode 100644 s390x/migration-during-skey.c
Signature · 1 line
2.36.1
author
#1 lore raw

PATCH 1/3 s390x: add library for skey-related functions

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

---
 lib/s390x/skey.c       | 92 ++++++++++++++++++++++++++++++++++++++++++
 lib/s390x/skey.h       | 32 +++++++++++++++
 s390x/Makefile         |  1 +
 s390x/migration-skey.c | 44 +++-----------------
 4 files changed, 131 insertions(+), 38 deletions(-)
 create mode 100644 lib/s390x/skey.c
 create mode 100644 lib/s390x/skey.h
lib/s390x/skey.c new file +92 -0
new file mode 100644
index 000000000000..100f0949a244
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Storage key migration test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+
+#include <libcflat.h>
+#include <asm/facility.h>
+#include <asm/mem.h>
+#include <skey.h>
+
+/*
+ * Set storage keys on pagebuf.
+ * pagebuf must point to page_count consecutive pages.
+ */
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+	unsigned char key_to_set;
+	unsigned long i;
+
+	for (i = 0; i < page_count; i++) {
+		/*
+		 * Storage keys are 7 bit, lowest bit is always returned as zero
+		 * by iske.
+		 * This loop will set all 7 bits which means we set fetch
+		 * protection as well as reference and change indication for
+		 * some keys.
+		 */
+		key_to_set = i * 2;
+		set_storage_key(pagebuf + i * PAGE_SIZE, key_to_set, 1);
+	}
+}
+
+/*
+ * Verify storage keys on pagebuf.
+ * Storage keys must have been set by skey_set_keys on pagebuf before.
+ *
+ * If storage keys match the expected result, will return a skey_verify_result
+ * with verify_failed false. All other fields are then invalid.
+ * If there is a mismatch, returned struct will have verify_failed true and will
+ * be filled with the details on the first mismatch encountered.
+ */
+struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+	union skey expected_key, actual_key;
+	struct skey_verify_result result = {
+		.verify_failed = true
+	};
+	uint8_t *cur_page;
+	unsigned long i;
+
+	for (i = 0; i < page_count; i++) {
+		cur_page = pagebuf + i * PAGE_SIZE;
+		actual_key.val = get_storage_key(cur_page);
+		expected_key.val = i * 2;
+
+		/*
+		 * The PoP neither gives a guarantee that the reference bit is
+		 * accurate nor that it won't be cleared by hardware. Hence we
+		 * don't rely on it and just clear the bits to avoid compare
+		 * errors.
+		 */
+		actual_key.str.rf = 0;
+		expected_key.str.rf = 0;
+
+		if (actual_key.val != expected_key.val) {
+			result.expected_key.val = expected_key.val;
+			result.actual_key.val = actual_key.val;
+			result.page_mismatch_idx = i;
+			result.page_mismatch_addr = (unsigned long)cur_page;
+			return result;
+		}
+	}
+
+	result.verify_failed = false;
+	return result;
+}
+
+void skey_report_verify(struct skey_verify_result * const result)
+{
+	if (result->verify_failed)
+		report_fail("page skey mismatch: first page idx = %lu, addr = 0x%lx, "
+			"expected_key = 0x%x, actual_key = 0x%x",
+			result->page_mismatch_idx, result->page_mismatch_addr,
+			result->expected_key.val, result->actual_key.val);
+	else
+		report_pass("skeys match");
+}
lib/s390x/skey.h new file +32 -0
new file mode 100644
index 000000000000..a0f8caa1270b
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Storage key migration test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+#ifndef S390X_SKEY_H
+#define S390X_SKEY_H
+
+#include <libcflat.h>
+#include <asm/facility.h>
+#include <asm/page.h>
+#include <asm/mem.h>
+
+struct skey_verify_result {
+	bool verify_failed;
+	union skey expected_key;
+	union skey actual_key;
+	unsigned long page_mismatch_idx;
+	unsigned long page_mismatch_addr;
+};
+
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count);
+
+struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count);
+
+void skey_report_verify(struct skey_verify_result * const result);
+
+#endif /* S390X_SKEY_H */
s390x/Makefile +1 -0
index bf1504f9d58c..d097b7071dfb 100644
@@ -99,6 +99,7 @@ cflatobjs += lib/s390x/malloc_io.o
 cflatobjs += lib/s390x/uv.o
 cflatobjs += lib/s390x/sie.o
 cflatobjs += lib/s390x/fault.o
+cflatobjs += lib/s390x/skey.o
 
 OBJDIRS += lib/s390x
 
s390x/migration-skey.c +6 -38

61 lines not on this page · raw message

Signature · 1 line
2.36.1
#2 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoted 5 lines · On Thu, 1 Dec 2022 09:46:40 +0100 Nico Boehr <[email protected]> wrote:

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

a few nits, otherwise looks pretty straightforward

Quoted 22 lines
---
 lib/s390x/skey.c       | 92 ++++++++++++++++++++++++++++++++++++++++++
 lib/s390x/skey.h       | 32 +++++++++++++++
 s390x/Makefile         |  1 +
 s390x/migration-skey.c | 44 +++-----------------
 4 files changed, 131 insertions(+), 38 deletions(-)
 create mode 100644 lib/s390x/skey.c
 create mode 100644 lib/s390x/skey.h
lib/s390x/skey.c new file +7 -0
new file mode 100644
index 000000000000..100f0949a244
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Storage key migration test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:

lib/s390x/skey.c line 17

Quoted 10 lines
lib/s390x/skey.c new file +10 -0
+ *  Nico Boehr <[email protected]>
+ */
+
+#include <libcflat.h>
+#include <asm/facility.h>
+#include <asm/mem.h>
+#include <skey.h>
+
+/*
+ * Set storage keys on pagebuf.

surely you should explain better what the function does (e.g. how are you setting the keys and why)

lib/s390x/skey.c line 20

Quoted 3 lines
lib/s390x/skey.c new file +3 -0
+ * pagebuf must point to page_count consecutive pages.
+ */
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)

this name does not make clear what the function is doing. at first one would think that it sets the same key for all pages.

maybe something like set_storage_keys_test_pattern or skey_set_test_pattern or something like that

Quoted 4 lines
lib/s390x/skey.c new file +4 -0
+{
+	unsigned char key_to_set;
+	unsigned long i;
+

lib/s390x/skey.c line 34

Quoted 10 lines
lib/s390x/skey.c new file +10 -0
+	for (i = 0; i < page_count; i++) {
+		/*
+		 * Storage keys are 7 bit, lowest bit is always returned as zero
+		 * by iske.
+		 * This loop will set all 7 bits which means we set fetch
+		 * protection as well as reference and change indication for
+		 * some keys.
+		 */
+		key_to_set = i * 2;
+		set_storage_key(pagebuf + i * PAGE_SIZE, key_to_set, 1);

why not just i * 2 instead of using key_to_set ?

lib/s390x/skey.c line 47

Quoted 13 lines
lib/s390x/skey.c new file +13 -0
+	}
+}
+
+/*
+ * Verify storage keys on pagebuf.
+ * Storage keys must have been set by skey_set_keys on pagebuf before.
+ *
+ * If storage keys match the expected result, will return a skey_verify_result
+ * with verify_failed false. All other fields are then invalid.
+ * If there is a mismatch, returned struct will have verify_failed true and will
+ * be filled with the details on the first mismatch encountered.
+ */
+struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count)

and here then adjust the function name accordingly

Quoted 118 lines
lib/s390x/skey.c new file +45 -0
+{
+	union skey expected_key, actual_key;
+	struct skey_verify_result result = {
+		.verify_failed = true
+	};
+	uint8_t *cur_page;
+	unsigned long i;
+
+	for (i = 0; i < page_count; i++) {
+		cur_page = pagebuf + i * PAGE_SIZE;
+		actual_key.val = get_storage_key(cur_page);
+		expected_key.val = i * 2;
+
+		/*
+		 * The PoP neither gives a guarantee that the reference bit is
+		 * accurate nor that it won't be cleared by hardware. Hence we
+		 * don't rely on it and just clear the bits to avoid compare
+		 * errors.
+		 */
+		actual_key.str.rf = 0;
+		expected_key.str.rf = 0;
+
+		if (actual_key.val != expected_key.val) {
+			result.expected_key.val = expected_key.val;
+			result.actual_key.val = actual_key.val;
+			result.page_mismatch_idx = i;
+			result.page_mismatch_addr = (unsigned long)cur_page;
+			return result;
+		}
+	}
+
+	result.verify_failed = false;
+	return result;
+}
+
+void skey_report_verify(struct skey_verify_result * const result)
+{
+	if (result->verify_failed)
+		report_fail("page skey mismatch: first page idx = %lu, addr = 0x%lx, "
+			"expected_key = 0x%x, actual_key = 0x%x",
+			result->page_mismatch_idx, result->page_mismatch_addr,
+			result->expected_key.val, result->actual_key.val);
+	else
+		report_pass("skeys match");
+}
lib/s390x/skey.h new file +32 -0
new file mode 100644
index 000000000000..a0f8caa1270b
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Storage key migration test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+#ifndef S390X_SKEY_H
+#define S390X_SKEY_H
+
+#include <libcflat.h>
+#include <asm/facility.h>
+#include <asm/page.h>
+#include <asm/mem.h>
+
+struct skey_verify_result {
+	bool verify_failed;
+	union skey expected_key;
+	union skey actual_key;
+	unsigned long page_mismatch_idx;
+	unsigned long page_mismatch_addr;
+};
+
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count);
+
+struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count);
+
+void skey_report_verify(struct skey_verify_result * const result);
+
+#endif /* S390X_SKEY_H */
s390x/Makefile +1 -0
index bf1504f9d58c..d097b7071dfb 100644
@@ -99,6 +99,7 @@ cflatobjs += lib/s390x/malloc_io.o
 cflatobjs += lib/s390x/uv.o
 cflatobjs += lib/s390x/sie.o
 cflatobjs += lib/s390x/fault.o
+cflatobjs += lib/s390x/skey.o
 
 OBJDIRS += lib/s390x
 
s390x/migration-skey.c +3 -6
index b7bd82581abe..fed6fc1ed0f8 100644
@@ -10,55 +10,23 @@
 
 #include <libcflat.h>
 #include <asm/facility.h>
-#include <asm/page.h>
-#include <asm/mem.h>
-#include <asm/interrupt.h>
 #include <hardware.h>
+#include <skey.h>
 
 #define NUM_PAGES 128
-static uint8_t pagebuf[NUM_PAGES][PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
+static uint8_t pagebuf[NUM_PAGES * PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
 
 static void test_migration(void)
 {
-	union skey expected_key, actual_key;
-	int i, key_to_set, key_mismatches = 0;
+	struct skey_verify_result result;

s390x/migration-skey.c line 34

Quoted 10 lines
s390x/migration-skey.c +0 -9
 
-	for (i = 0; i < NUM_PAGES; i++) {
-		/*
-		 * Storage keys are 7 bit, lowest bit is always returned as zero
-		 * by iske.
-		 * This loop will set all 7 bits which means we set fetch
-		 * protection as well as reference and change indication for
-		 * some keys.
-		 */
-		key_to_set = i * 2;

ah I see, you have simply moved this code :)

Quoted 33 lines
s390x/migration-skey.c +3 -23
-		set_storage_key(pagebuf[i], key_to_set, 1);
-	}
+	skey_set_keys(pagebuf, NUM_PAGES);
 
 	puts("Please migrate me, then press return\n");
 	(void)getchar();
 
-	for (i = 0; i < NUM_PAGES; i++) {
-		actual_key.val = get_storage_key(pagebuf[i]);
-		expected_key.val = i * 2;
-
-		/*
-		 * The PoP neither gives a guarantee that the reference bit is
-		 * accurate nor that it won't be cleared by hardware. Hence we
-		 * don't rely on it and just clear the bits to avoid compare
-		 * errors.
-		 */
-		actual_key.str.rf = 0;
-		expected_key.str.rf = 0;
-
-		/* don't log anything when key matches to avoid spamming the log */
-		if (actual_key.val != expected_key.val) {
-			key_mismatches++;
-			report_fail("page %d expected_key=0x%x actual_key=0x%x", i, expected_key.val, actual_key.val);
-		}
-	}
-
-	report(!key_mismatches, "skeys after migration match");
+	result = skey_verify_keys(pagebuf, NUM_PAGES);
+	skey_report_verify(&result);
 }
 
 int main(void)
author
#3 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoting Claudio Imbrenda (2022-12-01 14:16:50)

Quoted 2 lines

On Thu, 1 Dec 2022 09:46:40 +0100
Nico Boehr <[email protected]> wrote:

[...]

Quoted 3 lines
Quoted 3 lines
lib/s390x/skey.c new file +0 -0
new file mode 100644
index 000000000000..100f0949a244

[...]

Quoted 5 lines
Quoted 2 lines

+/*
+ * Set storage keys on pagebuf.

surely you should explain better what the function does (e.g. how are you setting the keys and why)

Well there is the comment below which explains why the * 2 is needed, so what about this paragraph (after merging the commits as discussed before):

* Each page's storage key is generated by taking the page's index in pagebuf,
* XOR-ing that with the given seed and then multipling the result with two.

(But really that's also easy to see from the code below, so I am not sure if this really adds value.)

Quoted 9 lines
Quoted 3 lines

+ * pagebuf must point to page_count consecutive pages.
+ */
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)

this name does not make clear what the function is doing. at first one would think that it sets the same key for all pages.

maybe something like set_storage_keys_test_pattern or skey_set_test_pattern or something like that

Oh that's a nice suggestion, thanks.

Quoted 17 lines
Quoted 5 lines
lib/s390x/skey.c new file +4 -0
+{
+     unsigned char key_to_set;
+     unsigned long i;
+

lib/s390x/skey.c line 34

Quoted 10 lines
lib/s390x/skey.c new file +10 -0
+     for (i = 0; i < page_count; i++) {
+             /*
+              * Storage keys are 7 bit, lowest bit is always returned as zero
+              * by iske.
+              * This loop will set all 7 bits which means we set fetch
+              * protection as well as reference and change indication for
+              * some keys.
+              */
+             key_to_set = i * 2;
+             set_storage_key(pagebuf + i * PAGE_SIZE, key_to_set, 1);

why not just i * 2 instead of using key_to_set ?

Well you answered that yourself :)

In patch 2, the key_to_set expression becomes a bit more complex, so the extra variable makes sense to me.

#4 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoted 10 lines · On Thu, 01 Dec 2022 16:55:42 +0100 Nico Boehr <[email protected]> wrote:

Quoting Claudio Imbrenda (2022-12-01 14:16:50)

Quoted 2 lines

On Thu, 1 Dec 2022 09:46:40 +0100
Nico Boehr <[email protected]> wrote:

[...]

Quoted 3 lines
Quoted 3 lines
lib/s390x/skey.c new file +0 -0
new file mode 100644
index 000000000000..100f0949a244  

[...]

Quoted 2 lines
Quoted 2 lines

+/*
+ * Set storage keys on pagebuf.

... according to a pattern

Quoted 9 lines
Quoted 3 lines

surely you should explain better what the function does (e.g. how are you setting the keys and why)

Well there is the comment below which explains why the * 2 is needed, so what about this paragraph (after merging the commits as discussed before):

* Each page's storage key is generated by taking the page's index in pagebuf,
* XOR-ing that with the given seed and then multipling the result with two.

looks good

Quoted 3 lines

(But really that's also easy to see from the code below, so I am not sure if this really adds value.)

if you want to add documentation, do it properly, otherwise there is no point in having documentation at all :)

Quoted 31 lines
Quoted 10 lines
Quoted 4 lines

+ * pagebuf must point to page_count consecutive pages.
+ */
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)

this name does not make clear what the function is doing. at first one would think that it sets the same key for all pages.

maybe something like set_storage_keys_test_pattern or skey_set_test_pattern or something like that

Oh that's a nice suggestion, thanks.

Quoted 17 lines
Quoted 5 lines
lib/s390x/skey.c new file +4 -0
+{
+     unsigned char key_to_set;
+     unsigned long i;
+

lib/s390x/skey.c line 34

Quoted 10 lines
lib/s390x/skey.c new file +10 -0
+     for (i = 0; i < page_count; i++) {
+             /*
+              * Storage keys are 7 bit, lowest bit is always returned as zero
+              * by iske.
+              * This loop will set all 7 bits which means we set fetch
+              * protection as well as reference and change indication for
+              * some keys.
+              */
+             key_to_set = i * 2;
+             set_storage_key(pagebuf + i * PAGE_SIZE, key_to_set, 1);  

why not just i * 2 instead of using key_to_set ?

Quoted 4 lines

Well you answered that yourself :)

In patch 2, the key_to_set expression becomes a bit more complex, so the extra variable makes sense to me.

fair enough

#5 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

In reply to Nico Boehr
Quoted 4 lines · Nico Boehr

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

NACK

We're not putting test specific code into the library.

Quoted 224 lines
---
  lib/s390x/skey.c       | 92 ++++++++++++++++++++++++++++++++++++++++++
  lib/s390x/skey.h       | 32 +++++++++++++++
  s390x/Makefile         |  1 +
  s390x/migration-skey.c | 44 +++-----------------
  4 files changed, 131 insertions(+), 38 deletions(-)

create mode 100644 lib/s390x/skey.c
create mode 100644 lib/s390x/skey.h

lib/s390x/skey.c new file +92 -0
new file mode 100644
index 000000000000..100f0949a244
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Storage key migration test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+
+#include <libcflat.h>
+#include <asm/facility.h>
+#include <asm/mem.h>
+#include <skey.h>
+
+/*
+ * Set storage keys on pagebuf.
+ * pagebuf must point to page_count consecutive pages.
+ */
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+	unsigned char key_to_set;
+	unsigned long i;
+
+	for (i = 0; i < page_count; i++) {
+		/*
+		 * Storage keys are 7 bit, lowest bit is always returned as zero
+		 * by iske.
+		 * This loop will set all 7 bits which means we set fetch
+		 * protection as well as reference and change indication for
+		 * some keys.
+		 */
+		key_to_set = i * 2;
+		set_storage_key(pagebuf + i * PAGE_SIZE, key_to_set, 1);
+	}
+}
+
+/*
+ * Verify storage keys on pagebuf.
+ * Storage keys must have been set by skey_set_keys on pagebuf before.
+ *
+ * If storage keys match the expected result, will return a skey_verify_result
+ * with verify_failed false. All other fields are then invalid.
+ * If there is a mismatch, returned struct will have verify_failed true and will
+ * be filled with the details on the first mismatch encountered.
+ */
+struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+	union skey expected_key, actual_key;
+	struct skey_verify_result result = {
+		.verify_failed = true
+	};
+	uint8_t *cur_page;
+	unsigned long i;
+
+	for (i = 0; i < page_count; i++) {
+		cur_page = pagebuf + i * PAGE_SIZE;
+		actual_key.val = get_storage_key(cur_page);
+		expected_key.val = i * 2;
+
+		/*
+		 * The PoP neither gives a guarantee that the reference bit is
+		 * accurate nor that it won't be cleared by hardware. Hence we
+		 * don't rely on it and just clear the bits to avoid compare
+		 * errors.
+		 */
+		actual_key.str.rf = 0;
+		expected_key.str.rf = 0;
+
+		if (actual_key.val != expected_key.val) {
+			result.expected_key.val = expected_key.val;
+			result.actual_key.val = actual_key.val;
+			result.page_mismatch_idx = i;
+			result.page_mismatch_addr = (unsigned long)cur_page;
+			return result;
+		}
+	}
+
+	result.verify_failed = false;
+	return result;
+}
+
+void skey_report_verify(struct skey_verify_result * const result)
+{
+	if (result->verify_failed)
+		report_fail("page skey mismatch: first page idx = %lu, addr = 0x%lx, "
+			"expected_key = 0x%x, actual_key = 0x%x",
+			result->page_mismatch_idx, result->page_mismatch_addr,
+			result->expected_key.val, result->actual_key.val);
+	else
+		report_pass("skeys match");
+}
lib/s390x/skey.h new file +32 -0
new file mode 100644
index 000000000000..a0f8caa1270b
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Storage key migration test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+#ifndef S390X_SKEY_H
+#define S390X_SKEY_H
+
+#include <libcflat.h>
+#include <asm/facility.h>
+#include <asm/page.h>
+#include <asm/mem.h>
+
+struct skey_verify_result {
+	bool verify_failed;
+	union skey expected_key;
+	union skey actual_key;
+	unsigned long page_mismatch_idx;
+	unsigned long page_mismatch_addr;
+};
+
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count);
+
+struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count);
+
+void skey_report_verify(struct skey_verify_result * const result);
+
+#endif /* S390X_SKEY_H */
s390x/Makefile +1 -0
index bf1504f9d58c..d097b7071dfb 100644
@@ -99,6 +99,7 @@ cflatobjs += lib/s390x/malloc_io.o
  cflatobjs += lib/s390x/uv.o
  cflatobjs += lib/s390x/sie.o
  cflatobjs += lib/s390x/fault.o
+cflatobjs += lib/s390x/skey.o
 
  OBJDIRS += lib/s390x
 
s390x/migration-skey.c +6 -38
index b7bd82581abe..fed6fc1ed0f8 100644
@@ -10,55 +10,23 @@
 
  #include <libcflat.h>
  #include <asm/facility.h>
-#include <asm/page.h>
-#include <asm/mem.h>
-#include <asm/interrupt.h>
  #include <hardware.h>
+#include <skey.h>
 
  #define NUM_PAGES 128
-static uint8_t pagebuf[NUM_PAGES][PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
+static uint8_t pagebuf[NUM_PAGES * PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
 
  static void test_migration(void)
  {
-	union skey expected_key, actual_key;
-	int i, key_to_set, key_mismatches = 0;
+	struct skey_verify_result result;
 
-	for (i = 0; i < NUM_PAGES; i++) {
-		/*
-		 * Storage keys are 7 bit, lowest bit is always returned as zero
-		 * by iske.
-		 * This loop will set all 7 bits which means we set fetch
-		 * protection as well as reference and change indication for
-		 * some keys.
-		 */
-		key_to_set = i * 2;
-		set_storage_key(pagebuf[i], key_to_set, 1);
-	}
+	skey_set_keys(pagebuf, NUM_PAGES);
 
  	puts("Please migrate me, then press return\n");
  	(void)getchar();
 
-	for (i = 0; i < NUM_PAGES; i++) {
-		actual_key.val = get_storage_key(pagebuf[i]);
-		expected_key.val = i * 2;
-
-		/*
-		 * The PoP neither gives a guarantee that the reference bit is
-		 * accurate nor that it won't be cleared by hardware. Hence we
-		 * don't rely on it and just clear the bits to avoid compare
-		 * errors.
-		 */
-		actual_key.str.rf = 0;
-		expected_key.str.rf = 0;
-
-		/* don't log anything when key matches to avoid spamming the log */
-		if (actual_key.val != expected_key.val) {
-			key_mismatches++;
-			report_fail("page %d expected_key=0x%x actual_key=0x%x", i, expected_key.val, actual_key.val);
-		}
-	}
-
-	report(!key_mismatches, "skeys after migration match");
+	result = skey_verify_keys(pagebuf, NUM_PAGES);
+	skey_report_verify(&result);
  }
 
  int main(void)
#6 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoted 9 lines · Janosch Frank
Quoted 4 lines · Nico Boehr

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

NACK

We're not putting test specific code into the library.

Do we need a new file (in the third patch) for the new test at all, or could the new test simply be added to s390x/migration-skey.c instead?

Thomas

Quoted 224 lines
Quoted 223 lines

---
  lib/s390x/skey.c       | 92 ++++++++++++++++++++++++++++++++++++++++++ lib/s390x/skey.h       | 32 +++++++++++++++
  s390x/Makefile         |  1 +
  s390x/migration-skey.c | 44 +++-----------------

  4 files changed, 131 insertions(+), 38 deletions(-)

  create mode 100644 lib/s390x/skey.c
  create mode 100644 lib/s390x/skey.h

lib/s390x/skey.c new file +42 -0
new file mode 100644
index 000000000000..100f0949a244
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Storage key migration test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+
+#include <libcflat.h>
+#include <asm/facility.h>
+#include <asm/mem.h>
+#include <skey.h>
+
+/*
+ * Set storage keys on pagebuf.
+ * pagebuf must point to page_count consecutive pages.
+ */
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+    unsigned char key_to_set;
+    unsigned long i;
+
+    for (i = 0; i < page_count; i++) {
+        /*
+         * Storage keys are 7 bit, lowest bit is always returned as zero
+         * by iske.
+         * This loop will set all 7 bits which means we set fetch
+         * protection as well as reference and change indication for
+         * some keys.
+         */
+        key_to_set = i * 2;
+        set_storage_key(pagebuf + i * PAGE_SIZE, key_to_set, 1);
+    }
+}
+
+/*
+ * Verify storage keys on pagebuf.
+ * Storage keys must have been set by skey_set_keys on pagebuf before.
+ *
+ * If storage keys match the expected result, will return a 

skey_verify_result
+ * with verify_failed false. All other fields are then invalid.
+ * If there is a mismatch, returned struct will have verify_failed true and will
+ * be filled with the details on the first mismatch encountered.
+ */
+struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count)

lib/s390x/skey.c new file +39 -0
+{
+    union skey expected_key, actual_key;
+    struct skey_verify_result result = {
+        .verify_failed = true
+    };
+    uint8_t *cur_page;
+    unsigned long i;
+
+    for (i = 0; i < page_count; i++) {
+        cur_page = pagebuf + i * PAGE_SIZE;
+        actual_key.val = get_storage_key(cur_page);
+        expected_key.val = i * 2;
+
+        /*
+         * The PoP neither gives a guarantee that the reference bit is
+         * accurate nor that it won't be cleared by hardware. Hence we
+         * don't rely on it and just clear the bits to avoid compare
+         * errors.
+         */
+        actual_key.str.rf = 0;
+        expected_key.str.rf = 0;
+
+        if (actual_key.val != expected_key.val) {
+            result.expected_key.val = expected_key.val;
+            result.actual_key.val = actual_key.val;
+            result.page_mismatch_idx = i;
+            result.page_mismatch_addr = (unsigned long)cur_page;
+            return result;
+        }
+    }
+
+    result.verify_failed = false;
+    return result;
+}
+
+void skey_report_verify(struct skey_verify_result * const result)
+{
+    if (result->verify_failed)
+        report_fail("page skey mismatch: first page idx = %lu, addr = 

0x%lx, "
+            "expected_key = 0x%x, actual_key = 0x%x",
+            result->page_mismatch_idx, result->page_mismatch_addr, +            result->expected_key.val, result->actual_key.val); +    else
+        report_pass("skeys match");

lib/s390x/skey.c new file +1 -0
+}
lib/s390x/skey.h new file +28 -0
new file mode 100644
index 000000000000..a0f8caa1270b
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Storage key migration test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+#ifndef S390X_SKEY_H
+#define S390X_SKEY_H
+
+#include <libcflat.h>
+#include <asm/facility.h>
+#include <asm/page.h>
+#include <asm/mem.h>
+
+struct skey_verify_result {
+    bool verify_failed;
+    union skey expected_key;
+    union skey actual_key;
+    unsigned long page_mismatch_idx;
+    unsigned long page_mismatch_addr;
+};
+
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count);
+
+struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned 

long page_count);

lib/s390x/skey.h new file +4 -0
+
+void skey_report_verify(struct skey_verify_result * const result);
+
+#endif /* S390X_SKEY_H */
s390x/Makefile +0 -0
index bf1504f9d58c..d097b7071dfb 100644
@@ -99,6 +99,7 @@ cflatobjs += lib/s390x/malloc_io.o

  cflatobjs += lib/s390x/uv.o
  cflatobjs += lib/s390x/sie.o
  cflatobjs += lib/s390x/fault.o
+cflatobjs += lib/s390x/skey.o
  OBJDIRS += lib/s390x

s390x/migration-skey.c +6 -38
index b7bd82581abe..fed6fc1ed0f8 100644
@@ -10,55 +10,23 @@
   #include <libcflat.h>
   #include <asm/facility.h>
-#include <asm/page.h>
-#include <asm/mem.h>
-#include <asm/interrupt.h>
   #include <hardware.h>
+#include <skey.h>
   #define NUM_PAGES 128
-static uint8_t pagebuf[NUM_PAGES][PAGE_SIZE] 
 __attribute__((aligned(PAGE_SIZE)));
+static uint8_t pagebuf[NUM_PAGES * PAGE_SIZE] 
 __attribute__((aligned(PAGE_SIZE)));
   static void test_migration(void)
   {
-    union skey expected_key, actual_key;
-    int i, key_to_set, key_mismatches = 0;
+    struct skey_verify_result result;
-    for (i = 0; i < NUM_PAGES; i++) {
-        /*
-         * Storage keys are 7 bit, lowest bit is always returned as zero
-         * by iske.
-         * This loop will set all 7 bits which means we set fetch
-         * protection as well as reference and change indication for
-         * some keys.
-         */
-        key_to_set = i * 2;
-        set_storage_key(pagebuf[i], key_to_set, 1);
-    }
+    skey_set_keys(pagebuf, NUM_PAGES);
       puts("Please migrate me, then press return\n");
       (void)getchar();
-    for (i = 0; i < NUM_PAGES; i++) {
-        actual_key.val = get_storage_key(pagebuf[i]);
-        expected_key.val = i * 2;
-
-        /*
-         * The PoP neither gives a guarantee that the reference bit is
-         * accurate nor that it won't be cleared by hardware. Hence we
-         * don't rely on it and just clear the bits to avoid compare
-         * errors.
-         */
-        actual_key.str.rf = 0;
-        expected_key.str.rf = 0;
-
-        /* don't log anything when key matches to avoid spamming the log */
-        if (actual_key.val != expected_key.val) {
-            key_mismatches++;
-            report_fail("page %d expected_key=0x%x actual_key=0x%x", i, 
 expected_key.val, actual_key.val);
-        }
-    }
-
-    report(!key_mismatches, "skeys after migration match");
+    result = skey_verify_keys(pagebuf, NUM_PAGES);
+    skey_report_verify(&result);
   }
   int main(void)
author
#7 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoting Thomas Huth (2022-12-02 10:09:03)

Quoted 7 lines · Janosch Frank
Quoted 6 lines · Nico Boehr
Quoted 4 lines

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

Quoted 6 lines
Quoted 3 lines

NACK

We're not putting test specific code into the library.

Do we need a new file (in the third patch) for the new test at all, or could the new test simply be added to s390x/migration-skey.c instead?

Mh, not quite. One test wants to change storage keys *before* migrating, the other *while* migrating. Since we can only migrate once, it is not obvious to me how we could do that in one run.

Speaking of one run, what we could do is add a command line argument which decides which test to run and then call the same test with different arguments in unittests.cfg.

Other options I can think of (I don't like any of them):
- copy the code (probably the worst solution)
- header file in s390x which is included by both tests (better, but still bad, means double compilation of the test functions)

#8 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoted 10 lines · Nico Boehr

Quoting Thomas Huth (2022-12-02 10:09:03)

Quoted 9 lines · Janosch Frank
Quoted 8 lines · Nico Boehr
Quoted 4 lines

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

NACK

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

We're not putting test specific code into the library.

Do we need a new file (in the third patch) for the new test at all, or could the new test simply be added to s390x/migration-skey.c instead?

Mh, not quite. One test wants to change storage keys *before* migrating, the other *while* migrating. Since we can only migrate once, it is not obvious to me how we could do that in one run.

Speaking of one run, what we could do is add a command line argument which decides which test to run and then call the same test with different arguments in unittests.cfg.

Yes, that's what I had in mind - use a command line argument to select the test ... should be OK as long as both variants are listed in unittests.cfg, shouldn't it?

Thomas

#9 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoted 18 lines · Thomas Huth
Quoted 17 lines · Nico Boehr

Quoting Thomas Huth (2022-12-02 10:09:03)

Quoted 7 lines · Janosch Frank
Quoted 6 lines · Nico Boehr
Quoted 4 lines

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

Quoted 6 lines
Quoted 3 lines

NACK

We're not putting test specific code into the library.

Do we need a new file (in the third patch) for the new test at all, or could the new test simply be added to s390x/migration-skey.c instead?

Mh, not quite. One test wants to change storage keys *before* migrating, the other *while* migrating. Since we can only migrate once, it is not obvious to me how we could do that in one run.

Quoted 8 lines
Quoted 1 line

Speaking of one run, what we could do is add a command line argument which decides which test to run and then call the same test with different arguments in unittests.cfg.

Yes, that's what I had in mind - use a command line argument to select the test ... should be OK as long as both variants are listed in unittests.cfg, shouldn't it?

Thomas

@Thomas @Claudio:
I see two possible solutions if we want a "testlib" at some point (which for the record I don't have anything against):

Putting the files into lib/s390x/testlib/* which will then be part of our normal lib.
That's a minimal effort solution. It still puts those files into lib/* but they are at least contained in a directory.

Putting the files into s390x/testlib/* and creating a proper new lib. Which means we'd need a few more lines of makefile changes.

None of that is a huge amount of work.

#10 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoted 27 lines · Janosch Frank
Quoted 18 lines · Thomas Huth
Quoted 17 lines · Nico Boehr

Quoting Thomas Huth (2022-12-02 10:09:03)

Quoted 7 lines · Janosch Frank
Quoted 6 lines · Nico Boehr
Quoted 4 lines

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

Quoted 6 lines
Quoted 3 lines

NACK

We're not putting test specific code into the library.

Do we need a new file (in the third patch) for the new test at all, or could the new test simply be added to s390x/migration-skey.c instead?

Mh, not quite. One test wants to change storage keys *before* migrating, the other *while* migrating. Since we can only migrate once, it is not obvious to me how we could do that in one run.

Quoted 7 lines
Quoted 1 line

Speaking of one run, what we could do is add a command line argument which decides which test to run and then call the same test with different arguments in unittests.cfg.

Yes, that's what I had in mind - use a command line argument to select the test ... should be OK as long as both variants are listed in unittests.cfg, shouldn't it?

   Thomas

Quoted 8 lines

@Thomas @Claudio:
I see two possible solutions if we want a "testlib" at some point (which for the record I don't have anything against):

Putting the files into lib/s390x/testlib/* which will then be part of our normal lib.
That's a minimal effort solution. It still puts those files into lib/* but they are at least contained in a directory.

Putting the files into s390x/testlib/* and creating a proper new lib. Which means we'd need a few more lines of makefile changes.

Though this is an excellent topic for a Friday afternoon bikeshedding ... I don't mind much either way. I maybe just got a small preference to not touch the main lib/ folder here. I guess you could even call it s390x/migration-skey-common.c and leave the lib logic out of the game ... but I don't really mind. Up to you to decide ;-)

Thomas

#11 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoted 42 lines · On Fri, 2 Dec 2022 13:48:17 +0100 Thomas Huth <[email protected]> wrote:
Quoted 41 lines · Janosch Frank
Quoted 25 lines · Thomas Huth
Quoted 16 lines · Nico Boehr

Quoting Thomas Huth (2022-12-02 10:09:03)

Quoted 7 lines · Janosch Frank
Quoted 6 lines · Nico Boehr
Quoted 4 lines

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

Quoted 7 lines
Quoted 3 lines

NACK

We're not putting test specific code into the library.

Do we need a new file (in the third patch) for the new test at all, or could
the new test simply be added to s390x/migration-skey.c instead?

Quoted 7 lines

Mh, not quite. One test wants to change storage keys *before* migrating, the other *while* migrating. Since we can only migrate once, it is not obvious to me how we could do that in one run.

Speaking of one run, what we could do is add a command line argument which decides which test to run and then call the same test with different arguments in unittests.cfg.

Quoted 5 lines

Yes, that's what I had in mind - use a command line argument to select the test ... should be OK as long as both variants are listed in unittests.cfg, shouldn't it?

   Thomas

@Thomas @Claudio:
I see two possible solutions if we want a "testlib" at some point (which for the record I don't have anything against):

Putting the files into lib/s390x/testlib/* which will then be part of our normal lib.
That's a minimal effort solution. It still puts those files into lib/* but they are at least contained in a directory.

Quoted 9 lines
Quoted 2 lines

Putting the files into s390x/testlib/* and creating a proper new lib. Which means we'd need a few more lines of makefile changes.

Though this is an excellent topic for a Friday afternoon bikeshedding ... I don't mind much either way. I maybe just got a small preference to not touch the main lib/ folder here. I guess you could even call it s390x/migration-skey-common.c and leave the lib logic out of the game ... but I don't really mind. Up to you to decide ;-)

I really like the idea of having only one test and use a commandline parameter to decide which variant to run

this way no need to put things in external files

Quoted 2 lines

Thomas

author
#12 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoting Claudio Imbrenda (2022-12-02 13:56:47)

Quoted 55 lines
Quoted 42 lines · On Fri, 2 Dec 2022 13:48:17 +0100 Thomas Huth <[email protected]> wrote:
Quoted 41 lines · Janosch Frank
Quoted 25 lines · Thomas Huth
Quoted 16 lines · Nico Boehr

Quoting Thomas Huth (2022-12-02 10:09:03)

Quoted 7 lines · Janosch Frank
Quoted 6 lines · Nico Boehr
Quoted 4 lines

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

Quoted 7 lines
Quoted 3 lines

NACK

We're not putting test specific code into the library.

Do we need a new file (in the third patch) for the new test at all, or could
the new test simply be added to s390x/migration-skey.c instead?

Quoted 7 lines

Mh, not quite. One test wants to change storage keys *before* migrating, the other *while* migrating. Since we can only migrate once, it is not obvious to me how we could do that in one run.

Speaking of one run, what we could do is add a command line argument which decides which test to run and then call the same test with different arguments in unittests.cfg.

Quoted 5 lines

Yes, that's what I had in mind - use a command line argument to select the test ... should be OK as long as both variants are listed in unittests.cfg, shouldn't it?

   Thomas

@Thomas @Claudio:
I see two possible solutions if we want a "testlib" at some point (which for the record I don't have anything against):

Putting the files into lib/s390x/testlib/* which will then be part of our normal lib.
That's a minimal effort solution. It still puts those files into lib/* but they are at least contained in a directory.

Quoted 9 lines
Quoted 2 lines

Putting the files into s390x/testlib/* and creating a proper new lib. Which means we'd need a few more lines of makefile changes.

Though this is an excellent topic for a Friday afternoon bikeshedding ... I don't mind much either way. I maybe just got a small preference to not touch the main lib/ folder here. I guess you could even call it s390x/migration-skey-common.c and leave the lib logic out of the game ... but I don't really mind. Up to you to decide ;-)

Quoted 4 lines

I really like the idea of having only one test and use a commandline parameter to decide which variant to run

this way no need to put things in external files

OK, I also like this suggestion, then I'll refactor the tests.

author
#13 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

In reply to Janosch Frank

Quoting Janosch Frank (2022-12-02 10:03:22)

Quoted 9 lines · Nico Boehr
Quoted 4 lines

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

NACK

We're not putting test specific code into the library.

What do you mean by "test specific"? After all, it is used by two tests now, possibly more in the future.

Any alternative suggestions?

#14 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoted 7 lines · Nico Boehr

Quoting Janosch Frank (2022-12-02 10:03:22)

Quoted 6 lines · Nico Boehr
Quoted 4 lines

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

Quoted 7 lines
Quoted 3 lines

NACK

We're not putting test specific code into the library.

What do you mean by "test specific"? After all, it is used by two tests now, possibly more in the future.

Any alternative suggestions?

For me this is like putting kselftest macros/functions into the kernel.

The KUT library is more or less the kernel on which the tests in s390x/ are based on. It provides primitives which (hopefully and mostly) aren't specific to tests.

Yes:
Providing skey set and get functions for one or multiple pages to tests. I.e. sske and iske wrappers.

No:
Providing multi-page skey set and verify functions that set and verify skeys based on a pattern which is __hardcoded__ into the function using the skey wrappers. I.e. you're trying to create a new layer (test functionality) and stuffing it into the unit test kernel library.

What you want is a separate testlib which would reside in s390x/testlib/ where we can store often repeated functions and macros.

#15 lore raw

Re: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions

Quoted 26 lines · On Fri, 2 Dec 2022 12:20:34 +0100 Janosch Frank <[email protected]> wrote:
Quoted 7 lines · Nico Boehr

Quoting Janosch Frank (2022-12-02 10:03:22)

Quoted 6 lines · Nico Boehr
Quoted 4 lines

Upcoming changes will add a test which is very similar to the existing skey migration test. To reduce code duplication, move the common functions to a library which can be re-used by both tests.

Quoted 7 lines
Quoted 3 lines

NACK

We're not putting test specific code into the library.

What do you mean by "test specific"? After all, it is used by two tests now, possibly more in the future.

Any alternative suggestions?

For me this is like putting kselftest macros/functions into the kernel.

The KUT library is more or less the kernel on which the tests in s390x/ are based on. It provides primitives which (hopefully and mostly) aren't specific to tests.

Yes:
Providing skey set and get functions for one or multiple pages to tests. I.e. sske and iske wrappers.

Quoted 8 lines

No:
Providing multi-page skey set and verify functions that set and verify skeys based on a pattern which is __hardcoded__ into the function using the skey wrappers. I.e. you're trying to create a new layer (test functionality) and stuffing it into the unit test kernel library.

What you want is a separate testlib which would reside in s390x/testlib/ where we can store often repeated functions and macros.

oufff, then we need to also fix the cmma migration tests

author
#16 lore raw

PATCH 2/3 lib: s390x: skey: add seed value for storage keys

In reply to Nico Boehr

Upcoming changes will change storage keys in a loop. To make sure each iteration of the loops sets different keys, add variants of the storage key library functions which allow to specify a seed.

---
 lib/s390x/skey.c | 12 +++++++-----
 lib/s390x/skey.h | 14 ++++++++++++--
 2 files changed, 19 insertions(+), 7 deletions(-)
lib/s390x/skey.c +7 -5
index 100f0949a244..4ab0828ee98f 100644
@@ -14,10 +14,11 @@
 #include <skey.h>
 
 /*
- * Set storage keys on pagebuf.
+ * Set storage keys on pagebuf with a seed for the storage keys.
  * pagebuf must point to page_count consecutive pages.
+ * Only the lower seven bits of the seed are considered.
  */
-void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
+void skey_set_keys_with_seed(uint8_t *pagebuf, unsigned long page_count, unsigned char seed)
 {
 	unsigned char key_to_set;
 	unsigned long i;
@@ -30,7 +31,7 @@ void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
 		 * protection as well as reference and change indication for
 		 * some keys.
 		 */
-		key_to_set = i * 2;
+		key_to_set = (i ^ seed) * 2;
 		set_storage_key(pagebuf + i * PAGE_SIZE, key_to_set, 1);
 	}
 }
@@ -38,13 +39,14 @@ void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
 /*
  * Verify storage keys on pagebuf.
  * Storage keys must have been set by skey_set_keys on pagebuf before.
+ * skey_set_keys must have been called with the same seed value.
  *
  * If storage keys match the expected result, will return a skey_verify_result
  * with verify_failed false. All other fields are then invalid.
  * If there is a mismatch, returned struct will have verify_failed true and will
  * be filled with the details on the first mismatch encountered.
  */
-struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count)
+struct skey_verify_result skey_verify_keys_with_seed(uint8_t *pagebuf, unsigned long page_count, unsigned char seed)
 {
 	union skey expected_key, actual_key;
 	struct skey_verify_result result = {
@@ -56,7 +58,7 @@ struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_
 	for (i = 0; i < page_count; i++) {
 		cur_page = pagebuf + i * PAGE_SIZE;
 		actual_key.val = get_storage_key(cur_page);
-		expected_key.val = i * 2;
+		expected_key.val = (i ^ seed) * 2;
 
 		/*
 		 * The PoP neither gives a guarantee that the reference bit is
lib/s390x/skey.h +12 -2
index a0f8caa1270b..bba1c131276d 100644
@@ -23,9 +23,19 @@ struct skey_verify_result {
 	unsigned long page_mismatch_addr;
 };
 
-void skey_set_keys(uint8_t *pagebuf, unsigned long page_count);
+void skey_set_keys_with_seed(uint8_t *pagebuf, unsigned long page_count, unsigned char seed);
 
-struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count);
+static inline void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+	skey_set_keys_with_seed(pagebuf, page_count, 0);
+}
+
+struct skey_verify_result skey_verify_keys_with_seed(uint8_t *pagebuf, unsigned long page_count, unsigned char seed);
+
+static inline struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+	return skey_verify_keys_with_seed(pagebuf, page_count, 0);
+}
 
 void skey_report_verify(struct skey_verify_result * const result);
 
Signature · 1 line
2.36.1
#17 lore raw

Re: [kvm-unit-tests PATCH v1 2/3] lib: s390x: skey: add seed value for storage keys

Quoted 5 lines · On Thu, 1 Dec 2022 09:46:41 +0100 Nico Boehr <[email protected]> wrote:

Upcoming changes will change storage keys in a loop. To make sure each iteration of the loops sets different keys, add variants of the storage key library functions which allow to specify a seed.

I wonder if you can simply merge this patch with the previous one

Quoted 83 lines
---
 lib/s390x/skey.c | 12 +++++++-----
 lib/s390x/skey.h | 14 ++++++++++++--
 2 files changed, 19 insertions(+), 7 deletions(-)
lib/s390x/skey.c +7 -5
index 100f0949a244..4ab0828ee98f 100644
@@ -14,10 +14,11 @@
 #include <skey.h>
 
 /*
- * Set storage keys on pagebuf.
+ * Set storage keys on pagebuf with a seed for the storage keys.
  * pagebuf must point to page_count consecutive pages.
+ * Only the lower seven bits of the seed are considered.
  */
-void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
+void skey_set_keys_with_seed(uint8_t *pagebuf, unsigned long page_count, unsigned char seed)
 {
 	unsigned char key_to_set;
 	unsigned long i;
@@ -30,7 +31,7 @@ void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
 		 * protection as well as reference and change indication for
 		 * some keys.
 		 */
-		key_to_set = i * 2;
+		key_to_set = (i ^ seed) * 2;
 		set_storage_key(pagebuf + i * PAGE_SIZE, key_to_set, 1);
 	}
 }
@@ -38,13 +39,14 @@ void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
 /*
  * Verify storage keys on pagebuf.
  * Storage keys must have been set by skey_set_keys on pagebuf before.
+ * skey_set_keys must have been called with the same seed value.
  *
  * If storage keys match the expected result, will return a skey_verify_result
  * with verify_failed false. All other fields are then invalid.
  * If there is a mismatch, returned struct will have verify_failed true and will
  * be filled with the details on the first mismatch encountered.
  */
-struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count)
+struct skey_verify_result skey_verify_keys_with_seed(uint8_t *pagebuf, unsigned long page_count, unsigned char seed)
 {
 	union skey expected_key, actual_key;
 	struct skey_verify_result result = {
@@ -56,7 +58,7 @@ struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_
 	for (i = 0; i < page_count; i++) {
 		cur_page = pagebuf + i * PAGE_SIZE;
 		actual_key.val = get_storage_key(cur_page);
-		expected_key.val = i * 2;
+		expected_key.val = (i ^ seed) * 2;
 
 		/*
 		 * The PoP neither gives a guarantee that the reference bit is
lib/s390x/skey.h +12 -2
index a0f8caa1270b..bba1c131276d 100644
@@ -23,9 +23,19 @@ struct skey_verify_result {
 	unsigned long page_mismatch_addr;
 };
 
-void skey_set_keys(uint8_t *pagebuf, unsigned long page_count);
+void skey_set_keys_with_seed(uint8_t *pagebuf, unsigned long page_count, unsigned char seed);
 
-struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count);
+static inline void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+	skey_set_keys_with_seed(pagebuf, page_count, 0);
+}
+
+struct skey_verify_result skey_verify_keys_with_seed(uint8_t *pagebuf, unsigned long page_count, unsigned char seed);
+
+static inline struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+	return skey_verify_keys_with_seed(pagebuf, page_count, 0);
+}
 
 void skey_report_verify(struct skey_verify_result * const result);
 
author
#18 lore raw

Re: [kvm-unit-tests PATCH v1 2/3] lib: s390x: skey: add seed value for storage keys

Quoting Claudio Imbrenda (2022-12-01 14:27:58)

Quoted 10 lines
Quoted 5 lines · On Thu, 1 Dec 2022 09:46:41 +0100 Nico Boehr <[email protected]> wrote:

Upcoming changes will change storage keys in a loop. To make sure each iteration of the loops sets different keys, add variants of the storage key library functions which allow to specify a seed.

I wonder if you can simply merge this patch with the previous one

Yes, I thought about that too, but I was not sure whether people will like this approach, so I kept it as a seperate patch so I can undo it easily and it stands out a bit :)

I will merge with the previous commit if nobody complains.

author
#19 lore raw

PATCH 3/3 s390x: add storage key test during migration

In reply to Nico Boehr

Add a test which modifies storage keys while migration is in progress.

---
 s390x/Makefile                |   1 +
 s390x/migration-during-skey.c | 103 ++++++++++++++++++++++++++++++++++
 s390x/unittests.cfg           |   5 ++
 3 files changed, 109 insertions(+)
 create mode 100644 s390x/migration-during-skey.c
s390x/Makefile +1 -0
index d097b7071dfb..d9ba9b5fc392 100644
@@ -39,6 +39,7 @@ tests += $(TEST_DIR)/panic-loop-extint.elf
 tests += $(TEST_DIR)/panic-loop-pgm.elf
 tests += $(TEST_DIR)/migration-sck.elf
 tests += $(TEST_DIR)/exittime.elf
+tests += $(TEST_DIR)/migration-during-skey.elf
 
 pv-tests += $(TEST_DIR)/pv-diags.elf
 
s390x/migration-during-skey.c new file +103 -0

103 lines not on this page · raw message

s390x/unittests.cfg +5 -0
index 3caf81eda396..855c352929a4 100644
@@ -208,3 +208,8 @@ groups = migration
 [exittime]
 file = exittime.elf
 smp = 2
+
+[migration-during-skey]
+file = migration-during-skey.elf
+smp = 2
+groups = migration
Signature · 1 line
2.36.1