Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp274271rwd; Wed, 14 Jun 2023 15:59:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ432HYn6euShaKlsHb2u7AhayWjIzHqE+E+nHU+z6GbVQeCmnUKoTy94nW3fk/Wn/5qxSSY X-Received: by 2002:a17:90b:894:b0:256:d945:23c with SMTP id bj20-20020a17090b089400b00256d945023cmr2375024pjb.14.1686783551010; Wed, 14 Jun 2023 15:59:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686783550; cv=none; d=google.com; s=arc-20160816; b=lv3B6nS/IGMUNXazbZWWkHRsd1uq+1HO4gFAKLiF+HMye85qYoaEH1LqJcAArThZlz eJdW7hJcKMJKZKFcadB0qUFXTFUiyAljVKA/7CeHc0aedCpShubewKI7K6Kq/aF/vFhK PPK4Q6jZ2Bk4UK29/R/cZrf+U1ZHEagasSSsdRJHR3dRkhXkMV3FLIsACJKTVim/l5YO CxQJzfNQdzcnI0Df8g2/px8FnFkEIdrg5EFqv9IPGTCy6NDFZsuAt1hd0jIwi8SsD2jl EbEqRg7OwNFwon48RCCrw/szEopseZJMp0X+0u96C1fYaIg0eQ6bQ7lUj9ZGWObrhXDs b2Ig== 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=7hBNzsqDA+ibibh6Quql+9MMSvftWOYFNSxUTSCrW9A=; b=wC7cKxBqLUULtxDGm5bcsuqqvDv9IiQQn4KQSIr6ljQclzV1NVJRAkx4XfNZjRTwZv GkT1MEiCA1tSIqG7Htc37ecBGJ6/JVDk7Pa1lFmGWLIX7UdedMHy63Oj5exMgLp1Sf8I 97c06beCmVctVAC2C0g6aKHeJIBgnQA1cJFI+si1C1pU23I9gTkUzLC9xkFzR9ouWbst FML6PZBZgsXeH+Eo2d/SXD5zSStwQQbOUe5SoJm6WSTEcpAmNyhcjZ37xYgGVybTv0jI 7hKRwq0/xh8AnjFLEmCYazC91FSTDmqjOuc4WEFuxVXSaPbte/oxf3uEbCDPL63ZMq3o Nk7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=waQ12cIx; 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 k60-20020a17090a14c200b0025e23a82aa3si1908955pja.40.2023.06.14.15.58.58; Wed, 14 Jun 2023 15:59:10 -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=waQ12cIx; 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 S229837AbjFNWD7 (ORCPT + 99 others); Wed, 14 Jun 2023 18:03:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234331AbjFNWD6 (ORCPT ); Wed, 14 Jun 2023 18:03:58 -0400 Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B61C2695 for ; Wed, 14 Jun 2023 15:03:57 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id d75a77b69052e-3f9b7de94e7so79901cf.0 for ; Wed, 14 Jun 2023 15:03:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686780236; x=1689372236; 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=7hBNzsqDA+ibibh6Quql+9MMSvftWOYFNSxUTSCrW9A=; b=waQ12cIx29ruzEBjWasuZPAkcbUNyESfpjzBm/sofM4Ba5fd80/qEqEH4Vtn2MnlC/ McZM0JA2FfDbO9D9AXqxHMs3myciAe8MfnZXprubiA76wy41vYvDD0L/Kc0b9sOVoKXc 2VKpI5gWnv7/meQpohmXNBqKvSUF44HRQ2y16Bm1+aShU6ECmhMNDVb1S2ox0WJAUaQE EjVOmvj4B4i2OUvgw9nsc4j1YuwKCpWQaeCYYSW84FtPT66HP1QOGtVvvasaPyKdq5lA dpk7wHCKKjPEeVbhGwL/uUw05HrzlVMF2FYZ7kN61ib1JlXLlREgoD0qQnEpD+k4lxVw z0gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686780236; x=1689372236; 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=7hBNzsqDA+ibibh6Quql+9MMSvftWOYFNSxUTSCrW9A=; b=gBjjYRmKIfEfzW+GkkRU5t9yaKVC4zvlVg8yOzDwlUWB95e+7qcXaQVzWME5giIxUt LUJwH5bI/oRxAypRHvpbRbb/4ROIN3MG0XJPC33jnToT4gPEIT8ZPP/NPTD63YKWY+y8 Bm4z90ApydBN55N9mFTNRxHflNqsyNVWd9zFiUV0Dw5c52WZjSl7qTYlG07OSZQMuus8 6wzGwxkly3+80Jx3RtQ6p356INF1iEtRLVxUQUeDxkBl+V61LVPTJSRFnV47KZ4iKGOr UnRrIAzkGyZulYdzn77ReK+F+9DJq7EtxdjedfE1tQkGLTXMBRDAGoyXx5llIo5QkMdW xuRA== X-Gm-Message-State: AC+VfDwNnUsIgocTinHLGvei4ziNmW4FrRNyHWJRH1OB70gNEJ97E0Qi rw639aV4FTyBbmfinD1I6E3ZDBHoHqd6dQHxPJkYEA== X-Received: by 2002:a05:622a:1108:b0:3f8:5b2:aef5 with SMTP id e8-20020a05622a110800b003f805b2aef5mr12457qty.29.1686780235563; Wed, 14 Jun 2023 15:03:55 -0700 (PDT) MIME-Version: 1.0 References: <20230614151625.2077-1-yangjihong1@huawei.com> In-Reply-To: From: Ian Rogers Date: Wed, 14 Jun 2023 15:03:43 -0700 Message-ID: Subject: Re: [PATCH] perf top & record: Fix segfault when default cycles event is not supported To: Yang Jihong Cc: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, adrian.hunter@intel.com, kan.liang@linux.intel.com, linux-perf-users@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=-17.6 required=5.0 tests=BAYES_00,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, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_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 On Wed, Jun 14, 2023 at 9:18=E2=80=AFAM Ian Rogers wro= te: > > On Wed, Jun 14, 2023 at 8:18=E2=80=AFAM Yang Jihong wrote: > > > > The perf-record and perf-top call parse_event() to add a cycles event t= o > > an empty evlist. For the system that does not support hardware cycles > > event, such as QEMU, the evlist is empty due to the following code proc= ess: > > > > parse_event(evlist, "cycles:P" or ""cycles:Pu") > > parse_events(evlist, "cycles:P") > > __parse_events > > ... > > ret =3D parse_events__scanner(str, &parse_state); > > // ret =3D 0 > > ... > > ret2 =3D parse_events__sort_events_and_fix_groups() > > if (ret2 < 0) > > return ret; > > // The cycles event is not supported, here ret2 =3D -EINVAL, > > // Here return 0. > > ... > > evlist__splice_list_tail(evlist) > > // The code here does not execute to, so the evlist is still em= pty. > > > > A null pointer occurs when the content in the evlist is accessed later. > > > > Before: > > > > # perf list hw > > > > List of pre-defined events (to be used in -e or -M): > > > > # perf record true > > libperf: Miscounted nr_mmaps 0 vs 1 > > WARNING: No sample_id_all support, falling back to unordered processi= ng > > perf: Segmentation fault > > Obtained 1 stack frames. > > [0xc5beff] > > Segmentation fault > > > > Solution: > > If cycles event is not supported, try to fall back to cpu-clock event= . > > > > After: > > # perf record true > > [ perf record: Woken up 1 times to write data ] > > [ perf record: Captured and wrote 0.006 MB perf.data ] > > # > > > > Fixes: 7b100989b4f6 ("perf evlist: Remove __evlist__add_default") > > Signed-off-by: Yang Jihong > > Thanks, useful addition. The cpu-clock fall back wasn't present before > 7b100989b4f6 so is the fixes tag correct? Hmm... it should be coming from evsel__fallback: https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/p= erf/util/evsel.c?h=3Dtmp.perf-tools-next#n2840 so we shouldn't duplicate that logic. The question is why we're not doing the fallback. Thanks, Ian > Wrt segv, I'm beginning to think that we should always forcibly create > a core PMU even if we can't find one one in sysfs, my guess is that is > what triggers the segv. > > evlist__add_default doesn't really explain what the function is doing > and default can have >1 meaning. Perhaps, evlist__add_cycles. > > Thanks, > Ian > > > --- > > tools/perf/builtin-record.c | 4 +--- > > tools/perf/builtin-top.c | 3 +-- > > tools/perf/util/evlist.c | 18 ++++++++++++++++++ > > tools/perf/util/evlist.h | 1 + > > 4 files changed, 21 insertions(+), 5 deletions(-) > > > > diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c > > index aec18db7ff23..29ae2b84a63a 100644 > > --- a/tools/perf/builtin-record.c > > +++ b/tools/perf/builtin-record.c > > @@ -4161,9 +4161,7 @@ int cmd_record(int argc, const char **argv) > > record.opts.tail_synthesize =3D true; > > > > if (rec->evlist->core.nr_entries =3D=3D 0) { > > - bool can_profile_kernel =3D perf_event_paranoid_check(1= ); > > - > > - err =3D parse_event(rec->evlist, can_profile_kernel ? "= cycles:P" : "cycles:Pu"); > > + err =3D evlist__add_default(rec->evlist); > > if (err) > > goto out; > > } > > diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c > > index c363c04e16df..798cb9252a5f 100644 > > --- a/tools/perf/builtin-top.c > > +++ b/tools/perf/builtin-top.c > > @@ -1665,8 +1665,7 @@ int cmd_top(int argc, const char **argv) > > goto out_delete_evlist; > > > > if (!top.evlist->core.nr_entries) { > > - bool can_profile_kernel =3D perf_event_paranoid_check(1= ); > > - int err =3D parse_event(top.evlist, can_profile_kernel = ? "cycles:P" : "cycles:Pu"); > > + int err =3D evlist__add_default(top.evlist); > > > > if (err) > > goto out_delete_evlist; > > diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c > > index 7ef43f72098e..60efa762405e 100644 > > --- a/tools/perf/util/evlist.c > > +++ b/tools/perf/util/evlist.c > > @@ -287,6 +287,24 @@ struct evsel *evlist__add_aux_dummy(struct evlist = *evlist, bool system_wide) > > return evsel; > > } > > > > +int evlist__add_default(struct evlist *evlist) > > +{ > > + bool can_profile_kernel; > > + int err; > > + > > + can_profile_kernel =3D perf_event_paranoid_check(1); > > + err =3D parse_event(evlist, can_profile_kernel ? "cycles:P" : "= cycles:Pu"); > > + if (err) > > + return err; > > + > > + if (!evlist->core.nr_entries) { > > + pr_debug("The cycles event is not supported, trying to = fall back to cpu-clock event\n"); > > + return parse_event(evlist, "cpu-clock"); > > + } > > + > > + return 0; > > +} > > + > > #ifdef HAVE_LIBTRACEEVENT > > struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool sys= tem_wide) > > { > > diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h > > index 664c6bf7b3e0..47eea809ee91 100644 > > --- a/tools/perf/util/evlist.h > > +++ b/tools/perf/util/evlist.h > > @@ -116,6 +116,7 @@ int arch_evlist__cmp(const struct evsel *lhs, const= struct evsel *rhs); > > > > int evlist__add_dummy(struct evlist *evlist); > > struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system= _wide); > > +int evlist__add_default(struct evlist *evlist); > > static inline struct evsel *evlist__add_dummy_on_all_cpus(struct evlis= t *evlist) > > { > > return evlist__add_aux_dummy(evlist, true); > > -- > > 2.30.GIT > >