From patchwork Tue Jan 9 23:02:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 186597 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp458851dyi; Tue, 9 Jan 2024 15:05:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IFlDcc5zNdc1dGccteCCH3+P9RbgpIIJxG1lFQXQepW2pr21ljWT7wAfpmmBjypMflUrX8M X-Received: by 2002:a17:907:3f21:b0:a2b:29dc:3df0 with SMTP id hq33-20020a1709073f2100b00a2b29dc3df0mr132755ejc.70.1704841502226; Tue, 09 Jan 2024 15:05:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704841502; cv=none; d=google.com; s=arc-20160816; b=FjtWVJw99d8Pe8AuhV9fgDxWzU6sQtyNJEM59nX+1wV0zTDZOp98cdIkj0lM77dkYx 7IiqPiljCQo9zMK25FI8RivSHQhW36c4WW6kpX2MK9CX2nqKOGvpdQu7P4JuxqSMe+3V Ia6mZL17bs2dTDKfSzcZ/y/wK1S6PMeF/W3fVWPToh10r8cVWefZ0gNRp2OUoQn/7buD nt4m57rPIcgWyM32Bcwl10P6nJ2N0QEqhhQd/AU6rW4iTIg5JjQDWw/8T2E2esmzGInz fLyoUulIJnlDxgaH7ITNF0KFgDvFB6Jk1QhsjAVWRxFmQ6SVVmritVRJ7V+GxT/TCHaB 8yOA== 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=LadkSkOnespq7J+bb9MybmOp6ANC4ZDI49MV81r/bQE=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=fxrj3GunKJXQs5HogT+9Y8IR0vmI40uIlE3zAkYt9eeJGk5STXwgJiZpjsz0NNyaqY INqZUAdymevSNYGGok3/9rrmUUpc14ZwaW/3L60Uq6PYjJExYgQ7tb6gMmwSaeFEWkvk sZ1egiaVFp/ADZiJhsR+HVUgb2bhbb5WHyRgiDYzxPe2Vd3j+b4OSHpjwEwjiPrOxs01 gi8JpJyDoVdTGjWKDGcgnys1NxNtRjkOEpM4O0bsK8Z3CT6fRr0T3qudHtaZxsNbue2m Zv8E5DFWhtnV4qDNnNNn4ntqEIzn1kbm5dMB+bvqHXpEFFcGvBjlwWn2q8y87r0X5vAs bzPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=CZ3HgNV0; spf=pass (google.com: domain of linux-kernel+bounces-21493-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21493-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id fy20-20020a170906b7d400b00a2a189b1053si1137193ejb.365.2024.01.09.15.05.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 15:05:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21493-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=CZ3HgNV0; spf=pass (google.com: domain of linux-kernel+bounces-21493-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21493-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id D226B1F26145 for ; Tue, 9 Jan 2024 23:05:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 89C8D3FE50; Tue, 9 Jan 2024 23:03:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CZ3HgNV0" 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 74A003F8F7 for ; Tue, 9 Jan 2024 23:03:01 +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-5f6c12872fbso53384427b3.1 for ; Tue, 09 Jan 2024 15:03:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704841380; x=1705446180; 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=LadkSkOnespq7J+bb9MybmOp6ANC4ZDI49MV81r/bQE=; b=CZ3HgNV0hOupopA4+p9nuwDSscAzTLrKVCboN3zZ9Ces+bq4Yf0/QzkcD3ttrkV5MM 2y6OaV39QPEddBuLGlL12i5bp+/iWgReVsWx2SLD7z6OyNR9Ejpng7U+toBExEjL0cSQ 5dXrBHZRWgJZfIMZCflX9ARKwLo3BbeDbTCXpoYNILFLM6qSL5AMuvlvSUO1RhGrCTrk TuvXAIo+6Z1oRBdfqO4JY3kn65kmtHEjsL6L4BqIkCat/W+87EhryPAeVrGb11x54LqE AyXDLNmP/GYcARXi6YPM2kACkcOw/lk8xxLzpIQlMEswMMB87oYQJn58J/HAhF2rPiZr CLYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704841380; x=1705446180; 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=LadkSkOnespq7J+bb9MybmOp6ANC4ZDI49MV81r/bQE=; b=vOiYhrrjFseGzIbFIAwxsAynJ5ZMpHi8JJC8Cfz1A8Hve5PEr2LMyX1mkK/2Q+WVKP G9gtmsHf+wGnfPefW87+44eBiL2+2Kqp7Zmv6YSjm/KQHZZo5ip+kqgS05sHmRmObp9N QIn9hybCS9KHazuJJPsXsG7MsRFqNkn6gbpT8k8gXfSbQDRs1xgGz3fepTFDDHmg8v5n KfAs1kgSyE+0hslV9An8bQMZhQrMLbGcGXG6LM7CQSv5aOcbvd/VEF/tUdRVMlthbzNw FLSKHZ+caFEujf+QRv0/SpQaz8RxqtalWz9Cdmrz4UksZF1erAhGa9ex1SaLXb55Vo5v alDw== X-Gm-Message-State: AOJu0YwhAbsQsZM+MElx6SZIBgU7She7klGTxy36+Zq2BcGUFruW3Fdp eeSojNjO/SnbDarbqummIKA/UTjGNnS2xjli2w== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:a9a:b0:5f6:f1ec:2da5 with SMTP id ci26-20020a05690c0a9a00b005f6f1ec2da5mr104412ywb.9.1704841380561; Tue, 09 Jan 2024 15:03:00 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 15:02:24 -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-5-seanjc@google.com> Subject: [PATCH v10 04/29] KVM: x86/pmu: Setup fixed counters' eventsel during PMU initialization 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787655883199846100 X-GMAIL-MSGID: 1787655883199846100 Set the eventsel for all fixed counters during PMU initialization, the eventsel is hardcoded and consumed if and only if the counter is supported, i.e. there is no reason to redo the setup every time the PMU is refreshed. Configuring all KVM-supported fixed counter also eliminates a potential pitfall if/when KVM supports discontiguous fixed counters, in which case configuring only nr_arch_fixed_counters will be insufficient (ignoring the fact that KVM will need many other changes to support discontiguous fixed counters). Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/pmu_intel.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index f3c44ddc09f8..98e92b9ece09 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -407,27 +407,21 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) * Note, reference cycles is counted using a perf-defined "psuedo-encoding", * as there is no architectural general purpose encoding for reference cycles. */ -static void setup_fixed_pmc_eventsel(struct kvm_pmu *pmu) +static u64 intel_get_fixed_pmc_eventsel(int index) { const struct { - u8 eventsel; + u8 event; u8 unit_mask; } fixed_pmc_events[] = { [0] = { 0xc0, 0x00 }, /* Instruction Retired / PERF_COUNT_HW_INSTRUCTIONS. */ [1] = { 0x3c, 0x00 }, /* CPU Cycles/ PERF_COUNT_HW_CPU_CYCLES. */ [2] = { 0x00, 0x03 }, /* Reference Cycles / PERF_COUNT_HW_REF_CPU_CYCLES*/ }; - int i; BUILD_BUG_ON(ARRAY_SIZE(fixed_pmc_events) != KVM_PMC_MAX_FIXED); - for (i = 0; i < pmu->nr_arch_fixed_counters; i++) { - int index = array_index_nospec(i, KVM_PMC_MAX_FIXED); - struct kvm_pmc *pmc = &pmu->fixed_counters[index]; - - pmc->eventsel = (fixed_pmc_events[index].unit_mask << 8) | - fixed_pmc_events[index].eventsel; - } + return (fixed_pmc_events[index].unit_mask << 8) | + fixed_pmc_events[index].event; } static void intel_pmu_refresh(struct kvm_vcpu *vcpu) @@ -493,7 +487,6 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) kvm_pmu_cap.bit_width_fixed); pmu->counter_bitmask[KVM_PMC_FIXED] = ((u64)1 << edx.split.bit_width_fixed) - 1; - setup_fixed_pmc_eventsel(pmu); } for (i = 0; i < pmu->nr_arch_fixed_counters; i++) @@ -571,6 +564,7 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu) pmu->fixed_counters[i].vcpu = vcpu; pmu->fixed_counters[i].idx = i + INTEL_PMC_IDX_FIXED; pmu->fixed_counters[i].current_config = 0; + pmu->fixed_counters[i].eventsel = intel_get_fixed_pmc_eventsel(i); } lbr_desc->records.nr = 0;