From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796189282; b=AxnC0+I5F74oyYbB3UVSbkraJeNdWdmuEW0U+b9X4zXsqeF2oJT/1pNfNEuJ9ffNMica EI/wRyjW4133iqlcwKLCUNwGjVh9bgDkAayDH6F8JfLa+UBYkmGcO+wMe3QxRslI2BAc0 F6QIK9BXQGYDCnmgMnqa1iFesE4BLGjiiu7cwImFSK2dzl9E5xey2RIK3GcNiVE+nNwaP ky8eNcpGNSRNlUIEQBVxSsLolrK79Pkjeko3UUw3lt/ypsrLcs7PnZ8g4+pz2q7WLq6yz PMEM0EMhIgfPVIrQFAgI4RJaWirjKyL6JlLKnC7ExqaoUBD60BIW6RsRRRELGzjZ81Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796189282; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=kpiJQ58yRdinCjC6BtnWCPCBA61qdkOCXvZR4DMcAxQ=; b=rke0BnLsdtZpszJQcay8Yqb2x1sf9ZhB9U0aiLa/Jx6PgXJK/2Qe5MLCj6nF3ZcoWnei uQDdauaFs1NwW5Q9boJX9DWCbcvdoS4FTb2mtnCUAg9HeQI/9VEhzbuM9Qnwq/fpBvbXJ X+ppvWH7qH1wKy2YjzcunGU5jR0go4iCTSkc+5ZOP2ujL4mGe0c3texAaeax60V2C+kdS qUMuC6a0mSQz7uWbJ6MsH58jJF9a6V72aZ/MFxN7Opv3Sl3Jpljhly/0iQj1Oqk+Xd93K ZEDOtJTQhkdobPzcmW/YU9EwIlW7HGNY2FzZZeAdZoG7GBrYpRqziJEvisd5IlCeGFQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fi05lHgk; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9ABAD606A68B; Wed, 2 Dec 2026 05:28:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fi05lHgk Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA0E61F00935; Wed, 2 Dec 2026 05:28:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796189282; bh=kpiJQ58yRdinCjC6BtnWCPCBA61qdkOCXvZR4DMcAxQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=fi05lHgk7UUvnSoGXdsYfHmltZHpLhYNOMAWOtWhkpkGVfjL23/VCBgzl2sXF22Ne GEwtMbQPQUDfca40lu1umBi22GRT2kjeTIhjPvnN1CGyUFcUbneMhqNFLYMMJFDKWj oiRLM4qsaYQGZ0UJdlMWua8rWpHp6YMLABcmIimogZyKOO5TTKis8Epbd/g9qZOuNS 1QsGEyk26OAdLvC338mCNjqBXK1w6itABrIHiS+wgh4n3aV8cH+WzlJYXpS+mCYY1E UDjtHgqt08ust4X11hIeQ24tNO/OZbKUhiZtSBncjOqB7cfJRPfp/6SFnWBISQKZsb 8J1hYv2So+B2g== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH v2 2/4] KVM: x86/pmu: Recalc PMC emulation when deferring counter reprogramming Date: Wed, 2 Dec 2026 05:27:52 +0000 Message-ID: <20261202052754.3170526-3-yosry@kernel.org> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog In-Reply-To: <20261202052754.3170526-1-yosry@kernel.org> References: <20261202052754.3170526-1-yosry@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In preparation for always deferring counter reprogramming, recalculate PMC emulation for each counter first. On AMD, this would make sure that mode changes (e.g. via VMRUN or toggling EFER.SVME) are immediately propagated before instruction emulation completes, such that instructions are counted in the mode they retire in as hardware does. This gives kvm_pmu_request_counters_reprogram() similar treatment to kvm_pmu_request_counter_reprogram(), as done in commit 5dfd498bad5f ("KVM: x86/pmu: Calculate set of to-be-emulated PMCs at time of WRMSRs"). Now that kvm_pmu_request_counter_reprogram() is just a single-counter version of kvm_pmu_request_counters_reprogram(), just call the latter from the former. No functional change intended, as all architectural mode-changing paths on AMD do not currently defer counter reprogramming. Signed-off-by: Yosry Ahmed --- arch/x86/kvm/pmu.h | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 090c9bbb74f48..8aa9db1569167 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -218,26 +218,27 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops); void kvm_pmu_recalc_pmc_emulation(struct kvm_pmu *pmu, struct kvm_pmc *pmc); void kvm_pmu_handle_event(struct kvm_vcpu *vcpu); -static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc) -{ - kvm_pmu_recalc_pmc_emulation(pmc_to_pmu(pmc), pmc); - - set_bit(pmc->idx, pmc_to_pmu(pmc)->reprogram_pmi); - kvm_make_request(KVM_REQ_PMU, pmc->vcpu); -} - static inline void __kvm_pmu_reprogram_counters(struct kvm_pmu *pmu, u64 counters, bool defer) { + DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX); + struct kvm_pmc *pmc; + int bit; + if (!counters) return; atomic64_or(counters, &pmu->__reprogram_pmi); - if (defer) + if (defer) { + BUILD_BUG_ON(sizeof(bitmap) != sizeof(counters)); + bitmap_from_u64(bitmap, counters); + kvm_for_each_pmc(pmu, pmc, bit, bitmap) + kvm_pmu_recalc_pmc_emulation(pmu, pmc); kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu)); - else + } else { kvm_pmu_handle_event(pmu_to_vcpu(pmu)); + } } static inline void kvm_pmu_request_counters_reprogram(struct kvm_pmu *pmu, @@ -246,6 +247,11 @@ static inline void kvm_pmu_request_counters_reprogram(struct kvm_pmu *pmu, __kvm_pmu_reprogram_counters(pmu, counters, true); } +static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc) +{ + kvm_pmu_request_counters_reprogram(pmc_to_pmu(pmc), BIT_ULL(pmc->idx)); +} + /* * Check if a PMC is enabled by comparing it against global_ctrl bits. * -- 2.56.1.911.g883d8bcb20-goog