Skip to contentSkip to conversation

selftests: KVM: Improvements to binary stats test

PATCH Applied Oliver Upton 3 participants 6 messages Jul 19, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover selftests: KVM: Improvements to binary stats test

From: Oliver Upton <[email protected]>

Small series to improve the debuggability of the binary stats test w/ more descriptive test assertions + add coverage for boolean stats.

Applies to kvm/queue, with the following patches applied:

- 1b870fa5573e ("kvm: stats: tell userspace which values are boolean")
- https://lore.kernel.org/kvm/[email protected]/

First time sending patches from my new inbox, apologies if I've screwed something up.

Oliver Upton (3):
selftests: KVM: Check stat name before other fields
selftests: KVM: Provide descriptive assertions in
kvm_binary_stats_test
selftests: KVM: Add exponent check for boolean stats

 .../selftests/kvm/kvm_binary_stats_test.c     | 38 +++++++++++++------
 1 file changed, 27 insertions(+), 11 deletions(-)
Signature · 1 line
2.37.0.170.g444d1eabd0-goog
#1 lore raw

PATCH 1/3 selftests: KVM: Check stat name before other fields

From: Oliver Upton <[email protected]>

In order to provide more useful test assertions that describe the broken stats descriptor, perform sanity check on the stat name before any other descriptor field. While at it, avoid dereferencing the name field if the sanity check fails as it is more likely to contain garbage.

---
 tools/testing/selftests/kvm/kvm_binary_stats_test.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/kvm_binary_stats_test.c +5 -3
index b01e8b0851e7..40227ad9ba0c 100644
@@ -73,6 +73,10 @@ static void stats_test(int stats_fd)
 	for (i = 0; i < header.num_desc; ++i) {
 		pdesc = get_stats_descriptor(stats_desc, i, &header);
 
+		/* Check name string */
+		TEST_ASSERT(strlen(pdesc->name) < header.name_size,
+			    "KVM stats name (index: %d) too long", i);
+
 		/* Check type,unit,base boundaries */
 		TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK) <= KVM_STATS_TYPE_MAX,
 			    "Unknown KVM stats type");
@@ -99,9 +103,7 @@ static void stats_test(int stats_fd)
 			TEST_ASSERT(pdesc->exponent <= 0, "Unsupported KVM stats unit");
 			break;
 		}
-		/* Check name string */
-		TEST_ASSERT(strlen(pdesc->name) < header.name_size,
-			    "KVM stats name(%s) too long", pdesc->name);
+
 		/* Check size field, which should not be zero */
 		TEST_ASSERT(pdesc->size,
 			    "KVM descriptor(%s) with size of 0", pdesc->name);
Signature · 1 line
2.37.0.170.g444d1eabd0-goog
#2 lore raw

PATCH 2/3 selftests: KVM: Provide descriptive assertions in kvm_binary_stats_test

In reply to Oliver Upton

From: Oliver Upton <[email protected]>

As it turns out, tests sometimes fail. When that is the case, packing the test assertion with as much relevant information helps track down the problem more quickly.

Sharpen up the stat descriptor assertions in kvm_binary_stats_test to more precisely describe the reason for the test assertion and which stat is to blame.

---
 .../selftests/kvm/kvm_binary_stats_test.c     | 24 ++++++++++++-------
 1 file changed, 16 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/kvm_binary_stats_test.c +16 -8
index 40227ad9ba0c..3237c7c94bf0 100644
@@ -31,6 +31,7 @@ static void stats_test(int stats_fd)
 	struct kvm_stats_desc *stats_desc;
 	u64 *stats_data;
 	struct kvm_stats_desc *pdesc;
+	u32 type, unit, base;
 
 	/* Read kvm stats header */
 	read_stats_header(stats_fd, &header);
