On Wed, Aug 31, 2022 at 9:21 AM Aaron Lewis <aaronlewis@xxxxxxxxxx> wrote: > > Create an internal representation for filter events to abstract the > events userspace uses from the events the kernel uses. That will allow > the kernel to use a common event and a common code path between the > different types of filter events used in userspace once masked events > are introduced. > > No functional changes intended > > Signed-off-by: Aaron Lewis <aaronlewis@xxxxxxxxxx> > --- > arch/x86/kvm/pmu.c | 118 ++++++++++++++++++++++++++++++++------------- > arch/x86/kvm/pmu.h | 16 ++++++ > 2 files changed, 100 insertions(+), 34 deletions(-) > > diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c > index e7d94e6b7f28..50a36cc5bfd0 100644 > --- a/arch/x86/kvm/pmu.c > +++ b/arch/x86/kvm/pmu.c > @@ -239,6 +239,19 @@ static bool pmc_resume_counter(struct kvm_pmc *pmc) > return true; > } > > +static inline u16 get_event_select(u64 eventsel) > +{ > + u64 e = eventsel & > + static_call(kvm_x86_pmu_get_eventsel_event_mask)(); > + > + return (e & ARCH_PERFMON_EVENTSEL_EVENT) | ((e >> 24) & 0xF00ULL); > +} > + > +static inline u8 get_unit_mask(u64 eventsel) > +{ > + return (eventsel & ARCH_PERFMON_EVENTSEL_UMASK) >> 8; > +} > + > static int cmp_u64(const void *pa, const void *pb) > { > u64 a = *(u64 *)pa; > @@ -247,53 +260,63 @@ static int cmp_u64(const void *pa, const void *pb) > return (a > b) - (a < b); > } > > -static inline u64 get_event_select(u64 eventsel) > +static u64 *find_filter_entry(struct kvm_pmu_event_filter *filter, u64 key) > +{ > + return bsearch(&key, filter->events, filter->nevents, > + sizeof(filter->events[0]), cmp_u64); > +} > + > +static bool filter_contains_match(struct kvm_pmu_event_filter *filter, > + u64 eventsel) > +{ > + u16 event_select = get_event_select(eventsel); > + u8 unit_mask = get_unit_mask(eventsel); > + u64 key; > + > + key = KVM_PMU_ENCODE_FILTER_ENTRY(event_select, unit_mask); > + if (find_filter_entry(filter, key)) > + return true; > + return false; Perhaps just: return find_filter_entry(filter, key); Reviewed-by: Jim Mattson <jmattson@xxxxxxxxxx>