Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp22628263rwd; Fri, 30 Jun 2023 10:20:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5K2WrGfzAoGpr8iBVKAcHxB8tZ34Szj3/Re//QbOLqWfuH6WZ8knT7LdAp6HybDkash1Vi X-Received: by 2002:a05:6808:20a1:b0:3a3:3a0b:aab8 with SMTP id s33-20020a05680820a100b003a33a0baab8mr2944705oiw.48.1688145610473; Fri, 30 Jun 2023 10:20:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688145610; cv=none; d=google.com; s=arc-20160816; b=RfRxrhi4G2o9cD0ubCp8dx8QMuEahuW4ftt0RWLFfmjLnQsI7j5fByXdN6282P9CD+ px/Aos/fBw52PdQ16rWoYs/GbgTWtCmWqW8zaG8GXd7/DCIUuBlvwwi3/bgDL72Wh1Bd cvMjPIS9BaTOVioShSanjOsxnfDORZjvVjDQ2QvErkM5cSzpgHcg9a/JLYBsDR6YI96T jfxK7XmaKLbt+9jDczTiiMfMEXF8RiWVSVzYr6wbr6l8i7ibGvQPcteaxgcQlUD9YLEi Yzmf+7nY1cZaS9f3SpxSILBMIZsSPIAx2cnVOI8olQDvmgbcZjuhzO6H6XMwfpeOSmsW xTbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=djbszLtoHHDMZ0fSdJiXx0t0hVE4VrfDsRtDQpq854Y=; fh=XHPnnk2HM+nzPyN23+vFJlTu4sJqCRR6kvrz1AtBqR8=; b=AwH1PNqrJD+TrQoggr5THpWSn5FAuII/bAnbSNY6YfzaoAGkDTFcjAsuwWJGo3XUHT AOzLFOQrfQZCe6+nCMVC1eX9th8DK5/PEsk0msMhiELZjhX09ZCFj1odG8bU/mFpFd8S aRZvsedPrJE9rzyqnUZLLiGzU/mlhAjsVjJOlmDGzJuirpu6Olnyd614fU0H2fJ2JHgv +R4f/kYAsJlp1Bk0o/Jx9AcPeWySmwRyys8NSuUdJjIm9NJ9D7g/dKx7JHEh0LPeoDCH oAnkj8ZaoZEmNOeZ4aEZRkCaNzDFGzNKhithkZfT8qgM2ZqDXKTQ+AYvthcjzivH+RSu vFaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=3PtJ+g5t; 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 u1-20020a6540c1000000b005574bef6f54si12871410pgp.486.2023.06.30.10.19.57; Fri, 30 Jun 2023 10:20:10 -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=3PtJ+g5t; 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 S232873AbjF3RIl (ORCPT + 99 others); Fri, 30 Jun 2023 13:08:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232883AbjF3RIa (ORCPT ); Fri, 30 Jun 2023 13:08:30 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64F263C3E for ; Fri, 30 Jun 2023 10:08:17 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-98d34f1e54fso231997566b.2 for ; Fri, 30 Jun 2023 10:08:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688144896; x=1690736896; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=djbszLtoHHDMZ0fSdJiXx0t0hVE4VrfDsRtDQpq854Y=; b=3PtJ+g5tJVeNWTN9Ljx1dw7SLn5XosBx138xqjpGi4HRgfV8/lV5msek82vI7WzAcB f+nqVrAXRlxGnxi06J3cwEbTEXX3wwlj5znjrhrGTnIne5y6ZvD6Nq0BN7mylz+TnGue evW7if1wrjiiFzebMjDtoq5aK56wwaTDeRMNBaRsFv0JKbbQUEVX+GFwnja//KIcMV6h pKsLJ+m0Y6HqFVK1283i90jwnAKjuKmLnMFUrzpz2eLId4YHfd0C7s6lOCMP5s8SWi/+ 71aJRE2u3wILt3Nc5SCc/AxjKed6H8/z/zqo/FnNeNibFt2XY4Xz45HwXTytSAotDe7k kBVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688144896; x=1690736896; h=content-transfer-encoding:cc: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=djbszLtoHHDMZ0fSdJiXx0t0hVE4VrfDsRtDQpq854Y=; b=h4y2W6iV6Jg1XtZ+Kr21ET4Kmctd7Ljcw1fqbEddr7K1DwSedW2e/59xycEy9SwlWl Jj7oIrlUDYZdNxYn2takPD6EAvQFqlJliHIutwqolK3C8RFdwPNQ5yYx5Hh5C1k7WDOp FJs60zrf4lLNODIuS7MWlPvXeG2zEVWnJRkvfEAzdlz38b0DRr7akkDogf3LxyEtyh9Q jyX2i8XDf61BZol/tR6RC9d+lRAoqpvAO6rV3+xVXCPNIyTG6s2WP7ePP4/gTjdXrIU7 dwatgrgjaOnSamuxycu4nWdsfVbKimOU94z7cjPG/ESLrvjiq33NefgA4q9VQEFjklCv xjig== X-Gm-Message-State: ABy/qLZtj5fa+lfO6hbuIFH52y5DGoM/9Vx5HmdfjU5zcmUMFk2wwgZ4 9IqWBS3t7dFlJEcVKn73gr7N3unNmFhcJGn2C32ydQ== X-Received: by 2002:a17:906:9142:b0:958:4c75:705e with SMTP id y2-20020a170906914200b009584c75705emr2156152ejw.17.1688144895628; Fri, 30 Jun 2023 10:08:15 -0700 (PDT) MIME-Version: 1.0 References: <20230504120042.785651-1-rkagan@amazon.de> In-Reply-To: From: Mingwei Zhang Date: Fri, 30 Jun 2023 10:07:38 -0700 Message-ID: Subject: Re: [PATCH] KVM: x86: vPMU: truncate counter value to allowed width To: Sean Christopherson Cc: Roman Kagan , 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 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=unavailable 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:45=E2=80=AFAM Sean Christopherson wrote: > > On Fri, Jun 30, 2023, Roman Kagan wrote: > > 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_= pmc *pmc) > > > > > return counter & pmc_bitmask(pmc); > > > > > } > > > > > > > > > > +static inline void pmc_write_counter(struct kvm_pmc *pmc, u64 va= l) > > > > > +{ > > > > > + 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= than > > > > allowed? > > > > > > Yes, this was just to fix the counter offset weirdness. My plan is t= o 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. > > Can you reverting de0f619564f4 ("KVM: x86/pmu: Defer counter emulated ove= rflow > via pmc->prev_counter")? I suspect the problem is the prev_counter mess. For sure it is prev_counter issue, I have done some instrumentation as foll= ows: diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 48a0528080ab..946663a42326 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -322,8 +322,11 @@ static void reprogram_counter(struct kvm_pmc *pmc) if (!pmc_event_is_allowed(pmc)) goto reprogram_complete; - if (pmc->counter < pmc->prev_counter) + if (pmc->counter < pmc->prev_counter) { + pr_info("pmc->counter: %llx\tpmc->prev_counter: %llx\n", + pmc->counter, pmc->prev_counter); __kvm_perf_overflow(pmc, false); + } if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL) printk_once("kvm pmu: pin control bit is ignored\n"); I find some interesting changes on prev_counter: [ +7.295348] pmc->counter: 12 pmc->prev_counter: fffffffffb3d [ +0.622991] pmc->counter: 3 pmc->prev_counter: fffffffffb1a [ +6.943282] pmc->counter: 1 pmc->prev_counter: fffffffff746 [ +4.483523] pmc->counter: 0 pmc->prev_counter: ffffffffffff [ +12.817772] pmc->counter: 0 pmc->prev_counter: ffffffffffff [ +21.721233] pmc->counter: 0 pmc->prev_counter: ffffffffffff The first 3 logs will generate this: [ +11.811925] Uhhuh. NMI received for unknown reason 20 on CPU 2. [ +0.000003] Dazed and confused, but trying to continue While the last 3 logs won't. This is quite reasonable as looking into de0f619564f4 ("KVM: x86/pmu: Defer counter emulated overflow via pmc->prev_counter"), counter and prev_counter should only have 1 diff in value. So, the reasonable suspect should be the stale prev_counter. There might be several potential reasons behind this. Jim's theory is the highly reasonable one as I did another experiment and found that KVM may leave pmu->global_status as '0' when injecting an NMI.