From nobody Tue Apr 28 10:24:43 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8B2D8C433FE for ; Wed, 1 Jun 2022 09:43:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351200AbiFAJnD (ORCPT ); Wed, 1 Jun 2022 05:43:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350196AbiFAJnC (ORCPT ); Wed, 1 Jun 2022 05:43:02 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6F1A0522C1 for ; Wed, 1 Jun 2022 02:43:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654076580; 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: content-transfer-encoding:content-transfer-encoding; bh=ivDjIQXWN6AyT8LMwhNKski76n2fMPOrF0108bc7GWM=; b=fGlu2gh6WZ5YpsGwoCj+NtsYmUlID4uUH2vlXZjT2IXO+T9qP/Ky2fs3IVFzyQb0M5C0wX t2+dqlbDymn8y41FRQpu0NH9qlCU8GhZMIpDmXxgeEfnOc9mcqKQfQB7NFjWFVdpLEhDU6 dILoSnwYhB80BDnS/ht1J6bk67fEb50= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-648-Z6BNiidANg-9gYJPRg-rEQ-1; Wed, 01 Jun 2022 05:42:57 -0400 X-MC-Unique: Z6BNiidANg-9gYJPRg-rEQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 38233811E76; Wed, 1 Jun 2022 09:42:57 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id F30A440EC002; Wed, 1 Jun 2022 09:42:56 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: peterz@infradead.org, x86@kernel.org, Like Xu Subject: [PATCH] x86: events: Do not return bogus capabilities if PMU is broken Date: Wed, 1 Jun 2022 05:42:56 -0400 Message-Id: <20220601094256.362047-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Like Xu If the PMU is broken due to firmware issues, check_hw_exists() will return false but perf_get_x86_pmu_capability() will still return data from x86_pmu. Likewise if some of the hotplug callbacks cannot be installed the contents of x86_pmu will not be reverted. Handle the failure in both cases by clearing x86_pmu if init_hw_perf_events= () or reverts to software events only. Signed-off-by: Paolo Bonzini --- arch/x86/events/core.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 99cf67d63cf3..d23f3821daf5 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -2095,14 +2095,15 @@ static int __init init_hw_perf_events(void) } if (err !=3D 0) { pr_cont("no PMU driver, software events only.\n"); - return 0; + err =3D 0; + goto out_bad_pmu; } =20 pmu_check_apic(); =20 /* sanity check that the hardware exists or is emulated */ if (!check_hw_exists(&pmu, x86_pmu.num_counters, x86_pmu.num_counters_fix= ed)) - return 0; + goto out_bad_pmu; =20 pr_cont("%s PMU driver.\n", x86_pmu.name); =20 @@ -2211,6 +2212,8 @@ static int __init init_hw_perf_events(void) cpuhp_remove_state(CPUHP_AP_PERF_X86_STARTING); out: cpuhp_remove_state(CPUHP_PERF_X86_PREPARE); +out_bad_pmu: + memset(&x86_pmu, 0, sizeof(x86_pmu)); return err; } early_initcall(init_hw_perf_events); @@ -2982,6 +2985,11 @@ unsigned long perf_misc_flags(struct pt_regs *regs) =20 void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap) { + if (!x86_pmu.name) { + memset(cap, 0, sizeof(*cap)); + return; + } + cap->version =3D x86_pmu.version; /* * KVM doesn't support the hybrid PMU yet. --=20 2.31.1