* [PATCH v2] KVM: x86/pmu: Manipulate FIXED_CTR_CTRL MSR with macros
@ 2023-08-15 3:28 Dapeng Mi
2023-08-21 8:05 ` Like Xu
0 siblings, 1 reply; 3+ messages in thread
From: Dapeng Mi @ 2023-08-15 3:28 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Kan Liang, Like Xu
Cc: kvm, linux-perf-users, linux-kernel, Zhenyu Wang, Zhang Xiong,
Lv Zhiyuan, Dapeng Mi, Dapeng Mi
Magic numbers are used to manipulate the bit fields of
FIXED_CTR_CTRL MSR. This is not read-friendly and use macros to replace
these magic numbers to increase the readability.
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/kvm/pmu.c | 10 +++++-----
arch/x86/kvm/pmu.h | 6 ++++--
2 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index edb89b51b383..fb4ef2da3e32 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -420,11 +420,11 @@ static void reprogram_counter(struct kvm_pmc *pmc)
if (pmc_is_fixed(pmc)) {
fixed_ctr_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl,
pmc->idx - INTEL_PMC_IDX_FIXED);
- if (fixed_ctr_ctrl & 0x1)
+ if (fixed_ctr_ctrl & INTEL_FIXED_0_KERNEL)
eventsel |= ARCH_PERFMON_EVENTSEL_OS;
- if (fixed_ctr_ctrl & 0x2)
+ if (fixed_ctr_ctrl & INTEL_FIXED_0_USER)
eventsel |= ARCH_PERFMON_EVENTSEL_USR;
- if (fixed_ctr_ctrl & 0x8)
+ if (fixed_ctr_ctrl & INTEL_FIXED_0_ENABLE_PMI)
eventsel |= ARCH_PERFMON_EVENTSEL_INT;
new_config = (u64)fixed_ctr_ctrl;
}
@@ -749,8 +749,8 @@ static inline bool cpl_is_matched(struct kvm_pmc *pmc)
} else {
config = fixed_ctrl_field(pmc_to_pmu(pmc)->fixed_ctr_ctrl,
pmc->idx - INTEL_PMC_IDX_FIXED);
- select_os = config & 0x1;
- select_user = config & 0x2;
+ select_os = config & INTEL_FIXED_0_KERNEL;
+ select_user = config & INTEL_FIXED_0_USER;
}
return (static_call(kvm_x86_get_cpl)(pmc->vcpu) == 0) ? select_os : select_user;
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 7d9ba301c090..ffda2ecc3a22 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -12,7 +12,8 @@
MSR_IA32_MISC_ENABLE_BTS_UNAVAIL)
/* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */
-#define fixed_ctrl_field(ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0xf)
+#define fixed_ctrl_field(ctrl_reg, idx) \
+ (((ctrl_reg) >> ((idx) * INTEL_FIXED_BITS_STRIDE)) & INTEL_FIXED_BITS_MASK)
#define VMWARE_BACKDOOR_PMC_HOST_TSC 0x10000
#define VMWARE_BACKDOOR_PMC_REAL_TIME 0x10001
@@ -165,7 +166,8 @@ static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc)
if (pmc_is_fixed(pmc))
return fixed_ctrl_field(pmu->fixed_ctr_ctrl,
- pmc->idx - INTEL_PMC_IDX_FIXED) & 0x3;
+ pmc->idx - INTEL_PMC_IDX_FIXED) &
+ (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER);
return pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE;
}
base-commit: 240f736891887939571854bd6d734b6c9291f22e
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] KVM: x86/pmu: Manipulate FIXED_CTR_CTRL MSR with macros
2023-08-15 3:28 [PATCH v2] KVM: x86/pmu: Manipulate FIXED_CTR_CTRL MSR with macros Dapeng Mi
@ 2023-08-21 8:05 ` Like Xu
2023-08-23 2:22 ` Mi, Dapeng
0 siblings, 1 reply; 3+ messages in thread
From: Like Xu @ 2023-08-21 8:05 UTC (permalink / raw)
To: Dapeng Mi
Cc: kvm, linux-perf-users, linux-kernel, Zhenyu Wang, Zhang Xiong,
Lv Zhiyuan, Dapeng Mi, Sean Christopherson, Paolo Bonzini,
Kan Liang
On 15/8/2023 11:28 am, Dapeng Mi wrote:
> Magic numbers are used to manipulate the bit fields of
> FIXED_CTR_CTRL MSR. This is not read-friendly and use macros to replace
> these magic numbers to increase the readability.
More, reuse INTEL_FIXED_0_* macros for pmu->fixed_ctr_ctrl_mask, pls.
>
> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
> ---
> arch/x86/kvm/pmu.c | 10 +++++-----
> arch/x86/kvm/pmu.h | 6 ++++--
> 2 files changed, 9 insertions(+), 7 deletions(-)
>
> diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
> index edb89b51b383..fb4ef2da3e32 100644
> --- a/arch/x86/kvm/pmu.c
> +++ b/arch/x86/kvm/pmu.c
> @@ -420,11 +420,11 @@ static void reprogram_counter(struct kvm_pmc *pmc)
> if (pmc_is_fixed(pmc)) {
> fixed_ctr_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl,
> pmc->idx - INTEL_PMC_IDX_FIXED);
> - if (fixed_ctr_ctrl & 0x1)
> + if (fixed_ctr_ctrl & INTEL_FIXED_0_KERNEL)
> eventsel |= ARCH_PERFMON_EVENTSEL_OS;
> - if (fixed_ctr_ctrl & 0x2)
> + if (fixed_ctr_ctrl & INTEL_FIXED_0_USER)
> eventsel |= ARCH_PERFMON_EVENTSEL_USR;
> - if (fixed_ctr_ctrl & 0x8)
> + if (fixed_ctr_ctrl & INTEL_FIXED_0_ENABLE_PMI)
> eventsel |= ARCH_PERFMON_EVENTSEL_INT;
> new_config = (u64)fixed_ctr_ctrl;
> }
> @@ -749,8 +749,8 @@ static inline bool cpl_is_matched(struct kvm_pmc *pmc)
> } else {
> config = fixed_ctrl_field(pmc_to_pmu(pmc)->fixed_ctr_ctrl,
> pmc->idx - INTEL_PMC_IDX_FIXED);
> - select_os = config & 0x1;
> - select_user = config & 0x2;
> + select_os = config & INTEL_FIXED_0_KERNEL;
> + select_user = config & INTEL_FIXED_0_USER;
> }
>
> return (static_call(kvm_x86_get_cpl)(pmc->vcpu) == 0) ? select_os : select_user;
> diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
> index 7d9ba301c090..ffda2ecc3a22 100644
> --- a/arch/x86/kvm/pmu.h
> +++ b/arch/x86/kvm/pmu.h
> @@ -12,7 +12,8 @@
> MSR_IA32_MISC_ENABLE_BTS_UNAVAIL)
>
> /* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */
> -#define fixed_ctrl_field(ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0xf)
> +#define fixed_ctrl_field(ctrl_reg, idx) \
> + (((ctrl_reg) >> ((idx) * INTEL_FIXED_BITS_STRIDE)) & INTEL_FIXED_BITS_MASK)
>
> #define VMWARE_BACKDOOR_PMC_HOST_TSC 0x10000
> #define VMWARE_BACKDOOR_PMC_REAL_TIME 0x10001
> @@ -165,7 +166,8 @@ static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc)
>
> if (pmc_is_fixed(pmc))
> return fixed_ctrl_field(pmu->fixed_ctr_ctrl,
> - pmc->idx - INTEL_PMC_IDX_FIXED) & 0x3;
> + pmc->idx - INTEL_PMC_IDX_FIXED) &
> + (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER);
>
> return pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE;
> }
>
> base-commit: 240f736891887939571854bd6d734b6c9291f22e
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] KVM: x86/pmu: Manipulate FIXED_CTR_CTRL MSR with macros
2023-08-21 8:05 ` Like Xu
@ 2023-08-23 2:22 ` Mi, Dapeng
0 siblings, 0 replies; 3+ messages in thread
From: Mi, Dapeng @ 2023-08-23 2:22 UTC (permalink / raw)
To: Like Xu
Cc: kvm, linux-perf-users, linux-kernel, Zhenyu Wang, Zhang Xiong,
Lv Zhiyuan, Dapeng Mi, Sean Christopherson, Paolo Bonzini,
Kan Liang
On 8/21/2023 4:05 PM, Like Xu wrote:
> On 15/8/2023 11:28 am, Dapeng Mi wrote:
>> Magic numbers are used to manipulate the bit fields of
>> FIXED_CTR_CTRL MSR. This is not read-friendly and use macros to replace
>> these magic numbers to increase the readability.
>
> More, reuse INTEL_FIXED_0_* macros for pmu->fixed_ctr_ctrl_mask, pls.
Sure. Thanks.
>
>>
>> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
>> ---
>> arch/x86/kvm/pmu.c | 10 +++++-----
>> arch/x86/kvm/pmu.h | 6 ++++--
>> 2 files changed, 9 insertions(+), 7 deletions(-)
>>
>> diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
>> index edb89b51b383..fb4ef2da3e32 100644
>> --- a/arch/x86/kvm/pmu.c
>> +++ b/arch/x86/kvm/pmu.c
>> @@ -420,11 +420,11 @@ static void reprogram_counter(struct kvm_pmc *pmc)
>> if (pmc_is_fixed(pmc)) {
>> fixed_ctr_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl,
>> pmc->idx - INTEL_PMC_IDX_FIXED);
>> - if (fixed_ctr_ctrl & 0x1)
>> + if (fixed_ctr_ctrl & INTEL_FIXED_0_KERNEL)
>> eventsel |= ARCH_PERFMON_EVENTSEL_OS;
>> - if (fixed_ctr_ctrl & 0x2)
>> + if (fixed_ctr_ctrl & INTEL_FIXED_0_USER)
>> eventsel |= ARCH_PERFMON_EVENTSEL_USR;
>> - if (fixed_ctr_ctrl & 0x8)
>> + if (fixed_ctr_ctrl & INTEL_FIXED_0_ENABLE_PMI)
>> eventsel |= ARCH_PERFMON_EVENTSEL_INT;
>> new_config = (u64)fixed_ctr_ctrl;
>> }
>> @@ -749,8 +749,8 @@ static inline bool cpl_is_matched(struct kvm_pmc
>> *pmc)
>> } else {
>> config = fixed_ctrl_field(pmc_to_pmu(pmc)->fixed_ctr_ctrl,
>> pmc->idx - INTEL_PMC_IDX_FIXED);
>> - select_os = config & 0x1;
>> - select_user = config & 0x2;
>> + select_os = config & INTEL_FIXED_0_KERNEL;
>> + select_user = config & INTEL_FIXED_0_USER;
>> }
>> return (static_call(kvm_x86_get_cpl)(pmc->vcpu) == 0) ?
>> select_os : select_user;
>> diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
>> index 7d9ba301c090..ffda2ecc3a22 100644
>> --- a/arch/x86/kvm/pmu.h
>> +++ b/arch/x86/kvm/pmu.h
>> @@ -12,7 +12,8 @@
>> MSR_IA32_MISC_ENABLE_BTS_UNAVAIL)
>> /* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */
>> -#define fixed_ctrl_field(ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) &
>> 0xf)
>> +#define fixed_ctrl_field(ctrl_reg, idx) \
>> + (((ctrl_reg) >> ((idx) * INTEL_FIXED_BITS_STRIDE)) &
>> INTEL_FIXED_BITS_MASK)
>> #define VMWARE_BACKDOOR_PMC_HOST_TSC 0x10000
>> #define VMWARE_BACKDOOR_PMC_REAL_TIME 0x10001
>> @@ -165,7 +166,8 @@ static inline bool pmc_speculative_in_use(struct
>> kvm_pmc *pmc)
>> if (pmc_is_fixed(pmc))
>> return fixed_ctrl_field(pmu->fixed_ctr_ctrl,
>> - pmc->idx - INTEL_PMC_IDX_FIXED) & 0x3;
>> + pmc->idx - INTEL_PMC_IDX_FIXED) &
>> + (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER);
>> return pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE;
>> }
>>
>> base-commit: 240f736891887939571854bd6d734b6c9291f22e
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-08-23 2:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-15 3:28 [PATCH v2] KVM: x86/pmu: Manipulate FIXED_CTR_CTRL MSR with macros Dapeng Mi
2023-08-21 8:05 ` Like Xu
2023-08-23 2:22 ` Mi, Dapeng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).