Received: by 2002:ab2:7407:0:b0:1f4:b336:87c4 with SMTP id e7csp83143lqn; Thu, 11 Apr 2024 14:49:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVs/q3gQRnLNqn+5GUc9erOmWxLlFbkfXe10F8VjxnRndwPaluazeUGnIEPyqnKB0RrinncX3T8W/YrioY/TSHTYI+zDH68T0ppi0kz5Q== X-Google-Smtp-Source: AGHT+IGAyE3P69QPCH9yZiG3d2e565Jq+MtY+gGl6bqnTL3sTBi1UiMZhm84k+mcDiB6tLw1vMjH X-Received: by 2002:a17:90b:180a:b0:2a2:dcfe:bd69 with SMTP id lw10-20020a17090b180a00b002a2dcfebd69mr5185647pjb.13.1712872162761; Thu, 11 Apr 2024 14:49:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712872162; cv=pass; d=google.com; s=arc-20160816; b=CPYA4XgMw71PJS8gcFweXqAtVdzio+1yGVhWPpKhEX1ilhBmRtgeIp1aKN7JstdZrp Bt7V4NSJXKK8K/3KmL3jq+giBoX6ZiizqawVSLDoaDkIU+Zs++3CaNuoaAewrcehwzU4 ULAAGekEEsq1O6xliJLYYaaX23S/5mk0xb2FOUTWGeehV75cJPb+xakeaFSS+n5VlHwa N/Is/BhO+abGifTb0wEL2CcxoYc7XnzRaIh/8lsHPbGeRlDJWBLmnJ6tWrK3RJJLnCP9 cCL+lOpL0/P9xDqs/CYc0BTR/AChYFVd8yWXA/VBcZbPX6PYB/Szk6n2FKyqfnaTr7EY gYnw== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=E46uucbaaoqsuZZvyzOBFLngOEYFFwjcligp3RIvVUA=; fh=M5xBWokht33GHXUxmUJ0ZSNCIrIeumPXVggfHgJ4hvA=; b=wyrWJaBkvcBzAsqpcOJKn2DbAvTwKetcI8hL6ZmGn+Ti0LskIcnY1l6iNPKulkfFu8 BVduiQYPxUpqS8wTYmpurk90WhjpUGCg+XnelZbD1wDjW4F423H98ni1Wu8iuem0e0Vx YG4atmsdEu02Lg3T1OZI/XqnKe5m+yN/1HYMbIQlWX+Llm+wvlCARULX9JYgVGD5WJkG H1hBfMhqys/6pCxbIYbn+n0LQcEncTbBNnVzoBDDUnBg1XJAgaLd9eSuXVObL+jwxYRp GGL7k2wA3/koooAh0MR63f1I0fTZ4RJ0GBIKPauV3NguAqrrP1CAKBQPWQm70wZF/2y0 74iQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fz0dM8Qr; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-141641-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141641-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id v12-20020a17090a088c00b002a4f0f7a137si4101024pjc.102.2024.04.11.14.49.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 14:49:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-141641-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fz0dM8Qr; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-141641-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141641-linux.lists.archive=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 3EEC9284D44 for ; Thu, 11 Apr 2024 21:49:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D206A3B798; Thu, 11 Apr 2024 21:49:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fz0dM8Qr" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 92B663BBC2 for ; Thu, 11 Apr 2024 21:49:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712872152; cv=none; b=rbuuWNkp4tGdIfLtzlpaiGo7hzjVUWZ1Vvl4t9QKBIUZezfpzPGBE4kzrDhGkyyLeNccPbOPy9ccC6lxja4Ry02znAh0WcPk+xfI/WEAH09krqSIiMfow97Zo3iJwjV3TJUvjQzWmkbq3onLuxsX6cA+JMfZSNNuA+WI41NLEMs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712872152; c=relaxed/simple; bh=YNOXp9b/Jpb+es+bSQ62skx+KBDAECbNrG40A/IivHA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oA0Pzb7lZA3kIge5ZgOeYgg2mn4hzGJJtx4csqtG23JE9SvhCAMVg7Ri4gTcGByYqOaoV6/5zUGo0bth3DtnP9ZoCs/u+m53fUM+MJQ5xUd58TH27vjVmiPzEQw53ULhUKR0EZnzf09xkTrWhJiZU/aLD7la95F5BVayxxeam4Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fz0dM8Qr; arc=none smtp.client-ip=209.85.219.202 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-yb1-f202.google.com with SMTP id 3f1490d57ef6-dd933a044baso1640591276.0 for ; Thu, 11 Apr 2024 14:49:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712872149; x=1713476949; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=E46uucbaaoqsuZZvyzOBFLngOEYFFwjcligp3RIvVUA=; b=fz0dM8QrZYckAcpi5DyJDPwgQSGXvY120TrbGeIwqVEckqJ8WjXLXe4KsufnIfIaZs hLrMZrrdPe7SYCoxmYtNiPvgd/kAKTNpdcRN1BPgCtKRvF1W6C7lfEShhzps2CV1zqRI bmyIZee7lQICCo7zgVvvTt/UnHXPxs7Hih6OaxNX4noSESUNyKH/m6QhfcbmTqv9PpHl MlnAv9zHx4CKbwdeW3fMjeSJrFG5saiFN9hyoWX+T2dQQ09i9eveNSvGkOCkyy+A3DH5 BaipWAfEYEP55TyPEoD3sc86ptjgXRgCJIzylTHWMy5iFvryOVKU5BTQ9oC5IlUE8rKH L8ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712872149; x=1713476949; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=E46uucbaaoqsuZZvyzOBFLngOEYFFwjcligp3RIvVUA=; b=n9B7iRo0IhvhAPfLv7cViEH9o4UbizGH6YuDVn5tpGPcod4YzUZZ+r5kygB6KKowHV 4w6ZJtTco0thn5dZjjM0uCXIUTQooshaMH4w/gAG+2+y9mLF9VFkk4Ke6OoSoHQ5BlnR JIl4qbQfVlhrFlktFomjNoYmB0yveaKv2v7FltpH6zUMOckqdoJS/+PzMGQIkVW4o9jT 9Oew212s+B/sSUuOibtV76EYKmgXJJpTUVBquvlXofgcPJPH/PN6paudAHdvAp6J8Gn6 BCN3qOpKuqslzuwt17CQ/u1KAAep8C5CcGad38GAICn2xc1Std5hc0YSX9OHpVpVLWT3 tQHg== X-Forwarded-Encrypted: i=1; AJvYcCWo+iMCoi5rPbCyCdY2wyVh5hemoBmhp/A0MmmafhxwdeFFqv9t3ZOGHXHA793dKj0Uz2ij/dttWhyHd9r3x0LVvenYx6clOAuVjkT8 X-Gm-Message-State: AOJu0Yx7pZUazMrH2Yq941sKuH9KAF0oxKcfyza4Hsl5SnjiIci1L0xA Zd4SR6zmGsuh+AKjFFpGVpYTe0fZA4unGZqPrj5Fb5ch1D2SQbWMbm2Qrjr891imx7mn6agL19g oWQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:ab70:0:b0:dc7:463a:46d2 with SMTP id u103-20020a25ab70000000b00dc7463a46d2mr626352ybi.0.1712872149661; Thu, 11 Apr 2024 14:49:09 -0700 (PDT) Date: Thu, 11 Apr 2024 14:49:08 -0700 In-Reply-To: <20240126085444.324918-27-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 References: <20240126085444.324918-1-xiong.y.zhang@linux.intel.com> <20240126085444.324918-27-xiong.y.zhang@linux.intel.com> Message-ID: Subject: Re: [RFC PATCH 26/41] KVM: x86/pmu: Add host_perf_cap field in kvm_caps to record host PMU capability From: Sean Christopherson To: Xiong Zhang Cc: 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, 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 Content-Type: text/plain; charset="us-ascii" On Fri, Jan 26, 2024, Xiong Zhang wrote: > 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); I would strongly prefer KVM snapshot the host's MSR_IA32_PERF_CAPABILITIES, if the CPU has PDMC, i.e. not leave it zero if the PMU is disabled. > > 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; This is confusing, host_perf_cap doesn't track "capabilities" so much as it tracks a raw host value. Luckily, I have a series that I am going to post this week that adds another struct for tracking host values, e.g. host_xss, host_efer, etc. > }; > > -- > 2.34.1 >