Skip to contentSkip to conversation

s390x: test CMM during migration

PATCH Superseded Nico Boehr 3 participants 11 messages Nov 24, 2022 – Nov 28, 2022 KVM

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Nico Boehr

author
#0 lore raw

v2 cover s390x: test CMM during migration

v1->v2:
---
* cmm lib: return struct instead of passing in a pointer (thanks Claudio)
* cmm lib: remove get_page_addr() (thanks Claudio)
* cmm lib: print address of mismatch (thanks Claudio)
* cmm lib: misc comments reworked, added and variables renamed
* make sure page states change on every iteration (thanks Claudio)
* add WRITE_ONCE even when not strictly needed (thanks Claudio)

Add a test which changes CMM page states while VM is being migrated.

Nico Boehr (2):
s390x: add a library for CMM-related functions
s390x: add CMM test during migration

 lib/s390x/cmm.c              |  90 ++++++++++++++++++++++++++
 lib/s390x/cmm.h              |  31 +++++++++
 s390x/Makefile               |   2 +
 s390x/migration-cmm.c        |  34 +++-------
 s390x/migration-during-cmm.c | 121 +++++++++++++++++++++++++++++++++++
 s390x/unittests.cfg          |   5 ++
 6 files changed, 257 insertions(+), 26 deletions(-)
 create mode 100644 lib/s390x/cmm.c
 create mode 100644 lib/s390x/cmm.h
 create mode 100644 s390x/migration-during-cmm.c
Signature · 1 line
2.36.1
author
#1 lore raw

PATCH v2 1/2 s390x: add a library for CMM-related functions

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

---
 lib/s390x/cmm.c       | 90 +++++++++++++++++++++++++++++++++++++++++++
 lib/s390x/cmm.h       | 31 +++++++++++++++
 s390x/Makefile        |  1 +
 s390x/migration-cmm.c | 34 ++++------------
 4 files changed, 130 insertions(+), 26 deletions(-)
 create mode 100644 lib/s390x/cmm.c
 create mode 100644 lib/s390x/cmm.h
lib/s390x/cmm.c new file +90 -0
new file mode 100644
index 000000000000..5da02fe628f9
@@ -0,0 +1,90 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * CMM test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+#include <libcflat.h>
+#include <bitops.h>
+#include "cmm.h"
+
+/*
+ * Maps ESSA actions to states the page is allowed to be in after the
+ * respective action was executed.
+ */
+const int allowed_essa_state_masks[4] = {
+	BIT(ESSA_USAGE_STABLE),					/* ESSA_SET_STABLE */
+	BIT(ESSA_USAGE_UNUSED),					/* ESSA_SET_UNUSED */
+	BIT(ESSA_USAGE_VOLATILE),				/* ESSA_SET_VOLATILE */
+	BIT(ESSA_USAGE_VOLATILE) | BIT(ESSA_USAGE_POT_VOLATILE) /* ESSA_SET_POT_VOLATILE */
+};
+
+/*
+ * Set CMM page states on pagebuf.
+ * pagebuf must point to page_count consecutive pages.
+ * page_count must be a multiple of 4.
+ */
+void cmm_set_page_states(uint8_t *pagebuf, int page_count)
+{
+	unsigned long addr = (unsigned long)pagebuf;
+	int i;
+
+	assert(page_count % 4 == 0);
+	for (i = 0; i < page_count; i += 4) {
+		essa(ESSA_SET_STABLE, addr + i * PAGE_SIZE);
+		essa(ESSA_SET_UNUSED, addr + (i + 1) * PAGE_SIZE);
+		essa(ESSA_SET_VOLATILE, addr + (i + 2) * PAGE_SIZE);
+		essa(ESSA_SET_POT_VOLATILE, addr + (i + 3) * PAGE_SIZE);
+	}
+}
+
+/*
+ * Verify CMM page states on pagebuf.
+ * Page states must have been set by cmm_set_page_states on pagebuf before.
+ * page_count must be a multiple of 4.
+ *
+ * If page states match the expected result, will return a cmm_verify_result
+ * with verify_failed false. All other fields are then invalid.
+ * If there is a mismatch, the returned struct will have verify_failed true
+ * and will be filled with details on the first mismatch encountered.
+ */
+struct cmm_verify_result cmm_verify_page_states(uint8_t *pagebuf, int page_count)
+{
+	struct cmm_verify_result result = {
+		.verify_failed = true
+	};
+	int i, expected_mask, actual_mask;
+	unsigned long addr;
+
+	assert(page_count % 4 == 0);
+
+	for (i = 0; i < page_count; i++) {
+		addr = (unsigned long)(pagebuf + i * PAGE_SIZE);
+		actual_mask = essa(ESSA_GET_STATE, addr);
+		/* usage state in bits 60 and 61 */
+		actual_mask = BIT((actual_mask >> 2) & 0x3);
+		expected_mask = allowed_essa_state_masks[i % ARRAY_SIZE(allowed_essa_state_masks)];
+		if (!(actual_mask & expected_mask)) {
+			result.page_mismatch_idx = i;
+			result.page_mismatch_addr = addr;
+			result.expected_mask = expected_mask;
+			result.actual_mask = actual_mask;
+			result.verify_failed = true;
+			return result;
+		}
+	}
+
+	result.verify_failed = false;
+	return result;
+}
+
+void cmm_report_verify(struct cmm_verify_result const *result)
+{
+	if (result->verify_failed)
+		report_fail("page state mismatch: first page idx = %d, addr = %lx, expected_mask = 0x%x, actual_mask = 0x%x", result->page_mismatch_idx, result->page_mismatch_addr, result->expected_mask, result->actual_mask);
+	else
+		report_pass("page states match");
+}
lib/s390x/cmm.h new file +31 -0
new file mode 100644
index 000000000000..41dcc2f953fd
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * CMM test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+#ifndef S390X_CMM_H
+#define S390X_CMM_H
+
+#include <libcflat.h>
+#include <asm/page.h>
+#include <asm/cmm.h>
+
+struct cmm_verify_result {
+	bool verify_failed;
+	char expected_mask;
+	char actual_mask;
+	int page_mismatch_idx;
+	unsigned long page_mismatch_addr;
+};
+
+void cmm_set_page_states(uint8_t *pagebuf, int page_count);
+
+struct cmm_verify_result cmm_verify_page_states(uint8_t *pagebuf, int page_count);
+
+void cmm_report_verify(struct cmm_verify_result const *result);
+
+#endif /* S390X_CMM_H */
s390x/Makefile +1 -0
index bf1504f9d58c..401cb6371cee 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/cmm.o
 
 OBJDIRS += lib/s390x
 
