Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp18825949rwd; Wed, 28 Jun 2023 00:53:56 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ749ed68aul7dS7Uv7ApkMrRUxjEdj9fD8XTkTMxKrEAPKA/D8vckFerqJ3hp2e3tfJI5PE X-Received: by 2002:aa7:cd0a:0:b0:51b:df35:c223 with SMTP id b10-20020aa7cd0a000000b0051bdf35c223mr14314006edw.12.1687938836602; Wed, 28 Jun 2023 00:53:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687938836; cv=none; d=google.com; s=arc-20160816; b=WBt123XHgVMvnQDTafwCDmjtZ44ewQXjzEXProA8bH5FqGPZlCkAOC1uDKNmHA6bzN OxsPZUiTQiE2JjH+kTHDUX1pr5N3LUt8dXz0rLO6W7mdHyPSuN2eccPuMfnPGTnaYM6x jtN7Qh7AtFezjdPhXq0dZYFU+fn7sENZ7up8kB2V+tv3E+F6a42lCkp1Uc/5X+zX+dJB dQbLMv4HGInBQPiY5ndqJUoPwpTAJAetjxnOaIiBBwlhGU38RlA5dsgbdBnm2FfEUS0V cHGclLsG3Q5qJxVB2NPOUKpuBJh0KeFbLmSpyhjYTdRG8MIwL2kYLyEH7vPmW/+zwDoa aEPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :to:subject; bh=T3RRZnkUqR3OwQ/FS/vjyYk2GfLkO21cyaSpgghsKGU=; fh=ioIPNWuXkqhCwxHsgXZdi6YP6juZSKhA2fSEccrprR4=; b=tV46NWH1d05fDUqQNHP9aM9dWJMZvsk0maV1OKjmU8wP26l7b4DUlFNExM8gZ/brnj yLGGfDtTJ8LuEPkcf1nWJTT8sypxBEkTn04Klt7sc6YodbXItZ4F3C4TrqjwHU8Aaxt1 prKVovABlT0OoRvB1iEK9LvL7vLsDonerqYnMY1X8Lv83GLfUKYiYTtpsoRW1YOn1+p3 DrrD23t/T8o+gQZrzk6bz4QP1K2z/LeU3k/af1Q9O26ff2O+Xv5WMrV41rD8Yd8mLYhv glD2GVDDnRpYmP577biIq78LbiMp5KmNaBRtj4uBy41uVRcD1FPYtrci314PONYnWvDT Up+g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d22-20020a05640208d600b0051dad61c20bsi1741275edz.477.2023.06.28.00.53.32; Wed, 28 Jun 2023 00:53:56 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230507AbjF1DB4 (ORCPT + 99 others); Tue, 27 Jun 2023 23:01:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229680AbjF1DBx (ORCPT ); Tue, 27 Jun 2023 23:01:53 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EAC8171A; Tue, 27 Jun 2023 20:01:51 -0700 (PDT) Received: from kwepemm600003.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4QrRB06rrrzlWJK; Wed, 28 Jun 2023 10:59:04 +0800 (CST) Received: from [10.67.111.205] (10.67.111.205) by kwepemm600003.china.huawei.com (7.193.23.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Wed, 28 Jun 2023 11:01:48 +0800 Subject: Re: [PATCH v1] perf pmus: Add placeholder core PMU To: Ian Rogers , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Adrian Hunter , Kan Liang , James Clark , Suzuki Poulouse , Rob Herring , Ravi Bangoria , , References: <20230627182834.117565-1-irogers@google.com> From: Yang Jihong Message-ID: <344a8de1-469d-a8f9-100e-9a67e562939b@huawei.com> Date: Wed, 28 Jun 2023 11:01:47 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: <20230627182834.117565-1-irogers@google.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.111.205] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600003.china.huawei.com (7.193.23.202) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,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 Hello, On 2023/6/28 2:28, Ian Rogers wrote: > 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; > Great. This patch solves the problem, the test results are shown in [1]. Tested-by: Yang Jihong [1]: # perf --debug verbose=2 record true ------------------------------------------------------------ perf_event_attr: size 136 { sample_period, sample_freq } 4000 sample_type IP|TID|TIME|PERIOD read_format ID|LOST disabled 1 inherit 1 mmap 1 comm 1 freq 1 enable_on_exec 1 task 1 precise_ip 3 sample_id_all 1 mmap2 1 comm_exec 1 ksymbol 1 bpf_event 1 ------------------------------------------------------------ sys_perf_event_open: pid 703 cpu 0 group_fd -1 flags 0x8 sys_perf_event_open failed, error -2 decreasing precise_ip by one (2) ------------------------------------------------------------ perf_event_attr: size 136 { sample_period, sample_freq } 4000 sample_type IP|TID|TIME|PERIOD read_format ID|LOST disabled 1 inherit 1 mmap 1 comm 1 freq 1 enable_on_exec 1 task 1 precise_ip 2 sample_id_all 1 mmap2 1 comm_exec 1 ksymbol 1 bpf_event 1 ------------------------------------------------------------ sys_perf_event_open: pid 703 cpu 0 group_fd -1 flags 0x8 sys_perf_event_open failed, error -2 decreasing precise_ip by one (1) ------------------------------------------------------------ perf_event_attr: size 136 { sample_period, sample_freq } 4000 sample_type IP|TID|TIME|PERIOD read_format ID|LOST disabled 1 inherit 1 mmap 1 comm 1 freq 1 enable_on_exec 1 task 1 precise_ip 1 sample_id_all 1 mmap2 1 comm_exec 1 ksymbol 1 bpf_event 1 ------------------------------------------------------------ sys_perf_event_open: pid 703 cpu 0 group_fd -1 flags 0x8 sys_perf_event_open failed, error -2 decreasing precise_ip by one (0) ------------------------------------------------------------ perf_event_attr: size 136 { sample_period, sample_freq } 4000 sample_type IP|TID|TIME|PERIOD read_format ID|LOST disabled 1 inherit 1 mmap 1 comm 1 freq 1 enable_on_exec 1 task 1 sample_id_all 1 mmap2 1 comm_exec 1 ksymbol 1 bpf_event 1 ------------------------------------------------------------ sys_perf_event_open: pid 703 cpu 0 group_fd -1 flags 0x8 sys_perf_event_open failed, error -2 Warning: The cycles event is not supported, trying to fall back to cpu-clock-ticks ------------------------------------------------------------ perf_event_attr: type 1 size 136 { sample_period, sample_freq } 4000 sample_type IP|TID|TIME|PERIOD read_format ID|LOST disabled 1 inherit 1 mmap 1 comm 1 freq 1 enable_on_exec 1 task 1 precise_ip 3 sample_id_all 1 mmap2 1 comm_exec 1 ksymbol 1 bpf_event 1 ------------------------------------------------------------ sys_perf_event_open: pid 703 cpu 0 group_fd -1 flags 0x8 = 5 sys_perf_event_open: pid 703 cpu 1 group_fd -1 flags 0x8 = 6 sys_perf_event_open: pid 703 cpu 2 group_fd -1 flags 0x8 = 7 sys_perf_event_open: pid 703 cpu 3 group_fd -1 flags 0x8 = 9 sys_perf_event_open: pid 703 cpu 4 group_fd -1 flags 0x8 = 10 sys_perf_event_open: pid 703 cpu 5 group_fd -1 flags 0x8 = 11 sys_perf_event_open: pid 703 cpu 6 group_fd -1 flags 0x8 = 12 sys_perf_event_open: pid 703 cpu 7 group_fd -1 flags 0x8 = 13 [ perf record: Captured and wrote 0.006 MB perf.data (116 samples) ] Thanks, Yang