Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1548055rda; Mon, 23 Oct 2023 16:41:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHhyo5AiENeAiI7N8BRNpnKB2VFtfPnwPyLENQARtfuug2CASHuzYsht88QkxCOpIm95Vus X-Received: by 2002:a05:6870:9e96:b0:1ea:5ae0:bd81 with SMTP id pu22-20020a0568709e9600b001ea5ae0bd81mr14844758oab.4.1698104500131; Mon, 23 Oct 2023 16:41:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698104500; cv=none; d=google.com; s=arc-20160816; b=M1FsCOJ7ldbtaovOfmPnvNBF/ZgY13LAzRQcIkpbUeU0jqPXH4hQ0ODK2vj5QC98kw 8GkQBpgExFcfSe5hZQJGQDgFulQIdWQEfVtt8PvpuzMM0g/Z6frYRsyFFBekW4zfkNDo EYxJA6wkWpP5bzEiL09HIIt13g+wy9s83m3vYxmMwcFULloJxoqc4/yH01fzo9ORAYln S6KlvWqCNb/Vq6y8OyEdZCD3sF0/6JXM39gGAKYoQqGtyLs9pns00FPcU9FV9gNOrHC9 Z7muFy91e83fZT3CEgt5L0BvuzjCXZI6VwiTm8BQSA+ALIwwAM0rr4fjNH03yk7gIhnb LgHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=OISog/o/YxfGLkUORpMvb/M1MSKf5KJE5wnNSowczuI=; fh=t7gFcWLRyAqmyldGbr6d3KgIXGxOYU/i61iCoIU6S88=; b=fVtJ6Oocajr4n0I0v2EvGofLGUclu7i6rNUfX6r0P4OKzclK5gNZCQh7Ky7RT3JVIz sDvcJLYu/VDHrJ5i9yflCwoOhVjFQMHiP0e1MTECeH+xQW9kw0nMuHYsDe97kLnIjlAq FseaXKsPv8+GaWwnGm1RlHsJo37sR1JAOh4A42ypEthVXZ1csBx6jabgSvLr9nPIUK3I jsTYJ4C6Pqr6WbvjE4b8BeTL9TPdsG69pnVVuQ11qgNzxSZKirdocySXn8nF43rHk41X oAgcQqjmffwaYKPzainwm985xUjKjIDVH/16k9wLlj0b2N3EQKrX7dU20QHZFjuKgAUr 16Iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wLXVmR9U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id s1-20020a63f041000000b00584b2a790absi7088983pgj.821.2023.10.23.16.41.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 16:41:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wLXVmR9U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 0888A805EAFB; Mon, 23 Oct 2023 16:41:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231898AbjJWXke (ORCPT + 99 others); Mon, 23 Oct 2023 19:40:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231694AbjJWXkR (ORCPT ); Mon, 23 Oct 2023 19:40:17 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B85810D3 for ; Mon, 23 Oct 2023 16:40:13 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9a528c2c8bso4710513276.1 for ; Mon, 23 Oct 2023 16:40:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698104412; x=1698709212; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=OISog/o/YxfGLkUORpMvb/M1MSKf5KJE5wnNSowczuI=; b=wLXVmR9UP/bjufj9VE+LKQaDenAFjAQxbqE1P3oYxr3WlAFfhMZ8PadwSS32blTuOT HflzfbuPUK6FudK3LQWK9bH2PGepgLbk+EQFjzInzWZVgrYQebPVIomaKLwBvP2pJ37X ldKgt69Y/wAvI0y/rEt22TYnLTN1PNRHQQVIVpjAu+BuI+ANZyOSGu/OSsV8zVqu4Y2i 99UIBrBCiOcqZ/hFVnA7JyyFOdaszzo+wiKlgbKvDNME9MW1A58+xKVGv6Vfrc58IbCx zDdT4XymXvR4LtkHxfGTjrNs9pnKqhXa90YdXfi7kDuCGJ1mOXx3V7ZgiOiLSyNySAB3 j4Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698104412; x=1698709212; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OISog/o/YxfGLkUORpMvb/M1MSKf5KJE5wnNSowczuI=; b=l/1chyc9U7mTGclhPFwfiCY46/ORTKzO6j2Voq244nJ3dALursE4c338aMosz5ESrQ qVYDF0AEgCUCwxN+PcSIOhj5AKaexNw+CX7AStpNZ5sswF7jVlof1GcxQdHgRGFjATTY 44NJ6g5sPIwBCeoQi7PlA017PcP3QVQrSYKp/ZWvl6Dof2fFrMH6Jvw8sNn3Spd6uorb lS9G5TK/wFxfn3u9vcdPwUqGj9rG3cnGXFxYHOuZXU2sfLwc9a5ChsfeuqmysUrIoN0+ rTcdtIcuzo/VkxLJjNEFygH9ALRqalPjtgkgE9Flt5ZpEMG87p5m0Pz5iJ/t/22DuAw6 oerA== X-Gm-Message-State: AOJu0Yy7jRXxaAF3fdUMrG0JUxtqJvJMY/aS8uygfkjhdJQgDAVHS3pn 8BSCx4JZdNli8GakBAyxQqMXhfODlxU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:da8d:0:b0:d9b:454d:352d with SMTP id n135-20020a25da8d000000b00d9b454d352dmr217208ybf.9.1698104412611; Mon, 23 Oct 2023 16:40:12 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 23 Oct 2023 16:39:59 -0700 In-Reply-To: <20231023234000.2499267-1-seanjc@google.com> Mime-Version: 1.0 References: <20231023234000.2499267-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Message-ID: <20231023234000.2499267-6-seanjc@google.com> Subject: [PATCH 5/6] KVM: x86/pmu: Update sample period in pmc_write_counter() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Mingwei Zhang , Roman Kagan , Jim Mattson , Dapeng Mi , Like Xu Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 23 Oct 2023 16:41:01 -0700 (PDT) Update a PMC's sample period in pmc_write_counter() to deduplicate code across all callers of pmc_write_counter(). Opportunistically move pmc_write_counter() into pmc.c now that it's doing more work. WRMSR isn't such a hot path that an extra CALL+RET pair will be problematic, and the order of function definitions needs to be changed anyways, i.e. now is a convenient time to eat the churn. Signed-off-by: Sean Christopherson --- arch/x86/kvm/pmu.c | 27 +++++++++++++++++++++++++++ arch/x86/kvm/pmu.h | 25 +------------------------ arch/x86/kvm/svm/pmu.c | 1 - arch/x86/kvm/vmx/pmu_intel.c | 2 -- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index c06090196b00..3725d001239d 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -161,6 +161,15 @@ static u64 pmc_get_pebs_precise_level(struct kvm_pmc *pmc) return 1; } +static u64 get_sample_period(struct kvm_pmc *pmc, u64 counter_value) +{ + u64 sample_period = (-counter_value) & pmc_bitmask(pmc); + + if (!sample_period) + sample_period = pmc_bitmask(pmc) + 1; + return sample_period; +} + static int pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, u64 config, bool exclude_user, bool exclude_kernel, bool intr) @@ -268,6 +277,24 @@ static void pmc_stop_counter(struct kvm_pmc *pmc) } } +static void pmc_update_sample_period(struct kvm_pmc *pmc) +{ + if (!pmc->perf_event || pmc->is_paused || + !is_sampling_event(pmc->perf_event)) + return; + + perf_event_period(pmc->perf_event, + get_sample_period(pmc, pmc->counter)); +} + +void pmc_write_counter(struct kvm_pmc *pmc, u64 val) +{ + pmc->counter += val - pmc_read_counter(pmc); + pmc->counter &= pmc_bitmask(pmc); + pmc_update_sample_period(pmc); +} +EXPORT_SYMBOL_GPL(pmc_write_counter); + static int filter_cmp(const void *pa, const void *pb, u64 mask) { u64 a = *(u64 *)pa & mask; diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index db9a12c0a2ef..cae85e550f60 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -74,11 +74,7 @@ static inline u64 pmc_read_counter(struct kvm_pmc *pmc) return counter & pmc_bitmask(pmc); } -static inline void pmc_write_counter(struct kvm_pmc *pmc, u64 val) -{ - pmc->counter += val - pmc_read_counter(pmc); - pmc->counter &= pmc_bitmask(pmc); -} +void pmc_write_counter(struct kvm_pmc *pmc, u64 val); static inline bool pmc_is_gp(struct kvm_pmc *pmc) { @@ -128,25 +124,6 @@ static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr) return NULL; } -static inline u64 get_sample_period(struct kvm_pmc *pmc, u64 counter_value) -{ - u64 sample_period = (-counter_value) & pmc_bitmask(pmc); - - if (!sample_period) - sample_period = pmc_bitmask(pmc) + 1; - return sample_period; -} - -static inline void pmc_update_sample_period(struct kvm_pmc *pmc) -{ - if (!pmc->perf_event || pmc->is_paused || - !is_sampling_event(pmc->perf_event)) - return; - - perf_event_period(pmc->perf_event, - get_sample_period(pmc, pmc->counter)); -} - static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc) { struct kvm_pmu *pmu = pmc_to_pmu(pmc); diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c index 3fd47de14b38..b6a7ad4d6914 100644 --- a/arch/x86/kvm/svm/pmu.c +++ b/arch/x86/kvm/svm/pmu.c @@ -161,7 +161,6 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER); if (pmc) { pmc_write_counter(pmc, data); - pmc_update_sample_period(pmc); return 0; } /* MSR_EVNTSELn */ diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 90c1f7f07e53..a6216c874729 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -437,11 +437,9 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) !(msr & MSR_PMC_FULL_WIDTH_BIT)) data = (s64)(s32)data; pmc_write_counter(pmc, data); - pmc_update_sample_period(pmc); break; } else if ((pmc = get_fixed_pmc(pmu, msr))) { pmc_write_counter(pmc, data); - pmc_update_sample_period(pmc); break; } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) { reserved_bits = pmu->reserved_bits; -- 2.42.0.758.gaed0368e0e-goog