diff mbox series

[v2,1/7] KVM: selftests: Replace int with uint32_t for nevents

Message ID 20230420104622.12504-2-ljrcore@126.com (mailing list archive)
State New
Headers show
Series KVM: selftests: Add tests for pmu event filter | expand

Commit Message

Jinrong Liang April 20, 2023, 10:46 a.m. UTC
From: Jinrong Liang <cloudliang@tencent.com>

From: Jinrong Liang <cloudliang@tencent.com>

Defined as type __u32, the nevents field in kvm_pmu_event_filter
can only accept positive values within a specific range. Therefore,
replacing int with uint32_t for nevents ensures consistency and
readability in the code. This change has been tested and verified
to work correctly with all relevant code.

Signed-off-by: Jinrong Liang <cloudliang@tencent.com>
---
 .../selftests/kvm/x86_64/pmu_event_filter_test.c     | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Comments

Sean Christopherson May 25, 2023, 4:23 p.m. UTC | #1
On Thu, Apr 20, 2023, Jinrong Liang wrote:
> From: Jinrong Liang <cloudliang@tencent.com>
> 
> From: Jinrong Liang <cloudliang@tencent.com>
> 
> Defined as type __u32, the nevents field in kvm_pmu_event_filter
> can only accept positive values within a specific range. Therefore,
> replacing int with uint32_t for nevents ensures consistency and
> readability in the code.

Not really.  It fixes one type of inconsistency that is fairly common (userspace
passing an integer count to the kernel), and replaces it with a different type
of inconsistency (signed iterator comparing against an unsigned count).  There's
already one of those in remove_event(), but I'd rather not create more.

Passing an unsigned int to track what *should* be a small-ish, postive integer
can also make it more difficult to detect bugs, e.g. assertions like this won't
work:

	TEST_ASSERT(nevents >= 0);

If this code were being written from scratch then I wouldn't object to using
uint32_t everywhere, but I don't see the point of trying to retroactively change
the code.
diff mbox series

Patch

diff --git a/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c b/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
index 1f60dfae69e0..c0521fc9e8f6 100644
--- a/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
+++ b/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
@@ -194,7 +194,7 @@  static struct kvm_pmu_event_filter *alloc_pmu_event_filter(uint32_t nevents)
 
 
 static struct kvm_pmu_event_filter *
-create_pmu_event_filter(const uint64_t event_list[], int nevents,
+create_pmu_event_filter(const uint64_t event_list[], uint32_t nevents,
 			uint32_t action, uint32_t flags)
 {
 	struct kvm_pmu_event_filter *f;
@@ -648,7 +648,7 @@  const struct masked_events_test test_cases[] = {
 };
 
 static int append_test_events(const struct masked_events_test *test,
-			      uint64_t *events, int nevents)
+			      uint64_t *events, uint32_t nevents)
 {
 	const uint64_t *evts;
 	int i;
@@ -670,7 +670,7 @@  static bool bool_eq(bool a, bool b)
 }
 
 static void run_masked_events_tests(struct kvm_vcpu *vcpu, uint64_t *events,
-				    int nevents)
+				    uint32_t nevents)
 {
 	int ntests = ARRAY_SIZE(test_cases);
 	struct perf_counter c;
@@ -695,7 +695,7 @@  static void run_masked_events_tests(struct kvm_vcpu *vcpu, uint64_t *events,
 	}
 }
 
-static void add_dummy_events(uint64_t *events, int nevents)
+static void add_dummy_events(uint64_t *events, uint32_t nevents)
 {
 	int i;
 
@@ -714,7 +714,7 @@  static void add_dummy_events(uint64_t *events, int nevents)
 
 static void test_masked_events(struct kvm_vcpu *vcpu)
 {
-	int nevents = MAX_FILTER_EVENTS - MAX_TEST_EVENTS;
+	uint32_t nevents = MAX_FILTER_EVENTS - MAX_TEST_EVENTS;
 	uint64_t events[MAX_FILTER_EVENTS];
 
 	/* Run the test cases against a sparse PMU event filter. */
@@ -726,7 +726,7 @@  static void test_masked_events(struct kvm_vcpu *vcpu)
 }
 
 static int run_filter_test(struct kvm_vcpu *vcpu, const uint64_t *events,
-			   int nevents, uint32_t flags)
+			   uint32_t nevents, uint32_t flags)
 {
 	struct kvm_pmu_event_filter *f;
 	int r;