From patchwork Tue Jan 24 23:49:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13114892 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D25DC54E94 for ; Tue, 24 Jan 2023 23:49:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234160AbjAXXtS (ORCPT ); Tue, 24 Jan 2023 18:49:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234282AbjAXXtQ (ORCPT ); Tue, 24 Jan 2023 18:49:16 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91914271E for ; Tue, 24 Jan 2023 15:49:15 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id n93-20020a17090a2ce600b0022beeabcf6cso156079pjd.0 for ; Tue, 24 Jan 2023 15:49:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=xHI4355sZtl+nMxSrviADFAGPhVPX2cjlZsacGsw9pI=; b=RAAd1Iu4UYOh1XsF8Cfe52BTlzhxuWODb0kKCcvoM9rqv0AU2jMW7496Q3aayEBvSQ neTdVSs7NeBj8zQJoFhhPzVm4joC8mZ8YbsOFxm5lNQKU799j0EnbwviVogAmKs8Ab1c fqS18Q9i6Nn0gHXXGy/B2LkZR7WW+r80VpeFpfcDNbJXHe2wRky4HEb6hHsxiUCk0P8u h9rY0X2k6Jt+brsvgb/OoozZLc/VmyA2i597RiNFtjRGBnXywLqsLHtWIG4FkJethTsc V9zGA5ufPSq/AnK4LrkicqQCJOeYOFd7ga8eRDs+oArYI8rhs6ln+HkHH/9o+/AxO0cD hAXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=xHI4355sZtl+nMxSrviADFAGPhVPX2cjlZsacGsw9pI=; b=wbBpgm31ywFo7NSkKBM1h2x0vw0k2FzCeLx+t4b/RCQF5ZmhbLupk4kjtTkzY4SlXd L9RLqoYz1OPI4OxIj2FeL6ZH0wEAtqNH3Nsju4pIcg6VT53HDzSnRD7iz6nx5N/65VEz 6u+Zc2NFkM0rquiajnA76zoIaVv7npUJgO7hW6yckAeIjsqVCCFlba3qD26hVysjIv6i rIMSw1SbV3KLz49AWTiT+ua+GrUs20fLSW0RgbylB5hEqz/nyuodIW9dykbmlaRPDK7p vG58CLexVCJTyp14fcg5BK1erpyWWIfe2Aem5TQCxIXpjKbVMtOsldNHhO9iMJadPJsx yzdA== X-Gm-Message-State: AFqh2krwx0yge+GSQcgh3qkw4KAJnj8COl68X/kYRv5XqC6dI75SknU0 32DGTlkqhmDHti5M45h7F9UGmKsAVFI= X-Google-Smtp-Source: AMrXdXvYyk1sWuRZ5UE/FSLyVafxn2kpbItwCV1ruZJj5qGZULz4RlmSKkf8HnPq/QxXtVj4pEO2mBr8YmE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:aa7:972c:0:b0:579:6402:5b39 with SMTP id k12-20020aa7972c000000b0057964025b39mr3200002pfg.11.1674604155069; Tue, 24 Jan 2023 15:49:15 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 24 Jan 2023 23:49:00 +0000 In-Reply-To: <20230124234905.3774678-1-seanjc@google.com> Mime-Version: 1.0 References: <20230124234905.3774678-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230124234905.3774678-2-seanjc@google.com> Subject: [PATCH 1/6] KVM: x86/pmu: Cap kvm_pmu_cap.num_counters_gp at KVM's internal max From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Aaron Lewis , Weijiang Yang , Like Xu Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Limit kvm_pmu_cap.num_counters_gp during kvm_init_pmu_capability() based on the vendor PMU capabilities so that consuming num_counters_gp naturally does the right thing. This fixes a mostly theoretical bug where KVM could over-report its PMU support in KVM_GET_SUPPORTED_CPUID for leaf 0xA, e.g. if the number of counters reported by perf is greater than KVM's hardcoded internal limit. Incorporating input from the AMD PMU also avoids over-reporting MSRs to save when running on AMD. Signed-off-by: Sean Christopherson --- arch/x86/kvm/pmu.h | 5 ++++- arch/x86/kvm/svm/pmu.c | 1 + arch/x86/kvm/vmx/pmu_intel.c | 1 + arch/x86/kvm/x86.c | 6 +++--- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 30bfccc6df60..8c04e9109db0 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -42,6 +42,7 @@ struct kvm_pmu_ops { void (*cleanup)(struct kvm_vcpu *vcpu); const u64 EVENTSEL_EVENT; + const int MAX_NR_GP_COUNTERS; }; void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops); @@ -163,7 +164,7 @@ static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc) extern struct x86_pmu_capability kvm_pmu_cap; -static inline void kvm_init_pmu_capability(void) +static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) { bool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL; @@ -182,6 +183,8 @@ static inline void kvm_init_pmu_capability(void) } kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2); + kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp, + pmu_ops->MAX_NR_GP_COUNTERS); kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed, KVM_PMC_MAX_FIXED); } diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c index 5da8c292e3e3..cc77a0681800 100644 --- a/arch/x86/kvm/svm/pmu.c +++ b/arch/x86/kvm/svm/pmu.c @@ -232,4 +232,5 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = { .init = amd_pmu_init, .reset = amd_pmu_reset, .EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT, + .MAX_NR_GP_COUNTERS = KVM_AMD_PMC_MAX_GENERIC, }; diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 7980fda3978d..5d525d677967 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -812,4 +812,5 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = { .deliver_pmi = intel_pmu_deliver_pmi, .cleanup = intel_pmu_cleanup, .EVENTSEL_EVENT = ARCH_PERFMON_EVENTSEL_EVENT, + .MAX_NR_GP_COUNTERS = KVM_INTEL_PMC_MAX_GENERIC, }; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index da02a08e21b5..ad95ce92a154 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7061,12 +7061,12 @@ static void kvm_init_msr_list(void) break; case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR_MAX: if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >= - min(KVM_INTEL_PMC_MAX_GENERIC, kvm_pmu_cap.num_counters_gp)) + kvm_pmu_cap.num_counters_gp) continue; break; case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL_MAX: if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >= - min(KVM_INTEL_PMC_MAX_GENERIC, kvm_pmu_cap.num_counters_gp)) + kvm_pmu_cap.num_counters_gp) continue; break; case MSR_IA32_XFD: @@ -9386,7 +9386,7 @@ static int __kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) if (boot_cpu_has(X86_FEATURE_XSAVES)) rdmsrl(MSR_IA32_XSS, host_xss); - kvm_init_pmu_capability(); + kvm_init_pmu_capability(ops->pmu_ops); r = ops->hardware_setup(); if (r != 0) From patchwork Tue Jan 24 23:49:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13114894 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07960C38142 for ; Tue, 24 Jan 2023 23:49:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234425AbjAXXtY (ORCPT ); Tue, 24 Jan 2023 18:49:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234282AbjAXXtS (ORCPT ); Tue, 24 Jan 2023 18:49:18 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B1FEAD09 for ; Tue, 24 Jan 2023 15:49:17 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id b9-20020a170903228900b00194a0110d7bso9681449plh.6 for ; Tue, 24 Jan 2023 15:49:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=ezITNigk0nCUK1P2pm1bMFWUxaZdj3btWqkAvgj3Ekg=; b=M8JljCyrd+ISLaFzsLrzmO6ZC+OOlCGhA7o8j565gCVFoFVY8UKbeS77PE6odaXGPw 0Ol3Eqrkp1yeW03OupoRu8c9851edOve1tk88KY8YHup2IqmC1PWm7iNeYSx6eVr6YkS fqILlYYFSjcPQVyxmmUg2wO9MldKEwf9sMMhucAw/qGPE9rTJZOm5udFoet906siuU/n 7MBJ+rlOtbS8+sgw8Wbk2eMmhB2BIpCWejjRxz6SoxXm98I9LXiIh4UqaAnuxElH47oz Ku+/pznQ6+O0BIVbv684KV5jtnOsFr4GNP6O6e13eTrG1crEcttKHsOhDs3Izy1h2P2M iF9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=ezITNigk0nCUK1P2pm1bMFWUxaZdj3btWqkAvgj3Ekg=; b=XxdjyXWaRzV6bul23WeHOU7XjharnAf8FNnlUOEtJYd6lpSMFMYDH5RN2dtCxs8/SK F0JNEeIpi6lPvRiTUb9m6rtePGEDuOJj/8fso3m1OXU16S/Ml0W+hgar/DOiFrtt1XGx ElBqChzeAJYy2LQRGMjW7TRUDIsGn4cAK6bAdZBtVtvXeCJl2+vhze5ayo2Y0QVO9IPx lesaHjmefbhXcYb6h1uoKgdQAcdR4fGinGAnNp6s//8fpMeiTbdBJW2+X/kv9ANXmnGj 28kgHu3irFwVrqffXGPvlqdlkD1vGRrqJlnnmtkd7xJx4lu8waGOotv1oix6b9IZIvuo 4RJg== X-Gm-Message-State: AFqh2kpb7WMUpYzW+XiZ9YUdVNYrytL/6uLo3A2ApLuSs3rQA4oMXCsJ tMhmgqSizf60PBBdQZDtaS53ygCW2xg= X-Google-Smtp-Source: AMrXdXuDcHqUYEP9AhwzvxiuxnjKVX9UJDsQBmXvp/p2qxuA128ge6f0FI5nqk4JzQ4zBIXxIZ21NaWI5rQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:b96:b0:58d:b8f6:6f6d with SMTP id g22-20020a056a000b9600b0058db8f66f6dmr3046704pfj.32.1674604156683; Tue, 24 Jan 2023 15:49:16 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 24 Jan 2023 23:49:01 +0000 In-Reply-To: <20230124234905.3774678-1-seanjc@google.com> Mime-Version: 1.0 References: <20230124234905.3774678-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230124234905.3774678-3-seanjc@google.com> Subject: [PATCH 2/6] KVM: x86/pmu: Gate all "unimplemented MSR" prints on report_ignored_msrs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Aaron Lewis , Weijiang Yang , Like Xu Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add helpers to print unimplemented MSR accesses and condition all such prints on report_ignored_msrs, i.e. honor userspace's request to not print unimplemented MSRs. Even though vcpu_unimpl() is ratelimited, printing can still be problematic, e.g. if a print gets stalled when host userspace is writing MSRs during live migration, an effective stall can result in very noticeable disruption in the guest. E.g. the profile below was taken while calling KVM_SET_MSRS on the PMU counters while the PMU was disabled in KVM. - 99.75% 0.00% [.] __ioctl - __ioctl - 99.74% entry_SYSCALL_64_after_hwframe do_syscall_64 sys_ioctl - do_vfs_ioctl - 92.48% kvm_vcpu_ioctl - kvm_arch_vcpu_ioctl - 85.12% kvm_set_msr_ignored_check svm_set_msr kvm_set_msr_common printk vprintk_func vprintk_default vprintk_emit console_unlock call_console_drivers univ8250_console_write serial8250_console_write uart_console_write Reported-by: Aaron Lewis Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- arch/x86/kvm/hyperv.c | 10 ++++------ arch/x86/kvm/svm/svm.c | 5 ++--- arch/x86/kvm/vmx/vmx.c | 4 +--- arch/x86/kvm/x86.c | 18 +++++------------- arch/x86/kvm/x86.h | 12 ++++++++++++ 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 71aff0edc0ed..3eb8caf87ee4 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -1430,8 +1430,7 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: return syndbg_set_msr(vcpu, msr, data, host); default: - vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n", - msr, data); + kvm_pr_unimpl_wrmsr(vcpu, msr, data); return 1; } return 0; @@ -1552,8 +1551,7 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) return 1; break; default: - vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n", - msr, data); + kvm_pr_unimpl_wrmsr(vcpu, msr, data); return 1; } @@ -1608,7 +1606,7 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: return syndbg_get_msr(vcpu, msr, pdata, host); default: - vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); + kvm_pr_unimpl_rdmsr(vcpu, msr); return 1; } @@ -1673,7 +1671,7 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, data = APIC_BUS_FREQUENCY; break; default: - vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); + kvm_pr_unimpl_rdmsr(vcpu, msr); return 1; } *pdata = data; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index d13cf53e7390..dd21e8b1a259 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3015,8 +3015,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) break; case MSR_IA32_DEBUGCTLMSR: if (!lbrv) { - vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", - __func__, data); + kvm_pr_unimpl_wrmsr(vcpu, ecx, data); break; } if (data & DEBUGCTL_RESERVED_BITS) @@ -3045,7 +3044,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) case MSR_VM_CR: return svm_set_vm_cr(vcpu, data); case MSR_VM_IGNNE: - vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data); + kvm_pr_unimpl_wrmsr(vcpu, ecx, data); break; case MSR_AMD64_DE_CFG: { struct kvm_msr_entry msr_entry; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index c788aa382611..8f0f67c75f35 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2206,9 +2206,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) invalid = data & ~vmx_get_supported_debugctl(vcpu, msr_info->host_initiated); if (invalid & (DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR)) { - if (report_ignored_msrs) - vcpu_unimpl(vcpu, "%s: BTF|LBR in IA32_DEBUGCTLMSR 0x%llx, nop\n", - __func__, data); + kvm_pr_unimpl_wrmsr(vcpu, msr_index, data); data &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR); invalid &= ~(DEBUGCTLMSR_BTF|DEBUGCTLMSR_LBR); } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ad95ce92a154..d4a610ffe2b8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3560,7 +3560,6 @@ static void record_steal_time(struct kvm_vcpu *vcpu) int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) { - bool pr = false; u32 msr = msr_info->index; u64 data = msr_info->data; @@ -3606,15 +3605,13 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) if (data == BIT_ULL(18)) { vcpu->arch.msr_hwcr = data; } else if (data != 0) { - vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", - data); + kvm_pr_unimpl_wrmsr(vcpu, msr, data); return 1; } break; case MSR_FAM10H_MMIO_CONF_BASE: if (data != 0) { - vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " - "0x%llx\n", data); + kvm_pr_unimpl_wrmsr(vcpu, msr, data); return 1; } break; @@ -3794,16 +3791,13 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: - pr = true; - fallthrough; case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: if (kvm_pmu_is_valid_msr(vcpu, msr)) return kvm_pmu_set_msr(vcpu, msr_info); - if (pr || data != 0) - vcpu_unimpl(vcpu, "disabled perfctr wrmsr: " - "0x%x data 0x%llx\n", msr, data); + if (data) + kvm_pr_unimpl_wrmsr(vcpu, msr, data); break; case MSR_K7_CLK_CTL: /* @@ -3831,9 +3825,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) /* Drop writes to this legacy MSR -- see rdmsr * counterpart for further detail. */ - if (report_ignored_msrs) - vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n", - msr, data); + kvm_pr_unimpl_wrmsr(vcpu, msr, data); break; case MSR_AMD64_OSVW_ID_LENGTH: if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 9de72586f406..f3554bf05201 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -331,6 +331,18 @@ extern bool report_ignored_msrs; extern bool eager_page_split; +static inline void kvm_pr_unimpl_wrmsr(struct kvm_vcpu *vcpu, u32 msr, u64 data) +{ + if (report_ignored_msrs) + vcpu_unimpl(vcpu, "Unhandled WRMSR(0x%x) = 0x%llx\n", msr, data); +} + +static inline void kvm_pr_unimpl_rdmsr(struct kvm_vcpu *vcpu, u32 msr) +{ + if (report_ignored_msrs) + vcpu_unimpl(vcpu, "Unhandled RDMSR(0x%x)\n", msr); +} + static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) { return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult, From patchwork Tue Jan 24 23:49:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13114893 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E644BC61D9D for ; Tue, 24 Jan 2023 23:49:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234163AbjAXXt0 (ORCPT ); Tue, 24 Jan 2023 18:49:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234364AbjAXXtX (ORCPT ); Tue, 24 Jan 2023 18:49:23 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 171B64FACD for ; Tue, 24 Jan 2023 15:49:18 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id d10-20020a631d4a000000b00491da16dc44so7554891pgm.16 for ; Tue, 24 Jan 2023 15:49:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=PCuRaPACRckOLmMloMboeg+q3NxGqmxKSfZdPmlSu+Y=; b=lQgVxYI7Exbz/lKh6x84rMBA9rVOW4Lup6GD4ApHB6oURXKeNhSWfzSzS36TaNiAeB HpNwQsE/GD2SSSG1me6iMgqx+5OU7pErcewwwbJfYjRkua0LE6tZqv3RRg78k5MGiLY9 hAqhOa9PPiZTpHHRL391qNjCWrU1/eGinF1girUo0rpUAG95vrGjsAkCO3ykM41H9Ay8 pmix913ZqE5rdz5+Nt9AUnUF/PkDDB5bB8Hz2sMjjC6J4caP/KAA4uctFs0jiVZbpZMN HPlzJn6AzD/QW6eh3/7AA3o+9kAzAIZTWG1WaPAxLh5gyM+qbFTDz9iQOKQU30zpEnhi oRmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=PCuRaPACRckOLmMloMboeg+q3NxGqmxKSfZdPmlSu+Y=; b=XKDCERzZrDODRP31BvTz6Hha4k8fvERhhbJu9TyMQaYx0J1GCha4ri+QtjlgqWFmBL jdR3Xy3FF0AHDMy6PN7FUbXFFz50BDNanIJudNh4X+AE1ucGidMdKYEYT1GanAqvJWrN UAmF74ifxQOKpwrKowWgr5me286tCS34EG0LRviO8/NE9g1ArejiqUy4YyGua5G+todo lmJB1zRAzLn9F1Jl3iooPcI8+i58SETWX1dsD852V+Gj0FwpQR02Gnr5lO5H+QjyoiBZ YU08ttKExYKuFEMV/9MvzFiRQgHJg03SOqssz+sIbIWW8KsCMpjVxgC0bZ54nUn0lqfc NNog== X-Gm-Message-State: AFqh2krk2mC+vH97IVOMr2yyLzF+lQRjsg7YUJah2m6B2yGb23fSGOq+ pIOLZCSCjiFd49BRh3UE2Fwu1+soVss= X-Google-Smtp-Source: AMrXdXv+L6/9pHp9W3kDcSor6PnS/wvwK/SJwdt9uWwpfHJrbHNyg6r9R1SSmgEoF76rKj8r2ten0LoOJ14= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:65cb:b0:227:4d5:6d64 with SMTP id i11-20020a17090a65cb00b0022704d56d64mr3937714pjs.29.1674604158365; Tue, 24 Jan 2023 15:49:18 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 24 Jan 2023 23:49:02 +0000 In-Reply-To: <20230124234905.3774678-1-seanjc@google.com> Mime-Version: 1.0 References: <20230124234905.3774678-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230124234905.3774678-4-seanjc@google.com> Subject: [PATCH 3/6] KVM: x86/pmu: Use separate array for defining "PMU MSRs to save" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Aaron Lewis , Weijiang Yang , Like Xu Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move all potential to-be-saved PMU MSRs into a separate array so that a future patch can easily omit all PMU MSRs from the list when the PMU is disabled. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 153 ++++++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 71 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d4a610ffe2b8..9b6e1af63531 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1419,7 +1419,7 @@ EXPORT_SYMBOL_GPL(kvm_emulate_rdpmc); * may depend on host virtualization features rather than host cpu features. */ -static const u32 msrs_to_save_all[] = { +static const u32 msrs_to_save_base[] = { MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, MSR_STAR, #ifdef CONFIG_X86_64 @@ -1436,6 +1436,10 @@ static const u32 msrs_to_save_all[] = { MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B, MSR_IA32_UMWAIT_CONTROL, + MSR_IA32_XFD, MSR_IA32_XFD_ERR, +}; + +static const u32 msrs_to_save_pmu[] = { MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1, MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS, @@ -1460,11 +1464,10 @@ static const u32 msrs_to_save_all[] = { MSR_F15H_PERF_CTL3, MSR_F15H_PERF_CTL4, MSR_F15H_PERF_CTL5, MSR_F15H_PERF_CTR0, MSR_F15H_PERF_CTR1, MSR_F15H_PERF_CTR2, MSR_F15H_PERF_CTR3, MSR_F15H_PERF_CTR4, MSR_F15H_PERF_CTR5, - - MSR_IA32_XFD, MSR_IA32_XFD_ERR, }; -static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)]; +static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_base) + + ARRAY_SIZE(msrs_to_save_pmu)]; static unsigned num_msrs_to_save; static const u32 emulated_msrs_all[] = { @@ -6994,84 +6997,92 @@ long kvm_arch_vm_ioctl(struct file *filp, return r; } -static void kvm_init_msr_list(void) +static void kvm_probe_msr_to_save(u32 msr_index) { u32 dummy[2]; + + if (rdmsr_safe(msr_index, &dummy[0], &dummy[1])) + return; + + /* + * Even MSRs that are valid in the host may not be exposed to guests in + * some cases. + */ + switch (msr_index) { + case MSR_IA32_BNDCFGS: + if (!kvm_mpx_supported()) + return; + break; + case MSR_TSC_AUX: + if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP) && + !kvm_cpu_cap_has(X86_FEATURE_RDPID)) + return; + break; + case MSR_IA32_UMWAIT_CONTROL: + if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG)) + return; + break; + case MSR_IA32_RTIT_CTL: + case MSR_IA32_RTIT_STATUS: + if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) + return; + break; + case MSR_IA32_RTIT_CR3_MATCH: + if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || + !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering)) + return; + break; + case MSR_IA32_RTIT_OUTPUT_BASE: + case MSR_IA32_RTIT_OUTPUT_MASK: + if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || + (!intel_pt_validate_hw_cap(PT_CAP_topa_output) && + !intel_pt_validate_hw_cap(PT_CAP_single_range_output))) + return; + break; + case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: + if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || + (msr_index - MSR_IA32_RTIT_ADDR0_A >= + intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)) + return; + break; + case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR_MAX: + if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >= + kvm_pmu_cap.num_counters_gp) + return; + break; + case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL_MAX: + if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >= + kvm_pmu_cap.num_counters_gp) + return; + break; + case MSR_IA32_XFD: + case MSR_IA32_XFD_ERR: + if (!kvm_cpu_cap_has(X86_FEATURE_XFD)) + return; + break; + default: + break; + } + + msrs_to_save[num_msrs_to_save++] = msr_index; +} + +static void kvm_init_msr_list(void) +{ unsigned i; BUILD_BUG_ON_MSG(KVM_PMC_MAX_FIXED != 3, - "Please update the fixed PMCs in msrs_to_saved_all[]"); + "Please update the fixed PMCs in msrs_to_save_pmu[]"); num_msrs_to_save = 0; num_emulated_msrs = 0; num_msr_based_features = 0; - for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) { - if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0) - continue; + for (i = 0; i < ARRAY_SIZE(msrs_to_save_base); i++) + kvm_probe_msr_to_save(msrs_to_save_base[i]); - /* - * Even MSRs that are valid in the host may not be exposed - * to the guests in some cases. - */ - switch (msrs_to_save_all[i]) { - case MSR_IA32_BNDCFGS: - if (!kvm_mpx_supported()) - continue; - break; - case MSR_TSC_AUX: - if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP) && - !kvm_cpu_cap_has(X86_FEATURE_RDPID)) - continue; - break; - case MSR_IA32_UMWAIT_CONTROL: - if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG)) - continue; - break; - case MSR_IA32_RTIT_CTL: - case MSR_IA32_RTIT_STATUS: - if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) - continue; - break; - case MSR_IA32_RTIT_CR3_MATCH: - if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || - !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering)) - continue; - break; - case MSR_IA32_RTIT_OUTPUT_BASE: - case MSR_IA32_RTIT_OUTPUT_MASK: - if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || - (!intel_pt_validate_hw_cap(PT_CAP_topa_output) && - !intel_pt_validate_hw_cap(PT_CAP_single_range_output))) - continue; - break; - case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: - if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || - msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >= - intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2) - continue; - break; - case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR_MAX: - if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >= - kvm_pmu_cap.num_counters_gp) - continue; - break; - case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL_MAX: - if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >= - kvm_pmu_cap.num_counters_gp) - continue; - break; - case MSR_IA32_XFD: - case MSR_IA32_XFD_ERR: - if (!kvm_cpu_cap_has(X86_FEATURE_XFD)) - continue; - break; - default: - break; - } - - msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i]; - } + for (i = 0; i < ARRAY_SIZE(msrs_to_save_pmu); i++) + kvm_probe_msr_to_save(msrs_to_save_pmu[i]); for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) { if (!static_call(kvm_x86_has_emulated_msr)(NULL, emulated_msrs_all[i])) From patchwork Tue Jan 24 23:49:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13114895 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C458C54E94 for ; Tue, 24 Jan 2023 23:49:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234345AbjAXXt2 (ORCPT ); Tue, 24 Jan 2023 18:49:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234387AbjAXXtX (ORCPT ); Tue, 24 Jan 2023 18:49:23 -0500 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AE9E50849 for ; Tue, 24 Jan 2023 15:49:20 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id e11-20020a17090a77cb00b0022925dd66d3so134361pjs.4 for ; Tue, 24 Jan 2023 15:49:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=cSFedQsv7Wc1M6mfIq8Uerd1CseDG+3+LjpR3/a1jI4=; b=Vp+bCCyRCOzXxx9AZRk/kLf2yoM9R2H7MV3TG8iO4ixZfrmXyu3VH5OT9URCnFT6Ao SnOE2UHK2AnZMfppSvxPBExJYZ4Fxe9T9DFqELJjMzTPwm3THgO6m/pOUi4AULV2sZ1b 5BNeuoAAa7KPpu6EiXS3gy8WFApTYM5YBCvJTG6h76zjPe9l+cRD0Zp1KCyUns7fe6nX VX1PrSIkSYQxk0xiGxVaWwC1UOgh09tR1C/pu6yybMIHQDasONsVEtBIkxqac5Q4XQoh QATfefxt08qKbI42PfLX9jDT1Nf1BnuHKNDKAqU040eTQcTdnTjdSThoIhCikZ1drvSv bb7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=cSFedQsv7Wc1M6mfIq8Uerd1CseDG+3+LjpR3/a1jI4=; b=B+Fq0LTvbu/iAcSS8PmzocmCzEtuLkXGbIxwXpcfPBD0MTAcpQ6IEstqWHnEIil0/b VRZ5ZYxwzVWoi9/xjeNnUuVHQYgZ5YMR0nSMQwvdHLUN/pDCQA4cnReOtxthRj4vjU2e j+38SrUZ2CGPqhAWLbb3Bg9coP4G0viELm49J/YVC6Z+nGwJ0xjXMKp9WU4YpGiFt/5K eR4Nr1aul2fvMHtT+2ynDXme36MMGORA8/LR98+A52lkkv8udZKm4B8Y+sfTlanmCv/M f7sxDo5l2LFWXG24ooBQo4xtSnAWOKvkSjGwmqinQza/IzreMWju3QGqhj86RwUVSItl ixQA== X-Gm-Message-State: AFqh2kp3Qq4wcJk8VeLeRppmvZVhsJIqjS+0o/y7Xv7Hdn3XLvNgYY9k 25PWB4O7RW4r/vDj9RQO9ifH+HpOA+Y= X-Google-Smtp-Source: AMrXdXv339G7NMnbhLg4zibqJM/2hsDjjR6Pg293Q4PQitRC5ZTlozoeYq7nwj6TlxtlrhWWEQeAgD92yFo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:bd97:b0:192:9e63:e2bc with SMTP id q23-20020a170902bd9700b001929e63e2bcmr3138979pls.11.1674604159781; Tue, 24 Jan 2023 15:49:19 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 24 Jan 2023 23:49:03 +0000 In-Reply-To: <20230124234905.3774678-1-seanjc@google.com> Mime-Version: 1.0 References: <20230124234905.3774678-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230124234905.3774678-5-seanjc@google.com> Subject: [PATCH 4/6] KVM: x86/pmu: Don't tell userspace to save PMU MSRs if PMU is disabled From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Aaron Lewis , Weijiang Yang , Like Xu Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Omit all PMU MSRs from the "MSRs to save" list if the PMU is disabled so that userspace doesn't waste time saving and restoring dummy values. KVM provides "error" semantics (read zeros, drop writes) for such known-but- unsupported MSRs, i.e. has fudged around this issue for quite some time. Keep the "error" semantics as-is for now, the logic will be cleaned up in a separate patch. Cc: Aaron Lewis Cc: Weijiang Yang Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9b6e1af63531..25da2cc09e55 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7081,8 +7081,10 @@ static void kvm_init_msr_list(void) for (i = 0; i < ARRAY_SIZE(msrs_to_save_base); i++) kvm_probe_msr_to_save(msrs_to_save_base[i]); - for (i = 0; i < ARRAY_SIZE(msrs_to_save_pmu); i++) - kvm_probe_msr_to_save(msrs_to_save_pmu[i]); + if (enable_pmu) { + for (i = 0; i < ARRAY_SIZE(msrs_to_save_pmu); i++) + kvm_probe_msr_to_save(msrs_to_save_pmu[i]); + } for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) { if (!static_call(kvm_x86_has_emulated_msr)(NULL, emulated_msrs_all[i])) From patchwork Tue Jan 24 23:49:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13114897 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE213C38142 for ; Tue, 24 Jan 2023 23:49:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234599AbjAXXtc (ORCPT ); Tue, 24 Jan 2023 18:49:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234506AbjAXXtZ (ORCPT ); Tue, 24 Jan 2023 18:49:25 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E0B9518ED for ; Tue, 24 Jan 2023 15:49:21 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id s14-20020a17090302ce00b00192d831a155so9691276plk.11 for ; Tue, 24 Jan 2023 15:49:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=etFpX/482XpVxpk8+X7x9LZK6GHklrmOWmBG2Y9l9JY=; b=PaPFcjkY84c1BKENp3ubJyva/NBLjPo7sEgZagcT9qRPJXepm8n4k89Wf8MPHqwKV2 xL2OgFZmaF2JpR1nfmxlaWEWDb0SwBh4jQ84r9tWNS3F4rOTph2CtS2Ud0rtf2oY/pk+ 65GpkRiNS+I9N4JGjXmB5WmUJe4XN0xc9GAjoJiJNVAMjRnWDoc2gK7HW6T6w8zwXL3O 553HY1KeSy3AINSbZpAOVOFSwHpc2Tin9dHIK7KsOMQXnKKLDahp8tBeTiCMoPoChrwY 5GpuYETk271PwyEYZ7xzfyOBnY6mgTYC88yDxdYQTM2LFZt6YibyDdBDND7jAHi3UFpA p4mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=etFpX/482XpVxpk8+X7x9LZK6GHklrmOWmBG2Y9l9JY=; b=eScVSMCkwhAk35DLITkGQWF5bHgv+u+LQjJK1WRVmx+tl2SPpjcezI3A5HQGQeECx0 gi3D7Fz/q7OYmZtZ+8juHmoKyoXOcAGsPrLZGo2qiAUtvdD2sXoNYd7Mv0+h8atG1QHf lWtgygqh7s1+uWJdHfN2LuBTu6gkikHuq7NLPv7LR0QTFSmf+VCp9N6j1KUJZQpZN0xR 1ecRMJsuTRD8d+yYTdxglM99T5YNOQVcnJ/tzWgI5vrhFiQF2ntHPWLbVJEkZK604AQU eiP7fNcxMx4q0KndJ+9lwRyyfofDYr5ai8jHzwGhANPofaMAJQsvbER6ARBzXMhkE5qa mh2Q== X-Gm-Message-State: AO0yUKWkhvaPk7TgXLASIjSznceASXLIzoaSIfy6P7tEfLkhbIqin1BJ njtJ5G7xnFGXMBQ1Fkh3EHeueE5J8wk= X-Google-Smtp-Source: AK7set8SaE2MZIRg9LgmCIwbSkWZ/LgOB2ipKmhXTLetCVx/z8v1pT85QcQW4E3nEHJMgo6DIweuCZPB1ls= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:131e:0:b0:4d9:ed5e:5e99 with SMTP id i30-20020a63131e000000b004d9ed5e5e99mr11422pgl.69.1674604161397; Tue, 24 Jan 2023 15:49:21 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 24 Jan 2023 23:49:04 +0000 In-Reply-To: <20230124234905.3774678-1-seanjc@google.com> Mime-Version: 1.0 References: <20230124234905.3774678-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230124234905.3774678-6-seanjc@google.com> Subject: [PATCH 5/6] KVM: x86/pmu: Don't tell userspace to save MSRs for non-existent fixed PMCs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Aaron Lewis , Weijiang Yang , Like Xu Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Like Xu Limit the set of MSRs for fixed PMU counters based on the number of fixed counters actually supported by the host so that userspace doesn't waste time saving and restoring dummy values. Signed-off-by: Like Xu [sean: split for !enable_pmu logic, drop min(), write changelog] Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index cd0151e6af62..adb92fc4d7c9 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -514,6 +514,7 @@ struct kvm_pmc { #define MSR_ARCH_PERFMON_PERFCTR_MAX (MSR_ARCH_PERFMON_PERFCTR0 + KVM_INTEL_PMC_MAX_GENERIC - 1) #define MSR_ARCH_PERFMON_EVENTSEL_MAX (MSR_ARCH_PERFMON_EVENTSEL0 + KVM_INTEL_PMC_MAX_GENERIC - 1) #define KVM_PMC_MAX_FIXED 3 +#define MSR_ARCH_PERFMON_FIXED_CTR_MAX (MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_PMC_MAX_FIXED - 1) #define KVM_AMD_PMC_MAX_GENERIC 6 struct kvm_pmu { unsigned nr_arch_gp_counters; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 25da2cc09e55..3c49c86b973d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7055,6 +7055,11 @@ static void kvm_probe_msr_to_save(u32 msr_index) kvm_pmu_cap.num_counters_gp) return; break; + case MSR_ARCH_PERFMON_FIXED_CTR0 ... MSR_ARCH_PERFMON_FIXED_CTR_MAX: + if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >= + kvm_pmu_cap.num_counters_fixed) + return; + break; case MSR_IA32_XFD: case MSR_IA32_XFD_ERR: if (!kvm_cpu_cap_has(X86_FEATURE_XFD)) From patchwork Tue Jan 24 23:49:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13114896 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C98B8C54EED for ; Tue, 24 Jan 2023 23:49:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234486AbjAXXtb (ORCPT ); Tue, 24 Jan 2023 18:49:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234326AbjAXXt0 (ORCPT ); Tue, 24 Jan 2023 18:49:26 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B3BD49560 for ; Tue, 24 Jan 2023 15:49:23 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id s24-20020a17090aa11800b00229fef3ac5dso131772pjp.5 for ; Tue, 24 Jan 2023 15:49:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=UzMRX5SFUfOVkL2DhNd2WmgacyEPpUxAyESmr0cYTPo=; b=U+2hjRjwdAw4rgSwJT4rTLx+XWZ6VA8xRr9CDH5MmtQ0DMC6I8AZu0utxcCMToxUt0 qEvmcAkPf0Z2PX0nGGVKoNQUWR9l3i/D88vwPrfs8FLHGmwsaNXkyQ4SaZ0wNIo8qhJZ R8B0xXP4sv2OG4APlw8RdWx2ZdMWW2NUv5HfnHJ7M+2LhRT5WIz/p0YGDe3qGGVS8kRc WcwkOSYbpY2N7QzFWXEuHkLkmO9KTO0XKtPlZ+kjdrEluzgZ2iPKY0u7rrxc+5NhusWx XxSaFjfe7ss8AqeFDPdA+yjfi8DZ9I7MtXqYDCSNAlrfv5iKsocGtMA8YD6lzk6vGcYX foLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=UzMRX5SFUfOVkL2DhNd2WmgacyEPpUxAyESmr0cYTPo=; b=zZgVgrV/FUhrrcB+Ydf0zo0mYng0dmXZZ6anyJcyJxHbgSrrx+86XvLfRaMknKgLqM QpIaUJ4PlFD/2bA1ECsj15huoCxF8eeX77ivZGtvxGsqVN9klZNhLazj50+bmQY0ptUQ CscGIDFYF3uzVLF46nCncGg/VGzKFa9pMYojdsKXiNuX1ld6cGvx0kIKslg+8HXPiQJs YyKwZKoWID4BrU4JrQoY3MOG2XkI0LmJMsURxWVM7Q5uPpTTbPXEJeiEPxwbWuIeKSjS Ec16CGg4gQF9mymWvUwDo4oXMDnnaJupEGs3AE4nBU4mksutEnvIFARzRlQA+BKaYJ6N jFaA== X-Gm-Message-State: AFqh2krRzv4Td521abKrYFHMsErgbjARjA5IRnVkDTdLhllHAAvCIiZn KxCgOFiuzyFIpXfVTT7cSt2se5soj4c= X-Google-Smtp-Source: AMrXdXuK/2YHhj4eTdRh15QrAi1gGGAbhQFHxWDpJQGzDuXV8X4TYOIqARvH1aClbII5MaCT1Ll9QeXYTuY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:4088:b0:576:cc71:b8e4 with SMTP id bw8-20020a056a00408800b00576cc71b8e4mr3170694pfb.20.1674604163135; Tue, 24 Jan 2023 15:49:23 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 24 Jan 2023 23:49:05 +0000 In-Reply-To: <20230124234905.3774678-1-seanjc@google.com> Mime-Version: 1.0 References: <20230124234905.3774678-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230124234905.3774678-7-seanjc@google.com> Subject: [PATCH 6/6] KVM: x86/pmu: Provide "error" semantics for unsupported-but-known PMU MSRs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Aaron Lewis , Weijiang Yang , Like Xu Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Provide "error" semantics (read zeros, drop writes) for userspace accesses to MSRs that are ultimately unsupported for whatever reason, but for which KVM told userspace to save and restore the MSR, i.e. for MSRs that KVM included in KVM_GET_MSR_INDEX_LIST. Previously, KVM special cased a few PMU MSRs that were problematic at one point or another. Extend the treatment to all PMU MSRs, e.g. to avoid spurious unsupported accesses. Note, the logic can also be used for non-PMU MSRs, but as of today only PMU MSRs can end up being unsupported after KVM told userspace to save and restore them. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 51 ++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3c49c86b973d..64c567a1b32b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3561,6 +3561,18 @@ static void record_steal_time(struct kvm_vcpu *vcpu) mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); } +static bool kvm_is_msr_to_save(u32 msr_index) +{ + unsigned int i; + + for (i = 0; i < num_msrs_to_save; i++) { + if (msrs_to_save[i] == msr_index) + return true; + } + + return false; +} + int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) { u32 msr = msr_info->index; @@ -3876,20 +3888,18 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.guest_fpu.xfd_err = data; break; #endif - case MSR_IA32_PEBS_ENABLE: - case MSR_IA32_DS_AREA: - case MSR_PEBS_DATA_CFG: - case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: + default: if (kvm_pmu_is_valid_msr(vcpu, msr)) return kvm_pmu_set_msr(vcpu, msr_info); + /* * Userspace is allowed to write '0' to MSRs that KVM reports * as to-be-saved, even if an MSRs isn't fully supported. */ - return !msr_info->host_initiated || data; - default: - if (kvm_pmu_is_valid_msr(vcpu, msr)) - return kvm_pmu_set_msr(vcpu, msr_info); + if (msr_info->host_initiated && !data && + kvm_is_msr_to_save(msr)) + break; + return KVM_MSR_RET_INVALID; } return 0; @@ -3979,20 +3989,6 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */ msr_info->data = 0; break; - case MSR_IA32_PEBS_ENABLE: - case MSR_IA32_DS_AREA: - case MSR_PEBS_DATA_CFG: - case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: - if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) - return kvm_pmu_get_msr(vcpu, msr_info); - /* - * Userspace is allowed to read MSRs that KVM reports as - * to-be-saved, even if an MSR isn't fully supported. - */ - if (!msr_info->host_initiated) - return 1; - msr_info->data = 0; - break; case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: @@ -4248,6 +4244,17 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) default: if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) return kvm_pmu_get_msr(vcpu, msr_info); + + /* + * Userspace is allowed to read MSRs that KVM reports as + * to-be-saved, even if an MSR isn't fully supported. + */ + if (msr_info->host_initiated && + kvm_is_msr_to_save(msr_info->index)) { + msr_info->data = 0; + break; + } + return KVM_MSR_RET_INVALID; } return 0;