s390x/migration-cmm.c +8 -26
index aa7910ca76bf..720ef9fb9799 100644
@@ -14,41 +14,23 @@
 #include <asm/cmm.h>
 #include <bitops.h>
 
+#include "cmm.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)
 {
-	int i, state_mask, actual_state;
-	/*
-	 * Maps ESSA actions to states the page is allowed to be in after the
-	 * respective action was executed.
-	 */
-	int allowed_essa_state_masks[4] = {
-		BIT(ESSA_USAGE_STABLE),					/* ESSA_SET_STABLE */
-		BIT(ESSA_USAGE_UNUSED),					/* ESSA_SET_UNUSED */
-		BIT(ESSA_USAGE_VOLATILE),				/* ESSA_SET_VOLATILE */
-		BIT(ESSA_USAGE_VOLATILE) | BIT(ESSA_USAGE_POT_VOLATILE) /* ESSA_SET_POT_VOLATILE */
-	};
+	struct cmm_verify_result result;
 
-	assert(NUM_PAGES % 4 == 0);
-	for (i = 0; i < NUM_PAGES; i += 4) {
-		essa(ESSA_SET_STABLE, (unsigned long)pagebuf[i]);
-		essa(ESSA_SET_UNUSED, (unsigned long)pagebuf[i + 1]);
-		essa(ESSA_SET_VOLATILE, (unsigned long)pagebuf[i + 2]);
-		essa(ESSA_SET_POT_VOLATILE, (unsigned long)pagebuf[i + 3]);
-	}
+	cmm_set_page_states(pagebuf, NUM_PAGES);
 
 	puts("Please migrate me, then press return\n");
 	(void)getchar();
 
-	for (i = 0; i < NUM_PAGES; i++) {
-		actual_state = essa(ESSA_GET_STATE, (unsigned long)pagebuf[i]);
-		/* extract the usage state in bits 60 and 61 */
-		actual_state = (actual_state >> 2) & 0x3;
-		state_mask = allowed_essa_state_masks[i % ARRAY_SIZE(allowed_essa_state_masks)];
-		report(BIT(actual_state) & state_mask, "page %d state: expected_mask=0x%x actual_mask=0x%lx", i, state_mask, BIT(actual_state));
-	}
+	result = cmm_verify_page_states(pagebuf, NUM_PAGES);
+	cmm_report_verify(&result);
 }
 
 int main(void)
Signature · 1 line
2.36.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [kvm-unit-tests PATCH v2 1/2] s390x: add a library for CMM-related functions

Quoted 28 lines · Nico Boehr

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

---
  lib/s390x/cmm.c       | 90 +++++++++++++++++++++++++++++++++++++++++++
  lib/s390x/cmm.h       | 31 +++++++++++++++
  s390x/Makefile        |  1 +
  s390x/migration-cmm.c | 34 ++++------------
  4 files changed, 130 insertions(+), 26 deletions(-)

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

