diff mbox series

[v3,3/3] KVM: x86/pmu: Add KVM_PMU_CALL() to simplify static calls of kvm_pmu_ops

Message ID 20240425125252.48963-4-wei.w.wang@intel.com (mailing list archive)
State New, archived
Headers show
Series KVM/x86: Enhancements to static calls | expand

Commit Message

Wang, Wei W April 25, 2024, 12:52 p.m. UTC
Similar to KVM_X86_CALL(), KVM_PMU_CALL() is added to streamline the usage
of static calls of kvm_pmu_ops, which improves code readability.

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Wei Wang <wei.w.wang@intel.com>
---
 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/pmu.c              | 24 ++++++++++++------------
 2 files changed, 13 insertions(+), 12 deletions(-)

Comments

Sean Christopherson May 2, 2024, 11:36 p.m. UTC | #1
On Thu, Apr 25, 2024, Wei Wang wrote:
>  #define KVM_X86_CALL(func) static_call(kvm_x86_##func)
> +#define KVM_PMU_CALL(func) static_call(kvm_x86_pmu_##func)

...

> @@ -796,7 +796,7 @@ void kvm_pmu_init(struct kvm_vcpu *vcpu)
>  	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
>  
>  	memset(pmu, 0, sizeof(*pmu));
> -	static_call(kvm_x86_pmu_init)(vcpu);
> +	KVM_PMU_CALL(init)(vcpu);
>  	kvm_pmu_refresh(vcpu);

I usually like macros to use CAPS so that they're clearly macros, but in this
case I find the code a bit jarring.  Essentially, I *want* my to be fooled into
thinking it's a function call, because that's really what it is.

So rather than all caps, what if we follow function naming style?  E.g.

	memset(pmu, 0, sizeof(*pmu));
	kvm_pmu_call(init)(vcpu);
	kvm_pmu_refresh(vcpu);

and

	if (lapic_in_kernel(vcpu)) {
		kvm_pmu_call(deliver_pmi)(vcpu);
		kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC);
	}

and

	switch (msr) {
	case MSR_CORE_PERF_GLOBAL_STATUS:
	case MSR_CORE_PERF_GLOBAL_CTRL:
	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
		return kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu));
	default:
		break;
	}
	return kvm_pmu_call(msr_idx_to_pmc)(vcpu, msr) ||
	       kvm_pmu_call(is_valid_msr)(vcpu, msr);

all are easier for my brain to parse.
Wang, Wei W May 3, 2024, 2:15 a.m. UTC | #2
On Friday, May 3, 2024 7:36 AM, Sean Christopherson wrote:
> On Thu, Apr 25, 2024, Wei Wang wrote:
> >  #define KVM_X86_CALL(func) static_call(kvm_x86_##func)
> > +#define KVM_PMU_CALL(func) static_call(kvm_x86_pmu_##func)
> 
> ...
> 
> > @@ -796,7 +796,7 @@ void kvm_pmu_init(struct kvm_vcpu *vcpu)
> >  	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
> >
> >  	memset(pmu, 0, sizeof(*pmu));
> > -	static_call(kvm_x86_pmu_init)(vcpu);
> > +	KVM_PMU_CALL(init)(vcpu);
> >  	kvm_pmu_refresh(vcpu);
> 
> I usually like macros to use CAPS so that they're clearly macros, but in this case
> I find the code a bit jarring.  Essentially, I *want* my to be fooled into thinking
> it's a function call, because that's really what it is.
> 
> So rather than all caps, what if we follow function naming style?  E.g.

Yep, it looks good to me, and the coding-style doc mentions that "CAPITALIZED
macro names are appreciated but macros resembling functions may be named in
lower case".

To maintain consistency, maybe apply the same lower-case style for KVM_X86_CALL()?
Sean Christopherson May 3, 2024, 1:24 p.m. UTC | #3
On Fri, May 03, 2024, Wei W Wang wrote:
> On Friday, May 3, 2024 7:36 AM, Sean Christopherson wrote:
> > On Thu, Apr 25, 2024, Wei Wang wrote:
> > >  #define KVM_X86_CALL(func) static_call(kvm_x86_##func)
> > > +#define KVM_PMU_CALL(func) static_call(kvm_x86_pmu_##func)
> > 
> > ...
> > 
> > > @@ -796,7 +796,7 @@ void kvm_pmu_init(struct kvm_vcpu *vcpu)
> > >  	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
> > >
> > >  	memset(pmu, 0, sizeof(*pmu));
> > > -	static_call(kvm_x86_pmu_init)(vcpu);
> > > +	KVM_PMU_CALL(init)(vcpu);
> > >  	kvm_pmu_refresh(vcpu);
> > 
> > I usually like macros to use CAPS so that they're clearly macros, but in this case
> > I find the code a bit jarring.  Essentially, I *want* my to be fooled into thinking
> > it's a function call, because that's really what it is.
> > 
> > So rather than all caps, what if we follow function naming style?  E.g.
> 
> Yep, it looks good to me, and the coding-style doc mentions that "CAPITALIZED
> macro names are appreciated but macros resembling functions may be named in
> lower case".
> 
> To maintain consistency, maybe apply the same lower-case style for KVM_X86_CALL()?

