Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp1607194img; Sat, 23 Mar 2019 07:21:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqxumXfAG211Z1pY0rV7Cwylgt8bqrpMs8HuFZ1gefaJ0DCOOUJLQhndiHPcxg92EMKgiJu3 X-Received: by 2002:a65:4608:: with SMTP id v8mr14519051pgq.9.1553350885482; Sat, 23 Mar 2019 07:21:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553350885; cv=none; d=google.com; s=arc-20160816; b=VijwSGg7MkfQdRMXjPxU7+5pRVKWnu6NIt8oVwBt6786TgbcdcGtWgNVqaRnVkw5YE 3uQjEHACMBv4z+QryyzR2KXPNomjPdMy6eXk/kpRtWcAqXj7lZEgxCIicbJ49tu60r5e bdhPlBtE4iNyHl2QUwFbDoQf8V3/SN4bl9/ZbaYNAHOXsr6IDmjbxbKFYkboz204Z+dT 0rsHaZenq/MLmeJHmfjpiNtp1TC5hEI6ccZ5IhvEhSaDYuz3N+5PwXdPOL1m/Z3Gg2cJ CmRXLpjDFTUax+9rg6C3py0vDxvlNm6+FsixW18JnFTWhwmEpDm4K6ljO9z03UdvV2tD JdwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=rAHAyMFszqSlZx3mg8i2hIv2vprmYEpmvGbfVqv2QB4=; b=jY413aNn1X+64J0U+YbDsyoqulDI27ABO7FqWCxAebkBMN8mrsUCPMhAMDZZ2+Gd3y 5W/FYzQ+YlMHTmeQKbwE70YEqTV9KcZ7g6Pn2NG5IkBKOEUCfTy8tqBatjf3goybdgDO 4zC952aze82llXLiwORrHz8KTSGNhPy+enNsFN6dJmbO+wqridHRvzeJLlfWoPcyyOoM f+i9gwJpJwOCov4e6yLF36vrUFYkrxbQU0PIUEFyuJWHHUUQHjGKtFdWCkYB12bBjxUQ qH8t0+VIaFgOxWiEgGuIvc+uOGfNttnBzBDATOQSrXVI2B2fxsahmNdkGk1X+/bdp+qN irZQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f6si9041377pgc.99.2019.03.23.07.21.10; Sat, 23 Mar 2019 07:21:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727688AbfCWOS6 (ORCPT + 99 others); Sat, 23 Mar 2019 10:18:58 -0400 Received: from mga01.intel.com ([192.55.52.88]:28331 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727596AbfCWOS5 (ORCPT ); Sat, 23 Mar 2019 10:18:57 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Mar 2019 07:18:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,256,1549958400"; d="scan'208";a="129543558" Received: from xulike-server.sh.intel.com ([10.239.48.134]) by orsmga006.jf.intel.com with ESMTP; 23 Mar 2019 07:18:55 -0700 From: Like Xu To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: like.xu@intel.com, wei.w.wang@intel.com, Andi Kleen , Peter Zijlstra , Kan Liang , Ingo Molnar , Paolo Bonzini Subject: [RFC] [PATCH v2 3/5] KVM/x86/vPMU: add Intel vPMC enable/disable and save/restore support Date: Sat, 23 Mar 2019 22:18:06 +0800 Message-Id: <1553350688-39627-4-git-send-email-like.xu@linux.intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1553350688-39627-1-git-send-email-like.xu@linux.intel.com> References: <1553350688-39627-1-git-send-email-like.xu@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We may not assume the guest fixed vPMC would be assigned by an host fixed counter and vice versa. This issue (the host hw->idx has a different type of guest hw->idx) is named as the cross-mapping and it needs to keep semantics for mask select and enable ctrl. Signed-off-by: Like Xu --- arch/x86/kvm/vmx/pmu_intel.c | 92 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index bb16031..0b69acc 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -133,6 +133,34 @@ static void intel_pmu_update_host_fixed_ctrl(u64 new_ctrl, u8 host_idx) wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, host_ctrl); } +static void intel_pmu_enable_host_counter(struct kvm_pmc *pmc) +{ + u8 host_idx; + + if (!intel_pmc_is_assigned(pmc)) + return; + + host_idx = pmc->perf_event->hw.idx; + if (host_idx >= INTEL_PMC_IDX_FIXED) + intel_pmu_enable_host_fixed_counter(pmc); + else + intel_pmu_enable_host_gp_counter(pmc); +} + +static void intel_pmu_disable_host_counter(struct kvm_pmc *pmc) +{ + u8 host_idx; + + if (!intel_pmc_is_assigned(pmc)) + return; + + host_idx = pmc->perf_event->hw.idx; + if (host_idx >= INTEL_PMC_IDX_FIXED) + intel_pmu_disable_host_fixed_counter(pmc); + else + intel_pmu_disable_host_gp_counter(pmc); +} + static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) { int i; @@ -262,6 +290,57 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) return ret; } +static void intel_pmu_save_guest_pmc(struct kvm_pmu *pmu, u32 idx) +{ + struct kvm_pmc *pmc = intel_pmc_idx_to_pmc(pmu, idx); + + if (!intel_pmc_is_assigned(pmc)) + return; + + rdmsrl(pmc->perf_event->hw.event_base, pmc->counter); + wrmsrl(pmc->perf_event->hw.event_base, 0); +} + +static void intel_pmu_restore_guest_pmc(struct kvm_pmu *pmu, u32 idx) +{ + struct kvm_pmc *pmc = intel_pmc_idx_to_pmc(pmu, idx); + u8 ctrl; + + if (!intel_pmc_is_assigned(pmc)) + return; + + if (pmc->idx >= INTEL_PMC_IDX_FIXED) { + ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, + pmc->idx - INTEL_PMC_IDX_FIXED); + if (ctrl) + intel_pmu_enable_host_counter(pmc); + else + intel_pmu_disable_host_counter(pmc); + } else { + if (!(pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE)) + intel_pmu_disable_host_counter(pmc); + else + intel_pmu_enable_host_counter(pmc); + } + + wrmsrl(pmc->perf_event->hw.event_base, pmc->counter); +} + +static void intel_pmc_stop_counter(struct kvm_pmc *pmc) +{ + struct kvm_pmu *pmu = pmc_to_pmu(pmc); + + if (!pmc->perf_event) + return; + + intel_pmu_disable_host_counter(pmc); + intel_pmu_save_guest_pmc(pmu, pmc->idx); + pmc_read_counter(pmc); + perf_event_release_kernel(pmc->perf_event); + pmc->perf_event = NULL; + pmc->hw_life_count = 0; +} + static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); @@ -424,17 +503,20 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu) static void intel_pmu_reset(struct kvm_vcpu *vcpu) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); + struct kvm_pmc *pmc; int i; for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) { - struct kvm_pmc *pmc = &pmu->gp_counters[i]; - - pmc_stop_counter(pmc); + pmc = &pmu->gp_counters[i]; + intel_pmc_stop_counter(pmc); pmc->counter = pmc->eventsel = 0; } - for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) - pmc_stop_counter(&pmu->fixed_counters[i]); + for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) { + pmc = &pmu->fixed_counters[i]; + intel_pmc_stop_counter(pmc); + pmc->counter = 0; + } pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status = pmu->global_ovf_ctrl = 0; -- 1.8.3.1