Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3554414rdb; Wed, 13 Sep 2023 16:11:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFnYmjKdfSJFyCCy2tyUrSRZ3m+2zhwcI2CkaR1K63IXbe5IR+N5zdGrZzty58uG2hvxFc9 X-Received: by 2002:a05:6a20:42a3:b0:13e:9dba:ea52 with SMTP id o35-20020a056a2042a300b0013e9dbaea52mr4393048pzj.13.1694646670104; Wed, 13 Sep 2023 16:11:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694646670; cv=none; d=google.com; s=arc-20160816; b=rEhCKyV7kVtq93XIp16YTYgcDjNKPSFpk2UCqGXSHYmReHiI/Hl5e50iy7M06DvfDZ JguD4qmreJ3E/Ell0k/EM/V26xg0IIkIO53kzhuuNWJEr0IfgYNRG2nrnFjfVXI4Bn0I FRSBFhR24OWBGElSq6GJHhtvJ+wu9+/i4omrL3MbzHydZEq05Xvz/n517QW1g4vBB9e8 z3VF5fptXCvnJraiXX7hHP2f+Mge30IfInN3AFvABEDU2WRhl/ypWPeTMfyvFGDa2sjp x8/iPKpuKApglp3vXma5vYbWbrmIhWkj4oVRrGZ3oSPAboUiVIij/J80KGeoinrTqG5S 9P/A== 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=fWFFHFQX+xYywYbbSeTK3OfCUdymYd0gdqigI2aRV9M=; fh=HWoC6vMYXRCxJeLV1dlY/G0LIdME61RzrCznF6MHsEo=; b=DfqiZ50XWcxqVn9C1XX0/+BGXKX54saN00rqe6b3VwfX4uNfjF+RwSaYO7llnelv3F PpZHQS78AVPle8sfPfwR8go0TJKHtyBC1r2fMF9HiNAJM5A84T+oYJu+mBUE7YNNcvaE K/LxHZhmAi2CeUrywB/jozDnqLMgDehXffdnA0VXrQ0yLM4FWeCE9XbRNibqzavAqppx cO4s4T36gh+so3UaEpVPX/piCs4Negio+nl7dYA6YVkeMCkSZIkQPv3Hy6M/XtcwX9pX tLpGOPsv59Ng2Uqbd5iKih31kpm0wZGjlzASZzQbUY8P0kewFy9TOZZWT6D3VgK3RunI TYPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=olZSaljD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id z32-20020a056a001da000b0068fbd15c04dsi257224pfw.3.2023.09.13.16.11.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 16:11:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=olZSaljD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id DECB38157DCF; Wed, 13 Sep 2023 09:34:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229472AbjIMQec (ORCPT + 99 others); Wed, 13 Sep 2023 12:34:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229872AbjIMQeb (ORCPT ); Wed, 13 Sep 2023 12:34:31 -0400 Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2F5D19A7 for ; Wed, 13 Sep 2023 09:34:26 -0700 (PDT) Received: by mail-il1-x12c.google.com with SMTP id e9e14a558f8ab-34df0f0a5beso1115ab.0 for ; Wed, 13 Sep 2023 09:34:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694622866; x=1695227666; darn=vger.kernel.org; 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=fWFFHFQX+xYywYbbSeTK3OfCUdymYd0gdqigI2aRV9M=; b=olZSaljD7gc3EvQKp6lTos7gCt9aMJEWG2JHdlkz/SEWg9Ai3zVCs6xXP9RvLVbgKe xAuYOWZBhMB40MGL2/C40FUF5UGEXvhHQ4Ha8fomYli2YWdimrItgsbkrUVL/sbnP4zv vbirh+CRf7oa0jAqAXRYLXthSCqzctj0KZ8bmsoftxyYaKy4dYNklfBl9fbGFUYajLW/ 9Wv0++X2xp79w8zyNnQ2PU3mmmnvr+ydBUKMDcRVzr44MiN89Sf9rTT1pdaKyQ/HD5vq 9tCi0d5CcAL/tcTV4P6B5h6ij+S9sOR6wC35hkLQg0LYW1xb+NIJ59CsZEKaaAOv1oKa yPyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694622866; x=1695227666; 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=fWFFHFQX+xYywYbbSeTK3OfCUdymYd0gdqigI2aRV9M=; b=p37hDaanvprhyzyHFgVqOVZ+ndWvZdVnA2BdmEcOAXCiTLsX2QfF7nDStnQ1IKbQvo FQsICQtQkMdqqfBP2U+0x6ZIzGV3dnMpZIs9MMI5FzazrCjOWlfOwBPcbjha1rrLPq7z mdu1OyXno+WpjdtfoxX4ZbQbYQRIagfe4i20Vc9iJyVq1Fxb6JaAGmJoc565ZvWmR/eu HExvOP5TFNOlde5vTPxDsQzx/hrRseKOMcWobVaEBdpnJb2gVc/LpQ0zGOxAhW6+jz9t JLwHJgVxqEtideSn88TjRrnY50rc0kTJJEq9oVCMgDdxUGNl6BUAyxgoORu74p1FYxJX QTXQ== X-Gm-Message-State: AOJu0YyaeAH5oQ5nOc/fUIvotbrJKfUOxsBPUOzJttklxZecCXF23rkZ znNdUh0vF8uv26vs8DWkGA0OSsME7eYRt9E+tjU5zA== X-Received: by 2002:a92:c263:0:b0:34d:ff4c:5e3a with SMTP id h3-20020a92c263000000b0034dff4c5e3amr289482ild.10.1694622866155; Wed, 13 Sep 2023 09:34:26 -0700 (PDT) MIME-Version: 1.0 References: <20230913153355.138331-1-james.clark@arm.com> <20230913153355.138331-2-james.clark@arm.com> In-Reply-To: <20230913153355.138331-2-james.clark@arm.com> From: Ian Rogers Date: Wed, 13 Sep 2023 09:34:13 -0700 Message-ID: Subject: Re: [PATCH v3 1/3] perf pmu: Move pmu__find_core_pmu() to pmus.c To: James Clark Cc: linux-perf-users@vger.kernel.org, acme@kernel.org, John Garry , Will Deacon , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Adrian Hunter , Kan Liang , Haixin Yu , Jing Zhang , Eduard Zingerman , Ravi Bangoria , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 13 Sep 2023 09:34:41 -0700 (PDT) X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email On Wed, Sep 13, 2023 at 8:34=E2=80=AFAM James Clark w= rote: > > pmu__find_core_pmu() more logically belongs in pmus.c because it > iterates over all PMUs, so move it to pmus.c > > At the same time rename it to perf_pmus__find_core_pmu() to match the > naming convention in this file. > > list_prepare_entry() can't be used in perf_pmus__scan_core() anymore now > that it's called from the same compilation unit. This is with -O2 > (specifically -O1 -ftree-vrp -finline-functions > -finline-small-functions) which allow the bounds of the array > access to be determined at compile time. list_prepare_entry() subtracts > the offset of the 'list' member in struct perf_pmu from &core_pmus, > which isn't a struct perf_pmu. The compiler sees that pmu results in > &core_pmus - 8 and refuses to compile. At runtime this works because > list_for_each_entry_continue() always adds the offset back again before > dereferencing ->next, but it's technically undefined behavior. With > -fsanitize=3Dundefined an additional warning is generated. > > Using list_first_entry_or_null() to get the first entry here avoids > doing &core_pmus - 8 but has the same result and fixes both the compile > warning and the undefined behavior warning. There are other uses of > list_prepare_entry() in pmus.c, but the compiler doesn't seem to be > able to see that they can also be called with &core_pmus, so I won't > change any at this time. > > Signed-off-by: James Clark Reviewed-by: Ian Rogers Thanks, Ian > --- > tools/perf/arch/arm64/util/pmu.c | 6 +++--- > tools/perf/tests/expr.c | 2 +- > tools/perf/util/expr.c | 2 +- > tools/perf/util/pmu.c | 17 ----------------- > tools/perf/util/pmu.h | 2 +- > tools/perf/util/pmus.c | 20 +++++++++++++++++++- > 6 files changed, 25 insertions(+), 24 deletions(-) > > diff --git a/tools/perf/arch/arm64/util/pmu.c b/tools/perf/arch/arm64/uti= l/pmu.c > index 615084eb88d8..3d9330feebd2 100644 > --- a/tools/perf/arch/arm64/util/pmu.c > +++ b/tools/perf/arch/arm64/util/pmu.c > @@ -10,7 +10,7 @@ > > const struct pmu_metrics_table *pmu_metrics_table__find(void) > { > - struct perf_pmu *pmu =3D pmu__find_core_pmu(); > + struct perf_pmu *pmu =3D perf_pmus__find_core_pmu(); > > if (pmu) > return perf_pmu__find_metrics_table(pmu); > @@ -20,7 +20,7 @@ const struct pmu_metrics_table *pmu_metrics_table__find= (void) > > const struct pmu_events_table *pmu_events_table__find(void) > { > - struct perf_pmu *pmu =3D pmu__find_core_pmu(); > + struct perf_pmu *pmu =3D perf_pmus__find_core_pmu(); > > if (pmu) > return perf_pmu__find_events_table(pmu); > @@ -32,7 +32,7 @@ double perf_pmu__cpu_slots_per_cycle(void) > { > char path[PATH_MAX]; > unsigned long long slots =3D 0; > - struct perf_pmu *pmu =3D pmu__find_core_pmu(); > + struct perf_pmu *pmu =3D perf_pmus__find_core_pmu(); > > if (pmu) { > perf_pmu__pathname_scnprintf(path, sizeof(path), > diff --git a/tools/perf/tests/expr.c b/tools/perf/tests/expr.c > index 78544092ef0c..e3aa9d4fcf3a 100644 > --- a/tools/perf/tests/expr.c > +++ b/tools/perf/tests/expr.c > @@ -76,7 +76,7 @@ static int test__expr(struct test_suite *t __maybe_unus= ed, int subtest __maybe_u > struct expr_parse_ctx *ctx; > bool is_intel =3D false; > char strcmp_cpuid_buf[256]; > - struct perf_pmu *pmu =3D pmu__find_core_pmu(); > + struct perf_pmu *pmu =3D perf_pmus__find_core_pmu(); > char *cpuid =3D perf_pmu__getcpuid(pmu); > char *escaped_cpuid1, *escaped_cpuid2; > > diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c > index 4488f306de78..7be23b3ac082 100644 > --- a/tools/perf/util/expr.c > +++ b/tools/perf/util/expr.c > @@ -509,7 +509,7 @@ double expr__strcmp_cpuid_str(const struct expr_parse= _ctx *ctx __maybe_unused, > bool compute_ids __maybe_unused, const char *test_= id) > { > double ret; > - struct perf_pmu *pmu =3D pmu__find_core_pmu(); > + struct perf_pmu *pmu =3D perf_pmus__find_core_pmu(); > char *cpuid =3D perf_pmu__getcpuid(pmu); > > if (!cpuid) > diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c > index e2159854ab26..f50a5636633f 100644 > --- a/tools/perf/util/pmu.c > +++ b/tools/perf/util/pmu.c > @@ -2050,20 +2050,3 @@ void perf_pmu__delete(struct perf_pmu *pmu) > zfree(&pmu->id); > free(pmu); > } > - > -struct perf_pmu *pmu__find_core_pmu(void) > -{ > - struct perf_pmu *pmu =3D NULL; > - > - while ((pmu =3D perf_pmus__scan_core(pmu))) { > - /* > - * The cpumap should cover all CPUs. Otherwise, some CPUs= may > - * not support some events or have different event IDs. > - */ > - if (RC_CHK_ACCESS(pmu->cpus)->nr !=3D cpu__max_cpu().cpu) > - return NULL; > - > - return pmu; > - } > - return NULL; > -} > diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h > index bd5d804a6736..d7b46085642d 100644 > --- a/tools/perf/util/pmu.h > +++ b/tools/perf/util/pmu.h > @@ -264,6 +264,6 @@ int perf_pmu__pathname_fd(int dirfd, const char *pmu_= name, const char *filename, > struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, con= st 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); > -struct perf_pmu *pmu__find_core_pmu(void); > +struct perf_pmu *perf_pmus__find_core_pmu(void); > > #endif /* __PMU_H */ > diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c > index 6631367c756f..cec869cbe163 100644 > --- a/tools/perf/util/pmus.c > +++ b/tools/perf/util/pmus.c > @@ -10,6 +10,7 @@ > #include > #include > #include > +#include "cpumap.h" > #include "debug.h" > #include "evsel.h" > #include "pmus.h" > @@ -268,7 +269,7 @@ struct perf_pmu *perf_pmus__scan_core(struct perf_pmu= *pmu) > { > if (!pmu) { > pmu_read_sysfs(/*core_only=3D*/true); > - pmu =3D list_prepare_entry(pmu, &core_pmus, list); > + return list_first_entry_or_null(&core_pmus, typeof(*pmu),= list); > } > list_for_each_entry_continue(pmu, &core_pmus, list) > return pmu; > @@ -592,3 +593,20 @@ struct perf_pmu *evsel__find_pmu(const struct evsel = *evsel) > } > return pmu; > } > + > +struct perf_pmu *perf_pmus__find_core_pmu(void) > +{ > + struct perf_pmu *pmu =3D NULL; > + > + while ((pmu =3D perf_pmus__scan_core(pmu))) { > + /* > + * The cpumap should cover all CPUs. Otherwise, some CPUs= may > + * not support some events or have different event IDs. > + */ > + if (RC_CHK_ACCESS(pmu->cpus)->nr !=3D cpu__max_cpu().cpu) > + return NULL; > + > + return pmu; > + } > + return NULL; > +} > -- > 2.34.1 >