Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5635448rwl; Tue, 11 Apr 2023 08:04:09 -0700 (PDT) X-Google-Smtp-Source: AKy350bRNIl87MffAVrlC/tq2UcIutN/QF3017STZrgqeyymmPU4q0EiXnmoJYTlTv9tYX/WFJAB X-Received: by 2002:a17:907:c23:b0:94a:6cc0:aa09 with SMTP id ga35-20020a1709070c2300b0094a6cc0aa09mr10261386ejc.9.1681225448763; Tue, 11 Apr 2023 08:04:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681225448; cv=none; d=google.com; s=arc-20160816; b=E4SQ5UFF2MgrZn/Dwa2kVBPAGZC9QyZ94UuD4A8Y6GdvZ5MKTnF8GdL7KjiUNZqnE3 huhRjTA3+TVYEOh9fTNEtknvl24OH1JJIjK0813I//vIVDCkB5cTlpVNJEETEGW5U5YZ /OHxAPboIS4witG+FyLkRPn96ZbaLk/1McBvBMc3/lKJcsyMmp6uy6KaAA70RCDXomWk 0kDDiwY3L6XomdMB7LsyME4TEYoV3Ba7lJhRQOE5KWOnK31U9CdbB6E2rwhzPi8orOC9 2rgZn9UspXbwniNfmxR4q2HqE+QGrAfh8j5p1PPDJ2al0sOq7QHOZ+62uwmRb8465a71 RFgA== 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=JUzuStF55OhH4eYJH9aoC4gqNsDP4T1tXhgFLxA6Gjo=; b=UKTFajGu4ojSkecJWt82GzHyDpjWr0rs/cAZY+PXh1uFOJmqaIzsxr0jVAdwQkKuIx D5xSwexN0xgSgraROkdVyW814hoQ4BMip95aCF+Sh8KfBW1nZstBR4sMiTcAzLp5Auv2 KZLYUlpY1BqDjaxQu8Ks+KdKQOx/EdDCpq4VIyWRpguCi+wuGOc/Fr1u8incB04rRgMD w7JFpknxYAjc5kiW8tk48IeCLnuJ1aAMh6CLYH0CE3DXlk2VwlYVH2Y/S3CssLpoO2mB 7UyoJMY5P152PwzvUZY4XiIhxCNt8BwxPNt9QqBJkPayRdiNBCgkThgL4kzjHY/m5BP2 Zw/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=nPlZ7EJ4; 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 o11-20020a170906600b00b0092be6bcd27bsi11480403ejj.556.2023.04.11.08.03.43; Tue, 11 Apr 2023 08:04:08 -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=20210112 header.b=nPlZ7EJ4; 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 S229721AbjDKO6y (ORCPT + 99 others); Tue, 11 Apr 2023 10:58:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229852AbjDKO6x (ORCPT ); Tue, 11 Apr 2023 10:58:53 -0400 Received: from mail-oa1-x2a.google.com (mail-oa1-x2a.google.com [IPv6:2001:4860:4864:20::2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BD321BF4 for ; Tue, 11 Apr 2023 07:58:52 -0700 (PDT) Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-1842e278605so10067417fac.6 for ; Tue, 11 Apr 2023 07:58:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1681225131; x=1683817131; 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=JUzuStF55OhH4eYJH9aoC4gqNsDP4T1tXhgFLxA6Gjo=; b=nPlZ7EJ4LlvNMM7msupsPQS6oXYYW6nRWe9Y9uR8NI2kQxz7q6XIRvI/j+7eVOgGyK AWPjUjGgqH7W+HLqNVWo6n//cgFGnycoAFwwD12SGiY9jvAB6Eo/DbZ3kkVHPz4BTRxv 8qnlW/suArakriYS8Y/RTEaP30ThehXxph/k016mx2YbaEO+o31xy4iWVAirb3+Nc+oN qpCO84/x9tQjDjDt0An/iROzPMy66W6TWgBsN8fK0XNNAFgPjBs4YhRSqdDxvRjAOw6Y CAPtW4WQXymaYyC8+WQSHSZ76yLdQxxD3ZMYO3gauaINwjcCfyv+nJ+2xfPmmVz9tCez yJDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681225131; x=1683817131; 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=JUzuStF55OhH4eYJH9aoC4gqNsDP4T1tXhgFLxA6Gjo=; b=YHPRsrLd9EDic2tkb9aRckVyS9injHm1P1s/7EiOL1Zx+HqbAbdlP0Rx9QOWsnpjyO Eb07NVDcr7K8eIIdDgDfWNnt/Tq96tE1b7ibGxwNdB67rtMOmLDCYs13jw3s0RpBKkk/ mD5TqLsztRQYcQAe/ICYZoxNu1rX0CHN87P0c1h8R6LVjZ5hRQC34W77T5YISNLdMfHl iEgcJ8EwAboEwslhcMD+oMwJDKEVxPz9TbRjebhM51CaO3gunquBFDPkM6Lt+QfLqgJc +CbOVRdyknLEYjqlOXu+YyacwA+13JfmRDzo9nGlI1WWcP0SGO7fGEHWkeqWHVJEJjAg OxpQ== X-Gm-Message-State: AAQBX9fgdfWfdJ0Jd9qVW3FTK/Z2x/jJrIltEX7nmac1ONfBM8In8z1t aDnP582KJ2jXhXS3MQoBMiIaFcANXviA4T+qhvJOtA== X-Received: by 2002:a05:6870:7012:b0:177:c2fb:8cec with SMTP id u18-20020a056870701200b00177c2fb8cecmr1469994oae.9.1681225131251; Tue, 11 Apr 2023 07:58:51 -0700 (PDT) MIME-Version: 1.0 References: <20230410105056.60973-1-likexu@tencent.com> <20230410105056.60973-6-likexu@tencent.com> <9a7d5814-9eb1-d7af-7968-a6e3ebb90248@gmail.com> <81bbb700-9346-3d0d-ab86-6e684b185772@gmail.com> In-Reply-To: <81bbb700-9346-3d0d-ab86-6e684b185772@gmail.com> From: Jim Mattson Date: Tue, 11 Apr 2023 07:58:40 -0700 Message-ID: Subject: Re: [PATCH V5 05/10] KVM: x86/pmu: Disable vPMU if the minimum num of counters isn't met To: Like Xu Cc: Sean Christopherson , Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-15.7 required=5.0 tests=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,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 Tue, Apr 11, 2023 at 6:18=E2=80=AFAM Like Xu w= rote: > > On 11/4/2023 8:58 pm, Jim Mattson wrote: > > On Mon, Apr 10, 2023 at 11:17=E2=80=AFPM Like Xu wrote: > >> > >> On 11/4/2023 1:36 pm, Jim Mattson wrote: > >>> On Mon, Apr 10, 2023 at 3:51=E2=80=AFAM Like Xu wrote: > >>>> > >>>> From: Like Xu > >>>> > >>>> Disable PMU support when running on AMD and perf reports fewer than = four > >>>> general purpose counters. All AMD PMUs must define at least four cou= nters > >>>> due to AMD's legacy architecture hardcoding the number of counters > >>>> without providing a way to enumerate the number of counters to softw= are, > >>>> e.g. from AMD's APM: > >>>> > >>>> The legacy architecture defines four performance counters (PerfCt= rn) > >>>> and corresponding event-select registers (PerfEvtSeln). > >>>> > >>>> Virtualizing fewer than four counters can lead to guest instability = as > >>>> software expects four counters to be available. > >>> > >>> I'm confused. Isn't zero less than four? > >> > >> As I understand it, you are saying that virtualization of zero counter= is also > >> reasonable. > >> If so, the above statement could be refined as: > >> > >> Virtualizing fewer than four counters when vPMU is enabled ma= y lead to guest > >> instability > >> as software expects at least four counters to be available, t= hus the vPMU is > >> disabled if the > >> minimum number of KVM supported counters is not reached durin= g initialization. > >> > >> Jim, does this help you or could you explain more about your confusion= ? > > > > You say that "fewer than four counters can lead to guest instability > > as software expects four counters to be available." Your solution is > > to disable the PMU, which leaves zero counters available. Zero is less > > than four. Hence, by your claim, disabling the PMU can lead to guest > > instability. I don't see how this is an improvement over one, two, or > > three counters. > > As you know, AMD pmu lacks an architected method (such as CPUID) to > indicate that the VM does not have any pmu counters available for the > current platform. Guests like Linux tend to check if their first counters > exist and work properly to infer that other pmu counters exist. "Guests like Linux," or just Linux? What do you mean by "tend"? When do they perform this check, and when do they not? > If KVM chooses to emulate greater than 1 less than 4 counters, then the > AMD guest PMU agent may assume that there are legacy 4 counters all > present (it's what the APM specifies), which requires the legacy code > to add #GP error handling for counters that should exist but actually not= . I would argue that regardless of the number of counters emulated, a guest PMU agent may assume that the 4 legacy counters are present, since that's what the APM specifies. > So at Sean's suggestion, we took a conservative approach. If KVM detects > less than 4 counters, we think KVM (under the current configuration and > platform) is not capable of emulating the most basic AMD pmu capability. > A large number of legacy instances are ready for 0 or 4+ ctrs, not 2 or 3 Which specific guest operating systems is this change intended for? > Does this help you ? I wouldn't mind a better move. Which AMD platforms have less than 4 counters available? > > > > >>> > >>>> Suggested-by: Sean Christopherson > >>>> Signed-off-by: Like Xu > >>>> --- > >>>> arch/x86/kvm/pmu.h | 3 +++ > >>>> 1 file changed, 3 insertions(+) > >>>> > >>>> diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h > >>>> index dd7c7d4ffe3b..002b527360f4 100644 > >>>> --- a/arch/x86/kvm/pmu.h > >>>> +++ b/arch/x86/kvm/pmu.h > >>>> @@ -182,6 +182,9 @@ static inline void kvm_init_pmu_capability(const= struct kvm_pmu_ops *pmu_ops) > >>>> enable_pmu =3D false; > >>>> } > >>>> > >>>> + if (!is_intel && kvm_pmu_cap.num_counters_gp < AMD64_NUM_COU= NTERS) Does this actually guarantee that the requisite number of counters are available and will always be available while the guest is running? What happens if some other client of the host perf subsystem requests a CPU-pinned counter after this checck? > >>>> + enable_pmu =3D false; > >>>> + > >>>> if (!enable_pmu) { > >>>> memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap)); > >>>> return; > >>>> -- > >>>> 2.40.0 > >>>>