Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1767861lql; Wed, 13 Mar 2024 07:42:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWf+alc2TA964Qh3VIopMa9gWoNeDKpRbW1swtDV0wWZcgWMHAUDoJ473r5KFf5xUgHVmJ2aguukJFGni3PxnkJHEFkHXBQ3s0kZauz+g== X-Google-Smtp-Source: AGHT+IHhKiLcUxTx5fdprisMpHL8x6rOyUTHhbmsLHHKW81LA3LXVcsqJZKPT5jauceVwJuo2hyc X-Received: by 2002:a50:ab19:0:b0:565:7edf:41b0 with SMTP id s25-20020a50ab19000000b005657edf41b0mr2733832edc.6.1710340966672; Wed, 13 Mar 2024 07:42:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710340966; cv=pass; d=google.com; s=arc-20160816; b=koY4QJeteU2a2PEbPCixe5NeZNdNYWtC92+uKJZlNr+GMJINdb2DsO5+BeS2pukSnF pPdLc7cKqmASfSKlzGswWXYnAy6DvZTFiS2+ZEXHrLz4yc70HxS6lASJVSDgzsDMv+2T xCvb9s30QHbDRcclLDFGoaAsapH9plMcRi2AnGWhtIteVv/+MVX8mCrmNLNmMAKf6mrm LnePvxzNulCzp+tQPwM8SjYpXa29fhCc4m2K0/2ASeCGgMn1N4HOFp6/S2oNaguzG++d Xm7+UjIV4PYTaHny+VqjPluqnRP+uxyoSbiWhmI3PW1Ij4cYLfTBTD32rkD8facwV6eu eV5Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :in-reply-to:date:dkim-signature; bh=DsNKRRM5eqSpZG2xIM+UvbeP4kbCL6MaPJhxll06mGE=; fh=adJeVsn6FBFEwQYhOHe1we2YXFmYiE7+I2xt6mK15RE=; b=FWDh7kYW5Kk5lVmNVDJYWBLXr0YYTGEpCKJuEKt+/w/psdvA7sW3T7MYOneBTJsMW4 tKqffUq5XuzAjbSqXyLVQazeGI/OUqNZJ64whjexkTNDV80uYx2D4yrL4hJGUcq7Rxqt jXwVucG2h4ebuMLTTh0wwjfHL2SIJGC3mR3ICsS1n7P3BLZQ17r2HiBje15wu8jG6ZaQ DFJ/Nk4jdwdWXp+hU3oBQDZqLQ5ijB/gjdlfv2lTRrNZPRoD+WGX38KlvK+8L7iUhUPA 7XeJZ4/2eMXisEb73O5o3DtHTLx5ZFZxWpRTvSwMFKtyUljI2QCnVIdWyIH//PHXYneU ovBA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JOmX3w+o; 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-101632-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101632-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id eh19-20020a0564020f9300b005671b2d704dsi4650814edb.451.2024.03.13.07.42.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 07:42:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-101632-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JOmX3w+o; 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-101632-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101632-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 374A51F21CEF for ; Wed, 13 Mar 2024 14:42:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3C06D3FB01; Wed, 13 Mar 2024 14:42:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JOmX3w+o" Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 C06304A0C for ; Wed, 13 Mar 2024 14:42:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710340942; cv=none; b=Az5fNzOorcR3YKnTnCg+hgrhmWL90p1jqislOSInfxjk9WefoqZHqLIu1LrIZLfSfc/OX2xYik2A0tpRSkOZQsBo2SQu1ZXgNthUk+ZuDTNEEMerQ0RgINiCQwiuuTuXME2iSJG/YXpFhx7LtQ5T3/eQdsOMxISBh9LAOL7z1mU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710340942; c=relaxed/simple; bh=chSdJDUtKzDuTUHtdpxMes7gNjiM378d/wKDulIvMMY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sAa7ms86WpR5jTd+FxpzyonzRNwAeT/bkSIRdmfSiO3IZe/dwhW+D2kJsrBwBuueLARDXK7ISEaadMbRFHLbd2ox3em25mY5U7ZUNxIPah67pZAEECjqHDk+qMkCiOdEguLJMdelO3ym9Wc6ut/2y9y1tnKyjXlOmqr08e46ahE= 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=JOmX3w+o; arc=none smtp.client-ip=209.85.214.201 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-pl1-f201.google.com with SMTP id d9443c01a7336-1dd916d7d55so10726585ad.2 for ; Wed, 13 Mar 2024 07:42:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710340940; x=1710945740; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=DsNKRRM5eqSpZG2xIM+UvbeP4kbCL6MaPJhxll06mGE=; b=JOmX3w+oqIU6KuLkFHyDzPpwKe5G+DEeYBq48twDtZ5BBeXcmlYbUygc567atDUNcf lRDKZdU88N4xBMJB0tPDWKuThr+fxzaFaxa87LkVzTdbGkrYEDiUzgiscT1x81P+stll 7V8fLehlMOt7md/z3HwTzm3PM38E78ZPKIK7HvwcL9HUxHjT3XBB8QodRq+jEeTcGcPA ORPV5hcsg4LTMDqibuWdCyQ5mHxqSKodbkNzC+bLfay7eTiFBbAoL7QzK5VMPJNJKH12 15Gt8IgWkGsAyaotGbUMnh0wITvnZlKzdd6x9SDjuE1XLQe3VJSuN4ZtL/WFNYeFvdiE gBYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710340940; x=1710945740; h=content-transfer-encoding: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=DsNKRRM5eqSpZG2xIM+UvbeP4kbCL6MaPJhxll06mGE=; b=QExlCZ4ha+L94Y43wk+nHpStG8nkiXj8di0wH6hCfnow2pDGwx84okVEV+d6ddgIIE IsvAfPLAn4kqB0WU28DzNdt8jc9WpW8QgnILQw5RWi0Cs4ZyGwbJbFBZujN81DWhfneo puPiArJHmwtUGw6uqbeBSNdATTzGkuK9SCtUFLgwy27LUOPY+0doyHuRe8LRj77bVLWX ugCI7vdUhCLNHSODSOn+qLfqXUE00FbVgC/b6vjHIochNYb8nrIMElgdDU2dBLNILEae Fwn0/cTP68B8zWFDEODwX1ivutkPgdYDZvS6KAzbQWd9n04MhqrMBzjdAygjz9r6KdA6 ZVJA== X-Forwarded-Encrypted: i=1; AJvYcCUuFdczYDaqewEd2VNWsCL6P0CCORHlFCCtpj+GyR4/HhsesRQSgkMDa1/zPOuE5KRen3mHko3QZHfKJ+Vy3G6aenvgqON9F2WaUVGj X-Gm-Message-State: AOJu0YxwlM0MSHH3dcxSNTy2gC4RfgfdHPLkrqDc9anfR4TgjKD3RA8p +bJgO5fkE4SrlxirGYA/q7cxn0OjsvxMUwH3WdAa2ejB3sPyB/LToi/A8BnJ11c+7NnldAfgr3K sqg== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:41cc:b0:1dd:9250:2d5d with SMTP id u12-20020a17090341cc00b001dd92502d5dmr8798ple.9.1710340940087; Wed, 13 Mar 2024 07:42:20 -0700 (PDT) Date: Wed, 13 Mar 2024 07:42:18 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240313003739.3365845-1-mizhang@google.com> Message-ID: Subject: Re: [PATCH] KVM: x86/pmu: Return correct value of IA32_PERF_CAPABILITIES for userspace after vCPU has run From: Sean Christopherson To: Wei W Wang Cc: Mingwei Zhang , Paolo Bonzini , "H. Peter Anvin" , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Aaron Lewis , Jim Mattson Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Wed, Mar 13, 2024, Wei W Wang wrote: > On Wednesday, March 13, 2024 8:38 AM, Mingwei Zhang wrote: > > Return correct value of IA32_PERF_CAPABILITIES when userspace tries to = read > > it after vCPU has already run. Previously, KVM will always return the g= uest > > cached value on get_msr() even if guest CPUID lacks X86_FEATURE_PDCM. T= he > > guest cached value on default is kvm_caps.supported_perf_cap. However, > > when userspace sets the value during live migration, the call fails bec= ause of > > the check on X86_FEATURE_PDCM. >=20 > Could you point where in the set_msr path that could fail? > (I don=E2=80=99t find there is a check of X86_FEATURE_PDCM in vmx_set_msr= and > kvm_set_msr_common) The changelog is misleading, it's not the PDCM feature bit, it's the PMU ve= rsion check in vmx_set_msr(): case MSR_IA32_PERF_CAPABILITIES: if (data && !vcpu_to_pmu(vcpu)->version) return 1; > > Initially, it sounds like a pure userspace issue. It is not. After vCPU= has run, > > KVM should faithfully return correct value to satisify legitimate reque= sts from > > userspace such as VM suspend/resume and live migrartion. In this case, = KVM > > should return 0 when guest cpuid lacks X86_FEATURE_PDCM.=20 > Some typos above (satisfy, migration, CPUID) >=20 > Seems the description here isn=E2=80=99t aligned to your code below? > The code below prevents userspace from reading the MSR value (not return = 0 as the > read value) in that case. Ya. > >So fix the > > problem by adding an additional check in vmx_set_msr(). > >=20 > > Note that IA32_PERF_CAPABILITIES is emulated on AMD side, which is fine > > because it set_msr() is guarded by kvm_caps.supported_perf_cap which is > > always 0. > >=20 > > Cc: Aaron Lewis > > Cc: Jim Mattson > > Signed-off-by: Mingwei Zhang > > --- > > arch/x86/kvm/vmx/vmx.c | 11 +++++++++++ > > 1 file changed, 11 insertions(+) > >=20 > > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index > > 40e3780d73ae..6d8667b56091 100644 > > --- a/arch/x86/kvm/vmx/vmx.c > > +++ b/arch/x86/kvm/vmx/vmx.c > > @@ -2049,6 +2049,17 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, > > struct msr_data *msr_info) > > msr_info->data =3D to_vmx(vcpu)->msr_ia32_sgxlepubkeyhash > > [msr_info->index - MSR_IA32_SGXLEPUBKEYHASH0]; > > break; > > + case MSR_IA32_PERF_CAPABILITIES: > > + /* > > + * Host VMM should not get potentially invalid MSR value if > > vCPU > > + * has already run but guest cpuid lacks the support for the > > + * MSR. > > + */ > > + if (msr_info->host_initiated && > > + kvm_vcpu_has_run(vcpu) && > > + !guest_cpuid_has(vcpu, X86_FEATURE_PDCM)) > > + return 1; As Wei pointed out, this doesn't match the changelog. And I don't think th= is is what we want, at least not in isolation. Making KVM more restrictive on us= erspace reads doesn't solve the live migration save/restore issue, and the kvm_vcpu= _has_run() adds yet another flavor of MSR handling. We discussed this whole MSRs mess at PUCK this morning. I forgot to hit RE= CORD, but Paolo took notes and will post them soon. Going from memory, the plan is to: 1. Commit to, and document, that userspace must do KVM_SET_CPUID{,2} prio= r to KVM_SET_MSRS. 2. Go with roughly what I proposed in the CET thread (for unsupported MSR= S, read 0 and drop writes of '0')[*]. 3. Add a quire for PERF_CAPABILITIES, ARCH_CAPABILITIES, and PLATFORM_INF= O (if quirk=3D=3Denabled, keep KVM's current behavior; if quirk=3D=3Ddis= abled, zero- initialize the MSRs). With those pieces in place, KVM can simply check X86_FEATURE_PDCM for both = reads and writes to PERF_CAPABILITIES, and the common userspace MSR handling will convert "unsupported" to "success" as appropriate. [*] https://lore.kernel.org/all/ZfDdS8rtVtyEr0UR@google.com