Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp18128292rwd; Tue, 27 Jun 2023 12:04:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5DBcLg2ErWvqrkybkq/fAgr7PMbqmwH0XJnuCghrvYhpy0viyHexGZdGxBtXWP5gNCBvK7 X-Received: by 2002:a17:907:6e0f:b0:991:e7c2:d0be with SMTP id sd15-20020a1709076e0f00b00991e7c2d0bemr4985204ejc.63.1687892665751; Tue, 27 Jun 2023 12:04:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687892665; cv=none; d=google.com; s=arc-20160816; b=HsBl5l5LhgN5adSmweXMOgvjiBjBCED4KqSmo5N47RqP74NCOSLvrMaWCEuAjRJaqI SYGohMIwkD7dj++gzL1ZmiWRrI/Hw9R8ztQHgUkFcUbr0T7DnosNR6C0E1AQAHNm88gz /c0EoDey24Jo5IU/mvIkqPrtZL/s3IvybqYfk2xL849SjkgZlNuuJn/PsqClW69cVgAs 5sy4MGUrFUbcvgQxmosd4dIC0cZTR+Q/F7SZNSkwbxq0yFvl3U/00GgjmsWZmI0+z+Il AUXfkoIPHAPCTfaq/DXyEdV12SLtM8UyUwH9Us0XRlj7peJnIsHN3H/nVsyfvVwp1YWV B+Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:mime-version:message-id:date :dkim-signature; bh=pCEk4rjcfYTL2DuAl4m74mxfXYI4yN7w7niW6JdDvXw=; fh=oqf07t2ZgIlvPFgZjHR1e+arboO10GsDl4Eyt123s0k=; b=yYAk1wix3ZdxBG6OFClLi35BuqSogKR3+jHWXnH87yqDOSvvMR8yYhCtU6LiQHzvyQ YR14GUWlomJnGuVrdVwW/aMMRzSMZNE0Tb055oOBN93ZsGsN9kivOiX+l5I+G6qj4GCe ECvzcQfav8srwZTN1+J/VjTWEGroDhrQZrGCn1J4pv9lBCmglIYNede0dCREwJu0FaVB JCCCoZ0t5ZjN3o4CKbxpPIvhvoXv8Ujq8PLhCx5etUlTuTRVRbTRiiScNJihNDVw/Iuk ZWhbHhcmzKIFYsktt8TuM/zDAZ9T4E9LyY/T9JVO/s7dj3UcE2le2NKR5TZZYqXQtxsj /BfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Cm3bw124; 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 t22-20020a170906949600b0098e42bef733si3236487ejx.1003.2023.06.27.12.04.00; Tue, 27 Jun 2023 12:04:25 -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=20221208 header.b=Cm3bw124; 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 S230494AbjF0S26 (ORCPT + 99 others); Tue, 27 Jun 2023 14:28:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229623AbjF0S25 (ORCPT ); Tue, 27 Jun 2023 14:28:57 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51B14E71 for ; Tue, 27 Jun 2023 11:28:55 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5704991ea05so65706677b3.1 for ; Tue, 27 Jun 2023 11:28:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687890534; x=1690482534; h=to:from:subject:mime-version:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=pCEk4rjcfYTL2DuAl4m74mxfXYI4yN7w7niW6JdDvXw=; b=Cm3bw124FnPJLd/hOVzZkDDEl6NNM4e2Hl/lg4rqzZFObSGD+uZnXFozeYjh0LOYLL kpEDPktwyWqw0Pq7ozb4zkXc9QwiUAW+kmkiYBWTROZNSUezXf+9Rsv9cqOvedHA/I76 ryK1te8HJfEiHohIYLEA69RFYB+bmV+37MNrsT4ebstip6UaYunO7AdrjpaYjflhlpL6 xS8yCJmHsstL8q/t/CDMkfPTjMJAoTgn+5QT+1HYSIFM8haYxKCw/kaL8B2g9p/CpxVE YTZo30pAo3ee7f6jXZWZIPJ6ae6c/EE7RzoncuCGrfwEJ/Jf8K3Ok1ReXADB6GJnX0AF AsKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687890534; x=1690482534; h=to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=pCEk4rjcfYTL2DuAl4m74mxfXYI4yN7w7niW6JdDvXw=; b=EQlU+I10KXD0RBGF1oe8XFe5R9X5QPdCKb0sFz17UkUW8bzeQ7Ep3R9xq6ftiYb3w6 RjwILoTxGrGMfsf+1kN8E0L4L3NR9QSskRWZGpGE4BRIcmg3yyrbi6Zju8jQI+UgbrqJ UdLtxTt40l5Xzblxqb/ajLElw4O1x5/FhmWllHK9CGhcEse2jeQQJ8Sr+dHvfXqWmbdC zxhyyDOizrJF7TW3LGgv2de6+RyuoYJSL6GT025W0jLAbKcWNdw24vnzW5FmGkWEelbF ZFJ2TdEBSSMNldBOaiYpcWSfc8z4RlbEgBLrMV9CvOuNd6pEjAN3mB2aprrLwR2J7PMA s3tg== X-Gm-Message-State: AC+VfDxGwOKZasDs7xkrbdblr0UdMC9qj/kRYd+yjzwlB6dJ93TX0/Wq WhfWhllIIP1adr8fDbpt/eqTu6eYKfu0 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:a518:9a69:cf62:b4d9]) (user=irogers job=sendgmr) by 2002:a81:ac08:0:b0:56d:1b6:5360 with SMTP id k8-20020a81ac08000000b0056d01b65360mr13365076ywh.5.1687890534617; Tue, 27 Jun 2023 11:28:54 -0700 (PDT) Date: Tue, 27 Jun 2023 11:28:34 -0700 Message-Id: <20230627182834.117565-1-irogers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v1] perf pmus: Add placeholder core PMU From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Suzuki Poulouse , Rob Herring , Ravi Bangoria , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Yang Jihong Content-Type: text/plain; charset="UTF-8" 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_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,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 If loading a core PMU fails, legacy hardware/cache events may segv due to there being no PMU. Create a placeholder empty PMU for this case. This was discussed in: https://lore.kernel.org/lkml/20230614151625.2077-1-yangjihong1@huawei.com/ Reported-by: Yang Jihong Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 25 +++++++++++++++++++++++++ tools/perf/util/pmu.h | 1 + tools/perf/util/pmus.c | 7 ++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 8d5ecd4ff1a9..7f984a7f16ca 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -928,6 +928,31 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char return NULL; } +/* Creates the PMU when sysfs scanning fails. */ +struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus) +{ + struct perf_pmu *pmu = zalloc(sizeof(*pmu)); + + if (!pmu) + return NULL; + + pmu->name = strdup("cpu"); + if (!pmu->name) { + free(pmu); + return NULL; + } + + pmu->is_core = true; + pmu->type = PERF_TYPE_RAW; + pmu->cpus = cpu_map__online(); + + INIT_LIST_HEAD(&pmu->format); + INIT_LIST_HEAD(&pmu->aliases); + INIT_LIST_HEAD(&pmu->caps); + list_add_tail(&pmu->list, core_pmus); + return pmu; +} + void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu) { struct perf_pmu_format *format; diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 8807a624e918..203b92860e3c 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -286,6 +286,7 @@ int perf_pmu__event_source_devices_fd(void); int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags); struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name); +struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus); void perf_pmu__delete(struct perf_pmu *pmu); #endif /* __PMU_H */ diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index 0866dee3fc62..3cd9de42139e 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -153,7 +153,12 @@ static void pmu_read_sysfs(bool core_only) closedir(dir); if (core_only) { - read_sysfs_core_pmus = true; + if (!list_empty(&core_pmus)) + read_sysfs_core_pmus = true; + else { + if (perf_pmu__create_placeholder_core_pmu(&core_pmus)) + read_sysfs_core_pmus = true; + } } else { read_sysfs_core_pmus = true; read_sysfs_all_pmus = true; -- 2.41.0.162.gfafddb0af9-goog