From nobody Tue Feb 10 06:04:58 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 655E6C76196 for ; Fri, 31 Mar 2023 20:30:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233135AbjCaUaA (ORCPT ); Fri, 31 Mar 2023 16:30:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230193AbjCaU3y (ORCPT ); Fri, 31 Mar 2023 16:29:54 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B04151A96F; Fri, 31 Mar 2023 13:29:53 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id z19so22399376plo.2; Fri, 31 Mar 2023 13:29:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294593; x=1682886593; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=PLhh5lXY8mRzGTGozorwCPHUrnN37AS+DlWp1fZ57Ms=; b=PvobSiVqjZzQJUcSIk/NI+SPYWuV2Nz3Hlt7g2jd0iCPqdnUnbU4PzRkNpdH8c+u+Y 1cF6Dx8r6hPGf3zgke+nPrA38Mdn94Fd/UIUv4HagZUVxHlYCm9tpty+nCLy234NXqOB Ig4bFULimXNrEzcgbLkHvZw1u0qZUEjVcFVcL2MOX0aYd8mqv/8gX9+8Fv1VGS3itjwn c7KuhQuUAo6wVAYBnby5flX3cJwP7Cj9u/oX8s8ifF2r5FSFcnBHTXb+37aRDxA92QUd nOCPk7W1PiEEetdSDpEnQWM1MABcTe3PPeY/jDoonJEWRThpWnvSqHDsfURWjzogKkCG c2SQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294593; x=1682886593; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=PLhh5lXY8mRzGTGozorwCPHUrnN37AS+DlWp1fZ57Ms=; b=RPOFpK+UKSl6sHW0u2BF6jcrc9RSongPPkH/g77e7r6CLKnZe/59dtfktRe/5z1a3C nPOb57BqZtl0D6RW29wZD7zoqi5TuCv0jiN73ppjv1+B4qFdK+VfrAlStB6dAfB3Kwhh P6EeVzmve3aXj3maK21HOwAmow09IdEG1yhcb4dS8P5ey1HME7f3r9NsfmM6cRvojry1 P0+qIa3GNEmH2Ku77di5dWg0YsiXOVaNvbc1xis9IwKdWEHcZP/07uWCN1dfSjoQ0ov5 LbYZEzlr/atA000fJJRdzeQC2+RAQeYBHkRhnodkVQohVuNXnS7BydVsR/67FKKSP76c pcXw== X-Gm-Message-State: AAQBX9dcpHmGN0XMj+egaKuMz7tFugjQtteA83q6hXUcWPw+Z1Mfwldb YU9hKFJrbKo6P/p/TPlkGJc= X-Google-Smtp-Source: AKy350btZovMwuvH45elymxAvNu1RTnI3xj0P4HEgarw/FzpW8DFFHE6reVTElr7E3AeK3aefC6mUA== X-Received: by 2002:a17:903:2290:b0:1a1:a727:a802 with SMTP id b16-20020a170903229000b001a1a727a802mr11880427plh.19.1680294593101; Fri, 31 Mar 2023 13:29:53 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:52 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 1/9] perf list: Use relative path for tracepoint scan Date: Fri, 31 Mar 2023 13:29:41 -0700 Message-Id: <20230331202949.810326-2-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Signed-off-by: Namhyung Kim Acked-by: Ian Rogers --- tools/perf/util/print-events.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index 62e9ea7dcf40..26a7e017c928 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -4,6 +4,7 @@ #include #include #include +#include #include =20 #include @@ -59,12 +60,20 @@ static const struct event_symbol event_symbols_tool[PER= F_TOOL_MAX] =3D { void print_tracepoint_events(const struct print_callbacks *print_cb, void = *print_state) { struct dirent **sys_namelist =3D NULL; + char *events_path =3D get_tracing_file("events"); int sys_items =3D tracing_events__scandir_alphasort(&sys_namelist); + int events_fd =3D open(events_path, O_PATH); + + put_tracing_file(events_path); + if (events_fd < 0) { + printf("Error: failed to open tracing events directory\n"); + return; + } =20 for (int i =3D 0; i < sys_items; i++) { struct dirent *sys_dirent =3D sys_namelist[i]; struct dirent **evt_namelist =3D NULL; - char *dir_path; + int dir_fd; int evt_items; =20 if (sys_dirent->d_type !=3D DT_DIR || @@ -72,22 +81,26 @@ void print_tracepoint_events(const struct print_callbac= ks *print_cb, void *print !strcmp(sys_dirent->d_name, "..")) continue; =20 - dir_path =3D get_events_file(sys_dirent->d_name); - if (!dir_path) + dir_fd =3D openat(events_fd, sys_dirent->d_name, O_PATH); + if (dir_fd < 0) continue; =20 - evt_items =3D scandir(dir_path, &evt_namelist, NULL, alphasort); + evt_items =3D scandirat(events_fd, sys_dirent->d_name, &evt_namelist, NU= LL, alphasort); for (int j =3D 0; j < evt_items; j++) { struct dirent *evt_dirent =3D evt_namelist[j]; char evt_path[MAXPATHLEN]; + int evt_fd; =20 if (evt_dirent->d_type !=3D DT_DIR || !strcmp(evt_dirent->d_name, ".") || !strcmp(evt_dirent->d_name, "..")) continue; =20 - if (tp_event_has_id(dir_path, evt_dirent) !=3D 0) + snprintf(evt_path, sizeof(evt_path), "%s/id", evt_dirent->d_name); + evt_fd =3D openat(dir_fd, evt_path, O_RDONLY); + if (evt_fd < 0) continue; + close(evt_fd); =20 snprintf(evt_path, MAXPATHLEN, "%s:%s", sys_dirent->d_name, evt_dirent->d_name); @@ -103,10 +116,11 @@ void print_tracepoint_events(const struct print_callb= acks *print_cb, void *print /*long_desc=3D*/NULL, /*encoding_desc=3D*/NULL); } - free(dir_path); + close(dir_fd); free(evt_namelist); } free(sys_namelist); + close(events_fd); } =20 void print_sdt_events(const struct print_callbacks *print_cb, void *print_= state) --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 06:04:58 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 69A8EC76196 for ; Fri, 31 Mar 2023 20:30:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233173AbjCaUaD (ORCPT ); Fri, 31 Mar 2023 16:30:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229967AbjCaU34 (ORCPT ); Fri, 31 Mar 2023 16:29:56 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D282B76E; Fri, 31 Mar 2023 13:29:55 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id j13so21688465pjd.1; Fri, 31 Mar 2023 13:29:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294595; x=1682886595; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=ON3ePqj/wXwvqLAB1yIimizzqbLUeCNd0TWXqbkcGVM=; b=Y0/w0BpUD/RjYQDJHQTZYdcHHj90Xry7pIWujDCnBcNgrI6ewhUvX+Za73JbTxs7vO XmqeUrQNZ6PSpjLbaQYTpQz3gC07EVRf+w2leb80++HMdS+OClW9auiVQ8alG7xRTTi8 tZ2gHwZ/XYcPdkKi2RPCa6RndYCZM6pknN+nu+q7TqtL17FJK/H5X4UIqzH6aj7ZZ53n yFHKsLXvWTYot/+pEncxNU8BkyX+wP802Vbk6wx6R+zco+dCywhomwW3xAURnHqzV2Ic rrIt7hs5P/5ce4SMST4pn7d6DbYGB2vj/9lS7cCTmYWluWtPFBw38qoEM8qRyngsvJoT pZpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294595; x=1682886595; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ON3ePqj/wXwvqLAB1yIimizzqbLUeCNd0TWXqbkcGVM=; b=yCYGkSdUH3z5SDjfKhNYMPUtY2RDNf3SaVC/t+c3AIQwNpkR6s34ei/30TG9p2XOdA K1cewOsXyrSxlMePsVP5Z6RZs1HOXGM6SI98DUGNBg6lpoXxqdyIw/1D6k9ErqqQCtIj 0e5xdzHcmDTbRk3qVQ+l5wVX0vkIvODCQPttTPh/S259KUr/MY0VBuEih+Htm3ATWciX 2W2QvleRpXEVT9mszXy1pzpLt/WzTD9c2Y/wvlpepYHGfOu1TeI4Jb/hzdvo7WDfxKDL 8KAkGzuglhlNdnTUFFK7yKd3gE3419w1TL8w+GnQIqlNR1AJbBxn0Mzl2JcMGQsgCscX Sjag== X-Gm-Message-State: AAQBX9fatTVXMES5DCrDPodmhExKEcuaHr7vzDqj8QVeZg8OPX9xkBZH IwmSDCXUpwXBETtK6A4bIoy8Ct8tn3Y= X-Google-Smtp-Source: AKy350a0MmdY/uaNdjYU5X3rSslGoDtaaLFqO9OBh6CD0QbVnn1ginhw6HqbHiswugjl1Scgr/nfUg== X-Received: by 2002:a17:903:27ce:b0:19c:da7f:a234 with SMTP id km14-20020a17090327ce00b0019cda7fa234mr22844841plb.67.1680294594606; Fri, 31 Mar 2023 13:29:54 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:54 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 2/9] perf tools: Fix a asan issue in parse_events_multi_pmu_add() Date: Fri, 31 Mar 2023 13:29:42 -0700 Message-Id: <20230331202949.810326-3-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the parse_events_multi_pmu_add() it passes the 'config' variable twice to parse_events_term__num() - one for config and another for loc_term. I'm not sure about the second one as it's converted to YYLTYPE variable. Asan reports it like below: In function =E2=80=98parse_events_term__num=E2=80=99, inlined from =E2=80=98parse_events_multi_pmu_add=E2=80=99 at util/par= se-events.c:1602:6: util/parse-events.c:2653:64: error: array subscript =E2=80=98YYLTYPE[0]= =E2=80=99 is partly outside array bounds of =E2=80=98char[8]=E2= =80=99 [-Werror=3Darray-bounds] 2653 | .err_term =3D loc_term ? loc_term->first_column = : 0, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~ util/parse-events.c: In function =E2=80=98parse_events_multi_pmu_add=E2= =80=99: util/parse-events.c:1587:15: note: object =E2=80=98config=E2=80=99 of siz= e 8 1587 | char *config; | ^~~~~~ cc1: all warnings being treated as errors Signed-off-by: Namhyung Kim Acked-by: Ian Rogers --- tools/perf/util/parse-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index cc8e8766ca30..0010e5e0ee68 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -1601,7 +1601,7 @@ int parse_events_multi_pmu_add(struct parse_events_st= ate *parse_state, =20 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER, - config, 1, false, &config, + config, 1, false, NULL, NULL) < 0) { free(config); goto out_err; --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 06:04:58 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 49C49C761A6 for ; Fri, 31 Mar 2023 20:30:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233227AbjCaUaI (ORCPT ); Fri, 31 Mar 2023 16:30:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbjCaU36 (ORCPT ); Fri, 31 Mar 2023 16:29:58 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3DB01A473; Fri, 31 Mar 2023 13:29:56 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id o2so22393318plg.4; Fri, 31 Mar 2023 13:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294596; x=1682886596; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=RYLcbGcSpDsJ6OJqWj4Xn7XRp6fiJcNPcFrLyu2WCoE=; b=oad0BYHUDzSkNBpOlyfUkLXc2FssNytWVUcfWpzbnbu2DAVp2rl7MCpfe82iKxzwOy EeeG4Z5UbpoUrko0DG24q2CidqbuBlw4K2Mx7OpclYJmrtPPpZvqmxHhQgELexYh7A9+ 3kKX7VFC2BAAF6yDNQX8ZaFgs/iZGg9oaCOTLi2hNWnfsEG7zyGjlzLItSQ8DmNbWB9z raaeBa+5P0JtoqnaZwhi+Bp0TWYwUr/caa9erQ5nSNGJU8V39NZMO3OopiEAlI86gjyX pmI9cR+dy5/klfLnGdjMcyqMh/z9AUufvub+AFHO8v6DOOgDm4T1aGSzeJoNPZZ3mDJG uSXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294596; x=1682886596; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=RYLcbGcSpDsJ6OJqWj4Xn7XRp6fiJcNPcFrLyu2WCoE=; b=1EdQs7LiVNS/GbbL3dUjBCPlIIA09/yRee5ismJ6LTW/PSjHPEXRtEW2j9fDmrSxwb hSuzLb5BB1egg9j7QBXI1Gj5g1DEw9IG5uAAAlq0IkakljjD9D07ZgzavJrWwP2KovZX XA4SSXdF/7nE/tgMBgbIZeqxnrcmXzSEMUIsIhZ1PrZSROlRT+LQJFTB0cNcymmA/vyX SQB1IYXHXNHVkLKNZ0xzMZ3Pk8nFXfJqJi82h/U47wJsaJsD1uXoBPWOs1BKAmqGh2TE cE5RgSUUHJ3HPaLpUIa0VwB2lIsAiCLK58bZO9hV/L4lfX+HOuc6PxQW93gvAZpDCb11 yCHg== X-Gm-Message-State: AAQBX9cBNe2YWl1Ib6lx/XNHR7scGiQX5SDLThiDCnIzQH66h8xfiEZ8 3nUaMg8LFLWrfvjMfhHkbFg= X-Google-Smtp-Source: AKy350ZcEji4uaR1p6lgxzaA12AZiN5GHsf6CZ6J375Iq8kmhHeBSVuDkGCSZ2qpAVVcEf58HLQ42A== X-Received: by 2002:a17:902:db03:b0:1a1:b15a:d916 with SMTP id m3-20020a170902db0300b001a1b15ad916mr35911481plx.3.1680294596038; Fri, 31 Mar 2023 13:29:56 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:55 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 3/9] perf pmu: Add perf_pmu__destroy() function Date: Fri, 31 Mar 2023 13:29:43 -0700 Message-Id: <20230331202949.810326-4-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" It seems there's no function to delete the perf pmu struct. Add the perf_pmu__destroy() to do the job. While at it, add some more helper functions to delete pmu aliases and caps. Signed-off-by: Namhyung Kim Acked-by: Ian Rogers --- tools/perf/util/pmu.c | 50 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/pmu.h | 2 ++ 2 files changed, 52 insertions(+) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index e3aae731bd6f..b112606f36ec 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -300,6 +300,16 @@ void perf_pmu_free_alias(struct perf_pmu_alias *newali= as) free(newalias); } =20 +static void perf_pmu__del_aliases(struct perf_pmu *pmu) +{ + struct perf_pmu_alias *alias, *tmp; + + list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) { + list_del(&alias->list); + perf_pmu_free_alias(alias); + } +} + /* Merge an alias, search in alias list. If this name is already * present merge both of them to combine all information. */ @@ -921,6 +931,8 @@ static struct perf_pmu *pmu_lookup(const char *lookup_n= ame) =20 if (is_hybrid) list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus); + else + INIT_LIST_HEAD(&pmu->hybrid_list); =20 pmu->default_config =3D perf_pmu__get_default_config(pmu); =20 @@ -1750,6 +1762,18 @@ static int perf_pmu__new_caps(struct list_head *list= , char *name, char *value) return -ENOMEM; } =20 +static void perf_pmu__del_caps(struct perf_pmu *pmu) +{ + struct perf_pmu_caps *caps, *tmp; + + list_for_each_entry_safe(caps, tmp, &pmu->caps, list) { + list_del(&caps->list); + free(caps->name); + free(caps->value); + free(caps); + } +} + /* * Reading/parsing the given pmu capabilities, which should be located at: * /sys/bus/event_source/devices//caps as sysfs group attributes. @@ -1932,3 +1956,29 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t s= ize, return 0; return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename); } + +static void perf_pmu__delete(struct perf_pmu *pmu) +{ + perf_pmu__del_formats(&pmu->format); + perf_pmu__del_aliases(pmu); + perf_pmu__del_caps(pmu); + + perf_cpu_map__put(pmu->cpus); + + free(pmu->default_config); + free(pmu->name); + free(pmu->alias_name); + free(pmu); +} + +void perf_pmu__destroy(void) +{ + struct perf_pmu *pmu, *tmp; + + list_for_each_entry_safe(pmu, tmp, &pmus, list) { + list_del(&pmu->list); + list_del(&pmu->hybrid_list); + + perf_pmu__delete(pmu); + } +} diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 24cf69ab32cd..72fd5de334c0 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -259,4 +259,6 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t size, const char *pmu_name, const char *filename); FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name); =20 +void perf_pmu__destroy(void); + #endif /* __PMU_H */ --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 06:04:58 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 3F7A3C761A6 for ; Fri, 31 Mar 2023 20:30:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233234AbjCaUaL (ORCPT ); Fri, 31 Mar 2023 16:30:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233169AbjCaU37 (ORCPT ); Fri, 31 Mar 2023 16:29:59 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1497F2221E; Fri, 31 Mar 2023 13:29:58 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id x15so21681033pjk.2; Fri, 31 Mar 2023 13:29:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294597; x=1682886597; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=fbGI83psWi01QhPjR2ttTrwM+cN287gT/1Q3Pzny4tc=; b=Ti80uQAx6OpqUQQn6abxy1r6kIrgrYp5ucXtvk9dt06p/1yVv32tGEh6g9F98fwx3X 4ZU2Pbe//dxggcX700aMTo5Buj7wwNVrYVDcgKgq+YaP5Ww555i6KSIjOOsZMmw0nfji 6WGO4+XKePfeeZxFXzP2MHvqnUHH14iXbQpfVtwoVEjHNGC8R2xpNa8HAtPqQclMk8Nw G8E7nYF68HrNV8hJIV4hXAff9O8MXr2xpgkO8QgIqm/cTpizEOz1J/iAMY8TQjtrLmuw tutwRo5a0SCtbL9JChQFctQ5IvH9dgkmbqLh3mQneOkri/z2K3NRcmhbZluknNz05H3I z1sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294597; x=1682886597; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fbGI83psWi01QhPjR2ttTrwM+cN287gT/1Q3Pzny4tc=; b=LYF6zqTIdeYjLySzn7RNX8CBhQG9VgPNlfh3R+3Wifzr8RkzefWRbzRs6eLrvIXFaP nOKavbjawHefckgyjsEEg+Ox88kCvvztRZvYLFhYmHauYp4Czmhj1T1qLbKcicTdSmGk j/XJVQTcoukNq4t/iuSYYYwprC9/f4r97BqfAFScQmqZYgvXGz/2yH4SLGlscxPGfR1B 7lNTeAcyj/btjUzAjFB7GSAMN49hrPc9za27lziPhdK9OhD6t5oXFIxz2S6beFmc9Ovv x9BnsXUyvzqkCKPdZiJEVcbpxH7odoNM4HMpA+AHQPshm+fQIzGhRxducumAx2CE0qsM ARDQ== X-Gm-Message-State: AAQBX9ejTEVEaaHe5zwck5uYlezk8ErBTTiro9TiiWWXneI2sgJBhDgs Rv9ghip7uC/f0BgEMFuXKt4= X-Google-Smtp-Source: AKy350b/pF7OzIJcVp6yjq63yeIGqqnSlGX4jZ4Gb6ee1SSYU4m0lXTFj6cS16t9Q9nb3lTUr0WS9w== X-Received: by 2002:a17:902:ceca:b0:1a1:956e:5417 with SMTP id d10-20020a170902ceca00b001a1956e5417mr35644440plg.22.1680294597444; Fri, 31 Mar 2023 13:29:57 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:57 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 4/9] perf bench: Add pmu-scan benchmark Date: Fri, 31 Mar 2023 13:29:44 -0700 Message-Id: <20230331202949.810326-5-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The pmu-scan benchmark will repeatedly scan the sysfs to get the available PMU information. $ ./perf bench internals pmu-scan # Running 'internals/pmu-scan' benchmark: Computing performance of sysfs PMU event scan for 100 times Average PMU scanning took: 6850.990 usec (+- 48.445 usec) Signed-off-by: Namhyung Kim Acked-by: Ian Rogers --- tools/perf/bench/Build | 1 + tools/perf/bench/bench.h | 1 + tools/perf/bench/pmu-scan.c | 184 ++++++++++++++++++++++++++++++++++++ tools/perf/builtin-bench.c | 1 + 4 files changed, 187 insertions(+) create mode 100644 tools/perf/bench/pmu-scan.c diff --git a/tools/perf/bench/Build b/tools/perf/bench/Build index 6b6155a8ad09..0f158dc8139b 100644 --- a/tools/perf/bench/Build +++ b/tools/perf/bench/Build @@ -15,6 +15,7 @@ perf-y +=3D find-bit-bench.o perf-y +=3D inject-buildid.o perf-y +=3D evlist-open-close.o perf-y +=3D breakpoint.o +perf-y +=3D pmu-scan.o =20 perf-$(CONFIG_X86_64) +=3D mem-memcpy-x86-64-asm.o perf-$(CONFIG_X86_64) +=3D mem-memset-x86-64-asm.o diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h index edfc25793cca..0d2b65976212 100644 --- a/tools/perf/bench/bench.h +++ b/tools/perf/bench/bench.h @@ -42,6 +42,7 @@ int bench_inject_build_id(int argc, const char **argv); int bench_evlist_open_close(int argc, const char **argv); int bench_breakpoint_thread(int argc, const char **argv); int bench_breakpoint_enable(int argc, const char **argv); +int bench_pmu_scan(int argc, const char **argv); =20 #define BENCH_FORMAT_DEFAULT_STR "default" #define BENCH_FORMAT_DEFAULT 0 diff --git a/tools/perf/bench/pmu-scan.c b/tools/perf/bench/pmu-scan.c new file mode 100644 index 000000000000..f0f007843bb8 --- /dev/null +++ b/tools/perf/bench/pmu-scan.c @@ -0,0 +1,184 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Benchmark scanning sysfs files for PMU information. + * + * Copyright 2023 Google LLC. + */ +#include +#include "bench.h" +#include "util/debug.h" +#include "util/pmu.h" +#include "util/pmus.h" +#include "util/stat.h" +#include +#include +#include +#include + +static unsigned int iterations =3D 100; + +struct pmu_scan_result { + char *name; + int nr_aliases; + int nr_formats; + int nr_caps; +}; + +static const struct option options[] =3D { + OPT_UINTEGER('i', "iterations", &iterations, + "Number of iterations used to compute average"), + OPT_END() +}; + +static const char *const bench_usage[] =3D { + "perf bench internals pmu-scan ", + NULL +}; + +static int nr_pmus; +static struct pmu_scan_result *results; + +static int save_result(void) +{ + struct perf_pmu *pmu; + struct list_head *list; + struct pmu_scan_result *r; + + perf_pmu__scan(NULL); + + perf_pmus__for_each_pmu(pmu) { + r =3D realloc(results, (nr_pmus + 1) * sizeof(*r)); + if (r =3D=3D NULL) + return -ENOMEM; + + results =3D r; + r =3D results + nr_pmus; + + r->name =3D strdup(pmu->name); + r->nr_caps =3D pmu->nr_caps; + + r->nr_aliases =3D 0; + list_for_each(list, &pmu->aliases) + r->nr_aliases++; + + r->nr_formats =3D 0; + list_for_each(list, &pmu->format) + r->nr_formats++; + + pr_debug("pmu[%d] name=3D%s, nr_caps=3D%d, nr_aliases=3D%d, nr_formats= =3D%d\n", + nr_pmus, r->name, r->nr_caps, r->nr_aliases, r->nr_formats); + nr_pmus++; + } + + perf_pmu__destroy(); + return 0; +} + +static int check_result(void) +{ + struct pmu_scan_result *r; + struct perf_pmu *pmu; + struct list_head *list; + int nr; + + for (int i =3D 0; i < nr_pmus; i++) { + r =3D &results[i]; + pmu =3D perf_pmu__find(r->name); + if (pmu =3D=3D NULL) { + pr_err("Cannot find PMU %s\n", r->name); + return -1; + } + + if (pmu->nr_caps !=3D (u32)r->nr_caps) { + pr_err("Unmatched number of event caps in %s: expect %d vs got %d\n", + pmu->name, r->nr_caps, pmu->nr_caps); + return -1; + } + + nr =3D 0; + list_for_each(list, &pmu->aliases) + nr++; + if (nr !=3D r->nr_aliases) { + pr_err("Unmatched number of event aliases in %s: expect %d vs got %d\n", + pmu->name, r->nr_aliases, nr); + return -1; + } + + nr =3D 0; + list_for_each(list, &pmu->format) + nr++; + if (nr !=3D r->nr_formats) { + pr_err("Unmatched number of event formats in %s: expect %d vs got %d\n", + pmu->name, r->nr_formats, nr); + return -1; + } + } + return 0; +} + +static void delete_result(void) +{ + for (int i =3D 0; i < nr_pmus; i++) + free(results[i].name); + free(results); + + results =3D NULL; + nr_pmus =3D 0; +} + +static int run_pmu_scan(void) +{ + struct stats stats; + struct timeval start, end, diff; + double time_average, time_stddev; + u64 runtime_us; + unsigned int i; + int ret; + + init_stats(&stats); + pr_info("Computing performance of sysfs PMU event scan for %u times\n", + iterations); + + if (save_result() < 0) { + pr_err("Failed to initialize PMU scan result\n"); + return -1; + } + + for (i =3D 0; i < iterations; i++) { + gettimeofday(&start, NULL); + perf_pmu__scan(NULL); + gettimeofday(&end, NULL); + + timersub(&end, &start, &diff); + runtime_us =3D diff.tv_sec * USEC_PER_SEC + diff.tv_usec; + update_stats(&stats, runtime_us); + + ret =3D check_result(); + perf_pmu__destroy(); + if (ret < 0) + break; + } + + time_average =3D avg_stats(&stats); + time_stddev =3D stddev_stats(&stats); + pr_info(" Average PMU scanning took: %.3f usec (+- %.3f usec)\n", + time_average, time_stddev); + + delete_result(); + return 0; +} + +int bench_pmu_scan(int argc, const char **argv) +{ + int err =3D 0; + + argc =3D parse_options(argc, argv, options, bench_usage, 0); + if (argc) { + usage_with_options(bench_usage, options); + exit(EXIT_FAILURE); + } + + err =3D run_pmu_scan(); + + return err; +} diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c index d0fcc3cdc185..58f1cfe1eb34 100644 --- a/tools/perf/builtin-bench.c +++ b/tools/perf/builtin-bench.c @@ -92,6 +92,7 @@ static struct bench internals_benchmarks[] =3D { { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse }, { "inject-build-id", "Benchmark build-id injection", bench_inject_build_i= d }, { "evlist-open-close", "Benchmark evlist open and close", bench_evlist_op= en_close }, + { "pmu-scan", "Benchmark sysfs PMU info scanning", bench_pmu_scan }, { NULL, NULL, NULL } }; =20 --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 06:04:58 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 C9B58C761A6 for ; Fri, 31 Mar 2023 20:30:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233163AbjCaUaO (ORCPT ); Fri, 31 Mar 2023 16:30:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233186AbjCaUaC (ORCPT ); Fri, 31 Mar 2023 16:30:02 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EDFF23B41; Fri, 31 Mar 2023 13:29:59 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id q102so21666820pjq.3; Fri, 31 Mar 2023 13:29:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294599; x=1682886599; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=GApJR9zLFQnP8aqhRF4/4bSrT+6uWVcNNs+zyY06OJY=; b=REZtFi+/crDI7AZhkYx5PzqWXMi9F2zeF8dZVXgzkQESNlUa/oXlk6pf5z8+Pkzn1J zVNfP17Y01enw8tF+Y0J+JVo1sHrUlDHnlc/OrGkalVZ0BuSe+MkFcq18InEO5ehk59n MFxIH7pvX4q7AR/dsN6wEA7OtxMWALRMBR6/19SHdUjfWtTp66lTCLa1EN2YhASxknHL LuH9GiSL6hgzAwISkxtQjhN2u3fMv1F0V2232RsGE6chBZJo7+SlHiQw6FLwuGO27ms4 t1iFc6qSnC2fhF3rPbey1piYQhRgLfY3I/QWi2kUFlRRuM5qabCuaZCe1jlDz3z9Qf91 VaZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294599; x=1682886599; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=GApJR9zLFQnP8aqhRF4/4bSrT+6uWVcNNs+zyY06OJY=; b=XJB8ph198avLxiH869U2LHh95QpeG/+CF806zH/G+Nl1gbtjrUaU23T0cLam1OtYvo tCrgBbUW5CkZezXGSPanqWMfCQAq2hjFZyfOMd6Fo4jel7sYRNwgD+45IVUS4k28YnlY 4KDeml4eVPeqdUcomP+tKhDxurUc84alR0aZNEzoftsj/gu9BVwku/USkQJT+w+09xuY Fk2PAAZ0R0yLzUOhgGt5IwhjHw5TyQUa/kgBR4oW8FFCk85P8MYL01Xp+Av8LNrQ1Z12 DT3tjaXF7LZPRFDC4UgcdqkKWgjVW13pHbguFB+6kRK4ds2Ppi2VEpVISaUa3++huz7k tzLA== X-Gm-Message-State: AAQBX9dxyeTsFy/annVP07Ud2BWk6khG5JEiD/7HhTv0F79fqSdDuBsG /dPRBDNIu28PFIyYEOYBbTY= X-Google-Smtp-Source: AKy350aQY9xgpQDqFvbkT5fwT0Iaur6OeYOM3xlx9WKPEoyZ2sGPR0Ok9dySjrtAZkoK4DDb+AY79Q== X-Received: by 2002:a17:902:ccd2:b0:1a2:7d:17f2 with SMTP id z18-20020a170902ccd200b001a2007d17f2mr31934863ple.69.1680294599136; Fri, 31 Mar 2023 13:29:59 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:58 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 5/9] perf pmu: Use relative path for sysfs scan Date: Fri, 31 Mar 2023 13:29:45 -0700 Message-Id: <20230331202949.810326-6-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The PMU information is in the kernel sysfs so it needs to scan the directory to get the whole information like event aliases, formats and so on. During the traversal, it opens a lot of files and directories like below: dir =3D opendir("/sys/bus/event_source/devices"); while (dentry =3D readdir(dir)) { char buf[PATH_MAX]; snprintf(buf, sizeof(buf), "%s/%s", "/sys/bus/event_source/devices", dentry->d_name); fd =3D open(buf, O_RDONLY); ... } But this is not good since it needs to copy the string to build the absolute pathname, and it makes redundant pathname walk (from the /sys) unnecessarily. We can use openat(2) to open the file in the given directory. While it's not a problem ususally, it can be a problem when the kernel has contentions on the sysfs. Add a couple of new helper to return the file descriptor of PMU directory so that it can use it with relative paths. * perf_pmu__event_source_devices_fd() - returns a fd for the PMU root ("/sys/bus/event_source/devices") * perf_pmu__pathname_fd() - returns a fd for "/" under the PMU root Now the above code can be converted something like below: dirfd =3D perf_pmu__event_source_devices_fd(); dir =3D fdopendir(dirfd); while (dentry =3D readdir(dir)) { fd =3D openat(dirfd, dentry->d_name, O_RDONLY); ... } Signed-off-by: Namhyung Kim Acked-by: Ian Rogers --- tools/perf/tests/pmu.c | 9 ++- tools/perf/util/pmu.c | 161 +++++++++++++++++++++++++---------------- tools/perf/util/pmu.h | 4 +- 3 files changed, 111 insertions(+), 63 deletions(-) diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c index 8507bd615e97..3cf25f883df7 100644 --- a/tools/perf/tests/pmu.c +++ b/tools/perf/tests/pmu.c @@ -3,6 +3,7 @@ #include "pmu.h" #include "tests.h" #include +#include #include #include #include @@ -149,10 +150,16 @@ static int test__pmu(struct test_suite *test __maybe_= unused, int subtest __maybe =20 do { struct perf_event_attr attr; + int fd; =20 memset(&attr, 0, sizeof(attr)); =20 - ret =3D perf_pmu__format_parse(format, &formats); + fd =3D open(format, O_DIRECTORY); + if (fd < 0) { + ret =3D fd; + break; + } + ret =3D perf_pmu__format_parse(fd, &formats); if (ret) break; =20 diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index b112606f36ec..9fc6b8b5732b 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -62,38 +62,38 @@ extern FILE *perf_pmu_in; =20 static bool hybrid_scanned; =20 +static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name); + /* * Parse & process all the sysfs attributes located under * the directory specified in 'dir' parameter. */ -int perf_pmu__format_parse(char *dir, struct list_head *head) +int perf_pmu__format_parse(int dirfd, struct list_head *head) { struct dirent *evt_ent; DIR *format_dir; int ret =3D 0; =20 - format_dir =3D opendir(dir); + format_dir =3D fdopendir(dirfd); if (!format_dir) return -EINVAL; =20 while (!ret && (evt_ent =3D readdir(format_dir))) { - char path[PATH_MAX]; char *name =3D evt_ent->d_name; - FILE *file; + int fd; =20 if (!strcmp(name, ".") || !strcmp(name, "..")) continue; =20 - snprintf(path, PATH_MAX, "%s/%s", dir, name); =20 ret =3D -EINVAL; - file =3D fopen(path, "r"); - if (!file) + fd =3D openat(dirfd, name, O_RDONLY); + if (fd < 0) break; =20 - perf_pmu_in =3D file; + perf_pmu_in =3D fdopen(fd, "r"); ret =3D perf_pmu_parse(head, name); - fclose(file); + fclose(perf_pmu_in); } =20 closedir(format_dir); @@ -105,17 +105,16 @@ int perf_pmu__format_parse(char *dir, struct list_hea= d *head) * located at: * /sys/bus/event_source/devices//format as sysfs group attributes. */ -static int pmu_format(const char *name, struct list_head *format) +static int pmu_format(int dirfd, const char *name, struct list_head *forma= t) { - char path[PATH_MAX]; - - if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "format")) - return -1; + int fd; =20 - if (!file_available(path)) + fd =3D perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY); + if (fd < 0) return 0; =20 - if (perf_pmu__format_parse(path, format)) + /* it'll close the fd */ + if (perf_pmu__format_parse(fd, format)) return -1; =20 return 0; @@ -158,7 +157,7 @@ int perf_pmu__convert_scale(const char *scale, char **e= nd, double *sval) return ret; } =20 -static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, = char *name) +static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, = char *name) { struct stat st; ssize_t sret; @@ -166,9 +165,9 @@ static int perf_pmu__parse_scale(struct perf_pmu_alias = *alias, char *dir, char * int fd, ret =3D -1; char path[PATH_MAX]; =20 - scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name); + scnprintf(path, PATH_MAX, "%s.scale", name); =20 - fd =3D open(path, O_RDONLY); + fd =3D openat(dirfd, path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -190,15 +189,15 @@ static int perf_pmu__parse_scale(struct perf_pmu_alia= s *alias, char *dir, char * return ret; } =20 -static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, c= har *name) +static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, c= har *name) { char path[PATH_MAX]; ssize_t sret; int fd; =20 - scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name); + scnprintf(path, PATH_MAX, "%s.unit", name); =20 - fd =3D open(path, O_RDONLY); + fd =3D openat(dirfd, path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -221,14 +220,14 @@ static int perf_pmu__parse_unit(struct perf_pmu_alias= *alias, char *dir, char *n } =20 static int -perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *nam= e) +perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, char *nam= e) { char path[PATH_MAX]; int fd; =20 - scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); + scnprintf(path, PATH_MAX, "%s.per-pkg", name); =20 - fd =3D open(path, O_RDONLY); + fd =3D openat(dirfd, path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -239,14 +238,14 @@ perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias,= char *dir, char *name) } =20 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, - char *dir, char *name) + int dirfd, char *name) { char path[PATH_MAX]; int fd; =20 - scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); + scnprintf(path, PATH_MAX, "%s.snapshot", name); =20 - fd =3D open(path, O_RDONLY); + fd =3D openat(dirfd, path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -332,7 +331,7 @@ static bool perf_pmu_merge_alias(struct perf_pmu_alias = *newalias, return false; } =20 -static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *= name, +static int __perf_pmu__new_alias(struct list_head *list, int dirfd, char *= name, char *desc, char *val, const struct pmu_event *pe) { struct parse_events_term *term; @@ -391,14 +390,14 @@ static int __perf_pmu__new_alias(struct list_head *li= st, char *dir, char *name, } =20 alias->name =3D strdup(name); - if (dir) { + if (dirfd >=3D 0) { /* * load unit name and scale if available */ - perf_pmu__parse_unit(alias, dir, name); - perf_pmu__parse_scale(alias, dir, name); - perf_pmu__parse_per_pkg(alias, dir, name); - perf_pmu__parse_snapshot(alias, dir, name); + perf_pmu__parse_unit(alias, dirfd, name); + perf_pmu__parse_scale(alias, dirfd, name); + perf_pmu__parse_per_pkg(alias, dirfd, name); + perf_pmu__parse_snapshot(alias, dirfd, name); } =20 alias->desc =3D desc ? strdup(desc) : NULL; @@ -419,7 +418,7 @@ static int __perf_pmu__new_alias(struct list_head *list= , char *dir, char *name, return 0; } =20 -static int perf_pmu__new_alias(struct list_head *list, char *dir, char *na= me, FILE *file) +static int perf_pmu__new_alias(struct list_head *list, int dirfd, char *na= me, FILE *file) { char buf[256]; int ret; @@ -433,7 +432,7 @@ static int perf_pmu__new_alias(struct list_head *list, = char *dir, char *name, FI /* Remove trailing newline from sysfs file */ strim(buf); =20 - return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL); + return __perf_pmu__new_alias(list, dirfd, name, NULL, buf, NULL); } =20 static inline bool pmu_alias_info_file(char *name) @@ -457,17 +456,17 @@ static inline bool pmu_alias_info_file(char *name) * Process all the sysfs attributes located under the directory * specified in 'dir' parameter. */ -static int pmu_aliases_parse(char *dir, struct list_head *head) +static int pmu_aliases_parse(int dirfd, struct list_head *head) { struct dirent *evt_ent; DIR *event_dir; + int fd; =20 - event_dir =3D opendir(dir); + event_dir =3D fdopendir(dirfd); if (!event_dir) return -EINVAL; =20 while ((evt_ent =3D readdir(event_dir))) { - char path[PATH_MAX]; char *name =3D evt_ent->d_name; FILE *file; =20 @@ -480,15 +479,14 @@ static int pmu_aliases_parse(char *dir, struct list_h= ead *head) if (pmu_alias_info_file(name)) continue; =20 - scnprintf(path, PATH_MAX, "%s/%s", dir, name); - - file =3D fopen(path, "r"); + fd =3D openat(dirfd, name, O_RDONLY); + file =3D fdopen(fd, "r"); if (!file) { - pr_debug("Cannot open %s\n", path); + pr_debug("Cannot open %s\n", name); continue; } =20 - if (perf_pmu__new_alias(head, dir, name, file) < 0) + if (perf_pmu__new_alias(head, dirfd, name, file) < 0) pr_debug("Cannot set up %s\n", name); fclose(file); } @@ -501,17 +499,16 @@ static int pmu_aliases_parse(char *dir, struct list_h= ead *head) * Reading the pmu event aliases definition, which should be located at: * /sys/bus/event_source/devices//events as sysfs group attributes. */ -static int pmu_aliases(const char *name, struct list_head *head) +static int pmu_aliases(int dirfd, const char *name, struct list_head *head) { - char path[PATH_MAX]; - - if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "events")) - return -1; + int fd; =20 - if (!file_available(path)) + fd =3D perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY); + if (fd < 0) return 0; =20 - if (pmu_aliases_parse(path, head)) + /* it'll close the fd */ + if (pmu_aliases_parse(fd, head)) return -1; =20 return 0; @@ -544,14 +541,15 @@ static int pmu_alias_terms(struct perf_pmu_alias *ali= as, /* Add all pmus in sysfs to pmu list: */ static void pmu_read_sysfs(void) { - char path[PATH_MAX]; + int fd; DIR *dir; struct dirent *dent; =20 - if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path))) + fd =3D perf_pmu__event_source_devices_fd(); + if (fd < 0) return; =20 - dir =3D opendir(path); + dir =3D fdopendir(fd); if (!dir) return; =20 @@ -559,7 +557,7 @@ static void pmu_read_sysfs(void) if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; /* add to static LIST_HEAD(pmus): */ - perf_pmu__find(dent->d_name); + perf_pmu__find2(fd, dent->d_name); } =20 closedir(dir); @@ -763,7 +761,7 @@ static int pmu_add_cpu_aliases_map_callback(const struc= t pmu_event *pe, =20 new_alias: /* need type casts to override 'const' */ - __perf_pmu__new_alias(data->head, NULL, (char *)pe->name, (char *)pe->des= c, + __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc, (char *)pe->event, pe); return 0; } @@ -814,7 +812,7 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu= _event *pe, =20 if (!strcmp(pmu->id, pe->compat) && pmu_uncore_alias_match(pe->pmu, pmu->name)) { - __perf_pmu__new_alias(idata->head, NULL, + __perf_pmu__new_alias(idata->head, -1, (char *)pe->name, (char *)pe->desc, (char *)pe->event, @@ -863,7 +861,7 @@ static int pmu_max_precise(struct perf_pmu *pmu) return max_precise; } =20 -static struct perf_pmu *pmu_lookup(const char *lookup_name) +static struct perf_pmu *pmu_lookup(int dirfd, const char *lookup_name) { struct perf_pmu *pmu; LIST_HEAD(format); @@ -884,13 +882,13 @@ static struct perf_pmu *pmu_lookup(const char *lookup= _name) * type value and format definitions. Load both right * now. */ - if (pmu_format(name, &format)) + if (pmu_format(dirfd, name, &format)) return NULL; =20 /* * Check the aliases first to avoid unnecessary work. */ - if (pmu_aliases(name, &aliases)) + if (pmu_aliases(dirfd, name, &aliases)) return NULL; =20 pmu =3D zalloc(sizeof(*pmu)); @@ -1024,6 +1022,27 @@ bool evsel__is_aux_event(const struct evsel *evsel) } =20 struct perf_pmu *perf_pmu__find(const char *name) +{ + struct perf_pmu *pmu; + int dirfd; + + /* + * Once PMU is loaded it stays in the list, + * so we keep us from multiple reading/parsing + * the pmu format definitions. + */ + pmu =3D pmu_find(name); + if (pmu) + return pmu; + + dirfd =3D perf_pmu__event_source_devices_fd(); + pmu =3D pmu_lookup(dirfd, name); + close(dirfd); + + return pmu; +} + +static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name) { struct perf_pmu *pmu; =20 @@ -1036,7 +1055,7 @@ struct perf_pmu *perf_pmu__find(const char *name) if (pmu) return pmu; =20 - return pmu_lookup(name); + return pmu_lookup(dirfd, name); } =20 static struct perf_pmu_format * @@ -1938,6 +1957,18 @@ int perf_pmu__event_source_devices_scnprintf(char *p= athname, size_t size) return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs); } =20 +int perf_pmu__event_source_devices_fd(void) +{ + char path[PATH_MAX]; + const char *sysfs =3D sysfs__mountpoint(); + + if (!sysfs) + return -1; + + scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs); + return open(path, O_DIRECTORY); +} + /* * Fill 'buf' with the path to a file or folder in 'pmu_name' in * sysfs. For example if pmu_name =3D "cs_etm" and 'filename' =3D "format" @@ -1957,6 +1988,14 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t s= ize, return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename); } =20 +int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *fil= ename, int flags) +{ + char path[PATH_MAX]; + + scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename); + return openat(dirfd, path, flags); +} + static void perf_pmu__delete(struct perf_pmu *pmu) { perf_pmu__del_formats(&pmu->format); diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 72fd5de334c0..751c7016e7b6 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -211,7 +211,7 @@ void perf_pmu_error(struct list_head *list, char *name,= char const *msg); int perf_pmu__new_format(struct list_head *list, char *name, int config, unsigned long *bits); void perf_pmu__set_format(unsigned long *bits, long from, long to); -int perf_pmu__format_parse(char *dir, struct list_head *head); +int perf_pmu__format_parse(int dirfd, struct list_head *head); void perf_pmu__del_formats(struct list_head *formats); =20 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu); @@ -257,6 +257,8 @@ double perf_pmu__cpu_slots_per_cycle(void); int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size); int perf_pmu__pathname_scnprintf(char *buf, size_t size, const char *pmu_name, const char *filename); +int perf_pmu__event_source_devices_fd(void); +int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *fil= ename, int flags); FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name); =20 void perf_pmu__destroy(void); --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 06:04:58 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 E5105C76196 for ; Fri, 31 Mar 2023 20:30:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233248AbjCaUaS (ORCPT ); Fri, 31 Mar 2023 16:30:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233190AbjCaUaD (ORCPT ); Fri, 31 Mar 2023 16:30:03 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A25922916; Fri, 31 Mar 2023 13:30:01 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id u10so22378180plz.7; Fri, 31 Mar 2023 13:30:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294600; x=1682886600; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=Kqxyb/rKt2Y/ABAu4ly+tta42zomTNbs/md/9tPbhmg=; b=ZmKzGxXQhI5XpYfBW7NUYEcSzjPjkr6umwjV2WNVK9ly3t1fbi9ObRFjfCt9HHmXF0 Nr2dtH6jJBaE0Ale5lX7nhM79p17IMTkJMYM5z7Z59A7otgECj409oDF/+c4/20XRg6d IwbhoHm3UzLgH2V85QTxkztR/o6tfP92l/wU2FQpmF0UNRWY0dZ1nZEuy58AxhGHXCxW 3F/xzj0B1iWz6xuUMwLc98NSJHA08BT+29DZwsiPWuXFvcCxtXCmSgiN8rgvpd4vPzKs Wx+IKA0ytjEk0SbeFuEON4DSAhsb68N1azPNLJZll/tDFy+q2d8iG2n4EDnD53+2VSQe Rc1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294600; x=1682886600; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Kqxyb/rKt2Y/ABAu4ly+tta42zomTNbs/md/9tPbhmg=; b=WRHkeAmJJHFVjjtmh4ZkanrX5wzJ4G0gzf/ciXsDOuVzxrb4CFXb+hFhdQnutFp+z0 5eW+falKOr/9ERJFWUXbIMHmCyQuBavpoKjLiG9I1UuOZzg8wJkN/FjzUEPahTF0+X95 0p/uNvp2iqY3BeEBt84IwqqCjBt1oNGK68GaWVF06boqJbKdcababEjOQDuky7c79xrY 6i78kwW1EaC7fvaIyFDG0aGD+QnUGT7E0pLjx9bRkuzFKl+JoducyPQEPZOSwTX/jG4Q hkUEFSfNOjCZk//H+bVxavbT+lODe4yy0u0eDYTXFUVm9nHcmaYr2RYDpWe8cJigpL9E ARwQ== X-Gm-Message-State: AAQBX9f0/CdjhVeL2+Fc0yKq5/bXzOfy7z+Wzq2RvJQuyk21+ZOregOC C06udTf7Ahl8Y93fYBb2phU= X-Google-Smtp-Source: AKy350ZAH8/+0ayD1LxdtmAHRou3pXLYNUfSkYyXNF0mtssUpc18QRNo5qF9e4msjulVr4MD1y5CVw== X-Received: by 2002:a17:902:f315:b0:19e:9849:1767 with SMTP id c21-20020a170902f31500b0019e98491767mr22990632ple.42.1680294600644; Fri, 31 Mar 2023 13:30:00 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:30:00 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 6/9] perf pmu: Use relative path in perf_pmu__caps_parse() Date: Fri, 31 Mar 2023 13:29:46 -0700 Message-Id: <20230331202949.810326-7-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Likewise, it needs to traverse the pmu/caps directory, let's use openat() with the dirfd instead of open() using the absolute path. Signed-off-by: Namhyung Kim Acked-by: Ian Rogers --- tools/perf/util/pmu.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 9fc6b8b5732b..0c1d87f10b23 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1804,6 +1804,7 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) char caps_path[PATH_MAX]; DIR *caps_dir; struct dirent *evt_ent; + int caps_fd; =20 if (pmu->caps_initialized) return pmu->nr_caps; @@ -1822,18 +1823,19 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) if (!caps_dir) return -EINVAL; =20 + caps_fd =3D dirfd(caps_dir); + while ((evt_ent =3D readdir(caps_dir)) !=3D NULL) { - char path[PATH_MAX + NAME_MAX + 1]; char *name =3D evt_ent->d_name; char value[128]; FILE *file; + int fd; =20 if (!strcmp(name, ".") || !strcmp(name, "..")) continue; =20 - snprintf(path, sizeof(path), "%s/%s", caps_path, name); - - file =3D fopen(path, "r"); + fd =3D openat(caps_fd, name, O_RDONLY); + file =3D fdopen(fd, "r"); if (!file) continue; =20 --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 06:04:58 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 2C4C2C77B6C for ; Fri, 31 Mar 2023 20:30:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231545AbjCaUa0 (ORCPT ); Fri, 31 Mar 2023 16:30:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233202AbjCaUaE (ORCPT ); Fri, 31 Mar 2023 16:30:04 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC85922222; Fri, 31 Mar 2023 13:30:02 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id j13so21688815pjd.1; Fri, 31 Mar 2023 13:30:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294602; x=1682886602; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=XJy9ywO65dgXdPExYWd/WvWg/5bb5i5vxOfS+9ONLpE=; b=Dq/ZgPQkU5fblBTtWmiLNvUS3Ic9vsP2pqx4BomQixfxHnz8czK3NIW/AbhsvVtDxG PTzdoZB7mn5rHN567sdRjrqPRtDd73uvKeHNdnXjQMNpQ1uo8fT96PCbmExoZGHwdFI3 Pc8NeVJ+Sh9YuLTgS+KE31VtrcLJ+vrM/U4Xgkh3DS0v6LZptXkFn02JgUyKnQ/WO/Ey DG8u4Bj1hoZIlcd1HTr/grLCBUGE4NSVXcdqpN4gvfLmOj3gQtq+w1xPePRqnmuljpWU bWdYUY9PXrw6q4UXwpzCMWLDQH5Naj2rvlNqZ0D5pBzljp45K4Taxjl4vDB+bbAwXOla hyGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294602; x=1682886602; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=XJy9ywO65dgXdPExYWd/WvWg/5bb5i5vxOfS+9ONLpE=; b=oathkcXPzMzrvWwIb7bEbfIG0QfMla9v6CTSJTeW/ML2r5k4f9JNwURwlpy5tU2K48 HQv5G/482yN8+r4o44cX+qfUwBU6/lt72esAvH8x+yTpn3LP2S3CxOKGYgvcoe9W8B45 CVnIkFT0CtE7LjY51ibjGdW0HUob7qrEHlVY+2JxvYHwZOh8r8QxwPJut7pxhXXtZAZr /U9gciVSIK1PqjCfN8C9ecPqaKMK/wMAhQ0xzd6m9bZz6EeZUmW2HlUNxWRUv5okN1Xu RDwtBPbPrde7JH5HXhbPikBVHGjpkOhyq3kJ0K8a6VUxY6X1B57HXTLLksq85NP2G+nU k09w== X-Gm-Message-State: AAQBX9dzWsmjBIweaxErhysL/TnC156Xdg7D0jBsJcA6M8D+7iu5LzBz 2u4fYmimFXFu3tL5+LdmjuM= X-Google-Smtp-Source: AKy350amgDAmFcEKgHVPM7C5h3M8fg1ZTRuceQkPIEUVEVDhXdyw7pWvPMoBDQ9A912D02kjHAAq1w== X-Received: by 2002:a17:902:c951:b0:1a2:3108:5cc9 with SMTP id i17-20020a170902c95100b001a231085cc9mr28632579pla.40.1680294602145; Fri, 31 Mar 2023 13:30:02 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.30.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:30:01 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 7/9] perf pmu: Use relative path in setup_pmu_alias_list() Date: Fri, 31 Mar 2023 13:29:47 -0700 Message-Id: <20230331202949.810326-8-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Likewise, x86 needs to traverse the PMU list to build alias. Let's use the new helpers to use relative paths. Signed-off-by: Namhyung Kim Acked-by: Ian Rogers --- tools/perf/arch/x86/util/pmu.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tools/perf/arch/x86/util/pmu.c b/tools/perf/arch/x86/util/pmu.c index f73b80dcd8bd..3c0de3370d7e 100644 --- a/tools/perf/arch/x86/util/pmu.c +++ b/tools/perf/arch/x86/util/pmu.c @@ -71,7 +71,7 @@ static struct pmu_alias *pmu_alias__new(char *name, char = *alias) =20 static int setup_pmu_alias_list(void) { - char path[PATH_MAX]; + int fd, dirfd; DIR *dir; struct dirent *dent; struct pmu_alias *pmu_alias; @@ -79,10 +79,11 @@ static int setup_pmu_alias_list(void) FILE *file; int ret =3D -ENOMEM; =20 - if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path))) + dirfd =3D perf_pmu__event_source_devices_fd(); + if (dirfd < 0) return -1; =20 - dir =3D opendir(path); + dir =3D fdopendir(dirfd); if (!dir) return -errno; =20 @@ -91,11 +92,11 @@ static int setup_pmu_alias_list(void) !strcmp(dent->d_name, "..")) continue; =20 - perf_pmu__pathname_scnprintf(path, sizeof(path), dent->d_name, "alias"); - if (!file_available(path)) + fd =3D perf_pmu__pathname_fd(dirfd, dent->d_name, "alias", O_RDONLY); + if (fd < 0) continue; =20 - file =3D fopen(path, "r"); + file =3D fdopen(fd, "r"); if (!file) continue; =20 --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 06:04:58 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 B8834C761A6 for ; Fri, 31 Mar 2023 20:30:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232341AbjCaUa3 (ORCPT ); Fri, 31 Mar 2023 16:30:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233223AbjCaUaG (ORCPT ); Fri, 31 Mar 2023 16:30:06 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2D8F24426; Fri, 31 Mar 2023 13:30:04 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id h12-20020a17090aea8c00b0023d1311fab3so24637056pjz.1; Fri, 31 Mar 2023 13:30:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294604; x=1682886604; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=lEdFQ1e9O/nmgtLY4q6GhreBjz/AYmss+2MTbnjFDcU=; b=aycFzHBdi0Sa6AEyDSKkTns8AJjhvaJozVBp/IYcun2wMMQmMwZ5oZV30btThiZEEl u7CKMhM0f0d+Ny+AChC4r87PKnCuWY0kOc2jFYfkT1k6KYP59BqYk9lJwOm0gnRgNE27 nKTh7PBOKHJ9PBBgJ3Wq7pFlH8oZVNn3N7NocMoe0PCRC+J6xZJCQpGavGDwQ+4AoQwq L9BurlyC52Lv6ZVtH3IcAo61KjsuvugZ6dhyXLkGe96R+gD5RluGFSlCRpwHF8eDMuLD ak16qZI2zV3i5ZXndX1om6aBL2O2qs3ol/HCgISa6g8s62HTkfWPeEfFPcRyKDFNyPOt aYzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294604; x=1682886604; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=lEdFQ1e9O/nmgtLY4q6GhreBjz/AYmss+2MTbnjFDcU=; b=cKWgaty8hRLyQXdKl33J50BAhrEK2hadv92a7MsMhrAcV1k8ppO8A2fbz3YyflfJly ltcd/QI++U6DJn3VBSq/YUfw8EKnX0PQGefBjZTy/CAIAUyzcxhHAkT/juj8++qEeU8q 6SSj54+NcPhKnTIHmDHi1yAC0Twq+zCIpyxyBc0n7rnsyrVDA7dIbrcI+myf58MgdLuK nLpxoIgY9Le/6GR33xNh/AqMSOsNmM9tgTaSXPKs3NWRak/BpEHvNZr2+mdGS/tiYw+1 Ht1MgBVI8Mnx1UlANdeqlYU0uZEOcwbnAB4l0ALRgIfn06CHLY5t+YkE9yfppNWsSyWC PLnA== X-Gm-Message-State: AAQBX9dm0i28nJG76nR95o3hmLCoaC7EYpHppIqQr0QOcC27JRfZDEFD skncOjuUZRF+h+/g4X96oas= X-Google-Smtp-Source: AKy350aRn4Df8msY0H66XmKOonObnVNoorAxA7bGhq2EYBbPFfIS1zNn+vTN8KMLtqsCcHSefjPJ+A== X-Received: by 2002:a17:902:e3c5:b0:19a:98c9:8cea with SMTP id r5-20020a170902e3c500b0019a98c98ceamr21379978ple.39.1680294603600; Fri, 31 Mar 2023 13:30:03 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.30.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:30:03 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 8/9] perf pmu: Add perf_pmu__{open,scan}_file_at() Date: Fri, 31 Mar 2023 13:29:48 -0700 Message-Id: <20230331202949.810326-9-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" These two helpers will also use openat() to reduce the overhead with relative pathnames. Convert other functions in pmu_lookup() to use the new helpers. Signed-off-by: Namhyung Kim Acked-by: Ian Rogers --- tools/perf/util/pmu.c | 57 ++++++++++++++++++++++++++++++++++--------- tools/perf/util/pmu.h | 6 ++++- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 0c1d87f10b23..78a407b42ad1 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -567,7 +567,7 @@ static void pmu_read_sysfs(void) * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/ar= m64) * may have a "cpus" file. */ -static struct perf_cpu_map *pmu_cpumask(const char *name) +static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name) { struct perf_cpu_map *cpus; const char *templates[] =3D { @@ -582,10 +582,11 @@ static struct perf_cpu_map *pmu_cpumask(const char *n= ame) =20 strlcpy(pmu_name, name, sizeof(pmu_name)); for (template =3D templates; *template; template++) { - file =3D perf_pmu__open_file(&pmu, *template); + file =3D perf_pmu__open_file_at(&pmu, dirfd, *template); if (!file) continue; cpus =3D perf_cpu_map__read(file); + fclose(file); if (cpus) return cpus; } @@ -593,15 +594,19 @@ static struct perf_cpu_map *pmu_cpumask(const char *n= ame) return NULL; } =20 -static bool pmu_is_uncore(const char *name) +static bool pmu_is_uncore(int dirfd, const char *name) { - char path[PATH_MAX]; + int fd; =20 if (perf_pmu__hybrid_mounted(name)) return false; =20 - perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpumask"); - return file_available(path); + fd =3D perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH); + if (fd < 0) + return false; + + close(fd); + return true; } =20 static char *pmu_id(const char *name) @@ -853,11 +858,11 @@ pmu_find_alias_name(const char *name __maybe_unused) return NULL; } =20 -static int pmu_max_precise(struct perf_pmu *pmu) +static int pmu_max_precise(int dirfd, struct perf_pmu *pmu) { int max_precise =3D -1; =20 - perf_pmu__scan_file(pmu, "caps/max_precise", "%d", &max_precise); + perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise= ); return max_precise; } =20 @@ -895,14 +900,14 @@ static struct perf_pmu *pmu_lookup(int dirfd, const c= har *lookup_name) if (!pmu) return NULL; =20 - pmu->cpus =3D pmu_cpumask(name); + pmu->cpus =3D pmu_cpumask(dirfd, name); pmu->name =3D strdup(name); =20 if (!pmu->name) goto err; =20 /* Read type, and ensure that type value is successfully assigned (return= 1) */ - if (perf_pmu__scan_file(pmu, "type", "%u", &type) !=3D 1) + if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) !=3D 1) goto err; =20 alias_name =3D pmu_find_alias_name(name); @@ -913,10 +918,10 @@ static struct perf_pmu *pmu_lookup(int dirfd, const c= har *lookup_name) } =20 pmu->type =3D type; - pmu->is_uncore =3D pmu_is_uncore(name); + pmu->is_uncore =3D pmu_is_uncore(dirfd, name); if (pmu->is_uncore) pmu->id =3D pmu_id(name); - pmu->max_precise =3D pmu_max_precise(pmu); + pmu->max_precise =3D pmu_max_precise(dirfd, pmu); pmu_add_cpu_aliases(&aliases, pmu); pmu_add_sys_aliases(&aliases, pmu); =20 @@ -1730,6 +1735,17 @@ FILE *perf_pmu__open_file(struct perf_pmu *pmu, cons= t char *name) return fopen(path, "r"); } =20 +FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *= name) +{ + int fd; + + fd =3D perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY); + if (fd < 0) + return NULL; + + return fdopen(fd, "r"); +} + int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char= *fmt, ...) { @@ -1747,6 +1763,23 @@ int perf_pmu__scan_file(struct perf_pmu *pmu, const = char *name, const char *fmt, return ret; } =20 +int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *na= me, + const char *fmt, ...) +{ + va_list args; + FILE *file; + int ret =3D EOF; + + va_start(args, fmt); + file =3D perf_pmu__open_file_at(pmu, dirfd, name); + if (file) { + ret =3D vfscanf(file, fmt, args); + fclose(file); + } + va_end(args); + return ret; +} + bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name) { char path[PATH_MAX]; diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 751c7016e7b6..32c3a75bca0e 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -220,7 +220,12 @@ bool is_pmu_core(const char *name); void print_pmu_events(const struct print_callbacks *print_cb, void *print_= state); bool pmu_have_event(const char *pname, const char *name); =20 +FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name); +FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *= name); + int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char= *fmt, ...) __scanf(3, 4); +int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *na= me, + const char *fmt, ...) __scanf(4, 5); =20 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name); =20 @@ -259,7 +264,6 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t size, const char *pmu_name, const char *filename); int perf_pmu__event_source_devices_fd(void); int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *fil= ename, int flags); -FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name); =20 void perf_pmu__destroy(void); =20 --=20 2.40.0.348.gf938b09366-goog From nobody Tue Feb 10 06:04:58 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 D1842C77B60 for ; Fri, 31 Mar 2023 20:30:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233341AbjCaUak (ORCPT ); Fri, 31 Mar 2023 16:30:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233257AbjCaUaY (ORCPT ); Fri, 31 Mar 2023 16:30:24 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83D1C22928; Fri, 31 Mar 2023 13:30:05 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id h12-20020a17090aea8c00b0023d1311fab3so24637135pjz.1; Fri, 31 Mar 2023 13:30:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294605; x=1682886605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=VszdLpAL7WqW9IxkGHXAfoIlaDcuh12lplT2Sv91rgM=; b=fJTPcqNjyxIKGGtqdvBiSEHrCMcmx8ZiItq9Z5trLCfiBkGu9Wm1Hbc18e7T+EJeiw LTe15kEhlp9pxxdNNl61QBW2DMakNHw307l9JIOqBuw9kje28RcSQ4i8qmiBF44zwioW OFRjZVuPkMuJBCPXgL1F2mT81a/nPDVnrfrCVPkfkOgyos8IaiBW4w4n3bU6R2WXkxgk 0dQFIDMek5uVuVONmkqeYpuSR3zGi7ziZngQOqB8p22myN0BgFQiekaWATJTKLHy+pqI 1s749Dlp50qjsCs0tVQJag9LNlhmeseuQd05FbxiIEvGMhoQMZ/+AeII2mGe2d3GS4GK 4d6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294605; x=1682886605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=VszdLpAL7WqW9IxkGHXAfoIlaDcuh12lplT2Sv91rgM=; b=aeN0HurK0b5OuiMiBJx7jptIpamnla58kYU6IVuWmhzgWyWD2/qEHKaJhQ8a/oHBo7 SGIwW+B/7KbghJbTpR2JIvHdL6rp07OqR4o9i88V0xKcP9VH0FZifo8I2S4hDm7QOWoM An9qjZg6zmJrw3N0SiMririv8Ju6XciLo4rLiQNAtdljXFdWI1ouri9uYF01gwKdwGnj PNUz6/eB7Q1uqAKS7q5Q+AOba9/9/dY9/Sh+jo7TjgHtCpG1iCqs6PsGZdmwJD5LHrLv MeLlkslFDGMF5DmQmpxe5IwIx0qBHmEs4SAAUXGt8Q518R5m6CG7Dub0GS1sKIM9o9Fm TSPQ== X-Gm-Message-State: AAQBX9fEAdRRlv+lxyk/0oNnkVWvsiwzuZ0/pNs/fPCceO02NAPhRW1v YAWfWCODRev0v/rmVmEyEE8= X-Google-Smtp-Source: AKy350arHlvmF6DIyZbkP9LcdFyyG3+qB4Th0ZjhnVAbe1BAKpnzxP1XVTMU1ToXoBk6tFW6Q5/Oxg== X-Received: by 2002:a17:903:32d0:b0:19c:dbce:dce8 with SMTP id i16-20020a17090332d000b0019cdbcedce8mr31103443plr.15.1680294605042; Fri, 31 Mar 2023 13:30:05 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.30.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:30:04 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 9/9] perf intel-pt: Use perf_pmu__scan_file_at() if possible Date: Fri, 31 Mar 2023 13:29:49 -0700 Message-Id: <20230331202949.810326-10-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Intel-PT calls perf_pmu__scan_file() a lot, let's use relative address when it accesses multiple files at one place. Signed-off-by: Namhyung Kim Acked-by: Adrian Hunter Acked-by: Ian Rogers --- tools/perf/arch/x86/util/intel-pt.c | 52 ++++++++++++++++++----------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/tools/perf/arch/x86/util/intel-pt.c b/tools/perf/arch/x86/util= /intel-pt.c index 1e39a034cee9..2cff11de9d8a 100644 --- a/tools/perf/arch/x86/util/intel-pt.c +++ b/tools/perf/arch/x86/util/intel-pt.c @@ -194,16 +194,19 @@ static u64 intel_pt_default_config(struct perf_pmu *i= ntel_pt_pmu) int pos =3D 0; u64 config; char c; + int dirfd; + + dirfd =3D perf_pmu__event_source_devices_fd(); =20 pos +=3D scnprintf(buf + pos, sizeof(buf) - pos, "tsc"); =20 - if (perf_pmu__scan_file(intel_pt_pmu, "caps/mtc", "%d", - &mtc) !=3D 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/mtc", "%d", + &mtc) !=3D 1) mtc =3D 1; =20 if (mtc) { - if (perf_pmu__scan_file(intel_pt_pmu, "caps/mtc_periods", "%x", - &mtc_periods) !=3D 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/mtc_periods", "%x", + &mtc_periods) !=3D 1) mtc_periods =3D 0; if (mtc_periods) { mtc_period =3D intel_pt_pick_bit(mtc_periods, 3); @@ -212,13 +215,13 @@ static u64 intel_pt_default_config(struct perf_pmu *i= ntel_pt_pmu) } } =20 - if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_cyc", "%d", - &psb_cyc) !=3D 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/psb_cyc", "%d", + &psb_cyc) !=3D 1) psb_cyc =3D 1; =20 if (psb_cyc && mtc_periods) { - if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_periods", "%x", - &psb_periods) !=3D 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/psb_periods", "%x", + &psb_periods) !=3D 1) psb_periods =3D 0; if (psb_periods) { psb_period =3D intel_pt_pick_bit(psb_periods, 3); @@ -227,8 +230,8 @@ static u64 intel_pt_default_config(struct perf_pmu *int= el_pt_pmu) } } =20 - if (perf_pmu__scan_file(intel_pt_pmu, "format/pt", "%c", &c) =3D=3D 1 && - perf_pmu__scan_file(intel_pt_pmu, "format/branch", "%c", &c) =3D=3D 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "format/pt", "%c", &c) = =3D=3D 1 && + perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "format/branch", "%c", &c= ) =3D=3D 1) pos +=3D scnprintf(buf + pos, sizeof(buf) - pos, ",pt,branch"); =20 pr_debug2("%s default config: %s\n", intel_pt_pmu->name, buf); @@ -236,6 +239,7 @@ static u64 intel_pt_default_config(struct perf_pmu *int= el_pt_pmu) intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format, buf, &config); =20 + close(dirfd); return config; } =20 @@ -488,7 +492,7 @@ static void intel_pt_valid_str(char *str, size_t len, u= 64 valid) } } =20 -static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu, +static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu, int dir= fd, const char *caps, const char *name, const char *supported, u64 config) { @@ -498,11 +502,11 @@ static int intel_pt_val_config_term(struct perf_pmu *= intel_pt_pmu, u64 bits; int ok; =20 - if (perf_pmu__scan_file(intel_pt_pmu, caps, "%llx", &valid) !=3D 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, caps, "%llx", &valid) != =3D 1) valid =3D 0; =20 if (supported && - perf_pmu__scan_file(intel_pt_pmu, supported, "%d", &ok) =3D=3D 1 && != ok) + perf_pmu__scan_file_at(intel_pt_pmu, dirfd, supported, "%d", &ok) =3D= =3D 1 && !ok) valid =3D 0; =20 valid |=3D 1; @@ -531,37 +535,45 @@ static int intel_pt_val_config_term(struct perf_pmu *= intel_pt_pmu, static int intel_pt_validate_config(struct perf_pmu *intel_pt_pmu, struct evsel *evsel) { - int err; + int err, dirfd; char c; =20 if (!evsel) return 0; =20 + dirfd =3D perf_pmu__event_source_devices_fd(); + if (dirfd < 0) + return dirfd; + /* * If supported, force pass-through config term (pt=3D1) even if user * sets pt=3D0, which avoids senseless kernel errors. */ - if (perf_pmu__scan_file(intel_pt_pmu, "format/pt", "%c", &c) =3D=3D 1 && + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "format/pt", "%c", &c) = =3D=3D 1 && !(evsel->core.attr.config & 1)) { pr_warning("pt=3D0 doesn't make sense, forcing pt=3D1\n"); evsel->core.attr.config |=3D 1; } =20 - err =3D intel_pt_val_config_term(intel_pt_pmu, "caps/cycle_thresholds", + err =3D intel_pt_val_config_term(intel_pt_pmu, dirfd, "caps/cycle_thresho= lds", "cyc_thresh", "caps/psb_cyc", evsel->core.attr.config); if (err) - return err; + goto out; =20 - err =3D intel_pt_val_config_term(intel_pt_pmu, "caps/mtc_periods", + err =3D intel_pt_val_config_term(intel_pt_pmu, dirfd, "caps/mtc_periods", "mtc_period", "caps/mtc", evsel->core.attr.config); if (err) - return err; + goto out; =20 - return intel_pt_val_config_term(intel_pt_pmu, "caps/psb_periods", + err =3D intel_pt_val_config_term(intel_pt_pmu, dirfd, "caps/psb_periods", "psb_period", "caps/psb_cyc", evsel->core.attr.config); + +out: + close(dirfd); + return err; } =20 static void intel_pt_config_sample_mode(struct perf_pmu *intel_pt_pmu, --=20 2.40.0.348.gf938b09366-goog