Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp626998rdb; Fri, 26 Jan 2024 06:11:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IGxM38OEz0i1f+EyyyhtBAXEG5LDIyf8tRIM8r32NAweIKcU3lyMWof1Oh93TJwQxXMgCWV X-Received: by 2002:a05:600c:b94:b0:40e:d369:ea75 with SMTP id fl20-20020a05600c0b9400b0040ed369ea75mr601419wmb.125.1706278282872; Fri, 26 Jan 2024 06:11:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706278282; cv=pass; d=google.com; s=arc-20160816; b=Ew3dCWoC/a/dP7RX/xm4V3CuPmhQYyFxbw64k9yb9HeRgH6+YCdrT1C2Pf01VOiXDl BgRJ3KACre27Z/BMqheZ6ZmehImuShwe74LfJrxHy7wHGHFOwUnvcOYsH86M5zpLEGEb irbj0r1jlFAb2W1cpNRI1KvqjO+EeqjZFYJ6l8sstZCEeUNUm2sJYv1KTzTFz337GzGa RiGDHXVRBiouCdFkomRfu89j9KX1soWiGkBe8UzMOg0FVIvnwi4IjIg/J1Rk/erZR0Oy n/QcYZLo9lhqROfTL8agBAEeDhLG0pnqVBYOslb2B10/P/nKfCw3eO+wLkwrHZyQfVH5 w7HQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=uPDZ1kZveS1Hxa8A5gKNNvJxlLpkTHrqr/TbTigX/ls=; fh=fKQXo0qMcal6BogvSG9bxyWEWsAUcPVWLMmdMql1Nx4=; b=viXhB0dpZ5P0mK3OebwNtaGvztWneNIvADFqUpUwm+vLqte6HT+189h4fB7r+aZHR0 vw7ajSISg4vBiNCNTWCs3GBCdq00gCPXO4TaJL778wn5pfmwJl1tGPpfDzAdVO3Gnq9W A0Vo5gdYYzanIYILVGJeZqN1i84y66KB/yuABELOgtyG8SR/2kT/MC04VoqQrx6gv7yx C8t7C8zUGeIZgb+8PtairBAn672HafPvJ3QnyX4duq0zvcBXvRb7V43ulfefFDvdF9Fw tb/lfRVd7CPklsHGvn0R/v2ATENchIsQ4zB+GJBJ1inZeU7BzOzFtfwYXBvC2i9YXdYx Y2cw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="E8o/GLhe"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-39846-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39846-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id bx14-20020a170906a1ce00b00a318b894edfsi602544ejb.620.2024.01.26.06.11.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 06:11:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-39846-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="E8o/GLhe"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-39846-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39846-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 165531F2E113 for ; Fri, 26 Jan 2024 09:42:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D5C3014A4CC; Fri, 26 Jan 2024 08:57:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="E8o/GLhe" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6C5014A096; Fri, 26 Jan 2024 08:57:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706259469; cv=none; b=UJgX1HBnKczReskb7JgrzDNGS0mRr/pmt04sc/jtvj2lVruHgaiX7S2QhqzZxFlBfRpxGnv9jg5x7lRqAvkjpdX/dYtW3rJEDlPiexzNmfx5Lbgd/7HoM0hN+QfAOKjyyelFhFSR6LpqwXNYUwcXABQWDhTRg26oAAq2UggKxSc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706259469; c=relaxed/simple; bh=IbYfCxqPksLaeoFkdiGUr44Kkbsmws1IykdSnPTgst4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q16m865Fm+NTVv6nsbPFk5x/Qn6J7BgT4AmFEBXWtwk4Qu29wI/HLsf6b6LJvEmsgqeieDeasYpQrLp+2o1GvL1chAWUlegBMPGjenqSBtRe6qn1ugfwTz5fAKslb6drUzKQaMDH++4DQZWlwtRiAo0RbBArYz3S0+0LekpYXAQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=E8o/GLhe; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706259468; x=1737795468; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IbYfCxqPksLaeoFkdiGUr44Kkbsmws1IykdSnPTgst4=; b=E8o/GLheeUlyXIaZ4eDYq0uBGnHuCsMCUgwgw64auF1sYjpxZLyGlUZL pdWT5TYpS5p9A+PqoF1duUDXxnuTgXmjThsH8TMpdV2b441S06RyGOYp0 3G/8NxDS+t/N7zO8VvODRBOTiudQV2M0Y/RvLbLieU8LR/rGSlIkrGJZ9 7IQvE6dJ+82FPvz2b2bk36mvZA8LhEd1w/6/Q+Y0TWjycEpIpbOS7qaH2 R8uFIxbrCOtQNaRQDfQdDQfrMh1f8v/0G3Ah41CRW1IAYADT++VDTE9/Z aGDsUUqF+8SZ890bszhxVZ0Oxf0HAeZYAn2ltJdr8Bd9tDbSbSsxO3LKc Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="9792724" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="9792724" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jan 2024 00:57:47 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="930310246" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="930310246" Received: from yanli3-mobl.ccr.corp.intel.com (HELO xiongzha-desk1.ccr.corp.intel.com) ([10.254.213.178]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jan 2024 00:57:42 -0800 From: Xiong Zhang To: seanjc@google.com, pbonzini@redhat.com, peterz@infradead.org, mizhang@google.com, kan.liang@intel.com, zhenyuw@linux.intel.com, dapeng1.mi@linux.intel.com, jmattson@google.com Cc: kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, zhiyuan.lv@intel.com, eranian@google.com, irogers@google.com, samantha.alt@intel.com, like.xu.linux@gmail.com, chao.gao@intel.com, xiong.y.zhang@linux.intel.com Subject: [RFC PATCH 26/41] KVM: x86/pmu: Add host_perf_cap field in kvm_caps to record host PMU capability Date: Fri, 26 Jan 2024 16:54:29 +0800 Message-Id: <20240126085444.324918-27-xiong.y.zhang@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240126085444.324918-1-xiong.y.zhang@linux.intel.com> References: <20240126085444.324918-1-xiong.y.zhang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Mingwei Zhang Add host_perf_cap field in kvm_caps to record host PMU capability. This helps KVM recognize the PMU capability difference between host and guest. This awareness improves performance in PMU context switch. In particular, KVM will need to zero out all MSRs that guest PMU does not use but host PMU does use. Having the host PMU feature set cached in host_perf_cap in kvm_caps structure saves a rdmsrl() to IA32_PERF_CAPABILITY MSR on each PMU context switch. In addition, this is more convenient approach than open another API on the host perf subsystem side. Signed-off-by: Mingwei Zhang --- arch/x86/kvm/vmx/vmx.c | 17 +++++++++-------- arch/x86/kvm/x86.h | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 349954f90fe9..50100954cd92 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7896,32 +7896,33 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) vmx_update_exception_bitmap(vcpu); } -static u64 vmx_get_perf_capabilities(void) +static void vmx_get_perf_capabilities(void) { u64 perf_cap = PMU_CAP_FW_WRITES; struct x86_pmu_lbr lbr; - u64 host_perf_cap = 0; + + kvm_caps.host_perf_cap = 0; if (!enable_pmu) - return 0; + return; if (boot_cpu_has(X86_FEATURE_PDCM)) - rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap); + rdmsrl(MSR_IA32_PERF_CAPABILITIES, kvm_caps.host_perf_cap); if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR) && !enable_passthrough_pmu) { x86_perf_get_lbr(&lbr); if (lbr.nr) - perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT; + perf_cap |= kvm_caps.host_perf_cap & PMU_CAP_LBR_FMT; } if (vmx_pebs_supported() && !enable_passthrough_pmu) { - perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK; + perf_cap |= kvm_caps.host_perf_cap & PERF_CAP_PEBS_MASK; if ((perf_cap & PERF_CAP_PEBS_FORMAT) < 4) perf_cap &= ~PERF_CAP_PEBS_BASELINE; } - return perf_cap; + kvm_caps.supported_perf_cap = perf_cap; } static __init void vmx_set_cpu_caps(void) @@ -7946,7 +7947,7 @@ static __init void vmx_set_cpu_caps(void) if (!enable_pmu) kvm_cpu_cap_clear(X86_FEATURE_PDCM); - kvm_caps.supported_perf_cap = vmx_get_perf_capabilities(); + vmx_get_perf_capabilities(); if (!enable_sgx) { kvm_cpu_cap_clear(X86_FEATURE_SGX); diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 38b73e98eae9..a29eb0469d7e 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -28,6 +28,7 @@ struct kvm_caps { u64 supported_mce_cap; u64 supported_xcr0; u64 supported_xss; + u64 host_perf_cap; u64 supported_perf_cap; }; -- 2.34.1