Message ID | 20230214050757.9623-9-likexu@tencent.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2769272wrn; Mon, 13 Feb 2023 21:12:40 -0800 (PST) X-Google-Smtp-Source: AK7set8q++sVPUHl4K+RF/om3Yqr1703tEmxUSEvkT+22f871yMpp1WgSl+NvD1N0nM8bhhN1AYc X-Received: by 2002:a17:902:db11:b0:198:ec2c:d4d9 with SMTP id m17-20020a170902db1100b00198ec2cd4d9mr1374121plx.53.1676351560058; Mon, 13 Feb 2023 21:12:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676351560; cv=none; d=google.com; s=arc-20160816; b=q1tTloGaRC7bLJvp93RHNj10TDu1nZ3hgVztpGUHO3xK/z1/VPE4lBwue2En134vvT RKBePoCAHjeo5eJ1NttKZPCAj7UCzQ7p2o6sj0oXlpw0rRLMJu+HGiufnFyK/HuWEqFF jR6BmUEBDs1RZ7jwcMVaiHahU8HKl/q9KqCXJ4BpD7zIQyVn1pxFexx+tTl2jCZ3FA+1 b7Sv48GAa7m+QcfPDDdtTp61PpKpWTfGXNfNjJ6VQqfWSYQ9x0iT1wkhB7naVCGEJkQ4 LZJYx0zEguMyiqXw95zYw6sUWN8HbX/D1dsFuEX7m5PTMyycHVU+YSx1mPmuAssDwLZ/ VRsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=nGZyZD+oFSHPBtvJdi5eUX94B7yH65jttBvLfq2sBHY=; b=dHn7cz78PfklCimtO+g+CvSNk5QYmKQzFufXgXGqCbY8VxKWXTc4/LGLt57RBD5tGk FvIkn98wjhlRqopSSZIKQGk+lY9nffj0iHUI7/B4KvAXTJoQbF5NaKuMzXr3iVReOE0a 3F67XF9oWmb8YJtWH/zW4caMyON6jL4dHTomaK0HCczSwWV1qectk/Dfh8ToZUSYQiyq xUF8DwaBPjyhUXMuuMsZDlpQgd2MtRSGDfvjoRpFeTKJMJEoy61M7ZKK4YXqFHiULOqe lCCYzuAWIIYzJx8JHlGmPz7Mm3M1xf/uzB2KeXbMJZocXNb7U4PP2zQtgX5kaOQ6TE01 Ka5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DGjr5dPh; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t13-20020a170902e84d00b001944983f4e2si12080651plg.491.2023.02.13.21.12.27; Mon, 13 Feb 2023 21:12:40 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=DGjr5dPh; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231556AbjBNFJJ (ORCPT <rfc822;tebrre53rla2o@gmail.com> + 99 others); Tue, 14 Feb 2023 00:09:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231524AbjBNFIq (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 14 Feb 2023 00:08:46 -0500 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A59D1A4AA; Mon, 13 Feb 2023 21:08:30 -0800 (PST) Received: by mail-pf1-x42a.google.com with SMTP id ct17so3849037pfb.12; Mon, 13 Feb 2023 21:08:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nGZyZD+oFSHPBtvJdi5eUX94B7yH65jttBvLfq2sBHY=; b=DGjr5dPheoMmqyZ35GGMNVLFwdr+aYsud7V5ikRqbHU8bOBqg9DMtcaDVpMGvJ6Gm3 RozvUtMZ8gPkmMJeC2oA2UfHyg5TmEz7pngd+s+NwlPttgN9SVsyngu76fNbHh8wVdS1 fZ4sSAXDWz3gHm0lVtLuw942/WLQypJqGkWCkwRHCqA+xWGq56yzGRBgm8OtO3DM0qeP ECxT9wfaLadKIzRvLcqhLdsifZlGYLtjYUZzCqtMl/Wkp5nvaNK8TONXozJzz0iyulbv +xwn/b8lBJEQ9uTmPWfu0lHZjxsaPjcQEJAr3LJ9TQ/8NpZl2oGB/rKTKXSaBQBFDODG 6TRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nGZyZD+oFSHPBtvJdi5eUX94B7yH65jttBvLfq2sBHY=; b=UOpWIjFZGprSh2d1sVQqKH42w5/ukdVmk84p8km1rhkKRjmHLxmxJMnCo7Q8Yn/HW8 B+t50yqKhwo68OzQ++L4N8v4m28/Wm+4grIm50jCDkN2c0jHCD2GC9pR21l3u3J+UXTs tIVReG+IIy41wYl+XfMjzrgkweLHkiCqQZtPqbyjkprwrHpYC2InzmtV7RZ6wrx1A5sb sPofEcQGnAyQ3mloyoLATzxa1WSb3CayIqrnl9nQ5ql/XgNKmVM65YiGtb/iT+n5WbWI HQlIn9zJPbEdCmZQusIrLHXP2JYz0NpbIQCDCDOBqIkc/Mg525PxZjZZ1tfksYaXuSkP k/ag== X-Gm-Message-State: AO0yUKVI9fBDIwKX1NFX5XR8pjyHCgLp2u/QgCnW3umdd1S4/xjmzNxm gZviyYcjD1CwQxQE+TcOK8I= X-Received: by 2002:aa7:96d4:0:b0:5a8:b417:d85e with SMTP id h20-20020aa796d4000000b005a8b417d85emr866791pfq.19.1676351310296; Mon, 13 Feb 2023 21:08:30 -0800 (PST) Received: from localhost.localdomain ([103.7.29.32]) by smtp.gmail.com with ESMTPSA id j22-20020aa79296000000b005a79f5d9f53sm8738919pfa.165.2023.02.13.21.08.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Feb 2023 21:08:30 -0800 (PST) From: Like Xu <like.xu.linux@gmail.com> X-Google-Original-From: Like Xu <likexu@tencent.com> To: Sean Christopherson <seanjc@google.com> Cc: Paolo Bonzini <pbonzini@redhat.com>, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 08/12] KVM: x86/pmu: Disable vPMU if the minimum num of counters isn't met Date: Tue, 14 Feb 2023 13:07:53 +0800 Message-Id: <20230214050757.9623-9-likexu@tencent.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230214050757.9623-1-likexu@tencent.com> References: <20230214050757.9623-1-likexu@tencent.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757782013114769292?= X-GMAIL-MSGID: =?utf-8?q?1757782013114769292?= |
Series |
KVM: x86: Add AMD Guest PerfMonV2 PMU support
|
|
Commit Message
Like Xu
Feb. 14, 2023, 5:07 a.m. UTC
From: Like Xu <likexu@tencent.com> For compatibility with old software, KVM/AMD should never report less than four counters if vPMU is supported. Thus KVM should sanity check the number of counters enumerated by perf and explicitly disable vPMU support if the min isn't met. E.g. if KVM needs 4 counters and perf says there are 3, then something is wrong and enumerating 4 to the guest is only going to cause more troubles. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Like Xu <likexu@tencent.com> --- arch/x86/kvm/pmu.h | 6 ++++++ 1 file changed, 6 insertions(+)
Comments
On Tue, Feb 14, 2023, Like Xu wrote: > From: Like Xu <likexu@tencent.com> > > For compatibility with old software, KVM/AMD should never report less > than four counters if vPMU is supported. Explain _why_. Anchor what "should" be done in hardware specifications and architecture. > Thus KVM should sanity check the number of counters enumerated by perf and > explicitly disable vPMU support if the min isn't met. E.g. if KVM needs 4 > counters and perf says there are 3, then something is wrong and enumerating 4 > to the guest is only going to cause more troubles. Again, state what the patch actually does, not what KVM "should do". E.g. Disable PMU support when running on AMD and perf reports fewer than four general purpose counters. All AMD PMUs must define at least four counters due to AMD's legacy architecture hardcoding the number of counters without providing a way to enumerate the number of counters to software, e.g. from AMD's APM. The legacy architecture defines four performance counters Virtualizing fewer than four counters can lead to guest instability as software expects four counters to be available. > Suggested-by: Sean Christopherson <seanjc@google.com> > Signed-off-by: Like Xu <likexu@tencent.com> > --- > arch/x86/kvm/pmu.h | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h > index d1cc02c8da88..46db5404894e 100644 > --- a/arch/x86/kvm/pmu.h > +++ b/arch/x86/kvm/pmu.h > @@ -170,6 +170,12 @@ static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) > if ((is_intel && !kvm_pmu_cap.version) || !kvm_pmu_cap.num_counters_gp) > enable_pmu = false; > > + /* > + * For AMD, disable vPMU if the minimum number of counters isn't met. > + */ Doesn't need to be a multiple line comment. This comment is also useless. It's quite clear from the code that PMU support is being disabled when there aren't enough counters, what's missing is _why_. > + if (!is_intel && kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS) > + enable_pmu = false; > + > if (!enable_pmu) { > memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap)); > return; > -- > 2.39.1 >
On Thu, Apr 06, 2023, Sean Christopherson wrote: > On Tue, Feb 14, 2023, Like Xu wrote: > > Suggested-by: Sean Christopherson <seanjc@google.com> > > Signed-off-by: Like Xu <likexu@tencent.com> > > --- > > arch/x86/kvm/pmu.h | 6 ++++++ > > 1 file changed, 6 insertions(+) > > > > diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h > > index d1cc02c8da88..46db5404894e 100644 > > --- a/arch/x86/kvm/pmu.h > > +++ b/arch/x86/kvm/pmu.h > > @@ -170,6 +170,12 @@ static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) > > if ((is_intel && !kvm_pmu_cap.version) || !kvm_pmu_cap.num_counters_gp) > > enable_pmu = false; > > > > + /* > > + * For AMD, disable vPMU if the minimum number of counters isn't met. > > + */ > > Doesn't need to be a multiple line comment. This comment is also useless. It's > quite clear from the code that PMU support is being disabled when there aren't > enough counters, what's missing is _why_. > > > + if (!is_intel && kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS) Actually, rather than bleed AMD details into common code, define a const int kvm_pmu_ops, e.g. const int MIN_NR_GP_COUNTERS and then the above !kvm_pmu_cap.num_counters_gp can get replaced with a more generic check. That will also give us a convenient location to document _why_ Intel and AMD have different mins (in particular, AMD's lack of any way to enumerate less than four counters to the guest). E.g. end up with if (enable_pmu) { perf_get_x86_pmu_capability(&kvm_pmu_cap); /* * For Intel, only support guest architectural pmu * on a host with architectural pmu. */ if ((is_intel && !kvm_pmu_cap.version) || (kvm_pmu_cap.num_counters_gp < pmu_ops->MIN_NR_GP_COUNTERS)) enable_pmu = false; } Hmm, and doesn't have to be done in this series, but we could do the same for the min/max PMU versions. > > + enable_pmu = false; > > + > > if (!enable_pmu) { > > memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap)); > > return; > > -- > > 2.39.1 > >
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index d1cc02c8da88..46db5404894e 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -170,6 +170,12 @@ static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) if ((is_intel && !kvm_pmu_cap.version) || !kvm_pmu_cap.num_counters_gp) enable_pmu = false; + /* + * For AMD, disable vPMU if the minimum number of counters isn't met. + */ + if (!is_intel && kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS) + enable_pmu = false; + if (!enable_pmu) { memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap)); return;