Skip to content

Commit e6cd31f

Browse files
jsmattsonjrbonzini
authored andcommitted
kvm: x86: Convert return type of *is_valid_rdpmc_ecx() to bool
These function names sound like predicates, and they have siblings, *is_valid_msr(), which _are_ predicates. Moreover, there are comments that essentially warn that these functions behave unexpectedly. Flip the polarity of the return values, so that they become predicates, and convert the boolean result to a success/failure code at the outer call site. Suggested-by: Sean Christopherson <[email protected]> Signed-off-by: Jim Mattson <[email protected]> Reviewed-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 7e2175e commit e6cd31f

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

arch/x86/kvm/pmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
319319
}
320320

321321
/* check if idx is a valid index to access PMU */
322-
int kvm_pmu_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
322+
bool kvm_pmu_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
323323
{
324324
return kvm_x86_ops.pmu_ops->is_valid_rdpmc_ecx(vcpu, idx);
325325
}

arch/x86/kvm/pmu.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct kvm_pmu_ops {
3232
struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu,
3333
unsigned int idx, u64 *mask);
3434
struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, u32 msr);
35-
int (*is_valid_rdpmc_ecx)(struct kvm_vcpu *vcpu, unsigned int idx);
35+
bool (*is_valid_rdpmc_ecx)(struct kvm_vcpu *vcpu, unsigned int idx);
3636
bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr);
3737
int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
3838
int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
@@ -149,7 +149,7 @@ void reprogram_counter(struct kvm_pmu *pmu, int pmc_idx);
149149
void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);
150150
void kvm_pmu_handle_event(struct kvm_vcpu *vcpu);
151151
int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
152-
int kvm_pmu_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx);
152+
bool kvm_pmu_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx);
153153
bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr);
154154
int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
155155
int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info);

arch/x86/kvm/svm/pmu.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,13 @@ static struct kvm_pmc *amd_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx)
181181
return get_gp_pmc_amd(pmu, base + pmc_idx, PMU_TYPE_COUNTER);
182182
}
183183

184-
/* returns 0 if idx's corresponding MSR exists; otherwise returns 1. */
185-
static int amd_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
184+
static bool amd_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
186185
{
187186
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
188187

189188
idx &= ~(3u << 30);
190189

191-
return (idx >= pmu->nr_arch_gp_counters);
190+
return idx < pmu->nr_arch_gp_counters;
192191
}
193192

194193
/* idx is the ECX register of RDPMC instruction */

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,15 @@ static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx)
118118
}
119119
}
120120

121-
/* returns 0 if idx's corresponding MSR exists; otherwise returns 1. */
122-
static int intel_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
121+
static bool intel_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
123122
{
124123
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
125124
bool fixed = idx & (1u << 30);
126125

127126
idx &= ~(3u << 30);
128127

129-
return (!fixed && idx >= pmu->nr_arch_gp_counters) ||
130-
(fixed && idx >= pmu->nr_arch_fixed_counters);
128+
return fixed ? idx < pmu->nr_arch_fixed_counters
129+
: idx < pmu->nr_arch_gp_counters;
131130
}
132131

133132
static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,

arch/x86/kvm/x86.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7476,7 +7476,9 @@ static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
74767476
static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
74777477
u32 pmc)
74787478
{
7479-
return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
7479+
if (kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc))
7480+
return 0;
7481+
return -EINVAL;
74807482
}
74817483

74827484
static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,

0 commit comments

Comments
 (0)