Skip to content

Commit fa32233

Browse files
suomilewissean-jc
authored andcommitted
KVM: selftests: Add helpers for PMC asserts in PMU event filter test
Add helper macros to consolidate the asserts that a PMC is/isn't counting (branch) instructions retired. This will make it easier to add additional asserts related to counting instructions later on. No functional changes intended. Signed-off-by: Aaron Lewis <aaronlewis@google.com> [sean: add "INSTRUCTIONS", massage changelog] Link: https://lore.kernel.org/r/20230407233254.957013-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 33ef141 commit fa32233

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,27 @@ static struct kvm_pmu_event_filter *remove_event(struct kvm_pmu_event_filter *f,
244244
return f;
245245
}
246246

247+
#define ASSERT_PMC_COUNTING_INSTRUCTIONS(count) \
248+
do { \
249+
if (count != NUM_BRANCHES) \
250+
pr_info("%s: Branch instructions retired = %lu (expected %u)\n", \
251+
__func__, count, NUM_BRANCHES); \
252+
TEST_ASSERT(count, "Allowed PMU event is not counting."); \
253+
} while (0)
254+
255+
#define ASSERT_PMC_NOT_COUNTING_INSTRUCTIONS(count) \
256+
do { \
257+
if (count) \
258+
pr_info("%s: Branch instructions retired = %lu (expected 0)\n", \
259+
__func__, count); \
260+
TEST_ASSERT(!count, "Disallowed PMU Event is counting"); \
261+
} while (0)
262+
247263
static void test_without_filter(struct kvm_vcpu *vcpu)
248264
{
249265
uint64_t count = run_vcpu_to_sync(vcpu);
250266

251-
if (count != NUM_BRANCHES)
252-
pr_info("%s: Branch instructions retired = %lu (expected %u)\n",
253-
__func__, count, NUM_BRANCHES);
254-
TEST_ASSERT(count, "Allowed PMU event is not counting");
267+
ASSERT_PMC_COUNTING_INSTRUCTIONS(count);
255268
}
256269

257270
static uint64_t test_with_filter(struct kvm_vcpu *vcpu,
@@ -269,12 +282,9 @@ static void test_amd_deny_list(struct kvm_vcpu *vcpu)
269282

270283
f = create_pmu_event_filter(&event, 1, KVM_PMU_EVENT_DENY, 0);
271284
count = test_with_filter(vcpu, f);
272-
273285
free(f);
274-
if (count != NUM_BRANCHES)
275-
pr_info("%s: Branch instructions retired = %lu (expected %u)\n",
276-
__func__, count, NUM_BRANCHES);
277-
TEST_ASSERT(count, "Allowed PMU event is not counting");
286+
287+
ASSERT_PMC_COUNTING_INSTRUCTIONS(count);
278288
}
279289

280290
static void test_member_deny_list(struct kvm_vcpu *vcpu)
@@ -283,10 +293,8 @@ static void test_member_deny_list(struct kvm_vcpu *vcpu)
283293
uint64_t count = test_with_filter(vcpu, f);
284294

285295
free(f);
286-
if (count)
287-
pr_info("%s: Branch instructions retired = %lu (expected 0)\n",
288-
__func__, count);
289-
TEST_ASSERT(!count, "Disallowed PMU Event is counting");
296+
297+
ASSERT_PMC_NOT_COUNTING_INSTRUCTIONS(count);
290298
}
291299

292300
static void test_member_allow_list(struct kvm_vcpu *vcpu)
@@ -295,10 +303,8 @@ static void test_member_allow_list(struct kvm_vcpu *vcpu)
295303
uint64_t count = test_with_filter(vcpu, f);
296304

297305
free(f);
298-
if (count != NUM_BRANCHES)
299-
pr_info("%s: Branch instructions retired = %lu (expected %u)\n",
300-
__func__, count, NUM_BRANCHES);
301-
TEST_ASSERT(count, "Allowed PMU event is not counting");
306+
307+
ASSERT_PMC_COUNTING_INSTRUCTIONS(count);
302308
}
303309

304310
static void test_not_member_deny_list(struct kvm_vcpu *vcpu)
@@ -310,10 +316,8 @@ static void test_not_member_deny_list(struct kvm_vcpu *vcpu)
310316
remove_event(f, AMD_ZEN_BR_RETIRED);
311317
count = test_with_filter(vcpu, f);
312318
free(f);
313-
if (count != NUM_BRANCHES)
314-
pr_info("%s: Branch instructions retired = %lu (expected %u)\n",
315-
__func__, count, NUM_BRANCHES);
316-
TEST_ASSERT(count, "Allowed PMU event is not counting");
319+
320+
ASSERT_PMC_COUNTING_INSTRUCTIONS(count);
317321
}
318322

319323
static void test_not_member_allow_list(struct kvm_vcpu *vcpu)
@@ -325,10 +329,8 @@ static void test_not_member_allow_list(struct kvm_vcpu *vcpu)
325329
remove_event(f, AMD_ZEN_BR_RETIRED);
326330
count = test_with_filter(vcpu, f);
327331
free(f);
328-
if (count)
329-
pr_info("%s: Branch instructions retired = %lu (expected 0)\n",
330-
__func__, count);
331-
TEST_ASSERT(!count, "Disallowed PMU Event is counting");
332+
333+
ASSERT_PMC_NOT_COUNTING_INSTRUCTIONS(count);
332334
}
333335

334336
/*

0 commit comments

Comments
 (0)