Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1421505rdh; Fri, 27 Oct 2023 13:55:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEVW4IGvySyfKkOASx59Fmx3KokkAXnV3eSxJoJyEcH8yZx6ibeoU7AiHVwmoOJLGS+TXM0 X-Received: by 2002:a05:6870:124a:b0:1ea:1667:6c8f with SMTP id 10-20020a056870124a00b001ea16676c8fmr3564581oao.50.1698440121422; Fri, 27 Oct 2023 13:55:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698440121; cv=none; d=google.com; s=arc-20160816; b=1FOCP4rJmbNI6eOEd/wpeMb5yvTG0mLfWDVf9AbNWA79FnYO9HhCkvHT8puaaKZMm/ BKsPfd3GvAKZMHu+R/xi/Wh13Yh7CdFAV00dw7aNcn0LiRko8/Xd7Vu8b+Pl9llx4UbO dsgY8MWVjbI0kDsgkHnD+KDSohNWBG4sIjFD84edT7aV9EXpBLiVIwDz8VAKmOQjpAqp WcVs2/pApRoI0DmGaE4tA20Wzg1/zPJd66OlYeVYHl2qVOXKMIvELWe7DHrGBeJ6ZuUv bw6WJisEKYzIlgHoq91R7cxLNmEPJgEw/hASZjQwp6RIbAU0AECaaPFKCxpVlI/24kvO g1Zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=3ONx5P+u41AFQig13/rFYqh/7sU4tfrNPS3sVnIzMbM=; fh=3NqlTUmdcbOdJ8/A+NGsDmU4rzvreaenag6WKBWWrAo=; b=AcSv0EkycJCCUb6cSOjAWtYU4NFm8PvV8eZgfy2uZD7uEUxsP5Sx3PGOyyIWB6UDKs 5f3jpDKEVvOIETs4O383xUYQZUNIdZET+9EsUh9AhWNZduQXZJFIEQeQ4Cwv8tiglvGN AMZ3au2e5NyEfoiU5bAY1oOBTEM4G8hZqR5LlSuw3L4dJiJvk+Wxod/KAko9ewR7z0PE ArzTCoWTyNugdo0Tv49WjwRh3N+scmyQjDfFvIuQOePkrVyeZApKoGhnb/XNIpgnsqhs hvkwm1hESot0UivvPl0DWViuIDsCDITsnCCMvWX/RJtGHEYzmB7rY14oYUTxAH75v6wC uDzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sjbmNdFu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id i127-20020a0df885000000b005a7f27f8f45si3331141ywf.528.2023.10.27.13.55.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 13:55:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sjbmNdFu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 115F7829588C; Fri, 27 Oct 2023 13:55:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235003AbjJ0UzR (ORCPT + 99 others); Fri, 27 Oct 2023 16:55:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229712AbjJ0UzQ (ORCPT ); Fri, 27 Oct 2023 16:55:16 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EC2C106 for ; Fri, 27 Oct 2023 13:55:14 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1c9bc9e6a89so22701715ad.0 for ; Fri, 27 Oct 2023 13:55:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698440113; x=1699044913; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=3ONx5P+u41AFQig13/rFYqh/7sU4tfrNPS3sVnIzMbM=; b=sjbmNdFup6m/yOleW7YhMzcMHGCh0XF3bh/pnNIueqkred6vJWp4yJ5fkSuD3Tc0xc HP+msDVgqOBIws+uLCHC3JyuAnbKTPsvmkh9C3O8gnmSXz7el8UHd147tk+DkYGHTcha dRJAtKsw7D5N7lGld6PgpbTwtcOqrN7k3+CD5Uzd6+cSnefZO0UwU91SL9DSQUUZYiKg Qt4f87eDbMVvvarucmXBW0QFcbOoWmjNZcHbnzz+XiUa7/184adM/3nSANpcAPrglaCj jrJDqk7aLHUNgpM2CRhQ8bZUM60yqVDlEPlw3f+6wdBducEHbEX1xAdv7h6Lv+6gdfHd TcKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698440113; x=1699044913; h=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=3ONx5P+u41AFQig13/rFYqh/7sU4tfrNPS3sVnIzMbM=; b=FLEC5RaWOHutorI7vKo50RN0B/EXaC6utISWpTbfCl2v6L6Zo7kjczBrAbPeFgZUOI aNg58Nn+rHka6Ur3srxS5fbsDcV3vovrIgrp2iMDXZrxVpaMHRfMzHlumMkeMJ48Vl9j 3wqG+0w5QSIJMcVZOAmPOV9EX7drarqgZJSp+2NwC+S3ssbCYGR+Ah16rcumnTsZ7axH iLFg9rMVvqBxz0aSIO67lEHtg8ofpKjqfBE73IKRDLqxyKKpuPFaZG8cegmYpgvgyvRh bMIqoy615+Ea9nAzsvmruFoi0jn4xGnVe+li/1FJZZ1yb8KPx3c7ZvriFxqSFR2Egme2 uFxw== X-Gm-Message-State: AOJu0YyZL6u8sNsBcUbu/si3TM1kA/IyaCLNLkyK+hAau1nyqTPFB9Je 1Qw1PaC05hMQVrmPTU9vp6qX1F2MgZ8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ed4d:b0:1ca:220:ce4f with SMTP id y13-20020a170902ed4d00b001ca0220ce4fmr67727plb.4.1698440113667; Fri, 27 Oct 2023 13:55:13 -0700 (PDT) Date: Fri, 27 Oct 2023 13:55:11 -0700 In-Reply-To: <20231023234000.2499267-7-seanjc@google.com> Mime-Version: 1.0 References: <20231023234000.2499267-1-seanjc@google.com> <20231023234000.2499267-7-seanjc@google.com> Message-ID: Subject: Re: [PATCH 6/6] KVM: x86/pmu: Track emulated counter events instead of previous counter From: Sean Christopherson To: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Mingwei Zhang , Roman Kagan , Jim Mattson , Dapeng Mi , Like Xu Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 27 Oct 2023 13:55:19 -0700 (PDT) On Mon, Oct 23, 2023, Sean Christopherson wrote: > @@ -226,13 +226,19 @@ static int pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, u64 config, > > static void pmc_pause_counter(struct kvm_pmc *pmc) > { > - u64 counter = pmc->counter; > + /* > + * Accumulate emulated events, even if the PMC was already paused, e.g. > + * if KVM emulated an event after a WRMSR, but before reprogramming, or > + * if KVM couldn't create a perf event. > + */ > + u64 counter = pmc->counter + pmc->emulated_counter; > > - if (!pmc->perf_event || pmc->is_paused) > - return; > + pmc->emulated_counter = 0; As pointed by Mingwei, who _very_ patiently explained to me what is broken, the snapshot used to detect overflow due to emulated_counter events needs to be taken _after_ pausing the perf event, i.e. the count from the perf event needs to be excluded. If overflow happens from pmc->counter => pmc->counter + pmc->perf_event, then hardware (via perf) will detect overflow. I.e. KVM is only responsible for detecting overflow solely due to emulated_counter. Include the count from the perf event can lead to KVM generating multiple overflow events, where architecturally only one should occur. > /* update counter, reset event value to avoid redundant accumulation */ > - counter += perf_event_pause(pmc->perf_event, true); > + if (pmc->perf_event && !pmc->is_paused) > + counter += perf_event_pause(pmc->perf_event, true); > + > pmc->counter = counter & pmc_bitmask(pmc); > pmc->is_paused = true; > }