Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp5309948rwb; Wed, 9 Aug 2023 01:57:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEgy6o3QBNruFsBE3J5dRpAsfD5yOpeK5h5tDpxlzbJXyru7WpwdhKRG7bzqtEl1V9zDiKx X-Received: by 2002:a9d:7d8f:0:b0:6bc:da49:676c with SMTP id j15-20020a9d7d8f000000b006bcda49676cmr2347104otn.24.1691571454266; Wed, 09 Aug 2023 01:57:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691571454; cv=none; d=google.com; s=arc-20160816; b=Ryj4qlgrEVpcrH6mPCQXEiy4j4eNYNFYxDzll3SE5jcsysEnEUX0KiTycMv6JbIXWQ D29LRUkgpM3hic/A8p6l1z6G9EMjULtgDj4cERHC6ZYG/MvSOXx9oOXJ8qyC71kv8oHD cUrJBo57doKFzPAr/R1H4PdO1+hWOcWdm5VZz8jz6LYMNvlpZLrbHeahFfMCbqMr1t4a tgnPiR6LzV/ivmJQBHWZwR4kx8i+xgUjrTXsrAm16EjKCu5uNziIBR6OCCMcHNIHPxp9 mub1u7j92VH7TkT1HT90qNKFzbOmck88wPtn7N1hyGhBvYq5/0U9WzYf1Y/o46NF1NNr P27g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature:date; bh=yImomCKPQTRiIKKVrT+JgQNcw1+Vd7AbyPQl4wHTuks=; fh=9v1DD3X7g3rPrLd0JGLh2c9xhgtlbNH2tAfZQYcidb8=; b=Ptzxg0ofLQCgCJ8pWXvDRGgtKwbdWrlZ7BO0tuFNFRXfNOT08/6bW2RI9q0JwtcXpE MPiADxi2HW2r2tdTMhnH1usrj1rtoj2VCs9D+U0E3ClmRTdBXzmUXM83UaruUozB8Xqo GXT5SCuJM5zLPgLoGvINhvgry/brN6Fa0Lw/6r9DKTM6NfIW+DsNo/HuY5wWl9j/fYis 50QtJzdFOMSXrFsV7Ea9YmE3TNhviVHmVwY+6XOteowfSWX50TXzB/fkAbewHhGC1wt2 wBgyfvR62puyRrn/oboNrultfMk8OeVrq9+0xBRAmLH7/NWsO/gVzEHXZL1Zhut8sf9r 7PJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=mFI5xz0M; 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=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l2-20020a654c42000000b005644c5717c1si8866167pgr.252.2023.08.09.01.57.21; Wed, 09 Aug 2023 01:57:34 -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=@linux.dev header.s=key1 header.b=mFI5xz0M; 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=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230502AbjHIIZT (ORCPT + 99 others); Wed, 9 Aug 2023 04:25:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229642AbjHIIZS (ORCPT ); Wed, 9 Aug 2023 04:25:18 -0400 Received: from out-103.mta0.migadu.com (out-103.mta0.migadu.com [IPv6:2001:41d0:1004:224b::67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BA171703 for ; Wed, 9 Aug 2023 01:25:16 -0700 (PDT) Date: Wed, 9 Aug 2023 08:25:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1691569514; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=yImomCKPQTRiIKKVrT+JgQNcw1+Vd7AbyPQl4wHTuks=; b=mFI5xz0M8wjVn59bTN51HUK3eQPqdXW7Qw4+KJiv4ABkiYig4iO0L8DrKdUsIG2OIF3CaL WUKXvfrgclxQ+xfgp5UQ6J+Eln/w45qy04+gZDUNVzyoWa6iJTDu3kjANDsDSTCbiPyahz DfurFvLnfWfyp0t9cYhvv8rZrn6VJfU= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Huang Shijie Cc: maz@kernel.org, james.morse@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, pbonzini@redhat.com, peterz@infradead.org, ingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, irogers@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, patches@amperecomputing.com, zwang@amperecomputing.com Subject: Re: [PATCH] perf/core: fix the bug in the event multiplexing Message-ID: References: <20230809013953.7692-1-shijie@os.amperecomputing.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230809013953.7692-1-shijie@os.amperecomputing.com> X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 Hi Huang, On Wed, Aug 09, 2023 at 09:39:53AM +0800, Huang Shijie wrote: > 2.) Root cause. > There is only 7 counters in my arm64 platform: > (one cycle counter) + (6 normal counters) > > In 1.3 above, we will use 10 event counters. > Since we only have 7 counters, the perf core will trigger > event multiplexing in hrtimer: > merge_sched_in() -->perf_mux_hrtimer_restart() --> > perf_rotate_context(). > > In the perf_rotate_context(), it does not restore some PMU registers > as context_switch() does. In context_switch(): > kvm_sched_in() --> kvm_vcpu_pmu_restore_guest() > kvm_sched_out() --> kvm_vcpu_pmu_restore_host() > > So we got wrong result. This is a rather vague description of the problem. AFAICT, the issue here is on VHE systems we wind up getting the EL0 count enable/disable bits backwards when entering the guest, which is corroborated by the data you have below. > +void arch_perf_rotate_pmu_set(void) > +{ > + if (is_guest()) > + kvm_vcpu_pmu_restore_guest(NULL); > + else > + kvm_vcpu_pmu_restore_host(NULL); > +} > + This sort of hook is rather nasty, and I'd strongly prefer a solution that's confined to KVM. I don't think the !is_guest() branch is necessary at all. Regardless of how the pmu context is changed, we need to go through vcpu_put() before getting back out to userspace. We can check for a running vCPU (ick) from kvm_set_pmu_events() and either do the EL0 bit flip there or make a request on the vCPU to call kvm_vcpu_pmu_restore_guest() immediately before reentering the guest. I'm slightly leaning towards the latter, unless anyone has a better idea here. -- Thanks, Oliver