Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp633700rdd; Tue, 9 Jan 2024 15:06:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IFndNz1vPJq9vA66P4G89MhX538wiE23yx1aNlPQNnDR6YR2VphBWbK3x9HxUWLcEnGyylb X-Received: by 2002:a25:a4a7:0:b0:dbe:9f8d:5fc5 with SMTP id g36-20020a25a4a7000000b00dbe9f8d5fc5mr135216ybi.10.1704841615727; Tue, 09 Jan 2024 15:06:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704841615; cv=none; d=google.com; s=arc-20160816; b=0bEZNRiU5Tvj+kYXATezOVrGRHWWfe9UPkswwuNLnlHF3UXRDJ8zexQhDzzMdQqHp6 /sJNEkaYoBWBTjPmLm9nUoZ6V7EpK6p8hr3+AL/q/+fE/s0eJbm9z1DaVI6vj0bOcUJo CYYVMh+K+SPWrNvJsvTRAHmnT2fDuAAxhwDU8ig7NHbsMozzLpPaQ74lxEuQdaq2q2UY ROZhJ/Yoot44/x8Do5Mv6uQKGMg2IJA0hPyyHFS+0rCHNcwiNEFDBB2la3+5ihiIXdo+ TvmOpzSA1/BV/PlBxuuIteW6vUD1GbVmJpCUQI2dTLhdG7+IBzOTv1jCIw6BkX6q258z gjnA== 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=GbgLIH6rTnv7SO9FN+B/HV3gRuklhpSWLpN0FQT/Z7Q=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=NiuO82fAn4/DuYJIYSC3L92RnpFsrffXAn0dgEmh2Twcaq2ZjRJnc0hssq9i5A5nuf hBL9P4I46R8kLMcFfNR+t8tWm+piItynX0CtTGSv4blBORRp7ylX3PiFd0IThrnQYrMo cQv6rI7Kfd7Cfv4b8FA0bnJG/9xJCJLhKSU560ORXqMyTHQsRYd4Dz645wQXgM+h6OV2 JnJFWyKOLdLfIAYqoXAwopO/6ibnHUFMF3nrcsSDb7L+6NczLO+APxdWGiDoC3MUwyJg t25MajbYzht1EKDhPg5yUTdWnCmJs3htpUl5dNGU+p4g84hreR2H7nRfnYWe8wfYSSn0 u1uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="pIE3U+U/"; spf=pass (google.com: domain of linux-kernel+bounces-21499-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21499-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 q20-20020a67b914000000b00467073c73d4si471955vsn.771.2024.01.09.15.06.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 15:06:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21499-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="pIE3U+U/"; spf=pass (google.com: domain of linux-kernel+bounces-21499-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21499-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 75EB01C20B00 for ; Tue, 9 Jan 2024 23:06:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CD1ED4655F; Tue, 9 Jan 2024 23:03:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pIE3U+U/" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 9466145BF5 for ; Tue, 9 Jan 2024 23:03:13 +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-yb1-f202.google.com with SMTP id 3f1490d57ef6-dbf228b9961so471622276.3 for ; Tue, 09 Jan 2024 15:03:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704841392; x=1705446192; 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=GbgLIH6rTnv7SO9FN+B/HV3gRuklhpSWLpN0FQT/Z7Q=; b=pIE3U+U/+O2YkfyluDy7TMg9Vbxdf/IuWMB/3iQZXxnPtHIt+J7q5lYqwdSoiYSTeb Y8Z+SSy5He0Moz+D5EIwuqCVE9QfCV8f4HJmVlhe15deWz30UM7FC211dnb0uVfiG2ox dvEQYPLEJoI+UblOA8I7tFYrRlUGGUsm7BSL79O6nl0FwbluSREIHmo1TeL1AgaIEfMA 0rXcV6IiCzwqalIJncVvWfOGbRLjL7KEY8ddya2e0Q0JTr8owWs6Q5f1gOe6F1Ae+m9M /YzKtvPdznJpGvxl5aUKMnCdBfF1l06FejSBdmg+fYIpBDlfD1H64XgJgbAyyWiCIp1x b3Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704841392; x=1705446192; 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=GbgLIH6rTnv7SO9FN+B/HV3gRuklhpSWLpN0FQT/Z7Q=; b=RTOQM7QPlWWFDTr6gKAwdPQdwV5szgOvsl8bbtF2r/EeSOWGFQ1bbbrVL2yIwAnA/K Hc73sXWhXzbq8k+TDdSbfASsa8l3Ni6w/SHnmdPmafi4u/MFycSjmV3lNRsblsTuelHb ASAvj0fjVT6HxHrNWmMfmqQu7TJSPWuIcmyz/JxIQOJZ340+kwH7v+grQLx1Zfq/ti0d vzGdrg3+LsYg+AlnPV/oPH2p0OHUkhrkzczlDCFZWcrl3/WcAaPka68sDZVRYoRCWBB5 Vn+/20AF137HtfvA0wkH7eczuENZ7om3EfZtvfxm2TSpy+3z45yasKvoaRxVb81mABC3 IlLA== X-Gm-Message-State: AOJu0YwHvcy1G8/7Md6MIgWXsUJig4k5sQePU8p9D9g75ucRjjbtPST4 1+HKKdcCH/292gB3ch5ynHLJ64J08S9Y8AHWbg== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1343:b0:dbe:a220:68f9 with SMTP id g3-20020a056902134300b00dbea22068f9mr47982ybu.0.1704841392693; Tue, 09 Jan 2024 15:03:12 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 15:02:30 -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-11-seanjc@google.com> Subject: [PATCH v10 10/29] KVM: x86/pmu: Treat "fixed" PMU type in RDPMC as index as a value, not flag 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" Refactor KVM's handling of ECX for RDPMC to treat the FIXED modifier as an explicit value, not a flag (minus one wart). While non-architectural PMUs do use bit 31 as a flag (for "fast" reads), architectural PMUs use the upper half of ECX to encode the type. From the SDM: ECX[31:16] specifies type of PMC while ECX[15:0] specifies the index of the PMC to be read within that type Note, that the known supported types are 4000H and 2000H, i.e. look a lot like flags, doesn't contradict the above statement that ECX[31:16] holds the type, at least not by any sane reading of the SDM. Keep the explicitly clearing of the FIXED "flag", as KVM subtly relies on that behavior to disallow unsupported types while allowing the correct indices for fixed counters. This wart will be cleaned up in short order. Opportunistically grab the per-type bitmask in the if-else blocks to eliminate the one-off usage of the local "fixed" bool. Reported-by: Jim Mattson Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/pmu_intel.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 5a5dfae6055c..c37dd3aa056b 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -28,6 +28,9 @@ */ #define INTEL_RDPMC_FIXED INTEL_PMC_FIXED_RDPMC_BASE +#define INTEL_RDPMC_TYPE_MASK GENMASK(31, 16) +#define INTEL_RDPMC_INDEX_MASK GENMASK(15, 0) + #define MSR_PMC_FULL_WIDTH_BIT (MSR_IA32_PMC0 - MSR_IA32_PERFCTR0) static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) @@ -66,10 +69,11 @@ static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx) static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, unsigned int idx, u64 *mask) { + unsigned int type = idx & INTEL_RDPMC_TYPE_MASK; struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); - bool fixed = idx & INTEL_RDPMC_FIXED; struct kvm_pmc *counters; unsigned int num_counters; + u64 bitmask; /* * The encoding of ECX for RDPMC is different for architectural versus @@ -90,16 +94,20 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, * i.e. let RDPMC fail due to accessing a non-existent counter. */ idx &= ~INTEL_RDPMC_FIXED; - if (fixed) { + if (type == INTEL_RDPMC_FIXED) { counters = pmu->fixed_counters; num_counters = pmu->nr_arch_fixed_counters; + bitmask = pmu->counter_bitmask[KVM_PMC_FIXED]; } else { counters = pmu->gp_counters; num_counters = pmu->nr_arch_gp_counters; + bitmask = pmu->counter_bitmask[KVM_PMC_GP]; } + if (idx >= num_counters) return NULL; - *mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP]; + + *mask &= bitmask; return &counters[array_index_nospec(idx, num_counters)]; } -- 2.43.0.472.g3155946c3a-goog