From nobody Fri Dec 19 17:35:32 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 018B9C00A8F for ; Mon, 23 Oct 2023 23:40:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231911AbjJWXkf (ORCPT ); Mon, 23 Oct 2023 19:40:35 -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-Google-Smtp-Source: AGHT+IGdvIZLByyBNIUhCTP9Wm8mWMSf/+Xbp5sifAxXAMGbFzJRLll9+DfM5lV5yha3lmakbmYIlQgF+wc= 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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; } =20 +static u64 get_sample_period(struct kvm_pmc *pmc, u64 counter_value) +{ + u64 sample_period =3D (-counter_value) & pmc_bitmask(pmc); + + if (!sample_period) + sample_period =3D 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) } } =20 +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 +=3D val - pmc_read_counter(pmc); + pmc->counter &=3D 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 =3D *(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); } =20 -static inline void pmc_write_counter(struct kvm_pmc *pmc, u64 val) -{ - pmc->counter +=3D val - pmc_read_counter(pmc); - pmc->counter &=3D pmc_bitmask(pmc); -} +void pmc_write_counter(struct kvm_pmc *pmc, u64 val); =20 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; } =20 -static inline u64 get_sample_period(struct kvm_pmc *pmc, u64 counter_value) -{ - u64 sample_period =3D (-counter_value) & pmc_bitmask(pmc); - - if (!sample_period) - sample_period =3D 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 =3D 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, struc= t msr_data *msr_info) pmc =3D 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, st= ruct msr_data *msr_info) !(msr & MSR_PMC_FULL_WIDTH_BIT)) data =3D (s64)(s32)data; pmc_write_counter(pmc, data); - pmc_update_sample_period(pmc); break; } else if ((pmc =3D get_fixed_pmc(pmu, msr))) { pmc_write_counter(pmc, data); - pmc_update_sample_period(pmc); break; } else if ((pmc =3D get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) { reserved_bits =3D pmu->reserved_bits; --=20 2.42.0.758.gaed0368e0e-goog