Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1856840rwb; Fri, 19 Aug 2022 10:31:31 -0700 (PDT) X-Google-Smtp-Source: AA6agR61y3c/L1bJehLOd3BrPAi5KyjuRNm8Zvkwa9QkmewmE2jIVva+8i+7O3Uxycgx4uqqmQQl X-Received: by 2002:a17:902:d2c1:b0:172:c1af:fd26 with SMTP id n1-20020a170902d2c100b00172c1affd26mr3805551plc.4.1660930291052; Fri, 19 Aug 2022 10:31:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660930291; cv=none; d=google.com; s=arc-20160816; b=ggqlZfpm1Rsem6pOhoPrclhtb9nazc/gPw8f2laBfgUfT0x6N3gGJkDKUq2erkFDjI 9I/y5rgCKC41tFPTNNEM3v45RFSA7lqb2ligHD2hCcL+tqbYpRrld94pgG/mrVgJ3ZQb K5qbsMmkmnNruUQQDMXb2aQnN2kKIMWe+6IL1FZXtKg1wr/CpQgQ40gC7Rz7uXo8j0SL 1l4RMoCvtfQTSRv5drLFnkWIqKY5qTAuVSHDXrHgr8qidoIWf9Wb5RwwgRLL6QMXt64p GoPuEqlM/W56LD5KMxHGS55x/QCMV3P7UYFsrjz/QmbMFRX5FCwfeADV6GKQJbhtcqyL EzUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=kK/ESDafCnoRJPk8raW2oSKZvspJXfHcZ7BzlAkknlU=; b=IFL/dvRikh+CAxyK0vt7OJJQUUIXGyB2mlmV9uv7TUCTmH/EeSU19aqEgswWLMttmT CYDjXoqCqWq48lUjx6EvSOGF5Lw0iLfAy4mxO0YLcm4i3xnBWTXhaxYE9gmbyFZqjCze zHWwSOKaKuV4rwv1x7EBM02cvs0cQc3gCpxie9r6KHjPje4Pfmwzjzsx0K+oHsnGoUam 1tKDfSlLOZ/R9ozftmwLCmy/4zFJ8H/cwcQRZvs8nVVb5Qdq76VCPggKGNdT8xfM4yQx KC0EnoCxcZzK5QFs9d2XkX2EgNXePJyjH31STbU6QgrfaxXk6s97Vwtz+RVFIZHsO1v4 kkKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=I1i12ifh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z10-20020a170903018a00b0016c009d3ca2si4941258plg.7.2022.08.19.10.31.19; Fri, 19 Aug 2022 10:31:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=I1i12ifh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354220AbiHSQtW (ORCPT + 99 others); Fri, 19 Aug 2022 12:49:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354005AbiHSQqx (ORCPT ); Fri, 19 Aug 2022 12:46:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF34312C359; Fri, 19 Aug 2022 09:12:45 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B0175618B3; Fri, 19 Aug 2022 16:12:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A3F35C433D6; Fri, 19 Aug 2022 16:12:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660925538; bh=UAT7Y9+eghMhtipisJYCxWU40Q9aQYtPksCApGHfw04=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I1i12ifhyDbQ+tpISgMweUsnT/s4gxxfjM9mMiwt/2KcJ30ocvIKlpZ5uPzXPalno BMXwP0VlmUBBzVgrQW99yoNkM5dJhaY7XzvzwC+zPs9iBvN6gSOlA751t1GPKscqeU L0HUJuDQ3GG4HHkD9G4xKINPQ7gapwhv7Iiz9cSw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jim Mattson , Paolo Bonzini , Sasha Levin Subject: [PATCH 5.10 505/545] KVM: x86/pmu: Use binary search to check filtered events Date: Fri, 19 Aug 2022 17:44:35 +0200 Message-Id: <20220819153852.083624234@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220819153829.135562864@linuxfoundation.org> References: <20220819153829.135562864@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jim Mattson [ Upstream commit 7ff775aca48adc854436b92c060e5eebfffb6a4a ] The PMU event filter may contain up to 300 events. Replace the linear search in reprogram_gp_counter() with a binary search. Signed-off-by: Jim Mattson Signed-off-by: Paolo Bonzini Message-Id: <20220115052431.447232-2-jmattson@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin --- arch/x86/kvm/pmu.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 2f83b5d948b3..350e7cdaad02 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include #include "x86.h" #include "cpuid.h" @@ -168,13 +170,17 @@ static bool pmc_resume_counter(struct kvm_pmc *pmc) return true; } +static int cmp_u64(const void *a, const void *b) +{ + return *(__u64 *)a - *(__u64 *)b; +} + void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) { u64 config; u32 type = PERF_TYPE_RAW; struct kvm *kvm = pmc->vcpu->kvm; struct kvm_pmu_event_filter *filter; - int i; bool allow_event = true; if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL) @@ -189,16 +195,13 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu); if (filter) { - for (i = 0; i < filter->nevents; i++) - if (filter->events[i] == - (eventsel & AMD64_RAW_EVENT_MASK_NB)) - break; - if (filter->action == KVM_PMU_EVENT_ALLOW && - i == filter->nevents) - allow_event = false; - if (filter->action == KVM_PMU_EVENT_DENY && - i < filter->nevents) - allow_event = false; + __u64 key = eventsel & AMD64_RAW_EVENT_MASK_NB; + + if (bsearch(&key, filter->events, filter->nevents, + sizeof(__u64), cmp_u64)) + allow_event = filter->action == KVM_PMU_EVENT_ALLOW; + else + allow_event = filter->action == KVM_PMU_EVENT_DENY; } if (!allow_event) return; @@ -507,6 +510,11 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp) /* Ensure nevents can't be changed between the user copies. */ *filter = tmp; + /* + * Sort the in-kernel list so that we can search it with bsearch. + */ + sort(&filter->events, filter->nevents, sizeof(__u64), cmp_u64, NULL); + mutex_lock(&kvm->lock); filter = rcu_replace_pointer(kvm->arch.pmu_event_filter, filter, mutex_is_locked(&kvm->lock)); -- 2.35.1