From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 75559379EE5 for ; Thu, 9 Apr 2026 21:21:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775769678; cv=none; b=uJGGnavNP9CDMS7ME/b20mwNNKmNhR/HhaFKo1LCRfkZn9V64QeMpsC4gqSOfP0TeRF+tk+mA3ml+XOUbEPAEtNpnwwtt5YwDgFLUFV0esxTibWn3FEdpyy5Y/4XuTUeyIZfU0F+AbxZKeuAfczCFNs1u7dN7Ssqr2vHGtGx7NU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775769678; c=relaxed/simple; bh=2wHI8W/EXibXyk6vk6DiujQnKf/gejgiKm+JT7pH3FA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QwaaBgeSOFRPHI1/Z2UVIePQJBjYlxnzwo+aLhrcvWgdR3B3SJuAPW+3UgDH2sMxIDs2kWBFx6Ek8crnQeCHvmbhbe+T6FdVvAg788nfIznaSSqdcGecCNs288D4v1avziTy8XqphbpL00i1+m0q4eIKcrzOEvmvtuyAYJVmODY= 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=pS7cZDcl; arc=none smtp.client-ip=209.85.215.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pS7cZDcl" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c709551ec08so1671333a12.3 for ; Thu, 09 Apr 2026 14:21:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775769676; x=1776374476; 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=ewFyxwGAGIIyCKqaXz10XpbtyAsV1BgckZS9sWmcPds=; b=pS7cZDcl4T/nSgmnt36+fC6uZOgSDbn5yEGgQ3MCToahUJMMVtgEqxFFZPQ3kw+sj0 6mqygZfons6YWJmkwVbSPigpQCqxgKjdwXBfYy0fwyPGNN4tgzBVtWtVEkLqQ7sjIelI vkKK35U2wG8gChYIzLaLYioSbgvQJ7ttKI11c7KxAv5z+4u0Otr4yVA26dR3RFevMh1r sgF+LCW2vrjs17gkbT3+ZrkjeQ55xKC2lnZd7exq+97sUmThMeYGx3hDqYMkMXb3udSo ylbgZHfEWpgo54Ak7gvFhDjAUTKkupVvb2YA5RTABrh1o0VJr284yl89rnRLn+RQumHB aJlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775769676; x=1776374476; 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=ewFyxwGAGIIyCKqaXz10XpbtyAsV1BgckZS9sWmcPds=; b=FI7QYEVGUJDXmZUrn4nxePtzTsQTY1hkmOK+VtxQGAHvMr8ipwAKFGeBFfx5BE2ZzP hD+a2AyMrMr2s0SvPNx4RJ3OMyij1mCh/BPTPMyn7B68qtiJWqTEX+iJQ/LzY6gKhgje mX3OcRDeGYJ9SorFLE86ToUxEVsX84LKJi5Judtoolnt0an/aYPsGwW7vUBoIOK3r9Zg hiGpcqgp2iTSVz5GPf2LzzcccjJ7PUI37b+XKibaCoszeCTgJf6VUw6samE1jXt5jXTI beOGNDOQhaKFw0ujlDIq+bLoEMfb3uWde/+t+tyd5LEblHllmsvkyHioLwi0jS/QKTs2 8BQQ== X-Forwarded-Encrypted: i=1; AJvYcCUR+S77eGfPajVNq0sDLU5qnOwx3cX6hpYbDyYyDxlf0HT+jOfm+9SQtqilflUaxUI2olC+NKWl4NZv/Ec=@vger.kernel.org X-Gm-Message-State: AOJu0YzTl1I9cV1CKXSH2VZY2DCFkNQKO8taqqGBL3KtTMvPcq2nReHF v3BQW6pPU7B+gMhYEEfXfT1Vk85dYubnzq72Ioi5SbLF2Zt/Ne9YfwGj/wHhC9ny2pZz+9Y9T/W fFXtfVA== X-Received: from pgr27.prod.google.com ([2002:a63:115b:0:b0:c74:1919:890f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:e083:b0:398:9d1e:1c6 with SMTP id adf61e73a8af0-39fe3791a90mr580399637.0.1775769675591; Thu, 09 Apr 2026 14:21:15 -0700 (PDT) Date: Thu, 9 Apr 2026 14:21:14 -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: <20260326031150.3774017-1-yosry@kernel.org> <20260326031150.3774017-5-yosry@kernel.org> Message-ID: Subject: Re: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions From: Sean Christopherson To: Jim Mattson Cc: Yosry Ahmed , Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Thu, Apr 09, 2026, Sean Christopherson wrote: > On Thu, Apr 09, 2026, Jim Mattson wrote: > > On Thu, Apr 9, 2026 at 10:48=E2=80=AFAM Sean Christopherson wrote: > > > On Thu, Apr 09, 2026, Jim Mattson wrote: > > > > > > In general, this deferral is misguided. The G/H bits should be > > > > > > re-evaluated before we call kvm_pmu_instruction_retired() for a= n > > > > > > emulated instruction. > > > > > > > > > > > > > ... > > > > > > > diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h > > > > > > > index f1c29ac306917..966e4138308f6 100644 > > > > > > > --- a/arch/x86/kvm/x86.h > > > > > > > +++ b/arch/x86/kvm/x86.h > > > > > > > @@ -9,6 +9,7 @@ > > > > > > > #include "kvm_cache_regs.h" > > > > > > > #include "kvm_emulate.h" > > > > > > > #include "cpuid.h" > > > > > > > +#include "pmu.h" > > > > > > > > > > > > > > #define KVM_MAX_MCE_BANKS 32 > > > > > > > > > > > > > > @@ -152,6 +153,8 @@ static inline void enter_guest_mode(struc= t kvm_vcpu *vcpu) > > > > > > > { > > > > > > > vcpu->arch.hflags |=3D HF_GUEST_MASK; > > > > > > > vcpu->stat.guest_mode =3D 1; > > > > > > > + > > > > > > > + kvm_pmu_handle_nested_transition(vcpu); > > > > > > > } > > > > > > > > > > > > This happens too late for VMRUN, since we have already called > > > > > > kvm_pmu_instruction_retired() via kvm_skip_emulated_instruction= (), and > > > > > > VMRUN counts as a *guest* instruction. > > > > > > > > > > It's just VMRUN that's problematic though, correct? I.e. the sch= eme as a whole > > > > > is fine, we just need to special case VMRUN due to SVM's erratum^= Warchitecture. > > > > > Alternatively, maybe we could get AMD to document the silly VMRUN= behavior as an > > > > > erratum, then we could claim KVM is architecturally superior. :-D > > > > > > > > Here, it's just VMRUN. Above, it's WRMSR(EFER). > > > > > > But clearing EFER.SVME while in the guest generates architecturally u= ndefined > > > behavior. I don't see any reason to complicate PMU virtualization fo= r that > > > scenario, especially now that KVM synthesizes triple fault for L1. > >=20 > > L1 can clear the virtual EFER.SVME. That is well-defined. >=20 > Gah, I forgot that the H/G bits are ignored when EFER.SVME=3D0. That's r= eally > annoying. What do you think about having two flavors of kvm_pmu_handle_nested_transit= ion()? One that defers via a request, and a "special" (SVM-only?) version that doe= s direct updates. Poking into PMU state in arbitrary contexts makes me nervous. E.g. when ca= lling svm_leave_nested(), odds are good EFER isn't even correct, and the update *= needs* to be deferred. I definitely don't love having two separate update mechanisms, but it seems= like the safest option in this case.