Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp22590796rwd; Fri, 30 Jun 2023 09:53:31 -0700 (PDT) X-Google-Smtp-Source: APBJJlEqe12dfY1TWMY1gIxUe52EuAIUeYZLx1Rt5RQuSovRtKVBn9jTR/5Azv8C2W/NiyEYDSQm X-Received: by 2002:a05:6359:5b0:b0:134:c584:5585 with SMTP id ee48-20020a05635905b000b00134c5845585mr2947878rwb.3.1688144011644; Fri, 30 Jun 2023 09:53:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688144011; cv=none; d=google.com; s=arc-20160816; b=dgjsWSHo8IfosoRFyauQFgZVmcdXh1Jn1ijjFqoCRQvZZejV2QyrY+FT2yeZMHnj7H xGWPX/InMGgYnyfy8RD6bsSh05R0c8jjrRyEQ7XiIf7EjFVhC452ziByw96uS9OYxIG+ BZkMCxUNW+uKCwdEhHKr4LqBYEYViAiZa1AFVg+sgbi3Eatx1ZGdCMxQg1fo/YW2+ps2 gd/Cr8JNtVV2pAPxwmYKJ+qU1KggWlD0qMFO72ITWCh/TRT38JqpU03LrZu+ubZKQV5g loLX1rS2U5bdL21INXj98GoLcONJvhzG3UvvW1t0GPNkg9/lkebOUngMLRw8QdGMSATQ uCRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:to:subject:message-id :date:from:in-reply-to:references:mime-version:dkim-signature; bh=1O2jb2ZRq3iKub8wzToWxUbybun5ApHXKfxPti5SoXo=; fh=VYfi5qAEQdb/dqa49Pg5mh/ZxobC8aTM/F/eoDPJCwU=; b=DPv22cPAFFOtFRxFqgkVNCTFQrvRSR0wVBkkn6bdb8AsCAJmRbLcVUrqrlczGPH/Oo r1lLKWoyBSA1EvJttwCDjP7dZTHiXVfYxP4OYhz4Xq2PavSaLfkIMBOkAnE39hH96vx+ y6bCRgGKSpl6F/c3iLSC0eamfHbWOg50532nbcJhZSydNbYN1ZRi+dFpkVLzWhZ2pVM6 VuejdGmLUCWMasULuB9lr3MWzGi4aeGUcatahRE4wUJ9JUvRH6rL8/RxhKT45IyvEgWP 9x2EK3n0aa7KQJFJ2e0W++VJ0zkKrMiF7GswIZHU4RbDAzeN1K7wOhduHi9R1g8J33iA zVbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=hB54yH6r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c5-20020a63da05000000b005481915c0c7si12284428pgh.32.2023.06.30.09.53.15; Fri, 30 Jun 2023 09:53:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=hB54yH6r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232246AbjF3Qkw (ORCPT + 99 others); Fri, 30 Jun 2023 12:40:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229957AbjF3Qku (ORCPT ); Fri, 30 Jun 2023 12:40:50 -0400 Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEFBA3A9B for ; Fri, 30 Jun 2023 09:40:49 -0700 (PDT) Received: by mail-qt1-x834.google.com with SMTP id d75a77b69052e-401d1d967beso2981cf.0 for ; Fri, 30 Jun 2023 09:40:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688143249; x=1690735249; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=1O2jb2ZRq3iKub8wzToWxUbybun5ApHXKfxPti5SoXo=; b=hB54yH6rPphXZa2pVpo7ah3BwI53myyvXN1Q2T3Owg6EnRSNtzjGMkgSegbzP9GoQS W0wBiYgT+q9g0MP9+MDg+zizhwFj786nKzoAHKlsbFtf5HYx6cWhcwCwbmcWfILEiUO7 62f/waacoZXisGna2kWdP5CwK6tNadmozDerJ97LXVUv4gTEAHuI0dM5txxNNdD9RwMB P4vEMTpsmLnhuQS3jZr3VoyZMzK4ozq+VX5V8Muqy7Yu66Q7nThPDJ4jEZLck3g2FxbE diSmMbMKsL7xb7gLopsSerkwCBe7p5pSpaZIQZ3dJec1pWXkkM6aW0gflCGChTshz9Eh /VCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688143249; x=1690735249; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1O2jb2ZRq3iKub8wzToWxUbybun5ApHXKfxPti5SoXo=; b=dnO6c9jOmdWghDrqzeO0SVxMCUvSgPHTVur6Vp1PQUs8mzbon/SKIjwBrtKp3LmJZV pVlGH6rl5SVraK6u7NFV5AroKmETsJQMWJGeKRlkeerwHhjgIG+bBFWWJ6zTgJP6R4fa I+hJBXMEF4silu1VtzVHCPztVkznEmTOS8oUwGJOBKLShhBbUGIuLEVmHoFp7Poql9NY gxmzwmVo1bL+VL8JOQZiCPOjWlcuqUCHdZWNuvsULdFJUVeJp/BbNArMuva9oWmeAw+e LTZjh++r+aNOtgjcIl44nZDz+VoGnk3CLVMV89cRVVbklyr8KE2cesckEyt3zu/Yoa8m V4yg== X-Gm-Message-State: AC+VfDy2HPn+2CNXuGBSH+iTv02UUGnHxhGTjctzoSTaaDUh+/b+VLf2 LHgAYrgY3W/4cf/VS56A4f8PLSei0mZe1/xTG+47qUbWRpfuyvfQD/p4UQ== X-Received: by 2002:ac8:5905:0:b0:3f8:5b2:aef0 with SMTP id 5-20020ac85905000000b003f805b2aef0mr815604qty.24.1688143248846; Fri, 30 Jun 2023 09:40:48 -0700 (PDT) MIME-Version: 1.0 References: <20230504120042.785651-1-rkagan@amazon.de> In-Reply-To: From: Jim Mattson Date: Fri, 30 Jun 2023 09:40:37 -0700 Message-ID: Subject: Re: [PATCH] KVM: x86: vPMU: truncate counter value to allowed width To: Roman Kagan , Sean Christopherson , Jim Mattson , Paolo Bonzini , Eric Hankland , kvm@vger.kernel.org, Dave Hansen , Like Xu , x86@kernel.org, Thomas Gleixner , linux-kernel@vger.kernel.org, "H. Peter Anvin" , Borislav Petkov , Ingo Molnar , Mingwei Zhang Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 30, 2023 at 8:21=E2=80=AFAM Roman Kagan wrot= e: > > On Fri, Jun 30, 2023 at 07:28:29AM -0700, Sean Christopherson wrote: > > On Fri, Jun 30, 2023, Roman Kagan wrote: > > > On Thu, Jun 29, 2023 at 05:11:06PM -0700, Sean Christopherson wrote: > > > > @@ -74,6 +74,14 @@ static inline u64 pmc_read_counter(struct kvm_pm= c *pmc) > > > > return counter & pmc_bitmask(pmc); > > > > } > > > > > > > > +static inline void pmc_write_counter(struct kvm_pmc *pmc, u64 val) > > > > +{ > > > > + if (pmc->perf_event && !pmc->is_paused) > > > > + perf_event_set_count(pmc->perf_event, val); > > > > + > > > > + pmc->counter =3D val; > > > > > > Doesn't this still have the original problem of storing wider value t= han > > > allowed? > > > > Yes, this was just to fix the counter offset weirdness. My plan is to = apply your > > patch on top. Sorry for not making that clear. > > Ah, got it, thanks! > > Also I'm now chasing a problem that we occasionally see > > [3939579.462832] Uhhuh. NMI received for unknown reason 30 on CPU 43. > [3939579.462836] Do you have a strange power saving mode enabled? > [3939579.462836] Dazed and confused, but trying to continue > > in the guests when perf is used. These messages disappear when > 9cd803d496e7 ("KVM: x86: Update vPMCs when retiring instructions") is > reverted. I haven't yet figured out where exactly the culprit is. Maybe this is because KVM doesn't virtualize IA32_DEBUGCTL.Freeze_PerfMon_On_PMI? Consider: 1. PMC0 overflows, GLOBAL_STATUS[0] is set, and an NMI is delivered. 2. Before the guest's PMI handler clears GLOBAL_CTRL, PMC1 overflows, GLOBAL_STATUS[1] is set, and an NMI is queued for delivery after the next IRET. 3. The guest's PMI handler clears GLOBAL_CTRL, reads 3 from GLOBAL_STATUS, writes 3 to GLOBAL_OVF_CTRL, re-enables GLOBAL_CTRL, and IRETs. 4. The queued NMI is delivered, but GLOBAL_STATUS is now 0. No one claims the NMI, so we get the spurious NMI message. I don't know why this would require counting the retirement of emulated instructions. It seems that hardware PMC overflow in the early part of the guest's PMI handler would also be a problem. > Thanks, > Roman. > > > > Amazon Development Center Germany GmbH > Krausenstr. 38 > 10117 Berlin > Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss > Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B > Sitz: Berlin > Ust-ID: DE 289 237 879 > > >