From nobody Tue Feb 10 18:36:07 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 57CE6C77B61 for ; Mon, 20 Mar 2023 03:39:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229784AbjCTDi5 (ORCPT ); Sun, 19 Mar 2023 23:38:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229731AbjCTDiv (ORCPT ); Sun, 19 Mar 2023 23:38:51 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED36122039 for ; Sun, 19 Mar 2023 20:38:45 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id d7-20020a25adc7000000b00953ffdfbe1aso11925900ybe.23 for ; Sun, 19 Mar 2023 20:38:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283525; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=viZfwPvf0caSxtFGqLDwlsYO52BarmzoWX8M5eoZoBk=; b=OrtovnaInILlCIRf6L7NJjDGfa5k9Acce0mhfYh+s2Wus1VqddW29oleoAHbBhIYMW CLvXLUKZdLagltpnwPsXblMivLI1AhDXqhqEcM2yE+7p7ApY/HG4ppObxPG8d3YwOiv8 dflRvDhpHRs4mqPmCyGWkPo4ZkqxvbKa1DJk6JP2BdaS5UUsqWwJPNWzHj5HDtWlvRe8 xeBVg7JpyIWD9gZA+mLvpjfG2mbGsaCsa/Dw+5Xj2zgu5SidlZpOPFoXoVplKR0O+i8L XwkrQFXf2VtuRN9zTLijz4IxotpxF1jgCM7/+p1HQW4tD/vzxBewwT1c4opH5TBvo3Hv DdxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283525; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=viZfwPvf0caSxtFGqLDwlsYO52BarmzoWX8M5eoZoBk=; b=CT158hlaLGDT3Q6bfeW2ImQ3ciBiPYhqShHzuXBWHzt7lMlxb/OmFeU7GgixREAOWM ux3p1V2gPxoELA1q1eR4hf8Ws/IoajAY3V38aSyF28amfR+Zs8Pn7MLprPbmObYDraDo 55m+gE2RGYymJg/zPGJhbr7Dm9g+Z0HDqhn6yYc4QFCzz2d3tVrUZxgOAbiaAIPl35Kb 7vIFFlkYs3PbWXvm5PmKRCE/TV/sLrWMsdVUdYOUJ1fskAFo0e8fuK7gcsU1dSWlGAbt VSwKjOxV6oPGnWB1+yorptkJvt6CJXnogIqFEcB5R0JdIBrgh4+qSjguzmSDrbYje8SC 9Eng== X-Gm-Message-State: AO0yUKUm0A6krHfzNK+rN+TGtE1Uul/auN2EvrAU22St90N98YxnLT/s 7Kgw1AuU13KZXbJKHwyXJiTzTFEk+ogo X-Google-Smtp-Source: AK7set9HA6GCjS7rQXIB5sBheW9QzDYpHx1Wud7NltgUnVFzU4bZ2gsYo7N/ovZdaIdTTmaYEv6lK0iOD5xj X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:1002:b0:a27:3ecc:ffe7 with SMTP id w2-20020a056902100200b00a273eccffe7mr4343205ybt.3.1679283525119; Sun, 19 Mar 2023 20:38:45 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:49 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-2-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 01/22] perf symbol: Avoid memory leak from abi::__cxa_demangle From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rather than allocate memory, allow abi::__cxa_demangle to do that. This avoids a problem where on error NULL was returned triggering a memory leak. Fixes: 3b4e4efe88f6 ("perf symbol: Add abi::__cxa_demangle C++ demangling s= upport") Signed-off-by: Ian Rogers --- tools/perf/util/demangle-cxx.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/demangle-cxx.cpp b/tools/perf/util/demangle-cx= x.cpp index 8708bcafd370..85b706641837 100644 --- a/tools/perf/util/demangle-cxx.cpp +++ b/tools/perf/util/demangle-cxx.cpp @@ -38,11 +38,10 @@ char *cxx_demangle_sym(const char *str, bool params __m= aybe_unused, =20 return cplus_demangle(str, flags); #elif defined(HAVE_CXA_DEMANGLE_SUPPORT) - size_t len =3D strlen(str); - char *output =3D (char*)malloc(len); + char *output; int status; =20 - output =3D abi::__cxa_demangle(str, output, &len, &status); + output =3D abi::__cxa_demangle(str, /*output_buffer=3D*/NULL, /*le= ngth=3D*/NULL, &status); return output; #else return NULL; --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 45BC3C77B6D for ; Mon, 20 Mar 2023 03:39:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229771AbjCTDjE (ORCPT ); Sun, 19 Mar 2023 23:39:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229734AbjCTDiy (ORCPT ); Sun, 19 Mar 2023 23:38:54 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED1EA1ABF7 for ; Sun, 19 Mar 2023 20:38:52 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-544781e30easo108370047b3.1 for ; Sun, 19 Mar 2023 20:38:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283532; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Xrt9Ph5px6prAvTQhXAecI3U6Lfan0vmOke50ovWG1M=; b=SLm8pysaaJPDn3tarDhYFdN0EoWhscBYsdzIjuWTUUtB0j5tEwe9R6xoqXPNrT+h5K qKOSI+OR5Q//CyRQL5Y6rR4Tls8LUt0f2xYILflCtzDTTw6Vur94Fe97Ks5zz4Eawyz1 lvxTNIpHcD/epo6xo2SUaNrFgX5A3JIn8sFS+kHVL9WNcJdiXjHODNVfbnVsGYg9IhBo g4MAGQT5FE1aYb8pbG9MqU7Pxjcm09dDr6JOyE0OASTf7Ogit8u8Bg++uq+JkKagUiXg o0ib7riT4+5/P3/WoHSyQinzMlXTvfshQjEhhHhn3+y5jiWSMlnMjrIj5eIR0pFeTPCG WEEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283532; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Xrt9Ph5px6prAvTQhXAecI3U6Lfan0vmOke50ovWG1M=; b=FQ++Xf2K0JMY0GNGZiUutWK7SD1B3DZllbgXJ1LLpShOElMTFweOSQWgE10uu7bwtc fIU6ylDCrU6Lz4SvmiyaZPvaI4Gls+3pVB/K0HGPKRDRbPzy2K2wDHLvMPeOCdZChuc5 xvtU318Vg4N2oF3gZVT3oRzTRWEhbboAlANhhdAdlD2pyQSTa1b9aen07BQESfCxHbWr fOp8Q9FKmN1B+zcx/wzR5kpem6/9CxpkGfzJyRhRFQM49GfGGWJtW0h7SeNE4Gd6qj97 Cc25/Ig0B71URgRmP7dgvAdLA5a6HN9BjunoZn7zfVzhxUUkoFU1f55Z/eaQ6X/iOsMO nDZw== X-Gm-Message-State: AO0yUKVZy7b5UY6ak7QynMEjRy4WGLWCWCdfxEn8cH4zkJLwPbCYXuhA 9PNZZKunlN7TQnmpzf3sqhlAtnmFUroe X-Google-Smtp-Source: AK7set9NhHU82JjZxbfhBgezTbXM9VYgl0aEqn4whSpWEHhQgRIw//AI/wwz8OmvRhylECrv+kV3c4/TOqdR X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:1002:b0:b4a:e062:3576 with SMTP id w2-20020a056902100200b00b4ae0623576mr3589232ybt.13.1679283531762; Sun, 19 Mar 2023 20:38:51 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:50 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-3-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 02/22] perf bpf_counter: Use public cpumap accessors From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Avoid the use of internal apis via the cpumap accessor functions. Signed-off-by: Ian Rogers --- tools/perf/util/bpf_counter.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c index 1b77436e067e..76ee3e86824a 100644 --- a/tools/perf/util/bpf_counter.c +++ b/tools/perf/util/bpf_counter.c @@ -545,7 +545,7 @@ static int bperf__load(struct evsel *evsel, struct targ= et *target) filter_type =3D=3D BPERF_FILTER_TGID) key =3D perf_thread_map__pid(evsel->core.threads, i); else if (filter_type =3D=3D BPERF_FILTER_CPU) - key =3D evsel->core.cpus->map[i].cpu; + key =3D perf_cpu_map__cpu(evsel->core.cpus, i).cpu; else break; =20 @@ -587,9 +587,9 @@ static int bperf_sync_counters(struct evsel *evsel) { int num_cpu, i, cpu; =20 - num_cpu =3D all_cpu_map->nr; + num_cpu =3D perf_cpu_map__nr(all_cpu_map); for (i =3D 0; i < num_cpu; i++) { - cpu =3D all_cpu_map->map[i].cpu; + cpu =3D perf_cpu_map__cpu(all_cpu_map, i).cpu; bperf_trigger_reading(evsel->bperf_leader_prog_fd, cpu); } return 0; --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 9B206C7618E for ; Mon, 20 Mar 2023 03:39:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229550AbjCTDjZ (ORCPT ); Sun, 19 Mar 2023 23:39:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229779AbjCTDjG (ORCPT ); Sun, 19 Mar 2023 23:39:06 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93EE42203E for ; Sun, 19 Mar 2023 20:39:00 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e129-20020a251e87000000b00b56598237f5so11781649ybe.16 for ; Sun, 19 Mar 2023 20:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283539; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rHDC7UBXOWCAEPbdLzihWokj+UpLxvZZidURX978WmE=; b=DD409jiJybjs9fQX7q7fqtCmw5nuGC5eXy8D6ZSD5O4pmYcN72p7hTEODVgBYZEhih BQ8Zkql4EPpaB/c9OmfzRn1pSmg46M89b8K41d3wbmu47kPetePp5bOjXNfKJ0uMuEVZ RbR5wRXgwvyBsjPdXzI2qrEcdwA1LaGDzHzRu7x6Q9/UMO88nh71PpsoMZsuOtW5tz/m BfsDjqHvt6vwK4UyqTSn7GTf9BTLFViOVkKIfsuWxt75dvaC4f8JP84RfzUVfiA3I1Nu hCJB5Fwv5gfaeAYFx+rQPgC1/9V5eGmTaMDpqoo0mAIcyEDjLbgzy7q1+Ej9yM6pdkT9 6xnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283539; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rHDC7UBXOWCAEPbdLzihWokj+UpLxvZZidURX978WmE=; b=0X9S5r8cmuxxN0XZXSobmxAUJmdMEFDI4jwy0evQ5NJn/yw1hCXgnjk/6GwBlV1Flf I3IxrD0bWHz00SOrWaGuKv91WgpgzfiqD6TPHOBavOQgjTPkM2LlDCMdbrulhCXP9zwu PB5e1aYuwvO6ZFZ5WBev90PuZpTvAl1YAMP3taEdX1CmHOJsrPDrTW3TY3wC/yYttVnR TP3xijq2a686fWZrPSdcY6FT3CDyizlqqGrjAWFwSkr8i0aaZD3gmJKfvWQHOopyUgSd gImyVUGQn9tanofbJn9fdf4Ad5VYHVfMt9j7nzwm0htC6Z+/HlfyG+XAL0+HT4P1qj8z dSxA== X-Gm-Message-State: AO0yUKV488bsxUlG7eRqPnvpkDAsZ8g/hlqZHiru/HH49GG6g3J9Vs1V SZa5JzMrg0PQu2umANn+4/c/xoExPNET X-Google-Smtp-Source: AK7set84I3Q5yq7Mc89TIl7TiUOtm8jwsNnxNmJCd6t1yy+aYw75XLnXCrbdgrXYoaHB+1L/4SUHw0NpECCk X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:150e:b0:b43:8424:2a4c with SMTP id q14-20020a056902150e00b00b4384242a4cmr4773681ybu.10.1679283539572; Sun, 19 Mar 2023 20:38:59 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:51 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-4-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 03/22] perf tests: Add common error route for code-reading From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A later change will enforce that the map is put on this path regardless of success or error. Signed-off-by: Ian Rogers --- tools/perf/tests/code-reading.c | 39 +++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-readin= g.c index cb8cd09938d5..fb67fd5ebd9f 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -236,18 +236,19 @@ static int read_object_code(u64 addr, size_t len, u8 = cpumode, const char *objdump_name; char decomp_name[KMOD_DECOMP_LEN]; bool decomp =3D false; - int ret; + int ret, err =3D 0; =20 pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr); =20 if (!thread__find_map(thread, cpumode, addr, &al) || !al.map->dso) { if (cpumode =3D=3D PERF_RECORD_MISC_HYPERVISOR) { pr_debug("Hypervisor address can not be resolved - skipping\n"); - return 0; + goto out; } =20 pr_debug("thread__find_map failed\n"); - return -1; + err =3D -1; + goto out; } =20 pr_debug("File is: %s\n", al.map->dso->long_name); @@ -255,7 +256,7 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, if (al.map->dso->symtab_type =3D=3D DSO_BINARY_TYPE__KALLSYMS && !dso__is_kcore(al.map->dso)) { pr_debug("Unexpected kernel address - skipping\n"); - return 0; + goto out; } =20 pr_debug("On file address is: %#"PRIx64"\n", al.addr); @@ -272,15 +273,18 @@ static int read_object_code(u64 addr, size_t len, u8 = cpumode, al.addr, buf1, len); if (ret_len !=3D len) { pr_debug("dso__data_read_offset failed\n"); - return -1; + err =3D -1; + goto out; } =20 /* * Converting addresses for use by objdump requires more information. * map__load() does that. See map__rip_2objdump() for details. */ - if (map__load(al.map)) - return -1; + if (map__load(al.map)) { + err =3D -1; + goto out; + } =20 /* objdump struggles with kcore - try each map only once */ if (dso__is_kcore(al.map->dso)) { @@ -290,12 +294,12 @@ static int read_object_code(u64 addr, size_t len, u8 = cpumode, if (state->done[d] =3D=3D al.map->start) { pr_debug("kcore map tested already"); pr_debug(" - skipping\n"); - return 0; + goto out; } } if (state->done_cnt >=3D ARRAY_SIZE(state->done)) { pr_debug("Too many kcore maps - skipping\n"); - return 0; + goto out; } state->done[state->done_cnt++] =3D al.map->start; } @@ -306,7 +310,8 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, decomp_name, sizeof(decomp_name)) < 0) { pr_debug("decompression failed\n"); - return -1; + err =3D -1; + goto out; } =20 decomp =3D true; @@ -337,15 +342,16 @@ static int read_object_code(u64 addr, size_t len, u8 = cpumode, */ pr_debug("objdump failed for kcore"); pr_debug(" - skipping\n"); - return 0; } else { - return -1; + err =3D -1; } + goto out; } } if (ret < 0) { pr_debug("read_via_objdump failed\n"); - return -1; + err =3D -1; + goto out; } =20 /* The results should be identical */ @@ -355,11 +361,12 @@ static int read_object_code(u64 addr, size_t len, u8 = cpumode, dump_buf(buf1, len); pr_debug("buf2 (objdump):\n"); dump_buf(buf2, len); - return -1; + err =3D -1; + goto out; } pr_debug("Bytes read match those read by objdump\n"); - - return 0; +out: + return err; } =20 static int process_sample_event(struct machine *machine, --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 7541DC7618A for ; Mon, 20 Mar 2023 03:39:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229971AbjCTDjr (ORCPT ); Sun, 19 Mar 2023 23:39:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229754AbjCTDjZ (ORCPT ); Sun, 19 Mar 2023 23:39:25 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64DE82279D for ; Sun, 19 Mar 2023 20:39:09 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5446a91c40cso109976187b3.18 for ; Sun, 19 Mar 2023 20:39:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283548; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=jWDjPPDoVS0JDf2qDq/S5D6izq+O5FLiZzLIDYhTDcw=; b=nLz4ieuq6UQ5762iyzEeXxWSNK23K6yAuzQg63tWTPUIEyVjby55CsOVPeUtvgl1yI HcneNx08ADteLp+/O4bi+8YI0ur6CZ7cZBuHbFdrn/k2l5ddq7nfLQILexB0idnupNXt w//iLOiyCoxAN57trk5THriviyko0qKJ5C0TCMc24ZSufaekqPSTiY2CwqtDNhcW91BN 07P8aJ15SRl2/dwVRVr0S6lrQYbmxbFi/IwX/Ecr9HTBRoRLBSVu7S4R3Dm6qFRT66IW +ums7G4p3gNHeT9ZvVnnPnnTzYl3upmQoDXnwHM6tvEhQjSqj9mSuYE4OFEdrsf+z/+E djGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283548; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jWDjPPDoVS0JDf2qDq/S5D6izq+O5FLiZzLIDYhTDcw=; b=qPipz7zCFtV4AbsYf/Fj594NMUCuO0FxCgfBg6eHo2YmQ65zrb8ChffPFc0kIgBH5F FZnRd9yUs1MwrvRLBXovUUTsaMVjLTjqTBTF2g5Pl8XEiCzFj74v5A9WleKNP33bxds9 CIovNqOXVperDVwMi7HHnTHGdP5wQfIzvXcPw/HBrj0rgDhjDHcxKvDmsUKZwIQYf0pf vZ2/ISTQ4xHPgkQJFQPv8aAQoI7Lyq9AEdmSc2/uN/1wtzAAIm2lF5JHfHjTP6lMe9TK 4xSF5rryLUFCoWR4F7oH5/zSicf7fnf5hc8aO0zXO1G3Sk3rVrC1uQ9HmAaCOLXeYs+K zX4w== X-Gm-Message-State: AO0yUKWZCE8uaFlkn68qA8sTFjUosxAfgd7aZrnN9maLlZazR/t4u+Yg bSwbbX2SK0pYZ6ibl5iyreFDkiykMHeE X-Google-Smtp-Source: AK7set+e/p/tdOZmnVTvRXJkVG1NofWhKYs0Acw/s+ryqCYiUJ57g7N82BnK7fiHBi5vw8sOBCGCb/J/FUzC X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a81:af02:0:b0:541:822f:af40 with SMTP id n2-20020a81af02000000b00541822faf40mr9649912ywh.10.1679283548003; Sun, 19 Mar 2023 20:39:08 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:52 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-5-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 04/22] perf test: Fix memory leak in symbols From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" machine__delete doesn't delete threads. Add call to delete threads ahead of deleting the machine. Signed-off-by: Ian Rogers --- tools/perf/tests/symbols.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c index 057b16df6416..0793f8f419e2 100644 --- a/tools/perf/tests/symbols.c +++ b/tools/perf/tests/symbols.c @@ -38,6 +38,7 @@ static int init_test_info(struct test_info *ti) static void exit_test_info(struct test_info *ti) { thread__put(ti->thread); + machine__delete_threads(ti->machine); machine__delete(ti->machine); } =20 --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 4E08BC7618A for ; Mon, 20 Mar 2023 03:40:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229826AbjCTDj7 (ORCPT ); Sun, 19 Mar 2023 23:39:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbjCTDjk (ORCPT ); Sun, 19 Mar 2023 23:39:40 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41A1C1B55A for ; Sun, 19 Mar 2023 20:39:18 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id z31-20020a25a122000000b00b38d2b9a2e9so11653999ybh.3 for ; Sun, 19 Mar 2023 20:39:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283555; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Dp06SaO3sEqKpsudj2OEFM6hmtCSHDlmEVrT0Oyh9g0=; b=oVlLy7C6zk07uvV078+G3tAiOSzYAnnphvIKsGkr7X87lY7rdXejY5aqdAZhnZqurU NLmHm30WqEW3T4Ohz83AZNdwIWA6BqcBR51g66JhSdnUB7xRaQw7PU2zYgMibh7YrqIq 9ZpEBh7W7VfsRswyeQNnDf7VnFEPy0zWYb+cMafP1CKpk/2l3eQRqA1CnuX1SzUIaMWr r63nsFVxf5KF2TKfTYcpNWMAD4DyXgcYuzFT/9jJUuHF6VK6HrQ07RrMtoOuAL/ZjWKt zoWMGS/5DkjqASK2ZdZrIcQmSSRA/BLLQIk4M7DZAHsdX88Ep1WdRiCCP8rADhVeSBRx 70KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283555; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Dp06SaO3sEqKpsudj2OEFM6hmtCSHDlmEVrT0Oyh9g0=; b=Flt1qRzIYOVnHmTvpISEhnu0iJ7Si6VxaT2SJ7xeUAS95UlL+qOjgABwdljZpmnjQE vGb/a7kPlJrYA9e90mFcchOXpNV59vLD4gFmvuw2iak1kgwY46DzaxG/T+FWhu+K40dt vHkcweocdzIUFGrmLXs6tJy6yAXaSInbi1bCV4NNZ1zv1BeISvvF8VMnm+T9j20GoGGJ LAdirZx2wikngAkQWurMeHJeci+Fg9TQGbQ6+vDbNrG4aEjtxSTED4zI2YgNMqp0IDPX HTAoxrt63DAgnvyiW34DnWL+RqjlWD/OthXw352SVlipVkyaE6Q6HGdsRgwCeTa6l+mB jrFg== X-Gm-Message-State: AO0yUKVSsqPKzj4jUdxyHnEptzrGQeL0skurXYwgcTpmti/qBIKY3Gvp Eep1q8ILoeYAPfgpAvdhPr4zqGtiHKjU X-Google-Smtp-Source: AK7set8DXFIYSHJXW7PIqklNOFVLXkRtk81Ryce9utE5CyGINekjemf91kZj1GKnU0xUvk3YNr67AY0kdQGO X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a81:b649:0:b0:541:8995:5334 with SMTP id h9-20020a81b649000000b0054189955334mr9279059ywk.3.1679283555031; Sun, 19 Mar 2023 20:39:15 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:53 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-6-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 05/22] perf symbol: Sort names under write lock From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If finding a name doesn't find the sorted names then they are allocated and sorted. This shouldn't be done under a read lock as another reader may access it. Release the read lock and acquire the write lock, then release the write lock and reacquire the read lock. Signed-off-by: Ian Rogers --- tools/perf/util/symbol.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 317c0706852f..a458aa8b87bb 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -2018,6 +2018,9 @@ static int map__groups__sort_by_name_from_rbtree(stru= ct maps *maps) if (maps_by_name =3D=3D NULL) return -1; =20 + up_read(&maps->lock); + down_write(&maps->lock); + maps->maps_by_name =3D maps_by_name; maps->nr_maps_allocated =3D maps->nr_maps; =20 @@ -2025,6 +2028,10 @@ static int map__groups__sort_by_name_from_rbtree(str= uct maps *maps) maps_by_name[i++] =3D map; =20 __maps__sort_by_name(maps); + + up_write(&maps->lock); + down_read(&maps->lock); + return 0; } =20 --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 69328C7618A for ; Mon, 20 Mar 2023 03:40:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229995AbjCTDkR (ORCPT ); Sun, 19 Mar 2023 23:40:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229983AbjCTDjt (ORCPT ); Sun, 19 Mar 2023 23:39:49 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 888AB22039 for ; Sun, 19 Mar 2023 20:39:23 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5446a91c40cso109980077b3.18 for ; Sun, 19 Mar 2023 20:39:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283563; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=SEgK9Ln5LjjiXI0L0Kxets4CmUmlfUmvUqPqbDDTdl0=; b=iE93voMhxSdI6g4grMz6AeHPcOpjoN6MQEnEy0aqJzAZ3zuTMBmYzejdVWAqWA4V8P kzwYl5AogceXE8LWM38Fhj1fN142/VT9urxjAsrH8a3Gvul+f/5tqRcq1zTQsPY6YJNH ImVpubBBkhXjtpMXJI5Q2szGSmBptjjf9WgwqpsKmboKbm0LeOgzQbifuFzxI8KyyX9W 8X/D2BEOuIZ/9ERwAwqPt8u/HDN8YZnQmQlF6FuT6dGVQboECclpGfAdHJPjZpyZIB26 c+ZIPEyx4BNCGSkMjLzkn9G2FO8Otp1yUgzS8KS5MP3zfN1dr2dTJ8d+28A/QKgR9Sbf mhJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283563; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=SEgK9Ln5LjjiXI0L0Kxets4CmUmlfUmvUqPqbDDTdl0=; b=4tLWnpTvXCJA0jmqrY6Ag3HvtwHKFaahLqXysD4Vno1xgqziIm4rDQBu6xCojHsXcZ 0YPTMCAX2CHMI6a+I3MBrAO1W7h3WXLRKir1OeMRNspALrAH+CJ8nblyz1r0RQmqS4/Q nC0HJax0JRENZKfy+fuR13VL6DjLPulLrTO9iUHELZbxxelh9CcyFo+F+ZKI7IwHu/cS 9RlhcdehcxXN/AtzQDmfdvgRq73Xe7eri+jC1tunCG+n8hxFco0fFcF1w5O55ZEDnYRF MK2bwjg8muYx2Zoy6Aqw9Eow/pwVJPz1TiA3jcrLLf6h89YIs+kBxaoAMHU0VT14RDRm BJWg== X-Gm-Message-State: AO0yUKUX90MgaU2EkE42H8v7xobGzfqNJr28OdQGyd6/2OFgVdldkN1E iOdXnuCuajGzGWsOVbolpoEcBFINI+kA X-Google-Smtp-Source: AK7set+JWLD5JfFl2BO2e4AAJo+GfgSVDq7gMoZgxs2HnqkHPNuL7Ce2Y+zBprz0etVpkiET3eUbnZCV3vsF X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:1896:b0:b17:294f:fb30 with SMTP id cj22-20020a056902189600b00b17294ffb30mr4768643ybb.2.1679283563183; Sun, 19 Mar 2023 20:39:23 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:54 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-7-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 06/22] perf map: Move map list node into symbol From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Using a perf map as a list node is only done in symbol. Move the list_node struct into symbol as a single pointer to the map. This makes reference count behavior more obvious and easy to check. Signed-off-by: Ian Rogers --- tools/perf/util/map.h | 5 +-- tools/perf/util/symbol.c | 90 ++++++++++++++++++++++++++-------------- 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 3dcfe06db6b3..2879cae05ee0 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -16,10 +16,7 @@ struct maps; struct machine; =20 struct map { - union { - struct rb_node rb_node; - struct list_head node; - }; + struct rb_node rb_node; u64 start; u64 end; bool erange_warned:1; diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index a458aa8b87bb..2676a163e237 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -48,6 +48,11 @@ static bool symbol__is_idle(const char *name); int vmlinux_path__nr_entries; char **vmlinux_path; =20 +struct map_list_node { + struct list_head node; + struct map *map; +}; + struct symbol_conf symbol_conf =3D { .nanosecs =3D false, .use_modules =3D true, @@ -1219,16 +1224,22 @@ struct kcore_mapfn_data { static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data) { struct kcore_mapfn_data *md =3D data; - struct map *map; + struct map_list_node *list_node; + + list_node =3D malloc(sizeof(*list_node)); + if (list_node =3D=3D NULL) + return -ENOMEM; =20 - map =3D map__new2(start, md->dso); - if (map =3D=3D NULL) + list_node->map =3D map__new2(start, md->dso); + if (list_node->map =3D=3D NULL) { + free(list_node); return -ENOMEM; + } =20 - map->end =3D map->start + len; - map->pgoff =3D pgoff; + list_node->map->end =3D list_node->map->start + len; + list_node->map->pgoff =3D pgoff; =20 - list_add(&map->node, &md->maps); + list_add(&list_node->node, &md->maps); =20 return 0; } @@ -1264,12 +1275,19 @@ int maps__merge_in(struct maps *kmaps, struct map *= new_map) * |new.............| -> |new..| |new..| * |old....| -> |old....| */ - struct map *m =3D map__clone(new_map); + struct map_list_node *m; =20 + m =3D malloc(sizeof(*m)); if (!m) return -ENOMEM; =20 - m->end =3D old_map->start; + m->map =3D map__clone(new_map); + if (!m->map) { + free(m); + return -ENOMEM; + } + + m->map->end =3D old_map->start; list_add_tail(&m->node, &merged); new_map->pgoff +=3D old_map->end - new_map->start; new_map->start =3D old_map->end; @@ -1299,10 +1317,13 @@ int maps__merge_in(struct maps *kmaps, struct map *= new_map) } =20 while (!list_empty(&merged)) { - old_map =3D list_entry(merged.next, struct map, node); - list_del_init(&old_map->node); - maps__insert(kmaps, old_map); - map__put(old_map); + struct map_list_node *old_node; + + old_node =3D list_entry(merged.next, struct map_list_node, node); + list_del_init(&old_node->node); + maps__insert(kmaps, old_node->map); + map__put(old_node->map); + free(old_node); } =20 if (new_map) { @@ -1317,7 +1338,7 @@ static int dso__load_kcore(struct dso *dso, struct ma= p *map, { struct maps *kmaps =3D map__kmaps(map); struct kcore_mapfn_data md; - struct map *old_map, *new_map, *replacement_map =3D NULL, *next; + struct map *old_map, *replacement_map =3D NULL, *next; struct machine *machine; bool is_64_bit; int err, fd; @@ -1378,11 +1399,12 @@ static int dso__load_kcore(struct dso *dso, struct = map *map, /* Find the kernel map using the '_stext' symbol */ if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) { u64 replacement_size =3D 0; + struct map_list_node *new_node; =20 - list_for_each_entry(new_map, &md.maps, node) { - u64 new_size =3D new_map->end - new_map->start; + list_for_each_entry(new_node, &md.maps, node) { + u64 new_size =3D new_node->map->end - new_node->map->start; =20 - if (!(stext >=3D new_map->start && stext < new_map->end)) + if (!(stext >=3D new_node->map->start && stext < new_node->map->end)) continue; =20 /* @@ -1392,40 +1414,43 @@ static int dso__load_kcore(struct dso *dso, struct = map *map, * falls within more than one in the list. */ if (!replacement_map || new_size < replacement_size) { - replacement_map =3D new_map; + replacement_map =3D new_node->map; replacement_size =3D new_size; } } } =20 if (!replacement_map) - replacement_map =3D list_entry(md.maps.next, struct map, node); + replacement_map =3D list_entry(md.maps.next, struct map_list_node, node)= ->map; =20 /* Add new maps */ while (!list_empty(&md.maps)) { - new_map =3D list_entry(md.maps.next, struct map, node); - list_del_init(&new_map->node); - if (new_map =3D=3D replacement_map) { - map->start =3D new_map->start; - map->end =3D new_map->end; - map->pgoff =3D new_map->pgoff; - map->map_ip =3D new_map->map_ip; - map->unmap_ip =3D new_map->unmap_ip; + struct map_list_node *new_node; + + new_node =3D list_entry(md.maps.next, struct map_list_node, node); + list_del_init(&new_node->node); + if (new_node->map =3D=3D replacement_map) { + map->start =3D new_node->map->start; + map->end =3D new_node->map->end; + map->pgoff =3D new_node->map->pgoff; + map->map_ip =3D new_node->map->map_ip; + map->unmap_ip =3D new_node->map->unmap_ip; /* Ensure maps are correctly ordered */ map__get(map); maps__remove(kmaps, map); maps__insert(kmaps, map); map__put(map); - map__put(new_map); + map__put(new_node->map); } else { /* * Merge kcore map into existing maps, * and ensure that current maps (eBPF) * stay intact. */ - if (maps__merge_in(kmaps, new_map)) + if (maps__merge_in(kmaps, new_node->map)) goto out_err; } + free(new_node); } =20 if (machine__is(machine, "x86_64")) { @@ -1462,9 +1487,12 @@ static int dso__load_kcore(struct dso *dso, struct m= ap *map, =20 out_err: while (!list_empty(&md.maps)) { - map =3D list_entry(md.maps.next, struct map, node); - list_del_init(&map->node); - map__put(map); + struct map_list_node *list_node; + + list_node =3D list_entry(md.maps.next, struct map_list_node, node); + list_del_init(&list_node->node); + map__put(list_node->map); + free(list_node); } close(fd); return -EINVAL; --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 DB986C7618A for ; Mon, 20 Mar 2023 03:40:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229933AbjCTDkd (ORCPT ); Sun, 19 Mar 2023 23:40:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229836AbjCTDkB (ORCPT ); Sun, 19 Mar 2023 23:40:01 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D74BA22798 for ; Sun, 19 Mar 2023 20:39:39 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 204-20020a250fd5000000b00b6d6655dc35so439777ybp.6 for ; Sun, 19 Mar 2023 20:39:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283571; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yXNogTUg851iWaq/BXAqub9AU/jLhe361hVCgOTJKwk=; b=fM8KSNZQYf5k2RW6lmMxfktdVjN5MBwoxmfn0DmUDkTXeyhpr3bVi/Gg+dqjX6MBYQ CnT3nBQ38gyndee1XyypGgzti5TT9ajRw8ntTVdMWVBJTOSbFoWNNwiZK6O9cvQQLw4N AzdKirk669I7hX152E1PYdLD+jbuUnsrWsUjINuLjD+4ys/7S1FvDHdcs6oV/bMBBQiU PjurPRWy7jVWFIt0zV6xCSTSas6d5N+Tq8JVL7L2zlYh3cgyRQN8UPeAkSp4jR2zNiC2 OJX2ZauIUofWXQHiUtcElnhQfrim8Iula+S5Ncm/q9jRnHyVlvlfI5aLZZTp98NAF2Js PAlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283571; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yXNogTUg851iWaq/BXAqub9AU/jLhe361hVCgOTJKwk=; b=ZKllJvFJk37smmQqHXFYuqVPlz3xaLF8uWJkktHnQUJmdAarq8TCBRY1u+Q0wMDZIc tZPiHwTGLyyz6AMx2OhN3mkbZOIeT2q53/jpPJ978s4gZYc+KTWgYsgUdxGZuh+ylyvX Tf0IfK8VFZ3uGAicE82VztpJiYr3g29fTRJ3tgwEjmwTXAUE75dAeQI16R1TEWOeb9ae jU2JtCgYqHdYEpQ9HfbEXgbcqsqZcWNQs0xX5WROF62V6UmfmFCPG2HWJ0iMf3v0tRvk 0DLkUbfENSDgg0VNvOHI82doyHZanfVBkdHj59rASeRCY5Vk6n5XxtpEQNOOkEXyAZ1V QJYA== X-Gm-Message-State: AO0yUKVdlRBeORMufM7kopsYijdIIJn809T6ph7ziDJ2YWBWA6Hh/FBv kzKNK5z6jGtDCWufz8Wjv7BnFbVFbfRd X-Google-Smtp-Source: AK7set+yqJGMY9ZgZB1weyc1JjfI4+z0qbak89nEOyPU8P23deQ0TCg2yDATE46HG3GqlFMkxq2zHTWo7Krt X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a81:c606:0:b0:541:9063:8e9e with SMTP id l6-20020a81c606000000b0054190638e9emr8953346ywi.2.1679283571587; Sun, 19 Mar 2023 20:39:31 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:55 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-8-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 07/22] perf maps: Remove rb_node from struct map From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" struct map is reference counted, having it also be a node in an red-black tree complicates the reference counting. Switch to having a map_rb_node which is a red-block tree node but points at the reference counted struct map. This reference is responsible for a single reference count. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/util/event.c | 13 +- tools/perf/builtin-report.c | 6 +- tools/perf/tests/maps.c | 8 +- tools/perf/tests/vmlinux-kallsyms.c | 17 ++- tools/perf/util/bpf_lock_contention.c | 2 +- tools/perf/util/machine.c | 68 ++++++---- tools/perf/util/map.c | 16 --- tools/perf/util/map.h | 1 - tools/perf/util/maps.c | 180 +++++++++++++++++--------- tools/perf/util/maps.h | 17 ++- tools/perf/util/probe-event.c | 18 ++- tools/perf/util/symbol-elf.c | 9 +- tools/perf/util/symbol.c | 77 +++++++---- tools/perf/util/synthetic-events.c | 26 ++-- tools/perf/util/thread.c | 10 +- tools/perf/util/vdso.c | 7 +- 16 files changed, 291 insertions(+), 184 deletions(-) diff --git a/tools/perf/arch/x86/util/event.c b/tools/perf/arch/x86/util/ev= ent.c index e4288d09f3a0..17bf60babfbd 100644 --- a/tools/perf/arch/x86/util/event.c +++ b/tools/perf/arch/x86/util/event.c @@ -19,7 +19,7 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *= tool, struct machine *machine) { int rc =3D 0; - struct map *pos; + struct map_rb_node *pos; struct maps *kmaps =3D machine__kernel_maps(machine); union perf_event *event =3D zalloc(sizeof(event->mmap) + machine->id_hdr_size); @@ -33,11 +33,12 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool= *tool, maps__for_each_entry(kmaps, pos) { struct kmap *kmap; size_t size; + struct map *map =3D pos->map; =20 - if (!__map__is_extra_kernel_map(pos)) + if (!__map__is_extra_kernel_map(map)) continue; =20 - kmap =3D map__kmap(pos); + kmap =3D map__kmap(map); =20 size =3D sizeof(event->mmap) - sizeof(event->mmap.filename) + PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) + @@ -58,9 +59,9 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *= tool, =20 event->mmap.header.size =3D size; =20 - event->mmap.start =3D pos->start; - event->mmap.len =3D pos->end - pos->start; - event->mmap.pgoff =3D pos->pgoff; + event->mmap.start =3D map->start; + event->mmap.len =3D map->end - map->start; + event->mmap.pgoff =3D map->pgoff; event->mmap.pid =3D machine->pid; =20 strlcpy(event->mmap.filename, kmap->name, PATH_MAX); diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 6400615b5e98..c453b7fa7418 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -840,9 +840,11 @@ static struct task *tasks_list(struct task *task, stru= ct machine *machine) static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp) { size_t printed =3D 0; - struct map *map; + struct map_rb_node *rb_node; + + maps__for_each_entry(maps, rb_node) { + struct map *map =3D rb_node->map; =20 - maps__for_each_entry(maps, map) { printed +=3D fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRI= x64 " %" PRIu64 " %s\n", indent, "", map->start, map->end, map->prot & PROT_READ ? 'r' : '-', diff --git a/tools/perf/tests/maps.c b/tools/perf/tests/maps.c index a69988a89d26..8246d37e4b7a 100644 --- a/tools/perf/tests/maps.c +++ b/tools/perf/tests/maps.c @@ -15,10 +15,12 @@ struct map_def { =20 static int check_maps(struct map_def *merged, unsigned int size, struct ma= ps *maps) { - struct map *map; + struct map_rb_node *rb_node; unsigned int i =3D 0; =20 - maps__for_each_entry(maps, map) { + maps__for_each_entry(maps, rb_node) { + struct map *map =3D rb_node->map; + if (i > 0) TEST_ASSERT_VAL("less maps expected", (map && i < size) || (!map && i = =3D=3D size)); =20 @@ -74,7 +76,7 @@ static int test__maps__merge_in(struct test_suite *t __ma= ybe_unused, int subtest =20 map->start =3D bpf_progs[i].start; map->end =3D bpf_progs[i].end; - maps__insert(maps, map); + TEST_ASSERT_VAL("failed to insert map", maps__insert(maps, map) =3D=3D 0= ); map__put(map); } =20 diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux= -kallsyms.c index 8ab035b55875..c8abb3ca8347 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -118,7 +118,8 @@ static int test__vmlinux_matches_kallsyms(struct test_s= uite *test __maybe_unused int err =3D TEST_FAIL; struct rb_node *nd; struct symbol *sym; - struct map *kallsyms_map, *vmlinux_map, *map; + struct map *kallsyms_map, *vmlinux_map; + struct map_rb_node *rb_node; struct machine kallsyms, vmlinux; struct maps *maps; u64 mem_start, mem_end; @@ -290,15 +291,15 @@ static int test__vmlinux_matches_kallsyms(struct test= _suite *test __maybe_unused =20 header_printed =3D false; =20 - maps__for_each_entry(maps, map) { - struct map * + maps__for_each_entry(maps, rb_node) { + struct map *map =3D rb_node->map; /* * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while * the kernel will have the path for the vmlinux file being used, * so use the short name, less descriptive but the same ("[kernel]" in * both cases. */ - pair =3D maps__find_by_name(kallsyms.kmaps, (map->dso->kernel ? + struct map *pair =3D maps__find_by_name(kallsyms.kmaps, (map->dso->kerne= l ? map->dso->short_name : map->dso->name)); if (pair) { @@ -314,8 +315,8 @@ static int test__vmlinux_matches_kallsyms(struct test_s= uite *test __maybe_unused =20 header_printed =3D false; =20 - maps__for_each_entry(maps, map) { - struct map *pair; + maps__for_each_entry(maps, rb_node) { + struct map *pair, *map =3D rb_node->map; =20 mem_start =3D vmlinux_map->unmap_ip(vmlinux_map, map->start); mem_end =3D vmlinux_map->unmap_ip(vmlinux_map, map->end); @@ -344,7 +345,9 @@ static int test__vmlinux_matches_kallsyms(struct test_s= uite *test __maybe_unused =20 maps =3D machine__kernel_maps(&kallsyms); =20 - maps__for_each_entry(maps, map) { + maps__for_each_entry(maps, rb_node) { + struct map *map =3D rb_node->map; + if (!map->priv) { if (!header_printed) { pr_info("WARN: Maps only in kallsyms:\n"); diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lo= ck_contention.c index 235fc7150545..0b47863d2460 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -282,7 +282,7 @@ int lock_contention_read(struct lock_contention *con) } =20 /* make sure it loads the kernel map */ - map__load(maps__first(machine->kmaps)); + map__load(maps__first(machine->kmaps)->map); =20 prev_key =3D NULL; while (!bpf_map_get_next_key(fd, prev_key, &key)) { diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 803c9d1803dd..93a07079d174 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -882,6 +882,7 @@ static int machine__process_ksymbol_register(struct mac= hine *machine, =20 if (!map) { struct dso *dso =3D dso__new(event->ksymbol.name); + int err; =20 if (dso) { dso->kernel =3D DSO_SPACE__KERNEL; @@ -901,8 +902,11 @@ static int machine__process_ksymbol_register(struct ma= chine *machine, =20 map->start =3D event->ksymbol.addr; map->end =3D map->start + event->ksymbol.len; - maps__insert(machine__kernel_maps(machine), map); + err =3D maps__insert(machine__kernel_maps(machine), map); map__put(map); + if (err) + return err; + dso__set_loaded(dso); =20 if (is_bpf_image(event->ksymbol.name)) { @@ -1002,6 +1006,7 @@ static struct map *machine__addnew_module_map(struct = machine *machine, u64 start struct map *map =3D NULL; struct kmod_path m; struct dso *dso; + int err; =20 if (kmod_path__parse_name(&m, filename)) return NULL; @@ -1014,10 +1019,14 @@ static struct map *machine__addnew_module_map(struc= t machine *machine, u64 start if (map =3D=3D NULL) goto out; =20 - maps__insert(machine__kernel_maps(machine), map); + err =3D maps__insert(machine__kernel_maps(machine), map); =20 /* Put the map here because maps__insert already got it */ map__put(map); + + /* If maps__insert failed, return NULL. */ + if (err) + map =3D NULL; out: /* put the dso here, corresponding to machine__findnew_module_dso */ dso__put(dso); @@ -1184,10 +1193,11 @@ int machine__create_extra_kernel_map(struct machine= *machine, { struct kmap *kmap; struct map *map; + int err; =20 map =3D map__new2(xm->start, kernel); if (!map) - return -1; + return -ENOMEM; =20 map->end =3D xm->end; map->pgoff =3D xm->pgoff; @@ -1196,14 +1206,16 @@ int machine__create_extra_kernel_map(struct machine= *machine, =20 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN); =20 - maps__insert(machine__kernel_maps(machine), map); + err =3D maps__insert(machine__kernel_maps(machine), map); =20 - pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n", - kmap->name, map->start, map->end); + if (!err) { + pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n", + kmap->name, map->start, map->end); + } =20 map__put(map); =20 - return 0; + return err; } =20 static u64 find_entry_trampoline(struct dso *dso) @@ -1244,16 +1256,16 @@ int machine__map_x86_64_entry_trampolines(struct ma= chine *machine, struct maps *kmaps =3D machine__kernel_maps(machine); int nr_cpus_avail, cpu; bool found =3D false; - struct map *map; + struct map_rb_node *rb_node; u64 pgoff; =20 /* * In the vmlinux case, pgoff is a virtual address which must now be * mapped to a vmlinux offset. */ - maps__for_each_entry(kmaps, map) { + maps__for_each_entry(kmaps, rb_node) { + struct map *dest_map, *map =3D rb_node->map; struct kmap *kmap =3D __map__kmap(map); - struct map *dest_map; =20 if (!kmap || !is_entry_trampoline(kmap->name)) continue; @@ -1308,11 +1320,10 @@ __machine__create_kernel_maps(struct machine *machi= ne, struct dso *kernel) =20 machine->vmlinux_map =3D map__new2(0, kernel); if (machine->vmlinux_map =3D=3D NULL) - return -1; + return -ENOMEM; =20 machine->vmlinux_map->map_ip =3D machine->vmlinux_map->unmap_ip =3D ident= ity__map_ip; - maps__insert(machine__kernel_maps(machine), machine->vmlinux_map); - return 0; + return maps__insert(machine__kernel_maps(machine), machine->vmlinux_map); } =20 void machine__destroy_kernel_maps(struct machine *machine) @@ -1634,25 +1645,26 @@ static void machine__set_kernel_mmap(struct machine= *machine, machine->vmlinux_map->end =3D ~0ULL; } =20 -static void machine__update_kernel_mmap(struct machine *machine, +static int machine__update_kernel_mmap(struct machine *machine, u64 start, u64 end) { struct map *map =3D machine__kernel_map(machine); + int err; =20 map__get(map); maps__remove(machine__kernel_maps(machine), map); =20 machine__set_kernel_mmap(machine, start, end); =20 - maps__insert(machine__kernel_maps(machine), map); + err =3D maps__insert(machine__kernel_maps(machine), map); map__put(map); + return err; } =20 int machine__create_kernel_maps(struct machine *machine) { struct dso *kernel =3D machine__get_kernel(machine); const char *name =3D NULL; - struct map *map; u64 start =3D 0, end =3D ~0ULL; int ret; =20 @@ -1684,7 +1696,9 @@ int machine__create_kernel_maps(struct machine *machi= ne) * we have a real start address now, so re-order the kmaps * assume it's the last in the kmaps */ - machine__update_kernel_mmap(machine, start, end); + ret =3D machine__update_kernel_mmap(machine, start, end); + if (ret < 0) + goto out_put; } =20 if (machine__create_extra_kernel_maps(machine, kernel)) @@ -1692,9 +1706,12 @@ int machine__create_kernel_maps(struct machine *mach= ine) =20 if (end =3D=3D ~0ULL) { /* update end address of the kernel map using adjacent module address */ - map =3D map__next(machine__kernel_map(machine)); - if (map) - machine__set_kernel_mmap(machine, start, map->start); + struct map_rb_node *rb_node =3D maps__find_node(machine__kernel_maps(mac= hine), + machine__kernel_map(machine)); + struct map_rb_node *next =3D map_rb_node__next(rb_node); + + if (next) + machine__set_kernel_mmap(machine, start, next->map->start); } =20 out_put: @@ -1827,7 +1844,10 @@ static int machine__process_kernel_mmap_event(struct= machine *machine, if (strstr(kernel->long_name, "vmlinux")) dso__set_short_name(kernel, "[kernel.vmlinux]", false); =20 - machine__update_kernel_mmap(machine, xm->start, xm->end); + if (machine__update_kernel_mmap(machine, xm->start, xm->end) < 0) { + dso__put(kernel); + goto out_problem; + } =20 if (build_id__is_defined(bid)) dso__set_build_id(kernel, bid); @@ -3325,11 +3345,11 @@ int machine__for_each_dso(struct machine *machine, = machine__dso_t fn, void *priv int machine__for_each_kernel_map(struct machine *machine, machine__map_t f= n, void *priv) { struct maps *maps =3D machine__kernel_maps(machine); - struct map *map; + struct map_rb_node *pos; int err =3D 0; =20 - for (map =3D maps__first(maps); map !=3D NULL; map =3D map__next(map)) { - err =3D fn(map, priv); + maps__for_each_entry(maps, pos) { + err =3D fn(pos->map, priv); if (err !=3D 0) { break; } diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index f3a3d9b3a40d..7620cfa114d4 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -111,7 +111,6 @@ void map__init(struct map *map, u64 start, u64 end, u64= pgoff, struct dso *dso) map->dso =3D dso__get(dso); map->map_ip =3D map__map_ip; map->unmap_ip =3D map__unmap_ip; - RB_CLEAR_NODE(&map->rb_node); map->erange_warned =3D false; refcount_set(&map->refcnt, 1); } @@ -397,7 +396,6 @@ struct map *map__clone(struct map *from) map =3D memdup(from, size); if (map !=3D NULL) { refcount_set(&map->refcnt, 1); - RB_CLEAR_NODE(&map->rb_node); dso__get(map->dso); } =20 @@ -537,20 +535,6 @@ bool map__contains_symbol(const struct map *map, const= struct symbol *sym) return ip >=3D map->start && ip < map->end; } =20 -static struct map *__map__next(struct map *map) -{ - struct rb_node *next =3D rb_next(&map->rb_node); - - if (next) - return rb_entry(next, struct map, rb_node); - return NULL; -} - -struct map *map__next(struct map *map) -{ - return map ? __map__next(map) : NULL; -} - struct kmap *__map__kmap(struct map *map) { if (!map->dso || !map->dso->kernel) diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 2879cae05ee0..d1a6f85fd31d 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -16,7 +16,6 @@ struct maps; struct machine; =20 struct map { - struct rb_node rb_node; u64 start; u64 end; bool erange_warned:1; diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 37bd5b40000d..83ec126bcbe5 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -10,8 +10,6 @@ #include "ui/ui.h" #include "unwind.h" =20 -static void __maps__insert(struct maps *maps, struct map *map); - static void maps__init(struct maps *maps, struct machine *machine) { maps->entries =3D RB_ROOT; @@ -32,10 +30,44 @@ static void __maps__free_maps_by_name(struct maps *maps) maps->nr_maps_allocated =3D 0; } =20 -void maps__insert(struct maps *maps, struct map *map) +static int __maps__insert(struct maps *maps, struct map *map) { + struct rb_node **p =3D &maps->entries.rb_node; + struct rb_node *parent =3D NULL; + const u64 ip =3D map->start; + struct map_rb_node *m, *new_rb_node; + + new_rb_node =3D malloc(sizeof(*new_rb_node)); + if (!new_rb_node) + return -ENOMEM; + + RB_CLEAR_NODE(&new_rb_node->rb_node); + new_rb_node->map =3D map; + + while (*p !=3D NULL) { + parent =3D *p; + m =3D rb_entry(parent, struct map_rb_node, rb_node); + if (ip < m->map->start) + p =3D &(*p)->rb_left; + else + p =3D &(*p)->rb_right; + } + + rb_link_node(&new_rb_node->rb_node, parent, p); + rb_insert_color(&new_rb_node->rb_node, &maps->entries); + map__get(map); + return 0; +} + +int maps__insert(struct maps *maps, struct map *map) +{ + int err; + down_write(&maps->lock); - __maps__insert(maps, map); + err =3D __maps__insert(maps, map); + if (err) + goto out; + ++maps->nr_maps; =20 if (map->dso && map->dso->kernel) { @@ -59,32 +91,39 @@ void maps__insert(struct maps *maps, struct map *map) =20 if (maps_by_name =3D=3D NULL) { __maps__free_maps_by_name(maps); - up_write(&maps->lock); - return; + err =3D -ENOMEM; + goto out; } =20 maps->maps_by_name =3D maps_by_name; maps->nr_maps_allocated =3D nr_allocate; - } +} maps->maps_by_name[maps->nr_maps - 1] =3D map; __maps__sort_by_name(maps); } + out: up_write(&maps->lock); + return err; } =20 -static void __maps__remove(struct maps *maps, struct map *map) +static void __maps__remove(struct maps *maps, struct map_rb_node *rb_node) { - rb_erase_init(&map->rb_node, &maps->entries); - map__put(map); + rb_erase_init(&rb_node->rb_node, &maps->entries); + map__put(rb_node->map); + free(rb_node); } =20 void maps__remove(struct maps *maps, struct map *map) { + struct map_rb_node *rb_node; + down_write(&maps->lock); if (maps->last_search_by_name =3D=3D map) maps->last_search_by_name =3D NULL; =20 - __maps__remove(maps, map); + rb_node =3D maps__find_node(maps, map); + assert(rb_node->map =3D=3D map); + __maps__remove(maps, rb_node); --maps->nr_maps; if (maps->maps_by_name) __maps__free_maps_by_name(maps); @@ -93,11 +132,12 @@ void maps__remove(struct maps *maps, struct map *map) =20 static void __maps__purge(struct maps *maps) { - struct map *pos, *next; + struct map_rb_node *pos, *next; =20 maps__for_each_entry_safe(maps, pos, next) { rb_erase_init(&pos->rb_node, &maps->entries); - map__put(pos); + map__put(pos->map); + free(pos); } } =20 @@ -153,21 +193,21 @@ struct symbol *maps__find_symbol(struct maps *maps, u= 64 addr, struct map **mapp) struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *na= me, struct map **mapp) { struct symbol *sym; - struct map *pos; + struct map_rb_node *pos; =20 down_read(&maps->lock); =20 maps__for_each_entry(maps, pos) { - sym =3D map__find_symbol_by_name(pos, name); + sym =3D map__find_symbol_by_name(pos->map, name); =20 if (sym =3D=3D NULL) continue; - if (!map__contains_symbol(pos, sym)) { + if (!map__contains_symbol(pos->map, sym)) { sym =3D NULL; continue; } if (mapp !=3D NULL) - *mapp =3D pos; + *mapp =3D pos->map; goto out; } =20 @@ -196,15 +236,15 @@ int maps__find_ams(struct maps *maps, struct addr_map= _symbol *ams) size_t maps__fprintf(struct maps *maps, FILE *fp) { size_t printed =3D 0; - struct map *pos; + struct map_rb_node *pos; =20 down_read(&maps->lock); =20 maps__for_each_entry(maps, pos) { printed +=3D fprintf(fp, "Map:"); - printed +=3D map__fprintf(pos, fp); + printed +=3D map__fprintf(pos->map, fp); if (verbose > 2) { - printed +=3D dso__fprintf(pos->dso, fp); + printed +=3D dso__fprintf(pos->map->dso, fp); printed +=3D fprintf(fp, "--\n"); } } @@ -231,11 +271,11 @@ int maps__fixup_overlappings(struct maps *maps, struc= t map *map, FILE *fp) next =3D root->rb_node; first =3D NULL; while (next) { - struct map *pos =3D rb_entry(next, struct map, rb_node); + struct map_rb_node *pos =3D rb_entry(next, struct map_rb_node, rb_node); =20 - if (pos->end > map->start) { + if (pos->map->end > map->start) { first =3D next; - if (pos->start <=3D map->start) + if (pos->map->start <=3D map->start) break; next =3D next->rb_left; } else @@ -244,14 +284,14 @@ int maps__fixup_overlappings(struct maps *maps, struc= t map *map, FILE *fp) =20 next =3D first; while (next) { - struct map *pos =3D rb_entry(next, struct map, rb_node); + struct map_rb_node *pos =3D rb_entry(next, struct map_rb_node, rb_node); next =3D rb_next(&pos->rb_node); =20 /* * Stop if current map starts after map->end. * Maps are ordered by start: next will not overlap for sure. */ - if (pos->start >=3D map->end) + if (pos->map->start >=3D map->end) break; =20 if (verbose >=3D 2) { @@ -262,7 +302,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) } else { fputs("overlapping maps:\n", fp); map__fprintf(map, fp); - map__fprintf(pos, fp); + map__fprintf(pos->map, fp); } } =20 @@ -271,8 +311,8 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) * Now check if we need to create new maps for areas not * overlapped by the new map: */ - if (map->start > pos->start) { - struct map *before =3D map__clone(pos); + if (map->start > pos->map->start) { + struct map *before =3D map__clone(pos->map); =20 if (before =3D=3D NULL) { err =3D -ENOMEM; @@ -280,14 +320,17 @@ int maps__fixup_overlappings(struct maps *maps, struc= t map *map, FILE *fp) } =20 before->end =3D map->start; - __maps__insert(maps, before); + err =3D __maps__insert(maps, before); + if (err) + goto put_map; + if (verbose >=3D 2 && !use_browser) map__fprintf(before, fp); map__put(before); } =20 - if (map->end < pos->end) { - struct map *after =3D map__clone(pos); + if (map->end < pos->map->end) { + struct map *after =3D map__clone(pos->map); =20 if (after =3D=3D NULL) { err =3D -ENOMEM; @@ -295,15 +338,19 @@ int maps__fixup_overlappings(struct maps *maps, struc= t map *map, FILE *fp) } =20 after->start =3D map->end; - after->pgoff +=3D map->end - pos->start; - assert(pos->map_ip(pos, map->end) =3D=3D after->map_ip(after, map->end)= ); - __maps__insert(maps, after); + after->pgoff +=3D map->end - pos->map->start; + assert(pos->map->map_ip(pos->map, map->end) =3D=3D + after->map_ip(after, map->end)); + err =3D __maps__insert(maps, after); + if (err) + goto put_map; + if (verbose >=3D 2 && !use_browser) map__fprintf(after, fp); map__put(after); } put_map: - map__put(pos); + map__put(pos->map); =20 if (err) goto out; @@ -322,12 +369,12 @@ int maps__clone(struct thread *thread, struct maps *p= arent) { struct maps *maps =3D thread->maps; int err; - struct map *map; + struct map_rb_node *rb_node; =20 down_read(&parent->lock); =20 - maps__for_each_entry(parent, map) { - struct map *new =3D map__clone(map); + maps__for_each_entry(parent, rb_node) { + struct map *new =3D map__clone(rb_node->map); =20 if (new =3D=3D NULL) { err =3D -ENOMEM; @@ -338,7 +385,10 @@ int maps__clone(struct thread *thread, struct maps *pa= rent) if (err) goto out_unlock; =20 - maps__insert(maps, new); + err =3D maps__insert(maps, new); + if (err) + goto out_unlock; + map__put(new); } =20 @@ -348,40 +398,31 @@ int maps__clone(struct thread *thread, struct maps *p= arent) return err; } =20 -static void __maps__insert(struct maps *maps, struct map *map) +struct map_rb_node *maps__find_node(struct maps *maps, struct map *map) { - struct rb_node **p =3D &maps->entries.rb_node; - struct rb_node *parent =3D NULL; - const u64 ip =3D map->start; - struct map *m; + struct map_rb_node *rb_node; =20 - while (*p !=3D NULL) { - parent =3D *p; - m =3D rb_entry(parent, struct map, rb_node); - if (ip < m->start) - p =3D &(*p)->rb_left; - else - p =3D &(*p)->rb_right; + maps__for_each_entry(maps, rb_node) { + if (rb_node->map =3D=3D map) + return rb_node; } - - rb_link_node(&map->rb_node, parent, p); - rb_insert_color(&map->rb_node, &maps->entries); - map__get(map); + return NULL; } =20 struct map *maps__find(struct maps *maps, u64 ip) { struct rb_node *p; - struct map *m; + struct map_rb_node *m; + =20 down_read(&maps->lock); =20 p =3D maps->entries.rb_node; while (p !=3D NULL) { - m =3D rb_entry(p, struct map, rb_node); - if (ip < m->start) + m =3D rb_entry(p, struct map_rb_node, rb_node); + if (ip < m->map->start) p =3D p->rb_left; - else if (ip >=3D m->end) + else if (ip >=3D m->map->end) p =3D p->rb_right; else goto out; @@ -390,14 +431,29 @@ struct map *maps__find(struct maps *maps, u64 ip) m =3D NULL; out: up_read(&maps->lock); - return m; + return m ? m->map : NULL; } =20 -struct map *maps__first(struct maps *maps) +struct map_rb_node *maps__first(struct maps *maps) { struct rb_node *first =3D rb_first(&maps->entries); =20 if (first) - return rb_entry(first, struct map, rb_node); + return rb_entry(first, struct map_rb_node, rb_node); return NULL; } + +struct map_rb_node *map_rb_node__next(struct map_rb_node *node) +{ + struct rb_node *next; + + if (!node) + return NULL; + + next =3D rb_next(&node->rb_node); + + if (!next) + return NULL; + + return rb_entry(next, struct map_rb_node, rb_node); +} diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 7e729ff42749..512746ec0f9a 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -15,15 +15,22 @@ struct map; struct maps; struct thread; =20 +struct map_rb_node { + struct rb_node rb_node; + struct map *map; +}; + +struct map_rb_node *maps__first(struct maps *maps); +struct map_rb_node *map_rb_node__next(struct map_rb_node *node); +struct map_rb_node *maps__find_node(struct maps *maps, struct map *map); struct map *maps__find(struct maps *maps, u64 addr); -struct map *maps__first(struct maps *maps); -struct map *map__next(struct map *map); =20 #define maps__for_each_entry(maps, map) \ - for (map =3D maps__first(maps); map; map =3D map__next(map)) + for (map =3D maps__first(maps); map; map =3D map_rb_node__next(map)) =20 #define maps__for_each_entry_safe(maps, map, next) \ - for (map =3D maps__first(maps), next =3D map__next(map); map; map =3D nex= t, next =3D map__next(map)) + for (map =3D maps__first(maps), next =3D map_rb_node__next(map); map; \ + map =3D next, next =3D map_rb_node__next(map)) =20 struct maps { struct rb_root entries; @@ -63,7 +70,7 @@ void maps__put(struct maps *maps); int maps__clone(struct thread *thread, struct maps *parent); size_t maps__fprintf(struct maps *maps, FILE *fp); =20 -void maps__insert(struct maps *maps, struct map *map); +int maps__insert(struct maps *maps, struct map *map); =20 void maps__remove(struct maps *maps, struct map *map); =20 diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index 881d94f65a6b..cdf5d655d84c 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -151,23 +151,27 @@ static int kernel_get_symbol_address_by_name(const ch= ar *name, u64 *addr, static struct map *kernel_get_module_map(const char *module) { struct maps *maps =3D machine__kernel_maps(host_machine); - struct map *pos; + struct map_rb_node *pos; =20 /* A file path -- this is an offline module */ if (module && strchr(module, '/')) return dso__new_map(module); =20 if (!module) { - pos =3D machine__kernel_map(host_machine); - return map__get(pos); + struct map *map =3D machine__kernel_map(host_machine); + + return map__get(map); } =20 maps__for_each_entry(maps, pos) { /* short_name is "[module]" */ - if (strncmp(pos->dso->short_name + 1, module, - pos->dso->short_name_len - 2) =3D=3D 0 && - module[pos->dso->short_name_len - 2] =3D=3D '\0') { - return map__get(pos); + const char *short_name =3D pos->map->dso->short_name; + u16 short_name_len =3D pos->map->dso->short_name_len; + + if (strncmp(short_name + 1, module, + short_name_len - 2) =3D=3D 0 && + module[short_name_len - 2] =3D=3D '\0') { + return map__get(pos->map); } } return NULL; diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index c0a2de42c51b..325fbeea8dff 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1355,10 +1355,14 @@ static int dso__process_kernel_symbol(struct dso *d= so, struct map *map, map->unmap_ip =3D map__unmap_ip; /* Ensure maps are correctly ordered */ if (kmaps) { + int err; + map__get(map); maps__remove(kmaps, map); - maps__insert(kmaps, map); + err =3D maps__insert(kmaps, map); map__put(map); + if (err) + return err; } } =20 @@ -1411,7 +1415,8 @@ static int dso__process_kernel_symbol(struct dso *dso= , struct map *map, curr_map->map_ip =3D curr_map->unmap_ip =3D identity__map_ip; } curr_dso->symtab_type =3D dso->symtab_type; - maps__insert(kmaps, curr_map); + if (maps__insert(kmaps, curr_map)) + return -1; /* * Add it before we drop the reference to curr_map, i.e. while * we still are sure to have a reference to this DSO via diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 2676a163e237..cc23d111e2d3 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -268,13 +268,13 @@ void symbols__fixup_end(struct rb_root_cached *symbol= s, bool is_kallsyms) =20 void maps__fixup_end(struct maps *maps) { - struct map *prev =3D NULL, *curr; + struct map_rb_node *prev =3D NULL, *curr; =20 down_write(&maps->lock); =20 maps__for_each_entry(maps, curr) { - if (prev !=3D NULL && !prev->end) - prev->end =3D curr->start; + if (prev !=3D NULL && !prev->map->end) + prev->map->end =3D curr->map->start; =20 prev =3D curr; } @@ -283,8 +283,8 @@ void maps__fixup_end(struct maps *maps) * We still haven't the actual symbols, so guess the * last map final address. */ - if (curr && !curr->end) - curr->end =3D ~0ULL; + if (curr && !curr->map->end) + curr->map->end =3D ~0ULL; =20 up_write(&maps->lock); } @@ -937,7 +937,10 @@ static int maps__split_kallsyms(struct maps *kmaps, st= ruct dso *dso, u64 delta, } =20 curr_map->map_ip =3D curr_map->unmap_ip =3D identity__map_ip; - maps__insert(kmaps, curr_map); + if (maps__insert(kmaps, curr_map)) { + dso__put(ndso); + return -1; + } ++kernel_range; } else if (delta) { /* Kernel was relocated at boot time */ @@ -1125,14 +1128,15 @@ int compare_proc_modules(const char *from, const ch= ar *to) static int do_validate_kcore_modules(const char *filename, struct maps *km= aps) { struct rb_root modules =3D RB_ROOT; - struct map *old_map; + struct map_rb_node *old_node; int err; =20 err =3D read_proc_modules(filename, &modules); if (err) return err; =20 - maps__for_each_entry(kmaps, old_map) { + maps__for_each_entry(kmaps, old_node) { + struct map *old_map =3D old_node->map; struct module_info *mi; =20 if (!__map__is_kmodule(old_map)) { @@ -1250,10 +1254,13 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgof= f, void *data) */ int maps__merge_in(struct maps *kmaps, struct map *new_map) { - struct map *old_map; + struct map_rb_node *rb_node; LIST_HEAD(merged); + int err =3D 0; + + maps__for_each_entry(kmaps, rb_node) { + struct map *old_map =3D rb_node->map; =20 - maps__for_each_entry(kmaps, old_map) { /* no overload with this one */ if (new_map->end < old_map->start || new_map->start >=3D old_map->end) @@ -1278,13 +1285,16 @@ int maps__merge_in(struct maps *kmaps, struct map *= new_map) struct map_list_node *m; =20 m =3D malloc(sizeof(*m)); - if (!m) - return -ENOMEM; + if (!m) { + err =3D -ENOMEM; + goto out; + } =20 m->map =3D map__clone(new_map); if (!m->map) { free(m); - return -ENOMEM; + err =3D -ENOMEM; + goto out; } =20 m->map->end =3D old_map->start; @@ -1316,21 +1326,24 @@ int maps__merge_in(struct maps *kmaps, struct map *= new_map) } } =20 +out: while (!list_empty(&merged)) { struct map_list_node *old_node; =20 old_node =3D list_entry(merged.next, struct map_list_node, node); list_del_init(&old_node->node); - maps__insert(kmaps, old_node->map); + if (!err) + err =3D maps__insert(kmaps, old_node->map); map__put(old_node->map); free(old_node); } =20 if (new_map) { - maps__insert(kmaps, new_map); + if (!err) + err =3D maps__insert(kmaps, new_map); map__put(new_map); } - return 0; + return err; } =20 static int dso__load_kcore(struct dso *dso, struct map *map, @@ -1338,7 +1351,8 @@ static int dso__load_kcore(struct dso *dso, struct ma= p *map, { struct maps *kmaps =3D map__kmaps(map); struct kcore_mapfn_data md; - struct map *old_map, *replacement_map =3D NULL, *next; + struct map *replacement_map =3D NULL; + struct map_rb_node *old_node, *next; struct machine *machine; bool is_64_bit; int err, fd; @@ -1385,7 +1399,9 @@ static int dso__load_kcore(struct dso *dso, struct ma= p *map, } =20 /* Remove old maps */ - maps__for_each_entry_safe(kmaps, old_map, next) { + maps__for_each_entry_safe(kmaps, old_node, next) { + struct map *old_map =3D old_node->map; + /* * We need to preserve eBPF maps even if they are * covered by kcore, because we need to access @@ -1438,17 +1454,21 @@ static int dso__load_kcore(struct dso *dso, struct = map *map, /* Ensure maps are correctly ordered */ map__get(map); maps__remove(kmaps, map); - maps__insert(kmaps, map); + err =3D maps__insert(kmaps, map); map__put(map); map__put(new_node->map); + if (err) + goto out_err; } else { /* * Merge kcore map into existing maps, * and ensure that current maps (eBPF) * stay intact. */ - if (maps__merge_in(kmaps, new_node->map)) + if (maps__merge_in(kmaps, new_node->map)) { + err =3D -EINVAL; goto out_err; + } } free(new_node); } @@ -1495,7 +1515,7 @@ static int dso__load_kcore(struct dso *dso, struct ma= p *map, free(list_node); } close(fd); - return -EINVAL; + return err; } =20 /* @@ -2039,8 +2059,9 @@ void __maps__sort_by_name(struct maps *maps) =20 static int map__groups__sort_by_name_from_rbtree(struct maps *maps) { - struct map *map; - struct map **maps_by_name =3D realloc(maps->maps_by_name, maps->nr_maps *= sizeof(map)); + struct map_rb_node *rb_node; + struct map **maps_by_name =3D realloc(maps->maps_by_name, + maps->nr_maps * sizeof(struct map *)); int i =3D 0; =20 if (maps_by_name =3D=3D NULL) @@ -2052,8 +2073,8 @@ static int map__groups__sort_by_name_from_rbtree(stru= ct maps *maps) maps->maps_by_name =3D maps_by_name; maps->nr_maps_allocated =3D maps->nr_maps; =20 - maps__for_each_entry(maps, map) - maps_by_name[i++] =3D map; + maps__for_each_entry(maps, rb_node) + maps_by_name[i++] =3D rb_node->map; =20 __maps__sort_by_name(maps); =20 @@ -2079,6 +2100,7 @@ static struct map *__maps__find_by_name(struct maps *= maps, const char *name) =20 struct map *maps__find_by_name(struct maps *maps, const char *name) { + struct map_rb_node *rb_node; struct map *map; =20 down_read(&maps->lock); @@ -2097,12 +2119,13 @@ struct map *maps__find_by_name(struct maps *maps, c= onst char *name) goto out_unlock; =20 /* Fallback to traversing the rbtree... */ - maps__for_each_entry(maps, map) + maps__for_each_entry(maps, rb_node) { + map =3D rb_node->map; if (strcmp(map->dso->short_name, name) =3D=3D 0) { maps->last_search_by_name =3D map; goto out_unlock; } - + } map =3D NULL; =20 out_unlock: diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic= -events.c index 6def01036eb5..57b95c1d7e39 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -669,7 +669,7 @@ int perf_event__synthesize_modules(struct perf_tool *to= ol, perf_event__handler_t struct machine *machine) { int rc =3D 0; - struct map *pos; + struct map_rb_node *pos; struct maps *maps =3D machine__kernel_maps(machine); union perf_event *event; size_t size =3D symbol_conf.buildid_mmap2 ? @@ -692,37 +692,39 @@ int perf_event__synthesize_modules(struct perf_tool *= tool, perf_event__handler_t event->header.misc =3D PERF_RECORD_MISC_GUEST_KERNEL; =20 maps__for_each_entry(maps, pos) { - if (!__map__is_kmodule(pos)) + struct map *map =3D pos->map; + + if (!__map__is_kmodule(map)) continue; =20 if (symbol_conf.buildid_mmap2) { - size =3D PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64)); + size =3D PERF_ALIGN(map->dso->long_name_len + 1, sizeof(u64)); event->mmap2.header.type =3D PERF_RECORD_MMAP2; event->mmap2.header.size =3D (sizeof(event->mmap2) - (sizeof(event->mmap2.filename) - size)); memset(event->mmap2.filename + size, 0, machine->id_hdr_size); event->mmap2.header.size +=3D machine->id_hdr_size; - event->mmap2.start =3D pos->start; - event->mmap2.len =3D pos->end - pos->start; + event->mmap2.start =3D map->start; + event->mmap2.len =3D map->end - map->start; event->mmap2.pid =3D machine->pid; =20 - memcpy(event->mmap2.filename, pos->dso->long_name, - pos->dso->long_name_len + 1); + memcpy(event->mmap2.filename, map->dso->long_name, + map->dso->long_name_len + 1); =20 perf_record_mmap2__read_build_id(&event->mmap2, machine, false); } else { - size =3D PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64)); + size =3D PERF_ALIGN(map->dso->long_name_len + 1, sizeof(u64)); event->mmap.header.type =3D PERF_RECORD_MMAP; event->mmap.header.size =3D (sizeof(event->mmap) - (sizeof(event->mmap.filename) - size)); memset(event->mmap.filename + size, 0, machine->id_hdr_size); event->mmap.header.size +=3D machine->id_hdr_size; - event->mmap.start =3D pos->start; - event->mmap.len =3D pos->end - pos->start; + event->mmap.start =3D map->start; + event->mmap.len =3D map->end - map->start; event->mmap.pid =3D machine->pid; =20 - memcpy(event->mmap.filename, pos->dso->long_name, - pos->dso->long_name_len + 1); + memcpy(event->mmap.filename, map->dso->long_name, + map->dso->long_name_len + 1); } =20 if (perf_tool__process_synth_event(tool, event, machine, process) !=3D 0= ) { diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index a2490a20eb56..24e53bd55f7d 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -352,9 +352,7 @@ int thread__insert_map(struct thread *thread, struct ma= p *map) return ret; =20 maps__fixup_overlappings(thread->maps, map, stderr); - maps__insert(thread->maps, map); - - return 0; + return maps__insert(thread->maps, map); } =20 static int __thread__prepare_access(struct thread *thread) @@ -362,12 +360,12 @@ static int __thread__prepare_access(struct thread *th= read) bool initialized =3D false; int err =3D 0; struct maps *maps =3D thread->maps; - struct map *map; + struct map_rb_node *rb_node; =20 down_read(&maps->lock); =20 - maps__for_each_entry(maps, map) { - err =3D unwind__prepare_access(thread->maps, map, &initialized); + maps__for_each_entry(maps, rb_node) { + err =3D unwind__prepare_access(thread->maps, rb_node->map, &initialized); if (err || initialized) break; } diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c index 43beb169631d..835c39efb80d 100644 --- a/tools/perf/util/vdso.c +++ b/tools/perf/util/vdso.c @@ -144,10 +144,11 @@ static enum dso_type machine__thread_dso_type(struct = machine *machine, struct thread *thread) { enum dso_type dso_type =3D DSO__TYPE_UNKNOWN; - struct map *map; + struct map_rb_node *rb_node; + + maps__for_each_entry(thread->maps, rb_node) { + struct dso *dso =3D rb_node->map->dso; =20 - maps__for_each_entry(thread->maps, map) { - struct dso *dso =3D map->dso; if (!dso || dso->long_name[0] !=3D '/') continue; dso_type =3D dso__type(dso, machine); --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 6A009C7618A for ; Mon, 20 Mar 2023 03:40:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229970AbjCTDkl (ORCPT ); Sun, 19 Mar 2023 23:40:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229889AbjCTDkJ (ORCPT ); Sun, 19 Mar 2023 23:40:09 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 083212210B for ; Sun, 19 Mar 2023 20:39:41 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id e129-20020a251e87000000b00b56598237f5so11782665ybe.16 for ; Sun, 19 Mar 2023 20:39:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283580; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dZNNsWcMQ/pjoAD+FijE1w1AA38KDLXls3D7uMYcCjM=; b=DA/taemE6JGccgMaBZc9Fwvl8rvPcIGT7TC5SP4lZQVImbMZ4BWJtiIsren8ki5p9J hDISKl3zo+xLorKCGCS6ZEHzZ0TrQpbES9OrtlkN3BV5ARFIG5cWI/chQd0E9MmOhHge 4zjEqhdGtxdiLh3A4k6z79sR9fvfVRJO1sQfGA11dmMtdcRk0L988axzEOTQyRG2y2nj L5lM8oPLodlVkxhyJMA2fTY2N1cpohT7WVgLWgpdksailASpXQaA8VuDuw+dqecg9Sg0 XdeVuZVA9UhIw2BQ2B9kSC87VHrtnfyY8bDPTLAMXpToeotw1Zo26nKAKhKSJJOBwzVn YHBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283580; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dZNNsWcMQ/pjoAD+FijE1w1AA38KDLXls3D7uMYcCjM=; b=wNU8km8xECsclJH477r4kQIlF9RrgrxSXPucUJbtpiR4h5/ykEK7kwQI9V68lz450J 5ZQfGFAtF62hJ0AiUVP/GCbs/Tb8Lht1/4+FatS/S6zvugPc5flqgK0G//FOUd8CBJ8e oHKA5ihFjbe8M5TzyDvb2s1oDQhLYvNR7H7ZCXYvqOzYqn5oh9pyIQBrUmXtAtLegczj nDZ1bkJfhBPudmWoc5QJj3mmiyss5xFmAPUYLZCgpEHHwmhHnQ0Rwp3hTlfFODUcZ7o8 rO39zA1yMKvgeNWQb//G6TVhwdIxtJll2EGHXSfq4XjgTZFoodcTV7fVdvTCdkd8Y4Jx k2rg== X-Gm-Message-State: AO0yUKUBkDuQGFWMwz2siLzgR/6XSkajy3DebfK9VS8iSoBJw2skop7A xYkrIJOlUzqT3Ke/tXof9A+eYp8vq+BC X-Google-Smtp-Source: AK7set9ued1vxNHe99YKNeAYMqKtrpppKMfIWYpmyYyAipvA6I2DHUu+bAHxdFDtbRKLEx7Le8bzXyoIucUf X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a25:9384:0:b0:b61:48bb:b94c with SMTP id a4-20020a259384000000b00b6148bbb94cmr4586594ybm.6.1679283580250; Sun, 19 Mar 2023 20:39:40 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:56 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-9-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 08/22] perf maps: Add functions to access maps From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce functions to access struct maps. These functions reduce the number of places reference counting is necessary. While tidying APIs do some small const-ification, in particlar to unwind_libunwind_ops. Signed-off-by: Ian Rogers --- .../scripts/python/Perf-Trace-Util/Context.c | 7 +- tools/perf/tests/code-reading.c | 2 +- tools/perf/ui/browsers/hists.c | 3 +- tools/perf/util/callchain.c | 9 +-- tools/perf/util/db-export.c | 12 ++-- tools/perf/util/dlfilter.c | 8 ++- tools/perf/util/event.c | 4 +- tools/perf/util/hist.c | 2 +- tools/perf/util/machine.c | 2 +- tools/perf/util/map.c | 14 ++-- tools/perf/util/maps.c | 71 +++++++++++-------- tools/perf/util/maps.h | 47 +++++++++--- .../scripting-engines/trace-event-python.c | 2 +- tools/perf/util/sort.c | 2 +- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 44 ++++++------ tools/perf/util/thread-stack.c | 4 +- tools/perf/util/thread.c | 4 +- tools/perf/util/unwind-libunwind-local.c | 16 +++-- tools/perf/util/unwind-libunwind.c | 31 ++++---- 20 files changed, 175 insertions(+), 111 deletions(-) diff --git a/tools/perf/scripts/python/Perf-Trace-Util/Context.c b/tools/pe= rf/scripts/python/Perf-Trace-Util/Context.c index b0d449f41650..feedd02b3b3d 100644 --- a/tools/perf/scripts/python/Perf-Trace-Util/Context.c +++ b/tools/perf/scripts/python/Perf-Trace-Util/Context.c @@ -100,10 +100,11 @@ static PyObject *perf_sample_insn(PyObject *obj, PyOb= ject *args) if (!c) return NULL; =20 - if (c->sample->ip && !c->sample->insn_len && - c->al->thread->maps && c->al->thread->maps->machine) - script_fetch_insn(c->sample, c->al->thread, c->al->thread->maps->machine= ); + if (c->sample->ip && !c->sample->insn_len && c->al->thread->maps) { + struct machine *machine =3D maps__machine(c->al->thread->maps); =20 + script_fetch_insn(c->sample, c->al->thread, machine); + } if (!c->sample->insn_len) Py_RETURN_NONE; /* N.B. This is a return statement */ =20 diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-readin= g.c index fb67fd5ebd9f..8d2036f2f944 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -269,7 +269,7 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, len =3D al.map->end - addr; =20 /* Read the object code using perf */ - ret_len =3D dso__data_read_offset(al.map->dso, thread->maps->machine, + ret_len =3D dso__data_read_offset(al.map->dso, maps__machine(thread->maps= ), al.addr, buf1, len); if (ret_len !=3D len) { pr_debug("dso__data_read_offset failed\n"); diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c index b72ee6822222..572ff38ceb0f 100644 --- a/tools/perf/ui/browsers/hists.c +++ b/tools/perf/ui/browsers/hists.c @@ -3139,7 +3139,8 @@ static int evsel__hists_browse(struct evsel *evsel, i= nt nr_events, const char *h continue; case 'k': if (browser->selection !=3D NULL) - hists_browser__zoom_map(browser, browser->selection->maps->machine->vm= linux_map); + hists_browser__zoom_map(browser, + maps__machine(browser->selection->maps)->vmlinux_map); continue; case 'V': verbose =3D (verbose + 1) % 4; diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index a093a15f048f..0aa979f64565 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -1112,6 +1112,8 @@ int hist_entry__append_callchain(struct hist_entry *h= e, struct perf_sample *samp int fill_callchain_info(struct addr_location *al, struct callchain_cursor_= node *node, bool hide_unresolved) { + struct machine *machine =3D maps__machine(node->ms.maps); + al->maps =3D node->ms.maps; al->map =3D node->ms.map; al->sym =3D node->ms.sym; @@ -1124,9 +1126,8 @@ int fill_callchain_info(struct addr_location *al, str= uct callchain_cursor_node * if (al->map =3D=3D NULL) goto out; } - - if (al->maps =3D=3D machine__kernel_maps(al->maps->machine)) { - if (machine__is_host(al->maps->machine)) { + if (al->maps =3D=3D machine__kernel_maps(machine)) { + if (machine__is_host(machine)) { al->cpumode =3D PERF_RECORD_MISC_KERNEL; al->level =3D 'k'; } else { @@ -1134,7 +1135,7 @@ int fill_callchain_info(struct addr_location *al, str= uct callchain_cursor_node * al->level =3D 'g'; } } else { - if (machine__is_host(al->maps->machine)) { + if (machine__is_host(machine)) { al->cpumode =3D PERF_RECORD_MISC_USER; al->level =3D '.'; } else if (perf_guest) { diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c index e0d4f08839fb..1cfcfdd3cf52 100644 --- a/tools/perf/util/db-export.c +++ b/tools/perf/util/db-export.c @@ -181,7 +181,7 @@ static int db_ids_from_al(struct db_export *dbe, struct= addr_location *al, if (al->map) { struct dso *dso =3D al->map->dso; =20 - err =3D db_export__dso(dbe, dso, al->maps->machine); + err =3D db_export__dso(dbe, dso, maps__machine(al->maps)); if (err) return err; *dso_db_id =3D dso->db_id; @@ -354,19 +354,21 @@ int db_export__sample(struct db_export *dbe, union pe= rf_event *event, }; struct thread *main_thread; struct comm *comm =3D NULL; + struct machine *machine; int err; =20 err =3D db_export__evsel(dbe, evsel); if (err) return err; =20 - err =3D db_export__machine(dbe, al->maps->machine); + machine =3D maps__machine(al->maps); + err =3D db_export__machine(dbe, machine); if (err) return err; =20 - main_thread =3D thread__main_thread(al->maps->machine, thread); + main_thread =3D thread__main_thread(machine, thread); =20 - err =3D db_export__threads(dbe, thread, main_thread, al->maps->machine, &= comm); + err =3D db_export__threads(dbe, thread, main_thread, machine, &comm); if (err) goto out_put; =20 @@ -380,7 +382,7 @@ int db_export__sample(struct db_export *dbe, union perf= _event *event, goto out_put; =20 if (dbe->cpr) { - struct call_path *cp =3D call_path_from_sample(dbe, al->maps->machine, + struct call_path *cp =3D call_path_from_sample(dbe, machine, thread, sample, evsel); if (cp) { diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c index 37beb7530288..fe2a0752a0f6 100644 --- a/tools/perf/util/dlfilter.c +++ b/tools/perf/util/dlfilter.c @@ -197,8 +197,12 @@ static const __u8 *dlfilter__insn(void *ctx, __u32 *le= n) if (!al->thread && machine__resolve(d->machine, al, d->sample) < 0) return NULL; =20 - if (al->thread->maps && al->thread->maps->machine) - script_fetch_insn(d->sample, al->thread, al->thread->maps->machine); + if (al->thread->maps) { + struct machine *machine =3D maps__machine(al->thread->maps); + + if (machine) + script_fetch_insn(d->sample, al->thread, machine); + } } =20 if (!d->sample->insn_len) diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 1fa14598b916..f40cdd6ac126 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -572,7 +572,7 @@ struct map *thread__find_map(struct thread *thread, u8 = cpumode, u64 addr, struct addr_location *al) { struct maps *maps =3D thread->maps; - struct machine *machine =3D maps->machine; + struct machine *machine =3D maps__machine(maps); bool load_map =3D false; =20 al->maps =3D maps; @@ -637,7 +637,7 @@ struct map *thread__find_map_fb(struct thread *thread, = u8 cpumode, u64 addr, struct addr_location *al) { struct map *map =3D thread__find_map(thread, cpumode, addr, al); - struct machine *machine =3D thread->maps->machine; + struct machine *machine =3D maps__machine(thread->maps); u8 addr_cpumode =3D machine__addr_cpumode(machine, cpumode, addr); =20 if (map || addr_cpumode =3D=3D cpumode) diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 3670136a0074..1b0e89cd5d99 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -241,7 +241,7 @@ void hists__calc_col_len(struct hists *hists, struct hi= st_entry *h) =20 if (h->cgroup) { const char *cgrp_name =3D "unknown"; - struct cgroup *cgrp =3D cgroup__find(h->ms.maps->machine->env, + struct cgroup *cgrp =3D cgroup__find(maps__machine(h->ms.maps)->env, h->cgroup); if (cgrp !=3D NULL) cgrp_name =3D cgrp->name; diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 93a07079d174..446c0273259d 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -2842,7 +2842,7 @@ static int find_prev_cpumode(struct ip_callchain *cha= in, struct thread *thread, static u64 get_leaf_frame_caller(struct perf_sample *sample, struct thread *thread, int usr_idx) { - if (machine__normalized_is(thread->maps->machine, "arm64")) + if (machine__normalized_is(maps__machine(thread->maps), "arm64")) return get_leaf_frame_caller_aarch64(sample, thread, usr_idx); else return 0; diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 7620cfa114d4..a99dbde656a2 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -234,7 +234,7 @@ bool __map__is_kernel(const struct map *map) { if (!map->dso->kernel) return false; - return machine__kernel_map(map__kmaps((struct map *)map)->machine) =3D=3D= map; + return machine__kernel_map(maps__machine(map__kmaps((struct map *)map))) = =3D=3D map; } =20 bool __map__is_extra_kernel_map(const struct map *map) @@ -475,11 +475,15 @@ u64 map__rip_2objdump(struct map *map, u64 rip) * kcore may not either. However the trampoline object code is on the * main kernel map, so just use that instead. */ - if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps= ->machine) { - struct map *kernel_map =3D machine__kernel_map(kmap->kmaps->machine); + if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps) { + struct machine *machine =3D maps__machine(kmap->kmaps); =20 - if (kernel_map) - map =3D kernel_map; + if (machine) { + struct map *kernel_map =3D machine__kernel_map(machine); + + if (kernel_map) + map =3D kernel_map; + } } =20 if (!map->dso->adjust_symbols) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 83ec126bcbe5..91bb015caede 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -13,7 +13,7 @@ static void maps__init(struct maps *maps, struct machine *machine) { maps->entries =3D RB_ROOT; - init_rwsem(&maps->lock); + init_rwsem(maps__lock(maps)); maps->machine =3D machine; maps->last_search_by_name =3D NULL; maps->nr_maps =3D 0; @@ -32,7 +32,7 @@ static void __maps__free_maps_by_name(struct maps *maps) =20 static int __maps__insert(struct maps *maps, struct map *map) { - struct rb_node **p =3D &maps->entries.rb_node; + struct rb_node **p =3D &maps__entries(maps)->rb_node; struct rb_node *parent =3D NULL; const u64 ip =3D map->start; struct map_rb_node *m, *new_rb_node; @@ -54,7 +54,7 @@ static int __maps__insert(struct maps *maps, struct map *= map) } =20 rb_link_node(&new_rb_node->rb_node, parent, p); - rb_insert_color(&new_rb_node->rb_node, &maps->entries); + rb_insert_color(&new_rb_node->rb_node, maps__entries(maps)); map__get(map); return 0; } @@ -63,7 +63,7 @@ int maps__insert(struct maps *maps, struct map *map) { int err; =20 - down_write(&maps->lock); + down_write(maps__lock(maps)); err =3D __maps__insert(maps, map); if (err) goto out; @@ -84,10 +84,11 @@ int maps__insert(struct maps *maps, struct map *map) * If we already performed some search by name, then we need to add the j= ust * inserted map and resort. */ - if (maps->maps_by_name) { - if (maps->nr_maps > maps->nr_maps_allocated) { - int nr_allocate =3D maps->nr_maps * 2; - struct map **maps_by_name =3D realloc(maps->maps_by_name, nr_allocate *= sizeof(map)); + if (maps__maps_by_name(maps)) { + if (maps__nr_maps(maps) > maps->nr_maps_allocated) { + int nr_allocate =3D maps__nr_maps(maps) * 2; + struct map **maps_by_name =3D realloc(maps__maps_by_name(maps), + nr_allocate * sizeof(map)); =20 if (maps_by_name =3D=3D NULL) { __maps__free_maps_by_name(maps); @@ -97,18 +98,18 @@ int maps__insert(struct maps *maps, struct map *map) =20 maps->maps_by_name =3D maps_by_name; maps->nr_maps_allocated =3D nr_allocate; -} - maps->maps_by_name[maps->nr_maps - 1] =3D map; + } + maps__maps_by_name(maps)[maps__nr_maps(maps) - 1] =3D map; __maps__sort_by_name(maps); } out: - up_write(&maps->lock); + up_write(maps__lock(maps)); return err; } =20 static void __maps__remove(struct maps *maps, struct map_rb_node *rb_node) { - rb_erase_init(&rb_node->rb_node, &maps->entries); + rb_erase_init(&rb_node->rb_node, maps__entries(maps)); map__put(rb_node->map); free(rb_node); } @@ -117,7 +118,7 @@ void maps__remove(struct maps *maps, struct map *map) { struct map_rb_node *rb_node; =20 - down_write(&maps->lock); + down_write(maps__lock(maps)); if (maps->last_search_by_name =3D=3D map) maps->last_search_by_name =3D NULL; =20 @@ -125,9 +126,9 @@ void maps__remove(struct maps *maps, struct map *map) assert(rb_node->map =3D=3D map); __maps__remove(maps, rb_node); --maps->nr_maps; - if (maps->maps_by_name) + if (maps__maps_by_name(maps)) __maps__free_maps_by_name(maps); - up_write(&maps->lock); + up_write(maps__lock(maps)); } =20 static void __maps__purge(struct maps *maps) @@ -135,7 +136,7 @@ static void __maps__purge(struct maps *maps) struct map_rb_node *pos, *next; =20 maps__for_each_entry_safe(maps, pos, next) { - rb_erase_init(&pos->rb_node, &maps->entries); + rb_erase_init(&pos->rb_node, maps__entries(maps)); map__put(pos->map); free(pos); } @@ -143,9 +144,9 @@ static void __maps__purge(struct maps *maps) =20 static void maps__exit(struct maps *maps) { - down_write(&maps->lock); + down_write(maps__lock(maps)); __maps__purge(maps); - up_write(&maps->lock); + up_write(maps__lock(maps)); } =20 bool maps__empty(struct maps *maps) @@ -170,6 +171,14 @@ void maps__delete(struct maps *maps) free(maps); } =20 +struct maps *maps__get(struct maps *maps) +{ + if (maps) + refcount_inc(&maps->refcnt); + + return maps; +} + void maps__put(struct maps *maps) { if (maps && refcount_dec_and_test(&maps->refcnt)) @@ -195,7 +204,7 @@ struct symbol *maps__find_symbol_by_name(struct maps *m= aps, const char *name, st struct symbol *sym; struct map_rb_node *pos; =20 - down_read(&maps->lock); + down_read(maps__lock(maps)); =20 maps__for_each_entry(maps, pos) { sym =3D map__find_symbol_by_name(pos->map, name); @@ -213,7 +222,7 @@ struct symbol *maps__find_symbol_by_name(struct maps *m= aps, const char *name, st =20 sym =3D NULL; out: - up_read(&maps->lock); + up_read(maps__lock(maps)); return sym; } =20 @@ -238,7 +247,7 @@ size_t maps__fprintf(struct maps *maps, FILE *fp) size_t printed =3D 0; struct map_rb_node *pos; =20 - down_read(&maps->lock); + down_read(maps__lock(maps)); =20 maps__for_each_entry(maps, pos) { printed +=3D fprintf(fp, "Map:"); @@ -249,7 +258,7 @@ size_t maps__fprintf(struct maps *maps, FILE *fp) } } =20 - up_read(&maps->lock); + up_read(maps__lock(maps)); =20 return printed; } @@ -260,9 +269,9 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) struct rb_node *next, *first; int err =3D 0; =20 - down_write(&maps->lock); + down_write(maps__lock(maps)); =20 - root =3D &maps->entries; + root =3D maps__entries(maps); =20 /* * Find first map where end > map->start. @@ -358,7 +367,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) =20 err =3D 0; out: - up_write(&maps->lock); + up_write(maps__lock(maps)); return err; } =20 @@ -371,7 +380,7 @@ int maps__clone(struct thread *thread, struct maps *par= ent) int err; struct map_rb_node *rb_node; =20 - down_read(&parent->lock); + down_read(maps__lock(parent)); =20 maps__for_each_entry(parent, rb_node) { struct map *new =3D map__clone(rb_node->map); @@ -394,7 +403,7 @@ int maps__clone(struct thread *thread, struct maps *par= ent) =20 err =3D 0; out_unlock: - up_read(&parent->lock); + up_read(maps__lock(parent)); return err; } =20 @@ -415,9 +424,9 @@ struct map *maps__find(struct maps *maps, u64 ip) struct map_rb_node *m; =20 =20 - down_read(&maps->lock); + down_read(maps__lock(maps)); =20 - p =3D maps->entries.rb_node; + p =3D maps__entries(maps)->rb_node; while (p !=3D NULL) { m =3D rb_entry(p, struct map_rb_node, rb_node); if (ip < m->map->start) @@ -430,13 +439,13 @@ struct map *maps__find(struct maps *maps, u64 ip) =20 m =3D NULL; out: - up_read(&maps->lock); + up_read(maps__lock(maps)); return m ? m->map : NULL; } =20 struct map_rb_node *maps__first(struct maps *maps) { - struct rb_node *first =3D rb_first(&maps->entries); + struct rb_node *first =3D rb_first(maps__entries(maps)); =20 if (first) return rb_entry(first, struct map_rb_node, rb_node); diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 512746ec0f9a..bde3390c7096 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -43,7 +43,7 @@ struct maps { unsigned int nr_maps_allocated; #ifdef HAVE_LIBUNWIND_SUPPORT void *addr_space; - struct unwind_libunwind_ops *unwind_libunwind_ops; + const struct unwind_libunwind_ops *unwind_libunwind_ops; #endif }; =20 @@ -58,20 +58,51 @@ struct kmap { struct maps *maps__new(struct machine *machine); void maps__delete(struct maps *maps); bool maps__empty(struct maps *maps); +int maps__clone(struct thread *thread, struct maps *parent); + +struct maps *maps__get(struct maps *maps); +void maps__put(struct maps *maps); =20 -static inline struct maps *maps__get(struct maps *maps) +static inline struct rb_root *maps__entries(struct maps *maps) { - if (maps) - refcount_inc(&maps->refcnt); - return maps; + return &maps->entries; } =20 -void maps__put(struct maps *maps); -int maps__clone(struct thread *thread, struct maps *parent); +static inline struct machine *maps__machine(struct maps *maps) +{ + return maps->machine; +} + +static inline struct rw_semaphore *maps__lock(struct maps *maps) +{ + return &maps->lock; +} + +static inline struct map **maps__maps_by_name(struct maps *maps) +{ + return maps->maps_by_name; +} + +static inline unsigned int maps__nr_maps(const struct maps *maps) +{ + return maps->nr_maps; +} + +#ifdef HAVE_LIBUNWIND_SUPPORT +static inline void *maps__addr_space(struct maps *maps) +{ + return maps->addr_space; +} + +static inline const struct unwind_libunwind_ops *maps__unwind_libunwind_op= s(const struct maps *maps) +{ + return maps->unwind_libunwind_ops; +} +#endif + size_t maps__fprintf(struct maps *maps, FILE *fp); =20 int maps__insert(struct maps *maps, struct map *map); - void maps__remove(struct maps *maps, struct map *map); =20 struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map *= *mapp); diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index 0f4ef61f2ffa..e5cc18f6fcda 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -1288,7 +1288,7 @@ static void python_export_sample_table(struct db_expo= rt *dbe, =20 tuple_set_d64(t, 0, es->db_id); tuple_set_d64(t, 1, es->evsel->db_id); - tuple_set_d64(t, 2, es->al->maps->machine->db_id); + tuple_set_d64(t, 2, maps__machine(es->al->maps)->db_id); tuple_set_d64(t, 3, es->al->thread->db_id); tuple_set_d64(t, 4, es->comm_db_id); tuple_set_d64(t, 5, es->dso_db_id); diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index 093a0c8b2e3d..e04d9bddba11 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c @@ -762,7 +762,7 @@ static int hist_entry__cgroup_snprintf(struct hist_entr= y *he, const char *cgrp_name =3D "N/A"; =20 if (he->cgroup) { - struct cgroup *cgrp =3D cgroup__find(he->ms.maps->machine->env, + struct cgroup *cgrp =3D cgroup__find(maps__machine(he->ms.maps)->env, he->cgroup); if (cgrp !=3D NULL) cgrp_name =3D cgrp->name; diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 325fbeea8dff..ccdafc3971ac 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1422,7 +1422,7 @@ static int dso__process_kernel_symbol(struct dso *dso= , struct map *map, * we still are sure to have a reference to this DSO via * *curr_map->dso. */ - dsos__add(&kmaps->machine->dsos, curr_dso); + dsos__add(&maps__machine(kmaps)->dsos, curr_dso); /* kmaps already got it */ map__put(curr_map); dso__set_loaded(curr_dso); diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index cc23d111e2d3..56b3775473d4 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -270,7 +270,7 @@ void maps__fixup_end(struct maps *maps) { struct map_rb_node *prev =3D NULL, *curr; =20 - down_write(&maps->lock); + down_write(maps__lock(maps)); =20 maps__for_each_entry(maps, curr) { if (prev !=3D NULL && !prev->map->end) @@ -286,7 +286,7 @@ void maps__fixup_end(struct maps *maps) if (curr && !curr->map->end) curr->map->end =3D ~0ULL; =20 - up_write(&maps->lock); + up_write(maps__lock(maps)); } =20 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const = char *name) @@ -839,7 +839,7 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, if (!kmaps) return -1; =20 - machine =3D kmaps->machine; + machine =3D maps__machine(kmaps); =20 x86_64 =3D machine__is(machine, "x86_64"); =20 @@ -963,7 +963,7 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, =20 if (curr_map !=3D initial_map && dso->kernel =3D=3D DSO_SPACE__KERNEL_GUEST && - machine__is_default_guest(kmaps->machine)) { + machine__is_default_guest(maps__machine(kmaps))) { dso__set_loaded(curr_map->dso); } =20 @@ -1362,7 +1362,7 @@ static int dso__load_kcore(struct dso *dso, struct ma= p *map, if (!kmaps) return -EINVAL; =20 - machine =3D kmaps->machine; + machine =3D maps__machine(kmaps); =20 /* This function requires that the map is the kernel map */ if (!__map__is_kernel(map)) @@ -1889,7 +1889,7 @@ int dso__load(struct dso *dso, struct map *map) else if (dso->kernel =3D=3D DSO_SPACE__KERNEL_GUEST) ret =3D dso__load_guest_kernel_sym(dso, map); =20 - machine =3D map__kmaps(map)->machine; + machine =3D maps__machine(map__kmaps(map)); if (machine__is(machine, "x86_64")) machine__map_x86_64_entry_trampolines(machine, dso); goto out; @@ -2054,32 +2054,32 @@ static int map__strcmp_name(const void *name, const= void *b) =20 void __maps__sort_by_name(struct maps *maps) { - qsort(maps->maps_by_name, maps->nr_maps, sizeof(struct map *), map__strcm= p); + qsort(maps__maps_by_name(maps), maps__nr_maps(maps), sizeof(struct map *)= , map__strcmp); } =20 static int map__groups__sort_by_name_from_rbtree(struct maps *maps) { struct map_rb_node *rb_node; - struct map **maps_by_name =3D realloc(maps->maps_by_name, - maps->nr_maps * sizeof(struct map *)); + struct map **maps_by_name =3D realloc(maps__maps_by_name(maps), + maps__nr_maps(maps) * sizeof(struct map *)); int i =3D 0; =20 if (maps_by_name =3D=3D NULL) return -1; =20 - up_read(&maps->lock); - down_write(&maps->lock); + up_read(maps__lock(maps)); + down_write(maps__lock(maps)); =20 maps->maps_by_name =3D maps_by_name; - maps->nr_maps_allocated =3D maps->nr_maps; + maps->nr_maps_allocated =3D maps__nr_maps(maps); =20 maps__for_each_entry(maps, rb_node) maps_by_name[i++] =3D rb_node->map; =20 __maps__sort_by_name(maps); =20 - up_write(&maps->lock); - down_read(&maps->lock); + up_write(maps__lock(maps)); + down_read(maps__lock(maps)); =20 return 0; } @@ -2088,11 +2088,12 @@ static struct map *__maps__find_by_name(struct maps= *maps, const char *name) { struct map **mapp; =20 - if (maps->maps_by_name =3D=3D NULL && + if (maps__maps_by_name(maps) =3D=3D NULL && map__groups__sort_by_name_from_rbtree(maps)) return NULL; =20 - mapp =3D bsearch(name, maps->maps_by_name, maps->nr_maps, sizeof(*mapp), = map__strcmp_name); + mapp =3D bsearch(name, maps__maps_by_name(maps), maps__nr_maps(maps), + sizeof(*mapp), map__strcmp_name); if (mapp) return *mapp; return NULL; @@ -2103,9 +2104,10 @@ struct map *maps__find_by_name(struct maps *maps, co= nst char *name) struct map_rb_node *rb_node; struct map *map; =20 - down_read(&maps->lock); + down_read(maps__lock(maps)); =20 - if (maps->last_search_by_name && strcmp(maps->last_search_by_name->dso->s= hort_name, name) =3D=3D 0) { + if (maps->last_search_by_name && + strcmp(maps->last_search_by_name->dso->short_name, name) =3D=3D 0) { map =3D maps->last_search_by_name; goto out_unlock; } @@ -2115,7 +2117,7 @@ struct map *maps__find_by_name(struct maps *maps, con= st char *name) * made. */ map =3D __maps__find_by_name(maps, name); - if (map || maps->maps_by_name !=3D NULL) + if (map || maps__maps_by_name(maps) !=3D NULL) goto out_unlock; =20 /* Fallback to traversing the rbtree... */ @@ -2129,7 +2131,7 @@ struct map *maps__find_by_name(struct maps *maps, con= st char *name) map =3D NULL; =20 out_unlock: - up_read(&maps->lock); + up_read(maps__lock(maps)); return map; } =20 @@ -2381,7 +2383,7 @@ static int dso__load_guest_kernel_sym(struct dso *dso= , struct map *map) { int err; const char *kallsyms_filename; - struct machine *machine =3D map__kmaps(map)->machine; + struct machine *machine =3D maps__machine(map__kmaps(map)); char path[PATH_MAX]; =20 if (machine->kallsyms_filename) { diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c index 1b992bbba4e8..4b85c1728012 100644 --- a/tools/perf/util/thread-stack.c +++ b/tools/perf/util/thread-stack.c @@ -155,8 +155,8 @@ static int thread_stack__init(struct thread_stack *ts, = struct thread *thread, ts->br_stack_sz =3D br_stack_sz; } =20 - if (thread->maps && thread->maps->machine) { - struct machine *machine =3D thread->maps->machine; + if (thread->maps && maps__machine(thread->maps)) { + struct machine *machine =3D maps__machine(thread->maps); const char *arch =3D perf_env__arch(machine->env); =20 ts->kernel_start =3D machine__kernel_start(machine); diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 24e53bd55f7d..292585a52281 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -362,7 +362,7 @@ static int __thread__prepare_access(struct thread *thre= ad) struct maps *maps =3D thread->maps; struct map_rb_node *rb_node; =20 - down_read(&maps->lock); + down_read(maps__lock(maps)); =20 maps__for_each_entry(maps, rb_node) { err =3D unwind__prepare_access(thread->maps, rb_node->map, &initialized); @@ -370,7 +370,7 @@ static int __thread__prepare_access(struct thread *thre= ad) break; } =20 - up_read(&maps->lock); + up_read(maps__lock(maps)); =20 return err; } diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unw= ind-libunwind-local.c index 81b6bd6e1536..952c5ee66fe7 100644 --- a/tools/perf/util/unwind-libunwind-local.c +++ b/tools/perf/util/unwind-libunwind-local.c @@ -665,24 +665,26 @@ static unw_accessors_t accessors =3D { =20 static int _unwind__prepare_access(struct maps *maps) { - maps->addr_space =3D unw_create_addr_space(&accessors, 0); - if (!maps->addr_space) { + void *addr_space =3D unw_create_addr_space(&accessors, 0); + + maps->addr_space =3D addr_space; + if (!addr_space) { pr_err("unwind: Can't create unwind address space.\n"); return -ENOMEM; } =20 - unw_set_caching_policy(maps->addr_space, UNW_CACHE_GLOBAL); + unw_set_caching_policy(addr_space, UNW_CACHE_GLOBAL); return 0; } =20 static void _unwind__flush_access(struct maps *maps) { - unw_flush_cache(maps->addr_space, 0, 0); + unw_flush_cache(maps__addr_space(maps), 0, 0); } =20 static void _unwind__finish_access(struct maps *maps) { - unw_destroy_addr_space(maps->addr_space); + unw_destroy_addr_space(maps__addr_space(maps)); } =20 static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb, @@ -707,7 +709,7 @@ static int get_entries(struct unwind_info *ui, unwind_e= ntry_cb_t cb, */ if (max_stack - 1 > 0) { WARN_ONCE(!ui->thread, "WARNING: ui->thread is NULL"); - addr_space =3D ui->thread->maps->addr_space; + addr_space =3D maps__addr_space(ui->thread->maps); =20 if (addr_space =3D=3D NULL) return -1; @@ -757,7 +759,7 @@ static int _unwind__get_entries(unwind_entry_cb_t cb, v= oid *arg, struct unwind_info ui =3D { .sample =3D data, .thread =3D thread, - .machine =3D thread->maps->machine, + .machine =3D maps__machine(thread->maps), .best_effort =3D best_effort }; =20 diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-li= bunwind.c index 509c287ee762..c14f04082377 100644 --- a/tools/perf/util/unwind-libunwind.c +++ b/tools/perf/util/unwind-libunwind.c @@ -22,12 +22,13 @@ int unwind__prepare_access(struct maps *maps, struct ma= p *map, bool *initialized const char *arch; enum dso_type dso_type; struct unwind_libunwind_ops *ops =3D local_unwind_libunwind_ops; + struct machine *machine; int err; =20 if (!dwarf_callchain_users) return 0; =20 - if (maps->addr_space) { + if (maps__addr_space(maps)) { pr_debug("unwind: thread map already set, dso=3D%s\n", map->dso->name); if (initialized) @@ -35,15 +36,16 @@ int unwind__prepare_access(struct maps *maps, struct ma= p *map, bool *initialized return 0; } =20 + machine =3D maps__machine(maps); /* env->arch is NULL for live-mode (i.e. perf top) */ - if (!maps->machine->env || !maps->machine->env->arch) + if (!machine->env || !machine->env->arch) goto out_register; =20 - dso_type =3D dso__type(map->dso, maps->machine); + dso_type =3D dso__type(map->dso, machine); if (dso_type =3D=3D DSO__TYPE_UNKNOWN) return 0; =20 - arch =3D perf_env__arch(maps->machine->env); + arch =3D perf_env__arch(machine->env); =20 if (!strcmp(arch, "x86")) { if (dso_type !=3D DSO__TYPE_64BIT) @@ -60,7 +62,7 @@ int unwind__prepare_access(struct maps *maps, struct map = *map, bool *initialized out_register: unwind__register_ops(maps, ops); =20 - err =3D maps->unwind_libunwind_ops->prepare_access(maps); + err =3D maps__unwind_libunwind_ops(maps)->prepare_access(maps); if (initialized) *initialized =3D err ? false : true; return err; @@ -68,14 +70,18 @@ int unwind__prepare_access(struct maps *maps, struct ma= p *map, bool *initialized =20 void unwind__flush_access(struct maps *maps) { - if (maps->unwind_libunwind_ops) - maps->unwind_libunwind_ops->flush_access(maps); + const struct unwind_libunwind_ops *ops =3D maps__unwind_libunwind_ops(map= s); + + if (ops) + ops->flush_access(maps); } =20 void unwind__finish_access(struct maps *maps) { - if (maps->unwind_libunwind_ops) - maps->unwind_libunwind_ops->finish_access(maps); + const struct unwind_libunwind_ops *ops =3D maps__unwind_libunwind_ops(map= s); + + if (ops) + ops->finish_access(maps); } =20 int unwind__get_entries(unwind_entry_cb_t cb, void *arg, @@ -83,8 +89,9 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg, struct perf_sample *data, int max_stack, bool best_effort) { - if (thread->maps->unwind_libunwind_ops) - return thread->maps->unwind_libunwind_ops->get_entries(cb, arg, thread, = data, - max_stack, best_effort); + const struct unwind_libunwind_ops *ops =3D maps__unwind_libunwind_ops(thr= ead->maps); + + if (ops) + return ops->get_entries(cb, arg, thread, data, max_stack); return 0; } --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 B9450C7618A for ; Mon, 20 Mar 2023 03:41:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230001AbjCTDlI (ORCPT ); Sun, 19 Mar 2023 23:41:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229997AbjCTDkS (ORCPT ); Sun, 19 Mar 2023 23:40:18 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A0FE22A12 for ; Sun, 19 Mar 2023 20:39:49 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id g5-20020a25a485000000b009419f64f6afso11775176ybi.2 for ; Sun, 19 Mar 2023 20:39:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283588; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WN6dXrTt7pqw2j1pzzhUrICkFQaPFA1pRnk3e8+XKSM=; b=ZJ+jg+3HYln5+NTdAnfQiTBZxsumu5+NNScoJA+Yf/R6zd+CS3L5T+9B5AVWg2OftS PTYDXWJ7zjAEU2s3GiKWTyDkr5m50R6B9U7DnYxH1/LVuoNGkSdm87D2ioXFjftJSa87 x+P0Vvk7fDFm2eYBya4nHetBCwlyNcXhESvgQDJRtWo7Jy8w0JY4vxlpdUOH+kO6rUwf N7fykSxpWjiwCcOgQrWFVmc8W3x8+FIdwXNOgswEEBzMHC/PPTlVTHgDsbyuxFTRFtwL zwyLpLvPcNFW/+OsSTRm8ITwtP9fwenZ2D8Lc+j084X4a1vtFYT8PBZprT4pPgTw4T3H MWOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283588; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WN6dXrTt7pqw2j1pzzhUrICkFQaPFA1pRnk3e8+XKSM=; b=LlPHqUNACRda3ztMG+iisHHSpZijzAMy9EPdOTrW2ySLlKjYwjGg+X/I0g134mPBDR 2DnNpfTvdxjKG+SxG/sA3B8FO84DFds49dP36FLwPlIjZvZxBx+aecvktUYYKtqbpgt5 +2qPk5fW+p+hlicCjzW9avH4ANgGVMs+qcX0PYYIulBvRak6HHEzGYA0o8AgVu5tUewI oefiodV2iHCVOh7E0q6e3gmyJJywZkRWBQ18uBH+cF7xo8cpPVscCGb5LG+OQ/Efg+Dv 8vtLx4gzWDf4ss4Z9PcK26InQH/tUVvZ8VVb36VAEtSg141n1L3FSvrzKkyqq2aYf4up HD+g== X-Gm-Message-State: AO0yUKUqgTcW22fJhLvvD0dPBF8PoWDFF6YY2H4aX3PjMThASJoeY/3j CenghLCmWJyrZ0+JbAsUJ3lVC5vHAKyq X-Google-Smtp-Source: AK7set8nVRdymlXOZhFXeDEFkt6rl4GjNWmd8HI80b2A6oDxZ8CfyLyD8xv1QItLWPiMJzSe4qTc8zg0N6Z1 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:1002:b0:b47:9e93:47a0 with SMTP id w2-20020a056902100200b00b479e9347a0mr4586044ybt.6.1679283588324; Sun, 19 Mar 2023 20:39:48 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:57 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-10-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 09/22] perf map: Add accessor for dso From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Later changes will add reference count checking for struct map, with dso being the most frequently accessed variable. Add an accessor so that the reference count check is only necessary in one place. Additional changes: - add a dso variable to avoid repeated map__dso calls. - in builtin-mem.c dump_raw_samples, code only partially tested for dso =3D=3D NULL. Make the possibility of NULL consistent. - in thread.c thread__memcpy fix use of spaces and use tabs. Signed-off-by: Ian Rogers --- tools/perf/builtin-annotate.c | 11 ++- tools/perf/builtin-buildid-list.c | 2 +- tools/perf/builtin-inject.c | 8 +- tools/perf/builtin-kallsyms.c | 4 +- tools/perf/builtin-mem.c | 10 +- tools/perf/builtin-report.c | 7 +- tools/perf/builtin-script.c | 19 ++-- tools/perf/builtin-top.c | 11 ++- tools/perf/builtin-trace.c | 2 +- .../scripts/python/Perf-Trace-Util/Context.c | 6 +- tools/perf/tests/code-reading.c | 28 +++--- tools/perf/tests/hists_common.c | 8 +- tools/perf/tests/hists_cumulate.c | 4 +- tools/perf/tests/hists_filter.c | 4 +- tools/perf/tests/hists_output.c | 2 +- tools/perf/tests/maps.c | 2 +- tools/perf/tests/symbols.c | 6 +- tools/perf/tests/vmlinux-kallsyms.c | 13 ++- tools/perf/ui/browsers/annotate.c | 9 +- tools/perf/ui/browsers/hists.c | 16 ++-- tools/perf/ui/browsers/map.c | 4 +- tools/perf/util/annotate.c | 16 ++-- tools/perf/util/auxtrace.c | 2 +- tools/perf/util/block-info.c | 4 +- tools/perf/util/bpf-event.c | 10 +- tools/perf/util/build-id.c | 2 +- tools/perf/util/callchain.c | 6 +- tools/perf/util/data-convert-json.c | 10 +- tools/perf/util/db-export.c | 4 +- tools/perf/util/dlfilter.c | 10 +- tools/perf/util/event.c | 9 +- tools/perf/util/evsel_fprintf.c | 2 +- tools/perf/util/hist.c | 10 +- tools/perf/util/intel-pt.c | 45 +++++---- tools/perf/util/machine.c | 70 ++++++++------ tools/perf/util/map.c | 96 ++++++++++++------- tools/perf/util/map.h | 7 +- tools/perf/util/maps.c | 7 +- tools/perf/util/probe-event.c | 30 +++--- .../util/scripting-engines/trace-event-perl.c | 10 +- .../scripting-engines/trace-event-python.c | 16 ++-- tools/perf/util/sort.c | 49 +++++----- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 55 +++++++---- tools/perf/util/synthetic-events.c | 12 +-- tools/perf/util/thread.c | 25 +++-- tools/perf/util/unwind-libdw.c | 10 +- tools/perf/util/vdso.c | 2 +- 48 files changed, 404 insertions(+), 293 deletions(-) diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c index 4750fac7bf93..9e220159e320 100644 --- a/tools/perf/builtin-annotate.c +++ b/tools/perf/builtin-annotate.c @@ -205,7 +205,7 @@ static int process_branch_callback(struct evsel *evsel, return 0; =20 if (a.map !=3D NULL) - a.map->dso->hit =3D 1; + map__dso(a.map)->hit =3D 1; =20 hist__account_cycles(sample->branch_stack, al, sample, false, NULL); =20 @@ -235,10 +235,11 @@ static int evsel__add_sample(struct evsel *evsel, str= uct perf_sample *sample, * the DSO? */ if (al->sym !=3D NULL) { - rb_erase_cached(&al->sym->rb_node, - &al->map->dso->symbols); + struct dso *dso =3D map__dso(al->map); + + rb_erase_cached(&al->sym->rb_node, &dso->symbols); symbol__delete(al->sym); - dso__reset_find_symbol_cache(al->map->dso); + dso__reset_find_symbol_cache(dso); } return 0; } @@ -320,7 +321,7 @@ static void hists__find_annotations(struct hists *hists, struct hist_entry *he =3D rb_entry(nd, struct hist_entry, rb_node); struct annotation *notes; =20 - if (he->ms.sym =3D=3D NULL || he->ms.map->dso->annotate_warned) + if (he->ms.sym =3D=3D NULL || map__dso(he->ms.map)->annotate_warned) goto find_next; =20 if (ann->sym_hist_filter && diff --git a/tools/perf/builtin-buildid-list.c b/tools/perf/builtin-buildid= -list.c index 00bfe89f0b5d..cad9ed44ce7c 100644 --- a/tools/perf/builtin-buildid-list.c +++ b/tools/perf/builtin-buildid-list.c @@ -24,7 +24,7 @@ =20 static int buildid__map_cb(struct map *map, void *arg __maybe_unused) { - const struct dso *dso =3D map->dso; + const struct dso *dso =3D map__dso(map); char bid_buf[SBUILD_ID_SIZE]; =20 memset(bid_buf, 0, sizeof(bid_buf)); diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 10bb1d494258..8f6909dd8a54 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -753,9 +753,11 @@ int perf_event__inject_buildid(struct perf_tool *tool,= union perf_event *event, } =20 if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) { - if (!al.map->dso->hit) { - al.map->dso->hit =3D 1; - dso__inject_build_id(al.map->dso, tool, machine, + struct dso *dso =3D map__dso(al.map); + + if (!dso->hit) { + dso->hit =3D 1; + dso__inject_build_id(dso, tool, machine, sample->cpumode, al.map->flags); } } diff --git a/tools/perf/builtin-kallsyms.c b/tools/perf/builtin-kallsyms.c index c08ee81529e8..5638ca4dbd8e 100644 --- a/tools/perf/builtin-kallsyms.c +++ b/tools/perf/builtin-kallsyms.c @@ -28,6 +28,7 @@ static int __cmd_kallsyms(int argc, const char **argv) =20 for (i =3D 0; i < argc; ++i) { struct map *map; + const struct dso *dso; struct symbol *symbol =3D machine__find_kernel_symbol_by_name(machine, a= rgv[i], &map); =20 if (symbol =3D=3D NULL) { @@ -35,8 +36,9 @@ static int __cmd_kallsyms(int argc, const char **argv) continue; } =20 + dso =3D map__dso(map); printf("%s: %s %s %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n= ", - symbol->name, map->dso->short_name, map->dso->long_name, + symbol->name, dso->short_name, dso->long_name, map->unmap_ip(map, symbol->start), map->unmap_ip(map, symbol->end), symbol->start, symbol->end); } diff --git a/tools/perf/builtin-mem.c b/tools/perf/builtin-mem.c index dedd612eae5e..1e27188b0de1 100644 --- a/tools/perf/builtin-mem.c +++ b/tools/perf/builtin-mem.c @@ -200,6 +200,7 @@ dump_raw_samples(struct perf_tool *tool, struct addr_location al; const char *fmt, *field_sep; char str[PAGE_SIZE_NAME_LEN]; + struct dso *dso =3D NULL; =20 if (machine__resolve(machine, &al, sample) < 0) { fprintf(stderr, "problem processing %d event, skipping it.\n", @@ -210,8 +211,11 @@ dump_raw_samples(struct perf_tool *tool, if (al.filtered || (mem->hide_unresolved && al.sym =3D=3D NULL)) goto out_put; =20 - if (al.map !=3D NULL) - al.map->dso->hit =3D 1; + if (al.map !=3D NULL) { + dso =3D map__dso(al.map); + if (dso) + dso->hit =3D 1; + } =20 field_sep =3D symbol_conf.field_sep; if (field_sep) { @@ -252,7 +256,7 @@ dump_raw_samples(struct perf_tool *tool, symbol_conf.field_sep, sample->data_src, symbol_conf.field_sep, - al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???", + dso ? dso->long_name : "???", al.sym ? al.sym->name : "???"); out_put: addr_location__put(&al); diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index c453b7fa7418..02ca87c13e91 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -314,7 +314,7 @@ static int process_sample_event(struct perf_tool *tool, } =20 if (al.map !=3D NULL) - al.map->dso->hit =3D 1; + map__dso(al.map)->hit =3D 1; =20 if (ui__has_annotation() || rep->symbol_ipc || rep->total_cycles_mode) { hist__account_cycles(sample->branch_stack, &al, sample, @@ -603,7 +603,7 @@ static void report__warn_kptr_restrict(const struct rep= ort *rep) return; =20 if (kernel_map =3D=3D NULL || - (kernel_map->dso->hit && + (map__dso(kernel_map)->hit && (kernel_kmap->ref_reloc_sym =3D=3D NULL || kernel_kmap->ref_reloc_sym->addr =3D=3D 0))) { const char *desc =3D @@ -844,6 +844,7 @@ static size_t maps__fprintf_task(struct maps *maps, int= indent, FILE *fp) =20 maps__for_each_entry(maps, rb_node) { struct map *map =3D rb_node->map; + const struct dso *dso =3D map__dso(map); =20 printed +=3D fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRI= x64 " %" PRIu64 " %s\n", indent, "", map->start, map->end, @@ -852,7 +853,7 @@ static size_t maps__fprintf_task(struct maps *maps, int= indent, FILE *fp) map->prot & PROT_EXEC ? 'x' : '-', map->flags & MAP_SHARED ? 's' : 'p', map->pgoff, - map->dso->id.ino, map->dso->name); + dso->id.ino, dso->name); } =20 return printed; diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 976f8bfe099c..9c7eb900ff7c 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -1011,11 +1011,11 @@ static int perf_sample__fprintf_brstackoff(struct p= erf_sample *sample, to =3D entries[i].to; =20 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) && - !alf.map->dso->adjust_symbols) + !map__dso(alf.map)->adjust_symbols) from =3D map__map_ip(alf.map, from); =20 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) && - !alt.map->dso->adjust_symbols) + !map__dso(alt.map)->adjust_symbols) to =3D map__map_ip(alt.map, to); =20 printed +=3D fprintf(fp, " 0x%"PRIx64, from); @@ -1044,6 +1044,7 @@ static int grab_bb(u8 *buffer, u64 start, u64 end, long offset, len; struct addr_location al; bool kernel; + struct dso *dso; =20 if (!start || !end) return 0; @@ -1074,11 +1075,12 @@ static int grab_bb(u8 *buffer, u64 start, u64 end, return 0; } =20 - if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) { + dso =3D map__dso(al.map); + if (!thread__find_map(thread, *cpumode, start, &al) || !dso) { pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end); return 0; } - if (al.map->dso->data.status =3D=3D DSO_DATA_STATUS_ERROR) { + if (dso->data.status =3D=3D DSO_DATA_STATUS_ERROR) { pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end); return 0; } @@ -1087,10 +1089,10 @@ static int grab_bb(u8 *buffer, u64 start, u64 end, map__load(al.map); =20 offset =3D al.map->map_ip(al.map, start); - len =3D dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer, + len =3D dso__data_read_offset(dso, machine, offset, (u8 *)buffer, end - start + MAXINSN); =20 - *is64bit =3D al.map->dso->is_64_bit; + *is64bit =3D dso->is_64_bit; if (len <=3D 0) pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n", start, end); @@ -1104,10 +1106,11 @@ static int map__fprintf_srccode(struct map *map, u6= 4 addr, FILE *fp, struct srcc unsigned line; int len; char *srccode; + struct dso *dso =3D map__dso(map); =20 - if (!map || !map->dso) + if (!map || !dso) return 0; - srcfile =3D get_srcline_split(map->dso, + srcfile =3D get_srcline_split(dso, map__rip_2objdump(map, addr), &line); if (!srcfile) diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index d4b5b02bab73..5010eee8fbae 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -114,6 +114,7 @@ static int perf_top__parse_source(struct perf_top *top,= struct hist_entry *he) struct symbol *sym; struct annotation *notes; struct map *map; + struct dso *dso; int err =3D -1; =20 if (!he || !he->ms.sym) @@ -123,12 +124,12 @@ static int perf_top__parse_source(struct perf_top *to= p, struct hist_entry *he) =20 sym =3D he->ms.sym; map =3D he->ms.map; + dso =3D map__dso(map); =20 /* * We can't annotate with just /proc/kallsyms */ - if (map->dso->symtab_type =3D=3D DSO_BINARY_TYPE__KALLSYMS && - !dso__is_kcore(map->dso)) { + if (dso->symtab_type =3D=3D DSO_BINARY_TYPE__KALLSYMS && !dso__is_kcore(d= so)) { pr_err("Can't annotate %s: No vmlinux file was found in the " "path\n", sym->name); sleep(1); @@ -169,6 +170,7 @@ static void ui__warn_map_erange(struct map *map, struct= symbol *sym, u64 ip) { struct utsname uts; int err =3D uname(&uts); + struct dso *dso =3D map__dso(map); =20 ui__warning("Out of bounds address found:\n\n" "Addr: %" PRIx64 "\n" @@ -180,7 +182,7 @@ static void ui__warn_map_erange(struct map *map, struct= symbol *sym, u64 ip) "Tools: %s\n\n" "Not all samples will be on the annotation output.\n\n" "Please report to linux-kernel@vger.kernel.org\n", - ip, map->dso->long_name, dso__symtab_origin(map->dso), + ip, dso->long_name, dso__symtab_origin(dso), map->start, map->end, sym->start, sym->end, sym->binding =3D=3D STB_GLOBAL ? 'g' : sym->binding =3D=3D STB_LOCAL ? 'l' : 'w', sym->name, @@ -810,7 +812,8 @@ static void perf_event__process_sample(struct perf_tool= *tool, __map__is_kernel(al.map) && map__has_symbols(al.map)) { if (symbol_conf.vmlinux_name) { char serr[256]; - dso__strerror_load(al.map->dso, serr, sizeof(serr)); + + dso__strerror_load(map__dso(al.map), serr, sizeof(serr)); ui__warning("The %s file can't be used: %s\n%s", symbol_conf.vmlinux_name, serr, msg); } else { diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index b363c609818b..72ef0bebb06b 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -2863,7 +2863,7 @@ static void print_location(FILE *f, struct perf_sampl= e *sample, { =20 if ((verbose > 0 || print_dso) && al->map) - fprintf(f, "%s@", al->map->dso->long_name); + fprintf(f, "%s@", map__dso(al->map)->long_name); =20 if ((verbose > 0 || print_sym) && al->sym) fprintf(f, "%s+0x%" PRIx64, al->sym->name, diff --git a/tools/perf/scripts/python/Perf-Trace-Util/Context.c b/tools/pe= rf/scripts/python/Perf-Trace-Util/Context.c index feedd02b3b3d..53b1587db403 100644 --- a/tools/perf/scripts/python/Perf-Trace-Util/Context.c +++ b/tools/perf/scripts/python/Perf-Trace-Util/Context.c @@ -145,6 +145,7 @@ static PyObject *perf_sample_src(PyObject *obj, PyObjec= t *args, bool get_srccode char *srccode =3D NULL; PyObject *result; struct map *map; + struct dso *dso; int len =3D 0; u64 addr; =20 @@ -153,9 +154,10 @@ static PyObject *perf_sample_src(PyObject *obj, PyObje= ct *args, bool get_srccode =20 map =3D c->al->map; addr =3D c->al->addr; + dso =3D map ? map__dso(map) : NULL; =20 - if (map && map->dso) - srcfile =3D get_srcline_split(map->dso, map__rip_2objdump(map, addr), &l= ine); + if (dso) + srcfile =3D get_srcline_split(dso, map__rip_2objdump(map, addr), &line); =20 if (get_srccode) { if (srcfile) diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-readin= g.c index 8d2036f2f944..936c61546e64 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -237,10 +237,11 @@ static int read_object_code(u64 addr, size_t len, u8 = cpumode, char decomp_name[KMOD_DECOMP_LEN]; bool decomp =3D false; int ret, err =3D 0; + struct dso *dso; =20 pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr); =20 - if (!thread__find_map(thread, cpumode, addr, &al) || !al.map->dso) { + if (!thread__find_map(thread, cpumode, addr, &al) || !map__dso(al.map)) { if (cpumode =3D=3D PERF_RECORD_MISC_HYPERVISOR) { pr_debug("Hypervisor address can not be resolved - skipping\n"); goto out; @@ -250,11 +251,10 @@ static int read_object_code(u64 addr, size_t len, u8 = cpumode, err =3D -1; goto out; } + dso =3D map__dso(al.map); + pr_debug("File is: %s\n", dso->long_name); =20 - pr_debug("File is: %s\n", al.map->dso->long_name); - - if (al.map->dso->symtab_type =3D=3D DSO_BINARY_TYPE__KALLSYMS && - !dso__is_kcore(al.map->dso)) { + if (dso->symtab_type =3D=3D DSO_BINARY_TYPE__KALLSYMS && !dso__is_kcore(d= so)) { pr_debug("Unexpected kernel address - skipping\n"); goto out; } @@ -269,7 +269,7 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, len =3D al.map->end - addr; =20 /* Read the object code using perf */ - ret_len =3D dso__data_read_offset(al.map->dso, maps__machine(thread->maps= ), + ret_len =3D dso__data_read_offset(dso, maps__machine(thread->maps), al.addr, buf1, len); if (ret_len !=3D len) { pr_debug("dso__data_read_offset failed\n"); @@ -287,7 +287,7 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, } =20 /* objdump struggles with kcore - try each map only once */ - if (dso__is_kcore(al.map->dso)) { + if (dso__is_kcore(dso)) { size_t d; =20 for (d =3D 0; d < state->done_cnt; d++) { @@ -304,9 +304,9 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, state->done[state->done_cnt++] =3D al.map->start; } =20 - objdump_name =3D al.map->dso->long_name; - if (dso__needs_decompress(al.map->dso)) { - if (dso__decompress_kmodule_path(al.map->dso, objdump_name, + objdump_name =3D dso->long_name; + if (dso__needs_decompress(dso)) { + if (dso__decompress_kmodule_path(dso, objdump_name, decomp_name, sizeof(decomp_name)) < 0) { pr_debug("decompression failed\n"); @@ -335,7 +335,7 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, len -=3D ret; if (len) { pr_debug("Reducing len to %zu\n", len); - } else if (dso__is_kcore(al.map->dso)) { + } else if (dso__is_kcore(dso)) { /* * objdump cannot handle very large segments * that may be found in kcore. @@ -572,6 +572,7 @@ static int do_test_code_reading(bool try_kcore) pid_t pid; struct map *map; bool have_vmlinux, have_kcore, excl_kernel =3D false; + struct dso *dso; =20 pid =3D getpid(); =20 @@ -595,8 +596,9 @@ static int do_test_code_reading(bool try_kcore) pr_debug("map__load failed\n"); goto out_err; } - have_vmlinux =3D dso__is_vmlinux(map->dso); - have_kcore =3D dso__is_kcore(map->dso); + dso =3D map__dso(map); + have_vmlinux =3D dso__is_vmlinux(dso); + have_kcore =3D dso__is_kcore(dso); =20 /* 2nd time through we just try kcore */ if (try_kcore && !have_kcore) diff --git a/tools/perf/tests/hists_common.c b/tools/perf/tests/hists_commo= n.c index 6f34d08b84e5..745ab18d17db 100644 --- a/tools/perf/tests/hists_common.c +++ b/tools/perf/tests/hists_common.c @@ -179,9 +179,11 @@ void print_hists_in(struct hists *hists) he =3D rb_entry(node, struct hist_entry, rb_node_in); =20 if (!he->filtered) { + struct dso *dso =3D map__dso(he->ms.map); + pr_info("%2d: entry: %-8s [%-8s] %20s: period =3D %"PRIu64"\n", i, thread__comm_str(he->thread), - he->ms.map->dso->short_name, + dso->short_name, he->ms.sym->name, he->stat.period); } =20 @@ -206,9 +208,11 @@ void print_hists_out(struct hists *hists) he =3D rb_entry(node, struct hist_entry, rb_node); =20 if (!he->filtered) { + struct dso *dso =3D map__dso(he->ms.map); + pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period =3D %"PRIu64"/%"PRIu64= "\n", i, thread__comm_str(he->thread), he->thread->tid, - he->ms.map->dso->short_name, + dso->short_name, he->ms.sym->name, he->stat.period, he->stat_acc ? he->stat_acc->period : 0); } diff --git a/tools/perf/tests/hists_cumulate.c b/tools/perf/tests/hists_cum= ulate.c index b42d37ff2399..f00ec9abdbcd 100644 --- a/tools/perf/tests/hists_cumulate.c +++ b/tools/perf/tests/hists_cumulate.c @@ -150,12 +150,12 @@ static void del_hist_entries(struct hists *hists) typedef int (*test_fn_t)(struct evsel *, struct machine *); =20 #define COMM(he) (thread__comm_str(he->thread)) -#define DSO(he) (he->ms.map->dso->short_name) +#define DSO(he) (map__dso(he->ms.map)->short_name) #define SYM(he) (he->ms.sym->name) #define CPU(he) (he->cpu) #define PID(he) (he->thread->tid) #define DEPTH(he) (he->callchain->max_depth) -#define CDSO(cl) (cl->ms.map->dso->short_name) +#define CDSO(cl) (map__dso(cl->ms.map)->short_name) #define CSYM(cl) (cl->ms.sym->name) =20 struct result { diff --git a/tools/perf/tests/hists_filter.c b/tools/perf/tests/hists_filte= r.c index 8e1ceeb9b7b6..7c552549f4a4 100644 --- a/tools/perf/tests/hists_filter.c +++ b/tools/perf/tests/hists_filter.c @@ -194,7 +194,7 @@ static int test__hists_filter(struct test_suite *test _= _maybe_unused, int subtes hists__filter_by_thread(hists); =20 /* now applying dso filter for 'kernel' */ - hists->dso_filter =3D fake_samples[0].map->dso; + hists->dso_filter =3D map__dso(fake_samples[0].map); hists__filter_by_dso(hists); =20 if (verbose > 2) { @@ -288,7 +288,7 @@ static int test__hists_filter(struct test_suite *test _= _maybe_unused, int subtes =20 /* now applying all filters at once. */ hists->thread_filter =3D fake_samples[1].thread; - hists->dso_filter =3D fake_samples[1].map->dso; + hists->dso_filter =3D map__dso(fake_samples[1].map); hists__filter_by_thread(hists); hists__filter_by_dso(hists); =20 diff --git a/tools/perf/tests/hists_output.c b/tools/perf/tests/hists_outpu= t.c index 62b0093253e3..428d11a938f2 100644 --- a/tools/perf/tests/hists_output.c +++ b/tools/perf/tests/hists_output.c @@ -116,7 +116,7 @@ static void del_hist_entries(struct hists *hists) typedef int (*test_fn_t)(struct evsel *, struct machine *); =20 #define COMM(he) (thread__comm_str(he->thread)) -#define DSO(he) (he->ms.map->dso->short_name) +#define DSO(he) (map__dso(he->ms.map)->short_name) #define SYM(he) (he->ms.sym->name) #define CPU(he) (he->cpu) #define PID(he) (he->thread->tid) diff --git a/tools/perf/tests/maps.c b/tools/perf/tests/maps.c index 8246d37e4b7a..ae7028fbf79e 100644 --- a/tools/perf/tests/maps.c +++ b/tools/perf/tests/maps.c @@ -26,7 +26,7 @@ static int check_maps(struct map_def *merged, unsigned in= t size, struct maps *ma =20 TEST_ASSERT_VAL("wrong map start", map->start =3D=3D merged[i].start); TEST_ASSERT_VAL("wrong map end", map->end =3D=3D merged[i].end); - TEST_ASSERT_VAL("wrong map name", !strcmp(map->dso->name, merged[i].nam= e)); + TEST_ASSERT_VAL("wrong map name", !strcmp(map__dso(map)->name, merged[i= ].name)); TEST_ASSERT_VAL("wrong map refcnt", refcount_read(&map->refcnt) =3D=3D 1= ); =20 i++; diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c index 0793f8f419e2..2d1aa42d36a9 100644 --- a/tools/perf/tests/symbols.c +++ b/tools/perf/tests/symbols.c @@ -102,6 +102,7 @@ static int test_file(struct test_info *ti, char *filena= me) { struct map *map =3D NULL; int ret, nr; + struct dso *dso; =20 pr_debug("Testing %s\n", filename); =20 @@ -109,7 +110,8 @@ static int test_file(struct test_info *ti, char *filena= me) if (ret !=3D TEST_OK) return ret; =20 - nr =3D dso__load(map->dso, map); + dso =3D map__dso(map); + nr =3D dso__load(dso, map); if (nr < 0) { pr_debug("dso__load() failed!\n"); ret =3D TEST_FAIL; @@ -122,7 +124,7 @@ static int test_file(struct test_info *ti, char *filena= me) goto out_put; } =20 - ret =3D test_dso(map->dso); + ret =3D test_dso(dso); out_put: map__put(map); =20 diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux= -kallsyms.c index c8abb3ca8347..c614c2db7e89 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -293,15 +293,16 @@ static int test__vmlinux_matches_kallsyms(struct test= _suite *test __maybe_unused =20 maps__for_each_entry(maps, rb_node) { struct map *map =3D rb_node->map; + struct dso *dso =3D map__dso(map); /* * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while * the kernel will have the path for the vmlinux file being used, * so use the short name, less descriptive but the same ("[kernel]" in * both cases. */ - struct map *pair =3D maps__find_by_name(kallsyms.kmaps, (map->dso->kerne= l ? - map->dso->short_name : - map->dso->name)); + struct map *pair =3D maps__find_by_name(kallsyms.kmaps, (dso->kernel ? + dso->short_name : + dso->name)); if (pair) { pair->priv =3D 1; } else { @@ -326,17 +327,19 @@ static int test__vmlinux_matches_kallsyms(struct test= _suite *test __maybe_unused continue; =20 if (pair->start =3D=3D mem_start) { + struct dso *dso =3D map__dso(map); + if (!header_printed) { pr_info("WARN: Maps in vmlinux with a different name in kallsyms:\n"); header_printed =3D true; } =20 pr_info("WARN: %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as", - map->start, map->end, map->pgoff, map->dso->name); + map->start, map->end, map->pgoff, dso->name); if (mem_end !=3D pair->end) pr_info(":\nWARN: *%" PRIx64 "-%" PRIx64 " %" PRIx64, pair->start, pair->end, pair->pgoff); - pr_info(" %s\n", pair->dso->name); + pr_info(" %s\n", dso->name); pair->priv =3D 1; } } diff --git a/tools/perf/ui/browsers/annotate.c b/tools/perf/ui/browsers/ann= otate.c index c03fa76c02ff..12c3ce530e42 100644 --- a/tools/perf/ui/browsers/annotate.c +++ b/tools/perf/ui/browsers/annotate.c @@ -441,7 +441,8 @@ static void ui_browser__init_asm_mode(struct ui_browser= *browser) static int sym_title(struct symbol *sym, struct map *map, char *title, size_t sz, int percent_type) { - return snprintf(title, sz, "%s %s [Percent: %s]", sym->name, map->dso->l= ong_name, + return snprintf(title, sz, "%s %s [Percent: %s]", sym->name, + map__dso(map)->long_name, percent_type_str(percent_type)); } =20 @@ -964,20 +965,22 @@ int symbol__tui_annotate(struct map_symbol *ms, struc= t evsel *evsel, }, .opts =3D opts, }; + struct dso *dso; int ret =3D -1, err; int not_annotated =3D list_empty(¬es->src->source); =20 if (sym =3D=3D NULL) return -1; =20 - if (ms->map->dso->annotate_warned) + dso =3D map__dso(ms->map); + if (dso->annotate_warned) return -1; =20 if (not_annotated) { err =3D symbol__annotate2(ms, evsel, opts, &browser.arch); if (err) { char msg[BUFSIZ]; - ms->map->dso->annotate_warned =3D true; + dso->annotate_warned =3D true; symbol__strerror_disassemble(ms, err, msg, sizeof(msg)); ui__error("Couldn't annotate %s:\n%s", sym->name, msg); goto out_free_offsets; diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c index 572ff38ceb0f..66d8c0802ecd 100644 --- a/tools/perf/ui/browsers/hists.c +++ b/tools/perf/ui/browsers/hists.c @@ -2487,7 +2487,7 @@ static struct symbol *symbol__new_unresolved(u64 addr= , struct map *map) return NULL; } =20 - dso__insert_symbol(map->dso, sym); + dso__insert_symbol(map__dso(map), sym); } =20 return sym; @@ -2499,7 +2499,7 @@ add_annotate_opt(struct hist_browser *browser __maybe= _unused, struct map_symbol *ms, u64 addr) { - if (!ms->map || !ms->map->dso || ms->map->dso->annotate_warned) + if (!ms->map || !map__dso(ms->map) || map__dso(ms->map)->annotate_warned) return 0; =20 if (!ms->sym) @@ -2590,8 +2590,10 @@ static int hists_browser__zoom_map(struct hist_brows= er *browser, struct map *map ui_helpline__pop(); } else { ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s DS= O\"", - __map__is_kernel(map) ? "the Kernel" : map->dso->short_name); - browser->hists->dso_filter =3D map->dso; + __map__is_kernel(map) + ? "the Kernel" + : map__dso(map)->short_name); + browser->hists->dso_filter =3D map__dso(map); perf_hpp__set_elide(HISTC_DSO, true); pstack__push(browser->pstack, &browser->hists->dso_filter); } @@ -2616,7 +2618,7 @@ add_dso_opt(struct hist_browser *browser, struct popu= p_action *act, =20 if (asprintf(optstr, "Zoom %s %s DSO (use the 'k' hotkey to zoom directly= into the kernel)", browser->hists->dso_filter ? "out of" : "into", - __map__is_kernel(map) ? "the Kernel" : map->dso->short_name) < 0) + __map__is_kernel(map) ? "the Kernel" : map__dso(map)->short_name) <= 0) return 0; =20 act->ms.map =3D map; @@ -3091,8 +3093,8 @@ static int evsel__hists_browse(struct evsel *evsel, i= nt nr_events, const char *h =20 if (!browser->selection || !browser->selection->map || - !browser->selection->map->dso || - browser->selection->map->dso->annotate_warned) { + !map__dso(browser->selection->map) || + map__dso(browser->selection->map)->annotate_warned) { continue; } =20 diff --git a/tools/perf/ui/browsers/map.c b/tools/perf/ui/browsers/map.c index 3d49b916c9e4..3d1b958d8832 100644 --- a/tools/perf/ui/browsers/map.c +++ b/tools/perf/ui/browsers/map.c @@ -76,7 +76,7 @@ static int map_browser__run(struct map_browser *browser) { int key; =20 - if (ui_browser__show(&browser->b, browser->map->dso->long_name, + if (ui_browser__show(&browser->b, map__dso(browser->map)->long_name, "Press ESC to exit, %s / to search", verbose > 0 ? "" : "restart with -v to use") < 0) return -1; @@ -106,7 +106,7 @@ int map__browse(struct map *map) { struct map_browser mb =3D { .b =3D { - .entries =3D &map->dso->symbols, + .entries =3D &map__dso(map)->symbols, .refresh =3D ui_browser__rb_tree_refresh, .seek =3D ui_browser__rb_tree_seek, .write =3D map_browser__write, diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index db475e44f42f..9494b34e84fc 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1593,7 +1593,7 @@ static void delete_last_nop(struct symbol *sym) =20 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *= buf, size_t buflen) { - struct dso *dso =3D ms->map->dso; + struct dso *dso =3D map__dso(ms->map); =20 BUG_ON(buflen =3D=3D 0); =20 @@ -1735,7 +1735,7 @@ static int symbol__disassemble_bpf(struct symbol *sym, struct map *map =3D args->ms.map; struct perf_bpil *info_linear; struct disassemble_info info; - struct dso *dso =3D map->dso; + struct dso *dso =3D map__dso(map); int pc =3D 0, count, sub_id; struct btf *btf =3D NULL; char tpath[PATH_MAX]; @@ -1958,7 +1958,7 @@ static int symbol__disassemble(struct symbol *sym, st= ruct annotate_args *args) { struct annotation_options *opts =3D args->options; struct map *map =3D args->ms.map; - struct dso *dso =3D map->dso; + struct dso *dso =3D map__dso(map); char *command; FILE *file; char symfs_filename[PATH_MAX]; @@ -2403,7 +2403,7 @@ int symbol__annotate_printf(struct map_symbol *ms, st= ruct evsel *evsel, { struct map *map =3D ms->map; struct symbol *sym =3D ms->sym; - struct dso *dso =3D map->dso; + struct dso *dso =3D map__dso(map); char *filename; const char *d_filename; const char *evsel_name =3D evsel__name(evsel); @@ -2586,7 +2586,7 @@ int map_symbol__annotation_dump(struct map_symbol *ms= , struct evsel *evsel, } =20 fprintf(fp, "%s() %s\nEvent: %s\n\n", - ms->sym->name, ms->map->dso->long_name, ev_name); + ms->sym->name, map__dso(ms->map)->long_name, ev_name); symbol__annotate_fprintf2(ms->sym, fp, opts); =20 fclose(fp); @@ -2812,7 +2812,7 @@ static void annotation__calc_lines(struct annotation = *notes, struct map *map, if (percent_max <=3D 0.5) continue; =20 - al->path =3D get_srcline(map->dso, notes->start + al->offset, NULL, + al->path =3D get_srcline(map__dso(map), notes->start + al->offset, NULL, false, true, notes->start + al->offset); insert_source_line(&tmp_root, al, opts); } @@ -2831,7 +2831,7 @@ static void symbol__calc_lines(struct map_symbol *ms,= struct rb_root *root, int symbol__tty_annotate2(struct map_symbol *ms, struct evsel *evsel, struct annotation_options *opts) { - struct dso *dso =3D ms->map->dso; + struct dso *dso =3D map__dso(ms->map); struct symbol *sym =3D ms->sym; struct rb_root source_line =3D RB_ROOT; struct hists *hists =3D evsel__hists(evsel); @@ -2867,7 +2867,7 @@ int symbol__tty_annotate2(struct map_symbol *ms, stru= ct evsel *evsel, int symbol__tty_annotate(struct map_symbol *ms, struct evsel *evsel, struct annotation_options *opts) { - struct dso *dso =3D ms->map->dso; + struct dso *dso =3D map__dso(ms->map); struct symbol *sym =3D ms->sym; struct rb_root source_line =3D RB_ROOT; int err; diff --git a/tools/perf/util/auxtrace.c b/tools/perf/util/auxtrace.c index 498ff7f24463..2341de8573c0 100644 --- a/tools/perf/util/auxtrace.c +++ b/tools/perf/util/auxtrace.c @@ -2557,7 +2557,7 @@ static struct dso *load_dso(const char *name) if (map__load(map) < 0) pr_err("File '%s' not found or has no symbols.\n", name); =20 - dso =3D dso__get(map->dso); + dso =3D dso__get(map__dso(map)); =20 map__put(map); =20 diff --git a/tools/perf/util/block-info.c b/tools/perf/util/block-info.c index 5ecd4f401f32..16a7b4adcf18 100644 --- a/tools/perf/util/block-info.c +++ b/tools/perf/util/block-info.c @@ -317,9 +317,9 @@ static int block_dso_entry(struct perf_hpp_fmt *fmt, st= ruct perf_hpp *hpp, struct block_fmt *block_fmt =3D container_of(fmt, struct block_fmt, fmt); struct map *map =3D he->ms.map; =20 - if (map && map->dso) { + if (map && map__dso(map)) { return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, - map->dso->short_name); + map__dso(map)->short_name); } =20 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index 025f331b3867..38fcf3ba5749 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -57,10 +57,12 @@ static int machine__process_bpf_event_load(struct machi= ne *machine, struct map *map =3D maps__find(machine__kernel_maps(machine), addr); =20 if (map) { - map->dso->binary_type =3D DSO_BINARY_TYPE__BPF_PROG_INFO; - map->dso->bpf_prog.id =3D id; - map->dso->bpf_prog.sub_id =3D i; - map->dso->bpf_prog.env =3D env; + struct dso *dso =3D map__dso(map); + + dso->binary_type =3D DSO_BINARY_TYPE__BPF_PROG_INFO; + dso->bpf_prog.id =3D id; + dso->bpf_prog.sub_id =3D i; + dso->bpf_prog.env =3D env; } } return 0; diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index ea9c083ab1e3..06a8cd88cbef 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -59,7 +59,7 @@ int build_id__mark_dso_hit(struct perf_tool *tool __maybe= _unused, } =20 if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) - al.map->dso->hit =3D 1; + map__dso(al.map)->hit =3D 1; =20 thread__put(thread); return 0; diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index 0aa979f64565..9e9c39dd9d2b 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -701,8 +701,8 @@ static enum match_result match_chain_strings(const char= *left, static enum match_result match_chain_dso_addresses(struct map *left_map, u= 64 left_ip, struct map *right_map, u64 right_ip) { - struct dso *left_dso =3D left_map ? left_map->dso : NULL; - struct dso *right_dso =3D right_map ? right_map->dso : NULL; + struct dso *left_dso =3D left_map ? map__dso(left_map) : NULL; + struct dso *right_dso =3D right_map ? map__dso(right_map) : NULL; =20 if (left_dso !=3D right_dso) return left_dso < right_dso ? MATCH_LT : MATCH_GT; @@ -1174,7 +1174,7 @@ char *callchain_list__sym_name(struct callchain_list = *cl, if (show_dso) scnprintf(bf + printed, bfsize - printed, " %s", cl->ms.map ? - cl->ms.map->dso->short_name : + map__dso(cl->ms.map)->short_name : "unknown"); =20 return bf; diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-con= vert-json.c index ba9d93ce9463..653709ab867a 100644 --- a/tools/perf/util/data-convert-json.c +++ b/tools/perf/util/data-convert-json.c @@ -128,15 +128,17 @@ static void output_sample_callchain_entry(struct perf= _tool *tool, output_json_key_format(out, false, 5, "ip", "\"0x%" PRIx64 "\"", ip); =20 if (al && al->sym && al->sym->namelen) { + struct dso *dso =3D al->map ? map__dso(al->map) : NULL; + fputc(',', out); output_json_key_string(out, false, 5, "symbol", al->sym->name); =20 - if (al->map && al->map->dso) { - const char *dso =3D al->map->dso->short_name; + if (dso) { + const char *dso_name =3D dso->short_name; =20 - if (dso && strlen(dso) > 0) { + if (dso_name && strlen(dso_name) > 0) { fputc(',', out); - output_json_key_string(out, false, 5, "dso", dso); + output_json_key_string(out, false, 5, "dso", dso_name); } } } diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c index 1cfcfdd3cf52..84c970c11794 100644 --- a/tools/perf/util/db-export.c +++ b/tools/perf/util/db-export.c @@ -179,7 +179,7 @@ static int db_ids_from_al(struct db_export *dbe, struct= addr_location *al, int err; =20 if (al->map) { - struct dso *dso =3D al->map->dso; + struct dso *dso =3D map__dso(al->map); =20 err =3D db_export__dso(dbe, dso, maps__machine(al->maps)); if (err) @@ -255,7 +255,7 @@ static struct call_path *call_path_from_sample(struct d= b_export *dbe, al.addr =3D node->ip; =20 if (al.map && !al.sym) - al.sym =3D dso__find_symbol(al.map->dso, al.addr); + al.sym =3D dso__find_symbol(map__dso(al.map), al.addr); =20 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset); =20 diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c index fe2a0752a0f6..8a7ffe0d805a 100644 --- a/tools/perf/util/dlfilter.c +++ b/tools/perf/util/dlfilter.c @@ -29,7 +29,7 @@ static void al_to_d_al(struct addr_location *al, struct p= erf_dlfilter_al *d_al) =20 d_al->size =3D sizeof(*d_al); if (al->map) { - struct dso *dso =3D al->map->dso; + struct dso *dso =3D map__dso(al->map); =20 if (symbol_conf.show_kernel_path && dso->long_name) d_al->dso =3D dso->long_name; @@ -220,6 +220,7 @@ static const char *dlfilter__srcline(void *ctx, __u32 *= line_no) unsigned int line =3D 0; char *srcfile =3D NULL; struct map *map; + struct dso *dso; u64 addr; =20 if (!d->ctx_valid || !line_no) @@ -231,9 +232,10 @@ static const char *dlfilter__srcline(void *ctx, __u32 = *line_no) =20 map =3D al->map; addr =3D al->addr; + dso =3D map ? map__dso(map) : NULL; =20 - if (map && map->dso) - srcfile =3D get_srcline_split(map->dso, map__rip_2objdump(map, addr), &l= ine); + if (dso) + srcfile =3D get_srcline_split(dso, map__rip_2objdump(map, addr), &line); =20 *line_no =3D line; return srcfile; @@ -279,7 +281,7 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip,= void *buf, __u32 len) offset =3D map->map_ip(map, ip); if (ip + len >=3D map->end) len =3D map->end - ip; - return dso__data_read_offset(map->dso, d->machine, offset, buf, len); + return dso__data_read_offset(map__dso(map), d->machine, offset, buf, len); } =20 static const struct perf_dlfilter_fns perf_dlfilter_fns =3D { diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index f40cdd6ac126..2ddc75dee019 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -685,6 +685,7 @@ int machine__resolve(struct machine *machine, struct ad= dr_location *al, struct perf_sample *sample) { struct thread *thread; + struct dso *dso; =20 if (symbol_conf.guest_code && !machine__is_host(machine)) thread =3D machine__findnew_guest_code(machine, sample->pid); @@ -695,9 +696,11 @@ int machine__resolve(struct machine *machine, struct a= ddr_location *al, =20 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid= ); thread__find_map(thread, sample->cpumode, sample->ip, al); + dso =3D al->map ? map__dso(al->map) : NULL; dump_printf(" ...... dso: %s\n", - al->map ? al->map->dso->long_name : - al->level =3D=3D 'H' ? "[hypervisor]" : ""); + dso + ? dso->long_name + : (al->level =3D=3D 'H' ? "[hypervisor]" : "")); =20 if (thread__is_filtered(thread)) al->filtered |=3D (1 << HIST_FILTER__THREAD); @@ -715,8 +718,6 @@ int machine__resolve(struct machine *machine, struct ad= dr_location *al, } =20 if (al->map) { - struct dso *dso =3D al->map->dso; - if (symbol_conf.dso_list && (!dso || !(strlist__has_entry(symbol_conf.dso_list, dso->short_name) || diff --git a/tools/perf/util/evsel_fprintf.c b/tools/perf/util/evsel_fprint= f.c index bd22c4932d10..dff5d8c4b06d 100644 --- a/tools/perf/util/evsel_fprintf.c +++ b/tools/perf/util/evsel_fprintf.c @@ -155,7 +155,7 @@ int sample__fprintf_callchain(struct perf_sample *sampl= e, int left_alignment, =20 if (print_ip) { /* Show binary offset for userspace addr */ - if (map && !map->dso->kernel) + if (map && !map__dso(map)->kernel) printed +=3D fprintf(fp, "%c%16" PRIx64, s, addr); else printed +=3D fprintf(fp, "%c%16" PRIx64, s, node->ip); diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 1b0e89cd5d99..fdf0562d2fd3 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -106,7 +106,7 @@ void hists__calc_col_len(struct hists *hists, struct hi= st_entry *h) hists__set_col_len(hists, HISTC_THREAD, len + 8); =20 if (h->ms.map) { - len =3D dso__name_len(h->ms.map->dso); + len =3D dso__name_len(map__dso(h->ms.map)); hists__new_col_len(hists, HISTC_DSO, len); } =20 @@ -120,7 +120,7 @@ void hists__calc_col_len(struct hists *hists, struct hi= st_entry *h) symlen +=3D BITS_PER_LONG / 4 + 2 + 3; hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); =20 - symlen =3D dso__name_len(h->branch_info->from.ms.map->dso); + symlen =3D dso__name_len(map__dso(h->branch_info->from.ms.map)); hists__new_col_len(hists, HISTC_DSO_FROM, symlen); } else { symlen =3D unresolved_col_width + 4 + 2; @@ -135,7 +135,7 @@ void hists__calc_col_len(struct hists *hists, struct hi= st_entry *h) symlen +=3D BITS_PER_LONG / 4 + 2 + 3; hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); =20 - symlen =3D dso__name_len(h->branch_info->to.ms.map->dso); + symlen =3D dso__name_len(map__dso(h->branch_info->to.ms.map)); hists__new_col_len(hists, HISTC_DSO_TO, symlen); } else { symlen =3D unresolved_col_width + 4 + 2; @@ -180,7 +180,7 @@ void hists__calc_col_len(struct hists *hists, struct hi= st_entry *h) } =20 if (h->mem_info->daddr.ms.map) { - symlen =3D dso__name_len(h->mem_info->daddr.ms.map->dso); + symlen =3D dso__name_len(map__dso(h->mem_info->daddr.ms.map)); hists__new_col_len(hists, HISTC_MEM_DADDR_DSO, symlen); } else { @@ -2104,7 +2104,7 @@ static bool hists__filter_entry_by_dso(struct hists *= hists, struct hist_entry *he) { if (hists->dso_filter !=3D NULL && - (he->ms.map =3D=3D NULL || he->ms.map->dso !=3D hists->dso_filter)) { + (he->ms.map =3D=3D NULL || map__dso(he->ms.map) !=3D hists->dso_filte= r)) { he->filtered |=3D (1 << HIST_FILTER__DSO); return true; } diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c index 955c1b9dc6a4..8cec88e09792 100644 --- a/tools/perf/util/intel-pt.c +++ b/tools/perf/util/intel-pt.c @@ -801,17 +801,19 @@ static int intel_pt_walk_next_insn(struct intel_pt_in= sn *intel_pt_insn, } =20 while (1) { - if (!thread__find_map(thread, cpumode, *ip, &al) || !al.map->dso) { + struct dso *dso; + + if (!thread__find_map(thread, cpumode, *ip, &al) || !map__dso(al.map)) { if (al.map) intel_pt_log("ERROR: thread has no dso for %#" PRIx64 "\n", *ip); else intel_pt_log("ERROR: thread has no map for %#" PRIx64 "\n", *ip); return -EINVAL; } + dso =3D map__dso(al.map); =20 - if (al.map->dso->data.status =3D=3D DSO_DATA_STATUS_ERROR && - dso__data_status_seen(al.map->dso, - DSO_DATA_STATUS_SEEN_ITRACE)) + if (dso->data.status =3D=3D DSO_DATA_STATUS_ERROR && + dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE)) return -ENOENT; =20 offset =3D al.map->map_ip(al.map, *ip); @@ -819,7 +821,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn= *intel_pt_insn, if (!to_ip && one_map) { struct intel_pt_cache_entry *e; =20 - e =3D intel_pt_cache_lookup(al.map->dso, machine, offset); + e =3D intel_pt_cache_lookup(dso, machine, offset); if (e && (!max_insn_cnt || e->insn_cnt <=3D max_insn_cnt)) { *insn_cnt_ptr =3D e->insn_cnt; @@ -829,8 +831,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn= *intel_pt_insn, intel_pt_insn->emulated_ptwrite =3D e->emulated_ptwrite; intel_pt_insn->length =3D e->length; intel_pt_insn->rel =3D e->rel; - memcpy(intel_pt_insn->buf, e->insn, - INTEL_PT_INSN_BUF_SZ); + memcpy(intel_pt_insn->buf, e->insn, INTEL_PT_INSN_BUF_SZ); intel_pt_log_insn_no_data(intel_pt_insn, *ip); return 0; } @@ -842,17 +843,17 @@ static int intel_pt_walk_next_insn(struct intel_pt_in= sn *intel_pt_insn, /* Load maps to ensure dso->is_64_bit has been updated */ map__load(al.map); =20 - x86_64 =3D al.map->dso->is_64_bit; + x86_64 =3D dso->is_64_bit; =20 while (1) { - len =3D dso__data_read_offset(al.map->dso, machine, + len =3D dso__data_read_offset(dso, machine, offset, buf, INTEL_PT_INSN_BUF_SZ); if (len <=3D 0) { intel_pt_log("ERROR: failed to read at offset %#" PRIx64 " ", offset); if (intel_pt_enable_logging) - dso__fprintf(al.map->dso, intel_pt_log_fp()); + dso__fprintf(dso, intel_pt_log_fp()); return -EINVAL; } =20 @@ -871,7 +872,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn= *intel_pt_insn, goto out; /* Check for emulated ptwrite */ offs =3D offset + intel_pt_insn->length; - eptw =3D intel_pt_emulated_ptwrite(al.map->dso, machine, offs); + eptw =3D intel_pt_emulated_ptwrite(dso, machine, offs); intel_pt_insn->emulated_ptwrite =3D eptw; goto out; } @@ -906,13 +907,13 @@ static int intel_pt_walk_next_insn(struct intel_pt_in= sn *intel_pt_insn, if (to_ip) { struct intel_pt_cache_entry *e; =20 - e =3D intel_pt_cache_lookup(al.map->dso, machine, start_offset); + e =3D intel_pt_cache_lookup(map__dso(al.map), machine, start_offset); if (e) return 0; } =20 /* Ignore cache errors */ - intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt, + intel_pt_cache_add(map__dso(al.map), machine, start_offset, insn_cnt, *ip - start_ip, intel_pt_insn); =20 return 0; @@ -983,13 +984,12 @@ static int __intel_pt_pgd_ip(uint64_t ip, void *data) if (!thread) return -EINVAL; =20 - if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso) + if (!thread__find_map(thread, cpumode, ip, &al) || !map__dso(al.map)) return -EINVAL; =20 offset =3D al.map->map_ip(al.map, ip); =20 - return intel_pt_match_pgd_ip(ptq->pt, ip, offset, - al.map->dso->long_name); + return intel_pt_match_pgd_ip(ptq->pt, ip, offset, map__dso(al.map)->long_= name); } =20 static bool intel_pt_pgd_ip(uint64_t ip, void *data) @@ -2744,7 +2744,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u6= 4 *ptss_ip) if (map__load(map)) return 0; =20 - start =3D dso__first_symbol(map->dso); + start =3D dso__first_symbol(map__dso(map)); =20 for (sym =3D start; sym; sym =3D dso__next_symbol(sym)) { if (sym->binding =3D=3D STB_GLOBAL && @@ -3381,18 +3381,21 @@ static int intel_pt_text_poke(struct intel_pt *pt, = union perf_event *event) return 0; =20 for (; cnt; cnt--, addr--) { + struct dso *dso; + if (intel_pt_find_map(thread, cpumode, addr, &al)) { if (addr < event->text_poke.addr) return 0; continue; } =20 - if (!al.map->dso || !al.map->dso->auxtrace_cache) + dso =3D map__dso(al.map); + if (!dso || !dso->auxtrace_cache) continue; =20 offset =3D al.map->map_ip(al.map, addr); =20 - e =3D intel_pt_cache_lookup(al.map->dso, machine, offset); + e =3D intel_pt_cache_lookup(dso, machine, offset); if (!e) continue; =20 @@ -3405,9 +3408,9 @@ static int intel_pt_text_poke(struct intel_pt *pt, un= ion perf_event *event) if (e->branch !=3D INTEL_PT_BR_NO_BRANCH) return 0; } else { - intel_pt_cache_invalidate(al.map->dso, machine, offset); + intel_pt_cache_invalidate(dso, machine, offset); intel_pt_log("Invalidated instruction cache for %s at %#"PRIx64"\n", - al.map->dso->long_name, addr); + dso->long_name, addr); } } =20 diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 446c0273259d..6e32344e66dc 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -47,7 +47,7 @@ static void __machine__remove_thread(struct machine *mach= ine, struct thread *th, =20 static struct dso *machine__kernel_dso(struct machine *machine) { - return machine->vmlinux_map->dso; + return map__dso(machine->vmlinux_map); } =20 static void dsos__init(struct dsos *dsos) @@ -878,12 +878,13 @@ static int machine__process_ksymbol_register(struct m= achine *machine, struct perf_sample *sample __maybe_unused) { struct symbol *sym; + struct dso *dso; struct map *map =3D maps__find(machine__kernel_maps(machine), event->ksym= bol.addr); =20 if (!map) { - struct dso *dso =3D dso__new(event->ksymbol.name); int err; =20 + dso =3D dso__new(event->ksymbol.name); if (dso) { dso->kernel =3D DSO_SPACE__KERNEL; map =3D map__new2(0, dso); @@ -895,9 +896,9 @@ static int machine__process_ksymbol_register(struct mac= hine *machine, } =20 if (event->ksymbol.ksym_type =3D=3D PERF_RECORD_KSYMBOL_TYPE_OOL) { - map->dso->binary_type =3D DSO_BINARY_TYPE__OOL; - map->dso->data.file_size =3D event->ksymbol.len; - dso__set_loaded(map->dso); + dso->binary_type =3D DSO_BINARY_TYPE__OOL; + dso->data.file_size =3D event->ksymbol.len; + dso__set_loaded(dso); } =20 map->start =3D event->ksymbol.addr; @@ -913,6 +914,8 @@ static int machine__process_ksymbol_register(struct mac= hine *machine, dso->binary_type =3D DSO_BINARY_TYPE__BPF_IMAGE; dso__set_long_name(dso, "", false); } + } else { + dso =3D map__dso(map); } =20 sym =3D symbol__new(map->map_ip(map, map->start), @@ -920,7 +923,7 @@ static int machine__process_ksymbol_register(struct mac= hine *machine, 0, 0, event->ksymbol.name); if (!sym) return -ENOMEM; - dso__insert_symbol(map->dso, sym); + dso__insert_symbol(dso, sym); return 0; } =20 @@ -938,9 +941,11 @@ static int machine__process_ksymbol_unregister(struct = machine *machine, if (map !=3D machine->vmlinux_map) maps__remove(machine__kernel_maps(machine), map); else { - sym =3D dso__find_symbol(map->dso, map->map_ip(map, map->start)); + struct dso *dso =3D map__dso(map); + + sym =3D dso__find_symbol(dso, map->map_ip(map, map->start)); if (sym) - dso__delete_symbol(map->dso, sym); + dso__delete_symbol(dso, sym); } =20 return 0; @@ -964,6 +969,7 @@ int machine__process_text_poke(struct machine *machine,= union perf_event *event, { struct map *map =3D maps__find(machine__kernel_maps(machine), event->text= _poke.addr); u8 cpumode =3D event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; + struct dso *dso =3D map ? map__dso(map) : NULL; =20 if (dump_trace) perf_event__fprintf_text_poke(event, machine, stdout); @@ -976,7 +982,7 @@ int machine__process_text_poke(struct machine *machine,= union perf_event *event, return 0; } =20 - if (map && map->dso) { + if (dso) { u8 *new_bytes =3D event->text_poke.bytes + event->text_poke.old_len; int ret; =20 @@ -985,7 +991,7 @@ int machine__process_text_poke(struct machine *machine,= union perf_event *event, * must be done prior to using kernel maps. */ map__load(map); - ret =3D dso__data_write_cache_addr(map->dso, map, machine, + ret =3D dso__data_write_cache_addr(dso, map, machine, event->text_poke.addr, new_bytes, event->text_poke.new_len); @@ -1421,10 +1427,11 @@ int machines__create_kernel_maps(struct machines *m= achines, pid_t pid) int machine__load_kallsyms(struct machine *machine, const char *filename) { struct map *map =3D machine__kernel_map(machine); - int ret =3D __dso__load_kallsyms(map->dso, filename, map, true); + struct dso *dso =3D map__dso(map); + int ret =3D __dso__load_kallsyms(dso, filename, map, true); =20 if (ret > 0) { - dso__set_loaded(map->dso); + dso__set_loaded(dso); /* * Since /proc/kallsyms will have multiple sessions for the * kernel, with modules between them, fixup the end of all @@ -1439,10 +1446,11 @@ int machine__load_kallsyms(struct machine *machine,= const char *filename) int machine__load_vmlinux_path(struct machine *machine) { struct map *map =3D machine__kernel_map(machine); - int ret =3D dso__load_vmlinux_path(map->dso, map); + struct dso *dso =3D map__dso(map); + int ret =3D dso__load_vmlinux_path(dso, map); =20 if (ret > 0) - dso__set_loaded(map->dso); + dso__set_loaded(dso); =20 return ret; } @@ -1484,6 +1492,7 @@ static bool is_kmod_dso(struct dso *dso) static int maps__set_module_path(struct maps *maps, const char *path, stru= ct kmod_path *m) { char *long_name; + struct dso *dso; struct map *map =3D maps__find_by_name(maps, m->name); =20 if (map =3D=3D NULL) @@ -1493,16 +1502,17 @@ static int maps__set_module_path(struct maps *maps,= const char *path, struct kmo if (long_name =3D=3D NULL) return -ENOMEM; =20 - dso__set_long_name(map->dso, long_name, true); - dso__kernel_module_get_build_id(map->dso, ""); + dso =3D map__dso(map); + dso__set_long_name(dso, long_name, true); + dso__kernel_module_get_build_id(dso, ""); =20 /* * Full name could reveal us kmod compression, so * we need to update the symtab_type if needed. */ - if (m->comp && is_kmod_dso(map->dso)) { - map->dso->symtab_type++; - map->dso->comp =3D m->comp; + if (m->comp && is_kmod_dso(dso)) { + dso->symtab_type++; + dso->comp =3D m->comp; } =20 return 0; @@ -1601,7 +1611,7 @@ static int machine__create_module(void *arg, const ch= ar *name, u64 start, return -1; map->end =3D start + size; =20 - dso__kernel_module_get_build_id(map->dso, machine->root_dir); + dso__kernel_module_get_build_id(map__dso(map), machine->root_dir); =20 return 0; } @@ -1787,7 +1797,7 @@ static int machine__process_kernel_mmap_event(struct = machine *machine, map->end =3D map->start + xm->end - xm->start; =20 if (build_id__is_defined(bid)) - dso__set_build_id(map->dso, bid); + dso__set_build_id(map__dso(map), bid); =20 } else if (is_kernel_mmap) { const char *symbol_name =3D xm->name + strlen(mmap_name); @@ -2247,18 +2257,20 @@ static char *callchain_srcline(struct map_symbol *m= s, u64 ip) { struct map *map =3D ms->map; char *srcline =3D NULL; + struct dso *dso; =20 if (!map || callchain_param.key =3D=3D CCKEY_FUNCTION) return srcline; =20 - srcline =3D srcline__tree_find(&map->dso->srclines, ip); + dso =3D map__dso(map); + srcline =3D srcline__tree_find(&dso->srclines, ip); if (!srcline) { bool show_sym =3D false; bool show_addr =3D callchain_param.key =3D=3D CCKEY_ADDRESS; =20 - srcline =3D get_srcline(map->dso, map__rip_2objdump(map, ip), + srcline =3D get_srcline(dso, map__rip_2objdump(map, ip), ms->sym, show_sym, show_addr, ip); - srcline__tree_insert(&map->dso->srclines, ip, srcline); + srcline__tree_insert(&dso->srclines, ip, srcline); } =20 return srcline; @@ -3034,6 +3046,7 @@ static int append_inlines(struct callchain_cursor *cu= rsor, struct map_symbol *ms struct map *map =3D ms->map; struct inline_node *inline_node; struct inline_list *ilist; + struct dso *dso; u64 addr; int ret =3D 1; =20 @@ -3042,13 +3055,14 @@ static int append_inlines(struct callchain_cursor *= cursor, struct map_symbol *ms =20 addr =3D map__map_ip(map, ip); addr =3D map__rip_2objdump(map, addr); + dso =3D map__dso(map); =20 - inline_node =3D inlines__tree_find(&map->dso->inlined_nodes, addr); + inline_node =3D inlines__tree_find(&dso->inlined_nodes, addr); if (!inline_node) { - inline_node =3D dso__parse_addr_inlines(map->dso, addr, sym); + inline_node =3D dso__parse_addr_inlines(dso, addr, sym); if (!inline_node) return ret; - inlines__tree_insert(&map->dso->inlined_nodes, inline_node); + inlines__tree_insert(&dso->inlined_nodes, inline_node); } =20 list_for_each_entry(ilist, &inline_node->val, list) { @@ -3325,7 +3339,7 @@ char *machine__resolve_kernel_addr(void *vmachine, un= signed long long *addrp, ch if (sym =3D=3D NULL) return NULL; =20 - *modp =3D __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL; + *modp =3D __map__is_kmodule(map) ? (char *)map__dso(map)->short_name : NU= LL; *addrp =3D map->unmap_ip(map, sym->start); return sym->name; } diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index a99dbde656a2..90062af6675a 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -232,7 +232,7 @@ struct map *map__new2(u64 start, struct dso *dso) =20 bool __map__is_kernel(const struct map *map) { - if (!map->dso->kernel) + if (!map__dso(map)->kernel) return false; return machine__kernel_map(maps__machine(map__kmaps((struct map *)map))) = =3D=3D map; } @@ -247,8 +247,9 @@ bool __map__is_extra_kernel_map(const struct map *map) bool __map__is_bpf_prog(const struct map *map) { const char *name; + struct dso *dso =3D map__dso(map); =20 - if (map->dso->binary_type =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO) + if (dso->binary_type =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO) return true; =20 /* @@ -256,15 +257,16 @@ bool __map__is_bpf_prog(const struct map *map) * type of DSO_BINARY_TYPE__BPF_PROG_INFO. In such cases, we can * guess the type based on name. */ - name =3D map->dso->short_name; + name =3D dso->short_name; return name && (strstr(name, "bpf_prog_") =3D=3D name); } =20 bool __map__is_bpf_image(const struct map *map) { const char *name; + struct dso *dso =3D map__dso(map); =20 - if (map->dso->binary_type =3D=3D DSO_BINARY_TYPE__BPF_IMAGE) + if (dso->binary_type =3D=3D DSO_BINARY_TYPE__BPF_IMAGE) return true; =20 /* @@ -272,18 +274,20 @@ bool __map__is_bpf_image(const struct map *map) * type of DSO_BINARY_TYPE__BPF_IMAGE. In such cases, we can * guess the type based on name. */ - name =3D map->dso->short_name; + name =3D dso->short_name; return name && is_bpf_image(name); } =20 bool __map__is_ool(const struct map *map) { - return map->dso && map->dso->binary_type =3D=3D DSO_BINARY_TYPE__OOL; + const struct dso *dso =3D map__dso(map); + + return dso && dso->binary_type =3D=3D DSO_BINARY_TYPE__OOL; } =20 bool map__has_symbols(const struct map *map) { - return dso__has_symbols(map->dso); + return dso__has_symbols(map__dso(map)); } =20 static void map__exit(struct map *map) @@ -306,18 +310,23 @@ void map__put(struct map *map) =20 void map__fixup_start(struct map *map) { - struct rb_root_cached *symbols =3D &map->dso->symbols; + struct dso *dso =3D map__dso(map); + struct rb_root_cached *symbols =3D &dso->symbols; struct rb_node *nd =3D rb_first_cached(symbols); + if (nd !=3D NULL) { struct symbol *sym =3D rb_entry(nd, struct symbol, rb_node); + map->start =3D sym->start; } } =20 void map__fixup_end(struct map *map) { - struct rb_root_cached *symbols =3D &map->dso->symbols; + struct dso *dso =3D map__dso(map); + struct rb_root_cached *symbols =3D &dso->symbols; struct rb_node *nd =3D rb_last(&symbols->rb_root); + if (nd !=3D NULL) { struct symbol *sym =3D rb_entry(nd, struct symbol, rb_node); map->end =3D sym->end; @@ -328,18 +337,19 @@ void map__fixup_end(struct map *map) =20 int map__load(struct map *map) { - const char *name =3D map->dso->long_name; + struct dso *dso =3D map__dso(map); + const char *name =3D dso->long_name; int nr; =20 - if (dso__loaded(map->dso)) + if (dso__loaded(dso)) return 0; =20 - nr =3D dso__load(map->dso, map); + nr =3D dso__load(dso, map); if (nr < 0) { - if (map->dso->has_build_id) { + if (dso->has_build_id) { char sbuild_id[SBUILD_ID_SIZE]; =20 - build_id__sprintf(&map->dso->bid, sbuild_id); + build_id__sprintf(&dso->bid, sbuild_id); pr_debug("%s with build id %s not found", name, sbuild_id); } else pr_debug("Failed to open %s", name); @@ -371,32 +381,36 @@ struct symbol *map__find_symbol(struct map *map, u64 = addr) if (map__load(map) < 0) return NULL; =20 - return dso__find_symbol(map->dso, addr); + return dso__find_symbol(map__dso(map), addr); } =20 struct symbol *map__find_symbol_by_name(struct map *map, const char *name) { + struct dso *dso; + if (map__load(map) < 0) return NULL; =20 - if (!dso__sorted_by_name(map->dso)) - dso__sort_by_name(map->dso); + dso =3D map__dso(map); + if (!dso__sorted_by_name(dso)) + dso__sort_by_name(dso); =20 - return dso__find_symbol_by_name(map->dso, name); + return dso__find_symbol_by_name(dso, name); } =20 struct map *map__clone(struct map *from) { size_t size =3D sizeof(struct map); struct map *map; + struct dso *dso =3D map__dso(from); =20 - if (from->dso && from->dso->kernel) + if (dso && dso->kernel) size +=3D sizeof(struct kmap); =20 map =3D memdup(from, size); if (map !=3D NULL) { refcount_set(&map->refcnt, 1); - dso__get(map->dso); + dso__get(dso); } =20 return map; @@ -404,20 +418,23 @@ struct map *map__clone(struct map *from) =20 size_t map__fprintf(struct map *map, FILE *fp) { + const struct dso *dso =3D map__dso(map); + return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", - map->start, map->end, map->pgoff, map->dso->name); + map->start, map->end, map->pgoff, dso->name); } =20 size_t map__fprintf_dsoname(struct map *map, FILE *fp) { char buf[symbol_conf.pad_output_len_dso + 1]; const char *dsoname =3D "[unknown]"; + const struct dso *dso =3D map ? map__dso(map) : NULL; =20 - if (map && map->dso) { - if (symbol_conf.show_kernel_path && map->dso->long_name) - dsoname =3D map->dso->long_name; + if (dso) { + if (symbol_conf.show_kernel_path && dso->long_name) + dsoname =3D dso->long_name; else - dsoname =3D map->dso->name; + dsoname =3D dso->name; } =20 if (symbol_conf.pad_output_len_dso) { @@ -432,15 +449,17 @@ char *map__srcline(struct map *map, u64 addr, struct = symbol *sym) { if (map =3D=3D NULL) return SRCLINE_UNKNOWN; - return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, tru= e, addr); + + return get_srcline(map__dso(map), map__rip_2objdump(map, addr), sym, true= , true, addr); } =20 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix, FILE *fp) { + const struct dso *dso =3D map ? map__dso(map) : NULL; int ret =3D 0; =20 - if (map && map->dso) { + if (dso) { char *srcline =3D map__srcline(map, addr, NULL); if (strncmp(srcline, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) !=3D 0) ret =3D fprintf(fp, "%s%s", prefix, srcline); @@ -469,6 +488,7 @@ void srccode_state_free(struct srccode_state *state) u64 map__rip_2objdump(struct map *map, u64 rip) { struct kmap *kmap =3D __map__kmap(map); + const struct dso *dso =3D map__dso(map); =20 /* * vmlinux does not have program headers for PTI entry trampolines and @@ -486,18 +506,18 @@ u64 map__rip_2objdump(struct map *map, u64 rip) } } =20 - if (!map->dso->adjust_symbols) + if (!dso->adjust_symbols) return rip; =20 - if (map->dso->rel) + if (dso->rel) return rip - map->pgoff; =20 /* * kernel modules also have DSO_TYPE_USER in dso->kernel, * but all kernel modules are ET_REL, so won't get here. */ - if (map->dso->kernel =3D=3D DSO_SPACE__USER) - return rip + map->dso->text_offset; + if (dso->kernel =3D=3D DSO_SPACE__USER) + return rip + dso->text_offset; =20 return map->unmap_ip(map, rip) - map->reloc; } @@ -516,18 +536,20 @@ u64 map__rip_2objdump(struct map *map, u64 rip) */ u64 map__objdump_2mem(struct map *map, u64 ip) { - if (!map->dso->adjust_symbols) + const struct dso *dso =3D map__dso(map); + + if (!dso->adjust_symbols) return map->unmap_ip(map, ip); =20 - if (map->dso->rel) + if (dso->rel) return map->unmap_ip(map, ip + map->pgoff); =20 /* * kernel modules also have DSO_TYPE_USER in dso->kernel, * but all kernel modules are ET_REL, so won't get here. */ - if (map->dso->kernel =3D=3D DSO_SPACE__USER) - return map->unmap_ip(map, ip - map->dso->text_offset); + if (dso->kernel =3D=3D DSO_SPACE__USER) + return map->unmap_ip(map, ip - dso->text_offset); =20 return ip + map->reloc; } @@ -541,7 +563,9 @@ bool map__contains_symbol(const struct map *map, const = struct symbol *sym) =20 struct kmap *__map__kmap(struct map *map) { - if (!map->dso || !map->dso->kernel) + const struct dso *dso =3D map__dso(map); + + if (!dso || !dso->kernel) return NULL; return (struct kmap *)(map + 1); } diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index d1a6f85fd31d..36c5add0144d 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -47,6 +47,11 @@ u64 map__unmap_ip(const struct map *map, u64 ip); /* Returns ip */ u64 identity__map_ip(const struct map *map __maybe_unused, u64 ip); =20 +static inline struct dso *map__dso(const struct map *map) +{ + return map->dso; +} + static inline size_t map__size(const struct map *map) { return map->end - map->start; @@ -69,7 +74,7 @@ struct thread; * Note: caller must ensure map->dso is not NULL (map is loaded). */ #define map__for_each_symbol(map, pos, n) \ - dso__for_each_symbol(map->dso, pos, n) + dso__for_each_symbol(map__dso(map), pos, n) =20 /* map__for_each_symbol_with_name - iterate over the symbols in the given = map * that have the given name diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 91bb015caede..09ec6bbafcbc 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -62,6 +62,7 @@ static int __maps__insert(struct maps *maps, struct map *= map) int maps__insert(struct maps *maps, struct map *map) { int err; + const struct dso *dso =3D map__dso(map); =20 down_write(maps__lock(maps)); err =3D __maps__insert(maps, map); @@ -70,7 +71,7 @@ int maps__insert(struct maps *maps, struct map *map) =20 ++maps->nr_maps; =20 - if (map->dso && map->dso->kernel) { + if (dso && dso->kernel) { struct kmap *kmap =3D map__kmap(map); =20 if (kmap) @@ -253,7 +254,7 @@ size_t maps__fprintf(struct maps *maps, FILE *fp) printed +=3D fprintf(fp, "Map:"); printed +=3D map__fprintf(pos->map, fp); if (verbose > 2) { - printed +=3D dso__fprintf(pos->map->dso, fp); + printed +=3D dso__fprintf(map__dso(pos->map), fp); printed +=3D fprintf(fp, "--\n"); } } @@ -307,7 +308,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) =20 if (use_browser) { pr_debug("overlapping maps in %s (disable tui for more info)\n", - map->dso->name); + map__dso(map)->name); } else { fputs("overlapping maps:\n", fp); map__fprintf(map, fp); diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index cdf5d655d84c..b26670a26005 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -165,8 +165,9 @@ static struct map *kernel_get_module_map(const char *mo= dule) =20 maps__for_each_entry(maps, pos) { /* short_name is "[module]" */ - const char *short_name =3D pos->map->dso->short_name; - u16 short_name_len =3D pos->map->dso->short_name_len; + struct dso *dso =3D map__dso(pos->map); + const char *short_name =3D dso->short_name; + u16 short_name_len =3D dso->short_name_len; =20 if (strncmp(short_name + 1, module, short_name_len - 2) =3D=3D 0 && @@ -182,13 +183,15 @@ struct map *get_target_map(const char *target, struct= nsinfo *nsi, bool user) /* Init maps of given executable or kernel */ if (user) { struct map *map; + struct dso *dso; =20 map =3D dso__new_map(target); - if (map && map->dso) { - mutex_lock(&map->dso->lock); - nsinfo__put(map->dso->nsinfo); - map->dso->nsinfo =3D nsinfo__get(nsi); - mutex_unlock(&map->dso->lock); + dso =3D map ? map__dso(map) : NULL; + if (dso) { + mutex_lock(&dso->lock); + nsinfo__put(dso->nsinfo); + dso->nsinfo =3D nsinfo__get(nsi); + mutex_unlock(&dso->lock); } return map; } else { @@ -341,7 +344,7 @@ static int kernel_get_module_dso(const char *module, st= ruct dso **pdso) snprintf(module_name, sizeof(module_name), "[%s]", module); map =3D maps__find_by_name(machine__kernel_maps(host_machine), module_na= me); if (map) { - dso =3D map->dso; + dso =3D map__dso(map); goto found; } pr_debug("Failed to find module %s.\n", module); @@ -349,7 +352,7 @@ static int kernel_get_module_dso(const char *module, st= ruct dso **pdso) } =20 map =3D machine__kernel_map(host_machine); - dso =3D map->dso; + dso =3D map__dso(map); if (!dso->has_build_id) dso__read_running_kernel_build_id(dso, host_machine); =20 @@ -3737,6 +3740,7 @@ int show_available_funcs(const char *target, struct n= sinfo *nsi, { struct rb_node *nd; struct map *map; + struct dso *dso; int ret; =20 ret =3D init_probe_symbol_maps(user); @@ -3762,14 +3766,14 @@ int show_available_funcs(const char *target, struct= nsinfo *nsi, (target) ? : "kernel"); goto end; } - if (!dso__sorted_by_name(map->dso)) - dso__sort_by_name(map->dso); + dso =3D map__dso(map); + if (!dso__sorted_by_name(dso)) + dso__sort_by_name(dso); =20 /* Show all (filtered) symbols */ setup_pager(); =20 - for (nd =3D rb_first_cached(&map->dso->symbol_names); nd; - nd =3D rb_next(nd)) { + for (nd =3D rb_first_cached(&dso->symbol_names); nd; nd =3D rb_next(nd)) { struct symbol_name_rb_node *pos =3D rb_entry(nd, struct symbol_name_rb_n= ode, rb_node); =20 if (strfilter__compare(_filter, pos->sym.name)) diff --git a/tools/perf/util/scripting-engines/trace-event-perl.c b/tools/p= erf/util/scripting-engines/trace-event-perl.c index 83fd2fd0ba16..039d0365ad41 100644 --- a/tools/perf/util/scripting-engines/trace-event-perl.c +++ b/tools/perf/util/scripting-engines/trace-event-perl.c @@ -315,12 +315,14 @@ static SV *perl_process_callchain(struct perf_sample = *sample, =20 if (node->ms.map) { struct map *map =3D node->ms.map; + struct dso *dso =3D map ? map__dso(map) : NULL; const char *dsoname =3D "[unknown]"; - if (map && map->dso) { - if (symbol_conf.show_kernel_path && map->dso->long_name) - dsoname =3D map->dso->long_name; + + if (dso) { + if (symbol_conf.show_kernel_path && dso->long_name) + dsoname =3D dso->long_name; else - dsoname =3D map->dso->name; + dsoname =3D dso->name; } if (!hv_stores(elem, "dso", newSVpv(dsoname,0))) { hv_undef(elem); diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index e5cc18f6fcda..b8e5c6f61d80 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -390,12 +390,13 @@ static PyObject *get_field_numeric_entry(struct tep_e= vent *event, static const char *get_dsoname(struct map *map) { const char *dsoname =3D "[unknown]"; + struct dso *dso =3D map ? map__dso(map) : NULL; =20 - if (map && map->dso) { - if (symbol_conf.show_kernel_path && map->dso->long_name) - dsoname =3D map->dso->long_name; + if (dso) { + if (symbol_conf.show_kernel_path && dso->long_name) + dsoname =3D dso->long_name; else - dsoname =3D map->dso->name; + dsoname =3D dso->name; } =20 return dsoname; @@ -780,9 +781,10 @@ static void set_sym_in_dict(PyObject *dict, struct add= r_location *al, char sbuild_id[SBUILD_ID_SIZE]; =20 if (al->map) { - pydict_set_item_string_decref(dict, dso_field, - _PyUnicode_FromString(al->map->dso->name)); - build_id__sprintf(&al->map->dso->bid, sbuild_id); + struct dso *dso =3D map__dso(al->map); + + pydict_set_item_string_decref(dict, dso_field, _PyUnicode_FromString(dso= ->name)); + build_id__sprintf(&dso->bid, sbuild_id); pydict_set_item_string_decref(dict, dso_bid_field, _PyUnicode_FromString(sbuild_id)); pydict_set_item_string_decref(dict, dso_map_start, diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index e04d9bddba11..d7b6b734bf90 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c @@ -184,8 +184,8 @@ struct sort_entry sort_comm =3D { =20 static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r) { - struct dso *dso_l =3D map_l ? map_l->dso : NULL; - struct dso *dso_r =3D map_r ? map_r->dso : NULL; + struct dso *dso_l =3D map_l ? map__dso(map_l) : NULL; + struct dso *dso_r =3D map_r ? map__dso(map_r) : NULL; const char *dso_name_l, *dso_name_r; =20 if (!dso_l || !dso_r) @@ -211,13 +211,13 @@ sort__dso_cmp(struct hist_entry *left, struct hist_en= try *right) static int _hist_entry__dso_snprintf(struct map *map, char *bf, size_t size, unsigned int width) { - if (map && map->dso) { - const char *dso_name =3D verbose > 0 ? map->dso->long_name : - map->dso->short_name; - return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name); - } + const struct dso *dso =3D map ? map__dso(map) : NULL; + const char *dso_name =3D "[unknown]"; + + if (dso) + dso_name =3D verbose > 0 ? dso->long_name : dso->short_name; =20 - return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]"); + return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name); } =20 static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf, @@ -233,7 +233,7 @@ static int hist_entry__dso_filter(struct hist_entry *he= , int type, const void *a if (type !=3D HIST_FILTER__DSO) return -1; =20 - return dso && (!he->ms.map || he->ms.map->dso !=3D dso); + return dso && (!he->ms.map || map__dso(he->ms.map) !=3D dso); } =20 struct sort_entry sort_dso =3D { @@ -313,11 +313,11 @@ static int _hist_entry__sym_snprintf(struct map_symbo= l *ms, size_t ret =3D 0; =20 if (verbose > 0) { - char o =3D map ? dso__symtab_origin(map->dso) : '!'; + struct dso *dso =3D map ? map__dso(map) : NULL; + char o =3D dso ? dso__symtab_origin(dso) : '!'; u64 rip =3D ip; =20 - if (map && map->dso && map->dso->kernel - && map->dso->adjust_symbols) + if (dso && dso->kernel && dso->adjust_symbols) rip =3D map->unmap_ip(map, ip); =20 ret +=3D repsep_snprintf(bf, size, "%-#*llx %c ", @@ -595,7 +595,7 @@ static char *hist_entry__get_srcfile(struct hist_entry = *e) if (!map) return no_srcfile; =20 - sf =3D __get_srcline(map->dso, map__rip_2objdump(map, e->ip), + sf =3D __get_srcline(map__dso(map), map__rip_2objdump(map, e->ip), e->ms.sym, false, true, true, e->ip); if (!strcmp(sf, SRCLINE_UNKNOWN)) return no_srcfile; @@ -941,7 +941,7 @@ static int hist_entry__dso_from_filter(struct hist_entr= y *he, int type, return -1; =20 return dso && (!he->branch_info || !he->branch_info->from.ms.map || - he->branch_info->from.ms.map->dso !=3D dso); + map__dso(he->branch_info->from.ms.map) !=3D dso); } =20 static int64_t @@ -973,7 +973,7 @@ static int hist_entry__dso_to_filter(struct hist_entry = *he, int type, return -1; =20 return dso && (!he->branch_info || !he->branch_info->to.ms.map || - he->branch_info->to.ms.map->dso !=3D dso); + map__dso(he->branch_info->to.ms.map) !=3D dso); } =20 static int64_t @@ -1465,6 +1465,7 @@ sort__dcacheline_cmp(struct hist_entry *left, struct = hist_entry *right) { u64 l, r; struct map *l_map, *r_map; + struct dso *l_dso, *r_dso; int rc; =20 if (!left->mem_info) return -1; @@ -1484,7 +1485,9 @@ sort__dcacheline_cmp(struct hist_entry *left, struct = hist_entry *right) if (!l_map) return -1; if (!r_map) return 1; =20 - rc =3D dso__cmp_id(l_map->dso, r_map->dso); + l_dso =3D map__dso(l_map); + r_dso =3D map__dso(r_map); + rc =3D dso__cmp_id(l_dso, r_dso); if (rc) return rc; /* @@ -1496,9 +1499,8 @@ sort__dcacheline_cmp(struct hist_entry *left, struct = hist_entry *right) */ =20 if ((left->cpumode !=3D PERF_RECORD_MISC_KERNEL) && - (!(l_map->flags & MAP_SHARED)) && - !l_map->dso->id.maj && !l_map->dso->id.min && - !l_map->dso->id.ino && !l_map->dso->id.ino_generation) { + (!(l_map->flags & MAP_SHARED)) && !l_dso->id.maj && !l_dso->id.min && + !l_dso->id.ino && !l_dso->id.ino_generation) { /* userspace anonymous */ =20 if (left->thread->pid_ > right->thread->pid_) return -1; @@ -1526,6 +1528,7 @@ static int hist_entry__dcacheline_snprintf(struct his= t_entry *he, char *bf, =20 if (he->mem_info) { struct map *map =3D he->mem_info->daddr.ms.map; + struct dso *dso =3D map__dso(map); =20 addr =3D cl_address(he->mem_info->daddr.al_addr, chk_double_cl); ms =3D &he->mem_info->daddr.ms; @@ -1534,8 +1537,7 @@ static int hist_entry__dcacheline_snprintf(struct his= t_entry *he, char *bf, if ((he->cpumode !=3D PERF_RECORD_MISC_KERNEL) && map && !(map->prot & PROT_EXEC) && (map->flags & MAP_SHARED) && - (map->dso->id.maj || map->dso->id.min || - map->dso->id.ino || map->dso->id.ino_generation)) + (dso->id.maj || dso->id.min || dso->id.ino || dso->id.ino_generation= )) level =3D 's'; else if (!map) level =3D 'X'; @@ -2031,9 +2033,8 @@ sort__dso_size_cmp(struct hist_entry *left, struct hi= st_entry *right) static int _hist_entry__dso_size_snprintf(struct map *map, char *bf, size_t bf_size, unsigned int width) { - if (map && map->dso) - return repsep_snprintf(bf, bf_size, "%*d", width, - map__size(map)); + if (map && map__dso(map)) + return repsep_snprintf(bf, bf_size, "%*d", width, map__size(map)); =20 return repsep_snprintf(bf, bf_size, "%*s", width, "unknown"); } diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index ccdafc3971ac..97085ad7fe9b 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1429,7 +1429,7 @@ static int dso__process_kernel_symbol(struct dso *dso= , struct map *map, *curr_mapp =3D curr_map; *curr_dsop =3D curr_dso; } else - *curr_dsop =3D curr_map->dso; + *curr_dsop =3D map__dso(curr_map); =20 return 0; } diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 56b3775473d4..7d92bd62c98a 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -786,6 +786,7 @@ static int maps__split_kallsyms_for_kcore(struct maps *= kmaps, struct dso *dso) *root =3D RB_ROOT_CACHED; =20 while (next) { + struct dso *curr_map_dso; char *module; =20 pos =3D rb_entry(next, struct symbol, rb_node); @@ -803,13 +804,13 @@ static int maps__split_kallsyms_for_kcore(struct maps= *kmaps, struct dso *dso) symbol__delete(pos); continue; } - + curr_map_dso =3D map__dso(curr_map); pos->start -=3D curr_map->start - curr_map->pgoff; if (pos->end > curr_map->end) pos->end =3D curr_map->end; if (pos->end) pos->end -=3D curr_map->start - curr_map->pgoff; - symbols__insert(&curr_map->dso->symbols, pos); + symbols__insert(&curr_map_dso->symbols, pos); ++count; } =20 @@ -851,12 +852,14 @@ static int maps__split_kallsyms(struct maps *kmaps, s= truct dso *dso, u64 delta, =20 module =3D strchr(pos->name, '\t'); if (module) { + struct dso *curr_map_dso; + if (!symbol_conf.use_modules) goto discard_symbol; =20 *module++ =3D '\0'; - - if (strcmp(curr_map->dso->short_name, module)) { + curr_map_dso =3D map__dso(curr_map); + if (strcmp(curr_map_dso->short_name, module)) { if (curr_map !=3D initial_map && dso->kernel =3D=3D DSO_SPACE__KERNEL_GUEST && machine__is_default_guest(machine)) { @@ -867,7 +870,7 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, * symbols are in its kmap. Mark it as * loaded. */ - dso__set_loaded(curr_map->dso); + dso__set_loaded(curr_map_dso); } =20 curr_map =3D maps__find_by_name(kmaps, module); @@ -879,8 +882,8 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, curr_map =3D initial_map; goto discard_symbol; } - - if (curr_map->dso->loaded && + curr_map_dso =3D map__dso(curr_map); + if (curr_map_dso->loaded && !machine__is_default_guest(machine)) goto discard_symbol; } @@ -949,8 +952,10 @@ static int maps__split_kallsyms(struct maps *kmaps, st= ruct dso *dso, u64 delta, } add_symbol: if (curr_map !=3D initial_map) { + struct dso *curr_map_dso =3D map__dso(curr_map); + rb_erase_cached(&pos->rb_node, root); - symbols__insert(&curr_map->dso->symbols, pos); + symbols__insert(&curr_map_dso->symbols, pos); ++moved; } else ++count; @@ -964,7 +969,7 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, if (curr_map !=3D initial_map && dso->kernel =3D=3D DSO_SPACE__KERNEL_GUEST && machine__is_default_guest(maps__machine(kmaps))) { - dso__set_loaded(curr_map->dso); + dso__set_loaded(map__dso(curr_map)); } =20 return count + moved; @@ -1138,13 +1143,14 @@ static int do_validate_kcore_modules(const char *fi= lename, struct maps *kmaps) maps__for_each_entry(kmaps, old_node) { struct map *old_map =3D old_node->map; struct module_info *mi; + struct dso *dso; =20 if (!__map__is_kmodule(old_map)) { continue; } - + dso =3D map__dso(old_map); /* Module must be in memory at the same address */ - mi =3D find_module(old_map->dso->short_name, &modules); + mi =3D find_module(dso->short_name, &modules); if (!mi || mi->start !=3D old_map->start) { err =3D -EINVAL; goto out; @@ -2042,14 +2048,17 @@ int dso__load(struct dso *dso, struct map *map) =20 static int map__strcmp(const void *a, const void *b) { - const struct map *ma =3D *(const struct map **)a, *mb =3D *(const struct = map **)b; - return strcmp(ma->dso->short_name, mb->dso->short_name); + const struct dso *dso_a =3D map__dso(*(const struct map **)a); + const struct dso *dso_b =3D map__dso(*(const struct map **)b); + + return strcmp(dso_a->short_name, dso_b->short_name); } =20 static int map__strcmp_name(const void *name, const void *b) { - const struct map *map =3D *(const struct map **)b; - return strcmp(name, map->dso->short_name); + const struct dso *dso =3D map__dso(*(const struct map **)b); + + return strcmp(name, dso->short_name); } =20 void __maps__sort_by_name(struct maps *maps) @@ -2106,10 +2115,13 @@ struct map *maps__find_by_name(struct maps *maps, c= onst char *name) =20 down_read(maps__lock(maps)); =20 - if (maps->last_search_by_name && - strcmp(maps->last_search_by_name->dso->short_name, name) =3D=3D 0) { - map =3D maps->last_search_by_name; - goto out_unlock; + if (maps->last_search_by_name) { + const struct dso *dso =3D map__dso(maps->last_search_by_name); + + if (strcmp(dso->short_name, name) =3D=3D 0) { + map =3D maps->last_search_by_name; + goto out_unlock; + } } /* * If we have maps->maps_by_name, then the name isn't in the rbtree, @@ -2122,8 +2134,11 @@ struct map *maps__find_by_name(struct maps *maps, co= nst char *name) =20 /* Fallback to traversing the rbtree... */ maps__for_each_entry(maps, rb_node) { + struct dso *dso; + map =3D rb_node->map; - if (strcmp(map->dso->short_name, name) =3D=3D 0) { + dso =3D map__dso(map); + if (strcmp(dso->short_name, name) =3D=3D 0) { maps->last_search_by_name =3D map; goto out_unlock; } diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic= -events.c index 57b95c1d7e39..fbd1a882b013 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -693,12 +693,14 @@ int perf_event__synthesize_modules(struct perf_tool *= tool, perf_event__handler_t =20 maps__for_each_entry(maps, pos) { struct map *map =3D pos->map; + struct dso *dso; =20 if (!__map__is_kmodule(map)) continue; =20 + dso =3D map__dso(map); if (symbol_conf.buildid_mmap2) { - size =3D PERF_ALIGN(map->dso->long_name_len + 1, sizeof(u64)); + size =3D PERF_ALIGN(dso->long_name_len + 1, sizeof(u64)); event->mmap2.header.type =3D PERF_RECORD_MMAP2; event->mmap2.header.size =3D (sizeof(event->mmap2) - (sizeof(event->mmap2.filename) - size)); @@ -708,12 +710,11 @@ int perf_event__synthesize_modules(struct perf_tool *= tool, perf_event__handler_t event->mmap2.len =3D map->end - map->start; event->mmap2.pid =3D machine->pid; =20 - memcpy(event->mmap2.filename, map->dso->long_name, - map->dso->long_name_len + 1); + memcpy(event->mmap2.filename, dso->long_name, dso->long_name_len + 1); =20 perf_record_mmap2__read_build_id(&event->mmap2, machine, false); } else { - size =3D PERF_ALIGN(map->dso->long_name_len + 1, sizeof(u64)); + size =3D PERF_ALIGN(dso->long_name_len + 1, sizeof(u64)); event->mmap.header.type =3D PERF_RECORD_MMAP; event->mmap.header.size =3D (sizeof(event->mmap) - (sizeof(event->mmap.filename) - size)); @@ -723,8 +724,7 @@ int perf_event__synthesize_modules(struct perf_tool *to= ol, perf_event__handler_t event->mmap.len =3D map->end - map->start; event->mmap.pid =3D machine->pid; =20 - memcpy(event->mmap.filename, map->dso->long_name, - map->dso->long_name_len + 1); + memcpy(event->mmap.filename, dso->long_name, dso->long_name_len + 1); } =20 if (perf_tool__process_synth_event(tool, event, machine, process) !=3D 0= ) { diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 292585a52281..42fdc80a6f2e 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -448,23 +448,22 @@ struct thread *thread__main_thread(struct machine *ma= chine, struct thread *threa int thread__memcpy(struct thread *thread, struct machine *machine, void *buf, u64 ip, int len, bool *is64bit) { - u8 cpumode =3D PERF_RECORD_MISC_USER; - struct addr_location al; - long offset; + u8 cpumode =3D PERF_RECORD_MISC_USER; + struct addr_location al; + long offset; =20 - if (machine__kernel_ip(machine, ip)) - cpumode =3D PERF_RECORD_MISC_KERNEL; + if (machine__kernel_ip(machine, ip)) + cpumode =3D PERF_RECORD_MISC_KERNEL; =20 - if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso || - al.map->dso->data.status =3D=3D DSO_DATA_STATUS_ERROR || - map__load(al.map) < 0) - return -1; + if (!thread__find_map(thread, cpumode, ip, &al) || !map__dso(al.map) || + map__dso(al.map)->data.status =3D=3D DSO_DATA_STATUS_ERROR || map__load(= al.map) < 0) + return -1; =20 - offset =3D al.map->map_ip(al.map, ip); - if (is64bit) - *is64bit =3D al.map->dso->is_64_bit; + offset =3D al.map->map_ip(al.map, ip); + if (is64bit) + *is64bit =3D map__dso(al.map)->is_64_bit; =20 - return dso__data_read_offset(al.map->dso, machine, offset, buf, len= ); + return dso__data_read_offset(map__dso(al.map), machine, offset, buf, len); } =20 void thread__free_stitch_list(struct thread *thread) diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c index 94aa40f6e348..c8cba9d4bfd9 100644 --- a/tools/perf/util/unwind-libdw.c +++ b/tools/perf/util/unwind-libdw.c @@ -52,7 +52,7 @@ static int __report_module(struct addr_location *al, u64 = ip, thread__find_symbol(ui->thread, PERF_RECORD_MISC_USER, ip, al); =20 if (al->map) - dso =3D al->map->dso; + dso =3D map__dso(al->map); =20 if (!dso) return 0; @@ -134,17 +134,17 @@ static int access_dso_mem(struct unwind_info *ui, Dwa= rf_Addr addr, { struct addr_location al; ssize_t size; + struct dso *dso; =20 if (!thread__find_map(ui->thread, PERF_RECORD_MISC_USER, addr, &al)) { pr_debug("unwind: no map for %lx\n", (unsigned long)addr); return -1; } - - if (!al.map->dso) + dso =3D map__dso(al.map); + if (!dso) return -1; =20 - size =3D dso__data_read_addr(al.map->dso, al.map, ui->machine, - addr, (u8 *) data, sizeof(*data)); + size =3D dso__data_read_addr(dso, al.map, ui->machine, addr, (u8 *) data,= sizeof(*data)); =20 return !(size =3D=3D sizeof(*data)); } diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c index 835c39efb80d..ec777ee11493 100644 --- a/tools/perf/util/vdso.c +++ b/tools/perf/util/vdso.c @@ -147,7 +147,7 @@ static enum dso_type machine__thread_dso_type(struct ma= chine *machine, struct map_rb_node *rb_node; =20 maps__for_each_entry(thread->maps, rb_node) { - struct dso *dso =3D rb_node->map->dso; + struct dso *dso =3D map__dso(rb_node->map); =20 if (!dso || dso->long_name[0] !=3D '/') continue; --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 A308FC6FD1D for ; Mon, 20 Mar 2023 03:41:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229806AbjCTDlM (ORCPT ); Sun, 19 Mar 2023 23:41:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229713AbjCTDkV (ORCPT ); Sun, 19 Mar 2023 23:40:21 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7AAD22DC2 for ; Sun, 19 Mar 2023 20:39:55 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id d7-20020a25adc7000000b00953ffdfbe1aso11927657ybe.23 for ; Sun, 19 Mar 2023 20:39:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283595; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=X/A+UoSdYA6zofj2Avjp4/uhhBCheZU1OnzxaiqIHfQ=; b=gyBBhzvAkUE92RbO1mJRgl243lAd+Ge4lmraIbZG7J3+Eh2ndO0qbq/FT2p9fsouDR m9QS5qNXgB7CbalY3V2lOmJgxJh1DC5iKUSFKWV1gcnNZ5n3h2BCITGJEAXDHkBlw0Nh W5ciLPDXCZxG2A0/qijY+R7zz7Aed9rOQRR4ye2JRQV/um0YyW564WBTjo2YuhiWG1BD KhRoT1xZiTrrWjkR517IMdbyAo4wHPBRJzUKdpveWe6xUMM24T7J0PWppjLUBFq7dGyg vQFM14xlLoimtLwkWk911ZlpB0jAy9N2yoTSPNolSwl7AMWCQqpfelCAcP/2BxsYuqTV R53w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283595; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=X/A+UoSdYA6zofj2Avjp4/uhhBCheZU1OnzxaiqIHfQ=; b=iRbiztK8wEu4EqOwMerUIiX23nbaayVxmkNX4Zrz+hbtUKWOTsuCpzzvfdw09PDw38 iG6+QOyM3xYeVt/Ur7hfJAE7ok7WmEXkP3KSQTf+MVKJxamqrI5SWOBZOzCDAWQ/yuKM OyTQZrN4grpjNqkQN08tyZQ2CK+ezxI0lstX0n09NzbWD/wf4S9jlcWbz48XiCQ+A9UC GF4tl5daYdxIPf87D84emPl/UFOkSMFY92PTtBStFtSM/SJNRDA8yHe4nCSjCkYNq3b3 RVN1F2dP373tOD/E/5LwBGTDlw0lvz4meveJ7ALCbkJLP5Jp4kWDrXW247ai/1bEp+gf fMwg== X-Gm-Message-State: AO0yUKXXM3kukxpapur7SYnVOC1+FZ+6RJJ3zUPQ/Ehkilj2lrNrMZzg UB5P6GgplHNaFHYoDa8Fn/pka/TFoFRj X-Google-Smtp-Source: AK7set+iBDGhOKpBEtUU/eW4e+N8AgBVu14pF4Wmsk4SFH1VkwaT+uQQ1KkuEQ5bcsSwRnFqAN5bYpjvDCIP X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a81:a747:0:b0:545:1d7f:acbf with SMTP id e68-20020a81a747000000b005451d7facbfmr681762ywh.10.1679283594910; Sun, 19 Mar 2023 20:39:54 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:58 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-11-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 10/22] perf map: Add accessor for start and end From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Later changes will add reference count checking for struct map, start and end are frequently accessed variables. Add an accessor so that the reference count check is only necessary in one place. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/tests/dwarf-unwind.c | 2 +- tools/perf/arch/x86/util/event.c | 4 +- tools/perf/builtin-buildid-list.c | 2 +- tools/perf/builtin-report.c | 2 +- tools/perf/builtin-script.c | 2 +- tools/perf/builtin-top.c | 2 +- tools/perf/tests/code-reading.c | 8 +-- tools/perf/tests/maps.c | 4 +- tools/perf/tests/mmap-thread-lookup.c | 2 +- tools/perf/tests/vmlinux-kallsyms.c | 14 +++--- tools/perf/util/annotate.c | 4 +- tools/perf/util/dlfilter.c | 8 +-- tools/perf/util/intel-pt.c | 8 +-- tools/perf/util/machine.c | 14 +++--- tools/perf/util/map.c | 8 +-- tools/perf/util/map.h | 12 ++++- tools/perf/util/maps.c | 30 ++++++------ tools/perf/util/probe-event.c | 4 +- .../scripting-engines/trace-event-python.c | 6 +-- tools/perf/util/symbol-elf.c | 8 +-- tools/perf/util/symbol.c | 49 ++++++++++--------- tools/perf/util/symbol_fprintf.c | 2 +- tools/perf/util/synthetic-events.c | 16 +++--- tools/perf/util/unwind-libdw.c | 6 +-- 24 files changed, 114 insertions(+), 103 deletions(-) diff --git a/tools/perf/arch/x86/tests/dwarf-unwind.c b/tools/perf/arch/x86= /tests/dwarf-unwind.c index a54dea7c112f..497593be80f2 100644 --- a/tools/perf/arch/x86/tests/dwarf-unwind.c +++ b/tools/perf/arch/x86/tests/dwarf-unwind.c @@ -33,7 +33,7 @@ static int sample_ustack(struct perf_sample *sample, return -1; } =20 - stack_size =3D map->end - sp; + stack_size =3D map__end(map) - sp; stack_size =3D stack_size > STACK_SIZE ? STACK_SIZE : stack_size; =20 memcpy(buf, (void *) sp, stack_size); diff --git a/tools/perf/arch/x86/util/event.c b/tools/perf/arch/x86/util/ev= ent.c index 17bf60babfbd..3b2475707756 100644 --- a/tools/perf/arch/x86/util/event.c +++ b/tools/perf/arch/x86/util/event.c @@ -59,8 +59,8 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *= tool, =20 event->mmap.header.size =3D size; =20 - event->mmap.start =3D map->start; - event->mmap.len =3D map->end - map->start; + event->mmap.start =3D map__start(map); + event->mmap.len =3D map__size(map); event->mmap.pgoff =3D map->pgoff; event->mmap.pid =3D machine->pid; =20 diff --git a/tools/perf/builtin-buildid-list.c b/tools/perf/builtin-buildid= -list.c index cad9ed44ce7c..eea28cbcc0b7 100644 --- a/tools/perf/builtin-buildid-list.c +++ b/tools/perf/builtin-buildid-list.c @@ -30,7 +30,7 @@ static int buildid__map_cb(struct map *map, void *arg __m= aybe_unused) memset(bid_buf, 0, sizeof(bid_buf)); if (dso->has_build_id) build_id__sprintf(&dso->bid, bid_buf); - printf("%s %16" PRIx64 " %16" PRIx64, bid_buf, map->start, map->end); + printf("%s %16" PRIx64 " %16" PRIx64, bid_buf, map__start(map), map__end(= map)); if (dso->long_name !=3D NULL) { printf(" %s", dso->long_name); } else if (dso->short_name !=3D NULL) { diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 02ca87c13e91..4ce1aef3e253 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -847,7 +847,7 @@ static size_t maps__fprintf_task(struct maps *maps, int= indent, FILE *fp) const struct dso *dso =3D map__dso(map); =20 printed +=3D fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRI= x64 " %" PRIu64 " %s\n", - indent, "", map->start, map->end, + indent, "", map__start(map), map__end(map), map->prot & PROT_READ ? 'r' : '-', map->prot & PROT_WRITE ? 'w' : '-', map->prot & PROT_EXEC ? 'x' : '-', diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 9c7eb900ff7c..eb49689d0f00 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -1209,7 +1209,7 @@ static int ip__fprintf_sym(uint64_t addr, struct thre= ad *thread, if (al.addr < al.sym->end) off =3D al.addr - al.sym->start; else - off =3D al.addr - al.map->start - al.sym->start; + off =3D al.addr - map__start(al.map) - al.sym->start; printed +=3D fprintf(fp, "\t%s", al.sym->name); if (off) printed +=3D fprintf(fp, "%+d", off); diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 5010eee8fbae..b45565f718f4 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -183,7 +183,7 @@ static void ui__warn_map_erange(struct map *map, struct= symbol *sym, u64 ip) "Not all samples will be on the annotation output.\n\n" "Please report to linux-kernel@vger.kernel.org\n", ip, dso->long_name, dso__symtab_origin(dso), - map->start, map->end, sym->start, sym->end, + map__start(map), map__end(map), sym->start, sym->end, sym->binding =3D=3D STB_GLOBAL ? 'g' : sym->binding =3D=3D STB_LOCAL ? 'l' : 'w', sym->name, err ? "[unknown]" : uts.machine, diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-readin= g.c index 936c61546e64..1545fcaa95c6 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -265,8 +265,8 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, len =3D BUFSZ; =20 /* Do not go off the map */ - if (addr + len > al.map->end) - len =3D al.map->end - addr; + if (addr + len > map__end(al.map)) + len =3D map__end(al.map) - addr; =20 /* Read the object code using perf */ ret_len =3D dso__data_read_offset(dso, maps__machine(thread->maps), @@ -291,7 +291,7 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, size_t d; =20 for (d =3D 0; d < state->done_cnt; d++) { - if (state->done[d] =3D=3D al.map->start) { + if (state->done[d] =3D=3D map__start(al.map)) { pr_debug("kcore map tested already"); pr_debug(" - skipping\n"); goto out; @@ -301,7 +301,7 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, pr_debug("Too many kcore maps - skipping\n"); goto out; } - state->done[state->done_cnt++] =3D al.map->start; + state->done[state->done_cnt++] =3D map__start(al.map); } =20 objdump_name =3D dso->long_name; diff --git a/tools/perf/tests/maps.c b/tools/perf/tests/maps.c index ae7028fbf79e..fd0c464fcf95 100644 --- a/tools/perf/tests/maps.c +++ b/tools/perf/tests/maps.c @@ -24,8 +24,8 @@ static int check_maps(struct map_def *merged, unsigned in= t size, struct maps *ma if (i > 0) TEST_ASSERT_VAL("less maps expected", (map && i < size) || (!map && i = =3D=3D size)); =20 - TEST_ASSERT_VAL("wrong map start", map->start =3D=3D merged[i].start); - TEST_ASSERT_VAL("wrong map end", map->end =3D=3D merged[i].end); + TEST_ASSERT_VAL("wrong map start", map__start(map) =3D=3D merged[i].sta= rt); + TEST_ASSERT_VAL("wrong map end", map__end(map) =3D=3D merged[i].end); TEST_ASSERT_VAL("wrong map name", !strcmp(map__dso(map)->name, merged[i= ].name)); TEST_ASSERT_VAL("wrong map refcnt", refcount_read(&map->refcnt) =3D=3D 1= ); =20 diff --git a/tools/perf/tests/mmap-thread-lookup.c b/tools/perf/tests/mmap-= thread-lookup.c index a4301fc7b770..5cc4644e353d 100644 --- a/tools/perf/tests/mmap-thread-lookup.c +++ b/tools/perf/tests/mmap-thread-lookup.c @@ -202,7 +202,7 @@ static int mmap_events(synth_cb synth) break; } =20 - pr_debug("map %p, addr %" PRIx64 "\n", al.map, al.map->start); + pr_debug("map %p, addr %" PRIx64 "\n", al.map, map__start(al.map)); } =20 machine__delete_threads(machine); diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux= -kallsyms.c index c614c2db7e89..0a75623172c2 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -267,7 +267,7 @@ static int test__vmlinux_matches_kallsyms(struct test_s= uite *test __maybe_unused =20 continue; } - } else if (mem_start =3D=3D kallsyms.vmlinux_map->end) { + } else if (mem_start =3D=3D map__end(kallsyms.vmlinux_map)) { /* * Ignore aliases to _etext, i.e. to the end of the kernel text area, * such as __indirect_thunk_end. @@ -319,14 +319,14 @@ static int test__vmlinux_matches_kallsyms(struct test= _suite *test __maybe_unused maps__for_each_entry(maps, rb_node) { struct map *pair, *map =3D rb_node->map; =20 - mem_start =3D vmlinux_map->unmap_ip(vmlinux_map, map->start); - mem_end =3D vmlinux_map->unmap_ip(vmlinux_map, map->end); + mem_start =3D vmlinux_map->unmap_ip(vmlinux_map, map__start(map)); + mem_end =3D vmlinux_map->unmap_ip(vmlinux_map, map__end(map)); =20 pair =3D maps__find(kallsyms.kmaps, mem_start); if (pair =3D=3D NULL || pair->priv) continue; =20 - if (pair->start =3D=3D mem_start) { + if (map__start(pair) =3D=3D mem_start) { struct dso *dso =3D map__dso(map); =20 if (!header_printed) { @@ -335,10 +335,10 @@ static int test__vmlinux_matches_kallsyms(struct test= _suite *test __maybe_unused } =20 pr_info("WARN: %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as", - map->start, map->end, map->pgoff, dso->name); - if (mem_end !=3D pair->end) + map__start(map), map__end(map), map->pgoff, dso->name); + if (mem_end !=3D map__end(pair)) pr_info(":\nWARN: *%" PRIx64 "-%" PRIx64 " %" PRIx64, - pair->start, pair->end, pair->pgoff); + map__start(pair), map__end(pair), pair->pgoff); pr_info(" %s\n", dso->name); pair->priv =3D 1; } diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 9494b34e84fc..f60f5efb2ad9 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1016,13 +1016,13 @@ int addr_map_symbol__account_cycles(struct addr_map= _symbol *ams, if (start && (start->ms.sym =3D=3D ams->ms.sym || (ams->ms.sym && - start->addr =3D=3D ams->ms.sym->start + ams->ms.map->start))) + start->addr =3D=3D ams->ms.sym->start + map__start(ams->ms.map)))) saddr =3D start->al_addr; if (saddr =3D=3D 0) pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx6= 4" saddr %"PRIx64"\n", ams->addr, start ? start->addr : 0, - ams->ms.sym ? ams->ms.sym->start + ams->ms.map->start : 0, + ams->ms.sym ? ams->ms.sym->start + map__start(ams->ms.map) : 0, saddr); err =3D symbol__account_cycles(ams->al_addr, saddr, ams->ms.sym, cycles); if (err) diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c index 8a7ffe0d805a..fe401fa4be02 100644 --- a/tools/perf/util/dlfilter.c +++ b/tools/perf/util/dlfilter.c @@ -51,7 +51,7 @@ static void al_to_d_al(struct addr_location *al, struct p= erf_dlfilter_al *d_al) if (al->addr < sym->end) d_al->symoff =3D al->addr - sym->start; else - d_al->symoff =3D al->addr - al->map->start - sym->start; + d_al->symoff =3D al->addr - map__start(al->map) - sym->start; d_al->sym_binding =3D sym->binding; } else { d_al->sym =3D NULL; @@ -268,7 +268,7 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip,= void *buf, __u32 len) =20 map =3D al->map; =20 - if (map && ip >=3D map->start && ip < map->end && + if (map && ip >=3D map__start(map) && ip < map__end(map) && machine__kernel_ip(d->machine, ip) =3D=3D machine__kernel_ip(d->machi= ne, d->sample->ip)) goto have_map; =20 @@ -279,8 +279,8 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip,= void *buf, __u32 len) map =3D a.map; have_map: offset =3D map->map_ip(map, ip); - if (ip + len >=3D map->end) - len =3D map->end - ip; + if (ip + len >=3D map__end(map)) + len =3D map__end(map) - ip; return dso__data_read_offset(map__dso(map), d->machine, offset, buf, len); } =20 diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c index 8cec88e09792..a2e62daa708e 100644 --- a/tools/perf/util/intel-pt.c +++ b/tools/perf/util/intel-pt.c @@ -887,7 +887,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn= *intel_pt_insn, goto out_no_cache; } =20 - if (*ip >=3D al.map->end) + if (*ip >=3D map__end(al.map)) break; =20 offset +=3D intel_pt_insn->length; @@ -2750,7 +2750,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u6= 4 *ptss_ip) if (sym->binding =3D=3D STB_GLOBAL && !strcmp(sym->name, "__switch_to")) { ip =3D map->unmap_ip(map, sym->start); - if (ip >=3D map->start && ip < map->end) { + if (ip >=3D map__start(map) && ip < map__end(map)) { switch_ip =3D ip; break; } @@ -2768,7 +2768,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u6= 4 *ptss_ip) for (sym =3D start; sym; sym =3D dso__next_symbol(sym)) { if (!strcmp(sym->name, ptss)) { ip =3D map->unmap_ip(map, sym->start); - if (ip >=3D map->start && ip < map->end) { + if (ip >=3D map__start(map) && ip < map__end(map)) { *ptss_ip =3D ip; break; } @@ -3356,7 +3356,7 @@ static int intel_pt_process_aux_output_hw_id(struct i= ntel_pt *pt, static int intel_pt_find_map(struct thread *thread, u8 cpumode, u64 addr, struct addr_location *al) { - if (!al->map || addr < al->map->start || addr >=3D al->map->end) { + if (!al->map || addr < map__start(al->map) || addr >=3D map__end(al->map)= ) { if (!thread__find_map(thread, cpumode, addr, al)) return -1; } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 6e32344e66dc..08fb3ab0c205 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -902,7 +902,7 @@ static int machine__process_ksymbol_register(struct mac= hine *machine, } =20 map->start =3D event->ksymbol.addr; - map->end =3D map->start + event->ksymbol.len; + map->end =3D map__start(map) + event->ksymbol.len; err =3D maps__insert(machine__kernel_maps(machine), map); map__put(map); if (err) @@ -918,7 +918,7 @@ static int machine__process_ksymbol_register(struct mac= hine *machine, dso =3D map__dso(map); } =20 - sym =3D symbol__new(map->map_ip(map, map->start), + sym =3D symbol__new(map->map_ip(map, map__start(map)), event->ksymbol.len, 0, 0, event->ksymbol.name); if (!sym) @@ -943,7 +943,7 @@ static int machine__process_ksymbol_unregister(struct m= achine *machine, else { struct dso *dso =3D map__dso(map); =20 - sym =3D dso__find_symbol(dso, map->map_ip(map, map->start)); + sym =3D dso__find_symbol(dso, map->map_ip(map, map__start(map))); if (sym) dso__delete_symbol(dso, sym); } @@ -1216,7 +1216,7 @@ int machine__create_extra_kernel_map(struct machine *= machine, =20 if (!err) { pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n", - kmap->name, map->start, map->end); + kmap->name, map__start(map), map__end(map)); } =20 map__put(map); @@ -1721,7 +1721,7 @@ int machine__create_kernel_maps(struct machine *machi= ne) struct map_rb_node *next =3D map_rb_node__next(rb_node); =20 if (next) - machine__set_kernel_mmap(machine, start, next->map->start); + machine__set_kernel_mmap(machine, start, map__start(next->map)); } =20 out_put: @@ -1794,7 +1794,7 @@ static int machine__process_kernel_mmap_event(struct = machine *machine, if (map =3D=3D NULL) goto out_problem; =20 - map->end =3D map->start + xm->end - xm->start; + map->end =3D map__start(map) + xm->end - xm->start; =20 if (build_id__is_defined(bid)) dso__set_build_id(map__dso(map), bid); @@ -3288,7 +3288,7 @@ int machine__get_kernel_start(struct machine *machine) * kernel_start =3D 1ULL << 63 for x86_64. */ if (!err && !machine__is(machine, "x86_64")) - machine->kernel_start =3D map->start; + machine->kernel_start =3D map__start(map); } return err; } diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 90062af6675a..416fc449bde8 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -421,7 +421,7 @@ size_t map__fprintf(struct map *map, FILE *fp) const struct dso *dso =3D map__dso(map); =20 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", - map->start, map->end, map->pgoff, dso->name); + map__start(map), map__end(map), map->pgoff, dso->name); } =20 size_t map__fprintf_dsoname(struct map *map, FILE *fp) @@ -558,7 +558,7 @@ bool map__contains_symbol(const struct map *map, const = struct symbol *sym) { u64 ip =3D map->unmap_ip(map, sym->start); =20 - return ip >=3D map->start && ip < map->end; + return ip >=3D map__start(map) && ip < map__end(map); } =20 struct kmap *__map__kmap(struct map *map) @@ -592,12 +592,12 @@ struct maps *map__kmaps(struct map *map) =20 u64 map__map_ip(const struct map *map, u64 ip) { - return ip - map->start + map->pgoff; + return ip - map__start(map) + map->pgoff; } =20 u64 map__unmap_ip(const struct map *map, u64 ip) { - return ip + map->start - map->pgoff; + return ip + map__start(map) - map->pgoff; } =20 u64 identity__map_ip(const struct map *map __maybe_unused, u64 ip) diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 36c5add0144d..16646b94fa3a 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -52,9 +52,19 @@ static inline struct dso *map__dso(const struct map *map) return map->dso; } =20 +static inline u64 map__start(const struct map *map) +{ + return map->start; +} + +static inline u64 map__end(const struct map *map) +{ + return map->end; +} + static inline size_t map__size(const struct map *map) { - return map->end - map->start; + return map__end(map) - map__start(map); } =20 /* rip/ip <-> addr suitable for passing to `objdump --start-address=3D` */ diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 09ec6bbafcbc..1fd57db72226 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -34,7 +34,7 @@ static int __maps__insert(struct maps *maps, struct map *= map) { struct rb_node **p =3D &maps__entries(maps)->rb_node; struct rb_node *parent =3D NULL; - const u64 ip =3D map->start; + const u64 ip =3D map__start(map); struct map_rb_node *m, *new_rb_node; =20 new_rb_node =3D malloc(sizeof(*new_rb_node)); @@ -47,7 +47,7 @@ static int __maps__insert(struct maps *maps, struct map *= map) while (*p !=3D NULL) { parent =3D *p; m =3D rb_entry(parent, struct map_rb_node, rb_node); - if (ip < m->map->start) + if (ip < map__start(m->map)) p =3D &(*p)->rb_left; else p =3D &(*p)->rb_right; @@ -229,7 +229,7 @@ struct symbol *maps__find_symbol_by_name(struct maps *m= aps, const char *name, st =20 int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams) { - if (ams->addr < ams->ms.map->start || ams->addr >=3D ams->ms.map->end) { + if (ams->addr < map__start(ams->ms.map) || ams->addr >=3D map__end(ams->m= s.map)) { if (maps =3D=3D NULL) return -1; ams->ms.map =3D maps__find(maps, ams->addr); @@ -283,9 +283,9 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) while (next) { struct map_rb_node *pos =3D rb_entry(next, struct map_rb_node, rb_node); =20 - if (pos->map->end > map->start) { + if (map__end(pos->map) > map__start(map)) { first =3D next; - if (pos->map->start <=3D map->start) + if (map__start(pos->map) <=3D map__start(map)) break; next =3D next->rb_left; } else @@ -301,7 +301,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) * Stop if current map starts after map->end. * Maps are ordered by start: next will not overlap for sure. */ - if (pos->map->start >=3D map->end) + if (map__start(pos->map) >=3D map__end(map)) break; =20 if (verbose >=3D 2) { @@ -321,7 +321,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) * Now check if we need to create new maps for areas not * overlapped by the new map: */ - if (map->start > pos->map->start) { + if (map__start(map) > map__start(pos->map)) { struct map *before =3D map__clone(pos->map); =20 if (before =3D=3D NULL) { @@ -329,7 +329,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) goto put_map; } =20 - before->end =3D map->start; + before->end =3D map__start(map); err =3D __maps__insert(maps, before); if (err) goto put_map; @@ -339,7 +339,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) map__put(before); } =20 - if (map->end < pos->map->end) { + if (map->end < map__end(pos->map)) { struct map *after =3D map__clone(pos->map); =20 if (after =3D=3D NULL) { @@ -347,10 +347,10 @@ int maps__fixup_overlappings(struct maps *maps, struc= t map *map, FILE *fp) goto put_map; } =20 - after->start =3D map->end; - after->pgoff +=3D map->end - pos->map->start; - assert(pos->map->map_ip(pos->map, map->end) =3D=3D - after->map_ip(after, map->end)); + after->start =3D map__end(map); + after->pgoff +=3D map__end(map) - map__start(pos->map); + assert(pos->map->map_ip(pos->map, map__end(map)) =3D=3D + after->map_ip(after, map__end(map))); err =3D __maps__insert(maps, after); if (err) goto put_map; @@ -430,9 +430,9 @@ struct map *maps__find(struct maps *maps, u64 ip) p =3D maps__entries(maps)->rb_node; while (p !=3D NULL) { m =3D rb_entry(p, struct map_rb_node, rb_node); - if (ip < m->map->start) + if (ip < map__start(m->map)) p =3D p->rb_left; - else if (ip >=3D m->map->end) + else if (ip >=3D map__end(m->map)) p =3D p->rb_right; else goto out; diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index b26670a26005..4d9dbeeb6014 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -143,7 +143,7 @@ static int kernel_get_symbol_address_by_name(const char= *name, u64 *addr, return -ENOENT; *addr =3D map->unmap_ip(map, sym->start) - ((reloc) ? 0 : map->reloc) - - ((reladdr) ? map->start : 0); + ((reladdr) ? map__start(map) : 0); } return 0; } @@ -257,7 +257,7 @@ static bool kprobe_warn_out_range(const char *symbol, u= 64 address) =20 map =3D kernel_get_module_map(NULL); if (map) { - ret =3D address <=3D map->start || map->end < address; + ret =3D address <=3D map__start(map) || map__end(map) < address; if (ret) pr_warning("%s is out of .text, skip it.\n", symbol); map__put(map); diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index b8e5c6f61d80..cbf09eaf3734 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -409,7 +409,7 @@ static unsigned long get_offset(struct symbol *sym, str= uct addr_location *al) if (al->addr < sym->end) offset =3D al->addr - sym->start; else - offset =3D al->addr - al->map->start - sym->start; + offset =3D al->addr - map__start(al->map) - sym->start; =20 return offset; } @@ -788,9 +788,9 @@ static void set_sym_in_dict(PyObject *dict, struct addr= _location *al, pydict_set_item_string_decref(dict, dso_bid_field, _PyUnicode_FromString(sbuild_id)); pydict_set_item_string_decref(dict, dso_map_start, - PyLong_FromUnsignedLong(al->map->start)); + PyLong_FromUnsignedLong(map__start(al->map))); pydict_set_item_string_decref(dict, dso_map_end, - PyLong_FromUnsignedLong(al->map->end)); + PyLong_FromUnsignedLong(map__end(al->map))); } if (al->sym) { pydict_set_item_string_decref(dict, sym_field, diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 97085ad7fe9b..0542985ecaf6 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1349,7 +1349,7 @@ static int dso__process_kernel_symbol(struct dso *dso= , struct map *map, if (*remap_kernel && dso->kernel && !kmodule) { *remap_kernel =3D false; map->start =3D shdr->sh_addr + ref_reloc(kmap); - map->end =3D map->start + shdr->sh_size; + map->end =3D map__start(map) + shdr->sh_size; map->pgoff =3D shdr->sh_offset; map->map_ip =3D map__map_ip; map->unmap_ip =3D map__unmap_ip; @@ -1391,7 +1391,7 @@ static int dso__process_kernel_symbol(struct dso *dso= , struct map *map, u64 start =3D sym->st_value; =20 if (kmodule) - start +=3D map->start + shdr->sh_offset; + start +=3D map__start(map) + shdr->sh_offset; =20 curr_dso =3D dso__new(dso_name); if (curr_dso =3D=3D NULL) @@ -1409,7 +1409,7 @@ static int dso__process_kernel_symbol(struct dso *dso= , struct map *map, =20 if (adjust_kernel_syms) { curr_map->start =3D shdr->sh_addr + ref_reloc(kmap); - curr_map->end =3D curr_map->start + shdr->sh_size; + curr_map->end =3D map__start(curr_map) + shdr->sh_size; curr_map->pgoff =3D shdr->sh_offset; } else { curr_map->map_ip =3D curr_map->unmap_ip =3D identity__map_ip; @@ -1530,7 +1530,7 @@ dso__load_sym_internal(struct dso *dso, struct map *m= ap, struct symsrc *syms_ss, * attempted to prelink vdso to its virtual address. */ if (dso__is_vdso(dso)) - map->reloc =3D map->start - dso->text_offset; + map->reloc =3D map__start(map) - dso->text_offset; =20 dso->adjust_symbols =3D runtime_ss->adjust_symbols || ref_reloc(kmap); /* diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 7d92bd62c98a..7ea272daafe5 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -273,8 +273,8 @@ void maps__fixup_end(struct maps *maps) down_write(maps__lock(maps)); =20 maps__for_each_entry(maps, curr) { - if (prev !=3D NULL && !prev->map->end) - prev->map->end =3D curr->map->start; + if (prev !=3D NULL && !map__end(prev->map)) + prev->map->end =3D map__start(curr->map); =20 prev =3D curr; } @@ -283,7 +283,7 @@ void maps__fixup_end(struct maps *maps) * We still haven't the actual symbols, so guess the * last map final address. */ - if (curr && !curr->map->end) + if (curr && !map__end(curr->map)) curr->map->end =3D ~0ULL; =20 up_write(maps__lock(maps)); @@ -805,11 +805,11 @@ static int maps__split_kallsyms_for_kcore(struct maps= *kmaps, struct dso *dso) continue; } curr_map_dso =3D map__dso(curr_map); - pos->start -=3D curr_map->start - curr_map->pgoff; - if (pos->end > curr_map->end) - pos->end =3D curr_map->end; + pos->start -=3D map__start(curr_map) - curr_map->pgoff; + if (pos->end > map__end(curr_map)) + pos->end =3D map__end(curr_map); if (pos->end) - pos->end -=3D curr_map->start - curr_map->pgoff; + pos->end -=3D map__start(curr_map) - curr_map->pgoff; symbols__insert(&curr_map_dso->symbols, pos); ++count; } @@ -1151,7 +1151,7 @@ static int do_validate_kcore_modules(const char *file= name, struct maps *kmaps) dso =3D map__dso(old_map); /* Module must be in memory at the same address */ mi =3D find_module(dso->short_name, &modules); - if (!mi || mi->start !=3D old_map->start) { + if (!mi || mi->start !=3D map__start(old_map)) { err =3D -EINVAL; goto out; } @@ -1246,7 +1246,7 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgoff,= void *data) return -ENOMEM; } =20 - list_node->map->end =3D list_node->map->start + len; + list_node->map->end =3D map__start(list_node->map) + len; list_node->map->pgoff =3D pgoff; =20 list_add(&list_node->node, &md->maps); @@ -1268,21 +1268,21 @@ int maps__merge_in(struct maps *kmaps, struct map *= new_map) struct map *old_map =3D rb_node->map; =20 /* no overload with this one */ - if (new_map->end < old_map->start || - new_map->start >=3D old_map->end) + if (map__end(new_map) < map__start(old_map) || + map__start(new_map) >=3D map__end(old_map)) continue; =20 - if (new_map->start < old_map->start) { + if (map__start(new_map) < map__start(old_map)) { /* * |new...... * |old.... */ - if (new_map->end < old_map->end) { + if (map__end(new_map) < map__end(old_map)) { /* * |new......| -> |new..| * |old....| -> |old....| */ - new_map->end =3D old_map->start; + new_map->end =3D map__start(old_map); } else { /* * |new.............| -> |new..| |new..| @@ -1303,17 +1303,17 @@ int maps__merge_in(struct maps *kmaps, struct map *= new_map) goto out; } =20 - m->map->end =3D old_map->start; + m->map->end =3D map__start(old_map); list_add_tail(&m->node, &merged); - new_map->pgoff +=3D old_map->end - new_map->start; - new_map->start =3D old_map->end; + new_map->pgoff +=3D map__end(old_map) - map__start(new_map); + new_map->start =3D map__end(old_map); } } else { /* * |new...... * |old.... */ - if (new_map->end < old_map->end) { + if (map__end(new_map) < map__end(old_map)) { /* * |new..| -> x * |old.........| -> |old.........| @@ -1326,8 +1326,8 @@ int maps__merge_in(struct maps *kmaps, struct map *ne= w_map) * |new......| -> |new...| * |old....| -> |old....| */ - new_map->pgoff +=3D old_map->end - new_map->start; - new_map->start =3D old_map->end; + new_map->pgoff +=3D map__end(old_map) - map__start(new_map); + new_map->start =3D map__end(old_map); } } } @@ -1424,9 +1424,10 @@ static int dso__load_kcore(struct dso *dso, struct m= ap *map, struct map_list_node *new_node; =20 list_for_each_entry(new_node, &md.maps, node) { - u64 new_size =3D new_node->map->end - new_node->map->start; + u64 new_size =3D map__size(new_node->map); =20 - if (!(stext >=3D new_node->map->start && stext < new_node->map->end)) + if (!(stext >=3D map__start(new_node->map) && + stext < map__end(new_node->map))) continue; =20 /* @@ -1452,8 +1453,8 @@ static int dso__load_kcore(struct dso *dso, struct ma= p *map, new_node =3D list_entry(md.maps.next, struct map_list_node, node); list_del_init(&new_node->node); if (new_node->map =3D=3D replacement_map) { - map->start =3D new_node->map->start; - map->end =3D new_node->map->end; + map->start =3D map__start(new_node->map); + map->end =3D map__end(new_node->map); map->pgoff =3D new_node->map->pgoff; map->map_ip =3D new_node->map->map_ip; map->unmap_ip =3D new_node->map->unmap_ip; diff --git a/tools/perf/util/symbol_fprintf.c b/tools/perf/util/symbol_fpri= ntf.c index 2664fb65e47a..d9e5ad040b6a 100644 --- a/tools/perf/util/symbol_fprintf.c +++ b/tools/perf/util/symbol_fprintf.c @@ -30,7 +30,7 @@ size_t __symbol__fprintf_symname_offs(const struct symbol= *sym, if (al->addr < sym->end) offset =3D al->addr - sym->start; else - offset =3D al->addr - al->map->start - sym->start; + offset =3D al->addr - map__start(al->map) - sym->start; length +=3D fprintf(fp, "+0x%lx", offset); } return length; diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic= -events.c index fbd1a882b013..b2e4afa5efa1 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -706,8 +706,8 @@ int perf_event__synthesize_modules(struct perf_tool *to= ol, perf_event__handler_t (sizeof(event->mmap2.filename) - size)); memset(event->mmap2.filename + size, 0, machine->id_hdr_size); event->mmap2.header.size +=3D machine->id_hdr_size; - event->mmap2.start =3D map->start; - event->mmap2.len =3D map->end - map->start; + event->mmap2.start =3D map__start(map); + event->mmap2.len =3D map__size(map); event->mmap2.pid =3D machine->pid; =20 memcpy(event->mmap2.filename, dso->long_name, dso->long_name_len + 1); @@ -720,8 +720,8 @@ int perf_event__synthesize_modules(struct perf_tool *to= ol, perf_event__handler_t (sizeof(event->mmap.filename) - size)); memset(event->mmap.filename + size, 0, machine->id_hdr_size); event->mmap.header.size +=3D machine->id_hdr_size; - event->mmap.start =3D map->start; - event->mmap.len =3D map->end - map->start; + event->mmap.start =3D map__start(map); + event->mmap.len =3D map__size(map); event->mmap.pid =3D machine->pid; =20 memcpy(event->mmap.filename, dso->long_name, dso->long_name_len + 1); @@ -1143,8 +1143,8 @@ static int __perf_event__synthesize_kernel_mmap(struc= t perf_tool *tool, event->mmap2.header.size =3D (sizeof(event->mmap2) - (sizeof(event->mmap2.filename) - size) + machine->id_hdr_size); event->mmap2.pgoff =3D kmap->ref_reloc_sym->addr; - event->mmap2.start =3D map->start; - event->mmap2.len =3D map->end - event->mmap.start; + event->mmap2.start =3D map__start(map); + event->mmap2.len =3D map__end(map) - event->mmap.start; event->mmap2.pid =3D machine->pid; =20 perf_record_mmap2__read_build_id(&event->mmap2, machine, true); @@ -1156,8 +1156,8 @@ static int __perf_event__synthesize_kernel_mmap(struc= t perf_tool *tool, event->mmap.header.size =3D (sizeof(event->mmap) - (sizeof(event->mmap.filename) - size) + machine->id_hdr_size); event->mmap.pgoff =3D kmap->ref_reloc_sym->addr; - event->mmap.start =3D map->start; - event->mmap.len =3D map->end - event->mmap.start; + event->mmap.start =3D map__start(map); + event->mmap.len =3D map__end(map) - event->mmap.start; event->mmap.pid =3D machine->pid; } =20 diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c index c8cba9d4bfd9..b79f57e5648f 100644 --- a/tools/perf/util/unwind-libdw.c +++ b/tools/perf/util/unwind-libdw.c @@ -62,19 +62,19 @@ static int __report_module(struct addr_location *al, u6= 4 ip, Dwarf_Addr s; =20 dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL); - if (s !=3D al->map->start - al->map->pgoff) + if (s !=3D map__start(al->map) - al->map->pgoff) mod =3D 0; } =20 if (!mod) mod =3D dwfl_report_elf(ui->dwfl, dso->short_name, dso->long_name, -1, - al->map->start - al->map->pgoff, false); + map__start(al->map) - al->map->pgoff, false); if (!mod) { char filename[PATH_MAX]; =20 if (dso__build_id_filename(dso, filename, sizeof(filename), false)) mod =3D dwfl_report_elf(ui->dwfl, dso->short_name, filename, -1, - al->map->start - al->map->pgoff, false); + map__start(al->map) - al->map->pgoff, false); } =20 if (mod) { --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 16499C7618A for ; Mon, 20 Mar 2023 03:41:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230022AbjCTDlj (ORCPT ); Sun, 19 Mar 2023 23:41:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230017AbjCTDka (ORCPT ); Sun, 19 Mar 2023 23:40:30 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CC7F222CB for ; Sun, 19 Mar 2023 20:40:03 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-536d63d17dbso110903927b3.22 for ; Sun, 19 Mar 2023 20:40:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283602; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=y10GnXOzH9U5tz9iFzVskmw05u/+9XHwA2jBfRW8OlY=; b=ZVomonjoLY0q9cKdr2OeubslSy7rduq4h3LiVMua2Gtaz59IYf5m+BA8wv2jSvHAg7 zchoBdhKIKx10hwDg4H7G+QooFben/vb8LlkZ7WxU3cM0HBeiIpfBH7jx/wJ3/GeJRf3 QCtTlBh7WNawAtlg3vU2cn1wwaZ7zNWj+hDW4I/w7bL4PjmnWmzwMK0Zgb2u+XhJCAMP KvoKYDrSfRGfm7D5btbJMtr4S+LaDe4pJrZ4vfZ0CyWncVxCLSe881h2FMRo/jzEMGIh thzdx+ZBqqThRLq9733OkQO4mYFxGanzGSoAPvUELix9bQeIDNFTbAGnRAde5wYK5X2m 8VoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283602; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=y10GnXOzH9U5tz9iFzVskmw05u/+9XHwA2jBfRW8OlY=; b=N7QBBTMPBKRbk++tK449tE1KlAGxVORA/KIX4KGG8pxyG8fW9N2gmsK9RJ4xAB7AVk bpW58m+efJOKhBQNecqvNc7bsbuXiFpiKgyF4Jadr16esuS79pimtSd5/4ERRSZiSl1K w7yHJj8nIbAhGbiIqDvcpweet5fKlUVVyPca0LTUVw2X7r24o2lTHYE6Whs88HqySZWr wSHvk3Yk0vNQoYiIHwZDZ8uD8FqNeLufZW5aS1KFK4XrJkm/KF4umMUjPHw/+kgO3bQN 8g8odfXiypJzv9xmLnGt17MbaC9jtXenQFExaY3BwWjjhT6m/70JntlcyUe/wAi+tsqb xjEg== X-Gm-Message-State: AO0yUKW7lf5WT/h7Q6l7lRh1mbXI7xyd3p4DlvT5+1aTVf2UujCGlB40 8v2WMeXsfGOocCJ4yXIhlzxtORhdf8X7 X-Google-Smtp-Source: AK7set9e1ValGvdqpgqK0HZEf9ILOBrsd3K4MJU9K9TX6KJorK+AA56T5qrbfDEWWrQXxR5Q8Xe8V+p1W+mg X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:723:b0:b6a:2590:6c63 with SMTP id l3-20020a056902072300b00b6a25906c63mr3762098ybt.2.1679283601785; Sun, 19 Mar 2023 20:40:01 -0700 (PDT) Date: Sun, 19 Mar 2023 20:37:59 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-12-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 11/22] perf map: Rename map_ip and unmap_ip From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add dso to match comment. This avoids a naming conflict with later added accessor functions for variables in struct map. Signed-off-by: Ian Rogers --- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-script.c | 4 ++-- tools/perf/util/machine.c | 4 ++-- tools/perf/util/map.c | 8 ++++---- tools/perf/util/map.h | 4 ++-- tools/perf/util/symbol-elf.c | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c index f3029742b800..4d4b770a401c 100644 --- a/tools/perf/builtin-kmem.c +++ b/tools/perf/builtin-kmem.c @@ -423,7 +423,7 @@ static u64 find_callsite(struct evsel *evsel, struct pe= rf_sample *sample) if (!caller) { /* found */ if (node->ms.map) - addr =3D map__unmap_ip(node->ms.map, node->ip); + addr =3D map__dso_unmap_ip(node->ms.map, node->ip); else addr =3D node->ip; =20 diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index eb49689d0f00..21944adf4c17 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -1012,11 +1012,11 @@ static int perf_sample__fprintf_brstackoff(struct p= erf_sample *sample, =20 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) && !map__dso(alf.map)->adjust_symbols) - from =3D map__map_ip(alf.map, from); + from =3D map__dso_map_ip(alf.map, from); =20 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) && !map__dso(alt.map)->adjust_symbols) - to =3D map__map_ip(alt.map, to); + to =3D map__dso_map_ip(alt.map, to); =20 printed +=3D fprintf(fp, " 0x%"PRIx64, from); if (PRINT_FIELD(DSO)) { diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 08fb3ab0c205..5bf035b23a79 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -3053,7 +3053,7 @@ static int append_inlines(struct callchain_cursor *cu= rsor, struct map_symbol *ms if (!symbol_conf.inline_name || !map || !sym) return ret; =20 - addr =3D map__map_ip(map, ip); + addr =3D map__dso_map_ip(map, ip); addr =3D map__rip_2objdump(map, addr); dso =3D map__dso(map); =20 @@ -3098,7 +3098,7 @@ static int unwind_entry(struct unwind_entry *entry, v= oid *arg) * its corresponding binary. */ if (entry->ms.map) - addr =3D map__map_ip(entry->ms.map, entry->ip); + addr =3D map__dso_map_ip(entry->ms.map, entry->ip); =20 srcline =3D callchain_srcline(&entry->ms, addr); return callchain_cursor_append(cursor, entry->ip, &entry->ms, diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 416fc449bde8..d97a6d20626f 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -109,8 +109,8 @@ void map__init(struct map *map, u64 start, u64 end, u64= pgoff, struct dso *dso) map->pgoff =3D pgoff; map->reloc =3D 0; map->dso =3D dso__get(dso); - map->map_ip =3D map__map_ip; - map->unmap_ip =3D map__unmap_ip; + map->map_ip =3D map__dso_map_ip; + map->unmap_ip =3D map__dso_unmap_ip; map->erange_warned =3D false; refcount_set(&map->refcnt, 1); } @@ -590,12 +590,12 @@ struct maps *map__kmaps(struct map *map) return kmap->kmaps; } =20 -u64 map__map_ip(const struct map *map, u64 ip) +u64 map__dso_map_ip(const struct map *map, u64 ip) { return ip - map__start(map) + map->pgoff; } =20 -u64 map__unmap_ip(const struct map *map, u64 ip) +u64 map__dso_unmap_ip(const struct map *map, u64 ip) { return ip + map__start(map) - map->pgoff; } diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 16646b94fa3a..9b0a84e46e48 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -41,9 +41,9 @@ struct kmap *map__kmap(struct map *map); struct maps *map__kmaps(struct map *map); =20 /* ip -> dso rip */ -u64 map__map_ip(const struct map *map, u64 ip); +u64 map__dso_map_ip(const struct map *map, u64 ip); /* dso rip -> ip */ -u64 map__unmap_ip(const struct map *map, u64 ip); +u64 map__dso_unmap_ip(const struct map *map, u64 ip); /* Returns ip */ u64 identity__map_ip(const struct map *map __maybe_unused, u64 ip); =20 diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 0542985ecaf6..93ae3f22fd03 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1351,8 +1351,8 @@ static int dso__process_kernel_symbol(struct dso *dso= , struct map *map, map->start =3D shdr->sh_addr + ref_reloc(kmap); map->end =3D map__start(map) + shdr->sh_size; map->pgoff =3D shdr->sh_offset; - map->map_ip =3D map__map_ip; - map->unmap_ip =3D map__unmap_ip; + map->map_ip =3D map__dso_map_ip; + map->unmap_ip =3D map__dso_unmap_ip; /* Ensure maps are correctly ordered */ if (kmaps) { int err; --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 D8DFBC7618A for ; Mon, 20 Mar 2023 03:42:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229579AbjCTDmA (ORCPT ); Sun, 19 Mar 2023 23:42:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229988AbjCTDlD (ORCPT ); Sun, 19 Mar 2023 23:41:03 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A1BF65AD for ; Sun, 19 Mar 2023 20:40:17 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5411f21f849so109793937b3.16 for ; Sun, 19 Mar 2023 20:40:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283609; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=p/sEclgs5uA8dRpnnE1PKUjX2abTYHVRGUEO9o/xRG0=; b=qRVsSMObKRLDR/rwltYXSwTP9FvKpCdAoqKbb03CbsNmn61Xnl1IlxVVG7cDVsfsPQ EkoBagepTjTusAvQzNdbYg8LqODVgmnQsILp77k0pDjiCK3g4HM+bH8jD7wk30K30KAA 5prTQCWtMpAMQ2MsyMthnGb+6DxXUPsaxM4CQIgX9EmIFuO3vx3ekgM1kmex4NncNjfm 7Du+dakPLYK8XXoOIWtX48b35altI6qiASD1YWTexOMRqAkvR5F8/dq9MmNndRbY6F93 y5/qctpAbLYbuohsE/NE8QjF68tqsbPwEHMgl6KKxbe1T3OjPdA0O23zFkUTuYtWPc/f Ryow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283609; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=p/sEclgs5uA8dRpnnE1PKUjX2abTYHVRGUEO9o/xRG0=; b=A/a42KnqtvSPwTAbiyGD+JJdWMGBA1+eq8TlDGXnMjNKmpLKR+IPwcjrJdddnrM/3o RJuBh+yFnhO5uK60K5XwQJTrEXQ/w44aooQYcAAQ2heUlvIbHy2KrfdKRa2P8D9VkXMw KvyzPE1+2wnaRy93aPgHnX8XEGYlDzC2hvxjLjZLy6Eaj53HZPpzaW5dm9Y4A0TwQyTT jZ1smsif0rv5qQKCO7jS6/Nk+j6WGEzjNH8IkQ6DJNBiDje4H07Zs59+UMONmbVFyS+O msDxXIRFYxnn0HvqhPwj9WhMBDJrhytbnBnJIZ4yM5PvPSHgxBox4SNoKABszi/6ecU9 o8eA== X-Gm-Message-State: AO0yUKXjAPCcMBn34RzSzhiVIc8Tko+hSRo9ZInDdywve9lGM4/FT3jE X+xHJz0P5oce3qjGt0wwDn0Ao1Ibf2vC X-Google-Smtp-Source: AK7set8avdBdvb8XjOigdDXZxHwvu/1IwVjeRbou2HjumXO/1xuImBzQAtwcZi5/8ZFEKsNIU+DPldDJoKCB X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a81:ae0d:0:b0:544:bb1e:f9cf with SMTP id m13-20020a81ae0d000000b00544bb1ef9cfmr6474508ywh.4.1679283609684; Sun, 19 Mar 2023 20:40:09 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:00 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-13-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 12/22] perf map: Add helper for map_ip and unmap_ip From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Later changes will add reference count checking for struct map, add a helper function to invoke the map_ip and unmap_ip function pointers. The helper allows the reference count check to be in fewer places. Signed-off-by: Ian Rogers --- tools/perf/arch/s390/annotate/instructions.c | 3 ++- tools/perf/builtin-kallsyms.c | 2 +- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-lock.c | 4 ++-- tools/perf/builtin-script.c | 2 +- tools/perf/tests/vmlinux-kallsyms.c | 10 +++++----- tools/perf/util/annotate.c | 16 +++++++++------- tools/perf/util/bpf_lock_contention.c | 4 ++-- tools/perf/util/dlfilter.c | 2 +- tools/perf/util/dso.c | 6 ++++-- tools/perf/util/event.c | 8 ++++---- tools/perf/util/evsel_fprintf.c | 2 +- tools/perf/util/intel-pt.c | 10 +++++----- tools/perf/util/machine.c | 16 ++++++++-------- tools/perf/util/map.c | 10 +++++----- tools/perf/util/map.h | 10 ++++++++++ tools/perf/util/maps.c | 8 ++++---- tools/perf/util/probe-event.c | 8 ++++---- .../util/scripting-engines/trace-event-python.c | 2 +- tools/perf/util/sort.c | 12 ++++++------ tools/perf/util/symbol.c | 4 ++-- tools/perf/util/thread.c | 2 +- tools/perf/util/unwind-libdw.c | 2 +- 23 files changed, 80 insertions(+), 65 deletions(-) diff --git a/tools/perf/arch/s390/annotate/instructions.c b/tools/perf/arch= /s390/annotate/instructions.c index 0e136630659e..6548933e8dc0 100644 --- a/tools/perf/arch/s390/annotate/instructions.c +++ b/tools/perf/arch/s390/annotate/instructions.c @@ -39,7 +39,8 @@ static int s390_call__parse(struct arch *arch, struct ins= _operands *ops, target.addr =3D map__objdump_2mem(map, ops->target.addr); =20 if (maps__find_ams(ms->maps, &target) =3D=3D 0 && - map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.ad= dr)) =3D=3D ops->target.addr) + map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.ad= dr)) =3D=3D + ops->target.addr) ops->target.sym =3D target.ms.sym; =20 return 0; diff --git a/tools/perf/builtin-kallsyms.c b/tools/perf/builtin-kallsyms.c index 5638ca4dbd8e..3751df744577 100644 --- a/tools/perf/builtin-kallsyms.c +++ b/tools/perf/builtin-kallsyms.c @@ -39,7 +39,7 @@ static int __cmd_kallsyms(int argc, const char **argv) dso =3D map__dso(map); printf("%s: %s %s %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n= ", symbol->name, dso->short_name, dso->long_name, - map->unmap_ip(map, symbol->start), map->unmap_ip(map, symbol->end), + map__unmap_ip(map, symbol->start), map__unmap_ip(map, symbol->end), symbol->start, symbol->end); } =20 diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c index 4d4b770a401c..fcd2ef3bd3f5 100644 --- a/tools/perf/builtin-kmem.c +++ b/tools/perf/builtin-kmem.c @@ -1024,7 +1024,7 @@ static void __print_slab_result(struct rb_root *root, =20 if (sym !=3D NULL) snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name, - addr - map->unmap_ip(map, sym->start)); + addr - map__unmap_ip(map, sym->start)); else snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr); printf(" %-34s |", buf); diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 3c8a19ebc496..40b1e53e2d23 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -900,7 +900,7 @@ static int get_symbol_name_offset(struct map *map, stru= ct symbol *sym, u64 ip, return 0; } =20 - offset =3D map->map_ip(map, ip) - sym->start; + offset =3D map__map_ip(map, ip) - sym->start; =20 if (offset) return scnprintf(buf, size, "%s+%#lx", sym->name, offset); @@ -1070,7 +1070,7 @@ static int report_lock_contention_begin_event(struct = evsel *evsel, return -ENOMEM; } =20 - addrs[filters.nr_addrs++] =3D kmap->unmap_ip(kmap, sym->start); + addrs[filters.nr_addrs++] =3D map__unmap_ip(kmap, sym->start); filters.addrs =3D addrs; } } diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 21944adf4c17..9dc3193f7c1a 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -1088,7 +1088,7 @@ static int grab_bb(u8 *buffer, u64 start, u64 end, /* Load maps to ensure dso->is_64_bit has been updated */ map__load(al.map); =20 - offset =3D al.map->map_ip(al.map, start); + offset =3D map__map_ip(al.map, start); len =3D dso__data_read_offset(dso, machine, offset, (u8 *)buffer, end - start + MAXINSN); =20 diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux= -kallsyms.c index 0a75623172c2..05a322ea3f9f 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -13,7 +13,7 @@ #include "debug.h" #include "machine.h" =20 -#define UM(x) kallsyms_map->unmap_ip(kallsyms_map, (x)) +#define UM(x) map__unmap_ip(kallsyms_map, (x)) =20 static bool is_ignored_symbol(const char *name, char type) { @@ -221,8 +221,8 @@ static int test__vmlinux_matches_kallsyms(struct test_s= uite *test __maybe_unused if (sym->start =3D=3D sym->end) continue; =20 - mem_start =3D vmlinux_map->unmap_ip(vmlinux_map, sym->start); - mem_end =3D vmlinux_map->unmap_ip(vmlinux_map, sym->end); + mem_start =3D map__unmap_ip(vmlinux_map, sym->start); + mem_end =3D map__unmap_ip(vmlinux_map, sym->end); =20 first_pair =3D machine__find_kernel_symbol(&kallsyms, mem_start, NULL); pair =3D first_pair; @@ -319,8 +319,8 @@ static int test__vmlinux_matches_kallsyms(struct test_s= uite *test __maybe_unused maps__for_each_entry(maps, rb_node) { struct map *pair, *map =3D rb_node->map; =20 - mem_start =3D vmlinux_map->unmap_ip(vmlinux_map, map__start(map)); - mem_end =3D vmlinux_map->unmap_ip(vmlinux_map, map__end(map)); + mem_start =3D map__unmap_ip(vmlinux_map, map__start(map)); + mem_end =3D map__unmap_ip(vmlinux_map, map__end(map)); =20 pair =3D maps__find(kallsyms.kmaps, mem_start); if (pair =3D=3D NULL || pair->priv) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index f60f5efb2ad9..e8570b7cc36f 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -280,7 +280,8 @@ static int call__parse(struct arch *arch, struct ins_op= erands *ops, struct map_s target.addr =3D map__objdump_2mem(map, ops->target.addr); =20 if (maps__find_ams(ms->maps, &target) =3D=3D 0 && - map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.ad= dr)) =3D=3D ops->target.addr) + map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.ad= dr)) =3D=3D + ops->target.addr) ops->target.sym =3D target.ms.sym; =20 return 0; @@ -384,8 +385,8 @@ static int jump__parse(struct arch *arch, struct ins_op= erands *ops, struct map_s } =20 target.addr =3D map__objdump_2mem(map, ops->target.addr); - start =3D map->unmap_ip(map, sym->start), - end =3D map->unmap_ip(map, sym->end); + start =3D map__unmap_ip(map, sym->start); + end =3D map__unmap_ip(map, sym->end); =20 ops->target.outside =3D target.addr < start || target.addr > end; =20 @@ -408,7 +409,8 @@ static int jump__parse(struct arch *arch, struct ins_op= erands *ops, struct map_s * the symbol searching and disassembly should be done. */ if (maps__find_ams(ms->maps, &target) =3D=3D 0 && - map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.ad= dr)) =3D=3D ops->target.addr) + map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.ad= dr)) =3D=3D + ops->target.addr) ops->target.sym =3D target.ms.sym; =20 if (!ops->target.outside) { @@ -889,7 +891,7 @@ static int __symbol__inc_addr_samples(struct map_symbol= *ms, unsigned offset; struct sym_hist *h; =20 - pr_debug3("%s: addr=3D%#" PRIx64 "\n", __func__, ms->map->unmap_ip(ms->ma= p, addr)); + pr_debug3("%s: addr=3D%#" PRIx64 "\n", __func__, map__unmap_ip(ms->map, a= ddr)); =20 if ((addr < sym->start || addr >=3D sym->end) && (addr !=3D sym->end || sym->start !=3D sym->end)) { @@ -1985,8 +1987,8 @@ static int symbol__disassemble(struct symbol *sym, st= ruct annotate_args *args) return err; =20 pr_debug("%s: filename=3D%s, sym=3D%s, start=3D%#" PRIx64 ", end=3D%#" PR= Ix64 "\n", __func__, - symfs_filename, sym->name, map->unmap_ip(map, sym->start), - map->unmap_ip(map, sym->end)); + symfs_filename, sym->name, map__unmap_ip(map, sym->start), + map__unmap_ip(map, sym->end)); =20 pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso->long_name, sym, sym->name); diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lo= ck_contention.c index 0b47863d2460..9a76fc6484b4 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -74,7 +74,7 @@ int lock_contention_prepare(struct lock_contention *con) continue; } =20 - addrs[con->filters->nr_addrs++] =3D kmap->unmap_ip(kmap, sym->start); + addrs[con->filters->nr_addrs++] =3D map__unmap_ip(kmap, sym->start); con->filters->addrs =3D addrs; } naddrs =3D con->filters->nr_addrs; @@ -233,7 +233,7 @@ static const char *lock_contention_get_name(struct lock= _contention *con, if (sym) { unsigned long offset; =20 - offset =3D kmap->map_ip(kmap, addr) - sym->start; + offset =3D map__map_ip(kmap, addr) - sym->start; =20 if (offset =3D=3D 0) return sym->name; diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c index fe401fa4be02..16238f823a5e 100644 --- a/tools/perf/util/dlfilter.c +++ b/tools/perf/util/dlfilter.c @@ -278,7 +278,7 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip,= void *buf, __u32 len) =20 map =3D a.map; have_map: - offset =3D map->map_ip(map, ip); + offset =3D map__map_ip(map, ip); if (ip + len >=3D map__end(map)) len =3D map__end(map) - ip; return dso__data_read_offset(map__dso(map), d->machine, offset, buf, len); diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index f1a14c0ad26d..e36b418df2c6 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1122,7 +1122,8 @@ ssize_t dso__data_read_addr(struct dso *dso, struct m= ap *map, struct machine *machine, u64 addr, u8 *data, ssize_t size) { - u64 offset =3D map->map_ip(map, addr); + u64 offset =3D map__map_ip(map, addr); + return dso__data_read_offset(dso, machine, offset, data, size); } =20 @@ -1162,7 +1163,8 @@ ssize_t dso__data_write_cache_addr(struct dso *dso, s= truct map *map, struct machine *machine, u64 addr, const u8 *data, ssize_t size) { - u64 offset =3D map->map_ip(map, addr); + u64 offset =3D map__map_ip(map, addr); + return dso__data_write_cache_offs(dso, machine, offset, data, size); } =20 diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 2ddc75dee019..2712d1a8264e 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -487,7 +487,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *= event, struct machine *ma =20 al.map =3D maps__find(machine__kernel_maps(machine), tp->addr); if (al.map && map__load(al.map) >=3D 0) { - al.addr =3D al.map->map_ip(al.map, tp->addr); + al.addr =3D map__map_ip(al.map, tp->addr); al.sym =3D map__find_symbol(al.map, al.addr); if (al.sym) ret +=3D symbol__fprintf_symname_offs(al.sym, &al, fp); @@ -622,7 +622,7 @@ struct map *thread__find_map(struct thread *thread, u8 = cpumode, u64 addr, */ if (load_map) map__load(al->map); - al->addr =3D al->map->map_ip(al->map, al->addr); + al->addr =3D map__map_ip(al->map, al->addr); } =20 return al->map; @@ -743,12 +743,12 @@ int machine__resolve(struct machine *machine, struct = addr_location *al, } if (!ret && al->sym) { snprintf(al_addr_str, sz, "0x%"PRIx64, - al->map->unmap_ip(al->map, al->sym->start)); + map__unmap_ip(al->map, al->sym->start)); ret =3D strlist__has_entry(symbol_conf.sym_list, al_addr_str); } if (!ret && symbol_conf.addr_list && al->map) { - unsigned long addr =3D al->map->unmap_ip(al->map, al->addr); + unsigned long addr =3D map__unmap_ip(al->map, al->addr); =20 ret =3D intlist__has_entry(symbol_conf.addr_list, addr); if (!ret && symbol_conf.addr_range) { diff --git a/tools/perf/util/evsel_fprintf.c b/tools/perf/util/evsel_fprint= f.c index dff5d8c4b06d..a09ac00810b7 100644 --- a/tools/perf/util/evsel_fprintf.c +++ b/tools/perf/util/evsel_fprintf.c @@ -151,7 +151,7 @@ int sample__fprintf_callchain(struct perf_sample *sampl= e, int left_alignment, printed +=3D fprintf(fp, " <-"); =20 if (map) - addr =3D map->map_ip(map, node->ip); + addr =3D map__map_ip(map, node->ip); =20 if (print_ip) { /* Show binary offset for userspace addr */ diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c index a2e62daa708e..fe893c9bab3f 100644 --- a/tools/perf/util/intel-pt.c +++ b/tools/perf/util/intel-pt.c @@ -816,7 +816,7 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn= *intel_pt_insn, dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE)) return -ENOENT; =20 - offset =3D al.map->map_ip(al.map, *ip); + offset =3D map__map_ip(al.map, *ip); =20 if (!to_ip && one_map) { struct intel_pt_cache_entry *e; @@ -987,7 +987,7 @@ static int __intel_pt_pgd_ip(uint64_t ip, void *data) if (!thread__find_map(thread, cpumode, ip, &al) || !map__dso(al.map)) return -EINVAL; =20 - offset =3D al.map->map_ip(al.map, ip); + offset =3D map__map_ip(al.map, ip); =20 return intel_pt_match_pgd_ip(ptq->pt, ip, offset, map__dso(al.map)->long_= name); } @@ -2749,7 +2749,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u6= 4 *ptss_ip) for (sym =3D start; sym; sym =3D dso__next_symbol(sym)) { if (sym->binding =3D=3D STB_GLOBAL && !strcmp(sym->name, "__switch_to")) { - ip =3D map->unmap_ip(map, sym->start); + ip =3D map__unmap_ip(map, sym->start); if (ip >=3D map__start(map) && ip < map__end(map)) { switch_ip =3D ip; break; @@ -2767,7 +2767,7 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u6= 4 *ptss_ip) =20 for (sym =3D start; sym; sym =3D dso__next_symbol(sym)) { if (!strcmp(sym->name, ptss)) { - ip =3D map->unmap_ip(map, sym->start); + ip =3D map__unmap_ip(map, sym->start); if (ip >=3D map__start(map) && ip < map__end(map)) { *ptss_ip =3D ip; break; @@ -3393,7 +3393,7 @@ static int intel_pt_text_poke(struct intel_pt *pt, un= ion perf_event *event) if (!dso || !dso->auxtrace_cache) continue; =20 - offset =3D al.map->map_ip(al.map, addr); + offset =3D map__map_ip(al.map, addr); =20 e =3D intel_pt_cache_lookup(dso, machine, offset); if (!e) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 5bf035b23a79..afb77bd161e2 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -918,7 +918,7 @@ static int machine__process_ksymbol_register(struct mac= hine *machine, dso =3D map__dso(map); } =20 - sym =3D symbol__new(map->map_ip(map, map__start(map)), + sym =3D symbol__new(map__map_ip(map, map__start(map)), event->ksymbol.len, 0, 0, event->ksymbol.name); if (!sym) @@ -943,7 +943,7 @@ static int machine__process_ksymbol_unregister(struct m= achine *machine, else { struct dso *dso =3D map__dso(map); =20 - sym =3D dso__find_symbol(dso, map->map_ip(map, map__start(map))); + sym =3D dso__find_symbol(dso, map__map_ip(map, map__start(map))); if (sym) dso__delete_symbol(dso, sym); } @@ -1278,7 +1278,7 @@ int machine__map_x86_64_entry_trampolines(struct mach= ine *machine, =20 dest_map =3D maps__find(kmaps, map->pgoff); if (dest_map !=3D map) - map->pgoff =3D dest_map->map_ip(dest_map, map->pgoff); + map->pgoff =3D map__map_ip(dest_map, map->pgoff); found =3D true; } if (found || machine->trampolines_mapped) @@ -3340,7 +3340,7 @@ char *machine__resolve_kernel_addr(void *vmachine, un= signed long long *addrp, ch return NULL; =20 *modp =3D __map__is_kmodule(map) ? (char *)map__dso(map)->short_name : NU= LL; - *addrp =3D map->unmap_ip(map, sym->start); + *addrp =3D map__unmap_ip(map, sym->start); return sym->name; } =20 @@ -3383,17 +3383,17 @@ bool machine__is_lock_function(struct machine *mach= ine, u64 addr) return false; } =20 - machine->sched.text_start =3D kmap->unmap_ip(kmap, sym->start); + machine->sched.text_start =3D map__unmap_ip(kmap, sym->start); =20 /* should not fail from here */ sym =3D machine__find_kernel_symbol_by_name(machine, "__sched_text_end",= &kmap); - machine->sched.text_end =3D kmap->unmap_ip(kmap, sym->start); + machine->sched.text_end =3D map__unmap_ip(kmap, sym->start); =20 sym =3D machine__find_kernel_symbol_by_name(machine, "__lock_text_start"= , &kmap); - machine->lock.text_start =3D kmap->unmap_ip(kmap, sym->start); + machine->lock.text_start =3D map__unmap_ip(kmap, sym->start); =20 sym =3D machine__find_kernel_symbol_by_name(machine, "__lock_text_end", = &kmap); - machine->lock.text_end =3D kmap->unmap_ip(kmap, sym->start); + machine->lock.text_end =3D map__unmap_ip(kmap, sym->start); } =20 /* failed to get kernel symbols */ diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index d97a6d20626f..816bffbbf344 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -519,7 +519,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip) if (dso->kernel =3D=3D DSO_SPACE__USER) return rip + dso->text_offset; =20 - return map->unmap_ip(map, rip) - map->reloc; + return map__unmap_ip(map, rip) - map->reloc; } =20 /** @@ -539,24 +539,24 @@ u64 map__objdump_2mem(struct map *map, u64 ip) const struct dso *dso =3D map__dso(map); =20 if (!dso->adjust_symbols) - return map->unmap_ip(map, ip); + return map__unmap_ip(map, ip); =20 if (dso->rel) - return map->unmap_ip(map, ip + map->pgoff); + return map__unmap_ip(map, ip + map->pgoff); =20 /* * kernel modules also have DSO_TYPE_USER in dso->kernel, * but all kernel modules are ET_REL, so won't get here. */ if (dso->kernel =3D=3D DSO_SPACE__USER) - return map->unmap_ip(map, ip - dso->text_offset); + return map__unmap_ip(map, ip - dso->text_offset); =20 return ip + map->reloc; } =20 bool map__contains_symbol(const struct map *map, const struct symbol *sym) { - u64 ip =3D map->unmap_ip(map, sym->start); + u64 ip =3D map__unmap_ip(map, sym->start); =20 return ip >=3D map__start(map) && ip < map__end(map); } diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 9b0a84e46e48..9118eba71032 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -52,6 +52,16 @@ static inline struct dso *map__dso(const struct map *map) return map->dso; } =20 +static inline u64 map__map_ip(const struct map *map, u64 ip) +{ + return map->map_ip(map, ip); +} + +static inline u64 map__unmap_ip(const struct map *map, u64 ip) +{ + return map->unmap_ip(map, ip); +} + static inline u64 map__start(const struct map *map) { return map->start; diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 1fd57db72226..ffd4a4a64026 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -194,7 +194,7 @@ struct symbol *maps__find_symbol(struct maps *maps, u64= addr, struct map **mapp) if (map !=3D NULL && map__load(map) >=3D 0) { if (mapp !=3D NULL) *mapp =3D map; - return map__find_symbol(map, map->map_ip(map, addr)); + return map__find_symbol(map, map__map_ip(map, addr)); } =20 return NULL; @@ -237,7 +237,7 @@ int maps__find_ams(struct maps *maps, struct addr_map_s= ymbol *ams) return -1; } =20 - ams->al_addr =3D ams->ms.map->map_ip(ams->ms.map, ams->addr); + ams->al_addr =3D map__map_ip(ams->ms.map, ams->addr); ams->ms.sym =3D map__find_symbol(ams->ms.map, ams->al_addr); =20 return ams->ms.sym ? 0 : -1; @@ -349,8 +349,8 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) =20 after->start =3D map__end(map); after->pgoff +=3D map__end(map) - map__start(pos->map); - assert(pos->map->map_ip(pos->map, map__end(map)) =3D=3D - after->map_ip(after, map__end(map))); + assert(map__map_ip(pos->map, map__end(map)) =3D=3D + map__map_ip(after, map__end(map))); err =3D __maps__insert(maps, after); if (err) goto put_map; diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index 4d9dbeeb6014..bb44a3798df8 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -141,7 +141,7 @@ static int kernel_get_symbol_address_by_name(const char= *name, u64 *addr, sym =3D machine__find_kernel_symbol_by_name(host_machine, name, &map); if (!sym) return -ENOENT; - *addr =3D map->unmap_ip(map, sym->start) - + *addr =3D map__unmap_ip(map, sym->start) - ((reloc) ? 0 : map->reloc) - ((reladdr) ? map__start(map) : 0); } @@ -400,7 +400,7 @@ static int find_alternative_probe_point(struct debuginf= o *dinfo, "Consider identifying the final function used at run time and set = the probe directly on that.\n", pp->function); } else - address =3D map->unmap_ip(map, sym->start) - map->reloc; + address =3D map__unmap_ip(map, sym->start) - map->reloc; break; } if (!address) { @@ -2249,7 +2249,7 @@ static int find_perf_probe_point_from_map(struct prob= e_trace_point *tp, goto out; =20 pp->retprobe =3D tp->retprobe; - pp->offset =3D addr - map->unmap_ip(map, sym->start); + pp->offset =3D addr - map__unmap_ip(map, sym->start); pp->function =3D strdup(sym->name); ret =3D pp->function ? 0 : -ENOMEM; =20 @@ -3123,7 +3123,7 @@ static int find_probe_trace_events_from_map(struct pe= rf_probe_event *pev, goto err_out; } /* Add one probe point */ - tp->address =3D map->unmap_ip(map, sym->start) + pp->offset; + tp->address =3D map__unmap_ip(map, sym->start) + pp->offset; =20 /* Check the kprobe (not in module) is within .text */ if (!pev->uprobes && !pev->target && diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index cbf09eaf3734..41d4f9e6a8b7 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -471,7 +471,7 @@ static PyObject *python_process_callchain(struct perf_s= ample *sample, struct addr_location node_al; unsigned long offset; =20 - node_al.addr =3D map->map_ip(map, node->ip); + node_al.addr =3D map__map_ip(map, node->ip); node_al.map =3D map; offset =3D get_offset(node->ms.sym, &node_al); =20 diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index d7b6b734bf90..321d4859ae16 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c @@ -318,7 +318,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol = *ms, u64 rip =3D ip; =20 if (dso && dso->kernel && dso->adjust_symbols) - rip =3D map->unmap_ip(map, ip); + rip =3D map__unmap_ip(map, ip); =20 ret +=3D repsep_snprintf(bf, size, "%-#*llx %c ", BITS_PER_LONG / 4 + 2, rip, o); @@ -329,7 +329,7 @@ static int _hist_entry__sym_snprintf(struct map_symbol = *ms, if (sym->type =3D=3D STT_OBJECT) { ret +=3D repsep_snprintf(bf + ret, size - ret, "%s", sym->name); ret +=3D repsep_snprintf(bf + ret, size - ret, "+0x%llx", - ip - map->unmap_ip(map, sym->start)); + ip - map__unmap_ip(map, sym->start)); } else { ret +=3D repsep_snprintf(bf + ret, size - ret, "%.*s", width - ret, @@ -1106,7 +1106,7 @@ static int _hist_entry__addr_snprintf(struct map_symb= ol *ms, if (sym->type =3D=3D STT_OBJECT) { ret +=3D repsep_snprintf(bf + ret, size - ret, "%s", sym->name); ret +=3D repsep_snprintf(bf + ret, size - ret, "+0x%llx", - ip - map->unmap_ip(map, sym->start)); + ip - map__unmap_ip(map, sym->start)); } else { ret +=3D repsep_snprintf(bf + ret, size - ret, "%.*s", width - ret, @@ -2063,9 +2063,9 @@ sort__addr_cmp(struct hist_entry *left, struct hist_e= ntry *right) struct map *right_map =3D right->ms.map; =20 if (left_map) - left_ip =3D left_map->unmap_ip(left_map, left_ip); + left_ip =3D map__unmap_ip(left_map, left_ip); if (right_map) - right_ip =3D right_map->unmap_ip(right_map, right_ip); + right_ip =3D map__unmap_ip(right_map, right_ip); =20 return _sort__addr_cmp(left_ip, right_ip); } @@ -2077,7 +2077,7 @@ static int hist_entry__addr_snprintf(struct hist_entr= y *he, char *bf, struct map *map =3D he->ms.map; =20 if (map) - ip =3D map->unmap_ip(map, ip); + ip =3D map__unmap_ip(map, ip); =20 return repsep_snprintf(bf, size, "%-#*llx", width, ip); } diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 7ea272daafe5..ea80c4a07609 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -891,8 +891,8 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, * So that we look just like we get from .ko files, * i.e. not prelinked, relative to initial_map->start. */ - pos->start =3D curr_map->map_ip(curr_map, pos->start); - pos->end =3D curr_map->map_ip(curr_map, pos->end); + pos->start =3D map__map_ip(curr_map, pos->start); + pos->end =3D map__map_ip(curr_map, pos->end); } else if (x86_64 && is_entry_trampoline(pos->name)) { /* * These symbols are not needed anymore since the diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 42fdc80a6f2e..6fe503da712b 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -459,7 +459,7 @@ int thread__memcpy(struct thread *thread, struct machin= e *machine, map__dso(al.map)->data.status =3D=3D DSO_DATA_STATUS_ERROR || map__load(= al.map) < 0) return -1; =20 - offset =3D al.map->map_ip(al.map, ip); + offset =3D map__map_ip(al.map, ip); if (is64bit) *is64bit =3D map__dso(al.map)->is_64_bit; =20 diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c index b79f57e5648f..538320e4260c 100644 --- a/tools/perf/util/unwind-libdw.c +++ b/tools/perf/util/unwind-libdw.c @@ -115,7 +115,7 @@ static int entry(u64 ip, struct unwind_info *ui) pr_debug("unwind: %s:ip =3D 0x%" PRIx64 " (0x%" PRIx64 ")\n", al.sym ? al.sym->name : "''", ip, - al.map ? al.map->map_ip(al.map, ip) : (u64) 0); + al.map ? map__map_ip(al.map, ip) : (u64) 0); return 0; } =20 --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 73C9EC6FD1D for ; Mon, 20 Mar 2023 03:42:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230005AbjCTDmJ (ORCPT ); Sun, 19 Mar 2023 23:42:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229999AbjCTDlH (ORCPT ); Sun, 19 Mar 2023 23:41:07 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 485C01E1D1 for ; Sun, 19 Mar 2023 20:40:19 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id m6-20020a056902118600b00aeb1e3dbd1bso11643577ybu.9 for ; Sun, 19 Mar 2023 20:40:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283617; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bEJ5nngxb+YsJBYVKsyl4Y08iHIOpJbTkoy32Inc7VA=; b=B8Y9M4TNFto9BkAnqK46CmFOfU81BEhghn+YQaAs8S8DTmMGdUFP3JX2ZY4bLUJ9sO 7oTaj2yuDz0kemM7NZrwzmkxkZA6/x9BkLXlTtAKkuEfAiJbAp2egVGYIG5IWEnPzvGK UT0dmgYdeOydLdVpMLICrWuhBwmKQZWq5roLLooAl5VdoTd/+Io9EwvqW0jZSaQVdLIM p8EXl0X14HKJUq9osnowRdr70mfasAhZAd5Wocd5o5mFu2DNi2Lrf7qrkTFrbFE9xJJf ussWqaubSWth5bKk6qO8nMaV0ozBtADvpyJz8d2YrbMPTUMbPZkmOxrSxR0iNzhve5Gl aQrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283617; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bEJ5nngxb+YsJBYVKsyl4Y08iHIOpJbTkoy32Inc7VA=; b=i+oiM/aHTuodkLl0uOeTnnbkTg0JpOM9coPprMRAE9s8Uzity1H5QVvgLN8DQek+4e cKezg4ZM5DY6ePQux0bYozO5L3nj/O8fqEFU1olEFMIQ/njeW5Op9GxIgN4UFCEzd4P7 c3e8yhZMTUkegXx+cllMrGjPSuheyIyt8Rs/K1HU6ljo4q50GZrMQ2Vr4iH3MeslQPSR BREfWvtn1hSnDE+Ht/KzJ7P7DXovyjeoFo+hqg7qoSZ7cNkDplJgR/hGMOOWo77Lm746 mLXUgwLooBb8/2DEsZTok3kR+aFn8ugRWawIy001OcPaVQFGLeUdEynUEA5eYD+UEowh DV5w== X-Gm-Message-State: AO0yUKW0fpzdxsvlbsfUd66QuMR+8RVBM3uHKshTG5hVmp1dB5aR6lpe Nd3yyNGPyrDGuUYv7CwWeCRTKFycK1pM X-Google-Smtp-Source: AK7set9GJDOya15LEUfsxc4TAujX7u728aKlDndr/xyfFwVAD7tJLXojxR2Wb5jNFUXso3DRPJW+IfUTaICS X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:154e:b0:b6a:9ef0:2060 with SMTP id r14-20020a056902154e00b00b6a9ef02060mr2524010ybu.1.1679283617318; Sun, 19 Mar 2023 20:40:17 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:01 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-14-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 13/22] perf map: Add accessors for prot, priv and flags From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Later changes will add reference count checking for struct map. Add an accessor so that the reference count check is only necessary in one place. Signed-off-by: Ian Rogers --- tools/perf/builtin-inject.c | 2 +- tools/perf/builtin-report.c | 9 +++++---- tools/perf/tests/vmlinux-kallsyms.c | 4 ++-- tools/perf/util/map.h | 15 +++++++++++++++ tools/perf/util/sort.c | 6 +++--- tools/perf/util/symbol.c | 4 ++-- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 8f6909dd8a54..fd2b38458a5d 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -758,7 +758,7 @@ int perf_event__inject_buildid(struct perf_tool *tool, = union perf_event *event, if (!dso->hit) { dso->hit =3D 1; dso__inject_build_id(dso, tool, machine, - sample->cpumode, al.map->flags); + sample->cpumode, map__flags(al.map)); } } =20 diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 4ce1aef3e253..8650d9503b77 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -845,13 +845,14 @@ static size_t maps__fprintf_task(struct maps *maps, i= nt indent, FILE *fp) maps__for_each_entry(maps, rb_node) { struct map *map =3D rb_node->map; const struct dso *dso =3D map__dso(map); + u32 prot =3D map__prot(map); =20 printed +=3D fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRI= x64 " %" PRIu64 " %s\n", indent, "", map__start(map), map__end(map), - map->prot & PROT_READ ? 'r' : '-', - map->prot & PROT_WRITE ? 'w' : '-', - map->prot & PROT_EXEC ? 'x' : '-', - map->flags & MAP_SHARED ? 's' : 'p', + prot & PROT_READ ? 'r' : '-', + prot & PROT_WRITE ? 'w' : '-', + prot & PROT_EXEC ? 'x' : '-', + map__flags(map) ? 's' : 'p', map->pgoff, dso->id.ino, dso->name); } diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux= -kallsyms.c index 05a322ea3f9f..7db102868bc2 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -323,7 +323,7 @@ static int test__vmlinux_matches_kallsyms(struct test_s= uite *test __maybe_unused mem_end =3D map__unmap_ip(vmlinux_map, map__end(map)); =20 pair =3D maps__find(kallsyms.kmaps, mem_start); - if (pair =3D=3D NULL || pair->priv) + if (pair =3D=3D NULL || map__priv(pair)) continue; =20 if (map__start(pair) =3D=3D mem_start) { @@ -351,7 +351,7 @@ static int test__vmlinux_matches_kallsyms(struct test_s= uite *test __maybe_unused maps__for_each_entry(maps, rb_node) { struct map *map =3D rb_node->map; =20 - if (!map->priv) { + if (!map__priv(map)) { if (!header_printed) { pr_info("WARN: Maps only in kallsyms:\n"); header_printed =3D true; diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 9118eba71032..fd440c9c279e 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -72,6 +72,21 @@ static inline u64 map__end(const struct map *map) return map->end; } =20 +static inline u32 map__flags(const struct map *map) +{ + return map->flags; +} + +static inline u32 map__prot(const struct map *map) +{ + return map->prot; +} + +static inline bool map__priv(const struct map *map) +{ + return map->priv; +} + static inline size_t map__size(const struct map *map) { return map__end(map) - map__start(map); diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index 321d4859ae16..31a8df42cb2f 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c @@ -1499,7 +1499,7 @@ sort__dcacheline_cmp(struct hist_entry *left, struct = hist_entry *right) */ =20 if ((left->cpumode !=3D PERF_RECORD_MISC_KERNEL) && - (!(l_map->flags & MAP_SHARED)) && !l_dso->id.maj && !l_dso->id.min && + (!(map__flags(l_map) & MAP_SHARED)) && !l_dso->id.maj && !l_dso->id.m= in && !l_dso->id.ino && !l_dso->id.ino_generation) { /* userspace anonymous */ =20 @@ -1535,8 +1535,8 @@ static int hist_entry__dcacheline_snprintf(struct his= t_entry *he, char *bf, =20 /* print [s] for shared data mmaps */ if ((he->cpumode !=3D PERF_RECORD_MISC_KERNEL) && - map && !(map->prot & PROT_EXEC) && - (map->flags & MAP_SHARED) && + map && !(map__prot(map) & PROT_EXEC) && + (map__flags(map) & MAP_SHARED) && (dso->id.maj || dso->id.min || dso->id.ino || dso->id.ino_generation= )) level =3D 's'; else if (!map) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index ea80c4a07609..b73c4bf0137e 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1393,7 +1393,7 @@ static int dso__load_kcore(struct dso *dso, struct ma= p *map, } =20 /* Read new maps into temporary lists */ - err =3D file__read_maps(fd, map->prot & PROT_EXEC, kcore_mapfn, &md, + err =3D file__read_maps(fd, map__prot(map) & PROT_EXEC, kcore_mapfn, &md, &is_64_bit); if (err) goto out_err; @@ -1505,7 +1505,7 @@ static int dso__load_kcore(struct dso *dso, struct ma= p *map, =20 close(fd); =20 - if (map->prot & PROT_EXEC) + if (map__prot(map) & PROT_EXEC) pr_debug("Using %s for kernel object code\n", kcore_filename); else pr_debug("Using %s for kernel data\n", kcore_filename); --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 CF69BC7618A for ; Mon, 20 Mar 2023 03:42:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230008AbjCTDmO (ORCPT ); Sun, 19 Mar 2023 23:42:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229650AbjCTDlc (ORCPT ); Sun, 19 Mar 2023 23:41:32 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BB8C222F7 for ; Sun, 19 Mar 2023 20:40:27 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5425c04765dso109039937b3.0 for ; Sun, 19 Mar 2023 20:40:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283626; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wiouAZ40EdNQ9HzOWi9Q5cOqGSHkPPBEOvAUks0/VHE=; b=IX97UIGEBtiKRAgenEIk0ukW04xt+QkXaOyXR1ev1h2vlBMjRH4mWxH5AhmFSCBb6+ Qsey9L5vqch4pWbOc6zUmEjSb5qWi1DediryZI4nA7LT7EIFl6tCefe9leeDjUgrJThg ytX5mzcbE/Mo5Iel6wUuU2mP+2tib4GBULzOJoFH/+xQM6dEEsdJTdxef2VlLhmgiXfg ese6LEKZliUC5aOM5lR/7aSwJYRsNg4Gvy8BT+0sNtJpazGVNPvht+AZ/MwuNyxLIfCB K89HW5mEfv0bTQ50DFfBwo//OCQR9ogC6aTvjT4rs516yFb8Q/6yvdAhgkO7tNP/pvqQ 1v7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283626; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wiouAZ40EdNQ9HzOWi9Q5cOqGSHkPPBEOvAUks0/VHE=; b=BEmgo4iz3fEQ2oio6YaZe9YkxP16OU8U4kD1EoMrtDIg5oXoiZ4defh1yrhtp/f1yG 3dxDf/IwuVbRho1SNpvVPBwZbY0Gk51INQbhFs/eJ33NGMiBmjmL/RbrPHCKSlswV0Am CfKUZXx0EnZgn2l7WqpOtWr+iwF/0N6h9rcR/J+pp1WmzGSrVKXHQWMnI2BZtxuxwHY+ 1u+NcjnBpcIV/rcnkXQYeREOlWy/r+2moBcPTQ4s7AXLHTaA3nqwDraotu6qXgz2IIc+ +nRNOnSb7VLbUFSzW5y10JqYnFV8XjLFGCrpvys994A8tpW+5rHa4zyuTnTDWL0+umwZ iZzg== X-Gm-Message-State: AO0yUKWDeIPPI5zrPOaqmFuQZdlOfQmA9mrJ/0wixj0JKWCKkY4yQKOO Hy7sFA6DAKFk/QWXIh8BMygTphuaSJ7U X-Google-Smtp-Source: AK7set+sO8vskdeB+ZXyD/v777u22MCMnSTVUlT5666qVbud2c34Y+7qBMGQfXOswgfaY0AN8K7MSYy77B5f X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a5b:d0f:0:b0:b17:e69b:b82a with SMTP id y15-20020a5b0d0f000000b00b17e69bb82amr3328018ybp.8.1679283625811; Sun, 19 Mar 2023 20:40:25 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:02 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-15-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 14/22] perf map: Add accessors for pgoff and reloc From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Later changes will add reference count checking for struct map. Add accessors so that the reference count check is only necessary in one place. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/util/event.c | 2 +- tools/perf/builtin-report.c | 2 +- tools/perf/tests/vmlinux-kallsyms.c | 4 ++-- tools/perf/util/machine.c | 4 ++-- tools/perf/util/map.c | 14 +++++++------- tools/perf/util/map.h | 10 ++++++++++ tools/perf/util/probe-event.c | 8 ++++---- tools/perf/util/symbol.c | 6 +++--- tools/perf/util/unwind-libdw.c | 6 +++--- 9 files changed, 33 insertions(+), 23 deletions(-) diff --git a/tools/perf/arch/x86/util/event.c b/tools/perf/arch/x86/util/ev= ent.c index 3b2475707756..5741ffe47312 100644 --- a/tools/perf/arch/x86/util/event.c +++ b/tools/perf/arch/x86/util/event.c @@ -61,7 +61,7 @@ int perf_event__synthesize_extra_kmaps(struct perf_tool *= tool, =20 event->mmap.start =3D map__start(map); event->mmap.len =3D map__size(map); - event->mmap.pgoff =3D map->pgoff; + event->mmap.pgoff =3D map__pgoff(map); event->mmap.pid =3D machine->pid; =20 strlcpy(event->mmap.filename, kmap->name, PATH_MAX); diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 8650d9503b77..c7e4160f64ad 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -853,7 +853,7 @@ static size_t maps__fprintf_task(struct maps *maps, int= indent, FILE *fp) prot & PROT_WRITE ? 'w' : '-', prot & PROT_EXEC ? 'x' : '-', map__flags(map) ? 's' : 'p', - map->pgoff, + map__pgoff(map), dso->id.ino, dso->name); } =20 diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux= -kallsyms.c index 7db102868bc2..af511233c764 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -335,10 +335,10 @@ static int test__vmlinux_matches_kallsyms(struct test= _suite *test __maybe_unused } =20 pr_info("WARN: %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as", - map__start(map), map__end(map), map->pgoff, dso->name); + map__start(map), map__end(map), map__pgoff(map), dso->name); if (mem_end !=3D map__end(pair)) pr_info(":\nWARN: *%" PRIx64 "-%" PRIx64 " %" PRIx64, - map__start(pair), map__end(pair), pair->pgoff); + map__start(pair), map__end(pair), map__pgoff(pair)); pr_info(" %s\n", dso->name); pair->priv =3D 1; } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index afb77bd161e2..916d98885128 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1276,9 +1276,9 @@ int machine__map_x86_64_entry_trampolines(struct mach= ine *machine, if (!kmap || !is_entry_trampoline(kmap->name)) continue; =20 - dest_map =3D maps__find(kmaps, map->pgoff); + dest_map =3D maps__find(kmaps, map__pgoff(map)); if (dest_map !=3D map) - map->pgoff =3D map__map_ip(dest_map, map->pgoff); + map->pgoff =3D map__map_ip(dest_map, map__pgoff(map)); found =3D true; } if (found || machine->trampolines_mapped) diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 816bffbbf344..1fe367e2cf19 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -421,7 +421,7 @@ size_t map__fprintf(struct map *map, FILE *fp) const struct dso *dso =3D map__dso(map); =20 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", - map__start(map), map__end(map), map->pgoff, dso->name); + map__start(map), map__end(map), map__pgoff(map), dso->name); } =20 size_t map__fprintf_dsoname(struct map *map, FILE *fp) @@ -510,7 +510,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip) return rip; =20 if (dso->rel) - return rip - map->pgoff; + return rip - map__pgoff(map); =20 /* * kernel modules also have DSO_TYPE_USER in dso->kernel, @@ -519,7 +519,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip) if (dso->kernel =3D=3D DSO_SPACE__USER) return rip + dso->text_offset; =20 - return map__unmap_ip(map, rip) - map->reloc; + return map__unmap_ip(map, rip) - map__reloc(map); } =20 /** @@ -542,7 +542,7 @@ u64 map__objdump_2mem(struct map *map, u64 ip) return map__unmap_ip(map, ip); =20 if (dso->rel) - return map__unmap_ip(map, ip + map->pgoff); + return map__unmap_ip(map, ip + map__pgoff(map)); =20 /* * kernel modules also have DSO_TYPE_USER in dso->kernel, @@ -551,7 +551,7 @@ u64 map__objdump_2mem(struct map *map, u64 ip) if (dso->kernel =3D=3D DSO_SPACE__USER) return map__unmap_ip(map, ip - dso->text_offset); =20 - return ip + map->reloc; + return ip + map__reloc(map); } =20 bool map__contains_symbol(const struct map *map, const struct symbol *sym) @@ -592,12 +592,12 @@ struct maps *map__kmaps(struct map *map) =20 u64 map__dso_map_ip(const struct map *map, u64 ip) { - return ip - map__start(map) + map->pgoff; + return ip - map__start(map) + map__pgoff(map); } =20 u64 map__dso_unmap_ip(const struct map *map, u64 ip) { - return ip + map__start(map) - map->pgoff; + return ip + map__start(map) - map__pgoff(map); } =20 u64 identity__map_ip(const struct map *map __maybe_unused, u64 ip) diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index fd440c9c279e..102485699aa8 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -72,6 +72,16 @@ static inline u64 map__end(const struct map *map) return map->end; } =20 +static inline u64 map__pgoff(const struct map *map) +{ + return map->pgoff; +} + +static inline u64 map__reloc(const struct map *map) +{ + return map->reloc; +} + static inline u32 map__flags(const struct map *map) { return map->flags; diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index bb44a3798df8..6e2110d605fb 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -135,14 +135,14 @@ static int kernel_get_symbol_address_by_name(const ch= ar *name, u64 *addr, /* ref_reloc_sym is just a label. Need a special fix*/ reloc_sym =3D kernel_get_ref_reloc_sym(&map); if (reloc_sym && strcmp(name, reloc_sym->name) =3D=3D 0) - *addr =3D (!map->reloc || reloc) ? reloc_sym->addr : + *addr =3D (!map__reloc(map) || reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr; else { sym =3D machine__find_kernel_symbol_by_name(host_machine, name, &map); if (!sym) return -ENOENT; *addr =3D map__unmap_ip(map, sym->start) - - ((reloc) ? 0 : map->reloc) - + ((reloc) ? 0 : map__reloc(map)) - ((reladdr) ? map__start(map) : 0); } return 0; @@ -400,7 +400,7 @@ static int find_alternative_probe_point(struct debuginf= o *dinfo, "Consider identifying the final function used at run time and set = the probe directly on that.\n", pp->function); } else - address =3D map__unmap_ip(map, sym->start) - map->reloc; + address =3D map__unmap_ip(map, sym->start) - map__reloc(map); break; } if (!address) { @@ -866,7 +866,7 @@ post_process_kernel_probe_trace_events(struct probe_tra= ce_event *tevs, free(tevs[i].point.symbol); tevs[i].point.symbol =3D tmp; tevs[i].point.offset =3D tevs[i].point.address - - (map->reloc ? reloc_sym->unrelocated_addr : + (map__reloc(map) ? reloc_sym->unrelocated_addr : reloc_sym->addr); } return skipped; diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index b73c4bf0137e..0431c328f4d6 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -805,11 +805,11 @@ static int maps__split_kallsyms_for_kcore(struct maps= *kmaps, struct dso *dso) continue; } curr_map_dso =3D map__dso(curr_map); - pos->start -=3D map__start(curr_map) - curr_map->pgoff; + pos->start -=3D map__start(curr_map) - map__pgoff(curr_map); if (pos->end > map__end(curr_map)) pos->end =3D map__end(curr_map); if (pos->end) - pos->end -=3D map__start(curr_map) - curr_map->pgoff; + pos->end -=3D map__start(curr_map) - map__pgoff(curr_map); symbols__insert(&curr_map_dso->symbols, pos); ++count; } @@ -1455,7 +1455,7 @@ static int dso__load_kcore(struct dso *dso, struct ma= p *map, if (new_node->map =3D=3D replacement_map) { map->start =3D map__start(new_node->map); map->end =3D map__end(new_node->map); - map->pgoff =3D new_node->map->pgoff; + map->pgoff =3D map__pgoff(new_node->map); map->map_ip =3D new_node->map->map_ip; map->unmap_ip =3D new_node->map->unmap_ip; /* Ensure maps are correctly ordered */ diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c index 538320e4260c..9565f9906e5d 100644 --- a/tools/perf/util/unwind-libdw.c +++ b/tools/perf/util/unwind-libdw.c @@ -62,19 +62,19 @@ static int __report_module(struct addr_location *al, u6= 4 ip, Dwarf_Addr s; =20 dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL); - if (s !=3D map__start(al->map) - al->map->pgoff) + if (s !=3D map__start(al->map) - map__pgoff(al->map)) mod =3D 0; } =20 if (!mod) mod =3D dwfl_report_elf(ui->dwfl, dso->short_name, dso->long_name, -1, - map__start(al->map) - al->map->pgoff, false); + map__start(al->map) - map__pgoff(al->map), false); if (!mod) { char filename[PATH_MAX]; =20 if (dso__build_id_filename(dso, filename, sizeof(filename), false)) mod =3D dwfl_report_elf(ui->dwfl, dso->short_name, filename, -1, - map__start(al->map) - al->map->pgoff, false); + map__start(al->map) - map__pgoff(al->map), false); } =20 if (mod) { --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 73A06C6FD1D for ; Mon, 20 Mar 2023 03:42:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229904AbjCTDmb (ORCPT ); Sun, 19 Mar 2023 23:42:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229861AbjCTDls (ORCPT ); Sun, 19 Mar 2023 23:41:48 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A67922CBC for ; Sun, 19 Mar 2023 20:40:36 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id i11-20020a256d0b000000b0086349255277so11809269ybc.8 for ; Sun, 19 Mar 2023 20:40:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283635; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Py8U8PqFwPIRVvHHDItGJJ+sTjfy4CxHiyg06BPRgeo=; b=n+gKE63K3SKDTQo/iu8pU9gFXI2PAJbYBkvXhYMxrv7G3p+hiuhE4hXChTy3JqmfXu pKDQKBWo91txB3O0gHkwCYsUDlg4BXrQiJXJkx7HWp/SSg1q00qidcrDQ/BbqGBhuTL1 UvdWPY37uZ9xexSslCjA6IkxEDS45AHDt4NOfm8JZochOhauqt97OWLrYRa7gVhruy1g RpVUeYdk40b9OxcCBqJaYr0tcLGmaP9OGlLz8G5jH2rcOXPq0Ekj+DUBOTH523FygzT9 4cN7qK1mnLlwld/+/3nDFJFiJxGak8rLgLaoCaz5kQxyUgZ0E2QmhR1r92tIEQXe2DNy rv3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283635; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Py8U8PqFwPIRVvHHDItGJJ+sTjfy4CxHiyg06BPRgeo=; b=nwzTQsnRE/ydqZey4O9qVLl8bD6vAUKML6KKpM1uYlsE4ueoSzJdVbfdgI5oBpvqW8 snmPgdamXxLSKXTUDenwGV6JSeKZQ43Vj/mZlOtMy2oBSfyKwnz4wR1X+2aoWzLyJBs5 QVFnbnx0DZi3AECTtAGjIdc/D206qzIfiFB0jw02+JAeZd4LEh9maS8MSdCCZTaSAx1m WgbyFsIBhm+Gft7dEvbWv0yz31o3Di/btGZuyeUokpGPpmI6jTdT6tmIaVMy1atSfRXD nYVDQOomNzLfgO8s9X2hXXiIkJ6y5lyoCwFWa0UqDT29o9bE2an5gLU8ZAHzduZEqP5t AiCg== X-Gm-Message-State: AO0yUKWAnCTGIJ3QMgvc3q2rJr6Iy6vnYZaPUkWWVAr4O9fX8Zn9IPFu MpOw+Ab6y1Vpn8VPyWhnoCERiHPxdsdd X-Google-Smtp-Source: AK7set8vSqZsRSfY53oCnbMUg7lQPOwN0CXJrzMHSihbyBNkzmqp/4+dNcN0A9xOYeEdkDXhOjRr9HhciVv9 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a25:8f8b:0:b0:b68:7b14:186b with SMTP id u11-20020a258f8b000000b00b687b14186bmr2946840ybl.1.1679283635105; Sun, 19 Mar 2023 20:40:35 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:03 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-16-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 15/22] perf test: Add extra diagnostics to maps test From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Dump the resultant and comparison maps on failure. Signed-off-by: Ian Rogers --- tools/perf/tests/maps.c | 51 +++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/tools/perf/tests/maps.c b/tools/perf/tests/maps.c index fd0c464fcf95..1c7293476aca 100644 --- a/tools/perf/tests/maps.c +++ b/tools/perf/tests/maps.c @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 +#include #include #include #include "tests.h" @@ -17,22 +18,42 @@ static int check_maps(struct map_def *merged, unsigned = int size, struct maps *ma { struct map_rb_node *rb_node; unsigned int i =3D 0; - - maps__for_each_entry(maps, rb_node) { - struct map *map =3D rb_node->map; - - if (i > 0) - TEST_ASSERT_VAL("less maps expected", (map && i < size) || (!map && i = =3D=3D size)); - - TEST_ASSERT_VAL("wrong map start", map__start(map) =3D=3D merged[i].sta= rt); - TEST_ASSERT_VAL("wrong map end", map__end(map) =3D=3D merged[i].end); - TEST_ASSERT_VAL("wrong map name", !strcmp(map__dso(map)->name, merged[i= ].name)); - TEST_ASSERT_VAL("wrong map refcnt", refcount_read(&map->refcnt) =3D=3D 1= ); - - i++; + bool failed =3D false; + + if (maps__nr_maps(maps) !=3D size) { + pr_debug("Expected %d maps, got %d", size, maps__nr_maps(maps)); + failed =3D true; + } else { + maps__for_each_entry(maps, rb_node) { + struct map *map =3D rb_node->map; + + if (map__start(map) !=3D merged[i].start || + map__end(map) !=3D merged[i].end || + strcmp(map__dso(map)->name, merged[i].name) || + refcount_read(&map->refcnt) !=3D 1) { + failed =3D true; + } + i++; + } } - - return TEST_OK; + if (failed) { + pr_debug("Expected:\n"); + for (i =3D 0; i < size; i++) { + pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: 1\n", + merged[i].start, merged[i].end, merged[i].name); + } + pr_debug("Got:\n"); + maps__for_each_entry(maps, rb_node) { + struct map *map =3D rb_node->map; + + pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: %d\n= ", + map__start(map), + map__end(map), + map__dso(map)->name, + refcount_read(&map->refcnt)); + } + } + return failed ? TEST_FAIL : TEST_OK; } =20 static int test__maps__merge_in(struct test_suite *t __maybe_unused, int s= ubtest __maybe_unused) --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 3B448C7618A for ; Mon, 20 Mar 2023 03:42:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230129AbjCTDmo (ORCPT ); Sun, 19 Mar 2023 23:42:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230070AbjCTDl5 (ORCPT ); Sun, 19 Mar 2023 23:41:57 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7226231F7 for ; Sun, 19 Mar 2023 20:40:43 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 204-20020a2514d5000000b00a3637aea9e1so11875700ybu.17 for ; Sun, 19 Mar 2023 20:40:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283643; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Sc0UoT/sOrVoOx3FR0aFOvpKiVJitcdLEI81kwZKe/g=; b=UmEP33hr/jPyu0yNNb2qGf4hbSQTq8fi6dIbd1w65AkitadLNWgAFBRVIskw3bUef5 tFNkH4yIg0GKr/pw4/15xpiZJQRWK5WcFRNxnsPD6wAnYlqn3OVLLeCoruhZK5BI7PpI CAgb8lADRB3PIShqAqa7B79AAM9QNvkuQ1Tz32gZo6EWNKQnRiusj/RVG42LM17H64bU 0Z39e1rhWe3JlBYvgJZl40GeBH4Z820ZTt7XS3nMiuL27ybtNFUGzkPDujBIfssPEKb9 6fZPy9qmNeQHPylel43XezvOWOAjy6iGTKTLLxaM+9VcQ21jjJ5bi11fBnOmxPWN+HZ4 v4og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283643; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Sc0UoT/sOrVoOx3FR0aFOvpKiVJitcdLEI81kwZKe/g=; b=Aet8x4wpDKV4oKtxXecrYGHthIPMC+1CqdZh0zRIkqn4e0lV/ldojaO8i5QH5VGT96 8x+6WuUD4JaknTK5zm/39Ql8/3u/66qP9AHKCbN8NGymqqBQMwwwrv/UUVqqhRz6Y8zq YShrcZUtMIdvfXp8KkSBzdBpg8tjSUsBbpemxKby2bTYG+e0aZRmii332uNEyVdA7DyZ hPqS48D6N86K6NTh4bRMF+Qq1ZbVMfevmEHrWNWoYqTZIkG3MMVZu9todFER9D8WxtoR qzX+pM3v0eGZYif+V/2cGs0Y32kv7d8tv5mKedWFZI+9dkuCVo+iBzj1O5puEzsi2jw2 tX4w== X-Gm-Message-State: AO0yUKX4p4dqsSvG8o1HDC/0bK3iNToLZFkIMXDeE6e2AJhND1gUGA9r 3h8Esi1KXihn6i8rmpFLpsA5lIjv7ck+ X-Google-Smtp-Source: AK7set8rIGJCJcTTopzK8gDqrWUi43rxBiQxYsPlBLHZMzFHhN/bfigSuXEcRY8MK2r/URicHWHJMXIbvOtn X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a5b:711:0:b0:a42:1dd7:6fc7 with SMTP id g17-20020a5b0711000000b00a421dd76fc7mr4398820ybq.11.1679283642774; Sun, 19 Mar 2023 20:40:42 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:04 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-17-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 16/22] perf maps: Modify maps_by_name to hold a reference to a map From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To make it clearer about the ownership of a reference count split the by-name case from the regular start-address sorted tree. Put the reference count when maps_by_name is freed, which requires moving a decrement to nr_maps in maps__remove. Add two missing map puts in maps__fixup_overlappings in the event maps__insert fails. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 30 ++++++++++++++++-------------- tools/perf/util/symbol.c | 21 +++++++++++++++++---- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index ffd4a4a64026..74e3133f5007 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -26,6 +26,9 @@ static void __maps__free_maps_by_name(struct maps *maps) /* * Free everything to try to do it from the rbtree in the next search */ + for (unsigned int i =3D 0; i < maps__nr_maps(maps); i++) + map__put(maps__maps_by_name(maps)[i]); + zfree(&maps->maps_by_name); maps->nr_maps_allocated =3D 0; } @@ -42,7 +45,7 @@ static int __maps__insert(struct maps *maps, struct map *= map) return -ENOMEM; =20 RB_CLEAR_NODE(&new_rb_node->rb_node); - new_rb_node->map =3D map; + new_rb_node->map =3D map__get(map); =20 while (*p !=3D NULL) { parent =3D *p; @@ -55,7 +58,6 @@ static int __maps__insert(struct maps *maps, struct map *= map) =20 rb_link_node(&new_rb_node->rb_node, parent, p); rb_insert_color(&new_rb_node->rb_node, maps__entries(maps)); - map__get(map); return 0; } =20 @@ -100,7 +102,7 @@ int maps__insert(struct maps *maps, struct map *map) maps->maps_by_name =3D maps_by_name; maps->nr_maps_allocated =3D nr_allocate; } - maps__maps_by_name(maps)[maps__nr_maps(maps) - 1] =3D map; + maps__maps_by_name(maps)[maps__nr_maps(maps) - 1] =3D map__get(map); __maps__sort_by_name(maps); } out: @@ -126,9 +128,9 @@ void maps__remove(struct maps *maps, struct map *map) rb_node =3D maps__find_node(maps, map); assert(rb_node->map =3D=3D map); __maps__remove(maps, rb_node); - --maps->nr_maps; if (maps__maps_by_name(maps)) __maps__free_maps_by_name(maps); + --maps->nr_maps; up_write(maps__lock(maps)); } =20 @@ -136,6 +138,9 @@ static void __maps__purge(struct maps *maps) { struct map_rb_node *pos, *next; =20 + if (maps__maps_by_name(maps)) + __maps__free_maps_by_name(maps); + maps__for_each_entry_safe(maps, pos, next) { rb_erase_init(&pos->rb_node, maps__entries(maps)); map__put(pos->map); @@ -293,7 +298,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) } =20 next =3D first; - while (next) { + while (next && !err) { struct map_rb_node *pos =3D rb_entry(next, struct map_rb_node, rb_node); next =3D rb_next(&pos->rb_node); =20 @@ -331,8 +336,10 @@ int maps__fixup_overlappings(struct maps *maps, struct= map *map, FILE *fp) =20 before->end =3D map__start(map); err =3D __maps__insert(maps, before); - if (err) + if (err) { + map__put(before); goto put_map; + } =20 if (verbose >=3D 2 && !use_browser) map__fprintf(before, fp); @@ -352,22 +359,17 @@ int maps__fixup_overlappings(struct maps *maps, struc= t map *map, FILE *fp) assert(map__map_ip(pos->map, map__end(map)) =3D=3D map__map_ip(after, map__end(map))); err =3D __maps__insert(maps, after); - if (err) + if (err) { + map__put(after); goto put_map; - + } if (verbose >=3D 2 && !use_browser) map__fprintf(after, fp); map__put(after); } put_map: map__put(pos->map); - - if (err) - goto out; } - - err =3D 0; -out: up_write(maps__lock(maps)); return err; } diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 0431c328f4d6..1f4c23052a54 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -2049,10 +2049,23 @@ int dso__load(struct dso *dso, struct map *map) =20 static int map__strcmp(const void *a, const void *b) { - const struct dso *dso_a =3D map__dso(*(const struct map **)a); - const struct dso *dso_b =3D map__dso(*(const struct map **)b); + const struct map *map_a =3D *(const struct map **)a; + const struct map *map_b =3D *(const struct map **)b; + const struct dso *dso_a =3D map__dso(map_a); + const struct dso *dso_b =3D map__dso(map_b); + int ret =3D strcmp(dso_a->short_name, dso_b->short_name); =20 - return strcmp(dso_a->short_name, dso_b->short_name); + if (ret =3D=3D 0 && map_a !=3D map_b) { + /* + * Ensure distinct but name equal maps have an order in part to + * aid reference counting. + */ + ret =3D (int)map__start(map_a) - (int)map__start(map_b); + if (ret =3D=3D 0) + ret =3D (int)((intptr_t)map_a - (intptr_t)map_b); + } + + return ret; } =20 static int map__strcmp_name(const void *name, const void *b) @@ -2084,7 +2097,7 @@ static int map__groups__sort_by_name_from_rbtree(stru= ct maps *maps) maps->nr_maps_allocated =3D maps__nr_maps(maps); =20 maps__for_each_entry(maps, rb_node) - maps_by_name[i++] =3D rb_node->map; + maps_by_name[i++] =3D map__get(rb_node->map); =20 __maps__sort_by_name(maps); =20 --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 C4AB2C6FD1D for ; Mon, 20 Mar 2023 03:42:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230120AbjCTDmj (ORCPT ); Sun, 19 Mar 2023 23:42:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229981AbjCTDlz (ORCPT ); Sun, 19 Mar 2023 23:41:55 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1645322C97 for ; Sun, 19 Mar 2023 20:40:50 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-54161af1984so108725917b3.3 for ; Sun, 19 Mar 2023 20:40:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283649; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7pO+SQR+vKVoLsOLUSYLhBdSXd8TwOVORUXWhtVtT5A=; b=lW0KMu0HiafhHVmvyM5YTCfTUCBViB+erzVHcqC6m4j1quhyFwEE43UBBGqWdbGkAb zHRvLu3SjhCt94FN5A+NnUz3Sb+saDxwCELkqsBIbiOK7fH96IOdYQ1XAGQJsebMjeGA h2hoyY05Q0n/B2M0e/Xw+4udM11cLWTSGIjBIuYjogV+GZeFS5wwzEMiGlypto8IzCtd 5r2W15PSmZIUZKUA7nQZUAXXZXUv+PDgQQYexDzB0Yw+oz5GLl3b7q6lPrA0aPyIuNYw Wiu58w6Z/YXW6HqQf5fkEFU3s2AXuCkFNDT1pnClxtPR8FT42oFykI1oiJy2ZJD192je SHGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283649; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7pO+SQR+vKVoLsOLUSYLhBdSXd8TwOVORUXWhtVtT5A=; b=NVuTxOuBywSrKGsnCX5/XTxTTmpal3o3Dd+NHi7uxU9qZ0IC4UHeSNG1Yw3jtaq1my 4FWKnJlFgfSXm9o4gZn76HJrFh8DyFrBhT6yJn/uBOdY6pX/6h9a08sBP0aqwuw3ekqu EMxT+2IahlFxMbJ0iKLMtPCZ3EACk1r4EDExVXcYt8aSSJKqkN7zOEM936LfNG2k4e2B tk4qc+EeIkXP68W9feKIZ5OpSUQBe66KJXOcUH/BzeJybs0YodAqrmKAp7UG14mtxKfi 1i5aToIi1UR+WUkAhCZcfig8PBP/XVEiNgetmLcdlX1X9vdZRC0QMqIIG+iyqy3cP7iP iG6Q== X-Gm-Message-State: AO0yUKXjKrI2Cio2yZPpBTuLBOW+M51mQllAQQWglAfo1mPcUUmIL3X/ pszqAxcarqQFdC01oB6N/IMkw5RM/y3J X-Google-Smtp-Source: AK7set8K1I4gcOYyiEQAHhSBap3bs+UlT4cuwpqyyRanGb4CYZYqEfBhH1OUb22lM4rAWPrvjzkFdVH4kROe X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a81:d13:0:b0:521:daa4:d687 with SMTP id 19-20020a810d13000000b00521daa4d687mr7157305ywn.0.1679283649725; Sun, 19 Mar 2023 20:40:49 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:05 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-18-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 17/22] perf map: Changes to reference counting From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When a pointer to a map exists do a get, when that pointer is overwritten or freed, put the map. This avoids issues with gets and puts being inconsistently used causing, use after puts, etc. For example, the map in struct addr_location is changed to hold a reference count. Reference count checking and address sanitizer were used to identify issues. Signed-off-by: Ian Rogers --- tools/perf/tests/code-reading.c | 1 + tools/perf/tests/hists_cumulate.c | 10 ++++ tools/perf/tests/hists_filter.c | 10 ++++ tools/perf/tests/hists_link.c | 18 +++++- tools/perf/tests/hists_output.c | 10 ++++ tools/perf/tests/mmap-thread-lookup.c | 1 + tools/perf/util/callchain.c | 9 +-- tools/perf/util/event.c | 8 ++- tools/perf/util/hist.c | 10 ++-- tools/perf/util/machine.c | 79 ++++++++++++++++----------- tools/perf/util/map.c | 2 +- 11 files changed, 114 insertions(+), 44 deletions(-) diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-readin= g.c index 1545fcaa95c6..efe026a35010 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -366,6 +366,7 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, } pr_debug("Bytes read match those read by objdump\n"); out: + map__put(al.map); return err; } =20 diff --git a/tools/perf/tests/hists_cumulate.c b/tools/perf/tests/hists_cum= ulate.c index f00ec9abdbcd..8c0e3f334747 100644 --- a/tools/perf/tests/hists_cumulate.c +++ b/tools/perf/tests/hists_cumulate.c @@ -112,6 +112,7 @@ static int add_hist_entries(struct hists *hists, struct= machine *machine) } =20 fake_samples[i].thread =3D al.thread; + map__put(fake_samples[i].map); fake_samples[i].map =3D al.map; fake_samples[i].sym =3D al.sym; } @@ -147,6 +148,14 @@ static void del_hist_entries(struct hists *hists) } } =20 +static void put_fake_samples(void) +{ + size_t i; + + for (i =3D 0; i < ARRAY_SIZE(fake_samples); i++) + map__put(fake_samples[i].map); +} + typedef int (*test_fn_t)(struct evsel *, struct machine *); =20 #define COMM(he) (thread__comm_str(he->thread)) @@ -733,6 +742,7 @@ static int test__hists_cumulate(struct test_suite *test= __maybe_unused, int subt /* tear down everything */ evlist__delete(evlist); machines__exit(&machines); + put_fake_samples(); =20 return err; } diff --git a/tools/perf/tests/hists_filter.c b/tools/perf/tests/hists_filte= r.c index 7c552549f4a4..98eff5935a1c 100644 --- a/tools/perf/tests/hists_filter.c +++ b/tools/perf/tests/hists_filter.c @@ -89,6 +89,7 @@ static int add_hist_entries(struct evlist *evlist, } =20 fake_samples[i].thread =3D al.thread; + map__put(fake_samples[i].map); fake_samples[i].map =3D al.map; fake_samples[i].sym =3D al.sym; } @@ -101,6 +102,14 @@ static int add_hist_entries(struct evlist *evlist, return TEST_FAIL; } =20 +static void put_fake_samples(void) +{ + size_t i; + + for (i =3D 0; i < ARRAY_SIZE(fake_samples); i++) + map__put(fake_samples[i].map); +} + static int test__hists_filter(struct test_suite *test __maybe_unused, int = subtest __maybe_unused) { int err =3D TEST_FAIL; @@ -322,6 +331,7 @@ static int test__hists_filter(struct test_suite *test _= _maybe_unused, int subtes evlist__delete(evlist); reset_output_field(); machines__exit(&machines); + put_fake_samples(); =20 return err; } diff --git a/tools/perf/tests/hists_link.c b/tools/perf/tests/hists_link.c index e7e4ee57ce04..64ce8097889c 100644 --- a/tools/perf/tests/hists_link.c +++ b/tools/perf/tests/hists_link.c @@ -6,6 +6,7 @@ #include "evsel.h" #include "evlist.h" #include "machine.h" +#include "map.h" #include "parse-events.h" #include "hists_common.h" #include "util/mmap.h" @@ -94,6 +95,7 @@ static int add_hist_entries(struct evlist *evlist, struct= machine *machine) } =20 fake_common_samples[k].thread =3D al.thread; + map__put(fake_common_samples[k].map); fake_common_samples[k].map =3D al.map; fake_common_samples[k].sym =3D al.sym; } @@ -126,11 +128,24 @@ static int add_hist_entries(struct evlist *evlist, st= ruct machine *machine) return -1; } =20 +static void put_fake_samples(void) +{ + size_t i, j; + + for (i =3D 0; i < ARRAY_SIZE(fake_common_samples); i++) + map__put(fake_common_samples[i].map); + for (i =3D 0; i < ARRAY_SIZE(fake_samples); i++) { + for (j =3D 0; j < ARRAY_SIZE(fake_samples[0]); j++) + map__put(fake_samples[i][j].map); + } +} + static int find_sample(struct sample *samples, size_t nr_samples, struct thread *t, struct map *m, struct symbol *s) { while (nr_samples--) { - if (samples->thread =3D=3D t && samples->map =3D=3D m && + if (samples->thread =3D=3D t && + samples->map =3D=3D m && samples->sym =3D=3D s) return 1; samples++; @@ -336,6 +351,7 @@ static int test__hists_link(struct test_suite *test __m= aybe_unused, int subtest evlist__delete(evlist); reset_output_field(); machines__exit(&machines); + put_fake_samples(); =20 return err; } diff --git a/tools/perf/tests/hists_output.c b/tools/perf/tests/hists_outpu= t.c index 428d11a938f2..cebd5226bb12 100644 --- a/tools/perf/tests/hists_output.c +++ b/tools/perf/tests/hists_output.c @@ -78,6 +78,7 @@ static int add_hist_entries(struct hists *hists, struct m= achine *machine) } =20 fake_samples[i].thread =3D al.thread; + map__put(fake_samples[i].map); fake_samples[i].map =3D al.map; fake_samples[i].sym =3D al.sym; } @@ -113,6 +114,14 @@ static void del_hist_entries(struct hists *hists) } } =20 +static void put_fake_samples(void) +{ + size_t i; + + for (i =3D 0; i < ARRAY_SIZE(fake_samples); i++) + map__put(fake_samples[i].map); +} + typedef int (*test_fn_t)(struct evsel *, struct machine *); =20 #define COMM(he) (thread__comm_str(he->thread)) @@ -620,6 +629,7 @@ static int test__hists_output(struct test_suite *test _= _maybe_unused, int subtes /* tear down everything */ evlist__delete(evlist); machines__exit(&machines); + put_fake_samples(); =20 return err; } diff --git a/tools/perf/tests/mmap-thread-lookup.c b/tools/perf/tests/mmap-= thread-lookup.c index 5cc4644e353d..898eda55b7a8 100644 --- a/tools/perf/tests/mmap-thread-lookup.c +++ b/tools/perf/tests/mmap-thread-lookup.c @@ -203,6 +203,7 @@ static int mmap_events(synth_cb synth) } =20 pr_debug("map %p, addr %" PRIx64 "\n", al.map, map__start(al.map)); + map__put(al.map); } =20 machine__delete_threads(machine); diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index 9e9c39dd9d2b..78dc7b6f7ff7 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -589,7 +589,7 @@ fill_node(struct callchain_node *node, struct callchain= _cursor *cursor) } call->ip =3D cursor_node->ip; call->ms =3D cursor_node->ms; - map__get(call->ms.map); + call->ms.map =3D map__get(call->ms.map); call->srcline =3D cursor_node->srcline; =20 if (cursor_node->branch) { @@ -1067,7 +1067,7 @@ int callchain_cursor_append(struct callchain_cursor *= cursor, node->ip =3D ip; map__zput(node->ms.map); node->ms =3D *ms; - map__get(node->ms.map); + node->ms.map =3D map__get(node->ms.map); node->branch =3D branch; node->nr_loop_iter =3D nr_loop_iter; node->iter_cycles =3D iter_cycles; @@ -1115,7 +1115,8 @@ int fill_callchain_info(struct addr_location *al, str= uct callchain_cursor_node * struct machine *machine =3D maps__machine(node->ms.maps); =20 al->maps =3D node->ms.maps; - al->map =3D node->ms.map; + map__put(al->map); + al->map =3D map__get(node->ms.map); al->sym =3D node->ms.sym; al->srcline =3D node->srcline; al->addr =3D node->ip; @@ -1528,7 +1529,7 @@ int callchain_node__make_parent_list(struct callchain= _node *node) goto out; *new =3D *chain; new->has_children =3D false; - map__get(new->ms.map); + new->ms.map =3D map__get(new->ms.map); list_add_tail(&new->list, &head); } parent =3D parent->parent; diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 2712d1a8264e..8293c8a3406b 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -485,13 +485,14 @@ size_t perf_event__fprintf_text_poke(union perf_event= *event, struct machine *ma if (machine) { struct addr_location al; =20 - al.map =3D maps__find(machine__kernel_maps(machine), tp->addr); + al.map =3D map__get(maps__find(machine__kernel_maps(machine), tp->addr)); if (al.map && map__load(al.map) >=3D 0) { al.addr =3D map__map_ip(al.map, tp->addr); al.sym =3D map__find_symbol(al.map, al.addr); if (al.sym) ret +=3D symbol__fprintf_symname_offs(al.sym, &al, fp); } + map__put(al.map); } ret +=3D fprintf(fp, " old len %u new len %u\n", tp->old_len, tp->new_len= ); old =3D true; @@ -582,6 +583,7 @@ struct map *thread__find_map(struct thread *thread, u8 = cpumode, u64 addr, al->filtered =3D 0; =20 if (machine =3D=3D NULL) { + map__put(al->map); al->map =3D NULL; return NULL; } @@ -600,6 +602,7 @@ struct map *thread__find_map(struct thread *thread, u8 = cpumode, u64 addr, al->level =3D 'u'; } else { al->level =3D 'H'; + map__put(al->map); al->map =3D NULL; =20 if ((cpumode =3D=3D PERF_RECORD_MISC_GUEST_USER || @@ -614,7 +617,7 @@ struct map *thread__find_map(struct thread *thread, u8 = cpumode, u64 addr, return NULL; } =20 - al->map =3D maps__find(maps, al->addr); + al->map =3D map__get(maps__find(maps, al->addr)); if (al->map !=3D NULL) { /* * Kernel maps might be changed when loading symbols so loading @@ -773,6 +776,7 @@ int machine__resolve(struct machine *machine, struct ad= dr_location *al, */ void addr_location__put(struct addr_location *al) { + map__zput(al->map); thread__zput(al->thread); } =20 diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index fdf0562d2fd3..02b4bf31b1a7 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -450,7 +450,7 @@ static int hist_entry__init(struct hist_entry *he, memset(&he->stat, 0, sizeof(he->stat)); } =20 - map__get(he->ms.map); + he->ms.map =3D map__get(he->ms.map); =20 if (he->branch_info) { /* @@ -465,13 +465,13 @@ static int hist_entry__init(struct hist_entry *he, memcpy(he->branch_info, template->branch_info, sizeof(*he->branch_info)); =20 - map__get(he->branch_info->from.ms.map); - map__get(he->branch_info->to.ms.map); + he->branch_info->from.ms.map =3D map__get(he->branch_info->from.ms.map); + he->branch_info->to.ms.map =3D map__get(he->branch_info->to.ms.map); } =20 if (he->mem_info) { - map__get(he->mem_info->iaddr.ms.map); - map__get(he->mem_info->daddr.ms.map); + he->mem_info->iaddr.ms.map =3D map__get(he->mem_info->iaddr.ms.map); + he->mem_info->daddr.ms.map =3D map__get(he->mem_info->daddr.ms.map); } =20 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 916d98885128..502e97010a3c 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -880,21 +880,29 @@ static int machine__process_ksymbol_register(struct m= achine *machine, struct symbol *sym; struct dso *dso; struct map *map =3D maps__find(machine__kernel_maps(machine), event->ksym= bol.addr); + bool put_map =3D false; + int err =3D 0; =20 if (!map) { - int err; - dso =3D dso__new(event->ksymbol.name); - if (dso) { - dso->kernel =3D DSO_SPACE__KERNEL; - map =3D map__new2(0, dso); - dso__put(dso); - } =20 - if (!dso || !map) { - return -ENOMEM; + if (!dso) { + err =3D -ENOMEM; + goto out; } - + dso->kernel =3D DSO_SPACE__KERNEL; + map =3D map__new2(0, dso); + dso__put(dso); + if (!map) { + err =3D -ENOMEM; + goto out; + } + /* + * The inserted map has a get on it, we need to put to release + * the reference count here, but do it after all accesses are + * done. + */ + put_map =3D true; if (event->ksymbol.ksym_type =3D=3D PERF_RECORD_KSYMBOL_TYPE_OOL) { dso->binary_type =3D DSO_BINARY_TYPE__OOL; dso->data.file_size =3D event->ksymbol.len; @@ -904,9 +912,10 @@ static int machine__process_ksymbol_register(struct ma= chine *machine, map->start =3D event->ksymbol.addr; map->end =3D map__start(map) + event->ksymbol.len; err =3D maps__insert(machine__kernel_maps(machine), map); - map__put(map); - if (err) - return err; + if (err) { + err =3D -ENOMEM; + goto out; + } =20 dso__set_loaded(dso); =20 @@ -921,10 +930,15 @@ static int machine__process_ksymbol_register(struct m= achine *machine, sym =3D symbol__new(map__map_ip(map, map__start(map)), event->ksymbol.len, 0, 0, event->ksymbol.name); - if (!sym) - return -ENOMEM; + if (!sym) { + err =3D -ENOMEM; + goto out; + } dso__insert_symbol(dso, sym); - return 0; +out: + if (put_map) + map__put(map); + return err; } =20 static int machine__process_ksymbol_unregister(struct machine *machine, @@ -1026,13 +1040,11 @@ static struct map *machine__addnew_module_map(struc= t machine *machine, u64 start goto out; =20 err =3D maps__insert(machine__kernel_maps(machine), map); - - /* Put the map here because maps__insert already got it */ - map__put(map); - /* If maps__insert failed, return NULL. */ - if (err) + if (err) { + map__put(map); map =3D NULL; + } out: /* put the dso here, corresponding to machine__findnew_module_dso */ dso__put(dso); @@ -1324,6 +1336,7 @@ __machine__create_kernel_maps(struct machine *machine= , struct dso *kernel) /* In case of renewal the kernel map, destroy previous one */ machine__destroy_kernel_maps(machine); =20 + map__put(machine->vmlinux_map); machine->vmlinux_map =3D map__new2(0, kernel); if (machine->vmlinux_map =3D=3D NULL) return -ENOMEM; @@ -1612,7 +1625,7 @@ static int machine__create_module(void *arg, const ch= ar *name, u64 start, map->end =3D start + size; =20 dso__kernel_module_get_build_id(map__dso(map), machine->root_dir); - + map__put(map); return 0; } =20 @@ -1658,16 +1671,18 @@ static void machine__set_kernel_mmap(struct machine= *machine, static int machine__update_kernel_mmap(struct machine *machine, u64 start, u64 end) { - struct map *map =3D machine__kernel_map(machine); + struct map *orig, *updated; int err; =20 - map__get(map); - maps__remove(machine__kernel_maps(machine), map); + orig =3D machine->vmlinux_map; + updated =3D map__get(orig); =20 + machine->vmlinux_map =3D updated; machine__set_kernel_mmap(machine, start, end); + maps__remove(machine__kernel_maps(machine), orig); + err =3D maps__insert(machine__kernel_maps(machine), updated); + map__put(orig); =20 - err =3D maps__insert(machine__kernel_maps(machine), map); - map__put(map); return err; } =20 @@ -2294,7 +2309,7 @@ static int add_callchain_ip(struct thread *thread, { struct map_symbol ms; struct addr_location al; - int nr_loop_iter =3D 0; + int nr_loop_iter =3D 0, err; u64 iter_cycles =3D 0; const char *srcline =3D NULL; =20 @@ -2355,9 +2370,11 @@ static int add_callchain_ip(struct thread *thread, ms.map =3D al.map; ms.sym =3D al.sym; srcline =3D callchain_srcline(&ms, al.addr); - return callchain_cursor_append(cursor, ip, &ms, - branch, flags, nr_loop_iter, - iter_cycles, branch_from, srcline); + err =3D callchain_cursor_append(cursor, ip, &ms, + branch, flags, nr_loop_iter, + iter_cycles, branch_from, srcline); + map__put(al.map); + return err; } =20 struct branch_info *sample__resolve_bstack(struct perf_sample *sample, diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 1fe367e2cf19..acbc37359e06 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -410,7 +410,7 @@ struct map *map__clone(struct map *from) map =3D memdup(from, size); if (map !=3D NULL) { refcount_set(&map->refcnt, 1); - dso__get(dso); + map->dso =3D dso__get(dso); } =20 return map; --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 70EBCC6FD1D for ; Mon, 20 Mar 2023 03:43:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230024AbjCTDnK (ORCPT ); Sun, 19 Mar 2023 23:43:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbjCTDmY (ORCPT ); Sun, 19 Mar 2023 23:42:24 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2729D227B9 for ; Sun, 19 Mar 2023 20:40:58 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5419fb7d6c7so108802217b3.11 for ; Sun, 19 Mar 2023 20:40:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283656; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Dx+Czfooqx4+y3PtKz3VngTOr7flvK3/ZnOwhjyajRw=; b=MnCFeZ3XYBxTeakZd+zOXa4Ab3HZffcdRzC8KKmGbbG9Fn9k1DFYzjlrNzkXYGXnxd 923XA9T3DkAR0cJ6qMDKJ9rhD3B6K8UXQ7POn0CCRqQcNXXrQxg5X0BE0b1a18KdG70D ul5rcjH1TSDixbX/5aMLw+P/mJ2v2v6aS45fo+mDzXKNT1tqCuO1ISTU5MXWf6YAVpTO 3nCtelHziS8hnvRvMZXud4/jWzVF9s8FLsd96uVwPGuS6/rBup1H+o/TDMxalS7iDz0Y SAUjZQ9vSBzD6XhPKS4ZAWFEt4Dxe2TohNmrm2CaXeTkKRG2T+JO6e8WEntCXe14Ejyh X20Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283656; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Dx+Czfooqx4+y3PtKz3VngTOr7flvK3/ZnOwhjyajRw=; b=MiwLZBegECDVVZ18ng2pLMjnVky6QF4D6kneE3qNBHQaaW+F9hh6mg68I42LnUubWt XboYcUsu/W27SBF8eHQXLGtjkb/5MJi3DE2+menO2s2r/2o6N2WEyEf9k9/U12+E4xey AkLoHQj0HebW0nspawKa88+Zr512ZyduFQHSvM0VRaK9GcsNOMJna0/hR3gTOijiWcgD RN4n7HY4gK3cVMyaCzNH8UoKMtkvywbrxac3nAGGPd+pKYVm5mcXWaGqkjPEGQxyJbJ1 bogjMxjkAOE/QWtPZnn1CaGu7UUKS3nDtHtofxWFNSbBYcIkyUwBVi13V6IStmN39leX YpPg== X-Gm-Message-State: AO0yUKUiaexyPYFzEX3qW5lhDYcI/qTp828gbeVDWTgAFYPCQ7Kc+krm npTzaMrwy8p159qFJ3MfEL23w0gAHZ/a X-Google-Smtp-Source: AK7set8Tdbub04SSF+TbbYlkVgHxpVLE5t37DZ88uqqR1sEcI7ns7735e5AVy4IaYP0kJ8d3N8QRGJexgh2j X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:72b:b0:b6b:e967:920d with SMTP id l11-20020a056902072b00b00b6be967920dmr1240639ybt.13.1679283656703; Sun, 19 Mar 2023 20:40:56 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:06 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-19-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 18/22] libperf: Add reference count checking macros. From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The macros serve as a way to debug use of a reference counted struct. The macros add a memory allocated pointer that is interposed between the reference counted original struct at a get and freed by a put. The pointer replaces the original struct, so use of the struct name via APIs remains unchanged. Signed-off-by: Ian Rogers --- tools/lib/perf/include/internal/rc_check.h | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 tools/lib/perf/include/internal/rc_check.h diff --git a/tools/lib/perf/include/internal/rc_check.h b/tools/lib/perf/in= clude/internal/rc_check.h new file mode 100644 index 000000000000..c0626d8beb59 --- /dev/null +++ b/tools/lib/perf/include/internal/rc_check.h @@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __LIBPERF_INTERNAL_RC_CHECK_H +#define __LIBPERF_INTERNAL_RC_CHECK_H + +#include +#include + +/* + * Shared reference count checking macros. + * + * Reference count checking is an approach to sanitizing the use of refere= nce + * counted structs. It leverages address and leak sanitizers to make sure = gets + * are paired with a put. Reference count checking adds a malloc-ed layer = of + * indirection on a get, and frees it on a put. A missed put will be repor= ted as + * a memory leak. A double put will be reported as a double free. Accessing + * after a put will cause a use-after-free and/or a segfault. + */ + +#ifndef REFCNT_CHECKING +/* Replaces "struct foo" so that the pointer may be interposed. */ +#define DECLARE_RC_STRUCT(struct_name) \ + struct struct_name + +/* Declare a reference counted struct variable. */ +#define RC_STRUCT(struct_name) struct struct_name + +/* + * Interpose the indirection. Result will hold the indirection and object = is the + * reference counted struct. + */ +#define ADD_RC_CHK(result, object) (result =3D object, object) + +/* Strip the indirection layer. */ +#define RC_CHK_ACCESS(object) object + +/* Frees the object and the indirection layer. */ +#define RC_CHK_FREE(object) free(object) + +/* A get operation adding the indirection layer. */ +#define RC_CHK_GET(result, object) ADD_RC_CHK(result, object) + +/* A put operation removing the indirection layer. */ +#define RC_CHK_PUT(object) {} + +#else + +/* Replaces "struct foo" so that the pointer may be interposed. */ +#define DECLARE_RC_STRUCT(struct_name) \ + struct original_##struct_name; \ + struct struct_name { \ + struct original_##struct_name *orig; \ + }; \ + struct original_##struct_name + +/* Declare a reference counted struct variable. */ +#define RC_STRUCT(struct_name) struct original_##struct_name + +/* + * Interpose the indirection. Result will hold the indirection and object = is the + * reference counted struct. + */ +#define ADD_RC_CHK(result, object) \ + ( \ + object ? (result =3D malloc(sizeof(*result)), \ + result ? (result->orig =3D object, result) \ + : (result =3D NULL, NULL)) \ + : (result =3D NULL, NULL) \ + ) + +/* Strip the indirection layer. */ +#define RC_CHK_ACCESS(object) object->orig + +/* Frees the object and the indirection layer. */ +#define RC_CHK_FREE(object) \ + do { \ + zfree(&object->orig); \ + free(object); \ + } while(0) + +/* A get operation adding the indirection layer. */ +#define RC_CHK_GET(result, object) ADD_RC_CHK(result, (object ? object->or= ig : NULL)) + +/* A put operation removing the indirection layer. */ +#define RC_CHK_PUT(object) \ + do { \ + if (object) { \ + object->orig =3D NULL; \ + free(object); \ + } \ + } while(0) + +#endif + +#endif /* __LIBPERF_INTERNAL_RC_CHECK_H */ --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 3935DC6FD1D for ; Mon, 20 Mar 2023 03:43:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230156AbjCTDnQ (ORCPT ); Sun, 19 Mar 2023 23:43:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229906AbjCTDm1 (ORCPT ); Sun, 19 Mar 2023 23:42:27 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B599D22DCB for ; Sun, 19 Mar 2023 20:41:05 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-536a4eba107so110149737b3.19 for ; Sun, 19 Mar 2023 20:41:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283663; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Kb4pdWYoXXxgxH3CLp93b6VqLVR3bmV9cj+P/uj/TFI=; b=an758w9gDCkPVYTpMAP6bqnDxZPBVMWOaN89LskDqzL/r5k5M7gBzXiZNplyWnChmu Dh6f3vmVbjSBydCjAKcVK9gJ9iKTOePUVOXSBuffy4cqjOlBEPRxMvUvr6xxmivWzDKq XPhu0iRYnhqLbHYGRlqkuD+1AdSValtid1ByGc4QOj+Iu7vtKd4fdmxkaeLfHJq9CGjp zJM3wEQlcdT2y2ym1LPLPLs2e2U0PdKJkE5uGxzYTp0vu+n2vtVXmbGYryS55s0f3x9z xlzUN/Zb/ovJvjCizieJzIzLiSc++Yvxrz9Ie8nQ9RxT5IyBoXPrzEV8Bt4aA0fJAYih lEPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283663; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Kb4pdWYoXXxgxH3CLp93b6VqLVR3bmV9cj+P/uj/TFI=; b=rhAYRpW2D930nYS+cKgK5n+6bDrb34rQyTt6gtCDBfbhlxy7wckYhmlkxJnX/uStpa sp25EGAWwCPOojqcY5MjEKftlx8Yb1WDgvXNt0QpWRCTCbgDsyjnJo0q8RFArYpwkQ62 JuOgT4+kiwJPQnO4sdLK88lXIB985Je4k2IZuwegYLSYbBfYN5Qk8smBH8I64lMxqhwE 7tc6+b9yfhEPMDkQBYlnfryC+2MNjCc54hEXNpmpnoxwTnQSa9G8dgZ+0DadcHVe197d ngYJ8tIZJ1ZsfY0eCvwz4kdHeUqH7winZuOmxvgBid6RK1PVL8hGoReoDDVUEckkKCB1 12EQ== X-Gm-Message-State: AO0yUKUi6VY1G58CsNHidKX+oZKRTN6tE4F/Ifi5RO1oHmKCbnSbJ/7X rlglyJzCru9tKHpYIE83LImmd0KS2jO3 X-Google-Smtp-Source: AK7set84qNzpSSSoyBu2MugFSfCUnx+OAoDYYdchYnHvV1ko1GNc8WFMYyd4UIVrNQrvdFTN6LrPEXyhhgy9 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:100f:b0:b3b:a48c:b23e with SMTP id w15-20020a056902100f00b00b3ba48cb23emr4698945ybt.2.1679283663691; Sun, 19 Mar 2023 20:41:03 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:07 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-20-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 19/22] perf cpumap: Add reference count checking From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Enabled when REFCNT_CHECKING is defined. The change adds a memory allocated pointer that is interposed between the reference counted cpu map at a get and freed by a put. The pointer replaces the original perf_cpu_map struct, so use of the perf_cpu_map via APIs remains unchanged. Any use of the cpu map without the API requires two versions, handled via the RC_CHK_ACCESS macro. This change is intended to catch: - use after put: using a cpumap after you have put it will cause a segv. - unbalanced puts: two puts for a get will result in a double free that can be captured and reported by tools like address sanitizer, including with the associated stack traces of allocation and frees. - missing puts: if a put is missing then the get turns into a memory leak that can be reported by leak sanitizer, including the stack trace at the point the get occurs. Signed-off-by: Ian Rogers --- tools/lib/perf/Makefile | 2 +- tools/lib/perf/cpumap.c | 94 +++++++++++++----------- tools/lib/perf/include/internal/cpumap.h | 4 +- tools/perf/tests/cpumap.c | 4 +- tools/perf/util/cpumap.c | 40 +++++----- tools/perf/util/pmu.c | 8 +- 6 files changed, 81 insertions(+), 71 deletions(-) diff --git a/tools/lib/perf/Makefile b/tools/lib/perf/Makefile index d8cad124e4c5..3a9b2140aa04 100644 --- a/tools/lib/perf/Makefile +++ b/tools/lib/perf/Makefile @@ -188,7 +188,7 @@ install_lib: libs cp -fpR $(LIBPERF_ALL) $(DESTDIR)$(libdir_SQ) =20 HDRS :=3D bpf_perf.h core.h cpumap.h threadmap.h evlist.h evsel.h event.h = mmap.h -INTERNAL_HDRS :=3D cpumap.h evlist.h evsel.h lib.h mmap.h threadmap.h xyar= ray.h +INTERNAL_HDRS :=3D cpumap.h evlist.h evsel.h lib.h mmap.h rc_check.h threa= dmap.h xyarray.h =20 INSTALL_HDRS_PFX :=3D $(DESTDIR)$(prefix)/include/perf INSTALL_HDRS :=3D $(addprefix $(INSTALL_HDRS_PFX)/, $(HDRS)) diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c index 6cd0be7c1bb4..56eed1ac80d9 100644 --- a/tools/lib/perf/cpumap.c +++ b/tools/lib/perf/cpumap.c @@ -10,16 +10,16 @@ #include #include =20 -static struct perf_cpu_map *perf_cpu_map__alloc(int nr_cpus) +struct perf_cpu_map *perf_cpu_map__alloc(int nr_cpus) { - struct perf_cpu_map *cpus =3D malloc(sizeof(*cpus) + sizeof(struct perf_c= pu) * nr_cpus); - - if (cpus !=3D NULL) { + struct perf_cpu_map *result; + RC_STRUCT(perf_cpu_map) *cpus =3D + malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus); + if (ADD_RC_CHK(result, cpus)) { cpus->nr =3D nr_cpus; refcount_set(&cpus->refcnt, 1); - } - return cpus; + return result; } =20 struct perf_cpu_map *perf_cpu_map__dummy_new(void) @@ -27,7 +27,7 @@ struct perf_cpu_map *perf_cpu_map__dummy_new(void) struct perf_cpu_map *cpus =3D perf_cpu_map__alloc(1); =20 if (cpus) - cpus->map[0].cpu =3D -1; + RC_CHK_ACCESS(cpus)->map[0].cpu =3D -1; =20 return cpus; } @@ -35,23 +35,30 @@ struct perf_cpu_map *perf_cpu_map__dummy_new(void) static void cpu_map__delete(struct perf_cpu_map *map) { if (map) { - WARN_ONCE(refcount_read(&map->refcnt) !=3D 0, + WARN_ONCE(refcount_read(&RC_CHK_ACCESS(map)->refcnt) !=3D 0, "cpu_map refcnt unbalanced\n"); - free(map); + RC_CHK_FREE(map); } } =20 struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map) { - if (map) - refcount_inc(&map->refcnt); - return map; + struct perf_cpu_map *result; + + if (RC_CHK_GET(result, map)) + refcount_inc(&RC_CHK_ACCESS(map)->refcnt); + + return result; } =20 void perf_cpu_map__put(struct perf_cpu_map *map) { - if (map && refcount_dec_and_test(&map->refcnt)) - cpu_map__delete(map); + if (map) { + if (refcount_dec_and_test(&RC_CHK_ACCESS(map)->refcnt)) + cpu_map__delete(map); + else + RC_CHK_PUT(map); + } } =20 static struct perf_cpu_map *cpu_map__default_new(void) @@ -68,7 +75,7 @@ static struct perf_cpu_map *cpu_map__default_new(void) int i; =20 for (i =3D 0; i < nr_cpus; ++i) - cpus->map[i].cpu =3D i; + RC_CHK_ACCESS(cpus)->map[i].cpu =3D i; } =20 return cpus; @@ -94,15 +101,16 @@ static struct perf_cpu_map *cpu_map__trim_new(int nr_c= pus, const struct perf_cpu int i, j; =20 if (cpus !=3D NULL) { - memcpy(cpus->map, tmp_cpus, payload_size); - qsort(cpus->map, nr_cpus, sizeof(struct perf_cpu), cmp_cpu); + memcpy(RC_CHK_ACCESS(cpus)->map, tmp_cpus, payload_size); + qsort(RC_CHK_ACCESS(cpus)->map, nr_cpus, sizeof(struct perf_cpu), cmp_cp= u); /* Remove dups */ j =3D 0; for (i =3D 0; i < nr_cpus; i++) { - if (i =3D=3D 0 || cpus->map[i].cpu !=3D cpus->map[i - 1].cpu) - cpus->map[j++].cpu =3D cpus->map[i].cpu; + if (i =3D=3D 0 || + RC_CHK_ACCESS(cpus)->map[i].cpu !=3D RC_CHK_ACCESS(cpus)->map[i - 1= ].cpu) + RC_CHK_ACCESS(cpus)->map[j++].cpu =3D RC_CHK_ACCESS(cpus)->map[i].cpu; } - cpus->nr =3D j; + RC_CHK_ACCESS(cpus)->nr =3D j; assert(j <=3D nr_cpus); } return cpus; @@ -263,20 +271,20 @@ struct perf_cpu perf_cpu_map__cpu(const struct perf_c= pu_map *cpus, int idx) .cpu =3D -1 }; =20 - if (cpus && idx < cpus->nr) - return cpus->map[idx]; + if (cpus && idx < RC_CHK_ACCESS(cpus)->nr) + return RC_CHK_ACCESS(cpus)->map[idx]; =20 return result; } =20 int perf_cpu_map__nr(const struct perf_cpu_map *cpus) { - return cpus ? cpus->nr : 1; + return cpus ? RC_CHK_ACCESS(cpus)->nr : 1; } =20 bool perf_cpu_map__empty(const struct perf_cpu_map *map) { - return map ? map->map[0].cpu =3D=3D -1 : true; + return map ? RC_CHK_ACCESS(map)->map[0].cpu =3D=3D -1 : true; } =20 int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu) @@ -287,10 +295,10 @@ int perf_cpu_map__idx(const struct perf_cpu_map *cpus= , struct perf_cpu cpu) return -1; =20 low =3D 0; - high =3D cpus->nr; + high =3D RC_CHK_ACCESS(cpus)->nr; while (low < high) { int idx =3D (low + high) / 2; - struct perf_cpu cpu_at_idx =3D cpus->map[idx]; + struct perf_cpu cpu_at_idx =3D RC_CHK_ACCESS(cpus)->map[idx]; =20 if (cpu_at_idx.cpu =3D=3D cpu.cpu) return idx; @@ -316,7 +324,9 @@ struct perf_cpu perf_cpu_map__max(const struct perf_cpu= _map *map) }; =20 // cpu_map__trim_new() qsort()s it, cpu_map__default_new() sorts it as we= ll. - return map->nr > 0 ? map->map[map->nr - 1] : result; + return RC_CHK_ACCESS(map)->nr > 0 + ? RC_CHK_ACCESS(map)->map[RC_CHK_ACCESS(map)->nr - 1] + : result; } =20 /** Is 'b' a subset of 'a'. */ @@ -324,15 +334,15 @@ bool perf_cpu_map__is_subset(const struct perf_cpu_ma= p *a, const struct perf_cpu { if (a =3D=3D b || !b) return true; - if (!a || b->nr > a->nr) + if (!a || RC_CHK_ACCESS(b)->nr > RC_CHK_ACCESS(a)->nr) return false; =20 - for (int i =3D 0, j =3D 0; i < a->nr; i++) { - if (a->map[i].cpu > b->map[j].cpu) + for (int i =3D 0, j =3D 0; i < RC_CHK_ACCESS(a)->nr; i++) { + if (RC_CHK_ACCESS(a)->map[i].cpu > RC_CHK_ACCESS(b)->map[j].cpu) return false; - if (a->map[i].cpu =3D=3D b->map[j].cpu) { + if (RC_CHK_ACCESS(a)->map[i].cpu =3D=3D RC_CHK_ACCESS(b)->map[j].cpu) { j++; - if (j =3D=3D b->nr) + if (j =3D=3D RC_CHK_ACCESS(b)->nr) return true; } } @@ -362,27 +372,27 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_= cpu_map *orig, return perf_cpu_map__get(other); } =20 - tmp_len =3D orig->nr + other->nr; + tmp_len =3D RC_CHK_ACCESS(orig)->nr + RC_CHK_ACCESS(other)->nr; tmp_cpus =3D malloc(tmp_len * sizeof(struct perf_cpu)); if (!tmp_cpus) return NULL; =20 /* Standard merge algorithm from wikipedia */ i =3D j =3D k =3D 0; - while (i < orig->nr && j < other->nr) { - if (orig->map[i].cpu <=3D other->map[j].cpu) { - if (orig->map[i].cpu =3D=3D other->map[j].cpu) + while (i < RC_CHK_ACCESS(orig)->nr && j < RC_CHK_ACCESS(other)->nr) { + if (RC_CHK_ACCESS(orig)->map[i].cpu <=3D RC_CHK_ACCESS(other)->map[j].cp= u) { + if (RC_CHK_ACCESS(orig)->map[i].cpu =3D=3D RC_CHK_ACCESS(other)->map[j]= .cpu) j++; - tmp_cpus[k++] =3D orig->map[i++]; + tmp_cpus[k++] =3D RC_CHK_ACCESS(orig)->map[i++]; } else - tmp_cpus[k++] =3D other->map[j++]; + tmp_cpus[k++] =3D RC_CHK_ACCESS(other)->map[j++]; } =20 - while (i < orig->nr) - tmp_cpus[k++] =3D orig->map[i++]; + while (i < RC_CHK_ACCESS(orig)->nr) + tmp_cpus[k++] =3D RC_CHK_ACCESS(orig)->map[i++]; =20 - while (j < other->nr) - tmp_cpus[k++] =3D other->map[j++]; + while (j < RC_CHK_ACCESS(other)->nr) + tmp_cpus[k++] =3D RC_CHK_ACCESS(other)->map[j++]; assert(k <=3D tmp_len); =20 merged =3D cpu_map__trim_new(k, tmp_cpus); diff --git a/tools/lib/perf/include/internal/cpumap.h b/tools/lib/perf/incl= ude/internal/cpumap.h index 35dd29642296..6c01bee4d048 100644 --- a/tools/lib/perf/include/internal/cpumap.h +++ b/tools/lib/perf/include/internal/cpumap.h @@ -4,6 +4,7 @@ =20 #include #include +#include =20 /** * A sized, reference counted, sorted array of integers representing CPU @@ -12,7 +13,7 @@ * gaps if CPU numbers were used. For events associated with a pid, rather= than * a CPU, a single dummy map with an entry of -1 is used. */ -struct perf_cpu_map { +DECLARE_RC_STRUCT(perf_cpu_map) { refcount_t refcnt; /** Length of the map array. */ int nr; @@ -24,6 +25,7 @@ struct perf_cpu_map { #define MAX_NR_CPUS 2048 #endif =20 +struct perf_cpu_map *perf_cpu_map__alloc(int nr_cpus); int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu= ); bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct pe= rf_cpu_map *b); =20 diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c index 3150fc1fed6f..d6f77b676d11 100644 --- a/tools/perf/tests/cpumap.c +++ b/tools/perf/tests/cpumap.c @@ -68,7 +68,7 @@ static int process_event_cpus(struct perf_tool *tool __ma= ybe_unused, TEST_ASSERT_VAL("wrong nr", perf_cpu_map__nr(map) =3D=3D 2); TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu =3D=3D 1); TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 1).cpu =3D=3D 256); - TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) =3D=3D 1); + TEST_ASSERT_VAL("wrong refcnt", refcount_read(&RC_CHK_ACCESS(map)->refcnt= ) =3D=3D 1); perf_cpu_map__put(map); return 0; } @@ -94,7 +94,7 @@ static int process_event_range_cpus(struct perf_tool *too= l __maybe_unused, TEST_ASSERT_VAL("wrong nr", perf_cpu_map__nr(map) =3D=3D 256); TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu =3D=3D 1); TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__max(map).cpu =3D=3D 256); - TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) =3D=3D 1); + TEST_ASSERT_VAL("wrong refcnt", refcount_read(&RC_CHK_ACCESS(map)->refcnt= ) =3D=3D 1); perf_cpu_map__put(map); return 0; } diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c index 5e564974fba4..22453893105f 100644 --- a/tools/perf/util/cpumap.c +++ b/tools/perf/util/cpumap.c @@ -77,9 +77,9 @@ static struct perf_cpu_map *cpu_map__from_entries(const s= truct perf_record_cpu_m * otherwise it would become 65535. */ if (data->cpus_data.cpu[i] =3D=3D (u16) -1) - map->map[i].cpu =3D -1; + RC_CHK_ACCESS(map)->map[i].cpu =3D -1; else - map->map[i].cpu =3D (int) data->cpus_data.cpu[i]; + RC_CHK_ACCESS(map)->map[i].cpu =3D (int) data->cpus_data.cpu[i]; } } =20 @@ -107,7 +107,7 @@ static struct perf_cpu_map *cpu_map__from_mask(const st= ruct perf_record_cpu_map_ =20 perf_record_cpu_map_data__read_one_mask(data, i, local_copy); for_each_set_bit(cpu, local_copy, 64) - map->map[j++].cpu =3D cpu + cpus_per_i; + RC_CHK_ACCESS(map)->map[j++].cpu =3D cpu + cpus_per_i; } return map; =20 @@ -124,11 +124,11 @@ static struct perf_cpu_map *cpu_map__from_range(const= struct perf_record_cpu_map return NULL; =20 if (data->range_cpu_data.any_cpu) - map->map[i++].cpu =3D -1; + RC_CHK_ACCESS(map)->map[i++].cpu =3D -1; =20 for (int cpu =3D data->range_cpu_data.start_cpu; cpu <=3D data->range_cpu= _data.end_cpu; i++, cpu++) - map->map[i].cpu =3D cpu; + RC_CHK_ACCESS(map)->map[i].cpu =3D cpu; =20 return map; } @@ -160,16 +160,13 @@ size_t cpu_map__fprintf(struct perf_cpu_map *map, FIL= E *fp) =20 struct perf_cpu_map *perf_cpu_map__empty_new(int nr) { - struct perf_cpu_map *cpus =3D malloc(sizeof(*cpus) + sizeof(int) * nr); + struct perf_cpu_map *cpus =3D perf_cpu_map__alloc(nr); =20 if (cpus !=3D NULL) { int i; =20 - cpus->nr =3D nr; for (i =3D 0; i < nr; i++) - cpus->map[i].cpu =3D -1; - - refcount_set(&cpus->refcnt, 1); + RC_CHK_ACCESS(cpus)->map[i].cpu =3D -1; } =20 return cpus; @@ -239,7 +236,7 @@ struct cpu_aggr_map *cpu_aggr_map__new(const struct per= f_cpu_map *cpus, { int idx; struct perf_cpu cpu; - struct cpu_aggr_map *c =3D cpu_aggr_map__empty_new(cpus->nr); + struct cpu_aggr_map *c =3D cpu_aggr_map__empty_new(perf_cpu_map__nr(cpus)= ); =20 if (!c) return NULL; @@ -263,7 +260,7 @@ struct cpu_aggr_map *cpu_aggr_map__new(const struct per= f_cpu_map *cpus, } } /* Trim. */ - if (c->nr !=3D cpus->nr) { + if (c->nr !=3D perf_cpu_map__nr(cpus)) { struct cpu_aggr_map *trimmed_c =3D realloc(c, sizeof(struct cpu_aggr_map) + sizeof(struct aggr_cpu_id) * c->nr); @@ -582,31 +579,32 @@ size_t cpu_map__snprint(struct perf_cpu_map *map, cha= r *buf, size_t size) =20 #define COMMA first ? "" : "," =20 - for (i =3D 0; i < map->nr + 1; i++) { + for (i =3D 0; i < perf_cpu_map__nr(map) + 1; i++) { struct perf_cpu cpu =3D { .cpu =3D INT_MAX }; - bool last =3D i =3D=3D map->nr; + bool last =3D i =3D=3D perf_cpu_map__nr(map); =20 if (!last) - cpu =3D map->map[i]; + cpu =3D perf_cpu_map__cpu(map, i); =20 if (start =3D=3D -1) { start =3D i; if (last) { ret +=3D snprintf(buf + ret, size - ret, "%s%d", COMMA, - map->map[i].cpu); + perf_cpu_map__cpu(map, i).cpu); } - } else if (((i - start) !=3D (cpu.cpu - map->map[start].cpu)) || last) { + } else if (((i - start) !=3D (cpu.cpu - perf_cpu_map__cpu(map, start).cp= u)) || last) { int end =3D i - 1; =20 if (start =3D=3D end) { ret +=3D snprintf(buf + ret, size - ret, "%s%d", COMMA, - map->map[start].cpu); + perf_cpu_map__cpu(map, start).cpu); } else { ret +=3D snprintf(buf + ret, size - ret, "%s%d-%d", COMMA, - map->map[start].cpu, map->map[end].cpu); + perf_cpu_map__cpu(map, start).cpu, + perf_cpu_map__cpu(map, end).cpu); } first =3D false; start =3D i; @@ -633,7 +631,7 @@ size_t cpu_map__snprint_mask(struct perf_cpu_map *map, = char *buf, size_t size) int i, cpu; char *ptr =3D buf; unsigned char *bitmap; - struct perf_cpu last_cpu =3D perf_cpu_map__cpu(map, map->nr - 1); + struct perf_cpu last_cpu =3D perf_cpu_map__cpu(map, perf_cpu_map__nr(map)= - 1); =20 if (buf =3D=3D NULL) return 0; @@ -644,7 +642,7 @@ size_t cpu_map__snprint_mask(struct perf_cpu_map *map, = char *buf, size_t size) return 0; } =20 - for (i =3D 0; i < map->nr; i++) { + for (i =3D 0; i < perf_cpu_map__nr(map); i++) { cpu =3D perf_cpu_map__cpu(map, i).cpu; bitmap[cpu / 8] |=3D 1 << (cpu % 8); } diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 45d9b8e28e16..25bb52e8c147 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1885,13 +1885,13 @@ int perf_pmu__cpus_match(struct perf_pmu *pmu, stru= ct perf_cpu_map *cpus, =20 perf_cpu_map__for_each_cpu(cpu, i, cpus) { if (!perf_cpu_map__has(pmu_cpus, cpu)) - unmatched_cpus->map[unmatched_nr++] =3D cpu; + RC_CHK_ACCESS(unmatched_cpus)->map[unmatched_nr++] =3D cpu; else - matched_cpus->map[matched_nr++] =3D cpu; + RC_CHK_ACCESS(matched_cpus)->map[matched_nr++] =3D cpu; } =20 - unmatched_cpus->nr =3D unmatched_nr; - matched_cpus->nr =3D matched_nr; + RC_CHK_ACCESS(unmatched_cpus)->nr =3D unmatched_nr; + RC_CHK_ACCESS(matched_cpus)->nr =3D matched_nr; *mcpus_ptr =3D matched_cpus; *ucpus_ptr =3D unmatched_cpus; return 0; --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 AB8CFC7618A for ; Mon, 20 Mar 2023 03:43:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230144AbjCTDnk (ORCPT ); Sun, 19 Mar 2023 23:43:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230135AbjCTDm6 (ORCPT ); Sun, 19 Mar 2023 23:42:58 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0462234D8 for ; Sun, 19 Mar 2023 20:41:14 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-541888850d4so110778817b3.21 for ; Sun, 19 Mar 2023 20:41:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283671; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=pn6gwqPn6dkc20Y3Eh8vm1i49cyw3lYEcOJG/9m8aKk=; b=NiOQcDHGuVa/g62h9FdRef0dQ+TFdAQpcESbK48/x4y4DKCLz/cCiJ7eSTW+2FaUho +vt8h1+pEAa2pp2dWrq2G5bLz0281bqJvwR6rNUXJJlA6ycYkWPP5GA+soVVRGYSA6oD ScsRhqIc5gWKoFXPFTZvlY13eBm6O1+2Ncha3+4aktqzNVczL5ztz8IAKR5MS/fuB1Xl BmkX7m9/u/dpqTE8S2+q9BO1TrIitG7vNm1oDCKFdhNrqkPd+NLq4BVJkOvpGveHZglC /5me0No4TPfL7unIzkqrwSG7WLGR1WT942+03stYpIZBYQMIhoDxDolsn5jKx5CNp5qM sr9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283671; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pn6gwqPn6dkc20Y3Eh8vm1i49cyw3lYEcOJG/9m8aKk=; b=T7/phUy0nCdz2IdHiqH/XR4P0+jbKcVIA9BZtc4tmDekTDgoOwpHrPgCw6YcKU/Ss6 GOKrCge6WYH3m/qSXW8S+8f8ibZmumV2sDnOrDxhHmvf64uGWSoPudNriNRz/b8VvP3e 0XquBFnrIr15zfydv/ZdDcCdsLnAPoQrowAb8sL6ngqWWc1G8f6ggV7Nfu6vUAlOQY4+ b/QzsA84ncxWOvI1qHHItmJeFZEJ4lwBc6HEjKu93b6fqu91Ozsoqxca10q4TqUn0Jjy DK+pgHyKY2A1ccKsOSMy/2bJsTRbc4O1d80KloZbXRu1jUum/n9MMynXvUNHWiNgmGPR p4hQ== X-Gm-Message-State: AO0yUKU5fT2UDM7V2it0kOXTQHif0SsEsOtYxQXMY3BujbNYy81FBjzg JjwrPAJDgdxh30BEvf5pi6Oc+q0IiKfe X-Google-Smtp-Source: AK7set/UatlbzEx6HL5P0QXXpFUhS3CcUNYUVMKS2z9YHa+UlelOdKde1lPJG336O7IKjWITD4YK34l3jTjw X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:723:b0:b6a:2590:6c63 with SMTP id l3-20020a056902072300b00b6a25906c63mr3764100ybt.2.1679283671647; Sun, 19 Mar 2023 20:41:11 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:08 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-21-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 20/22] perf namespaces: Add reference count checking From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add reference count checking controlled by REFCNT_CHECKING ifdef. The reference count checking interposes an allocated pointer between the reference counted struct on a get and frees the pointer on a put. Accesses after a put cause faults and use after free, missed puts are caughts as leaks and double puts are double frees. This checking helped resolve a memory leak and use after free: https://lore.kernel.org/linux-perf-users/CAP-5=3DfWZH20L4kv-BwVtGLwR=3DEm3A= OOT+Q4QGivvQuYn5AsPRg@mail.gmail.com/ Signed-off-by: Ian Rogers --- tools/perf/builtin-inject.c | 2 +- tools/perf/util/annotate.c | 2 +- tools/perf/util/dso.c | 2 +- tools/perf/util/dsos.c | 2 +- tools/perf/util/namespaces.c | 132 ++++++++++++++++++++--------------- tools/perf/util/namespaces.h | 3 +- tools/perf/util/symbol.c | 2 +- 7 files changed, 83 insertions(+), 62 deletions(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index fd2b38458a5d..fe6ddcf7fb1e 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -632,7 +632,7 @@ static int dso__read_build_id(struct dso *dso) else if (dso->nsinfo) { char *new_name; =20 - new_name =3D filename_with_chroot(dso->nsinfo->pid, + new_name =3D filename_with_chroot(RC_CHK_ACCESS(dso->nsinfo)->pid, dso->long_name); if (new_name && filename__read_build_id(new_name, &dso->bid) > 0) dso->has_build_id =3D true; diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index e8570b7cc36f..199f6cd5ad1e 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1701,7 +1701,7 @@ static int dso__disassemble_filename(struct dso *dso,= char *filename, size_t fil =20 mutex_lock(&dso->lock); if (access(filename, R_OK) && errno =3D=3D ENOENT && dso->nsinfo) { - char *new_name =3D filename_with_chroot(dso->nsinfo->pid, + char *new_name =3D filename_with_chroot(RC_CHK_ACCESS(dso->nsinfo)->pid, filename); if (new_name) { strlcpy(filename, new_name, filename_size); diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index e36b418df2c6..6c4129598f5d 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -515,7 +515,7 @@ static int __open_dso(struct dso *dso, struct machine *= machine) if (errno !=3D ENOENT || dso->nsinfo =3D=3D NULL) goto out; =20 - new_name =3D filename_with_chroot(dso->nsinfo->pid, name); + new_name =3D filename_with_chroot(RC_CHK_ACCESS(dso->nsinfo)->pid, name); if (!new_name) goto out; =20 diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index 2bd23e4cf19e..53b989072ec5 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -91,7 +91,7 @@ bool __dsos__read_build_ids(struct list_head *head, bool = with_hits) have_build_id =3D true; pos->has_build_id =3D true; } else if (errno =3D=3D ENOENT && pos->nsinfo) { - char *new_name =3D filename_with_chroot(pos->nsinfo->pid, + char *new_name =3D filename_with_chroot(RC_CHK_ACCESS(pos->nsinfo)->pid, pos->long_name); =20 if (new_name && filename__read_build_id(new_name, diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c index dd536220cdb9..8a3b7bd27b19 100644 --- a/tools/perf/util/namespaces.c +++ b/tools/perf/util/namespaces.c @@ -60,7 +60,7 @@ void namespaces__free(struct namespaces *namespaces) free(namespaces); } =20 -static int nsinfo__get_nspid(struct nsinfo *nsi, const char *path) +static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, c= onst char *path) { FILE *f =3D NULL; char *statln =3D NULL; @@ -74,19 +74,18 @@ static int nsinfo__get_nspid(struct nsinfo *nsi, const = char *path) while (getline(&statln, &linesz, f) !=3D -1) { /* Use tgid if CONFIG_PID_NS is not defined. */ if (strstr(statln, "Tgid:") !=3D NULL) { - nsi->tgid =3D (pid_t)strtol(strrchr(statln, '\t'), - NULL, 10); - nsi->nstgid =3D nsinfo__tgid(nsi); + *tgid =3D (pid_t)strtol(strrchr(statln, '\t'), NULL, 10); + *nstgid =3D *tgid; } =20 if (strstr(statln, "NStgid:") !=3D NULL) { nspid =3D strrchr(statln, '\t'); - nsi->nstgid =3D (pid_t)strtol(nspid, NULL, 10); + *nstgid =3D (pid_t)strtol(nspid, NULL, 10); /* * If innermost tgid is not the first, process is in a different * PID namespace. */ - nsi->in_pidns =3D (statln + sizeof("NStgid:") - 1) !=3D nspid; + *in_pidns =3D (statln + sizeof("NStgid:") - 1) !=3D nspid; break; } } @@ -121,8 +120,8 @@ int nsinfo__init(struct nsinfo *nsi) * want to switch as part of looking up dso/map data. */ if (old_stat.st_ino !=3D new_stat.st_ino) { - nsi->need_setns =3D true; - nsi->mntns_path =3D newns; + RC_CHK_ACCESS(nsi)->need_setns =3D true; + RC_CHK_ACCESS(nsi)->mntns_path =3D newns; newns =3D NULL; } =20 @@ -132,13 +131,26 @@ int nsinfo__init(struct nsinfo *nsi) if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsinfo__pid(nsi)) >=3D P= ATH_MAX) goto out; =20 - rv =3D nsinfo__get_nspid(nsi, spath); + rv =3D nsinfo__get_nspid(&RC_CHK_ACCESS(nsi)->tgid, &RC_CHK_ACCESS(nsi)->= nstgid, + &RC_CHK_ACCESS(nsi)->in_pidns, spath); =20 out: free(newns); return rv; } =20 +static struct nsinfo *nsinfo__alloc(void) +{ + struct nsinfo *res; + RC_STRUCT(nsinfo) *nsi; + + nsi =3D calloc(1, sizeof(*nsi)); + if (ADD_RC_CHK(res, nsi)) + refcount_set(&nsi->refcnt, 1); + + return res; +} + struct nsinfo *nsinfo__new(pid_t pid) { struct nsinfo *nsi; @@ -146,22 +158,21 @@ struct nsinfo *nsinfo__new(pid_t pid) if (pid =3D=3D 0) return NULL; =20 - nsi =3D calloc(1, sizeof(*nsi)); - if (nsi !=3D NULL) { - nsi->pid =3D pid; - nsi->tgid =3D pid; - nsi->nstgid =3D pid; - nsi->need_setns =3D false; - nsi->in_pidns =3D false; - /* Init may fail if the process exits while we're trying to look - * at its proc information. In that case, save the pid but - * don't try to enter the namespace. - */ - if (nsinfo__init(nsi) =3D=3D -1) - nsi->need_setns =3D false; + nsi =3D nsinfo__alloc(); + if (!nsi) + return NULL; =20 - refcount_set(&nsi->refcnt, 1); - } + RC_CHK_ACCESS(nsi)->pid =3D pid; + RC_CHK_ACCESS(nsi)->tgid =3D pid; + RC_CHK_ACCESS(nsi)->nstgid =3D pid; + RC_CHK_ACCESS(nsi)->need_setns =3D false; + RC_CHK_ACCESS(nsi)->in_pidns =3D false; + /* Init may fail if the process exits while we're trying to look at its + * proc information. In that case, save the pid but don't try to enter + * the namespace. + */ + if (nsinfo__init(nsi) =3D=3D -1) + RC_CHK_ACCESS(nsi)->need_setns =3D false; =20 return nsi; } @@ -173,21 +184,21 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi) if (nsi =3D=3D NULL) return NULL; =20 - nnsi =3D calloc(1, sizeof(*nnsi)); - if (nnsi !=3D NULL) { - nnsi->pid =3D nsinfo__pid(nsi); - nnsi->tgid =3D nsinfo__tgid(nsi); - nnsi->nstgid =3D nsinfo__nstgid(nsi); - nnsi->need_setns =3D nsinfo__need_setns(nsi); - nnsi->in_pidns =3D nsinfo__in_pidns(nsi); - if (nsi->mntns_path) { - nnsi->mntns_path =3D strdup(nsi->mntns_path); - if (!nnsi->mntns_path) { - free(nnsi); - return NULL; - } + nnsi =3D nsinfo__alloc(); + if (!nnsi) + return NULL; + + RC_CHK_ACCESS(nnsi)->pid =3D nsinfo__pid(nsi); + RC_CHK_ACCESS(nnsi)->tgid =3D nsinfo__tgid(nsi); + RC_CHK_ACCESS(nnsi)->nstgid =3D nsinfo__nstgid(nsi); + RC_CHK_ACCESS(nnsi)->need_setns =3D nsinfo__need_setns(nsi); + RC_CHK_ACCESS(nnsi)->in_pidns =3D nsinfo__in_pidns(nsi); + if (RC_CHK_ACCESS(nsi)->mntns_path) { + RC_CHK_ACCESS(nnsi)->mntns_path =3D strdup(RC_CHK_ACCESS(nsi)->mntns_pat= h); + if (!RC_CHK_ACCESS(nnsi)->mntns_path) { + nsinfo__put(nnsi); + return NULL; } - refcount_set(&nnsi->refcnt, 1); } =20 return nnsi; @@ -195,51 +206,60 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi) =20 static void nsinfo__delete(struct nsinfo *nsi) { - zfree(&nsi->mntns_path); - free(nsi); + if (nsi) { + WARN_ONCE(refcount_read(&RC_CHK_ACCESS(nsi)->refcnt) !=3D 0, + "nsinfo refcnt unbalanced\n"); + zfree(&RC_CHK_ACCESS(nsi)->mntns_path); + RC_CHK_FREE(nsi); + } } =20 struct nsinfo *nsinfo__get(struct nsinfo *nsi) { - if (nsi) - refcount_inc(&nsi->refcnt); - return nsi; + struct nsinfo *result; + + if (RC_CHK_GET(result, nsi)) + refcount_inc(&RC_CHK_ACCESS(nsi)->refcnt); + + return result; } =20 void nsinfo__put(struct nsinfo *nsi) { - if (nsi && refcount_dec_and_test(&nsi->refcnt)) + if (nsi && refcount_dec_and_test(&RC_CHK_ACCESS(nsi)->refcnt)) nsinfo__delete(nsi); + else + RC_CHK_PUT(nsi); } =20 bool nsinfo__need_setns(const struct nsinfo *nsi) { - return nsi->need_setns; + return RC_CHK_ACCESS(nsi)->need_setns; } =20 void nsinfo__clear_need_setns(struct nsinfo *nsi) { - nsi->need_setns =3D false; + RC_CHK_ACCESS(nsi)->need_setns =3D false; } =20 pid_t nsinfo__tgid(const struct nsinfo *nsi) { - return nsi->tgid; + return RC_CHK_ACCESS(nsi)->tgid; } =20 pid_t nsinfo__nstgid(const struct nsinfo *nsi) { - return nsi->nstgid; + return RC_CHK_ACCESS(nsi)->nstgid; } =20 pid_t nsinfo__pid(const struct nsinfo *nsi) { - return nsi->pid; + return RC_CHK_ACCESS(nsi)->pid; } =20 pid_t nsinfo__in_pidns(const struct nsinfo *nsi) { - return nsi->in_pidns; + return RC_CHK_ACCESS(nsi)->in_pidns; } =20 void nsinfo__mountns_enter(struct nsinfo *nsi, @@ -256,7 +276,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, nc->oldns =3D -1; nc->newns =3D -1; =20 - if (!nsi || !nsi->need_setns) + if (!nsi || !RC_CHK_ACCESS(nsi)->need_setns) return; =20 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >=3D PATH_MAX) @@ -270,7 +290,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, if (oldns < 0) goto errout; =20 - newns =3D open(nsi->mntns_path, O_RDONLY); + newns =3D open(RC_CHK_ACCESS(nsi)->mntns_path, O_RDONLY); if (newns < 0) goto errout; =20 @@ -339,9 +359,9 @@ int nsinfo__stat(const char *filename, struct stat *st,= struct nsinfo *nsi) =20 bool nsinfo__is_in_root_namespace(void) { - struct nsinfo nsi; + pid_t tgid =3D 0, nstgid =3D 0; + bool in_pidns =3D false; =20 - memset(&nsi, 0x0, sizeof(nsi)); - nsinfo__get_nspid(&nsi, "/proc/self/status"); - return !nsi.in_pidns; + nsinfo__get_nspid(&tgid, &nstgid, &in_pidns, "/proc/self/status"); + return !in_pidns; } diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h index 567829262c42..8c0731c6cbb7 100644 --- a/tools/perf/util/namespaces.h +++ b/tools/perf/util/namespaces.h @@ -13,6 +13,7 @@ #include #include #include +#include =20 #ifndef HAVE_SETNS_SUPPORT int setns(int fd, int nstype); @@ -29,7 +30,7 @@ struct namespaces { struct namespaces *namespaces__new(struct perf_record_namespaces *event); void namespaces__free(struct namespaces *namespaces); =20 -struct nsinfo { +DECLARE_RC_STRUCT(nsinfo) { pid_t pid; pid_t tgid; pid_t nstgid; diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 1f4c23052a54..103ab925ee2d 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1959,7 +1959,7 @@ int dso__load(struct dso *dso, struct map *map) =20 is_reg =3D is_regular_file(name); if (!is_reg && errno =3D=3D ENOENT && dso->nsinfo) { - char *new_name =3D filename_with_chroot(dso->nsinfo->pid, + char *new_name =3D filename_with_chroot(RC_CHK_ACCESS(dso->nsinfo)->pid, name); if (new_name) { is_reg =3D is_regular_file(new_name); --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 970BDC7618A for ; Mon, 20 Mar 2023 03:43:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230175AbjCTDnq (ORCPT ); Sun, 19 Mar 2023 23:43:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230148AbjCTDnE (ORCPT ); Sun, 19 Mar 2023 23:43:04 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CC0023661 for ; Sun, 19 Mar 2023 20:41:25 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5416d3a321eso109814747b3.12 for ; Sun, 19 Mar 2023 20:41:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283680; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+7ue61OBEym933r9aKReIuhTcMCuPYghhFEEMwn66Vw=; b=li44O/h8VTIylP8PQwDqmb/d2b50565D33sv/n2Y3BON8qKo6XfO1WLXVzIIFuD2uV CfvaptasgU/8wlGKyBQPU3+g+MXzzjFAEs5duMYdRRE8745SSk8GZwUSXrQobIdzZruo 710GV4vWzHdncWZc1X3C50lmftcm0XHGsOXs923OsZytEC1B+FUNio9h3GOyUlTwJkBh li2MTjU4l+6iAleuZLM6pVyQ3ZAUb6W6Dprx9U4cSXylODZzi7GtseBhFzHYcNJ8M5m8 R1IAvklfou5kFSzAmoqjtqV7jp0/NWn0ZbmukbGhsvlUdzv4ghoPRAt3xmZVHodp4vnS s67A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283680; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+7ue61OBEym933r9aKReIuhTcMCuPYghhFEEMwn66Vw=; b=Q/yeMnZvsWXcOuKLwZ2pXA1jgDx5HUUOM6NdYvfIdvpk4uNd3YBUCkW5Bums0wMXU9 ejWYTBiuMczXxVL+zx4ak6Y7mRSSQalQhI1+GgoYV5Hg5Sj9Br2TxbxD3jQiiDfvQIrM N1oHDsoPgP6tPIKWVo689QgxQFP4dJ6o9zjrXOZRp4sHplEAZFbI6n/XtEZoHvn60BhP CLChMalBMUHep4QJRphy035kwaccMDEhl0/rk9+ebtpSKyFjsEZ3odBu1lvdE6J/yAA6 qqJvBtqojjSsryC6SwGfvcOhaIM4sEZoyqnZ4RHI489co1xONKFEqyCIcCA3JvDpeFTJ Np5g== X-Gm-Message-State: AO0yUKW2tJBCX+8vaaIRDCfPe350meL8cUCLkN+pOmtJWFX4re88J0o9 6GToFClhUwCYNwj67+kwOqXWv+t7eoUO X-Google-Smtp-Source: AK7set/JOy3FIj1V10NdLxg9gAllO0JTbwNm7KYqdq//n/nXq2+fgbxLf/gIsFPAiukhccVeA40hfyLTvrVR X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:1002:b0:b47:9e93:47a0 with SMTP id w2-20020a056902100200b00b479e9347a0mr4588077ybt.6.1679283680158; Sun, 19 Mar 2023 20:41:20 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:09 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-22-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 21/22] perf maps: Add reference count checking. From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add reference count checking to make sure of good use of get and put. Add and use accessors to reduce RC_CHK clutter. The only significant issue was in tests/thread-maps-share.c where reference counts were released in the reverse order to acquisition, leading to a use after put. This was fixed by reversing the put order. Signed-off-by: Ian Rogers --- tools/perf/tests/thread-maps-share.c | 29 ++++++------- tools/perf/util/machine.c | 2 +- tools/perf/util/maps.c | 53 +++++++++++++----------- tools/perf/util/maps.h | 17 ++++---- tools/perf/util/symbol.c | 13 +++--- tools/perf/util/unwind-libdw.c | 2 +- tools/perf/util/unwind-libunwind-local.c | 2 +- tools/perf/util/unwind-libunwind.c | 2 +- 8 files changed, 64 insertions(+), 56 deletions(-) diff --git a/tools/perf/tests/thread-maps-share.c b/tools/perf/tests/thread= -maps-share.c index 84edd82c519e..dfe51b21bd7d 100644 --- a/tools/perf/tests/thread-maps-share.c +++ b/tools/perf/tests/thread-maps-share.c @@ -43,12 +43,12 @@ static int test__thread_maps_share(struct test_suite *t= est __maybe_unused, int s leader && t1 && t2 && t3 && other); =20 maps =3D leader->maps; - TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 4); + TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&RC_CHK_ACCESS(maps)->ref= cnt), 4); =20 /* test the maps pointer is shared */ - TEST_ASSERT_VAL("maps don't match", maps =3D=3D t1->maps); - TEST_ASSERT_VAL("maps don't match", maps =3D=3D t2->maps); - TEST_ASSERT_VAL("maps don't match", maps =3D=3D t3->maps); + TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) =3D=3D RC_CHK_ACC= ESS(t1->maps)); + TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) =3D=3D RC_CHK_ACC= ESS(t2->maps)); + TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) =3D=3D RC_CHK_ACC= ESS(t3->maps)); =20 /* * Verify the other leader was created by previous call. @@ -71,25 +71,26 @@ static int test__thread_maps_share(struct test_suite *t= est __maybe_unused, int s machine__remove_thread(machine, other_leader); =20 other_maps =3D other->maps; - TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&other_maps->refcnt), 2); + TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&RC_CHK_ACCESS(other_maps= )->refcnt), 2); =20 - TEST_ASSERT_VAL("maps don't match", other_maps =3D=3D other_leader->maps); + TEST_ASSERT_VAL("maps don't match", + RC_CHK_ACCESS(other_maps) =3D=3D RC_CHK_ACCESS(other_leader->maps)); =20 /* release thread group */ - thread__put(leader); - TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 3); - - thread__put(t1); - TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 2); + thread__put(t3); + TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&RC_CHK_ACCESS(maps)->ref= cnt), 3); =20 thread__put(t2); - TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 1); + TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&RC_CHK_ACCESS(maps)->ref= cnt), 2); =20 - thread__put(t3); + thread__put(t1); + TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&RC_CHK_ACCESS(maps)->ref= cnt), 1); + + thread__put(leader); =20 /* release other group */ thread__put(other_leader); - TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&other_maps->refcnt), 1); + TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&RC_CHK_ACCESS(other_maps= )->refcnt), 1); =20 thread__put(other); =20 diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 502e97010a3c..cfbced348335 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -434,7 +434,7 @@ static struct thread *findnew_guest_code(struct machine= *machine, return NULL; =20 /* Assume maps are set up if there are any */ - if (thread->maps->nr_maps) + if (RC_CHK_ACCESS(thread->maps)->nr_maps) return thread; =20 host_thread =3D machine__find_thread(host_machine, -1, pid); diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 74e3133f5007..3c8bbcb2c204 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -12,13 +12,13 @@ =20 static void maps__init(struct maps *maps, struct machine *machine) { - maps->entries =3D RB_ROOT; + RC_CHK_ACCESS(maps)->entries =3D RB_ROOT; init_rwsem(maps__lock(maps)); - maps->machine =3D machine; - maps->last_search_by_name =3D NULL; - maps->nr_maps =3D 0; - maps->maps_by_name =3D NULL; - refcount_set(&maps->refcnt, 1); + RC_CHK_ACCESS(maps)->machine =3D machine; + RC_CHK_ACCESS(maps)->last_search_by_name =3D NULL; + RC_CHK_ACCESS(maps)->nr_maps =3D 0; + RC_CHK_ACCESS(maps)->maps_by_name =3D NULL; + refcount_set(&RC_CHK_ACCESS(maps)->refcnt, 1); } =20 static void __maps__free_maps_by_name(struct maps *maps) @@ -29,8 +29,8 @@ static void __maps__free_maps_by_name(struct maps *maps) for (unsigned int i =3D 0; i < maps__nr_maps(maps); i++) map__put(maps__maps_by_name(maps)[i]); =20 - zfree(&maps->maps_by_name); - maps->nr_maps_allocated =3D 0; + zfree(&RC_CHK_ACCESS(maps)->maps_by_name); + RC_CHK_ACCESS(maps)->nr_maps_allocated =3D 0; } =20 static int __maps__insert(struct maps *maps, struct map *map) @@ -71,7 +71,7 @@ int maps__insert(struct maps *maps, struct map *map) if (err) goto out; =20 - ++maps->nr_maps; + ++RC_CHK_ACCESS(maps)->nr_maps; =20 if (dso && dso->kernel) { struct kmap *kmap =3D map__kmap(map); @@ -88,7 +88,7 @@ int maps__insert(struct maps *maps, struct map *map) * inserted map and resort. */ if (maps__maps_by_name(maps)) { - if (maps__nr_maps(maps) > maps->nr_maps_allocated) { + if (maps__nr_maps(maps) > RC_CHK_ACCESS(maps)->nr_maps_allocated) { int nr_allocate =3D maps__nr_maps(maps) * 2; struct map **maps_by_name =3D realloc(maps__maps_by_name(maps), nr_allocate * sizeof(map)); @@ -99,8 +99,8 @@ int maps__insert(struct maps *maps, struct map *map) goto out; } =20 - maps->maps_by_name =3D maps_by_name; - maps->nr_maps_allocated =3D nr_allocate; + RC_CHK_ACCESS(maps)->maps_by_name =3D maps_by_name; + RC_CHK_ACCESS(maps)->nr_maps_allocated =3D nr_allocate; } maps__maps_by_name(maps)[maps__nr_maps(maps) - 1] =3D map__get(map); __maps__sort_by_name(maps); @@ -122,15 +122,15 @@ void maps__remove(struct maps *maps, struct map *map) struct map_rb_node *rb_node; =20 down_write(maps__lock(maps)); - if (maps->last_search_by_name =3D=3D map) - maps->last_search_by_name =3D NULL; + if (RC_CHK_ACCESS(maps)->last_search_by_name =3D=3D map) + RC_CHK_ACCESS(maps)->last_search_by_name =3D NULL; =20 rb_node =3D maps__find_node(maps, map); assert(rb_node->map =3D=3D map); __maps__remove(maps, rb_node); if (maps__maps_by_name(maps)) __maps__free_maps_by_name(maps); - --maps->nr_maps; + --RC_CHK_ACCESS(maps)->nr_maps; up_write(maps__lock(maps)); } =20 @@ -162,33 +162,38 @@ bool maps__empty(struct maps *maps) =20 struct maps *maps__new(struct machine *machine) { - struct maps *maps =3D zalloc(sizeof(*maps)); + struct maps *res; + RC_STRUCT(maps) *maps =3D zalloc(sizeof(*maps)); =20 - if (maps !=3D NULL) - maps__init(maps, machine); + if (ADD_RC_CHK(res, maps)) + maps__init(res, machine); =20 - return maps; + return res; } =20 void maps__delete(struct maps *maps) { maps__exit(maps); unwind__finish_access(maps); - free(maps); + RC_CHK_FREE(maps); } =20 struct maps *maps__get(struct maps *maps) { - if (maps) - refcount_inc(&maps->refcnt); + struct maps *result; =20 - return maps; + if (RC_CHK_GET(result, maps)) + refcount_inc(&RC_CHK_ACCESS(maps)->refcnt); + + return result; } =20 void maps__put(struct maps *maps) { - if (maps && refcount_dec_and_test(&maps->refcnt)) + if (maps && refcount_dec_and_test(&RC_CHK_ACCESS(maps)->refcnt)) maps__delete(maps); + else + RC_CHK_PUT(maps); } =20 struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map *= *mapp) diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index bde3390c7096..0af4b7e42fca 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -8,6 +8,7 @@ #include #include #include "rwsem.h" +#include =20 struct ref_reloc_sym; struct machine; @@ -32,7 +33,7 @@ struct map *maps__find(struct maps *maps, u64 addr); for (map =3D maps__first(maps), next =3D map_rb_node__next(map); map; \ map =3D next, next =3D map_rb_node__next(map)) =20 -struct maps { +DECLARE_RC_STRUCT(maps) { struct rb_root entries; struct rw_semaphore lock; struct machine *machine; @@ -65,38 +66,38 @@ void maps__put(struct maps *maps); =20 static inline struct rb_root *maps__entries(struct maps *maps) { - return &maps->entries; + return &RC_CHK_ACCESS(maps)->entries; } =20 static inline struct machine *maps__machine(struct maps *maps) { - return maps->machine; + return RC_CHK_ACCESS(maps)->machine; } =20 static inline struct rw_semaphore *maps__lock(struct maps *maps) { - return &maps->lock; + return &RC_CHK_ACCESS(maps)->lock; } =20 static inline struct map **maps__maps_by_name(struct maps *maps) { - return maps->maps_by_name; + return RC_CHK_ACCESS(maps)->maps_by_name; } =20 static inline unsigned int maps__nr_maps(const struct maps *maps) { - return maps->nr_maps; + return RC_CHK_ACCESS(maps)->nr_maps; } =20 #ifdef HAVE_LIBUNWIND_SUPPORT static inline void *maps__addr_space(struct maps *maps) { - return maps->addr_space; + return RC_CHK_ACCESS(maps)->addr_space; } =20 static inline const struct unwind_libunwind_ops *maps__unwind_libunwind_op= s(const struct maps *maps) { - return maps->unwind_libunwind_ops; + return RC_CHK_ACCESS(maps)->unwind_libunwind_ops; } #endif =20 diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 103ab925ee2d..97e1689d4fd1 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -2093,8 +2093,8 @@ static int map__groups__sort_by_name_from_rbtree(stru= ct maps *maps) up_read(maps__lock(maps)); down_write(maps__lock(maps)); =20 - maps->maps_by_name =3D maps_by_name; - maps->nr_maps_allocated =3D maps__nr_maps(maps); + RC_CHK_ACCESS(maps)->maps_by_name =3D maps_by_name; + RC_CHK_ACCESS(maps)->nr_maps_allocated =3D maps__nr_maps(maps); =20 maps__for_each_entry(maps, rb_node) maps_by_name[i++] =3D map__get(rb_node->map); @@ -2129,11 +2129,12 @@ struct map *maps__find_by_name(struct maps *maps, c= onst char *name) =20 down_read(maps__lock(maps)); =20 - if (maps->last_search_by_name) { - const struct dso *dso =3D map__dso(maps->last_search_by_name); + + if (RC_CHK_ACCESS(maps)->last_search_by_name) { + const struct dso *dso =3D map__dso(RC_CHK_ACCESS(maps)->last_search_by_n= ame); =20 if (strcmp(dso->short_name, name) =3D=3D 0) { - map =3D maps->last_search_by_name; + map =3D RC_CHK_ACCESS(maps)->last_search_by_name; goto out_unlock; } } @@ -2153,7 +2154,7 @@ struct map *maps__find_by_name(struct maps *maps, con= st char *name) map =3D rb_node->map; dso =3D map__dso(map); if (strcmp(dso->short_name, name) =3D=3D 0) { - maps->last_search_by_name =3D map; + RC_CHK_ACCESS(maps)->last_search_by_name =3D map; goto out_unlock; } } diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c index 9565f9906e5d..bdccfc511b7e 100644 --- a/tools/perf/util/unwind-libdw.c +++ b/tools/perf/util/unwind-libdw.c @@ -230,7 +230,7 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg, struct unwind_info *ui, ui_buf =3D { .sample =3D data, .thread =3D thread, - .machine =3D thread->maps->machine, + .machine =3D RC_CHK_ACCESS(thread->maps)->machine, .cb =3D cb, .arg =3D arg, .max_stack =3D max_stack, diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unw= ind-libunwind-local.c index 952c5ee66fe7..2947c210576e 100644 --- a/tools/perf/util/unwind-libunwind-local.c +++ b/tools/perf/util/unwind-libunwind-local.c @@ -667,7 +667,7 @@ static int _unwind__prepare_access(struct maps *maps) { void *addr_space =3D unw_create_addr_space(&accessors, 0); =20 - maps->addr_space =3D addr_space; + RC_CHK_ACCESS(maps)->addr_space =3D addr_space; if (!addr_space) { pr_err("unwind: Can't create unwind address space.\n"); return -ENOMEM; diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-li= bunwind.c index c14f04082377..48a7aeb3f9ec 100644 --- a/tools/perf/util/unwind-libunwind.c +++ b/tools/perf/util/unwind-libunwind.c @@ -14,7 +14,7 @@ struct unwind_libunwind_ops __weak *arm64_unwind_libunwin= d_ops; =20 static void unwind__register_ops(struct maps *maps, struct unwind_libunwin= d_ops *ops) { - maps->unwind_libunwind_ops =3D ops; + RC_CHK_ACCESS(maps)->unwind_libunwind_ops =3D ops; } =20 int unwind__prepare_access(struct maps *maps, struct map *map, bool *initi= alized) --=20 2.40.0.rc1.284.g88254d51c5-goog From nobody Tue Feb 10 18:36:07 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 6B7ABC6FD1D for ; Mon, 20 Mar 2023 03:44:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230187AbjCTDoB (ORCPT ); Sun, 19 Mar 2023 23:44:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230017AbjCTDnK (ORCPT ); Sun, 19 Mar 2023 23:43:10 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F08B62385C for ; Sun, 19 Mar 2023 20:41:29 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-541942bfdccso109816157b3.14 for ; Sun, 19 Mar 2023 20:41:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679283688; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KpvMv0VvC8isLDO2CWzdC2F1PAQOHRFGWprt6s50TXg=; b=beOuuurP75XFQ6gmE/BSOfk2uaqjTkmRd8SPNpWmxRM/7wLN7A4VaUfvzbrKUBeiYF AmkUpEMU+Pj1WZ/74xWLxU0mBVmIp/JxK+A9cX7u4e/Jw5B26PAJbEcV7J5Uo6Xt4p6c d3DK9KVTENpe31344UU+9LGJPpHwVapmKTjPkt27WkIL3YmfxCPE+Mlfk+cBeB3TSotK Hk56aqAeqQ4AbV23DzYu/tg+OcJYhUHPd6MZcz5H+mF0kyWgCWfDKc6yjFfVrirDg86r Hjt0m6BCBwPq5PKqqMjDz6eOkg1dsBAz/auxHEJt3Eu9iPd1b2Cb6iljnzeucyKaXY3m LHMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679283688; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KpvMv0VvC8isLDO2CWzdC2F1PAQOHRFGWprt6s50TXg=; b=yO1ves3bS7qUM0QAD0BsXR+KVQ+upMAw9m1ix1CUNbCBUlB2o5nnIHyXUs8Axxwja5 +SvSVuil3AmF/iveZuzS9SqXXBrUe6xSVKNTc4pvXjRkBwfk/83XxQzqLVaXpoHWOIBQ Zq8W+Apiq9sj6O3OyWgJ9QbrGxbD9D2I+cU6QX+0dk7Lgy5+7XvlwGymcN7PNXiqpYuR rBpd4tX/C3WktLlunnaZndg9sBGghsysjwuCb75/68jOJjn+5uZoj7rvJTSy9SfyF+1N YSziEQvTfuT0NruAczgJXNcypniRZ8u4QITL74jTxzxRPnRekp4qeF8+elhW8iD5xnIW KfIg== X-Gm-Message-State: AO0yUKWSc01tsVscqa9ns6eWTKOx551FvIgZ9i74n9Uy3PznO0ciQLyn pm7ucQdm91qcOtc2v91xm6U/jkeIMDGo X-Google-Smtp-Source: AK7set+tXYOWDpvBSgwVzYB4wB/Fihq/+QZMKOEF5M4zy1zyw+DR8E8BYw2Thab7S/33k1MTVuVjjIHmkzZb X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1895:9fa0:27f5:cb71]) (user=irogers job=sendgmr) by 2002:a05:6902:154f:b0:acd:7374:f154 with SMTP id r15-20020a056902154f00b00acd7374f154mr4690350ybu.7.1679283688293; Sun, 19 Mar 2023 20:41:28 -0700 (PDT) Date: Sun, 19 Mar 2023 20:38:10 -0700 In-Reply-To: <20230320033810.980165-1-irogers@google.com> Message-Id: <20230320033810.980165-23-irogers@google.com> Mime-Version: 1.0 References: <20230320033810.980165-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v4 22/22] perf map: Add reference count checking From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , James Clark , John Garry , Riccardo Mancini , Yury Norov , Andy Shevchenko , Andrew Morton , Adrian Hunter , Leo Yan , Andi Kleen , Thomas Richter , Kan Liang , Madhavan Srinivasan , Shunsuke Nakamura , Song Liu , Masami Hiramatsu , Steven Rostedt , Miaoqian Lin , Stephen Brennan , Kajol Jain , Alexey Bayduraev , German Gomez , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , Dmitry Vyukov , Hao Luo Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There's no strict get/put policy with map that leads to leaks or use after free. Reference count checking identifies correct pairing of gets and puts. Signed-off-by: Ian Rogers --- tools/perf/arch/s390/annotate/instructions.c | 5 +- tools/perf/builtin-top.c | 4 +- tools/perf/tests/hists_link.c | 2 +- tools/perf/tests/maps.c | 20 +++--- tools/perf/tests/vmlinux-kallsyms.c | 4 +- tools/perf/util/annotate.c | 10 +-- tools/perf/util/machine.c | 25 +++---- tools/perf/util/map.c | 69 +++++++++++--------- tools/perf/util/map.h | 32 +++++---- tools/perf/util/maps.c | 13 ++-- tools/perf/util/symbol-elf.c | 26 ++++---- tools/perf/util/symbol.c | 40 +++++++----- 12 files changed, 136 insertions(+), 114 deletions(-) diff --git a/tools/perf/arch/s390/annotate/instructions.c b/tools/perf/arch= /s390/annotate/instructions.c index 6548933e8dc0..9953d510f7c1 100644 --- a/tools/perf/arch/s390/annotate/instructions.c +++ b/tools/perf/arch/s390/annotate/instructions.c @@ -39,8 +39,9 @@ static int s390_call__parse(struct arch *arch, struct ins= _operands *ops, target.addr =3D map__objdump_2mem(map, ops->target.addr); =20 if (maps__find_ams(ms->maps, &target) =3D=3D 0 && - map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.ad= dr)) =3D=3D - ops->target.addr) + map__rip_2objdump(target.ms.map, + RC_CHK_ACCESS(map)->map_ip(target.ms.map, target.addr) + ) =3D=3D ops->target.addr) ops->target.sym =3D target.ms.sym; =20 return 0; diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index b45565f718f4..2605040e6788 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -191,7 +191,7 @@ static void ui__warn_map_erange(struct map *map, struct= symbol *sym, u64 ip) if (use_browser <=3D 0) sleep(5); =20 - map->erange_warned =3D true; + RC_CHK_ACCESS(map)->erange_warned =3D true; } =20 static void perf_top__record_precise_ip(struct perf_top *top, @@ -225,7 +225,7 @@ static void perf_top__record_precise_ip(struct perf_top= *top, */ mutex_unlock(&he->hists->lock); =20 - if (err =3D=3D -ERANGE && !he->ms.map->erange_warned) + if (err =3D=3D -ERANGE && !RC_CHK_ACCESS(he->ms.map)->erange_warned) ui__warn_map_erange(he->ms.map, sym, ip); else if (err =3D=3D -ENOMEM) { pr_err("Not enough memory for annotating '%s' symbol!\n", diff --git a/tools/perf/tests/hists_link.c b/tools/perf/tests/hists_link.c index 64ce8097889c..141e2972e34f 100644 --- a/tools/perf/tests/hists_link.c +++ b/tools/perf/tests/hists_link.c @@ -145,7 +145,7 @@ static int find_sample(struct sample *samples, size_t n= r_samples, { while (nr_samples--) { if (samples->thread =3D=3D t && - samples->map =3D=3D m && + RC_CHK_ACCESS(samples->map) =3D=3D RC_CHK_ACCESS(m) && samples->sym =3D=3D s) return 1; samples++; diff --git a/tools/perf/tests/maps.c b/tools/perf/tests/maps.c index 1c7293476aca..b8dab6278bca 100644 --- a/tools/perf/tests/maps.c +++ b/tools/perf/tests/maps.c @@ -30,7 +30,7 @@ static int check_maps(struct map_def *merged, unsigned in= t size, struct maps *ma if (map__start(map) !=3D merged[i].start || map__end(map) !=3D merged[i].end || strcmp(map__dso(map)->name, merged[i].name) || - refcount_read(&map->refcnt) !=3D 1) { + refcount_read(&RC_CHK_ACCESS(map)->refcnt) !=3D 1) { failed =3D true; } i++; @@ -50,7 +50,7 @@ static int check_maps(struct map_def *merged, unsigned in= t size, struct maps *ma map__start(map), map__end(map), map__dso(map)->name, - refcount_read(&map->refcnt)); + refcount_read(&RC_CHK_ACCESS(map)->refcnt)); } } return failed ? TEST_FAIL : TEST_OK; @@ -95,8 +95,8 @@ static int test__maps__merge_in(struct test_suite *t __ma= ybe_unused, int subtest map =3D dso__new_map(bpf_progs[i].name); TEST_ASSERT_VAL("failed to create map", map); =20 - map->start =3D bpf_progs[i].start; - map->end =3D bpf_progs[i].end; + RC_CHK_ACCESS(map)->start =3D bpf_progs[i].start; + RC_CHK_ACCESS(map)->end =3D bpf_progs[i].end; TEST_ASSERT_VAL("failed to insert map", maps__insert(maps, map) =3D=3D 0= ); map__put(map); } @@ -111,16 +111,16 @@ static int test__maps__merge_in(struct test_suite *t = __maybe_unused, int subtest TEST_ASSERT_VAL("failed to create map", map_kcore3); =20 /* kcore1 map overlaps over all bpf maps */ - map_kcore1->start =3D 100; - map_kcore1->end =3D 1000; + RC_CHK_ACCESS(map_kcore1)->start =3D 100; + RC_CHK_ACCESS(map_kcore1)->end =3D 1000; =20 /* kcore2 map hides behind bpf_prog_2 */ - map_kcore2->start =3D 550; - map_kcore2->end =3D 570; + RC_CHK_ACCESS(map_kcore2)->start =3D 550; + RC_CHK_ACCESS(map_kcore2)->end =3D 570; =20 /* kcore3 map hides behind bpf_prog_3, kcore1 and adds new map */ - map_kcore3->start =3D 880; - map_kcore3->end =3D 1100; + RC_CHK_ACCESS(map_kcore3)->start =3D 880; + RC_CHK_ACCESS(map_kcore3)->end =3D 1100; =20 ret =3D maps__merge_in(maps, map_kcore1); TEST_ASSERT_VAL("failed to merge map", !ret); diff --git a/tools/perf/tests/vmlinux-kallsyms.c b/tools/perf/tests/vmlinux= -kallsyms.c index af511233c764..a087b24463ff 100644 --- a/tools/perf/tests/vmlinux-kallsyms.c +++ b/tools/perf/tests/vmlinux-kallsyms.c @@ -304,7 +304,7 @@ static int test__vmlinux_matches_kallsyms(struct test_s= uite *test __maybe_unused dso->short_name : dso->name)); if (pair) { - pair->priv =3D 1; + RC_CHK_ACCESS(pair)->priv =3D 1; } else { if (!header_printed) { pr_info("WARN: Maps only in vmlinux:\n"); @@ -340,7 +340,7 @@ static int test__vmlinux_matches_kallsyms(struct test_s= uite *test __maybe_unused pr_info(":\nWARN: *%" PRIx64 "-%" PRIx64 " %" PRIx64, map__start(pair), map__end(pair), map__pgoff(pair)); pr_info(" %s\n", dso->name); - pair->priv =3D 1; + RC_CHK_ACCESS(pair)->priv =3D 1; } } =20 diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 199f6cd5ad1e..8bae9d78d8c4 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -280,8 +280,9 @@ static int call__parse(struct arch *arch, struct ins_op= erands *ops, struct map_s target.addr =3D map__objdump_2mem(map, ops->target.addr); =20 if (maps__find_ams(ms->maps, &target) =3D=3D 0 && - map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.ad= dr)) =3D=3D - ops->target.addr) + map__rip_2objdump(target.ms.map, + RC_CHK_ACCESS(map)->map_ip(target.ms.map, target.addr) + ) =3D=3D ops->target.addr) ops->target.sym =3D target.ms.sym; =20 return 0; @@ -409,8 +410,9 @@ static int jump__parse(struct arch *arch, struct ins_op= erands *ops, struct map_s * the symbol searching and disassembly should be done. */ if (maps__find_ams(ms->maps, &target) =3D=3D 0 && - map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.ad= dr)) =3D=3D - ops->target.addr) + map__rip_2objdump(target.ms.map, + RC_CHK_ACCESS(map)->map_ip(target.ms.map, target.addr) + ) =3D=3D ops->target.addr) ops->target.sym =3D target.ms.sym; =20 if (!ops->target.outside) { diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index cfbced348335..6310d74f6d6d 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -909,8 +909,8 @@ static int machine__process_ksymbol_register(struct mac= hine *machine, dso__set_loaded(dso); } =20 - map->start =3D event->ksymbol.addr; - map->end =3D map__start(map) + event->ksymbol.len; + RC_CHK_ACCESS(map)->start =3D event->ksymbol.addr; + RC_CHK_ACCESS(map)->end =3D map__start(map) + event->ksymbol.len; err =3D maps__insert(machine__kernel_maps(machine), map); if (err) { err =3D -ENOMEM; @@ -952,7 +952,7 @@ static int machine__process_ksymbol_unregister(struct m= achine *machine, if (!map) return 0; =20 - if (map !=3D machine->vmlinux_map) + if (RC_CHK_ACCESS(map) !=3D RC_CHK_ACCESS(machine->vmlinux_map)) maps__remove(machine__kernel_maps(machine), map); else { struct dso *dso =3D map__dso(map); @@ -1217,8 +1217,8 @@ int machine__create_extra_kernel_map(struct machine *= machine, if (!map) return -ENOMEM; =20 - map->end =3D xm->end; - map->pgoff =3D xm->pgoff; + RC_CHK_ACCESS(map)->end =3D xm->end; + RC_CHK_ACCESS(map)->pgoff =3D xm->pgoff; =20 kmap =3D map__kmap(map); =20 @@ -1290,7 +1290,7 @@ int machine__map_x86_64_entry_trampolines(struct mach= ine *machine, =20 dest_map =3D maps__find(kmaps, map__pgoff(map)); if (dest_map !=3D map) - map->pgoff =3D map__map_ip(dest_map, map__pgoff(map)); + RC_CHK_ACCESS(map)->pgoff =3D map__map_ip(dest_map, map__pgoff(map)); found =3D true; } if (found || machine->trampolines_mapped) @@ -1341,7 +1341,8 @@ __machine__create_kernel_maps(struct machine *machine= , struct dso *kernel) if (machine->vmlinux_map =3D=3D NULL) return -ENOMEM; =20 - machine->vmlinux_map->map_ip =3D machine->vmlinux_map->unmap_ip =3D ident= ity__map_ip; + RC_CHK_ACCESS(machine->vmlinux_map)->map_ip =3D identity__map_ip; + RC_CHK_ACCESS(machine->vmlinux_map)->unmap_ip =3D identity__map_ip; return maps__insert(machine__kernel_maps(machine), machine->vmlinux_map); } =20 @@ -1622,7 +1623,7 @@ static int machine__create_module(void *arg, const ch= ar *name, u64 start, map =3D machine__addnew_module_map(machine, start, name); if (map =3D=3D NULL) return -1; - map->end =3D start + size; + RC_CHK_ACCESS(map)->end =3D start + size; =20 dso__kernel_module_get_build_id(map__dso(map), machine->root_dir); map__put(map); @@ -1658,14 +1659,14 @@ static int machine__create_modules(struct machine *= machine) static void machine__set_kernel_mmap(struct machine *machine, u64 start, u64 end) { - machine->vmlinux_map->start =3D start; - machine->vmlinux_map->end =3D end; + RC_CHK_ACCESS(machine->vmlinux_map)->start =3D start; + RC_CHK_ACCESS(machine->vmlinux_map)->end =3D end; /* * Be a bit paranoid here, some perf.data file came with * a zero sized synthesized MMAP event for the kernel. */ if (start =3D=3D 0 && end =3D=3D 0) - machine->vmlinux_map->end =3D ~0ULL; + RC_CHK_ACCESS(machine->vmlinux_map)->end =3D ~0ULL; } =20 static int machine__update_kernel_mmap(struct machine *machine, @@ -1809,7 +1810,7 @@ static int machine__process_kernel_mmap_event(struct = machine *machine, if (map =3D=3D NULL) goto out_problem; =20 - map->end =3D map__start(map) + xm->end - xm->start; + RC_CHK_ACCESS(map)->end =3D map__start(map) + xm->end - xm->start; =20 if (build_id__is_defined(bid)) dso__set_build_id(map__dso(map), bid); diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index acbc37359e06..9ac5c909ea9e 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -104,15 +104,15 @@ static inline bool replace_android_lib(const char *fi= lename, char *newfilename) =20 void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso = *dso) { - map->start =3D start; - map->end =3D end; - map->pgoff =3D pgoff; - map->reloc =3D 0; - map->dso =3D dso__get(dso); - map->map_ip =3D map__dso_map_ip; - map->unmap_ip =3D map__dso_unmap_ip; - map->erange_warned =3D false; - refcount_set(&map->refcnt, 1); + RC_CHK_ACCESS(map)->start =3D start; + RC_CHK_ACCESS(map)->end =3D end; + RC_CHK_ACCESS(map)->pgoff =3D pgoff; + RC_CHK_ACCESS(map)->reloc =3D 0; + RC_CHK_ACCESS(map)->dso =3D dso__get(dso); + RC_CHK_ACCESS(map)->map_ip =3D map__dso_map_ip; + RC_CHK_ACCESS(map)->unmap_ip =3D map__dso_unmap_ip; + RC_CHK_ACCESS(map)->erange_warned =3D false; + refcount_set(&RC_CHK_ACCESS(map)->refcnt, 1); } =20 struct map *map__new(struct machine *machine, u64 start, u64 len, @@ -120,11 +120,13 @@ struct map *map__new(struct machine *machine, u64 sta= rt, u64 len, u32 prot, u32 flags, struct build_id *bid, char *filename, struct thread *thread) { - struct map *map =3D malloc(sizeof(*map)); + struct map *res; + RC_STRUCT(map) *map; struct nsinfo *nsi =3D NULL; struct nsinfo *nnsi; =20 - if (map !=3D NULL) { + map =3D malloc(sizeof(*map)); + if (ADD_RC_CHK(res, map)) { char newfilename[PATH_MAX]; struct dso *dso, *header_bid_dso; int anon, no_dso, vdso, android; @@ -167,7 +169,7 @@ struct map *map__new(struct machine *machine, u64 start= , u64 len, if (dso =3D=3D NULL) goto out_delete; =20 - map__init(map, start, start + len, pgoff, dso); + map__init(res, start, start + len, pgoff, dso); =20 if (anon || no_dso) { map->map_ip =3D map->unmap_ip =3D identity__map_ip; @@ -204,10 +206,10 @@ struct map *map__new(struct machine *machine, u64 sta= rt, u64 len, } dso__put(dso); } - return map; + return res; out_delete: nsinfo__put(nsi); - free(map); + RC_CHK_FREE(res); return NULL; } =20 @@ -218,16 +220,18 @@ struct map *map__new(struct machine *machine, u64 sta= rt, u64 len, */ struct map *map__new2(u64 start, struct dso *dso) { - struct map *map =3D calloc(1, (sizeof(*map) + - (dso->kernel ? sizeof(struct kmap) : 0))); - if (map !=3D NULL) { + struct map *res; + RC_STRUCT(map) *map; + + map =3D calloc(1, sizeof(*map) + (dso->kernel ? sizeof(struct kmap) : 0)); + if (ADD_RC_CHK(res, map)) { /* * ->end will be filled after we load all the symbols */ - map__init(map, start, 0, 0, dso); + map__init(res, start, 0, 0, dso); } =20 - return map; + return res; } =20 bool __map__is_kernel(const struct map *map) @@ -292,20 +296,22 @@ bool map__has_symbols(const struct map *map) =20 static void map__exit(struct map *map) { - BUG_ON(refcount_read(&map->refcnt) !=3D 0); - dso__zput(map->dso); + BUG_ON(refcount_read(&RC_CHK_ACCESS(map)->refcnt) !=3D 0); + dso__zput(RC_CHK_ACCESS(map)->dso); } =20 void map__delete(struct map *map) { map__exit(map); - free(map); + RC_CHK_FREE(map); } =20 void map__put(struct map *map) { - if (map && refcount_dec_and_test(&map->refcnt)) + if (map && refcount_dec_and_test(&RC_CHK_ACCESS(map)->refcnt)) map__delete(map); + else + RC_CHK_PUT(map); } =20 void map__fixup_start(struct map *map) @@ -317,7 +323,7 @@ void map__fixup_start(struct map *map) if (nd !=3D NULL) { struct symbol *sym =3D rb_entry(nd, struct symbol, rb_node); =20 - map->start =3D sym->start; + RC_CHK_ACCESS(map)->start =3D sym->start; } } =20 @@ -329,7 +335,7 @@ void map__fixup_end(struct map *map) =20 if (nd !=3D NULL) { struct symbol *sym =3D rb_entry(nd, struct symbol, rb_node); - map->end =3D sym->end; + RC_CHK_ACCESS(map)->end =3D sym->end; } } =20 @@ -400,20 +406,21 @@ struct symbol *map__find_symbol_by_name(struct map *m= ap, const char *name) =20 struct map *map__clone(struct map *from) { - size_t size =3D sizeof(struct map); - struct map *map; + struct map *res; + RC_STRUCT(map) *map; + size_t size =3D sizeof(RC_STRUCT(map)); struct dso *dso =3D map__dso(from); =20 if (dso && dso->kernel) size +=3D sizeof(struct kmap); =20 - map =3D memdup(from, size); - if (map !=3D NULL) { + map =3D memdup(RC_CHK_ACCESS(from), size); + if (ADD_RC_CHK(res, map)) { refcount_set(&map->refcnt, 1); map->dso =3D dso__get(dso); } =20 - return map; + return res; } =20 size_t map__fprintf(struct map *map, FILE *fp) @@ -567,7 +574,7 @@ struct kmap *__map__kmap(struct map *map) =20 if (!dso || !dso->kernel) return NULL; - return (struct kmap *)(map + 1); + return (struct kmap *)(&RC_CHK_ACCESS(map)[1]); } =20 struct kmap *map__kmap(struct map *map) diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 102485699aa8..55d047e818e7 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -10,12 +10,13 @@ #include #include #include +#include =20 struct dso; struct maps; struct machine; =20 -struct map { +DECLARE_RC_STRUCT(map) { u64 start; u64 end; bool erange_warned:1; @@ -49,52 +50,52 @@ u64 identity__map_ip(const struct map *map __maybe_unus= ed, u64 ip); =20 static inline struct dso *map__dso(const struct map *map) { - return map->dso; + return RC_CHK_ACCESS(map)->dso; } =20 static inline u64 map__map_ip(const struct map *map, u64 ip) { - return map->map_ip(map, ip); + return RC_CHK_ACCESS(map)->map_ip(map, ip); } =20 static inline u64 map__unmap_ip(const struct map *map, u64 ip) { - return map->unmap_ip(map, ip); + return RC_CHK_ACCESS(map)->unmap_ip(map, ip); } =20 static inline u64 map__start(const struct map *map) { - return map->start; + return RC_CHK_ACCESS(map)->start; } =20 static inline u64 map__end(const struct map *map) { - return map->end; + return RC_CHK_ACCESS(map)->end; } =20 static inline u64 map__pgoff(const struct map *map) { - return map->pgoff; + return RC_CHK_ACCESS(map)->pgoff; } =20 static inline u64 map__reloc(const struct map *map) { - return map->reloc; + return RC_CHK_ACCESS(map)->reloc; } =20 static inline u32 map__flags(const struct map *map) { - return map->flags; + return RC_CHK_ACCESS(map)->flags; } =20 static inline u32 map__prot(const struct map *map) { - return map->prot; + return RC_CHK_ACCESS(map)->prot; } =20 static inline bool map__priv(const struct map *map) { - return map->priv; + return RC_CHK_ACCESS(map)->priv; } =20 static inline size_t map__size(const struct map *map) @@ -153,9 +154,12 @@ struct map *map__clone(struct map *map); =20 static inline struct map *map__get(struct map *map) { - if (map) - refcount_inc(&map->refcnt); - return map; + struct map *result; + + if (RC_CHK_GET(result, map)) + refcount_inc(&RC_CHK_ACCESS(map)->refcnt); + + return result; } =20 void map__put(struct map *map); diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 3c8bbcb2c204..a33ae321c65a 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -126,7 +126,7 @@ void maps__remove(struct maps *maps, struct map *map) RC_CHK_ACCESS(maps)->last_search_by_name =3D NULL; =20 rb_node =3D maps__find_node(maps, map); - assert(rb_node->map =3D=3D map); + assert(rb_node->RC_CHK_ACCESS(map) =3D=3D RC_CHK_ACCESS(map)); __maps__remove(maps, rb_node); if (maps__maps_by_name(maps)) __maps__free_maps_by_name(maps); @@ -339,7 +339,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) goto put_map; } =20 - before->end =3D map__start(map); + RC_CHK_ACCESS(before)->end =3D map__start(map); err =3D __maps__insert(maps, before); if (err) { map__put(before); @@ -351,7 +351,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) map__put(before); } =20 - if (map->end < map__end(pos->map)) { + if (map__end(map) < map__end(pos->map)) { struct map *after =3D map__clone(pos->map); =20 if (after =3D=3D NULL) { @@ -359,8 +359,9 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) goto put_map; } =20 - after->start =3D map__end(map); - after->pgoff +=3D map__end(map) - map__start(pos->map); + RC_CHK_ACCESS(after)->start =3D map__end(map); + RC_CHK_ACCESS(after)->pgoff +=3D + map__end(map) - map__start(pos->map); assert(map__map_ip(pos->map, map__end(map)) =3D=3D map__map_ip(after, map__end(map))); err =3D __maps__insert(maps, after); @@ -420,7 +421,7 @@ struct map_rb_node *maps__find_node(struct maps *maps, = struct map *map) struct map_rb_node *rb_node; =20 maps__for_each_entry(maps, rb_node) { - if (rb_node->map =3D=3D map) + if (rb_node->RC_CHK_ACCESS(map) =3D=3D RC_CHK_ACCESS(map)) return rb_node; } return NULL; diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 93ae3f22fd03..aa6113008627 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1348,11 +1348,11 @@ static int dso__process_kernel_symbol(struct dso *d= so, struct map *map, */ if (*remap_kernel && dso->kernel && !kmodule) { *remap_kernel =3D false; - map->start =3D shdr->sh_addr + ref_reloc(kmap); - map->end =3D map__start(map) + shdr->sh_size; - map->pgoff =3D shdr->sh_offset; - map->map_ip =3D map__dso_map_ip; - map->unmap_ip =3D map__dso_unmap_ip; + RC_CHK_ACCESS(map)->start =3D shdr->sh_addr + ref_reloc(kmap); + RC_CHK_ACCESS(map)->end =3D map__start(map) + shdr->sh_size; + RC_CHK_ACCESS(map)->pgoff =3D shdr->sh_offset; + RC_CHK_ACCESS(map)->map_ip =3D map__dso_map_ip; + RC_CHK_ACCESS(map)->unmap_ip =3D map__dso_unmap_ip; /* Ensure maps are correctly ordered */ if (kmaps) { int err; @@ -1373,7 +1373,7 @@ static int dso__process_kernel_symbol(struct dso *dso= , struct map *map, */ if (*remap_kernel && kmodule) { *remap_kernel =3D false; - map->pgoff =3D shdr->sh_offset; + RC_CHK_ACCESS(map)->pgoff =3D shdr->sh_offset; } =20 *curr_mapp =3D map; @@ -1408,11 +1408,13 @@ static int dso__process_kernel_symbol(struct dso *d= so, struct map *map, map__kmap(curr_map)->kmaps =3D kmaps; =20 if (adjust_kernel_syms) { - curr_map->start =3D shdr->sh_addr + ref_reloc(kmap); - curr_map->end =3D map__start(curr_map) + shdr->sh_size; - curr_map->pgoff =3D shdr->sh_offset; + RC_CHK_ACCESS(curr_map)->start =3D shdr->sh_addr + ref_reloc(kmap); + RC_CHK_ACCESS(curr_map)->end =3D map__start(curr_map) + + shdr->sh_size; + RC_CHK_ACCESS(curr_map)->pgoff =3D shdr->sh_offset; } else { - curr_map->map_ip =3D curr_map->unmap_ip =3D identity__map_ip; + RC_CHK_ACCESS(curr_map)->map_ip =3D identity__map_ip; + RC_CHK_ACCESS(curr_map)->unmap_ip =3D identity__map_ip; } curr_dso->symtab_type =3D dso->symtab_type; if (maps__insert(kmaps, curr_map)) @@ -1519,7 +1521,7 @@ dso__load_sym_internal(struct dso *dso, struct map *m= ap, struct symsrc *syms_ss, if (strcmp(elf_name, kmap->ref_reloc_sym->name)) continue; kmap->ref_reloc_sym->unrelocated_addr =3D sym.st_value; - map->reloc =3D kmap->ref_reloc_sym->addr - + RC_CHK_ACCESS(map)->reloc =3D kmap->ref_reloc_sym->addr - kmap->ref_reloc_sym->unrelocated_addr; break; } @@ -1530,7 +1532,7 @@ dso__load_sym_internal(struct dso *dso, struct map *m= ap, struct symsrc *syms_ss, * attempted to prelink vdso to its virtual address. */ if (dso__is_vdso(dso)) - map->reloc =3D map__start(map) - dso->text_offset; + RC_CHK_ACCESS(map)->reloc =3D map__start(map) - dso->text_offset; =20 dso->adjust_symbols =3D runtime_ss->adjust_symbols || ref_reloc(kmap); /* diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 97e1689d4fd1..2eeace6955dd 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -274,7 +274,7 @@ void maps__fixup_end(struct maps *maps) =20 maps__for_each_entry(maps, curr) { if (prev !=3D NULL && !map__end(prev->map)) - prev->map->end =3D map__start(curr->map); + RC_CHK_ACCESS(prev->map)->end =3D map__start(curr->map); =20 prev =3D curr; } @@ -284,7 +284,7 @@ void maps__fixup_end(struct maps *maps) * last map final address. */ if (curr && !map__end(curr->map)) - curr->map->end =3D ~0ULL; + RC_CHK_ACCESS(curr->map)->end =3D ~0ULL; =20 up_write(maps__lock(maps)); } @@ -860,7 +860,7 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, *module++ =3D '\0'; curr_map_dso =3D map__dso(curr_map); if (strcmp(curr_map_dso->short_name, module)) { - if (curr_map !=3D initial_map && + if (RC_CHK_ACCESS(curr_map) !=3D RC_CHK_ACCESS(initial_map) && dso->kernel =3D=3D DSO_SPACE__KERNEL_GUEST && machine__is_default_guest(machine)) { /* @@ -939,7 +939,8 @@ static int maps__split_kallsyms(struct maps *kmaps, str= uct dso *dso, u64 delta, return -1; } =20 - curr_map->map_ip =3D curr_map->unmap_ip =3D identity__map_ip; + RC_CHK_ACCESS(curr_map)->map_ip =3D identity__map_ip; + RC_CHK_ACCESS(curr_map)->unmap_ip =3D identity__map_ip; if (maps__insert(kmaps, curr_map)) { dso__put(ndso); return -1; @@ -1246,8 +1247,8 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgoff,= void *data) return -ENOMEM; } =20 - list_node->map->end =3D map__start(list_node->map) + len; - list_node->map->pgoff =3D pgoff; + list_node->RC_CHK_ACCESS(map)->end =3D map__start(list_node->map) + len; + list_node->RC_CHK_ACCESS(map)->pgoff =3D pgoff; =20 list_add(&list_node->node, &md->maps); =20 @@ -1282,7 +1283,7 @@ int maps__merge_in(struct maps *kmaps, struct map *ne= w_map) * |new......| -> |new..| * |old....| -> |old....| */ - new_map->end =3D map__start(old_map); + RC_CHK_ACCESS(new_map)->end =3D map__start(old_map); } else { /* * |new.............| -> |new..| |new..| @@ -1303,10 +1304,12 @@ int maps__merge_in(struct maps *kmaps, struct map *= new_map) goto out; } =20 - m->map->end =3D map__start(old_map); + + RC_CHK_ACCESS(m->map)->end =3D map__start(old_map); list_add_tail(&m->node, &merged); - new_map->pgoff +=3D map__end(old_map) - map__start(new_map); - new_map->start =3D map__end(old_map); + RC_CHK_ACCESS(new_map)->pgoff +=3D + map__end(old_map) - map__start(new_map); + RC_CHK_ACCESS(new_map)->start =3D map__end(old_map); } } else { /* @@ -1326,8 +1329,9 @@ int maps__merge_in(struct maps *kmaps, struct map *ne= w_map) * |new......| -> |new...| * |old....| -> |old....| */ - new_map->pgoff +=3D map__end(old_map) - map__start(new_map); - new_map->start =3D map__end(old_map); + RC_CHK_ACCESS(new_map)->pgoff +=3D + map__end(old_map) - map__start(new_map); + RC_CHK_ACCESS(new_map)->start =3D map__end(old_map); } } } @@ -1452,12 +1456,12 @@ static int dso__load_kcore(struct dso *dso, struct = map *map, =20 new_node =3D list_entry(md.maps.next, struct map_list_node, node); list_del_init(&new_node->node); - if (new_node->map =3D=3D replacement_map) { - map->start =3D map__start(new_node->map); - map->end =3D map__end(new_node->map); - map->pgoff =3D map__pgoff(new_node->map); - map->map_ip =3D new_node->map->map_ip; - map->unmap_ip =3D new_node->map->unmap_ip; + if (RC_CHK_ACCESS(new_node->map) =3D=3D RC_CHK_ACCESS(replacement_map)) { + RC_CHK_ACCESS(map)->start =3D map__start(new_node->map); + RC_CHK_ACCESS(map)->end =3D map__end(new_node->map); + RC_CHK_ACCESS(map)->pgoff =3D map__pgoff(new_node->map); + RC_CHK_ACCESS(map)->map_ip =3D RC_CHK_ACCESS(new_node->map)->map_ip; + RC_CHK_ACCESS(map)->unmap_ip =3D RC_CHK_ACCESS(new_node->map)->unmap_ip; /* Ensure maps are correctly ordered */ map__get(map); maps__remove(kmaps, map); --=20 2.40.0.rc1.284.g88254d51c5-goog