Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp633243rdd; Tue, 9 Jan 2024 15:06:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IEcOdrV4TFCY906C/AwltpPZp6RBt+2kdeh8alEJ1I52cEQ/kSuIpB7CEJ+Oa+Mpqj9lTaB X-Received: by 2002:a05:6102:358d:b0:467:bf0a:a0b2 with SMTP id h13-20020a056102358d00b00467bf0aa0b2mr37519vsu.21.1704841563299; Tue, 09 Jan 2024 15:06:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704841563; cv=none; d=google.com; s=arc-20160816; b=EcphBwVN6Br89cQRv/tukIvomvsNt01ugv/UrgsoOgxOnMgZrY5J2hlV3v+4AtcZxO ZYDh8QpA/yrFMz6FW41nCrWRL/oZC8+qcoCnpziQtrQVLGtKe7MWt/u32mNQqD5b483S 955LTT/tnvmI1jEOJIC+w0jZNYZWGsK29D+4sv0xAfDpkqd0VRNRzzioVrX4XFPvx0Za UAjI4ZNsIjK/mIZVxDe8WRxReqhvTNiV/1dyk0AUEw616mtcsaG1/L0wzKUsuWNK0Mj7 qCwY+pe3LIMebrZ+pC8vCRZrxa9o2nesI8E7zDIGmJMS0oESfACoUcU7wpBTRhFADKzF kQog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=6nByD1PFJyVRmLMcbHkCGLijfHuueVKWkXFPS4squYI=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=hF5w9t6zCPf+7wt30Fz4wff2yw0LD7DH2zsCWWu3d+SI+0GtIz0fx5TPOigIJMEh9X /8By3XjBYZaaPzMpGqsZGWIUTyt9kGjKH5SaNB7pHJaO32+7dceD/Sa5o9qbN3d9wrFd qoaa3HZOG2/2Vkn7vzUqTvqickkb5DTNUrknlU3OBxgSrXgzYKU6pzbVWsyL1Y7AG4qz EdDl5Dc5n/Wbvxd3kYos52ECusrrdamtkDf2BnlXCOetlXlc2vjT47pKubtR5oP+7o8y VkCN0TipPzKlBeNz1HJsynoUuPMgYO41qgVs10PEEvL5UrPz6K5hfjXSyjmRqBF1e3Zc AKVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Pb0f6nxz; spf=pass (google.com: domain of linux-kernel+bounces-21497-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21497-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id n18-20020a67ee92000000b0046784d570a6si441540vsp.661.2024.01.09.15.06.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 15:06:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21497-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Pb0f6nxz; spf=pass (google.com: domain of linux-kernel+bounces-21497-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21497-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 070E71C20C19 for ; Tue, 9 Jan 2024 23:06:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D014C4437E; Tue, 9 Jan 2024 23:03:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Pb0f6nxz" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 937C040C11 for ; Tue, 9 Jan 2024 23:03:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5f8ffd9fb8aso34880897b3.3 for ; Tue, 09 Jan 2024 15:03:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704841388; x=1705446188; 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=6nByD1PFJyVRmLMcbHkCGLijfHuueVKWkXFPS4squYI=; b=Pb0f6nxztz8gyKLIkCtHdNPCUnL1xh+lNJWXKBaILzAjhH//DxrqPwC6SE0p/LYyAe 043tGOqryZfZmnLOE0SbB+4A/gxhBR7yJKK+6TrvBuWRNpy82wCY1HsvK7xcLP2CEvaY V+vJ4HOT/dvBKxfjZZPBrauBW3yO5nVujInDUpSJO0T/n2hyWDzT4B5w5RulWOYHYjCX qCrJ9Jt0NXLlRjAUy3YC861mFS90KSjjOf+/EnRNG8WXXNx11Ek+Wsq6ILuSLAuIgv7f QTXtyrE5K34md9dx++iAwe96i34wJNp/u6H0t/tMzTqudjpQ7qRRVzU0lFwlU7Yeaoff PSlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704841388; x=1705446188; 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=6nByD1PFJyVRmLMcbHkCGLijfHuueVKWkXFPS4squYI=; b=gMccruDDH03Q2uX9sdUactdJVrDzOvldXdtqqPijhEkfjjwIXxsv42pj3CkypeF93O M+YA0ob35jYNOwfyGeK9cet3wPYfP8vAzyYoTyC7epB3jBl1dnMO2/W8ueVWW3bGsBFO NAoAimlp31pkXvjANlYJYyHdGuJR85ynJDnON7Vt4WRDtiY3Y82EuDTIa4nSShfogTSu nPHW01GZHLJgegSVpAMW2eYQhfZvquahNb0VXPMKCoIOThLNM+9BjRrP8uNUrExYI0Lo ZiEpskd4d/1vxFLJar5KJwCIcRrNKwQfd6eWD49GuHgqeYouJkNW6ezKpMMC3QTNvci6 jXIg== X-Gm-Message-State: AOJu0Yx7E/Brb0VyFWMD9aQt2NWADJLO3Ce0lGqLmMeQ7vQMDvpKk05M /vm0bi+zalFO5iergdR97R80T943jUz9WzU9kQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:c9b:b0:5f0:d791:c965 with SMTP id cm27-20020a05690c0c9b00b005f0d791c965mr122512ywb.3.1704841388652; Tue, 09 Jan 2024 15:03:08 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 15:02:28 -0800 In-Reply-To: <20240109230250.424295-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240109230250.424295-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20240109230250.424295-9-seanjc@google.com> Subject: [PATCH v10 08/29] KVM: x86/pmu: Apply "fast" RDPMC only to Intel PMUs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Dapeng Mi , Jim Mattson , Jinrong Liang , Aaron Lewis , Like Xu Content-Type: text/plain; charset="UTF-8" Move the handling of "fast" RDPMC instructions, which drop bits 63:32 of the count, to Intel. The "fast" flag, and all modifiers for that matter, are Intel-only and aren't supported by AMD. Opportunistically replace open coded bit crud with proper #defines, and add comments to try and disentangle the flags vs. values mess for non-architectural vs. architectural PMUs. Fixes: ca724305a2b0 ("KVM: x86/vPMU: Implement AMD vPMU code for KVM") Reviewed-by: Dapeng Mi Signed-off-by: Sean Christopherson --- arch/x86/kvm/pmu.c | 3 +-- arch/x86/kvm/vmx/pmu_intel.c | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 0b0d804ee239..09b0feb975c3 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -576,10 +576,9 @@ static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) { - bool fast_mode = idx & (1u << 31); struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); struct kvm_pmc *pmc; - u64 mask = fast_mode ? ~0u : ~0ull; + u64 mask = ~0ull; if (!pmu->version) return 1; diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 1b1f888ad32b..03bd188b5754 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -20,6 +20,15 @@ #include "nested.h" #include "pmu.h" +/* + * Perf's "BASE" is wildly misleading, architectural PMUs use bits 31:16 of ECX + * to encode the "type" of counter to read, i.e. this is not a "base". And to + * further confuse things, non-architectural PMUs use bit 31 as a flag for + * "fast" reads, whereas the "type" is an explicit value. + */ +#define INTEL_RDPMC_FIXED INTEL_PMC_FIXED_RDPMC_BASE +#define INTEL_RDPMC_FAST BIT(31) + #define MSR_PMC_FULL_WIDTH_BIT (MSR_IA32_PMC0 - MSR_IA32_PERFCTR0) static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) @@ -59,11 +68,14 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, unsigned int idx, u64 *mask) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); - bool fixed = idx & (1u << 30); + bool fixed = idx & INTEL_RDPMC_FIXED; struct kvm_pmc *counters; unsigned int num_counters; - idx &= ~(3u << 30); + if (idx & INTEL_RDPMC_FAST) + *mask &= GENMASK_ULL(31, 0); + + idx &= ~(INTEL_RDPMC_FIXED | INTEL_RDPMC_FAST); if (fixed) { counters = pmu->fixed_counters; num_counters = pmu->nr_arch_fixed_counters; -- 2.43.0.472.g3155946c3a-goog