Yeah, for sure, I should have explicitly called that out.
diff mbox series

Patch

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 90cdb7256a69..eafffc2e5732 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1853,6 +1853,7 @@  extern bool __read_mostly enable_apicv;
 extern struct kvm_x86_ops kvm_x86_ops;
 
 #define KVM_X86_CALL(func) static_call(kvm_x86_##func)
+#define KVM_PMU_CALL(func) static_call(kvm_x86_pmu_##func)
 
 #define KVM_X86_OP(func) \
 	DECLARE_STATIC_CALL(kvm_x86_##func, *(((struct kvm_x86_ops *)0)->func));
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index 6c92bc7647b3..2ec943e3d5ba 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -542,7 +542,7 @@  int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx)
 	if (!kvm_pmu_ops.check_rdpmc_early)
 		return 0;
 
-	return static_call(kvm_x86_pmu_check_rdpmc_early)(vcpu, idx);
+	return KVM_PMU_CALL(check_rdpmc_early)(vcpu, idx);
 }
 
 bool is_vmware_backdoor_pmc(u32 pmc_idx)
@@ -591,7 +591,7 @@  int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
 	if (is_vmware_backdoor_pmc(idx))
 		return kvm_pmu_rdpmc_vmware(vcpu, idx, data);
 
-	pmc = static_call(kvm_x86_pmu_rdpmc_ecx_to_pmc)(vcpu, idx, &mask);
+	pmc = KVM_PMU_CALL(rdpmc_ecx_to_pmc)(vcpu, idx, &mask);
 	if (!pmc)
 		return 1;
 
@@ -607,7 +607,7 @@  int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
 void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu)
 {
 	if (lapic_in_kernel(vcpu)) {
-		static_call(kvm_x86_pmu_deliver_pmi)(vcpu);
+		KVM_PMU_CALL(deliver_pmi)(vcpu);
 		kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC);
 	}
 }
@@ -622,14 +622,14 @@  bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 	default:
 		break;
 	}
-	return static_call(kvm_x86_pmu_msr_idx_to_pmc)(vcpu, msr) ||
-		static_call(kvm_x86_pmu_is_valid_msr)(vcpu, msr);
+	return KVM_PMU_CALL(msr_idx_to_pmc)(vcpu, msr) ||
+	       KVM_PMU_CALL(is_valid_msr)(vcpu, msr);
 }
 
 static void kvm_pmu_mark_pmc_in_use(struct kvm_vcpu *vcpu, u32 msr)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
-	struct kvm_pmc *pmc = static_call(kvm_x86_pmu_msr_idx_to_pmc)(vcpu, msr);
+	struct kvm_pmc *pmc = KVM_PMU_CALL(msr_idx_to_pmc)(vcpu, msr);
 
 	if (pmc)
 		__set_bit(pmc->idx, pmu->pmc_in_use);
@@ -654,7 +654,7 @@  int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		msr_info->data = 0;
 		break;
 	default:
-		return static_call(kvm_x86_pmu_get_msr)(vcpu, msr_info);
+		return KVM_PMU_CALL(get_msr)(vcpu, msr_info);
 	}
 
 	return 0;
@@ -713,7 +713,7 @@  int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		break;
 	default:
 		kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
-		return static_call(kvm_x86_pmu_set_msr)(vcpu, msr_info);
+		return KVM_PMU_CALL(set_msr)(vcpu, msr_info);
 	}
 
 	return 0;
@@ -740,7 +740,7 @@  static void kvm_pmu_reset(struct kvm_vcpu *vcpu)
 
 	pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status = 0;
 
-	static_call(kvm_x86_pmu_reset)(vcpu);
+	KVM_PMU_CALL(reset)(vcpu);
 }
 
 
@@ -778,7 +778,7 @@  void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (!vcpu->kvm->arch.enable_pmu)
 		return;
 
-	static_call(kvm_x86_pmu_refresh)(vcpu);
+	KVM_PMU_CALL(refresh)(vcpu);
 
 	/*
 	 * At RESET, both Intel and AMD CPUs set all enable bits for general
@@ -796,7 +796,7 @@  void kvm_pmu_init(struct kvm_vcpu *vcpu)
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 
 	memset(pmu, 0, sizeof(*pmu));
-	static_call(kvm_x86_pmu_init)(vcpu);
+	KVM_PMU_CALL(init)(vcpu);
 	kvm_pmu_refresh(vcpu);
 }
 
@@ -818,7 +818,7 @@  void kvm_pmu_cleanup(struct kvm_vcpu *vcpu)
 			pmc_stop_counter(pmc);
 	}
 
-	static_call(kvm_x86_pmu_cleanup)(vcpu);
+	KVM_PMU_CALL(cleanup)(vcpu);
 
 	bitmap_zero(pmu->pmc_in_use, X86_PMC_IDX_MAX);
 }