From patchwork Wed Nov 8 00:31:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 162839 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp607010vqo; Tue, 7 Nov 2023 16:33:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IEblpE8VpB+eOyoDttpp/uw0cZsspntxvSnUq0EUuZtleYf7f97UZPiSY898gOawjEC4H8E X-Received: by 2002:a05:6a20:3d1f:b0:14e:a1f0:a8ea with SMTP id y31-20020a056a203d1f00b0014ea1f0a8eamr677565pzi.3.1699403593485; Tue, 07 Nov 2023 16:33:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699403593; cv=none; d=google.com; s=arc-20160816; b=jP1uYPPARykdh+KNr9v9F8wPgtTKnH1BOBj01oAlJU6FBrLMLI4D0mmRX7B6WSMgtS RSQfPcBYjswBDBlDjSEM/vEbDYOd4aY6qKMv37UGFFoaxh4WGBiiXL5l6Db+TaBTxOSL cnAPthh31uNc8X4OOSErjVtuh0s1hxfe9toDHnx8pwaoTfzR9Kg+4y4P77qqF5MdTGkb dnHwc1ginP79Whfwhpd/nipKvwxbCEBzntebkF4kCv5VY4ZhY3Z23XopKEowJfI3lOpa TrmMSn5LdEjlkqg8ChVIBrlKrgO+L3L5vUE9FdVIHM90yWrrjb4EeuzRTY45WcsbAhDe pYmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=rIRxzXLXpgSnDoyGRMhFbBRVg5vafHFYB9OFSW8L9KA=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=CuTjpKMBq4IA6PLMLKorPIsEEPNIk7DAjjk6b4G0E5iznRpIce1PrwHGq5VbkCr/E/ 6tBWexuiCkc2sJwM1nmdyT6Ip3sCcQBmpk5euJGuHiG2hYRyTg/f0epgnTP2+FOKwAgy N1KS9OVD19ctW7G0DkrqKtcujWeraKg4i4AyqWLAwesEjsnUDToi6XXG6io0kd9tAHhS +gmIPIkCtB4oDEjhYsXHGhPMohJR0hNX0gdyWunXxHn1d7OKNYq/4Ccgm4arGDlpfRW7 xNjqtkmckoc7HPbTHgzMlnFOOm8zr5pa6GGMUyE+3gO/2usYAR9qYbCBAwxSxox+PuFW 040A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EKqf7npn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id a14-20020a17090a8c0e00b00280cc652dd6si866830pjo.173.2023.11.07.16.33.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 16:33:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EKqf7npn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id A012E82DD0B3; Tue, 7 Nov 2023 16:32:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235408AbjKHAbx (ORCPT + 32 others); Tue, 7 Nov 2023 19:31:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235187AbjKHAbq (ORCPT ); Tue, 7 Nov 2023 19:31:46 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CBD610F8 for ; Tue, 7 Nov 2023 16:31:44 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da2b87dd614so7890096276.2 for ; Tue, 07 Nov 2023 16:31:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699403503; x=1700008303; 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=rIRxzXLXpgSnDoyGRMhFbBRVg5vafHFYB9OFSW8L9KA=; b=EKqf7npnqL1q0Uy2J1ICeiF+7WGb1xzUlDB6MDolWbesG8wb2QGqb1it3vMRDUwky+ jPmjdzM2ft4+f+dQFKxESZAlwJdDTHI37Z5jhRod4P2JkI4ZxkU5PFi0foVrWeOnCh2g PnlAjYjJQqSbgqzLYAjUseCblR0g4JzpPSedDkR7xefEZlldAfTLG6m8H2Gv3PxBEMl8 nUvn6PAR/vLUjXNcp3u72BnXGsS2ESyipLDHZ61vnoC07mwJBLwimynn7jAfFwL3d/gC MCUsAEzvTOFNanyo+ZsslitTdlubRm8buysnzzRVu/bh7aRW/tgclxNHDjZvbCyLkbAa C/Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699403503; x=1700008303; 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=rIRxzXLXpgSnDoyGRMhFbBRVg5vafHFYB9OFSW8L9KA=; b=IVUwiE2z6ONIkjJaygHRAzvyhKOxfcQDR5SNjoHCwDlrZ9xQX2TZ9xNCG7zJTglKRN A0BH7s/zhNwVhZjlQEZpfk2CycZBYY+XeIXkQ05qFwfr2qI4+A1ndMqGoA3lmPVwKcKQ lMGUE2HKXDhEJp9+GUFJI21OAR51OwbQYAlKfpbFG+F1QnawYWSZpwqr75LsfRxg6CWl D2BWj4V1kg6RPfZGi8FhQ+YrYLnlatbsjhwoRmaaCR7Q8Oj3Z5DpiSgomkpjQ2KMx7dX F3A87XeD/cVlpsaarBPUrr+wTWP4SHMWFtNh1eOD7GPhuOW59llSo0RPVM+YXS+wTbSN n/Fw== X-Gm-Message-State: AOJu0YwHhnd1D709VPk6OKJd0cP8kN58kh+wGmGnOjKf08zWRlReJUqw R0zGd5iF09BGA1dAJJGXfU8au6yDrxA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:b109:0:b0:d9a:556d:5f8a with SMTP id g9-20020a25b109000000b00d9a556d5f8amr6081ybj.12.1699403503575; Tue, 07 Nov 2023 16:31:43 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 7 Nov 2023 16:31:18 -0800 In-Reply-To: <20231108003135.546002-1-seanjc@google.com> Mime-Version: 1.0 References: <20231108003135.546002-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231108003135.546002-3-seanjc@google.com> Subject: [PATCH v7 02/19] KVM: x86/pmu: Allow programming events that match unsupported arch events 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 07 Nov 2023 16:32:06 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781953822498806434 X-GMAIL-MSGID: 1781953822498806434 Remove KVM's bogus restriction that the guest can't program an event whose encoding matches an unsupported architectural event. The enumeration of an architectural event only says that if a CPU supports an architectural event, then the event can be programmed using the architectural encoding. The enumeration does NOT say anything about the encoding when the CPU doesn't report support the architectural event. Preventing the guest from counting events whose encoding happens to match an architectural event breaks existing functionality whenever Intel adds an architectural encoding that was *ever* used for a CPU that doesn't enumerate support for the architectural event, even if the encoding is for the exact same event! E.g. the architectural encoding for Top-Down Slots is 0x01a4. Broadwell CPUs, which do not support the Top-Down Slots architectural event, 0x10a4 is a valid, model-specific event. Denying guest usage of 0x01a4 if/when KVM adds support for Top-Down slots would break any Broadwell-based guest. Reported-by: Kan Liang Closes: https://lore.kernel.org/all/2004baa6-b494-462c-a11f-8104ea152c6a@linux.intel.com Fixes: a21864486f7e ("KVM: x86/pmu: Fix available_event_types check for REF_CPU_CYCLES event") Reviewed-by: Dapeng Mi Reviewed-by: Jim Mattson Signed-off-by: Sean Christopherson Reviewed-by: Kan Liang --- arch/x86/include/asm/kvm-x86-pmu-ops.h | 1 - arch/x86/kvm/pmu.c | 1 - arch/x86/kvm/pmu.h | 1 - arch/x86/kvm/svm/pmu.c | 6 ---- arch/x86/kvm/vmx/pmu_intel.c | 38 -------------------------- 5 files changed, 47 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-pmu-ops.h b/arch/x86/include/asm/kvm-x86-pmu-ops.h index 6c98f4bb4228..884af8ef7657 100644 --- a/arch/x86/include/asm/kvm-x86-pmu-ops.h +++ b/arch/x86/include/asm/kvm-x86-pmu-ops.h @@ -12,7 +12,6 @@ BUILD_BUG_ON(1) * a NULL definition, for example if "static_call_cond()" will be used * at the call sites. */ -KVM_X86_PMU_OP(hw_event_available) KVM_X86_PMU_OP(pmc_idx_to_pmc) KVM_X86_PMU_OP(rdpmc_ecx_to_pmc) KVM_X86_PMU_OP(msr_idx_to_pmc) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 9ae07db6f0f6..99ed72966528 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -374,7 +374,6 @@ static bool check_pmu_event_filter(struct kvm_pmc *pmc) static bool pmc_event_is_allowed(struct kvm_pmc *pmc) { return pmc_is_globally_enabled(pmc) && pmc_speculative_in_use(pmc) && - static_call(kvm_x86_pmu_hw_event_available)(pmc) && check_pmu_event_filter(pmc); } diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 1d64113de488..10fe5bf02705 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -19,7 +19,6 @@ #define VMWARE_BACKDOOR_PMC_APPARENT_TIME 0x10002 struct kvm_pmu_ops { - bool (*hw_event_available)(struct kvm_pmc *pmc); struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu, int pmc_idx); struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu, unsigned int idx, u64 *mask); diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c index 373ff6a6687b..5596fe816ea8 100644 --- a/arch/x86/kvm/svm/pmu.c +++ b/arch/x86/kvm/svm/pmu.c @@ -73,11 +73,6 @@ static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr, return amd_pmc_idx_to_pmc(pmu, idx); } -static bool amd_hw_event_available(struct kvm_pmc *pmc) -{ - return true; -} - static bool amd_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); @@ -249,7 +244,6 @@ static void amd_pmu_reset(struct kvm_vcpu *vcpu) } struct kvm_pmu_ops amd_pmu_ops __initdata = { - .hw_event_available = amd_hw_event_available, .pmc_idx_to_pmc = amd_pmc_idx_to_pmc, .rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc, .msr_idx_to_pmc = amd_msr_idx_to_pmc, diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index c6e227edcf8e..7737ee2fc62f 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -101,43 +101,6 @@ static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx) } } -static bool intel_hw_event_available(struct kvm_pmc *pmc) -{ - struct kvm_pmu *pmu = pmc_to_pmu(pmc); - u8 event_select = pmc->eventsel & ARCH_PERFMON_EVENTSEL_EVENT; - u8 unit_mask = (pmc->eventsel & ARCH_PERFMON_EVENTSEL_UMASK) >> 8; - int i; - - /* - * Fixed counters are always available if KVM reaches this point. If a - * fixed counter is unsupported in hardware or guest CPUID, KVM doesn't - * allow the counter's corresponding MSR to be written. KVM does use - * architectural events to program fixed counters, as the interface to - * perf doesn't allow requesting a specific fixed counter, e.g. perf - * may (sadly) back a guest fixed PMC with a general purposed counter. - * But if _hardware_ doesn't support the associated event, KVM simply - * doesn't enumerate support for the fixed counter. - */ - if (pmc_is_fixed(pmc)) - return true; - - BUILD_BUG_ON(ARRAY_SIZE(intel_arch_events) != NR_INTEL_ARCH_EVENTS); - - /* - * Disallow events reported as unavailable in guest CPUID. Note, this - * doesn't apply to pseudo-architectural events (see above). - */ - for (i = 0; i < NR_REAL_INTEL_ARCH_EVENTS; i++) { - if (intel_arch_events[i].eventsel != event_select || - intel_arch_events[i].unit_mask != unit_mask) - continue; - - return pmu->available_event_types & BIT(i); - } - - return true; -} - static bool intel_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); @@ -802,7 +765,6 @@ void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu) } struct kvm_pmu_ops intel_pmu_ops __initdata = { - .hw_event_available = intel_hw_event_available, .pmc_idx_to_pmc = intel_pmc_idx_to_pmc, .rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc, .msr_idx_to_pmc = intel_msr_idx_to_pmc,