@@ -72,18 +73,21 @@ static void stats_test(int stats_fd)
 	/* Sanity check for fields in descriptors */
 	for (i = 0; i < header.num_desc; ++i) {
 		pdesc = get_stats_descriptor(stats_desc, i, &header);
+		type = pdesc->flags & KVM_STATS_TYPE_MASK;
+		unit = pdesc->flags & KVM_STATS_UNIT_MASK;
+		base = pdesc->flags & KVM_STATS_BASE_MASK;
 
 		/* Check name string */
 		TEST_ASSERT(strlen(pdesc->name) < header.name_size,
 			    "KVM stats name (index: %d) too long", i);
 
 		/* Check type,unit,base boundaries */
-		TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK) <= KVM_STATS_TYPE_MAX,
-			    "Unknown KVM stats type");
-		TEST_ASSERT((pdesc->flags & KVM_STATS_UNIT_MASK) <= KVM_STATS_UNIT_MAX,
-			    "Unknown KVM stats unit");
-		TEST_ASSERT((pdesc->flags & KVM_STATS_BASE_MASK) <= KVM_STATS_BASE_MAX,
-			    "Unknown KVM stats base");
+		TEST_ASSERT(type <= KVM_STATS_TYPE_MAX,
+			    "Unknown KVM stats (%s) type: %u", pdesc->name, type);
+		TEST_ASSERT(unit <= KVM_STATS_UNIT_MAX,
+			    "Unknown KVM stats (%s) unit: %u", pdesc->name, unit);
+		TEST_ASSERT(base <= KVM_STATS_BASE_MAX,
+			    "Unknown KVM stats (%s) base: %u", pdesc->name, base);
 
 		/*
 		 * Check exponent for stats unit
@@ -97,10 +101,14 @@ static void stats_test(int stats_fd)
 		case KVM_STATS_UNIT_NONE:
 		case KVM_STATS_UNIT_BYTES:
 		case KVM_STATS_UNIT_CYCLES:
-			TEST_ASSERT(pdesc->exponent >= 0, "Unsupported KVM stats unit");
+			TEST_ASSERT(pdesc->exponent >= 0,
+				    "Unsupported KVM stats (%s) exponent: %i",
+				    pdesc->name, pdesc->exponent);
 			break;
 		case KVM_STATS_UNIT_SECONDS:
-			TEST_ASSERT(pdesc->exponent <= 0, "Unsupported KVM stats unit");
+			TEST_ASSERT(pdesc->exponent <= 0,
+				    "Unsupported KVM stats (%s) exponent: %i",
+				    pdesc->name, pdesc->exponent);
 			break;
 		}
 
Signature · 1 line
2.37.0.170.g444d1eabd0-goog
#3 lore raw

PATCH 3/3 selftests: KVM: Add exponent check for boolean stats

In reply to Oliver Upton

From: Oliver Upton <[email protected]>

The only sensible exponent for a boolean stat is 0. Add a test assertion requiring all boolean statistics to have an exponent of 0.

---
 tools/testing/selftests/kvm/kvm_binary_stats_test.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/testing/selftests/kvm/kvm_binary_stats_test.c +6 -0
index 3237c7c94bf0..0b45ac593387 100644
@@ -96,6 +96,7 @@ static void stats_test(int stats_fd)
 		 * Exponent for unit seconds should be less than or equal to 0
 		 * Exponent for unit clock cycles should be greater than or
 		 * equal to 0
+		 * Exponent for unit boolean should be 0
 		 */
 		switch (pdesc->flags & KVM_STATS_UNIT_MASK) {
 		case KVM_STATS_UNIT_NONE:
@@ -110,6 +111,11 @@ static void stats_test(int stats_fd)
 				    "Unsupported KVM stats (%s) exponent: %i",
 				    pdesc->name, pdesc->exponent);
 			break;
+		case KVM_STATS_UNIT_BOOLEAN:
+			TEST_ASSERT(pdesc->exponent == 0,
+				    "Unsupported KVM stats (%s) exponent: %d",
+				    pdesc->name, pdesc->exponent);
+			break;
 		}
 
 		/* Check size field, which should not be zero */
Signature · 1 line
2.37.0.170.g444d1eabd0-goog
Reviewed-by on the v1 series
#4 lore raw
In reply to Oliver Upton
Quoted 13 lines · Oliver Upton

From: Oliver Upton <[email protected]>

Small series to improve the debuggability of the binary stats test w/ more descriptive test assertions + add coverage for boolean stats.

Applies to kvm/queue, with the following patches applied:

- 1b870fa5573e ("kvm: stats: tell userspace which values are boolean")
- https://lore.kernel.org/kvm/[email protected]/

First time sending patches from my new inbox, apologies if I've screwed something up.

Quoted 12 lines

Oliver Upton (3):
selftests: KVM: Check stat name before other fields
selftests: KVM: Provide descriptive assertions in
kvm_binary_stats_test
selftests: KVM: Add exponent check for boolean stats

 .../selftests/kvm/kvm_binary_stats_test.c     | 38 +++++++++++++------
 1 file changed, 27 insertions(+), 11 deletions(-)
Signature · 1 line
2.37.0.170.g444d1eabd0-goog

For the series,

maintainer Applied
#5 lore raw
Quoted 23 lines · Andrew Jones
Quoted 22 lines · Oliver Upton

From: Oliver Upton <[email protected]>

Small series to improve the debuggability of the binary stats test w/ more descriptive test assertions + add coverage for boolean stats.

Applies to kvm/queue, with the following patches applied:

- 1b870fa5573e ("kvm: stats: tell userspace which values are boolean")
- https://lore.kernel.org/kvm/[email protected]/

First time sending patches from my new inbox, apologies if I've screwed something up.

Oliver Upton (3):
selftests: KVM: Check stat name before other fields
selftests: KVM: Provide descriptive assertions in
kvm_binary_stats_test
selftests: KVM: Add exponent check for boolean stats

  .../selftests/kvm/kvm_binary_stats_test.c     | 38 +++++++++++++------
  1 file changed, 27 insertions(+), 11 deletions(-)
Quoted 8 lines
Quoted 3 lines
Signature · 1 line
2.37.0.170.g444d1eabd0-goog

For the series,

Queued, thanks.

Paolo