lib/s390x/cmm.c new file +8 -0
new file mode 100644
index 000000000000..5da02fe628f9
@@ -0,0 +1,90 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * CMM test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>

lib/s390x/cmm.c line 18

Quoted 10 lines
lib/s390x/cmm.c new file +10 -0
+ */
+#include <libcflat.h>
+#include <bitops.h>
+#include "cmm.h"
+
+/*
+ * Maps ESSA actions to states the page is allowed to be in after the
+ * respective action was executed.
+ */
+const int allowed_essa_state_masks[4] = {

Could be declared as "static const int ...", I guess?

Apart from that nit:

author
#3 lore raw

Re: [kvm-unit-tests PATCH v2 1/2] s390x: add a library for CMM-related functions

Quoting Thomas Huth (2022-11-25 14:45:53)

Quoted 1 line · Nico Boehr

[...]

Quoted 3 lines
Quoted 3 lines
lib/s390x/cmm.c new file +0 -0
new file mode 100644
index 000000000000..5da02fe628f9

[...]

Quoted 7 lines

lib/s390x/cmm.c line 18

Quoted 5 lines
lib/s390x/cmm.c new file +5 -0
+/*
+ * Maps ESSA actions to states the page is allowed to be in after the
+ * respective action was executed.
+ */
+const int allowed_essa_state_masks[4] = {

Could be declared as "static const int ...", I guess?

Yes, done.

Quoted 2 lines

Apart from that nit:

Thanks.

#4 lore raw

Re: [kvm-unit-tests PATCH v2 1/2] s390x: add a library for CMM-related functions

In reply to Nico Boehr
Quoted 40 lines · On Thu, 24 Nov 2022 14:44:28 +0100 Nico Boehr <[email protected]> wrote:

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

---
 lib/s390x/cmm.c       | 90 +++++++++++++++++++++++++++++++++++++++++++
 lib/s390x/cmm.h       | 31 +++++++++++++++
 s390x/Makefile        |  1 +
 s390x/migration-cmm.c | 34 ++++------------
 4 files changed, 130 insertions(+), 26 deletions(-)
 create mode 100644 lib/s390x/cmm.c
 create mode 100644 lib/s390x/cmm.h
lib/s390x/cmm.c new file +20 -0
new file mode 100644
index 000000000000..5da02fe628f9
@@ -0,0 +1,90 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * CMM test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+#include <libcflat.h>
+#include <bitops.h>
+#include "cmm.h"
+
+/*
+ * Maps ESSA actions to states the page is allowed to be in after the
+ * respective action was executed.
+ */
+const int allowed_essa_state_masks[4] = {
+	BIT(ESSA_USAGE_STABLE),					/* ESSA_SET_STABLE */
+	BIT(ESSA_USAGE_UNUSED),					/* ESSA_SET_UNUSED */

lib/s390x/cmm.c line 30

Quoted 10 lines
lib/s390x/cmm.c new file +10 -0
+	BIT(ESSA_USAGE_VOLATILE),				/* ESSA_SET_VOLATILE */
+	BIT(ESSA_USAGE_VOLATILE) | BIT(ESSA_USAGE_POT_VOLATILE) /* ESSA_SET_POT_VOLATILE */
+};
+
+/*
+ * Set CMM page states on pagebuf.
+ * pagebuf must point to page_count consecutive pages.
+ * page_count must be a multiple of 4.
+ */
+void cmm_set_page_states(uint8_t *pagebuf, int page_count)

this could be an unsigned int (but maybe unsigned long would be better)

Quoted 14 lines
lib/s390x/cmm.c new file +14 -0
+{
+	unsigned long addr = (unsigned long)pagebuf;
+	int i;
+
+	assert(page_count % 4 == 0);
+	for (i = 0; i < page_count; i += 4) {
+		essa(ESSA_SET_STABLE, addr + i * PAGE_SIZE);
+		essa(ESSA_SET_UNUSED, addr + (i + 1) * PAGE_SIZE);
+		essa(ESSA_SET_VOLATILE, addr + (i + 2) * PAGE_SIZE);
+		essa(ESSA_SET_POT_VOLATILE, addr + (i + 3) * PAGE_SIZE);
+	}
+}
+
+/*

lib/s390x/cmm.c line 54

Quoted 10 lines
lib/s390x/cmm.c new file +10 -0
+ * Verify CMM page states on pagebuf.
+ * Page states must have been set by cmm_set_page_states on pagebuf before.
+ * page_count must be a multiple of 4.
+ *
+ * If page states match the expected result, will return a cmm_verify_result
+ * with verify_failed false. All other fields are then invalid.
+ * If there is a mismatch, the returned struct will have verify_failed true
+ * and will be filled with details on the first mismatch encountered.
+ */
+struct cmm_verify_result cmm_verify_page_states(uint8_t *pagebuf, int page_count)

same here

Quoted 11 lines
lib/s390x/cmm.c new file +11 -0
+{
+	struct cmm_verify_result result = {
+		.verify_failed = true
+	};
+	int i, expected_mask, actual_mask;
+	unsigned long addr;
+
+	assert(page_count % 4 == 0);
+
+	for (i = 0; i < page_count; i++) {
+		addr = (unsigned long)(pagebuf + i * PAGE_SIZE);

lib/s390x/cmm.c line 75

Quoted 10 lines
lib/s390x/cmm.c new file +10 -0
+		actual_mask = essa(ESSA_GET_STATE, addr);
+		/* usage state in bits 60 and 61 */
+		actual_mask = BIT((actual_mask >> 2) & 0x3);
+		expected_mask = allowed_essa_state_masks[i % ARRAY_SIZE(allowed_essa_state_masks)];
+		if (!(actual_mask & expected_mask)) {
+			result.page_mismatch_idx = i;
+			result.page_mismatch_addr = addr;
+			result.expected_mask = expected_mask;
+			result.actual_mask = actual_mask;
+			result.verify_failed = true;

it's already true, you don't need to set it again

lib/s390x/cmm.c line 87

Quoted 12 lines
lib/s390x/cmm.c new file +12 -0
+			return result;
+		}
+	}
+
+	result.verify_failed = false;
+	return result;
+}
+
+void cmm_report_verify(struct cmm_verify_result const *result)
+{
+	if (result->verify_failed)
+		report_fail("page state mismatch: first page idx = %d, addr = %lx, expected_mask = 0x%x, actual_mask = 0x%x", result->page_mismatch_idx, result->page_mismatch_addr, result->expected_mask, result->actual_mask);

this line looks longer than 120 columns

Quoted 20 lines
lib/s390x/cmm.c new file +3 -0
+	else
+		report_pass("page states match");
+}
lib/s390x/cmm.h new file +11 -0
new file mode 100644
index 000000000000..41dcc2f953fd
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * CMM test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+#ifndef S390X_CMM_H
+#define S390X_CMM_H

lib/s390x/cmm.h line 21

Quoted 10 lines
lib/s390x/cmm.h new file +10 -0
+
+#include <libcflat.h>
+#include <asm/page.h>
+#include <asm/cmm.h>
+
+struct cmm_verify_result {
+	bool verify_failed;
+	char expected_mask;
+	char actual_mask;
+	int page_mismatch_idx;

maybe also unsigned long to be consistent with
cmm_set_page_states

Quoted 76 lines
lib/s390x/cmm.h new file +10 -0
+	unsigned long page_mismatch_addr;
+};
+
+void cmm_set_page_states(uint8_t *pagebuf, int page_count);
+
+struct cmm_verify_result cmm_verify_page_states(uint8_t *pagebuf, int page_count);
+
+void cmm_report_verify(struct cmm_verify_result const *result);
+
+#endif /* S390X_CMM_H */
s390x/Makefile +1 -0
index bf1504f9d58c..401cb6371cee 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/cmm.o
 
 OBJDIRS += lib/s390x
 
s390x/migration-cmm.c +8 -26
index aa7910ca76bf..720ef9fb9799 100644
@@ -14,41 +14,23 @@
 #include <asm/cmm.h>
 #include <bitops.h>
 
+#include "cmm.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)
 {
-	int i, state_mask, actual_state;
-	/*
-	 * Maps ESSA actions to states the page is allowed to be in after the
-	 * respective action was executed.
-	 */
-	int allowed_essa_state_masks[4] = {
-		BIT(ESSA_USAGE_STABLE),					/* ESSA_SET_STABLE */
-		BIT(ESSA_USAGE_UNUSED),					/* ESSA_SET_UNUSED */
-		BIT(ESSA_USAGE_VOLATILE),				/* ESSA_SET_VOLATILE */
-		BIT(ESSA_USAGE_VOLATILE) | BIT(ESSA_USAGE_POT_VOLATILE) /* ESSA_SET_POT_VOLATILE */
-	};
+	struct cmm_verify_result result;
 
-	assert(NUM_PAGES % 4 == 0);
-	for (i = 0; i < NUM_PAGES; i += 4) {
-		essa(ESSA_SET_STABLE, (unsigned long)pagebuf[i]);
-		essa(ESSA_SET_UNUSED, (unsigned long)pagebuf[i + 1]);
-		essa(ESSA_SET_VOLATILE, (unsigned long)pagebuf[i + 2]);
-		essa(ESSA_SET_POT_VOLATILE, (unsigned long)pagebuf[i + 3]);
-	}
+	cmm_set_page_states(pagebuf, NUM_PAGES);
 
 	puts("Please migrate me, then press return\n");
 	(void)getchar();
 
-	for (i = 0; i < NUM_PAGES; i++) {
-		actual_state = essa(ESSA_GET_STATE, (unsigned long)pagebuf[i]);
-		/* extract the usage state in bits 60 and 61 */
-		actual_state = (actual_state >> 2) & 0x3;
-		state_mask = allowed_essa_state_masks[i % ARRAY_SIZE(allowed_essa_state_masks)];
-		report(BIT(actual_state) & state_mask, "page %d state: expected_mask=0x%x actual_mask=0x%lx", i, state_mask, BIT(actual_state));
-	}
+	result = cmm_verify_page_states(pagebuf, NUM_PAGES);
+	cmm_report_verify(&result);
 }
 
 int main(void)
author
#5 lore raw

Re: [kvm-unit-tests PATCH v2 1/2] s390x: add a library for CMM-related functions

Quoting Claudio Imbrenda (2022-11-25 15:03:48)

Quoted 2 lines

On Thu, 24 Nov 2022 14:44:28 +0100
Nico Boehr <[email protected]> wrote:

[...]

Quoted 5 lines
Quoted 5 lines
lib/s390x/cmm.c new file +0 -0
new file mode 100644
index 000000000000..5da02fe628f9

[...]

Quoted 8 lines

lib/s390x/cmm.c line 30

Quoted 6 lines
lib/s390x/cmm.c new file +6 -0
+/*
+ * Set CMM page states on pagebuf.
+ * pagebuf must point to page_count consecutive pages.
+ * page_count must be a multiple of 4.
+ */
+void cmm_set_page_states(uint8_t *pagebuf, int page_count)

this could be an unsigned int (but maybe unsigned long would be better)

Yes, changed to unsigned long.

[...]

Quoted 5 lines
Quoted 5 lines

+/*
+ * Verify CMM page states on pagebuf.
+ * Page states must have been set by cmm_set_page_states on pagebuf before.
+ * page_count must be a multiple of 4.
+ *

Quoted 8 lines
Quoted 6 lines

+ * If page states match the expected result, will return a cmm_verify_result
+ * with verify_failed false. All other fields are then invalid.
+ * If there is a mismatch, the returned struct will have verify_failed true
+ * and will be filled with details on the first mismatch encountered.
+ */
+struct cmm_verify_result cmm_verify_page_states(uint8_t *pagebuf, int page_count)

same here

Yes, done.

Quoted 13 lines

lib/s390x/cmm.c line 66

Quoted 13 lines
lib/s390x/cmm.c new file +12 -0
+{
+     struct cmm_verify_result result = {
+             .verify_failed = true
+     };
+     int i, expected_mask, actual_mask;
+     unsigned long addr;
+
+     assert(page_count % 4 == 0);
+
+     for (i = 0; i < page_count; i++) {
+             addr = (unsigned long)(pagebuf + i * PAGE_SIZE);
+             actual_mask = essa(ESSA_GET_STATE, addr);

Oh, essa() returns unsigned long, so let's make actual_mask, expected_mask etc. unsigned long, too.

Quoted 11 lines

lib/s390x/cmm.c line 75

Quoted 9 lines
lib/s390x/cmm.c new file +9 -0
+             /* usage state in bits 60 and 61 */
+             actual_mask = BIT((actual_mask >> 2) & 0x3);
+             expected_mask = allowed_essa_state_masks[i % ARRAY_SIZE(allowed_essa_state_masks)];
+             if (!(actual_mask & expected_mask)) {
+                     result.page_mismatch_idx = i;
+                     result.page_mismatch_addr = addr;
+                     result.expected_mask = expected_mask;
+                     result.actual_mask = actual_mask;
+                     result.verify_failed = true;

it's already true, you don't need to set it again

OK, removed.

[...]

Quoted 6 lines

lib/s390x/cmm.c line 87

Quoted 4 lines
lib/s390x/cmm.c new file +4 -0
+void cmm_report_verify(struct cmm_verify_result const *result)
+{
+     if (result->verify_failed)
+             report_fail("page state mismatch: first page idx = %d, addr = %lx, expected_mask = 0x%x, actual_mask = 0x%x", result->page_mismatch_idx, result->page_mismatch_addr, result->expected_mask, result->actual_mask);

this line looks longer than 120 columns

Yes, wrapped it.

Quoted 5 lines
Quoted 5 lines
lib/s390x/cmm.h new file +0 -0
new file mode 100644
index 000000000000..41dcc2f953fd

[...]

Quoted 8 lines

lib/s390x/cmm.h line 21

Quoted 5 lines
lib/s390x/cmm.h new file +5 -0
+struct cmm_verify_result {
+     bool verify_failed;
+     char expected_mask;
+     char actual_mask;
+     int page_mismatch_idx;

maybe also unsigned long to be consistent with
cmm_set_page_states

Yes, done.

author
#6 lore raw

PATCH v2 2/2 s390x: add CMM test during migration

In reply to Nico Boehr

Add a test which modifies CMM page states while migration is in progress.

---
 s390x/Makefile               |   1 +
 s390x/migration-during-cmm.c | 121 +++++++++++++++++++++++++++++++++++
 s390x/unittests.cfg          |   5 ++
 3 files changed, 127 insertions(+)
 create mode 100644 s390x/migration-during-cmm.c
s390x/Makefile +1 -0
index 401cb6371cee..64c7c04409ae 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-cmm.elf
 
 pv-tests += $(TEST_DIR)/pv-diags.elf
 
s390x/migration-during-cmm.c new file +121 -0

121 lines not on this page · raw message

s390x/unittests.cfg +5 -0
index 3caf81eda396..f6889bd4da01 100644
@@ -208,3 +208,8 @@ groups = migration
 [exittime]
 file = exittime.elf
 smp = 2
+
+[migration-during-cmm]
+file = migration-during-cmm.elf
+groups = migration
+smp = 2
Signature · 1 line
2.36.1
Reviewed-by on v2 2/2
#7 lore raw

Re: [kvm-unit-tests PATCH v2 2/2] s390x: add CMM test during migration

Quoted 39 lines · Nico Boehr

Add a test which modifies CMM page states while migration is in progress.

---
  s390x/Makefile               |   1 +
  s390x/migration-during-cmm.c | 121 +++++++++++++++++++++++++++++++++++
  s390x/unittests.cfg          |   5 ++
  3 files changed, 127 insertions(+)

create mode 100644 s390x/migration-during-cmm.c

s390x/Makefile +1 -0
index 401cb6371cee..64c7c04409ae 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-cmm.elf
 
  pv-tests += $(TEST_DIR)/pv-diags.elf
 
s390x/migration-during-cmm.c new file +10 -0
new file mode 100644
index 000000000000..afe1f73605ba
@@ -0,0 +1,121 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Perform CMMA actions while migrating.
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+

s390x/migration-during-cmm.c line 20

Quoted 10 lines
s390x/migration-during-cmm.c new file +10 -0
+#include <libcflat.h>
+#include <smp.h>
+#include <asm-generic/barrier.h>
+
+#include "cmm.h"
+
+#define NUM_PAGES 128
+
+/*
+ * Allocate 3 pages more than we need so we can start at different offsets. This ensures page states

I'd suggest to break the line after "offsets.", that's better to read.

Quoted 4 lines
s390x/migration-during-cmm.c new file +4 -0
+ * change on every loop iteration.
+ */
+static uint8_t pagebuf[(NUM_PAGES + 3) * PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
+

s390x/migration-during-cmm.c line 34

Quoted 10 lines
s390x/migration-during-cmm.c new file +10 -0
+static unsigned int thread_iters;
+static int thread_should_exit;
+static int thread_exited;
+struct cmm_verify_result result;
+
+static void test_cmm_during_migration(void)
+{
+	uint8_t *pagebuf_start;
+	/*
+	 * The second CPU must not print on the console, otherwise it will race with

"print to the console" ? ... no clue, I'm not a native speaker ;-)

Quoted 9 lines
s390x/migration-during-cmm.c new file +9 -0
+	 * the primary CPU on the SCLP buffer.
+	 */
+	while (!thread_should_exit) {
+		/*
+		 * Start on a offset different from the last iteration so page states change with
+		 * every iteration. This is why pagebuf has 3 extra pages.
+		 */
+		pagebuf_start = pagebuf + (thread_iters % 4) * PAGE_SIZE;
+		cmm_set_page_states(pagebuf_start, NUM_PAGES);

s390x/migration-during-cmm.c line 53

Quoted 10 lines
s390x/migration-during-cmm.c new file +10 -0
+
+		/*
+		 * Always increment even if the verify fails. This ensures primary CPU knows where
+		 * we left off and can do an additional verify round after migration finished.
+		 */
+		thread_iters++;
+
+		result = cmm_verify_page_states(pagebuf_start, NUM_PAGES);
+		if (result.verify_failed)
+			goto out;

A "break" would be nicer than a goto.

Quoted 50 lines
s390x/migration-during-cmm.c new file +50 -0
+	}
+
+out:
+	WRITE_ONCE(thread_exited, 1);
+}
+
+int main(void)
+{
+	bool has_essa = check_essa_available();
+	struct psw psw;
+
+	report_prefix_push("migration-during-cmm");
+	if (!has_essa) {
+		report_skip("ESSA is not available");
+		goto error;
+	}
+
+	if (smp_query_num_cpus() == 1) {
+		report_skip("need at least 2 cpus for this test");
+		goto error;
+	}
+
+	psw.mask = extract_psw_mask();
+	psw.addr = (unsigned long)test_cmm_during_migration;
+	smp_cpu_setup(1, psw);
+
+	puts("Please migrate me, then press return\n");
+	(void)getchar();
+
+	WRITE_ONCE(thread_should_exit, 1);
+
+	while (!thread_exited)
+		mb();
+
+	report_info("thread completed %u iterations", thread_iters);
+
+	report_prefix_push("during migration");
+	cmm_report_verify(&result);
+	report_prefix_pop();
+
+	/*
+	 * Verification of page states occurs on the thread. We don't know if we
+	 * were still migrating during the verification.
+	 * To be sure, make another verification round after the migration
+	 * finished to catch page states which might not have been migrated
+	 * correctly.
+	 */
+	report_prefix_push("after migration");
+	assert(thread_iters > 0);
+	result = cmm_verify_page_states(pagebuf + ((thread_iters - 1) % 4) * PAGE_SIZE, NUM_PAGES);

s390x/migration-during-cmm.c line 113

Quoted 10 lines
s390x/migration-during-cmm.c new file +10 -0
+	cmm_report_verify(&result);
+	report_prefix_pop();
+
+	goto done;
+
+error:
+	/*
+	 * If we just exit and don't ask migrate_cmd to migrate us, it
+	 * will just hang forever. Hence, also ask for migration when we
+	 * skip this test alltogether.

s/alltogether/all together/

Quoted 13 lines
s390x/migration-during-cmm.c new file +8 -0
+	 */
+	puts("Please migrate me, then press return\n");
+	(void)getchar();
+
+done:
+	report_prefix_pop();
+	return report_summary();
+}
s390x/unittests.cfg +0 -0
index 3caf81eda396..f6889bd4da01 100644

s390x/unittests.cfg groups = migration · line 215

Quoted 8 lines
s390x/unittests.cfg +5 -0
@@ -208,3 +208,8 @@ groups = migration
  [exittime]
  file = exittime.elf
  smp = 2
+
+[migration-during-cmm]
+file = migration-during-cmm.elf
+groups = migration
+smp = 2

With the nits fixed:

#8 lore raw

Re: [kvm-unit-tests PATCH v2 2/2] s390x: add CMM test during migration

On Fri, 25 Nov 2022 14:58:35 +0100
Thomas Huth <[email protected]> wrote:

[...]

Quoted 6 lines

s390x/migration-during-cmm.c line 113

Quoted 4 lines
s390x/migration-during-cmm.c new file +4 -0
+	/*
+	 * If we just exit and don't ask migrate_cmd to migrate us, it
+	 * will just hang forever. Hence, also ask for migration when we
+	 * skip this test alltogether.  

s/alltogether/all together/

nope, it's "altogether"

#9 lore raw

Re: [kvm-unit-tests PATCH v2 2/2] s390x: add CMM test during migration

Quoted 5 lines · Claudio Imbrenda

On Fri, 25 Nov 2022 14:58:35 +0100
Thomas Huth <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 6 lines

s390x/migration-during-cmm.c line 113

Quoted 4 lines
s390x/migration-during-cmm.c new file +4 -0
+	/*
+	 * If we just exit and don't ask migrate_cmd to migrate us, it
+	 * will just hang forever. Hence, also ask for migration when we
+	 * skip this test alltogether.

s/alltogether/all together/

nope, it's "altogether"

Drat, it must be close to the weekend already ;-)

Thomas

#10 lore raw

Re: [kvm-unit-tests PATCH v2 2/2] s390x: add CMM test during migration

In reply to Nico Boehr
Quoted 56 lines · On Thu, 24 Nov 2022 14:44:29 +0100 Nico Boehr <[email protected]> wrote:

Add a test which modifies CMM page states while migration is in progress.

---
 s390x/Makefile               |   1 +
 s390x/migration-during-cmm.c | 121 +++++++++++++++++++++++++++++++++++
 s390x/unittests.cfg          |   5 ++
 3 files changed, 127 insertions(+)
 create mode 100644 s390x/migration-during-cmm.c
s390x/Makefile +1 -0
index 401cb6371cee..64c7c04409ae 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-cmm.elf
 
 pv-tests += $(TEST_DIR)/pv-diags.elf
 
s390x/migration-during-cmm.c new file +27 -0
new file mode 100644
index 000000000000..afe1f73605ba
@@ -0,0 +1,121 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Perform CMMA actions while migrating.
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <[email protected]>
+ */
+
+#include <libcflat.h>
+#include <smp.h>
+#include <asm-generic/barrier.h>
+
+#include "cmm.h"
+
+#define NUM_PAGES 128
+
+/*
+ * Allocate 3 pages more than we need so we can start at different offsets. This ensures page states
+ * change on every loop iteration.
+ */
+static uint8_t pagebuf[(NUM_PAGES + 3) * PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
+
+static unsigned int thread_iters;
+static int thread_should_exit;
+static int thread_exited;

s390x/migration-during-cmm.c line 37

Quoted 10 lines
s390x/migration-during-cmm.c new file +10 -0
+struct cmm_verify_result result;
+
+static void test_cmm_during_migration(void)
+{
+	uint8_t *pagebuf_start;
+	/*
+	 * The second CPU must not print on the console, otherwise it will race with
+	 * the primary CPU on the SCLP buffer.
+	 */
+	while (!thread_should_exit) {

I don't see any mb()s here, maybe use READ_ONCE

Quoted 70 lines
s390x/migration-during-cmm.c new file +70 -0
+		/*
+		 * Start on a offset different from the last iteration so page states change with
+		 * every iteration. This is why pagebuf has 3 extra pages.
+		 */
+		pagebuf_start = pagebuf + (thread_iters % 4) * PAGE_SIZE;
+		cmm_set_page_states(pagebuf_start, NUM_PAGES);
+
+		/*
+		 * Always increment even if the verify fails. This ensures primary CPU knows where
+		 * we left off and can do an additional verify round after migration finished.
+		 */
+		thread_iters++;
+
+		result = cmm_verify_page_states(pagebuf_start, NUM_PAGES);
+		if (result.verify_failed)
+			goto out;
+	}
+
+out:
+	WRITE_ONCE(thread_exited, 1);
+}
+
+int main(void)
+{
+	bool has_essa = check_essa_available();
+	struct psw psw;
+
+	report_prefix_push("migration-during-cmm");
+	if (!has_essa) {
+		report_skip("ESSA is not available");
+		goto error;
+	}
+
+	if (smp_query_num_cpus() == 1) {
+		report_skip("need at least 2 cpus for this test");
+		goto error;
+	}
+
+	psw.mask = extract_psw_mask();
+	psw.addr = (unsigned long)test_cmm_during_migration;
+	smp_cpu_setup(1, psw);
+
+	puts("Please migrate me, then press return\n");
+	(void)getchar();
+
+	WRITE_ONCE(thread_should_exit, 1);
+
+	while (!thread_exited)
+		mb();
+
+	report_info("thread completed %u iterations", thread_iters);
+
+	report_prefix_push("during migration");
+	cmm_report_verify(&result);
+	report_prefix_pop();
+
+	/*
+	 * Verification of page states occurs on the thread. We don't know if we
+	 * were still migrating during the verification.
+	 * To be sure, make another verification round after the migration
+	 * finished to catch page states which might not have been migrated
+	 * correctly.
+	 */
+	report_prefix_push("after migration");
+	assert(thread_iters > 0);
+	result = cmm_verify_page_states(pagebuf + ((thread_iters - 1) % 4) * PAGE_SIZE, NUM_PAGES);
+	cmm_report_verify(&result);
+	report_prefix_pop();
+
+	goto done;

s390x/migration-during-cmm.c line 117

Quoted 10 lines
s390x/migration-during-cmm.c new file +10 -0
+
+error:
+	/*
+	 * If we just exit and don't ask migrate_cmd to migrate us, it
+	 * will just hang forever. Hence, also ask for migration when we
+	 * skip this test alltogether.
+	 */
+	puts("Please migrate me, then press return\n");
+	(void)getchar();
+

I wonder if this can be made easier to review and less error prone...

maybe something like:

static void migrate_once(void)

{
	static bool migrated = false;

	if (migrated)
		return;
	puts("Please migrate me, then press return\n");
	migrated = true;
	(void)getchar();
}

then you can put it where needed in the code _and_ unconditionally at the end

Quoted 17 lines
s390x/migration-during-cmm.c new file +4 -0
+done:
+	report_prefix_pop();
+	return report_summary();
+}
s390x/unittests.cfg +5 -0
index 3caf81eda396..f6889bd4da01 100644
@@ -208,3 +208,8 @@ groups = migration
 [exittime]
 file = exittime.elf
 smp = 2
+
+[migration-during-cmm]
+file = migration-during-cmm.elf
+groups = migration
+smp = 2