From nobody Sun Feb 8 06:05:17 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 3F17BC87FDC for ; Thu, 8 Jun 2023 23:29:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237153AbjFHX3a (ORCPT ); Thu, 8 Jun 2023 19:29:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229757AbjFHX3Q (ORCPT ); Thu, 8 Jun 2023 19:29:16 -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 5DD2A30E4 for ; Thu, 8 Jun 2023 16:29:14 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-bb39aebdd87so2572031276.0 for ; Thu, 08 Jun 2023 16:29:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266953; x=1688858953; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=sVrxFoXm1Y9BE+cSFKEBl3aqhI2wzHdN+Ni9StCoKlc=; b=tFiMSrtQwD/5A9NFS7UinYAeI6GpdGFuQ5rqSaHnkUkPLib+d0l+Os9dph+Y/D6IcU KB/pTYETV+CyOtxmwAJsgpa905tfPtSnUUfUBOnfDxBbrxqh5eURn7vMLsU38hMm+ui0 Z3NjyvHO9+jpyWQ5bOkv9cyYeJKRs8wLx2xsRCla0K/B/XQOxsi+VctGHZmpBo8A0gCq CQg41RjtE73ICFq75M3Cj4sctLt8m9K+AJN31KxAo870xSvLvHSjocO5LaGyc4A1c5Mh w67efiEXzITPqFx7LHFmujhLNV0NMbcbXO9tgGcMhbVQ3O03eWgp0oZ7MHCXGJaWJhJK 9SJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266953; x=1688858953; h=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=sVrxFoXm1Y9BE+cSFKEBl3aqhI2wzHdN+Ni9StCoKlc=; b=c8eVOlccyI9GIz8ewQm/vm1CcMtcSt10QuxYfZUIEMgzXo5MNJPP8TenhmqeonNDr7 UPEQ/CwFw2Tizb3+OZ08fcRRG06N+f1e2/YXs5s6wdKcftbyqCd4qGTAbO8IhJajBJAK ZcYuhe6jQeU1XDtwQAzIgrT9wTsthKShJrYUVR33YX1tJoeajYtsBEn8F3DtlQzSnQRB /gUf84fkTG6xCKMvaRnPnpdRK3Afp2dfK2K2qUB0QZrZ1c6cf59vbfXEk0E6Y9yROi4Y 0imIXI6DudVme5M+4Va+buPFsYoTjApJSdL0fEWIHLZ8VDrOcqk/UnQmKip5pVClhgvF QELg== X-Gm-Message-State: AC+VfDzONlerhV+DsiEyS+xB2ZF96hUN8g9LGGFu9pXL+odzuZ9u+usP hk3RMTeZ6pmQ6mRwIsSlEHmhfy2PWQa5 X-Google-Smtp-Source: ACHHUZ6q7cd5oaM+do72HcSC59pF4lji4zjehfp0QAiha0uuBSxG0h3bXE32gnB2HBzKTBzCP6jiY50dBdqe X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a25:ab8d:0:b0:bb3:9b99:f3f5 with SMTP id v13-20020a25ab8d000000b00bb39b99f3f5mr531633ybi.4.1686266953549; Thu, 08 Jun 2023 16:29:13 -0700 (PDT) Date: Thu, 8 Jun 2023 16:27:58 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-2-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 01/26] perf thread: Remove notion of dead threads From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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 dead thread list is best effort. Threads live on it until the reference count hits zero and they are removed. With correct reference counting this should never happen. It is, however, part of the 'perf sched' output that is now removed. If this is an issue we should implement tracking of dead threads in a robust not best-effort way. Signed-off-by: Ian Rogers --- tools/perf/builtin-sched.c | 23 +---------------------- tools/perf/util/cs-etm.c | 6 ------ tools/perf/util/intel-pt.c | 8 -------- tools/perf/util/machine.c | 32 +------------------------------- tools/perf/util/thread.c | 25 +------------------------ tools/perf/util/thread.h | 11 +---------- 6 files changed, 4 insertions(+), 101 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index cc4ba506e119..3a30c2ac5b47 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -2760,7 +2760,7 @@ struct total_run_stats { u64 total_run_time; }; =20 -static int __show_thread_runtime(struct thread *t, void *priv) +static int show_thread_runtime(struct thread *t, void *priv) { struct total_run_stats *stats =3D priv; struct thread_runtime *r; @@ -2783,22 +2783,6 @@ static int __show_thread_runtime(struct thread *t, v= oid *priv) return 0; } =20 -static int show_thread_runtime(struct thread *t, void *priv) -{ - if (t->dead) - return 0; - - return __show_thread_runtime(t, priv); -} - -static int show_deadthread_runtime(struct thread *t, void *priv) -{ - if (!t->dead) - return 0; - - return __show_thread_runtime(t, priv); -} - static size_t callchain__fprintf_folded(FILE *fp, struct callchain_node *n= ode) { const char *sep =3D " <- "; @@ -2890,11 +2874,6 @@ static void timehist_print_summary(struct perf_sched= *sched, if (!task_count) printf("\n"); =20 - printf("\nTerminated tasks:\n"); - machine__for_each_thread(m, show_deadthread_runtime, &totals); - if (task_count =3D=3D totals.task_count) - printf("\n"); - /* CPU idle stats not tracked when samples were skipped */ if (sched->skipped_samples && !sched->idle_hist) return; diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c index 91299cc56bf7..0f5be4ad24ba 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -3292,12 +3292,6 @@ int cs_etm__process_auxtrace_info_full(union perf_ev= ent *event, goto err_free_queues; } =20 - /* - * Initialize list node so that at thread__zput() we can avoid - * segmentation fault at list_del_init(). - */ - INIT_LIST_HEAD(&etm->unknown_thread->node); - err =3D thread__set_comm(etm->unknown_thread, "unknown", 0); if (err) goto err_delete_thread; diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c index fe893c9bab3f..dde2ca77a005 100644 --- a/tools/perf/util/intel-pt.c +++ b/tools/perf/util/intel-pt.c @@ -4311,14 +4311,6 @@ int intel_pt_process_auxtrace_info(union perf_event = *event, goto err_free_queues; } =20 - /* - * Since this thread will not be kept in any rbtree not in a - * list, initialize its list node so that at thread__put() the - * current thread lifetime assumption is kept and we don't segfault - * at list_del_init(). - */ - INIT_LIST_HEAD(&pt->unknown_thread->node); - err =3D thread__set_comm(pt->unknown_thread, "unknown", 0); if (err) goto err_delete_thread; diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 9e02e19c1b7a..a1954ac85f59 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -241,17 +241,6 @@ void machine__exit(struct machine *machine) =20 for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { struct threads *threads =3D &machine->threads[i]; - struct thread *thread, *n; - /* - * Forget about the dead, at this point whatever threads were - * left in the dead lists better have a reference count taken - * by who is using them, and then, when they drop those references - * and it finally hits zero, thread__put() will check and see that - * its not in the dead threads list and will not try to remove it - * from there, just calling thread__delete() straight away. - */ - list_for_each_entry_safe(thread, n, &threads->dead, node) - list_del_init(&thread->node); =20 exit_rwsem(&threads->lock); } @@ -2046,18 +2035,7 @@ static void __machine__remove_thread(struct machine = *machine, struct thread *th, rb_erase_cached(&th->rb_node, &threads->entries); RB_CLEAR_NODE(&th->rb_node); --threads->nr; - /* - * Move it first to the dead_threads list, then drop the reference, - * if this is the last reference, then the thread__delete destructor - * will be called and we will remove it from the dead_threads list. - */ - list_add_tail(&th->node, &threads->dead); =20 - /* - * We need to do the put here because if this is the last refcount, - * then we will be touching the threads->dead head when removing the - * thread. - */ thread__put(th); =20 if (lock) @@ -2145,10 +2123,8 @@ int machine__process_exit_event(struct machine *mach= ine, union perf_event *event if (dump_trace) perf_event__fprintf_task(event, stdout); =20 - if (thread !=3D NULL) { - thread__exited(thread); + if (thread !=3D NULL) thread__put(thread); - } =20 return 0; } @@ -3204,12 +3180,6 @@ int machine__for_each_thread(struct machine *machine, if (rc !=3D 0) return rc; } - - list_for_each_entry(thread, &threads->dead, node) { - rc =3D fn(thread, priv); - if (rc !=3D 0) - return rc; - } } return rc; } diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 4b5bdc277baa..d949bffc0ed6 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -125,31 +125,8 @@ struct thread *thread__get(struct thread *thread) =20 void thread__put(struct thread *thread) { - if (thread && refcount_dec_and_test(&thread->refcnt)) { - /* - * Remove it from the dead threads list, as last reference is - * gone, if it is in a dead threads list. - * - * We may not be there anymore if say, the machine where it was - * stored was already deleted, so we already removed it from - * the dead threads and some other piece of code still keeps a - * reference. - * - * This is what 'perf sched' does and finally drops it in - * perf_sched__lat(), where it calls perf_sched__read_events(), - * that processes the events by creating a session and deleting - * it, which ends up destroying the list heads for the dead - * threads, but before it does that it removes all threads from - * it using list_del_init(). - * - * So we need to check here if it is in a dead threads list and - * if so, remove it before finally deleting the thread, to avoid - * an use after free situation. - */ - if (!list_empty(&thread->node)) - list_del_init(&thread->node); + if (thread && refcount_dec_and_test(&thread->refcnt)) thread__delete(thread); - } } =20 static struct namespaces *__thread__namespaces(const struct thread *thread) diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 395c626699a9..86737812e06b 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -30,10 +30,7 @@ struct lbr_stitch { }; =20 struct thread { - union { - struct rb_node rb_node; - struct list_head node; - }; + struct rb_node rb_node; struct maps *maps; pid_t pid_; /* Not all tools update this */ pid_t tid; @@ -43,7 +40,6 @@ struct thread { refcount_t refcnt; bool comm_set; int comm_len; - bool dead; /* if set thread has exited */ struct list_head namespaces_list; struct rw_semaphore namespaces_lock; struct list_head comm_list; @@ -81,11 +77,6 @@ static inline void __thread__zput(struct thread **thread) =20 #define thread__zput(thread) __thread__zput(&thread) =20 -static inline void thread__exited(struct thread *thread) -{ - thread->dead =3D true; -} - struct namespaces *thread__namespaces(struct thread *thread); int thread__set_namespaces(struct thread *thread, u64 timestamp, struct perf_record_namespaces *event); --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 B4A35C7EE23 for ; Thu, 8 Jun 2023 23:29:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237161AbjFHX3f (ORCPT ); Thu, 8 Jun 2023 19:29:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236417AbjFHX3S (ORCPT ); Thu, 8 Jun 2023 19:29:18 -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 006C030E6 for ; Thu, 8 Jun 2023 16:29:16 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-561eb6c66f6so15567747b3.0 for ; Thu, 08 Jun 2023 16:29:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266956; x=1688858956; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ymnu/Vh789GfEOnzVpYprmKEH//VXYvaO1C6m6tLSA0=; b=pMSj7Ub+6mggNvEGxkdRszmf2MR1mNcKbiXbHyO+YlRxOoMzO2pRequho83r+bBf/I cBWn2zjyhiXt3ThfivITYJApq2Ft1QCg9S8IH9WZPZkGqy4u/uyB/IB5OWc0iiYr7EpW csXvR+0Sz7qKHkIRrrQ7Dv6+tzaTz8BkY3KRrQyXeHFv5JV4DONx4HXXBATZUeW1/Nvk AQs7WZNIKJ0ByeZeukd5Xgk4fkRtFPj4jGn+nlLgTdqq4F6Np1MotVYBHVbI6BXH2D15 s2J/JRwTvk/Bt9oksqYcOTykoU57gRI8OGtX3+/7tHPeJ570ajQ/1d3Wg/o3WjIP0bYV o4EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266956; x=1688858956; h=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=ymnu/Vh789GfEOnzVpYprmKEH//VXYvaO1C6m6tLSA0=; b=XCr171v8J31+XmL2aQ2twcrVzVncYyNkoT3lQnxsDB8IkXw8fAcLTaMDsIOExaS3pQ 7b54Fxp6zGYznBspd2q5NLQJL7rIk3bRleyf6pQ2hcwN2PKr+Oj4gSEd5lHD5jUOQolW Bh80h5aifddmJCSM8L216R655nCnHoJDZzInYaQBZRe4lINhqCQTNIU9z/AaUIoa4KLO F/5gVX4uhkDfh9ugot8FsedXekM16r+oPSq7XLARQjeLHQuQOqQbdTj5Rc5HzxycttbC ynth5sgKlvY2Oe3iIpCukzNaOSP3y9+8VGpKsY8vdeYYbeMZ9CkSiGpOXsAbnw7s/g9g vlOA== X-Gm-Message-State: AC+VfDxdl3yw25z/0Iyqm6ZgNSMZ4NMnq4nedUZ2f55bNnB9rGUHQkuy qBlesekAA5qgudEmR8b37QQaxE76ilPZ X-Google-Smtp-Source: ACHHUZ5i/+bckY1F3BVJityBZtENtui7ryds3PgsPmPbW4Zj1MTXKkbG1FZ038KzAc0HXgDF+4Zdk0bhUCur X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a81:a745:0:b0:567:289c:2642 with SMTP id e66-20020a81a745000000b00567289c2642mr684171ywh.10.1686266956242; Thu, 08 Jun 2023 16:29:16 -0700 (PDT) Date: Thu, 8 Jun 2023 16:27:59 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-3-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 02/26] perf thread: Make threads rbtree non-invasive From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Separate the rbtree out of thread and into a new struct thread_rb_node. The refcnt is in thread and the rbtree is responsible for a single count. Signed-off-by: Ian Rogers --- tools/perf/builtin-report.c | 2 +- tools/perf/builtin-trace.c | 2 +- tools/perf/util/machine.c | 101 +++++++++++++++++++++++------------- tools/perf/util/thread.c | 3 -- tools/perf/util/thread.h | 6 ++- 5 files changed, 73 insertions(+), 41 deletions(-) diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 92c6797e7cba..c7d526283baf 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -911,7 +911,7 @@ static int tasks_print(struct report *rep, FILE *fp) nd =3D rb_next(nd)) { task =3D tasks + itask++; =20 - task->thread =3D rb_entry(nd, struct thread, rb_node); + task->thread =3D rb_entry(nd, struct thread_rb_node, rb_node)->thread; INIT_LIST_HEAD(&task->children); INIT_LIST_HEAD(&task->list); thread__set_priv(task->thread, task); diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 62c7c99a0fe4..b0dd202d14eb 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -4348,7 +4348,7 @@ DEFINE_RESORT_RB(threads, (thread__nr_events(a->threa= d->priv) < thread__nr_event struct thread *thread; ) { - entry->thread =3D rb_entry(nd, struct thread, rb_node); + entry->thread =3D rb_entry(nd, struct thread_rb_node, rb_node)->thread; } =20 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index a1954ac85f59..cbf092e32ee9 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -43,7 +43,8 @@ #include #include =20 -static void __machine__remove_thread(struct machine *machine, struct threa= d *th, bool lock); +static void __machine__remove_thread(struct machine *machine, struct threa= d_rb_node *nd, + struct thread *th, bool lock); static int append_inlines(struct callchain_cursor *cursor, struct map_symb= ol *ms, u64 ip); =20 static struct dso *machine__kernel_dso(struct machine *machine) @@ -72,6 +73,21 @@ static void machine__threads_init(struct machine *machin= e) } } =20 +static int thread_rb_node__cmp_tid(const void *key, const struct rb_node *= nd) +{ + int to_find =3D (int) *((pid_t *)key); + + return to_find - (int)rb_entry(nd, struct thread_rb_node, rb_node)->threa= d->tid; +} + +static struct thread_rb_node *thread_rb_node__find(const struct thread *th, + struct rb_root *tree) +{ + struct rb_node *nd =3D rb_find(&th->tid, tree, thread_rb_node__cmp_tid); + + return rb_entry(nd, struct thread_rb_node, rb_node); +} + static int machine__set_mmap_name(struct machine *machine) { if (machine__is_host(machine)) @@ -214,10 +230,10 @@ void machine__delete_threads(struct machine *machine) down_write(&threads->lock); nd =3D rb_first_cached(&threads->entries); while (nd) { - struct thread *t =3D rb_entry(nd, struct thread, rb_node); + struct thread_rb_node *trb =3D rb_entry(nd, struct thread_rb_node, rb_n= ode); =20 nd =3D rb_next(nd); - __machine__remove_thread(machine, t, false); + __machine__remove_thread(machine, trb, trb->thread, false); } up_write(&threads->lock); } @@ -605,6 +621,7 @@ static struct thread *____machine__findnew_thread(struc= t machine *machine, struct rb_node **p =3D &threads->entries.rb_root.rb_node; struct rb_node *parent =3D NULL; struct thread *th; + struct thread_rb_node *nd; bool leftmost =3D true; =20 th =3D threads__get_last_match(threads, machine, pid, tid); @@ -613,7 +630,7 @@ static struct thread *____machine__findnew_thread(struc= t machine *machine, =20 while (*p !=3D NULL) { parent =3D *p; - th =3D rb_entry(parent, struct thread, rb_node); + th =3D rb_entry(parent, struct thread_rb_node, rb_node)->thread; =20 if (th->tid =3D=3D tid) { threads__set_last_match(threads, th); @@ -633,30 +650,39 @@ static struct thread *____machine__findnew_thread(str= uct machine *machine, return NULL; =20 th =3D thread__new(pid, tid); - if (th !=3D NULL) { - rb_link_node(&th->rb_node, parent, p); - rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost); + if (th =3D=3D NULL) + return NULL; =20 - /* - * We have to initialize maps separately after rb tree is updated. - * - * The reason is that we call machine__findnew_thread - * within thread__init_maps to find the thread - * leader and that would screwed the rb tree. - */ - if (thread__init_maps(th, machine)) { - rb_erase_cached(&th->rb_node, &threads->entries); - RB_CLEAR_NODE(&th->rb_node); - thread__put(th); - return NULL; - } - /* - * It is now in the rbtree, get a ref - */ - thread__get(th); - threads__set_last_match(threads, th); - ++threads->nr; + nd =3D malloc(sizeof(*nd)); + if (nd =3D=3D NULL) { + thread__put(th); + return NULL; + } + nd->thread =3D th; + + rb_link_node(&nd->rb_node, parent, p); + rb_insert_color_cached(&nd->rb_node, &threads->entries, leftmost); + + /* + * We have to initialize maps separately after rb tree is updated. + * + * The reason is that we call machine__findnew_thread within + * thread__init_maps to find the thread leader and that would screwed + * the rb tree. + */ + if (thread__init_maps(th, machine)) { + rb_erase_cached(&nd->rb_node, &threads->entries); + RB_CLEAR_NODE(&nd->rb_node); + free(nd); + thread__put(th); + return NULL; } + /* + * It is now in the rbtree, get a ref + */ + thread__get(th); + threads__set_last_match(threads, th); + ++threads->nr; =20 return th; } @@ -1109,7 +1135,7 @@ size_t machine__fprintf(struct machine *machine, FILE= *fp) =20 for (nd =3D rb_first_cached(&threads->entries); nd; nd =3D rb_next(nd)) { - struct thread *pos =3D rb_entry(nd, struct thread, rb_node); + struct thread *pos =3D rb_entry(nd, struct thread_rb_node, rb_node)->th= read; =20 ret +=3D thread__fprintf(pos, fp); } @@ -2020,10 +2046,14 @@ int machine__process_mmap_event(struct machine *mac= hine, union perf_event *event return 0; } =20 -static void __machine__remove_thread(struct machine *machine, struct threa= d *th, bool lock) +static void __machine__remove_thread(struct machine *machine, struct threa= d_rb_node *nd, + struct thread *th, bool lock) { struct threads *threads =3D machine__threads(machine, th->tid); =20 + if (!nd) + nd =3D thread_rb_node__find(th, &threads->entries.rb_root); + if (threads->last_match =3D=3D th) threads__set_last_match(threads, NULL); =20 @@ -2032,11 +2062,12 @@ static void __machine__remove_thread(struct machine= *machine, struct thread *th, =20 BUG_ON(refcount_read(&th->refcnt) =3D=3D 0); =20 - rb_erase_cached(&th->rb_node, &threads->entries); - RB_CLEAR_NODE(&th->rb_node); + thread__put(nd->thread); + rb_erase_cached(&nd->rb_node, &threads->entries); + RB_CLEAR_NODE(&nd->rb_node); --threads->nr; =20 - thread__put(th); + free(nd); =20 if (lock) up_write(&threads->lock); @@ -2044,7 +2075,7 @@ static void __machine__remove_thread(struct machine *= machine, struct thread *th, =20 void machine__remove_thread(struct machine *machine, struct thread *th) { - return __machine__remove_thread(machine, th, true); + return __machine__remove_thread(machine, NULL, th, true); } =20 int machine__process_fork_event(struct machine *machine, union perf_event = *event, @@ -3167,7 +3198,6 @@ int machine__for_each_thread(struct machine *machine, { struct threads *threads; struct rb_node *nd; - struct thread *thread; int rc =3D 0; int i; =20 @@ -3175,8 +3205,9 @@ int machine__for_each_thread(struct machine *machine, threads =3D &machine->threads[i]; for (nd =3D rb_first_cached(&threads->entries); nd; nd =3D rb_next(nd)) { - thread =3D rb_entry(nd, struct thread, rb_node); - rc =3D fn(thread, priv); + struct thread_rb_node *trb =3D rb_entry(nd, struct thread_rb_node, rb_n= ode); + + rc =3D fn(trb->thread, priv); if (rc !=3D 0) return rc; } diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index d949bffc0ed6..38d300e3e4d3 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -66,7 +66,6 @@ struct thread *thread__new(pid_t pid, pid_t tid) =20 list_add(&comm->list, &thread->comm_list); refcount_set(&thread->refcnt, 1); - RB_CLEAR_NODE(&thread->rb_node); /* Thread holds first ref to nsdata. */ thread->nsinfo =3D nsinfo__new(pid); srccode_state_init(&thread->srccode_state); @@ -84,8 +83,6 @@ void thread__delete(struct thread *thread) struct namespaces *namespaces, *tmp_namespaces; struct comm *comm, *tmp_comm; =20 - BUG_ON(!RB_EMPTY_NODE(&thread->rb_node)); - thread_stack__free(thread); =20 if (thread->maps) { diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 86737812e06b..3b3f9fb5a916 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -29,8 +29,12 @@ struct lbr_stitch { struct callchain_cursor_node *prev_lbr_cursor; }; =20 +struct thread_rb_node { + struct rb_node rb_node; + struct thread *thread; +}; + struct thread { - struct rb_node rb_node; struct maps *maps; pid_t pid_; /* Not all tools update this */ pid_t tid; --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 E5D9DC7EE37 for ; Thu, 8 Jun 2023 23:29:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237262AbjFHX3o (ORCPT ); Thu, 8 Jun 2023 19:29:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236957AbjFHX3Z (ORCPT ); Thu, 8 Jun 2023 19:29: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 B2E0530EB for ; Thu, 8 Jun 2023 16:29:19 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-565a33c35b1so15568817b3.0 for ; Thu, 08 Jun 2023 16:29:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266959; x=1688858959; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=fRj5/w2UOA0JOC/Xe07Iujyc9z+S2cbsaeq7FmsqXDU=; b=3EjdbJdsNYOeUhtii9EKtnbRkPhm67RmngBae/kT1z0LRVA/inT9GWkMD9HhDiwLqL l0Zhf4J/LzlSrmPLSTmlVmLkzWidwmwluVYvexZJiAiL/3IaYsHs88H40KIWwjrqqc+B HG41D/gem27yV8QvqPKrJ5IT7h9gvl+1T7ZZWULiRf8Hf2FxmgeoQuH1k/+SVIhRPnV3 mpcW//kQe2EjOXs2YRIYVj3d5bPpi/e2ffxJv0aHhmczbPl5I6D+cr3Pr5f1nhfwnDRl xNugUNIE4bm/JhcUBoCp2CMsBRFMjWOqD6dhQwGczXlYSeylgQv8UbNz/cQLOppBRrAM J1DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266959; x=1688858959; h=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=fRj5/w2UOA0JOC/Xe07Iujyc9z+S2cbsaeq7FmsqXDU=; b=a5OhOvZdTNE4TMFo91lSaTkirLOsgtMBqbdzc2sTymevtbZW+E9kv9fKYhAW+2seKl xT6UdV1FKwlKXAj4vjDL4IyLwTxUJ5FG3IN9ZN7sYkdyCfpSYsoSRLjxnw4BGBNF6sZD O6VnCTiaKBC53nzv4nz0Jz+afWqcS4Wq3Erj82P9+CILI1xW0/w7vQbQIpQ7iDMZRo89 Dsl/Cdpdf7uoIjQNVxvOdeDwBAknd2Mix+xi9n4PSyvt398ipUYERquKRQzxpiZGwpNy WqHiTznD2pDLzFS12zVn8L4vZZ5TXal7vwpuPPSa/4e1SqakFiMzBStIYlzXpxOSN3Jo Qcmg== X-Gm-Message-State: AC+VfDzJZjG6Mp3YsbOTv4jvMKljGznTdTH2d+A7NEHFhrn+qiI1Ogv0 eX85r0/GqoLJnw8BhESvCC7o1vYZISu8 X-Google-Smtp-Source: ACHHUZ4cBigZfPaCOmaoqRi5ygdASgmHJgUBIvwbO94FNfE0e7C65vCYglk4kMzc23yCQoxngaZpXsrOLUxF X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a81:b710:0:b0:55d:6af3:1e2c with SMTP id v16-20020a81b710000000b0055d6af31e2cmr627416ywh.3.1686266958935; Thu, 08 Jun 2023 16:29:18 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:00 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-4-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 03/26] perf thread: Add accessor functions for thread From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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 accessors will make it easier to add reference count checking in later patches. Signed-off-by: Ian Rogers --- tools/perf/arch/arm/tests/dwarf-unwind.c | 2 +- tools/perf/arch/arm64/tests/dwarf-unwind.c | 2 +- tools/perf/arch/powerpc/tests/dwarf-unwind.c | 2 +- tools/perf/arch/x86/tests/dwarf-unwind.c | 2 +- tools/perf/builtin-c2c.c | 6 +- tools/perf/builtin-inject.c | 2 +- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-report.c | 12 +- tools/perf/builtin-sched.c | 51 +++-- tools/perf/builtin-script.c | 20 +- tools/perf/builtin-top.c | 2 +- tools/perf/builtin-trace.c | 26 ++- .../scripts/python/Perf-Trace-Util/Context.c | 4 +- tools/perf/tests/code-reading.c | 2 +- tools/perf/tests/hists_common.c | 2 +- tools/perf/tests/hists_cumulate.c | 1 - tools/perf/tests/hists_output.c | 2 +- tools/perf/tests/perf-targz-src-pkg | 5 +- tools/perf/tests/thread-maps-share.c | 13 +- tools/perf/trace/beauty/pid.c | 4 +- tools/perf/ui/browsers/hists.c | 19 +- tools/perf/ui/stdio/hist.c | 2 +- tools/perf/util/arm-spe.c | 4 +- tools/perf/util/cs-etm.c | 2 +- tools/perf/util/data-convert-json.c | 8 +- tools/perf/util/db-export.c | 16 +- tools/perf/util/dlfilter.c | 4 +- tools/perf/util/event.c | 6 +- tools/perf/util/hist.c | 6 +- tools/perf/util/intel-bts.c | 2 +- tools/perf/util/intel-pt.c | 12 +- tools/perf/util/jitdump.c | 10 +- tools/perf/util/machine.c | 91 +++++---- tools/perf/util/map.c | 2 +- tools/perf/util/maps.c | 2 +- .../scripting-engines/trace-event-python.c | 14 +- tools/perf/util/session.c | 2 +- tools/perf/util/sort.c | 10 +- tools/perf/util/thread-stack.c | 25 +-- tools/perf/util/thread.c | 161 +++++++-------- tools/perf/util/thread.h | 188 +++++++++++++++++- tools/perf/util/unwind-libdw.c | 6 +- tools/perf/util/unwind-libunwind-local.c | 6 +- tools/perf/util/unwind-libunwind.c | 2 +- tools/perf/util/vdso.c | 2 +- 45 files changed, 485 insertions(+), 279 deletions(-) diff --git a/tools/perf/arch/arm/tests/dwarf-unwind.c b/tools/perf/arch/arm= /tests/dwarf-unwind.c index 566fb6c0eae7..9bc304cb7762 100644 --- a/tools/perf/arch/arm/tests/dwarf-unwind.c +++ b/tools/perf/arch/arm/tests/dwarf-unwind.c @@ -26,7 +26,7 @@ static int sample_ustack(struct perf_sample *sample, =20 sp =3D (unsigned long) regs[PERF_REG_ARM_SP]; =20 - map =3D maps__find(thread->maps, (u64)sp); + map =3D maps__find(thread__maps(thread), (u64)sp); if (!map) { pr_debug("failed to get stack map\n"); free(buf); diff --git a/tools/perf/arch/arm64/tests/dwarf-unwind.c b/tools/perf/arch/a= rm64/tests/dwarf-unwind.c index 90a7ef293ce7..b2603d0d3737 100644 --- a/tools/perf/arch/arm64/tests/dwarf-unwind.c +++ b/tools/perf/arch/arm64/tests/dwarf-unwind.c @@ -26,7 +26,7 @@ static int sample_ustack(struct perf_sample *sample, =20 sp =3D (unsigned long) regs[PERF_REG_ARM64_SP]; =20 - map =3D maps__find(thread->maps, (u64)sp); + map =3D maps__find(thread__maps(thread), (u64)sp); if (!map) { pr_debug("failed to get stack map\n"); free(buf); diff --git a/tools/perf/arch/powerpc/tests/dwarf-unwind.c b/tools/perf/arch= /powerpc/tests/dwarf-unwind.c index 32fffb593fbf..5ecf82893b84 100644 --- a/tools/perf/arch/powerpc/tests/dwarf-unwind.c +++ b/tools/perf/arch/powerpc/tests/dwarf-unwind.c @@ -26,7 +26,7 @@ static int sample_ustack(struct perf_sample *sample, =20 sp =3D (unsigned long) regs[PERF_REG_POWERPC_R1]; =20 - map =3D maps__find(thread->maps, (u64)sp); + map =3D maps__find(thread__maps(thread), (u64)sp); if (!map) { pr_debug("failed to get stack map\n"); free(buf); diff --git a/tools/perf/arch/x86/tests/dwarf-unwind.c b/tools/perf/arch/x86= /tests/dwarf-unwind.c index 497593be80f2..5bfec3345d59 100644 --- a/tools/perf/arch/x86/tests/dwarf-unwind.c +++ b/tools/perf/arch/x86/tests/dwarf-unwind.c @@ -26,7 +26,7 @@ static int sample_ustack(struct perf_sample *sample, =20 sp =3D (unsigned long) regs[PERF_REG_X86_SP]; =20 - map =3D maps__find(thread->maps, (u64)sp); + map =3D maps__find(thread__maps(thread), (u64)sp); if (!map) { pr_debug("failed to get stack map\n"); free(buf); diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c index 05dfd98af170..ee41a96f0c73 100644 --- a/tools/perf/builtin-c2c.c +++ b/tools/perf/builtin-c2c.c @@ -293,7 +293,7 @@ static int process_sample_event(struct perf_tool *tool = __maybe_unused, } =20 if (c2c.stitch_lbr) - al.thread->lbr_stitch_enable =3D true; + thread__set_lbr_stitch_enable(al.thread, true); =20 ret =3D sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, sysctl_perf_event_max_stack); @@ -1149,14 +1149,14 @@ pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp= *hpp, { int width =3D c2c_width(fmt, hpp, he->hists); =20 - return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_); + return scnprintf(hpp->buf, hpp->size, "%*d", width, thread__pid(he->threa= d)); } =20 static int64_t pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused, struct hist_entry *left, struct hist_entry *right) { - return left->thread->pid_ - right->thread->pid_; + return thread__pid(left->thread) - thread__pid(right->thread); } =20 static int64_t diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 61766eead4f4..d9e96d4624c6 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -417,7 +417,7 @@ static struct dso *findnew_dso(int pid, int tid, const = char *filename, } =20 vdso =3D is_vdso_map(filename); - nsi =3D nsinfo__get(thread->nsinfo); + nsi =3D nsinfo__get(thread__nsinfo(thread)); =20 if (vdso) { /* The vdso maps are always on the host and not the diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c index 2150eeced892..fe9439a4fd66 100644 --- a/tools/perf/builtin-kmem.c +++ b/tools/perf/builtin-kmem.c @@ -964,7 +964,7 @@ static int process_sample_event(struct perf_tool *tool = __maybe_unused, if (perf_kmem__skip_sample(sample)) return 0; =20 - dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid= ); + dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread__tid= (thread)); =20 if (evsel->handler !=3D NULL) { tracepoint_handler f =3D evsel->handler; diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index c7d526283baf..8ea6ab18534a 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -292,7 +292,7 @@ static int process_sample_event(struct perf_tool *tool, } =20 if (rep->stitch_lbr) - al.thread->lbr_stitch_enable =3D true; + thread__set_lbr_stitch_enable(al.thread, true); =20 if (symbol_conf.hide_unresolved && al.sym =3D=3D NULL) goto out_put; @@ -829,10 +829,10 @@ static struct task *tasks_list(struct task *task, str= uct machine *machine) return NULL; =20 /* Last one in the chain. */ - if (thread->ppid =3D=3D -1) + if (thread__ppid(thread) =3D=3D -1) return task; =20 - parent_thread =3D machine__find_thread(machine, -1, thread->ppid); + parent_thread =3D machine__find_thread(machine, -1, thread__ppid(thread)); if (!parent_thread) return ERR_PTR(-ENOENT); =20 @@ -869,12 +869,12 @@ static void task__print_level(struct task *task, FILE= *fp, int level) struct thread *thread =3D task->thread; struct task *child; int comm_indent =3D fprintf(fp, " %8d %8d %8d |%*s", - thread->pid_, thread->tid, thread->ppid, - level, ""); + thread__pid(thread), thread__tid(thread), + thread__ppid(thread), level, ""); =20 fprintf(fp, "%s\n", thread__comm_str(thread)); =20 - maps__fprintf_task(thread->maps, comm_indent, fp); + maps__fprintf_task(thread__maps(thread), comm_indent, fp); =20 if (!list_empty(&task->children)) { list_for_each_entry(child, &task->children, list) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 3a30c2ac5b47..fd37468c4f62 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -916,12 +916,12 @@ static int replay_fork_event(struct perf_sched *sched, =20 if (verbose > 0) { printf("fork event\n"); - printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid); - printf("... child: %s/%d\n", thread__comm_str(child), child->tid); + printf("... parent: %s/%d\n", thread__comm_str(parent), thread__tid(pare= nt)); + printf("... child: %s/%d\n", thread__comm_str(child), thread__tid(child= )); } =20 - register_pid(sched, parent->tid, thread__comm_str(parent)); - register_pid(sched, child->tid, thread__comm_str(child)); + register_pid(sched, thread__tid(parent), thread__comm_str(parent)); + register_pid(sched, thread__tid(child), thread__comm_str(child)); out_put: thread__put(child); thread__put(parent); @@ -1316,7 +1316,7 @@ static int latency_migrate_task_event(struct perf_sch= ed *sched, if (!atoms) { if (thread_atoms_insert(sched, migrant)) goto out_put; - register_pid(sched, migrant->tid, thread__comm_str(migrant)); + register_pid(sched, thread__tid(migrant), thread__comm_str(migrant)); atoms =3D thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pi= d); if (!atoms) { pr_err("migration-event: Internal tree error"); @@ -1359,10 +1359,13 @@ static void output_lat_thread(struct perf_sched *sc= hed, struct work_atoms *work_ sched->all_runtime +=3D work_list->total_runtime; sched->all_count +=3D work_list->nb_atoms; =20 - if (work_list->num_merged > 1) - ret =3D printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_l= ist->num_merged); - else - ret =3D printf(" %s:%d ", thread__comm_str(work_list->thread), work_lis= t->thread->tid); + if (work_list->num_merged > 1) { + ret =3D printf(" %s:(%d) ", thread__comm_str(work_list->thread), + work_list->num_merged); + } else { + ret =3D printf(" %s:%d ", thread__comm_str(work_list->thread), + thread__tid(work_list->thread)); + } =20 for (i =3D 0; i < 24 - ret; i++) printf(" "); @@ -1380,11 +1383,15 @@ static void output_lat_thread(struct perf_sched *sc= hed, struct work_atoms *work_ =20 static int pid_cmp(struct work_atoms *l, struct work_atoms *r) { + pid_t l_tid, r_tid; + if (l->thread =3D=3D r->thread) return 0; - if (l->thread->tid < r->thread->tid) + l_tid =3D thread__tid(l->thread); + r_tid =3D thread__tid(r->thread); + if (l_tid < r_tid) return -1; - if (l->thread->tid > r->thread->tid) + if (l_tid > r_tid) return 1; return (int)(l->thread - r->thread); } @@ -1679,14 +1686,14 @@ static int map_switch_event(struct perf_sched *sche= d, struct evsel *evsel, =20 timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp)); color_fprintf(stdout, color, " %12s secs ", stimestamp); - if (new_shortname || tr->comm_changed || (verbose > 0 && sched_in->tid)) { + if (new_shortname || tr->comm_changed || (verbose > 0 && thread__tid(sche= d_in))) { const char *pid_color =3D color; =20 if (thread__has_color(sched_in)) pid_color =3D COLOR_PIDS; =20 color_fprintf(stdout, pid_color, "%s =3D> %s:%d", - tr->shortname, thread__comm_str(sched_in), sched_in->tid); + tr->shortname, thread__comm_str(sched_in), thread__tid(sched_in)); tr->comm_changed =3D false; } =20 @@ -1948,8 +1955,8 @@ static char *timehist_get_commstr(struct thread *thre= ad) { static char str[32]; const char *comm =3D thread__comm_str(thread); - pid_t tid =3D thread->tid; - pid_t pid =3D thread->pid_; + pid_t tid =3D thread__tid(thread); + pid_t pid =3D thread__pid(thread); int n; =20 if (pid =3D=3D 0) @@ -2032,7 +2039,7 @@ static char task_state_char(struct thread *thread, in= t state) unsigned bit =3D state ? ffs(state) : 0; =20 /* 'I' for idle */ - if (thread->tid =3D=3D 0) + if (thread__tid(thread) =3D=3D 0) return 'I'; =20 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?'; @@ -2067,7 +2074,7 @@ static void timehist_print_sample(struct perf_sched *= sched, for (i =3D 0; i < max_cpus; ++i) { /* flag idle times with 'i'; others are sched events */ if (i =3D=3D sample->cpu) - c =3D (thread->tid =3D=3D 0) ? 'i' : 's'; + c =3D (thread__tid(thread) =3D=3D 0) ? 'i' : 's'; else c =3D ' '; printf("%c", c); @@ -2094,7 +2101,7 @@ static void timehist_print_sample(struct perf_sched *= sched, if (sched->show_wakeups && !sched->show_next) printf(" %-*s", comm_width, ""); =20 - if (thread->tid =3D=3D 0) + if (thread__tid(thread) =3D=3D 0) goto out; =20 if (sched->show_callchain) @@ -2626,7 +2633,7 @@ static int timehist_sched_change_event(struct perf_to= ol *tool, t =3D ptime->end; } =20 - if (!sched->idle_hist || thread->tid =3D=3D 0) { + if (!sched->idle_hist || thread__tid(thread) =3D=3D 0) { if (!cpu_list || test_bit(sample->cpu, cpu_bitmap)) timehist_update_runtime_stats(tr, t, tprev); =20 @@ -2634,7 +2641,7 @@ static int timehist_sched_change_event(struct perf_to= ol *tool, struct idle_thread_runtime *itr =3D (void *)tr; struct thread_runtime *last_tr; =20 - BUG_ON(thread->tid !=3D 0); + BUG_ON(thread__tid(thread) !=3D 0); =20 if (itr->last_thread =3D=3D NULL) goto out; @@ -2719,7 +2726,7 @@ static void print_thread_runtime(struct thread *t, float stddev; =20 printf("%*s %5d %9" PRIu64 " ", - comm_width, timehist_get_commstr(t), t->ppid, + comm_width, timehist_get_commstr(t), thread__ppid(t), (u64) r->run_stats.n); =20 print_sched_time(r->total_run_time, 8); @@ -2739,7 +2746,7 @@ static void print_thread_waittime(struct thread *t, struct thread_runtime *r) { printf("%*s %5d %9" PRIu64 " ", - comm_width, timehist_get_commstr(t), t->ppid, + comm_width, timehist_get_commstr(t), thread__ppid(t), (u64) r->run_stats.n); =20 print_sched_time(r->total_run_time, 8); diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index b02ad386a55b..e756290de2ac 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -1142,7 +1142,7 @@ static int print_srccode(struct thread *thread, u8 cp= umode, uint64_t addr) if (!al.map) return 0; ret =3D map__fprintf_srccode(al.map, al.addr, stdout, - &thread->srccode_state); + thread__srccode_state(thread)); if (ret) ret +=3D printf("\n"); return ret; @@ -1439,7 +1439,7 @@ static int perf_sample__fprintf_callindent(struct per= f_sample *sample, * The 'return' has already been popped off the stack so the depth has * to be adjusted to match the 'call'. */ - if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN) + if (thread__ts(thread) && sample->flags & PERF_IP_FLAG_RETURN) depth +=3D 1; =20 name =3D resolve_branch_sym(sample, evsel, thread, al, addr_al, &ip); @@ -1577,7 +1577,7 @@ static int perf_sample__fprintf_bts(struct perf_sampl= e *sample, printed +=3D fprintf(fp, "\n"); if (PRINT_FIELD(SRCCODE)) { int ret =3D map__fprintf_srccode(al->map, al->addr, stdout, - &thread->srccode_state); + thread__srccode_state(thread)); if (ret) { printed +=3D ret; printed +=3D printf("\n"); @@ -2086,9 +2086,9 @@ static bool show_event(struct perf_sample *sample, if (!symbol_conf.graph_function) return true; =20 - if (thread->filter) { - if (depth <=3D thread->filter_entry_depth) { - thread->filter =3D false; + if (thread__filter(thread)) { + if (depth <=3D thread__filter_entry_depth(thread)) { + thread__set_filter(thread, false); return false; } return true; @@ -2105,8 +2105,8 @@ static bool show_event(struct perf_sample *sample, while (*s) { unsigned len =3D strcspn(s, ","); if (nlen =3D=3D len && !strncmp(name, s, len)) { - thread->filter =3D true; - thread->filter_entry_depth =3D depth; + thread__set_filter(thread, true); + thread__set_filter_entry_depth(thread, depth); return true; } s +=3D len; @@ -2186,7 +2186,7 @@ static void process_event(struct perf_script *script, struct callchain_cursor *cursor =3D NULL; =20 if (script->stitch_lbr) - al->thread->lbr_stitch_enable =3D true; + thread__set_lbr_stitch_enable(al->thread, true); =20 if (symbol_conf.use_callchain && sample->callchain && thread__resolve_callchain(al->thread, &callchain_cursor, evsel, @@ -2241,7 +2241,7 @@ static void process_event(struct perf_script *script, =20 if (PRINT_FIELD(SRCCODE)) { if (map__fprintf_srccode(al->map, al->addr, stdout, - &thread->srccode_state)) + thread__srccode_state(thread))) printf("\n"); } =20 diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 27a7f068207d..9d3cbebb9b79 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -777,7 +777,7 @@ static void perf_event__process_sample(struct perf_tool= *tool, return; =20 if (top->stitch_lbr) - al.thread->lbr_stitch_enable =3D true; + thread__set_lbr_stitch_enable(al.thread, true); =20 if (!machine->kptr_restrict_warned && symbol_conf.kptr_restrict && diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index b0dd202d14eb..4c9bec39423b 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -1386,12 +1386,13 @@ static int thread__read_fd_path(struct thread *thre= ad, int fd) struct stat st; int ret; =20 - if (thread->pid_ =3D=3D thread->tid) { + if (thread__pid(thread) =3D=3D thread__tid(thread)) { scnprintf(linkname, sizeof(linkname), - "/proc/%d/fd/%d", thread->pid_, fd); + "/proc/%d/fd/%d", thread__pid(thread), fd); } else { scnprintf(linkname, sizeof(linkname), - "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd); + "/proc/%d/task/%d/fd/%d", + thread__pid(thread), thread__tid(thread), fd); } =20 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname)) @@ -1559,7 +1560,7 @@ static size_t trace__fprintf_comm_tid(struct trace *t= race, struct thread *thread if (trace->multiple_threads) { if (trace->show_comm) printed +=3D fprintf(fp, "%.14s/", thread__comm_str(thread)); - printed +=3D fprintf(fp, "%d ", thread->tid); + printed +=3D fprintf(fp, "%d ", thread__tid(thread)); } =20 return printed; @@ -2205,7 +2206,8 @@ static void thread__update_stats(struct thread *threa= d, struct thread_trace *ttr memset(new_errnos + stats->max_errno, 0, (err - stats->max_errno) * si= zeof(u32)); } else { pr_debug("Not enough memory for errno stats for thread \"%s\"(%d/%d), = results will be incomplete\n", - thread__comm_str(thread), thread->pid_, thread->tid); + thread__comm_str(thread), thread__pid(thread), + thread__tid(thread)); return; } =20 @@ -2550,7 +2552,7 @@ errno_print: { =20 if (child !=3D NULL) { fprintf(trace->output, "%ld", ret); - if (child->comm_set) + if (thread__comm_set(child)) fprintf(trace->output, " (%s)", thread__comm_str(child)); thread__put(child); } @@ -3616,14 +3618,16 @@ static int trace__set_filter_loop_pids(struct trace= *trace) struct thread *thread =3D machine__find_thread(trace->host, pids[0], pids= [0]); =20 while (thread && nr < ARRAY_SIZE(pids)) { - struct thread *parent =3D machine__find_thread(trace->host, thread->ppid= , thread->ppid); + struct thread *parent =3D machine__find_thread(trace->host, + thread__ppid(thread), + thread__ppid(thread)); =20 if (parent =3D=3D NULL) break; =20 if (!strcmp(thread__comm_str(parent), "sshd") || strstarts(thread__comm_str(parent), "gnome-terminal")) { - pids[nr++] =3D parent->tid; + pids[nr++] =3D thread__tid(parent); break; } thread =3D parent; @@ -4322,7 +4326,7 @@ static size_t trace__fprintf_thread(FILE *fp, struct = thread *thread, struct trac =20 ratio =3D (double)ttrace->nr_events / trace->nr_events * 100.0; =20 - printed +=3D fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->= tid); + printed +=3D fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread__= tid(thread)); printed +=3D fprintf(fp, "%lu events, ", ttrace->nr_events); printed +=3D fprintf(fp, "%.1f%%", ratio); if (ttrace->pfmaj) @@ -4344,7 +4348,9 @@ static unsigned long thread__nr_events(struct thread_= trace *ttrace) return ttrace ? ttrace->nr_events : 0; } =20 -DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr= _events(b->thread->priv)), +DEFINE_RESORT_RB(threads, + (thread__nr_events(thread__priv(a->thread)) < + thread__nr_events(thread__priv(b->thread))), struct thread *thread; ) { diff --git a/tools/perf/scripts/python/Perf-Trace-Util/Context.c b/tools/pe= rf/scripts/python/Perf-Trace-Util/Context.c index 53b1587db403..3954bd1587ce 100644 --- a/tools/perf/scripts/python/Perf-Trace-Util/Context.c +++ b/tools/perf/scripts/python/Perf-Trace-Util/Context.c @@ -100,8 +100,8 @@ static PyObject *perf_sample_insn(PyObject *obj, PyObje= ct *args) if (!c) return NULL; =20 - if (c->sample->ip && !c->sample->insn_len && c->al->thread->maps) { - struct machine *machine =3D maps__machine(c->al->thread->maps); + if (c->sample->ip && !c->sample->insn_len && thread__maps(c->al->thread))= { + struct machine *machine =3D maps__machine(thread__maps(c->al->thread)); =20 script_fetch_insn(c->sample, c->al->thread, machine); } diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-readin= g.c index efe026a35010..9d8eefbebd48 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 map__end(al.map) - addr; =20 /* Read the object code using perf */ - ret_len =3D dso__data_read_offset(dso, maps__machine(thread->maps), + ret_len =3D dso__data_read_offset(dso, maps__machine(thread__maps(thread)= ), al.addr, buf1, len); if (ret_len !=3D len) { pr_debug("dso__data_read_offset failed\n"); diff --git a/tools/perf/tests/hists_common.c b/tools/perf/tests/hists_commo= n.c index 745ab18d17db..d08add0f4da6 100644 --- a/tools/perf/tests/hists_common.c +++ b/tools/perf/tests/hists_common.c @@ -211,7 +211,7 @@ void print_hists_out(struct hists *hists) struct dso *dso =3D map__dso(he->ms.map); =20 pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period =3D %"PRIu64"/%"PRIu64= "\n", - i, thread__comm_str(he->thread), he->thread->tid, + i, thread__comm_str(he->thread), thread__tid(he->thread), 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 8c0e3f334747..62b9c6461ea6 100644 --- a/tools/perf/tests/hists_cumulate.c +++ b/tools/perf/tests/hists_cumulate.c @@ -162,7 +162,6 @@ typedef int (*test_fn_t)(struct evsel *, struct machine= *); #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) (map__dso(cl->ms.map)->short_name) #define CSYM(cl) (cl->ms.sym->name) diff --git a/tools/perf/tests/hists_output.c b/tools/perf/tests/hists_outpu= t.c index cebd5226bb12..cd2094c13e1e 100644 --- a/tools/perf/tests/hists_output.c +++ b/tools/perf/tests/hists_output.c @@ -128,7 +128,7 @@ typedef int (*test_fn_t)(struct evsel *, struct machine= *); #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 PID(he) (thread__tid(he->thread)) =20 /* default sort keys (no field) */ static int test1(struct evsel *evsel, struct machine *machine) diff --git a/tools/perf/tests/perf-targz-src-pkg b/tools/perf/tests/perf-ta= rgz-src-pkg index fae26b1cf08f..b3075c168cb2 100755 --- a/tools/perf/tests/perf-targz-src-pkg +++ b/tools/perf/tests/perf-targz-src-pkg @@ -7,16 +7,17 @@ # be in such tarball, which sometimes gets broken when we move files aroun= d, # like when we made some files that were in tools/perf/ available to other= tools/ # codebases by moving it to tools/include/, etc. +set -e =20 PERF=3D$1 cd ${PERF}/../.. -make perf-targz-src-pkg > /dev/null +make perf-targz-src-pkg TARBALL=3D$(ls -rt perf-*.tar.gz) TMP_DEST=3D$(mktemp -d) tar xf ${TARBALL} -C $TMP_DEST rm -f ${TARBALL} cd - > /dev/null -make -C $TMP_DEST/perf*/tools/perf > /dev/null +make -C $TMP_DEST/perf*/tools/perf RC=3D$? rm -rf ${TMP_DEST} exit $RC diff --git a/tools/perf/tests/thread-maps-share.c b/tools/perf/tests/thread= -maps-share.c index 858e725318a9..faf980b26252 100644 --- a/tools/perf/tests/thread-maps-share.c +++ b/tools/perf/tests/thread-maps-share.c @@ -42,13 +42,13 @@ static int test__thread_maps_share(struct test_suite *t= est __maybe_unused, int s TEST_ASSERT_VAL("failed to create threads", leader && t1 && t2 && t3 && other); =20 - maps =3D leader->maps; + maps =3D thread__maps(leader); TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(maps)), 4); =20 /* test the maps pointer is shared */ - 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)); + TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) =3D=3D RC_CHK_ACC= ESS(thread__maps(t1))); + TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) =3D=3D RC_CHK_ACC= ESS(thread__maps(t2))); + TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) =3D=3D RC_CHK_ACC= ESS(thread__maps(t3))); =20 /* * Verify the other leader was created by previous call. @@ -70,10 +70,11 @@ static int test__thread_maps_share(struct test_suite *t= est __maybe_unused, int s machine__remove_thread(machine, other); machine__remove_thread(machine, other_leader); =20 - other_maps =3D other->maps; + other_maps =3D thread__maps(other); TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(other_maps))= , 2); =20 - TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(other_maps) =3D=3D RC_C= HK_ACCESS(other_leader->maps)); + TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(other_maps) =3D=3D + RC_CHK_ACCESS(thread__maps(other_leader))); =20 /* release thread group */ thread__put(t3); diff --git a/tools/perf/trace/beauty/pid.c b/tools/perf/trace/beauty/pid.c index 1a6acc46807b..8f9c9950f8ba 100644 --- a/tools/perf/trace/beauty/pid.c +++ b/tools/perf/trace/beauty/pid.c @@ -8,10 +8,10 @@ size_t syscall_arg__scnprintf_pid(char *bf, size_t size, = struct syscall_arg *arg struct thread *thread =3D machine__findnew_thread(trace->host, pid, pid); =20 if (thread !=3D NULL) { - if (!thread->comm_set) + if (!thread__comm_set(thread)) thread__set_comm_from_proc(thread); =20 - if (thread->comm_set) + if (thread__comm_set(thread)) printed +=3D scnprintf(bf + printed, size - printed, " (%s)", thread__comm_str(thread)); thread__put(thread); diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c index 69c81759a64f..c7ad9e003080 100644 --- a/tools/perf/ui/browsers/hists.c +++ b/tools/perf/ui/browsers/hists.c @@ -2533,13 +2533,15 @@ do_zoom_thread(struct hist_browser *browser, struct= popup_action *act) thread__zput(browser->hists->thread_filter); ui_helpline__pop(); } else { + const char *comm_set_str =3D + thread__comm_set(thread) ? thread__comm_str(thread) : ""; + if (hists__has(browser->hists, thread)) { ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s(%= d) thread\"", - thread->comm_set ? thread__comm_str(thread) : "", - thread->tid); + comm_set_str, thread__tid(thread)); } else { ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s t= hread\"", - thread->comm_set ? thread__comm_str(thread) : ""); + comm_set_str); } =20 browser->hists->thread_filter =3D thread__get(thread); @@ -2557,20 +2559,19 @@ add_thread_opt(struct hist_browser *browser, struct= popup_action *act, char **optstr, struct thread *thread) { int ret; + const char *comm_set_str, *in_out; =20 if ((!hists__has(browser->hists, thread) && !hists__has(browser->hists, comm)) || thread =3D=3D NULL) return 0; =20 + in_out =3D browser->hists->thread_filter ? "out of" : "into"; + comm_set_str =3D thread__comm_set(thread) ? thread__comm_str(thread) : ""; if (hists__has(browser->hists, thread)) { ret =3D asprintf(optstr, "Zoom %s %s(%d) thread", - browser->hists->thread_filter ? "out of" : "into", - thread->comm_set ? thread__comm_str(thread) : "", - thread->tid); + in_out, comm_set_str, thread__tid(thread)); } else { - ret =3D asprintf(optstr, "Zoom %s %s thread", - browser->hists->thread_filter ? "out of" : "into", - thread->comm_set ? thread__comm_str(thread) : ""); + ret =3D asprintf(optstr, "Zoom %s %s thread", in_out, comm_set_str); } if (ret < 0) return 0; diff --git a/tools/perf/ui/stdio/hist.c b/tools/perf/ui/stdio/hist.c index f36270485168..b849caace398 100644 --- a/tools/perf/ui/stdio/hist.c +++ b/tools/perf/ui/stdio/hist.c @@ -885,7 +885,7 @@ size_t hists__fprintf(struct hists *hists, bool show_he= ader, int max_rows, } =20 if (h->ms.map =3D=3D NULL && verbose > 1) { - maps__fprintf(h->thread->maps, fp); + maps__fprintf(thread__maps(h->thread), fp); fprintf(fp, "%.10s end\n", graph_dotted_line); } } diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c index 7b36ba6b4079..afbd5869f6bf 100644 --- a/tools/perf/util/arm-spe.c +++ b/tools/perf/util/arm-spe.c @@ -254,9 +254,9 @@ static void arm_spe_set_pid_tid_cpu(struct arm_spe *spe, } =20 if (speq->thread) { - speq->pid =3D speq->thread->pid_; + speq->pid =3D thread__pid(speq->thread); if (queue->cpu =3D=3D -1) - speq->cpu =3D speq->thread->cpu; + speq->cpu =3D thread__cpu(speq->thread); } } =20 diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c index 0f5be4ad24ba..b550c7393155 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -1311,7 +1311,7 @@ static void cs_etm__set_pid_tid_cpu(struct cs_etm_aux= trace *etm, tidq->tid); =20 if (tidq->thread) - tidq->pid =3D tidq->thread->pid_; + tidq->pid =3D thread__pid(tidq->thread); } =20 int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq, diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-con= vert-json.c index 653709ab867a..291591e303cd 100644 --- a/tools/perf/util/data-convert-json.c +++ b/tools/perf/util/data-convert-json.c @@ -172,13 +172,13 @@ static int process_sample_event(struct perf_tool *too= l, output_json_format(out, false, 2, "{"); =20 output_json_key_format(out, false, 3, "timestamp", "%" PRIi64, sample->ti= me); - output_json_key_format(out, true, 3, "pid", "%i", al.thread->pid_); - output_json_key_format(out, true, 3, "tid", "%i", al.thread->tid); + output_json_key_format(out, true, 3, "pid", "%i", thread__pid(al.thread)); + output_json_key_format(out, true, 3, "tid", "%i", thread__tid(al.thread)); =20 if ((sample_type & PERF_SAMPLE_CPU)) output_json_key_format(out, true, 3, "cpu", "%i", sample->cpu); - else if (al.thread->cpu >=3D 0) - output_json_key_format(out, true, 3, "cpu", "%i", al.thread->cpu); + else if (thread__cpu(al.thread) >=3D 0) + output_json_key_format(out, true, 3, "cpu", "%i", thread__cpu(al.thread)= ); =20 output_json_key_string(out, true, 3, "comm", thread__comm_str(al.thread)); =20 diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c index 84c970c11794..751fd53bfd93 100644 --- a/tools/perf/util/db-export.c +++ b/tools/perf/util/db-export.c @@ -64,13 +64,13 @@ int db_export__thread(struct db_export *dbe, struct thr= ead *thread, { u64 main_thread_db_id =3D 0; =20 - if (thread->db_id) + if (thread__db_id(thread)) return 0; =20 - thread->db_id =3D ++dbe->thread_last_db_id; + thread__set_db_id(thread, ++dbe->thread_last_db_id); =20 if (main_thread) - main_thread_db_id =3D main_thread->db_id; + main_thread_db_id =3D thread__db_id(main_thread); =20 if (dbe->export_thread) return dbe->export_thread(dbe, thread, main_thread_db_id, @@ -251,7 +251,7 @@ static struct call_path *call_path_from_sample(struct d= b_export *dbe, */ al.sym =3D node->ms.sym; al.map =3D node->ms.map; - al.maps =3D thread->maps; + al.maps =3D thread__maps(thread); al.addr =3D node->ip; =20 if (al.map && !al.sym) @@ -321,7 +321,7 @@ static int db_export__threads(struct db_export *dbe, st= ruct thread *thread, * For a non-main thread, db_export__comm_thread() must be * called only if thread has not previously been exported. */ - bool export_comm_thread =3D comm && !thread->db_id; + bool export_comm_thread =3D comm && !thread__db_id(thread); =20 err =3D db_export__thread(dbe, thread, machine, main_thread); if (err) @@ -529,16 +529,16 @@ static int db_export__pid_tid(struct db_export *dbe, = struct machine *machine, struct thread *main_thread; int err =3D 0; =20 - if (!thread || !thread->comm_set) + if (!thread || !thread__comm_set(thread)) goto out_put; =20 - *is_idle =3D !thread->pid_ && !thread->tid; + *is_idle =3D !thread__pid(thread) && !thread__tid(thread); =20 main_thread =3D thread__main_thread(machine, thread); =20 err =3D db_export__threads(dbe, thread, main_thread, machine, comm_ptr); =20 - *db_id =3D thread->db_id; + *db_id =3D thread__db_id(thread); =20 thread__put(main_thread); out_put: diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c index 16238f823a5e..8016f21dc0b8 100644 --- a/tools/perf/util/dlfilter.c +++ b/tools/perf/util/dlfilter.c @@ -197,8 +197,8 @@ static const __u8 *dlfilter__insn(void *ctx, __u32 *len) if (!al->thread && machine__resolve(d->machine, al, d->sample) < 0) return NULL; =20 - if (al->thread->maps) { - struct machine *machine =3D maps__machine(al->thread->maps); + if (thread__maps(al->thread)) { + struct machine *machine =3D maps__machine(thread__maps(al->thread)); =20 if (machine) script_fetch_insn(d->sample, al->thread, machine); diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index e8b0666d913c..e1ce7cb5e421 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -573,7 +573,7 @@ int perf_event__process(struct perf_tool *tool __maybe_= unused, struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr, struct addr_location *al) { - struct maps *maps =3D thread->maps; + struct maps *maps =3D thread__maps(thread); struct machine *machine =3D maps__machine(maps); bool load_map =3D false; =20 @@ -639,7 +639,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 maps__machine(thread->maps); + struct machine *machine =3D maps__machine(thread__maps(thread)); u8 addr_cpumode =3D machine__addr_cpumode(machine, cpumode, addr); =20 if (map || addr_cpumode =3D=3D cpumode) @@ -696,7 +696,7 @@ int machine__resolve(struct machine *machine, struct ad= dr_location *al, if (thread =3D=3D NULL) return -1; =20 - dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid= ); + dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread__tid= (thread)); thread__find_map(thread, sample->cpumode, sample->ip, al); dso =3D al->map ? map__dso(al->map) : NULL; dump_printf(" ...... dso: %s\n", diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 3c9301a26dfc..4bc3affbe891 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -2778,12 +2778,12 @@ int __hists__scnprintf_title(struct hists *hists, c= har *bf, size_t size, bool sh if (hists__has(hists, thread)) { printed +=3D scnprintf(bf + printed, size - printed, ", Thread: %s(%d)", - (thread->comm_set ? thread__comm_str(thread) : ""), - thread->tid); + (thread__comm_set(thread) ? thread__comm_str(thread) : ""), + thread__tid(thread)); } else { printed +=3D scnprintf(bf + printed, size - printed, ", Thread: %s", - (thread->comm_set ? thread__comm_str(thread) : "")); + (thread__comm_set(thread) ? thread__comm_str(thread) : "")); } } if (dso) diff --git a/tools/perf/util/intel-bts.c b/tools/perf/util/intel-bts.c index 2c8147a62203..ec1b3bd9f530 100644 --- a/tools/perf/util/intel-bts.c +++ b/tools/perf/util/intel-bts.c @@ -456,7 +456,7 @@ static int intel_bts_process_queue(struct intel_bts_que= ue *btsq, u64 *timestamp) thread =3D machine__find_thread(btsq->bts->machine, -1, btsq->tid); if (thread) - btsq->pid =3D thread->pid_; + btsq->pid =3D thread__pid(thread); } else { thread =3D machine__findnew_thread(btsq->bts->machine, btsq->pid, btsq->tid); diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c index dde2ca77a005..45c7e7722916 100644 --- a/tools/perf/util/intel-pt.c +++ b/tools/perf/util/intel-pt.c @@ -1428,13 +1428,13 @@ static int intel_pt_get_guest_from_sideband(struct = intel_pt_queue *ptq) ptq->guest_machine =3D machine; } =20 - vcpu =3D ptq->thread ? ptq->thread->guest_cpu : -1; + vcpu =3D ptq->thread ? thread__guest_cpu(ptq->thread) : -1; if (vcpu < 0) return -1; =20 tid =3D machine__get_current_tid(machine, vcpu); =20 - if (ptq->guest_thread && ptq->guest_thread->tid !=3D tid) + if (ptq->guest_thread && thread__tid(ptq->guest_thread) !=3D tid) thread__zput(ptq->guest_thread); =20 if (!ptq->guest_thread) { @@ -1444,7 +1444,7 @@ static int intel_pt_get_guest_from_sideband(struct in= tel_pt_queue *ptq) } =20 ptq->guest_machine_pid =3D machine_pid; - ptq->guest_pid =3D ptq->guest_thread->pid_; + ptq->guest_pid =3D thread__pid(ptq->guest_thread); ptq->guest_tid =3D tid; ptq->vcpu =3D vcpu; =20 @@ -1467,9 +1467,9 @@ static void intel_pt_set_pid_tid_cpu(struct intel_pt = *pt, ptq->thread =3D machine__find_thread(pt->machine, -1, ptq->tid); =20 if (ptq->thread) { - ptq->pid =3D ptq->thread->pid_; + ptq->pid =3D thread__pid(ptq->thread); if (queue->cpu =3D=3D -1) - ptq->cpu =3D ptq->thread->cpu; + ptq->cpu =3D thread__cpu(ptq->thread); } =20 if (pt->have_guest_sideband && intel_pt_get_guest_from_sideband(ptq)) { @@ -3074,7 +3074,7 @@ static void intel_pt_sample_set_pid_tid_cpu(struct in= tel_pt_queue *ptq, if (ptq->pid =3D=3D -1) { ptq->thread =3D machine__find_thread(m, -1, ptq->tid); if (ptq->thread) - ptq->pid =3D ptq->thread->pid_; + ptq->pid =3D thread__pid(ptq->thread); return; } =20 diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c index 28e49502db5e..2380b41a4caa 100644 --- a/tools/perf/util/jitdump.c +++ b/tools/perf/util/jitdump.c @@ -799,17 +799,19 @@ static void jit_add_pid(struct machine *machine, pid_= t pid) return; } =20 - thread->priv =3D (void *)1; + thread__set_priv(thread, (void *)true); } =20 static bool jit_has_pid(struct machine *machine, pid_t pid) { struct thread *thread =3D machine__find_thread(machine, pid, pid); + void *priv; =20 if (!thread) - return 0; + return false; =20 - return (bool)thread->priv; + priv =3D thread__priv(thread); + return (bool)priv; } =20 int @@ -833,7 +835,7 @@ jit_process(struct perf_session *session, return 0; } =20 - nsi =3D nsinfo__get(thread->nsinfo); + nsi =3D nsinfo__get(thread__nsinfo(thread)); thread__put(thread); =20 /* diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index cbf092e32ee9..5d34d60a0045 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -77,13 +77,14 @@ static int thread_rb_node__cmp_tid(const void *key, con= st struct rb_node *nd) { int to_find =3D (int) *((pid_t *)key); =20 - return to_find - (int)rb_entry(nd, struct thread_rb_node, rb_node)->threa= d->tid; + return to_find - (int)thread__tid(rb_entry(nd, struct thread_rb_node, rb_= node)->thread); } =20 static struct thread_rb_node *thread_rb_node__find(const struct thread *th, struct rb_root *tree) { - struct rb_node *nd =3D rb_find(&th->tid, tree, thread_rb_node__cmp_tid); + pid_t to_find =3D thread__tid(th); + struct rb_node *nd =3D rb_find(&to_find, tree, thread_rb_node__cmp_tid); =20 return rb_entry(nd, struct thread_rb_node, rb_node); } @@ -440,7 +441,7 @@ static struct thread *findnew_guest_code(struct machine= *machine, return NULL; =20 /* Assume maps are set up if there are any */ - if (maps__nr_maps(thread->maps)) + if (maps__nr_maps(thread__maps(thread))) return thread; =20 host_thread =3D machine__find_thread(host_machine, -1, pid); @@ -453,7 +454,7 @@ static struct thread *findnew_guest_code(struct machine= *machine, * Guest code can be found in hypervisor process at the same address * so copy host maps. */ - err =3D maps__clone(thread, host_thread->maps); + err =3D maps__clone(thread, thread__maps(host_thread)); thread__put(host_thread); if (err) goto out_err; @@ -518,45 +519,45 @@ static void machine__update_thread_pid(struct machine= *machine, { struct thread *leader; =20 - if (pid =3D=3D th->pid_ || pid =3D=3D -1 || th->pid_ !=3D -1) + if (pid =3D=3D thread__pid(th) || pid =3D=3D -1 || thread__pid(th) !=3D -= 1) return; =20 - th->pid_ =3D pid; + thread__set_pid(th, pid); =20 - if (th->pid_ =3D=3D th->tid) + if (thread__pid(th) =3D=3D thread__tid(th)) return; =20 - leader =3D __machine__findnew_thread(machine, th->pid_, th->pid_); + leader =3D __machine__findnew_thread(machine, thread__pid(th), thread__pi= d(th)); if (!leader) goto out_err; =20 - if (!leader->maps) - leader->maps =3D maps__new(machine); + if (!thread__maps(leader)) + thread__set_maps(leader, maps__new(machine)); =20 - if (!leader->maps) + if (!thread__maps(leader)) goto out_err; =20 - if (th->maps =3D=3D leader->maps) + if (thread__maps(th) =3D=3D thread__maps(leader)) return; =20 - if (th->maps) { + if (thread__maps(th)) { /* * Maps are created from MMAP events which provide the pid and * tid. Consequently there never should be any maps on a thread * with an unknown pid. Just print an error if there are. */ - if (!maps__empty(th->maps)) + if (!maps__empty(thread__maps(th))) pr_err("Discarding thread maps for %d:%d\n", - th->pid_, th->tid); - maps__put(th->maps); + thread__pid(th), thread__tid(th)); + maps__put(thread__maps(th)); } =20 - th->maps =3D maps__get(leader->maps); + thread__set_maps(th, maps__get(thread__maps(leader))); out_put: thread__put(leader); return; out_err: - pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid); + pr_err("Failed to join map groups for %d:%d\n", thread__pid(th), thread__= tid(th)); goto out_put; } =20 @@ -573,7 +574,7 @@ __threads__get_last_match(struct threads *threads, stru= ct machine *machine, =20 th =3D threads->last_match; if (th !=3D NULL) { - if (th->tid =3D=3D tid) { + if (thread__tid(th) =3D=3D tid) { machine__update_thread_pid(machine, th, pid); return thread__get(th); } @@ -632,13 +633,13 @@ static struct thread *____machine__findnew_thread(str= uct machine *machine, parent =3D *p; th =3D rb_entry(parent, struct thread_rb_node, rb_node)->thread; =20 - if (th->tid =3D=3D tid) { + if (thread__tid(th) =3D=3D tid) { threads__set_last_match(threads, th); machine__update_thread_pid(machine, th, pid); return thread__get(th); } =20 - if (tid < th->tid) + if (tid < thread__tid(th)) p =3D &(*p)->rb_left; else { p =3D &(*p)->rb_right; @@ -2049,7 +2050,7 @@ int machine__process_mmap_event(struct machine *machi= ne, union perf_event *event static void __machine__remove_thread(struct machine *machine, struct threa= d_rb_node *nd, struct thread *th, bool lock) { - struct threads *threads =3D machine__threads(machine, th->tid); + struct threads *threads =3D machine__threads(machine, thread__tid(th)); =20 if (!nd) nd =3D thread_rb_node__find(th, &threads->entries.rb_root); @@ -2060,7 +2061,7 @@ static void __machine__remove_thread(struct machine *= machine, struct thread_rb_n if (lock) down_write(&threads->lock); =20 - BUG_ON(refcount_read(&th->refcnt) =3D=3D 0); + BUG_ON(refcount_read(thread__refcnt(th)) =3D=3D 0); =20 thread__put(nd->thread); rb_erase_cached(&nd->rb_node, &threads->entries); @@ -2099,9 +2100,9 @@ int machine__process_fork_event(struct machine *machi= ne, union perf_event *event * (fork) event that would have removed the thread was lost. Assume the * latter case and continue on as best we can. */ - if (parent->pid_ !=3D (pid_t)event->fork.ppid) { + if (thread__pid(parent) !=3D (pid_t)event->fork.ppid) { dump_printf("removing erroneous parent thread %d/%d\n", - parent->pid_, parent->tid); + thread__pid(parent), thread__tid(parent)); machine__remove_thread(machine, parent); thread__put(parent); parent =3D machine__findnew_thread(machine, event->fork.ppid, @@ -2511,7 +2512,7 @@ static void save_lbr_cursor_node(struct thread *threa= d, struct callchain_cursor *cursor, int idx) { - struct lbr_stitch *lbr_stitch =3D thread->lbr_stitch; + struct lbr_stitch *lbr_stitch =3D thread__lbr_stitch(thread); =20 if (!lbr_stitch) return; @@ -2553,7 +2554,7 @@ static int lbr_callchain_add_lbr_ip(struct thread *th= read, * in callchain_cursor_commit() when the writing session is closed. * Using curr and pos to track the current cursor node. */ - if (thread->lbr_stitch) { + if (thread__lbr_stitch(thread)) { cursor->curr =3D NULL; cursor->pos =3D cursor->nr; if (cursor->nr) { @@ -2581,7 +2582,7 @@ static int lbr_callchain_add_lbr_ip(struct thread *th= read, * But does not need to save current cursor node for entry 0. * It's impossible to stitch the whole LBRs of previous sample. */ - if (thread->lbr_stitch && (cursor->pos !=3D cursor->nr)) { + if (thread__lbr_stitch(thread) && (cursor->pos !=3D cursor->nr)) { if (!cursor->curr) cursor->curr =3D cursor->first; else @@ -2634,7 +2635,7 @@ static int lbr_callchain_add_lbr_ip(struct thread *th= read, static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread, struct callchain_cursor *cursor) { - struct lbr_stitch *lbr_stitch =3D thread->lbr_stitch; + struct lbr_stitch *lbr_stitch =3D thread__lbr_stitch(thread); struct callchain_cursor_node *cnode; struct stitch_list *stitch_node; int err; @@ -2658,7 +2659,7 @@ static int lbr_callchain_add_stitched_lbr_ip(struct t= hread *thread, =20 static struct stitch_list *get_stitch_node(struct thread *thread) { - struct lbr_stitch *lbr_stitch =3D thread->lbr_stitch; + struct lbr_stitch *lbr_stitch =3D thread__lbr_stitch(thread); struct stitch_list *stitch_node; =20 if (!list_empty(&lbr_stitch->free_lists)) { @@ -2682,7 +2683,7 @@ static bool has_stitched_lbr(struct thread *thread, struct branch_entry *cur_entries =3D perf_sample__branch_entries(cur); struct branch_stack *prev_stack =3D prev->branch_stack; struct branch_entry *prev_entries =3D perf_sample__branch_entries(prev); - struct lbr_stitch *lbr_stitch =3D thread->lbr_stitch; + struct lbr_stitch *lbr_stitch =3D thread__lbr_stitch(thread); int i, j, nr_identical_branches =3D 0; struct stitch_list *stitch_node; u64 cur_base, distance; @@ -2746,27 +2747,29 @@ static bool has_stitched_lbr(struct thread *thread, =20 static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr) { - if (thread->lbr_stitch) + if (thread__lbr_stitch(thread)) return true; =20 - thread->lbr_stitch =3D zalloc(sizeof(*thread->lbr_stitch)); - if (!thread->lbr_stitch) + thread__set_lbr_stitch(thread, zalloc(sizeof(struct lbr_stitch))); + if (!thread__lbr_stitch(thread)) goto err; =20 - thread->lbr_stitch->prev_lbr_cursor =3D calloc(max_lbr + 1, sizeof(struct= callchain_cursor_node)); - if (!thread->lbr_stitch->prev_lbr_cursor) + thread__lbr_stitch(thread)->prev_lbr_cursor =3D + calloc(max_lbr + 1, sizeof(struct callchain_cursor_node)); + if (!thread__lbr_stitch(thread)->prev_lbr_cursor) goto free_lbr_stitch; =20 - INIT_LIST_HEAD(&thread->lbr_stitch->lists); - INIT_LIST_HEAD(&thread->lbr_stitch->free_lists); + INIT_LIST_HEAD(&thread__lbr_stitch(thread)->lists); + INIT_LIST_HEAD(&thread__lbr_stitch(thread)->free_lists); =20 return true; =20 free_lbr_stitch: - zfree(&thread->lbr_stitch); + free(thread__lbr_stitch(thread)); + thread__set_lbr_stitch(thread, NULL); err: pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitc= h\n"); - thread->lbr_stitch_enable =3D false; + thread__set_lbr_stitch_enable(thread, false); return false; } =20 @@ -2802,9 +2805,9 @@ static int resolve_lbr_callchain_sample(struct thread= *thread, if (i =3D=3D chain_nr) return 0; =20 - if (thread->lbr_stitch_enable && !sample->no_hw_idx && + if (thread__lbr_stitch_enable(thread) && !sample->no_hw_idx && (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) { - lbr_stitch =3D thread->lbr_stitch; + lbr_stitch =3D thread__lbr_stitch(thread); =20 stitched_lbr =3D has_stitched_lbr(thread, sample, &lbr_stitch->prev_sample, @@ -2884,7 +2887,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(maps__machine(thread->maps), "arm64")) + if (machine__normalized_is(maps__machine(thread__maps(thread)), "arm64")) return get_leaf_frame_caller_aarch64(sample, thread, usr_idx); else return 0; @@ -3265,7 +3268,7 @@ int machine__set_current_tid(struct machine *machine,= int cpu, pid_t pid, if (!thread) return -ENOMEM; =20 - thread->cpu =3D cpu; + thread__set_cpu(thread, cpu); thread__put(thread); =20 return 0; diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 4d9944bbf5e4..ae1d54d4880a 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -137,7 +137,7 @@ struct map *map__new(struct machine *machine, u64 start= , u64 len, no_dso =3D is_no_dso_memory(filename); map->prot =3D prot; map->flags =3D flags; - nsi =3D nsinfo__get(thread->nsinfo); + nsi =3D nsinfo__get(thread__nsinfo(thread)); =20 if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) { snprintf(newfilename, sizeof(newfilename), diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 1aeb1db58fe5..5ae6379a1b42 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -384,7 +384,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) */ int maps__clone(struct thread *thread, struct maps *parent) { - struct maps *maps =3D thread->maps; + struct maps *maps =3D thread__maps(thread); int err; struct map_rb_node *rb_node; =20 diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index 40964078f92f..f3d262e871ac 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -1163,11 +1163,11 @@ static int python_export_thread(struct db_export *d= be, struct thread *thread, =20 t =3D tuple_new(5); =20 - tuple_set_d64(t, 0, thread->db_id); + tuple_set_d64(t, 0, thread__db_id(thread)); tuple_set_d64(t, 1, machine->db_id); tuple_set_d64(t, 2, main_thread_db_id); - tuple_set_s32(t, 3, thread->pid_); - tuple_set_s32(t, 4, thread->tid); + tuple_set_s32(t, 3, thread__pid(thread)); + tuple_set_s32(t, 4, thread__tid(thread)); =20 call_object(tables->thread_handler, t, "thread_table"); =20 @@ -1186,7 +1186,7 @@ static int python_export_comm(struct db_export *dbe, = struct comm *comm, =20 tuple_set_d64(t, 0, comm->db_id); tuple_set_string(t, 1, comm__str(comm)); - tuple_set_d64(t, 2, thread->db_id); + tuple_set_d64(t, 2, thread__db_id(thread)); tuple_set_d64(t, 3, comm->start); tuple_set_s32(t, 4, comm->exec); =20 @@ -1207,7 +1207,7 @@ static int python_export_comm_thread(struct db_export= *dbe, u64 db_id, =20 tuple_set_d64(t, 0, db_id); tuple_set_d64(t, 1, comm->db_id); - tuple_set_d64(t, 2, thread->db_id); + tuple_set_d64(t, 2, thread__db_id(thread)); =20 call_object(tables->comm_thread_handler, t, "comm_thread_table"); =20 @@ -1292,7 +1292,7 @@ static void python_export_sample_table(struct db_expo= rt *dbe, tuple_set_d64(t, 0, es->db_id); tuple_set_d64(t, 1, es->evsel->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, 3, thread__db_id(es->al->thread)); tuple_set_d64(t, 4, es->comm_db_id); tuple_set_d64(t, 5, es->dso_db_id); tuple_set_d64(t, 6, es->sym_db_id); @@ -1382,7 +1382,7 @@ static int python_export_call_return(struct db_export= *dbe, t =3D tuple_new(14); =20 tuple_set_d64(t, 0, cr->db_id); - tuple_set_d64(t, 1, cr->thread->db_id); + tuple_set_d64(t, 1, thread__db_id(cr->thread)); tuple_set_d64(t, 2, comm_db_id); tuple_set_d64(t, 3, cr->cp->db_id); tuple_set_d64(t, 4, cr->call_time); diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index e2806791c76a..65ac9f7fdf7e 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -2807,7 +2807,7 @@ static int perf_session__set_guest_cpu(struct perf_se= ssion *session, pid_t pid, =20 if (!thread) return -ENOMEM; - thread->guest_cpu =3D guest_cpu; + thread__set_guest_cpu(thread, guest_cpu); thread__put(thread); =20 return 0; diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index 650cd8df4041..5e45c770f91d 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c @@ -108,7 +108,7 @@ static int64_t cmp_null(const void *l, const void *r) static int64_t sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) { - return right->thread->tid - left->thread->tid; + return thread__tid(right->thread) - thread__tid(left->thread); } =20 static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf, @@ -117,7 +117,7 @@ static int hist_entry__thread_snprintf(struct hist_entr= y *he, char *bf, const char *comm =3D thread__comm_str(he->thread); =20 width =3D max(7U, width) - 8; - return repsep_snprintf(bf, size, "%7d:%-*.*s", he->thread->tid, + return repsep_snprintf(bf, size, "%7d:%-*.*s", thread__tid(he->thread), width, width, comm ?: ""); } =20 @@ -1543,8 +1543,10 @@ sort__dcacheline_cmp(struct hist_entry *left, struct= hist_entry *right) !l_dso->id.ino && !l_dso->id.ino_generation) { /* userspace anonymous */ =20 - if (left->thread->pid_ > right->thread->pid_) return -1; - if (left->thread->pid_ < right->thread->pid_) return 1; + if (thread__pid(left->thread) > thread__pid(right->thread)) + return -1; + if (thread__pid(left->thread) < thread__pid(right->thread)) + return 1; } =20 addr: diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c index 4b85c1728012..374d142e7390 100644 --- a/tools/perf/util/thread-stack.c +++ b/tools/perf/util/thread-stack.c @@ -112,7 +112,7 @@ struct thread_stack { */ static inline bool thread_stack__per_cpu(struct thread *thread) { - return !(thread->tid || thread->pid_); + return !(thread__tid(thread) || thread__pid(thread)); } =20 static int thread_stack__grow(struct thread_stack *ts) @@ -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 && maps__machine(thread->maps)) { - struct machine *machine =3D maps__machine(thread->maps); + if (thread__maps(thread) && maps__machine(thread__maps(thread))) { + struct machine *machine =3D maps__machine(thread__maps(thread)); const char *arch =3D perf_env__arch(machine->env); =20 ts->kernel_start =3D machine__kernel_start(machine); @@ -175,7 +175,7 @@ static struct thread_stack *thread_stack__new(struct th= read *thread, int cpu, bool callstack, unsigned int br_stack_sz) { - struct thread_stack *ts =3D thread->ts, *new_ts; + struct thread_stack *ts =3D thread__ts(thread), *new_ts; unsigned int old_sz =3D ts ? ts->arr_sz : 0; unsigned int new_sz =3D 1; =20 @@ -189,8 +189,8 @@ static struct thread_stack *thread_stack__new(struct th= read *thread, int cpu, if (ts) memcpy(new_ts, ts, old_sz * sizeof(*ts)); new_ts->arr_sz =3D new_sz; - zfree(&thread->ts); - thread->ts =3D new_ts; + free(thread__ts(thread)); + thread__set_ts(thread, new_ts); ts =3D new_ts; } =20 @@ -207,7 +207,7 @@ static struct thread_stack *thread_stack__new(struct th= read *thread, int cpu, =20 static struct thread_stack *thread__cpu_stack(struct thread *thread, int c= pu) { - struct thread_stack *ts =3D thread->ts; + struct thread_stack *ts =3D thread__ts(thread); =20 if (cpu < 0) cpu =3D 0; @@ -232,7 +232,7 @@ static inline struct thread_stack *thread__stack(struct= thread *thread, if (thread_stack__per_cpu(thread)) return thread__cpu_stack(thread, cpu); =20 - return thread->ts; + return thread__ts(thread); } =20 static int thread_stack__push(struct thread_stack *ts, u64 ret_addr, @@ -363,7 +363,7 @@ static int __thread_stack__flush(struct thread *thread,= struct thread_stack *ts) =20 int thread_stack__flush(struct thread *thread) { - struct thread_stack *ts =3D thread->ts; + struct thread_stack *ts =3D thread__ts(thread); unsigned int pos; int err =3D 0; =20 @@ -502,13 +502,14 @@ static void thread_stack__reset(struct thread *thread= , struct thread_stack *ts) =20 void thread_stack__free(struct thread *thread) { - struct thread_stack *ts =3D thread->ts; + struct thread_stack *ts =3D thread__ts(thread); unsigned int pos; =20 if (ts) { for (pos =3D 0; pos < ts->arr_sz; pos++) __thread_stack__free(thread, ts + pos); - zfree(&thread->ts); + free(thread__ts(thread)); + thread__set_ts(thread, NULL); } } =20 @@ -1127,7 +1128,7 @@ int thread_stack__process(struct thread *thread, stru= ct comm *comm, ts->rstate =3D X86_RETPOLINE_POSSIBLE; =20 /* Flush stack on exec */ - if (ts->comm !=3D comm && thread->pid_ =3D=3D thread->tid) { + if (ts->comm !=3D comm && thread__pid(thread) =3D=3D thread__tid(thread))= { err =3D __thread_stack__flush(thread, ts); if (err) return err; diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 38d300e3e4d3..9a1db3be6436 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -21,19 +21,20 @@ =20 int thread__init_maps(struct thread *thread, struct machine *machine) { - pid_t pid =3D thread->pid_; + pid_t pid =3D thread__pid(thread); =20 - if (pid =3D=3D thread->tid || pid =3D=3D -1) { - thread->maps =3D maps__new(machine); + if (pid =3D=3D thread__tid(thread) || pid =3D=3D -1) { + thread__set_maps(thread, maps__new(machine)); } else { struct thread *leader =3D __machine__findnew_thread(machine, pid, pid); + if (leader) { - thread->maps =3D maps__get(leader->maps); + thread__set_maps(thread, maps__get(thread__maps(leader))); thread__put(leader); } } =20 - return thread->maps ? 0 : -1; + return thread__maps(thread) ? 0 : -1; } =20 struct thread *thread__new(pid_t pid, pid_t tid) @@ -43,16 +44,16 @@ struct thread *thread__new(pid_t pid, pid_t tid) struct thread *thread =3D zalloc(sizeof(*thread)); =20 if (thread !=3D NULL) { - thread->pid_ =3D pid; - thread->tid =3D tid; - thread->ppid =3D -1; - thread->cpu =3D -1; - thread->guest_cpu =3D -1; - thread->lbr_stitch_enable =3D false; - INIT_LIST_HEAD(&thread->namespaces_list); - INIT_LIST_HEAD(&thread->comm_list); - init_rwsem(&thread->namespaces_lock); - init_rwsem(&thread->comm_lock); + thread__set_pid(thread, pid); + thread__set_tid(thread, tid); + thread__set_ppid(thread, -1); + thread__set_cpu(thread, -1); + thread__set_guest_cpu(thread, -1); + thread__set_lbr_stitch_enable(thread, false); + INIT_LIST_HEAD(thread__namespaces_list(thread)); + INIT_LIST_HEAD(thread__comm_list(thread)); + init_rwsem(thread__namespaces_lock(thread)); + init_rwsem(thread__comm_lock(thread)); =20 comm_str =3D malloc(32); if (!comm_str) @@ -64,11 +65,11 @@ struct thread *thread__new(pid_t pid, pid_t tid) if (!comm) goto err_thread; =20 - list_add(&comm->list, &thread->comm_list); - refcount_set(&thread->refcnt, 1); + list_add(&comm->list, thread__comm_list(thread)); + refcount_set(thread__refcnt(thread), 1); /* Thread holds first ref to nsdata. */ thread->nsinfo =3D nsinfo__new(pid); - srccode_state_init(&thread->srccode_state); + srccode_state_init(thread__srccode_state(thread)); } =20 return thread; @@ -85,30 +86,30 @@ void thread__delete(struct thread *thread) =20 thread_stack__free(thread); =20 - if (thread->maps) { - maps__put(thread->maps); - thread->maps =3D NULL; + if (thread__maps(thread)) { + maps__put(thread__maps(thread)); + thread__set_maps(thread, NULL); } - down_write(&thread->namespaces_lock); + down_write(thread__namespaces_lock(thread)); list_for_each_entry_safe(namespaces, tmp_namespaces, - &thread->namespaces_list, list) { + thread__namespaces_list(thread), list) { list_del_init(&namespaces->list); namespaces__free(namespaces); } - up_write(&thread->namespaces_lock); + up_write(thread__namespaces_lock(thread)); =20 - down_write(&thread->comm_lock); - list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) { + down_write(thread__comm_lock(thread)); + list_for_each_entry_safe(comm, tmp_comm, thread__comm_list(thread), list)= { list_del_init(&comm->list); comm__free(comm); } - up_write(&thread->comm_lock); + up_write(thread__comm_lock(thread)); =20 nsinfo__zput(thread->nsinfo); - srccode_state_free(&thread->srccode_state); + srccode_state_free(thread__srccode_state(thread)); =20 - exit_rwsem(&thread->namespaces_lock); - exit_rwsem(&thread->comm_lock); + exit_rwsem(thread__namespaces_lock(thread)); + exit_rwsem(thread__comm_lock(thread)); thread__free_stitch_list(thread); free(thread); } @@ -116,31 +117,31 @@ void thread__delete(struct thread *thread) struct thread *thread__get(struct thread *thread) { if (thread) - refcount_inc(&thread->refcnt); + refcount_inc(thread__refcnt(thread)); return thread; } =20 void thread__put(struct thread *thread) { - if (thread && refcount_dec_and_test(&thread->refcnt)) + if (thread && refcount_dec_and_test(thread__refcnt(thread))) thread__delete(thread); } =20 -static struct namespaces *__thread__namespaces(const struct thread *thread) +static struct namespaces *__thread__namespaces(struct thread *thread) { - if (list_empty(&thread->namespaces_list)) + if (list_empty(thread__namespaces_list(thread))) return NULL; =20 - return list_first_entry(&thread->namespaces_list, struct namespaces, list= ); + return list_first_entry(thread__namespaces_list(thread), struct namespace= s, list); } =20 struct namespaces *thread__namespaces(struct thread *thread) { struct namespaces *ns; =20 - down_read(&thread->namespaces_lock); + down_read(thread__namespaces_lock(thread)); ns =3D __thread__namespaces(thread); - up_read(&thread->namespaces_lock); + up_read(thread__namespaces_lock(thread)); =20 return ns; } @@ -154,7 +155,7 @@ static int __thread__set_namespaces(struct thread *thre= ad, u64 timestamp, if (!new) return -ENOMEM; =20 - list_add(&new->list, &thread->namespaces_list); + list_add(&new->list, thread__namespaces_list(thread)); =20 if (timestamp && curr) { /* @@ -174,25 +175,25 @@ int thread__set_namespaces(struct thread *thread, u64= timestamp, { int ret; =20 - down_write(&thread->namespaces_lock); + down_write(thread__namespaces_lock(thread)); ret =3D __thread__set_namespaces(thread, timestamp, event); - up_write(&thread->namespaces_lock); + up_write(thread__namespaces_lock(thread)); return ret; } =20 -struct comm *thread__comm(const struct thread *thread) +struct comm *thread__comm(struct thread *thread) { - if (list_empty(&thread->comm_list)) + if (list_empty(thread__comm_list(thread))) return NULL; =20 - return list_first_entry(&thread->comm_list, struct comm, list); + return list_first_entry(thread__comm_list(thread), struct comm, list); } =20 -struct comm *thread__exec_comm(const struct thread *thread) +struct comm *thread__exec_comm(struct thread *thread) { struct comm *comm, *last =3D NULL, *second_last =3D NULL; =20 - list_for_each_entry(comm, &thread->comm_list, list) { + list_for_each_entry(comm, thread__comm_list(thread), list) { if (comm->exec) return comm; second_last =3D last; @@ -205,7 +206,7 @@ struct comm *thread__exec_comm(const struct thread *thr= ead) * thread, that is very probably wrong. Prefer a later comm to avoid * that case. */ - if (second_last && !last->start && thread->pid_ =3D=3D thread->tid) + if (second_last && !last->start && thread__pid(thread) =3D=3D thread__tid= (thread)) return second_last; =20 return last; @@ -217,7 +218,7 @@ static int ____thread__set_comm(struct thread *thread, = const char *str, struct comm *new, *curr =3D thread__comm(thread); =20 /* Override the default :tid entry */ - if (!thread->comm_set) { + if (!thread__comm_set(thread)) { int err =3D comm__override(curr, str, timestamp, exec); if (err) return err; @@ -225,13 +226,13 @@ static int ____thread__set_comm(struct thread *thread= , const char *str, new =3D comm__new(str, timestamp, exec); if (!new) return -ENOMEM; - list_add(&new->list, &thread->comm_list); + list_add(&new->list, thread__comm_list(thread)); =20 if (exec) - unwind__flush_access(thread->maps); + unwind__flush_access(thread__maps(thread)); } =20 - thread->comm_set =3D true; + thread__set_comm_set(thread, true); =20 return 0; } @@ -241,9 +242,9 @@ int __thread__set_comm(struct thread *thread, const cha= r *str, u64 timestamp, { int ret; =20 - down_write(&thread->comm_lock); + down_write(thread__comm_lock(thread)); ret =3D ____thread__set_comm(thread, str, timestamp, exec); - up_write(&thread->comm_lock); + up_write(thread__comm_lock(thread)); return ret; } =20 @@ -255,7 +256,7 @@ int thread__set_comm_from_proc(struct thread *thread) int err =3D -1; =20 if (!(snprintf(path, sizeof(path), "%d/task/%d/comm", - thread->pid_, thread->tid) >=3D (int)sizeof(path)) && + thread__pid(thread), thread__tid(thread)) >=3D (int)sizeof(path))= && procfs__read_str(path, &comm, &sz) =3D=3D 0) { comm[sz - 1] =3D '\0'; err =3D thread__set_comm(thread, comm, 0); @@ -264,7 +265,7 @@ int thread__set_comm_from_proc(struct thread *thread) return err; } =20 -static const char *__thread__comm_str(const struct thread *thread) +static const char *__thread__comm_str(struct thread *thread) { const struct comm *comm =3D thread__comm(thread); =20 @@ -278,9 +279,9 @@ const char *thread__comm_str(struct thread *thread) { const char *str; =20 - down_read(&thread->comm_lock); + down_read(thread__comm_lock(thread)); str =3D __thread__comm_str(thread); - up_read(&thread->comm_lock); + up_read(thread__comm_lock(thread)); =20 return str; } @@ -289,23 +290,23 @@ static int __thread__comm_len(struct thread *thread, = const char *comm) { if (!comm) return 0; - thread->comm_len =3D strlen(comm); + thread__set_comm_len(thread, strlen(comm)); =20 - return thread->comm_len; + return thread__var_comm_len(thread); } =20 /* CHECKME: it should probably better return the max comm len from its com= m list */ int thread__comm_len(struct thread *thread) { - int comm_len =3D thread->comm_len; + int comm_len =3D thread__var_comm_len(thread); =20 if (!comm_len) { const char *comm; =20 - down_read(&thread->comm_lock); + down_read(thread__comm_lock(thread)); comm =3D __thread__comm_str(thread); comm_len =3D __thread__comm_len(thread, comm); - up_read(&thread->comm_lock); + up_read(thread__comm_lock(thread)); } =20 return comm_len; @@ -313,33 +314,33 @@ int thread__comm_len(struct thread *thread) =20 size_t thread__fprintf(struct thread *thread, FILE *fp) { - return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread= )) + - maps__fprintf(thread->maps, fp); + return fprintf(fp, "Thread %d %s\n", thread__tid(thread), thread__comm_st= r(thread)) + + maps__fprintf(thread__maps(thread), fp); } =20 int thread__insert_map(struct thread *thread, struct map *map) { int ret; =20 - ret =3D unwind__prepare_access(thread->maps, map, NULL); + ret =3D unwind__prepare_access(thread__maps(thread), map, NULL); if (ret) return ret; =20 - maps__fixup_overlappings(thread->maps, map, stderr); - return maps__insert(thread->maps, map); + maps__fixup_overlappings(thread__maps(thread), map, stderr); + return maps__insert(thread__maps(thread), map); } =20 static int __thread__prepare_access(struct thread *thread) { bool initialized =3D false; int err =3D 0; - struct maps *maps =3D thread->maps; + struct maps *maps =3D thread__maps(thread); struct map_rb_node *rb_node; =20 down_read(maps__lock(maps)); =20 maps__for_each_entry(maps, rb_node) { - err =3D unwind__prepare_access(thread->maps, rb_node->map, &initialized); + err =3D unwind__prepare_access(thread__maps(thread), rb_node->map, &init= ialized); if (err || initialized) break; } @@ -362,21 +363,22 @@ static int thread__prepare_access(struct thread *thre= ad) static int thread__clone_maps(struct thread *thread, struct thread *parent= , bool do_maps_clone) { /* This is new thread, we share map groups for process. */ - if (thread->pid_ =3D=3D parent->pid_) + if (thread__pid(thread) =3D=3D thread__pid(parent)) return thread__prepare_access(thread); =20 - if (thread->maps =3D=3D parent->maps) { + if (thread__maps(thread) =3D=3D thread__maps(parent)) { pr_debug("broken map groups on thread %d/%d parent %d/%d\n", - thread->pid_, thread->tid, parent->pid_, parent->tid); + thread__pid(thread), thread__tid(thread), + thread__pid(parent), thread__tid(parent)); return 0; } /* But this one is new process, copy maps. */ - return do_maps_clone ? maps__clone(thread, parent->maps) : 0; + return do_maps_clone ? maps__clone(thread, thread__maps(parent)) : 0; } =20 int thread__fork(struct thread *thread, struct thread *parent, u64 timesta= mp, bool do_maps_clone) { - if (parent->comm_set) { + if (thread__comm_set(parent)) { const char *comm =3D thread__comm_str(parent); int err; if (!comm) @@ -386,7 +388,7 @@ int thread__fork(struct thread *thread, struct thread *= parent, u64 timestamp, bo return err; } =20 - thread->ppid =3D parent->tid; + thread__set_ppid(thread, thread__tid(parent)); return thread__clone_maps(thread, parent, do_maps_clone); } =20 @@ -410,13 +412,13 @@ void thread__find_cpumode_addr_location(struct thread= *thread, u64 addr, =20 struct thread *thread__main_thread(struct machine *machine, struct thread = *thread) { - if (thread->pid_ =3D=3D thread->tid) + if (thread__pid(thread) =3D=3D thread__tid(thread)) return thread__get(thread); =20 - if (thread->pid_ =3D=3D -1) + if (thread__pid(thread) =3D=3D -1) return NULL; =20 - return machine__find_thread(machine, thread->pid_, thread->pid_); + return machine__find_thread(machine, thread__pid(thread), thread__pid(thr= ead)); } =20 int thread__memcpy(struct thread *thread, struct machine *machine, @@ -447,7 +449,7 @@ int thread__memcpy(struct thread *thread, struct machin= e *machine, =20 void thread__free_stitch_list(struct thread *thread) { - struct lbr_stitch *lbr_stitch =3D thread->lbr_stitch; + struct lbr_stitch *lbr_stitch =3D thread__lbr_stitch(thread); struct stitch_list *pos, *tmp; =20 if (!lbr_stitch) @@ -464,5 +466,6 @@ void thread__free_stitch_list(struct thread *thread) } =20 zfree(&lbr_stitch->prev_lbr_cursor); - zfree(&thread->lbr_stitch); + free(thread__lbr_stitch(thread)); + thread__set_lbr_stitch(thread, NULL); } diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 3b3f9fb5a916..b103992c3831 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -96,8 +96,8 @@ static inline int thread__set_comm(struct thread *thread,= const char *comm, int thread__set_comm_from_proc(struct thread *thread); =20 int thread__comm_len(struct thread *thread); -struct comm *thread__comm(const struct thread *thread); -struct comm *thread__exec_comm(const struct thread *thread); +struct comm *thread__comm(struct thread *thread); +struct comm *thread__exec_comm(struct thread *thread); const char *thread__comm_str(struct thread *thread); int thread__insert_map(struct thread *thread, struct map *map); int thread__fork(struct thread *thread, struct thread *parent, u64 timesta= mp, bool do_maps_clone); @@ -121,6 +121,126 @@ void thread__find_cpumode_addr_location(struct thread= *thread, u64 addr, int thread__memcpy(struct thread *thread, struct machine *machine, void *buf, u64 ip, int len, bool *is64bit); =20 +static inline struct maps *thread__maps(struct thread *thread) +{ + return thread->maps; +} + +static inline void thread__set_maps(struct thread *thread, struct maps *ma= ps) +{ + thread->maps =3D maps; +} + +static inline pid_t thread__pid(const struct thread *thread) +{ + return thread->pid_; +} + +static inline void thread__set_pid(struct thread *thread, pid_t pid_) +{ + thread->pid_ =3D pid_; +} + +static inline pid_t thread__tid(const struct thread *thread) +{ + return thread->tid; +} + +static inline void thread__set_tid(struct thread *thread, pid_t tid) +{ + thread->tid =3D tid; +} + +static inline pid_t thread__ppid(const struct thread *thread) +{ + return thread->ppid; +} + +static inline void thread__set_ppid(struct thread *thread, pid_t ppid) +{ + thread->ppid =3D ppid; +} + +static inline int thread__cpu(const struct thread *thread) +{ + return thread->cpu; +} + +static inline void thread__set_cpu(struct thread *thread, int cpu) +{ + thread->cpu =3D cpu; +} + +static inline int thread__guest_cpu(const struct thread *thread) +{ + return thread->guest_cpu; +} + +static inline void thread__set_guest_cpu(struct thread *thread, int guest_= cpu) +{ + thread->guest_cpu =3D guest_cpu; +} + +static inline refcount_t *thread__refcnt(struct thread *thread) +{ + return &thread->refcnt; +} + +static inline bool thread__comm_set(const struct thread *thread) +{ + return thread->comm_set; +} + +static inline void thread__set_comm_set(struct thread *thread, bool set) +{ + thread->comm_set =3D set; +} + +static inline int thread__var_comm_len(const struct thread *thread) +{ + return thread->comm_len; +} + +static inline void thread__set_comm_len(struct thread *thread, int len) +{ + thread->comm_len =3D len; +} + +static inline struct list_head *thread__namespaces_list(struct thread *thr= ead) +{ + return &thread->namespaces_list; +} + +static inline int thread__namespaces_list_empty(const struct thread *threa= d) +{ + return list_empty(&thread->namespaces_list); +} + +static inline struct rw_semaphore *thread__namespaces_lock(struct thread *= thread) +{ + return &thread->namespaces_lock; +} + +static inline struct list_head *thread__comm_list(struct thread *thread) +{ + return &thread->comm_list; +} + +static inline struct rw_semaphore *thread__comm_lock(struct thread *thread) +{ + return &thread->comm_lock; +} + +static inline u64 thread__db_id(const struct thread *thread) +{ + return thread->db_id; +} + +static inline void thread__set_db_id(struct thread *thread, u64 db_id) +{ + thread->db_id =3D db_id; +} + static inline void *thread__priv(struct thread *thread) { return thread->priv; @@ -131,6 +251,66 @@ static inline void thread__set_priv(struct thread *thr= ead, void *p) thread->priv =3D p; } =20 +static inline struct thread_stack *thread__ts(struct thread *thread) +{ + return thread->ts; +} + +static inline void thread__set_ts(struct thread *thread, struct thread_sta= ck *ts) +{ + thread->ts =3D ts; +} + +static inline struct nsinfo *thread__nsinfo(struct thread *thread) +{ + return thread->nsinfo; +} + +static inline struct srccode_state *thread__srccode_state(struct thread *t= hread) +{ + return &thread->srccode_state; +} + +static inline bool thread__filter(const struct thread *thread) +{ + return thread->filter; +} + +static inline void thread__set_filter(struct thread *thread, bool filter) +{ + thread->filter =3D filter; +} + +static inline int thread__filter_entry_depth(const struct thread *thread) +{ + return thread->filter_entry_depth; +} + +static inline void thread__set_filter_entry_depth(struct thread *thread, i= nt depth) +{ + thread->filter_entry_depth =3D depth; +} + +static inline bool thread__lbr_stitch_enable(const struct thread *thread) +{ + return thread->lbr_stitch_enable; +} + +static inline void thread__set_lbr_stitch_enable(struct thread *thread, bo= ol en) +{ + thread->lbr_stitch_enable =3D en; +} + +static inline struct lbr_stitch *thread__lbr_stitch(struct thread *thread) +{ + return thread->lbr_stitch; +} + +static inline void thread__set_lbr_stitch(struct thread *thread, struct lb= r_stitch *lbrs) +{ + thread->lbr_stitch =3D lbrs; +} + static inline bool thread__is_filtered(struct thread *thread) { if (symbol_conf.comm_list && @@ -139,12 +319,12 @@ static inline bool thread__is_filtered(struct thread = *thread) } =20 if (symbol_conf.pid_list && - !intlist__has_entry(symbol_conf.pid_list, thread->pid_)) { + !intlist__has_entry(symbol_conf.pid_list, thread__pid(thread))) { return true; } =20 if (symbol_conf.tid_list && - !intlist__has_entry(symbol_conf.tid_list, thread->tid)) { + !intlist__has_entry(symbol_conf.tid_list, thread__tid(thread))) { return true; } =20 diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c index bdccfc511b7e..3723b5e31b2a 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 RC_CHK_ACCESS(thread->maps)->machine, + .machine =3D RC_CHK_ACCESS(thread__maps(thread))->machine, .cb =3D cb, .arg =3D arg, .max_stack =3D max_stack, @@ -260,11 +260,11 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *a= rg, if (err) goto out; =20 - err =3D !dwfl_attach_state(ui->dwfl, EM_NONE, thread->tid, &callbacks, ui= ); + err =3D !dwfl_attach_state(ui->dwfl, EM_NONE, thread__tid(thread), &callb= acks, ui); if (err) goto out; =20 - err =3D dwfl_getthread_frames(ui->dwfl, thread->tid, frame_callback, ui); + err =3D dwfl_getthread_frames(ui->dwfl, thread__tid(thread), frame_callba= ck, ui); =20 if (err && ui->max_stack !=3D max_stack) err =3D 0; diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unw= ind-libunwind-local.c index 83dd79dcd597..11f3fc95aa11 100644 --- a/tools/perf/util/unwind-libunwind-local.c +++ b/tools/perf/util/unwind-libunwind-local.c @@ -325,7 +325,7 @@ static int read_unwind_spec_eh_frame(struct dso *dso, s= truct unwind_info *ui, return -EINVAL; } =20 - maps__for_each_entry(ui->thread->maps, map_node) { + maps__for_each_entry(thread__maps(ui->thread), map_node) { struct map *map =3D map_node->map; u64 start =3D map__start(map); =20 @@ -719,7 +719,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 maps__addr_space(ui->thread->maps); + addr_space =3D maps__addr_space(thread__maps(ui->thread)); =20 if (addr_space =3D=3D NULL) return -1; @@ -769,7 +769,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 maps__machine(thread->maps), + .machine =3D maps__machine(thread__maps(thread)), .best_effort =3D best_effort }; =20 diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-li= bunwind.c index 375d23d9a590..76cd63de80a8 100644 --- a/tools/perf/util/unwind-libunwind.c +++ b/tools/perf/util/unwind-libunwind.c @@ -89,7 +89,7 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg, struct perf_sample *data, int max_stack, bool best_effort) { - const struct unwind_libunwind_ops *ops =3D maps__unwind_libunwind_ops(thr= ead->maps); + const struct unwind_libunwind_ops *ops =3D maps__unwind_libunwind_ops(thr= ead__maps(thread)); =20 if (ops) return ops->get_entries(cb, arg, thread, data, max_stack, best_effort); diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c index ec777ee11493..ae3eee69b659 100644 --- a/tools/perf/util/vdso.c +++ b/tools/perf/util/vdso.c @@ -146,7 +146,7 @@ static enum dso_type machine__thread_dso_type(struct ma= chine *machine, enum dso_type dso_type =3D DSO__TYPE_UNKNOWN; struct map_rb_node *rb_node; =20 - maps__for_each_entry(thread->maps, rb_node) { + maps__for_each_entry(thread__maps(thread), rb_node) { struct dso *dso =3D map__dso(rb_node->map); =20 if (!dso || dso->long_name[0] !=3D '/') --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 00F56C7EE25 for ; Thu, 8 Jun 2023 23:29:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237172AbjFHX3l (ORCPT ); Thu, 8 Jun 2023 19:29:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237139AbjFHX3X (ORCPT ); Thu, 8 Jun 2023 19:29:23 -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 7DFCD30F1 for ; Thu, 8 Jun 2023 16:29:22 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bac6a453dd5so1655485276.2 for ; Thu, 08 Jun 2023 16:29:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266961; x=1688858961; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=d8nZzpxNSCbRXHMzfJBAp7eVEh2n3zxwlDLBqqql/Uw=; b=Qc8L5FoARN90TEPW/tyBzgIkWZWMgC4vWKig+/L6A+nCr71SA5XPdM4GS9PuePF8XD vDVxVO4oJh1fDlu65LjQD5xdYcsCS7gzFhfD9C41ue5zDcLBzC2PR5pa1JLVsFMSFmwR /Gjccb+eR+2tOYSY/bXlfmbYOqiBN4vn8OyOVFLo6eGRNsJcG3cObVOUT3yOPgaWmt/e 0t3prXyPPW2p8WgFaTmsrsK+nwyFr0+bA5n3BJoyGFQxtmlXO9gnb2wQgqdg91RtnM5Y u9HM9/WhfiAzifHPJ9RsufS+aukJguBxiGtTEZZAzCE+10ItDUd4dhL1y8uJwJIsO8Rx QbrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266961; x=1688858961; h=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=d8nZzpxNSCbRXHMzfJBAp7eVEh2n3zxwlDLBqqql/Uw=; b=fQ8zqv09AwOQvMQFEMH5ktqq51kYbZTb/Ggx+OkWOp5xN6tQk8C2SS3sdCBrovxHFc D0FstONN5Bba4CWve/2nrYCvMsdEJaW11husdKKKMit4k6SFRfX7+KA1ihldiCjU9MRK xl25E7ZVCvLIk60us/d7eo1vxi+51D7KPTkEvIx+mkJCv2nzh/JN0nUmcZhfQFuzFE/e RjSbOTGmts9GQstaxYS0kmgtX0j1RoVul2CquNAomE4HPrmsX5wj1NnQOl2MA3DcquHT tfqLyXhG4E8ujejE2/fNRKqaAkRsXau/Q8GAUp6xiYbXQioTm2K0HRHAjUz/poH50a5D 065w== X-Gm-Message-State: AC+VfDw6MhFYEFwWn3R8cNY8kVy8MMe/4QTCil3iH3+Pn9cg9lHHQ9yx owIXw32v3BBMppX0kFhRs2+wmF7lk/aJ X-Google-Smtp-Source: ACHHUZ6mO0KY7WuSqm4kMAhhX7wri86AghEsVjmLTPQvjGHUK8KzarbyNv9ibdY6QZfzQ4veQySzF++IRrLF X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a25:2004:0:b0:bbb:8c13:ce26 with SMTP id g4-20020a252004000000b00bbb8c13ce26mr599080ybg.11.1686266961427; Thu, 08 Jun 2023 16:29:21 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:01 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-5-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 04/26] perf maps: Make delete static, always use put From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Address/leak sanitizer with reference count checking can identify the location of leaks, so use put rather than delete to avoid free-ing memory when the reference count is >1. Add maps__zput to ensure the variable is cleared. Signed-off-by: Ian Rogers --- tools/perf/tests/maps.c | 2 +- tools/perf/util/machine.c | 2 +- tools/perf/util/maps.c | 2 +- tools/perf/util/maps.h | 9 ++++++++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/tools/perf/tests/maps.c b/tools/perf/tests/maps.c index 8c0eb5cf8bb5..5bb1123a91a7 100644 --- a/tools/perf/tests/maps.c +++ b/tools/perf/tests/maps.c @@ -140,7 +140,7 @@ static int test__maps__merge_in(struct test_suite *t __= maybe_unused, int subtest ret =3D check_maps(merged3, ARRAY_SIZE(merged3), maps); TEST_ASSERT_VAL("merge check failed", !ret); =20 - maps__delete(maps); + maps__zput(maps); return TEST_OK; } =20 diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 5d34d60a0045..8972c852d3bd 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -248,7 +248,7 @@ void machine__exit(struct machine *machine) return; =20 machine__destroy_kernel_maps(machine); - maps__delete(machine->kmaps); + maps__zput(machine->kmaps); dsos__exit(&machine->dsos); machine__exit_vdso(machine); zfree(&machine->root_dir); diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 5ae6379a1b42..5206a6433117 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -171,7 +171,7 @@ struct maps *maps__new(struct machine *machine) return result; } =20 -void maps__delete(struct maps *maps) +static void maps__delete(struct maps *maps) { maps__exit(maps); unwind__finish_access(maps); diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index d2963456cfbe..83144e0645ed 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -57,13 +57,20 @@ struct kmap { }; =20 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); =20 struct maps *maps__get(struct maps *maps); void maps__put(struct maps *maps); =20 +static inline void __maps__zput(struct maps **map) +{ + maps__put(*map); + *map =3D NULL; +} + +#define maps__zput(map) __maps__zput(&map) + static inline struct rb_root *maps__entries(struct maps *maps) { return &RC_CHK_ACCESS(maps)->entries; --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 DDA09C7EE25 for ; Thu, 8 Jun 2023 23:30:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237352AbjFHX3s (ORCPT ); Thu, 8 Jun 2023 19:29:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236926AbjFHX3Z (ORCPT ); Thu, 8 Jun 2023 19:29:25 -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 3D8E630ED for ; Thu, 8 Jun 2023 16:29:24 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-bb39aebdd87so2572227276.0 for ; Thu, 08 Jun 2023 16:29:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266964; x=1688858964; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=RGWgSynrOj/PLbqXNX2x48CdvWEXkGjqwrkmdfXHnVY=; b=PrpWinbTvoRzHNWvPNJNF33aAhfQd7XYp6Mxz2JjnjGgD8hI7sAxAVnVZFZDbqx9k7 kBAnswjlFIBVG/RzWzMZIKXQfjzOrDd1B3UW7qog5wXqFHCXPG6Mmah1WywxM6x/M/4M gul2G31zudV0rOyTH/RXGBdTG2oKj2vY491Nxf3Q9wIhew3j5F8lhsWhEkvPkb0V3L5K W0fkpz8PSLCd/K7qSh58D8nJ4WQoeqAEX79ukhshv7jrub3IYKPcPc8zlkyZzfpJUqNr 6wwNxnm+d0ExADXV7eCLubh9bpPLQmLVfYKVHroa9zm9SARHlihF7QiGILGCPxB/Yweb E5Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266964; x=1688858964; h=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=RGWgSynrOj/PLbqXNX2x48CdvWEXkGjqwrkmdfXHnVY=; b=PLQvi+5iCJ4dGiOCoq5BD6ukzqVLCXtnwkvzHeUeE1Ttcu4Hw+yH5ROrv+QFASkupZ JWVG7o+Dd12A8XmkUnYEO6eGVjmqCpcfN1UFKm/ZmJV/HuvtGNyTmylcYuAUQ+mhAqfV TJSk4bYYHfwMmkqtr9RK5lLl9rscD3PaUjSQ/YrCNoKvPr4UvdtIrmjx/CNgyTYWKVu/ CESR0k2dS5cxPS3HqdbPGxFG0uzinUODlkRrDemwsiSjI25N1R4AXNG9tyKGGwt1VNwD GEbCpmif3jIz9VJIND1Fwr3wj/pBA6Mw5VlPh3g3DLLaokRC9duktLTfSEVeQVV3qKvu jVtA== X-Gm-Message-State: AC+VfDy93I19rj8qNJ5SzV5f100t26YpfULk3m0F8IlvW+h+DzAO+py9 201NRaTL+hb8rW05wCYC49dzRTY0lDv1 X-Google-Smtp-Source: ACHHUZ73d7KA7YvKZlm+FTDRrvHKTW9MdriwohP40DR/Hzk2z5mXwezVFmP5a91auPb6VSCYNi8uC90GmFzP X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a05:6902:38a:b0:ba8:381b:f764 with SMTP id f10-20020a056902038a00b00ba8381bf764mr51238ybs.3.1686266963896; Thu, 08 Jun 2023 16:29:23 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:02 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-6-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 05/26] perf addr_location: Move to its own header From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" addr_location is a common abstraction, move it into its own header and source file in preparation for wider clean up. Signed-off-by: Ian Rogers --- tools/perf/util/Build | 1 + tools/perf/util/addr_location.c | 16 ++++++++++++++++ tools/perf/util/addr_location.h | 28 ++++++++++++++++++++++++++++ tools/perf/util/event.c | 12 ------------ tools/perf/util/symbol.h | 17 +---------------- 5 files changed, 46 insertions(+), 28 deletions(-) create mode 100644 tools/perf/util/addr_location.c create mode 100644 tools/perf/util/addr_location.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index c449741adf30..ff2fd1a36bb8 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -1,4 +1,5 @@ perf-y +=3D arm64-frame-pointer-unwind-support.o +perf-y +=3D addr_location.o perf-y +=3D annotate.o perf-y +=3D block-info.o perf-y +=3D block-range.o diff --git a/tools/perf/util/addr_location.c b/tools/perf/util/addr_locatio= n.c new file mode 100644 index 000000000000..c73fc2aa236c --- /dev/null +++ b/tools/perf/util/addr_location.c @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "addr_location.h" +#include "map.h" +#include "thread.h" + +/* + * The preprocess_sample method will return with reference counts for the + * in it, when done using (and perhaps getting ref counts if needing to + * keep a pointer to one of those entries) it must be paired with + * addr_location__put(), so that the refcounts can be decremented. + */ +void addr_location__put(struct addr_location *al) +{ + map__zput(al->map); + thread__zput(al->thread); +} diff --git a/tools/perf/util/addr_location.h b/tools/perf/util/addr_locatio= n.h new file mode 100644 index 000000000000..7dfa7417c0fe --- /dev/null +++ b/tools/perf/util/addr_location.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_ADDR_LOCATION +#define __PERF_ADDR_LOCATION 1 + +#include + +struct thread; +struct maps; +struct map; +struct symbol; + +struct addr_location { + struct thread *thread; + struct maps *maps; + struct map *map; + struct symbol *sym; + const char *srcline; + u64 addr; + char level; + u8 filtered; + u8 cpumode; + s32 cpu; + s32 socket; +}; + +void addr_location__put(struct addr_location *al); + +#endif /* __PERF_ADDR_LOCATION */ diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index e1ce7cb5e421..6ee23145ee7e 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -767,18 +767,6 @@ int machine__resolve(struct machine *machine, struct a= ddr_location *al, return 0; } =20 -/* - * The preprocess_sample method will return with reference counts for the - * in it, when done using (and perhaps getting ref counts if needing to - * keep a pointer to one of those entries) it must be paired with - * addr_location__put(), so that the refcounts can be decremented. - */ -void addr_location__put(struct addr_location *al) -{ - map__zput(al->map); - thread__zput(al->thread); -} - bool is_bts_event(struct perf_event_attr *attr) { return attr->type =3D=3D PERF_TYPE_HARDWARE && diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 7558735543c2..5ca8665dd2c1 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -9,6 +9,7 @@ #include #include #include +#include "addr_location.h" #include "path.h" #include "symbol_conf.h" #include "spark.h" @@ -120,22 +121,6 @@ struct ref_reloc_sym { u64 unrelocated_addr; }; =20 -struct addr_location { - struct thread *thread; - struct maps *maps; - struct map *map; - struct symbol *sym; - const char *srcline; - u64 addr; - char level; - u8 filtered; - u8 cpumode; - s32 cpu; - s32 socket; -}; - -void addr_location__put(struct addr_location *al); - int dso__load(struct dso *dso, struct map *map); int dso__load_vmlinux(struct dso *dso, struct map *map, const char *vmlinux, bool vmlinux_allocated); --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 0801EC7EE37 for ; Thu, 8 Jun 2023 23:30:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237497AbjFHX3w (ORCPT ); Thu, 8 Jun 2023 19:29:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237156AbjFHX3c (ORCPT ); Thu, 8 Jun 2023 19:29:32 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0334E30D3 for ; Thu, 8 Jun 2023 16:29:27 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-517bad1b8c5so232150a12.0 for ; Thu, 08 Jun 2023 16:29:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266966; x=1688858966; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=kjATaEFKnu3AxVMXvUzymAs1Q8GEb5ijJWr8l8ZMBuI=; b=L5m7+zFRnG2OaMCtRHfv1dL7EvBwYdevUAqImj02nP8bfiJtw8FhY3cVdhJCsgifZA 8WP2aCrm7afFY9x8yj5UnjzolzEtQ3po0lzf/Q0Q05vDXhjFyagsUR7IVxCnbcFrPSEO igMDXC144zNWoYE2VWPZgmN33EHFVg4RLrIp9Ap812CbSWBATPD65vsevxYHnujH9IbW F/s7LcAWUQjIJBGXr6rSIyfC+wBvV3V6wO1qQzgxppaokYXO8Qjv/Jxi7T8ODm5pOqm2 lkBcdejYt7OBNlb5rCy4QM0bWsCkpFdJqt8ukv93qpEV56NRYVMzvbKinzvW+d8uzKll jgTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266966; x=1688858966; h=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=kjATaEFKnu3AxVMXvUzymAs1Q8GEb5ijJWr8l8ZMBuI=; b=HF0kKnpR70lCWVtr168nnoKNeTc4bZFd0F/SjhHWuLabphFNB2qaRnvSKbXuP5bkk9 qiO9aUGJum3bjr+c30AnfPPevazQWM12z+m9zbLA52V7aCvCIq5SgVKKoEJ8RYGmkJ/i zwrg2xLhv4qjmF0G7/HiilylZobQ+Anwf0eu8DbPjF37Dck7hsnSLytkcWKa6F0r4uXr Lv74qrrigQ/Z15EB4c5XXXtt+AHcbiUHLZkDM4ViBmv8BiNZfvmbmGDmy2wvx9urbn9N oMzBVVglvx3l/1MRwIuDLZZkPrz8nicNUA76XK08FXRFAQkf4LYeEfvDlE3qc+PRHslN 1SLw== X-Gm-Message-State: AC+VfDykybrH7mPJ9iFu3rx0SUn8qXGqYfyanDb5WZi+6q9UhfrPG4OV xLY4//DHa9bqMkmeOlH1OrgPOBgggIDr X-Google-Smtp-Source: ACHHUZ6bwgHIFee9zw5KXtI7HDtjdNuwo3IPnGTwyXCgbHDTE1yqglPQ+eZ7zAKC73k888gez3h64WtSCFZm X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a65:670b:0:b0:53f:32cf:bcd1 with SMTP id u11-20020a65670b000000b0053f32cfbcd1mr168368pgf.5.1686266966456; Thu, 08 Jun 2023 16:29:26 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:03 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-7-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 06/26] perf addr_location: Add init/exit/copy functions From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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 addr_location holds references to multiple reference counted objects. Add init/exit functions to make maintenance of those more consistent with the rest of the code and to try to avoid leaks. Modification of thread reference counts isn't included in this change. Signed-off-by: Ian Rogers --- tools/perf/builtin-annotate.c | 28 ++++--- tools/perf/builtin-c2c.c | 12 ++- tools/perf/builtin-diff.c | 16 ++-- tools/perf/builtin-inject.c | 2 + tools/perf/builtin-kmem.c | 11 ++- tools/perf/builtin-kwork.c | 15 +++- tools/perf/builtin-mem.c | 4 +- tools/perf/builtin-report.c | 6 +- tools/perf/builtin-sched.c | 2 + tools/perf/builtin-script.c | 77 +++++++++++-------- tools/perf/builtin-timechart.c | 11 ++- tools/perf/builtin-top.c | 6 +- tools/perf/builtin-trace.c | 10 ++- tools/perf/tests/code-reading.c | 3 +- tools/perf/tests/hists_cumulate.c | 17 ++-- tools/perf/tests/hists_filter.c | 11 ++- tools/perf/tests/hists_link.c | 18 +++-- tools/perf/tests/hists_output.c | 10 ++- tools/perf/tests/mmap-thread-lookup.c | 4 +- tools/perf/util/addr_location.c | 30 +++++++- tools/perf/util/addr_location.h | 5 +- tools/perf/util/build-id.c | 2 + tools/perf/util/cs-etm.c | 20 +++-- tools/perf/util/data-convert-json.c | 8 +- tools/perf/util/db-export.c | 4 +- tools/perf/util/dlfilter.c | 13 +++- tools/perf/util/event.c | 16 ++-- tools/perf/util/evsel_fprintf.c | 8 +- tools/perf/util/hist.c | 8 +- tools/perf/util/intel-pt.c | 66 +++++++++++----- tools/perf/util/machine.c | 35 +++++---- .../scripting-engines/trace-event-python.c | 10 ++- tools/perf/util/thread.c | 13 +++- tools/perf/util/unwind-libdw.c | 21 ++++- tools/perf/util/unwind-libunwind-local.c | 13 +++- 35 files changed, 369 insertions(+), 166 deletions(-) diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c index 425a7e2fd6fb..aeeb801f1ed7 100644 --- a/tools/perf/builtin-annotate.c +++ b/tools/perf/builtin-annotate.c @@ -184,7 +184,7 @@ static int hist_iter__branch_callback(struct hist_entry= _iter *iter, =20 static int process_branch_callback(struct evsel *evsel, struct perf_sample *sample, - struct addr_location *al __maybe_unused, + struct addr_location *al, struct perf_annotate *ann, struct machine *machine) { @@ -195,21 +195,29 @@ static int process_branch_callback(struct evsel *evse= l, .hide_unresolved =3D symbol_conf.hide_unresolved, .ops =3D &hist_iter_branch, }; - struct addr_location a; + int ret; =20 - if (machine__resolve(machine, &a, sample) < 0) - return -1; + addr_location__init(&a); + if (machine__resolve(machine, &a, sample) < 0) { + ret =3D -1; + goto out; + } =20 - if (a.sym =3D=3D NULL) - return 0; + if (a.sym =3D=3D NULL) { + ret =3D 0; + goto out; + } =20 if (a.map !=3D NULL) map__dso(a.map)->hit =3D 1; =20 hist__account_cycles(sample->branch_stack, al, sample, false, NULL); =20 - return hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann); + ret =3D hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann); +out: + addr_location__exit(&a); + return ret; } =20 static bool has_annotation(struct perf_annotate *ann) @@ -272,10 +280,12 @@ static int process_sample_event(struct perf_tool *too= l, struct addr_location al; int ret =3D 0; =20 + addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { pr_warning("problem processing %d event, skipping it.\n", event->header.type); - return -1; + ret =3D -1; + goto out_put; } =20 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap)) @@ -288,7 +298,7 @@ static int process_sample_event(struct perf_tool *tool, ret =3D -1; } out_put: - addr_location__put(&al); + addr_location__exit(&al); return ret; } =20 diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c index ee41a96f0c73..530a44a59f41 100644 --- a/tools/perf/builtin-c2c.c +++ b/tools/perf/builtin-c2c.c @@ -286,10 +286,12 @@ static int process_sample_event(struct perf_tool *too= l __maybe_unused, struct mem_info *mi, *mi_dup; int ret; =20 + addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { pr_debug("problem processing %d event, skipping it.\n", event->header.type); - return -1; + ret =3D -1; + goto out; } =20 if (c2c.stitch_lbr) @@ -301,8 +303,10 @@ static int process_sample_event(struct perf_tool *tool= __maybe_unused, goto out; =20 mi =3D sample__resolve_mem(sample, &al); - if (mi =3D=3D NULL) - return -ENOMEM; + if (mi =3D=3D NULL) { + ret =3D -ENOMEM; + goto out; + } =20 /* * The mi object is released in hists__add_entry_ops, @@ -368,7 +372,7 @@ static int process_sample_event(struct perf_tool *tool = __maybe_unused, } =20 out: - addr_location__put(&al); + addr_location__exit(&al); return ret; =20 free_mi: diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c index dbb0562d6a4f..ca39657ee407 100644 --- a/tools/perf/builtin-diff.c +++ b/tools/perf/builtin-diff.c @@ -409,15 +409,17 @@ static int diff__process_sample_event(struct perf_too= l *tool, return 0; } =20 + addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { pr_warning("problem processing %d event, skipping it.\n", event->header.type); - return -1; + ret =3D -1; + goto out; } =20 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) { ret =3D 0; - goto out_put; + goto out; } =20 switch (compute) { @@ -426,7 +428,7 @@ static int diff__process_sample_event(struct perf_tool = *tool, NULL, NULL, NULL, sample, true)) { pr_warning("problem incrementing symbol period, " "skipping event\n"); - goto out_put; + goto out; } =20 hist__account_cycles(sample->branch_stack, &al, sample, false, @@ -437,7 +439,7 @@ static int diff__process_sample_event(struct perf_tool = *tool, if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH, NULL)) { pr_debug("problem adding hist entry, skipping event\n"); - goto out_put; + goto out; } break; =20 @@ -446,7 +448,7 @@ static int diff__process_sample_event(struct perf_tool = *tool, true)) { pr_warning("problem incrementing symbol period, " "skipping event\n"); - goto out_put; + goto out; } } =20 @@ -460,8 +462,8 @@ static int diff__process_sample_event(struct perf_tool = *tool, if (!al.filtered) hists->stats.total_non_filtered_period +=3D sample->period; ret =3D 0; -out_put: - addr_location__put(&al); +out: + addr_location__exit(&al); return ret; } =20 diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index d9e96d4624c6..d19a1b862306 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -743,6 +743,7 @@ int perf_event__inject_buildid(struct perf_tool *tool, = union perf_event *event, struct addr_location al; struct thread *thread; =20 + addr_location__init(&al); thread =3D machine__findnew_thread(machine, sample->pid, sample->tid); if (thread =3D=3D NULL) { pr_err("problem processing %d event, skipping it.\n", @@ -763,6 +764,7 @@ int perf_event__inject_buildid(struct perf_tool *tool, = union perf_event *event, thread__put(thread); repipe: perf_event__repipe(tool, event, sample, machine); + addr_location__exit(&al); return 0; } =20 diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c index fe9439a4fd66..a11f280d20bd 100644 --- a/tools/perf/builtin-kmem.c +++ b/tools/perf/builtin-kmem.c @@ -399,7 +399,9 @@ static u64 find_callsite(struct evsel *evsel, struct pe= rf_sample *sample) struct addr_location al; struct machine *machine =3D &kmem_session->machines.host; struct callchain_cursor_node *node; + u64 result; =20 + addr_location__init(&al); if (alloc_func_list =3D=3D NULL) { if (build_alloc_func_list() < 0) goto out; @@ -427,16 +429,19 @@ static u64 find_callsite(struct evsel *evsel, struct = perf_sample *sample) else addr =3D node->ip; =20 - return addr; + result =3D addr; + goto out; } else pr_debug3("skipping alloc function: %s\n", caller->name); =20 callchain_cursor_advance(&callchain_cursor); } =20 -out: pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip); - return sample->ip; + result =3D sample->ip; +out: + addr_location__exit(&al); + return result; } =20 struct sort_dimension { diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c index a9395c52b23b..2d80aef4eccc 100644 --- a/tools/perf/builtin-kwork.c +++ b/tools/perf/builtin-kwork.c @@ -739,17 +739,22 @@ static int timehist_exit_event(struct perf_kwork *kwo= rk, struct kwork_atom *atom =3D NULL; struct kwork_work *work =3D NULL; struct addr_location al; + int ret =3D 0; =20 + addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { pr_debug("Problem processing event, skipping it\n"); - return -1; + ret =3D -1; + goto out; } =20 atom =3D work_pop_atom(kwork, class, KWORK_TRACE_EXIT, KWORK_TRACE_ENTRY, evsel, sample, machine, &work); - if (work =3D=3D NULL) - return -1; + if (work =3D=3D NULL) { + ret =3D -1; + goto out; + } =20 if (atom !=3D NULL) { work->nr_atoms++; @@ -757,7 +762,9 @@ static int timehist_exit_event(struct perf_kwork *kwork, atom_del(atom); } =20 - return 0; +out: + addr_location__exit(&al); + return ret; } =20 static struct kwork_class kwork_irq; diff --git a/tools/perf/builtin-mem.c b/tools/perf/builtin-mem.c index 960bfd4b732a..51499c20da01 100644 --- a/tools/perf/builtin-mem.c +++ b/tools/perf/builtin-mem.c @@ -199,9 +199,11 @@ dump_raw_samples(struct perf_tool *tool, char str[PAGE_SIZE_NAME_LEN]; struct dso *dso =3D NULL; =20 + addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { fprintf(stderr, "problem processing %d event, skipping it.\n", event->header.type); + addr_location__exit(&al); return -1; } =20 @@ -256,7 +258,7 @@ dump_raw_samples(struct perf_tool *tool, dso ? dso->long_name : "???", al.sym ? al.sym->name : "???"); out_put: - addr_location__put(&al); + addr_location__exit(&al); return 0; } =20 diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 8ea6ab18534a..0b091a8983a5 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -285,10 +285,12 @@ static int process_sample_event(struct perf_tool *too= l, if (evswitch__discard(&rep->evswitch, evsel)) return 0; =20 + addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { pr_debug("problem processing %d event, skipping it.\n", event->header.type); - return -1; + ret =3D -1; + goto out_put; } =20 if (rep->stitch_lbr) @@ -331,7 +333,7 @@ static int process_sample_event(struct perf_tool *tool, if (ret < 0) pr_debug("problem adding hist entry, skipping event\n"); out_put: - addr_location__put(&al); + addr_location__exit(&al); return ret; } =20 diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index fd37468c4f62..c75ad82a6729 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -2584,6 +2584,7 @@ static int timehist_sched_change_event(struct perf_to= ol *tool, int rc =3D 0; int state =3D evsel__intval(evsel, sample, "prev_state"); =20 + addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { pr_err("problem processing %d event. skipping it\n", event->header.type); @@ -2692,6 +2693,7 @@ static int timehist_sched_change_event(struct perf_to= ol *tool, =20 evsel__save_time(evsel, sample->time, sample->cpu); =20 + addr_location__exit(&al); return rc; } =20 diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index e756290de2ac..784d478c2e05 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -919,7 +919,6 @@ static int perf_sample__fprintf_brstack(struct perf_sam= ple *sample, { struct branch_stack *br =3D sample->branch_stack; struct branch_entry *entries =3D perf_sample__branch_entries(sample); - struct addr_location alf, alt; u64 i, from, to; int printed =3D 0; =20 @@ -930,20 +929,22 @@ static int perf_sample__fprintf_brstack(struct perf_s= ample *sample, from =3D entries[i].from; to =3D entries[i].to; =20 + printed +=3D fprintf(fp, " 0x%"PRIx64, from); if (PRINT_FIELD(DSO)) { - memset(&alf, 0, sizeof(alf)); - memset(&alt, 0, sizeof(alt)); + struct addr_location alf, alt; + + addr_location__init(&alf); + addr_location__init(&alt); thread__find_map_fb(thread, sample->cpumode, from, &alf); thread__find_map_fb(thread, sample->cpumode, to, &alt); - } =20 - printed +=3D fprintf(fp, " 0x%"PRIx64, from); - if (PRINT_FIELD(DSO)) printed +=3D map__fprintf_dsoname_dsoff(alf.map, PRINT_FIELD(DSOFF), al= f.addr, fp); - - printed +=3D fprintf(fp, "/0x%"PRIx64, to); - if (PRINT_FIELD(DSO)) + printed +=3D fprintf(fp, "/0x%"PRIx64, to); printed +=3D map__fprintf_dsoname_dsoff(alt.map, PRINT_FIELD(DSOFF), al= t.addr, fp); + addr_location__exit(&alt); + addr_location__exit(&alf); + } else + printed +=3D fprintf(fp, "/0x%"PRIx64, to); =20 printed +=3D print_bstack_flags(fp, entries + i); } @@ -957,7 +958,6 @@ static int perf_sample__fprintf_brstacksym(struct perf_= sample *sample, { struct branch_stack *br =3D sample->branch_stack; struct branch_entry *entries =3D perf_sample__branch_entries(sample); - struct addr_location alf, alt; u64 i, from, to; int printed =3D 0; =20 @@ -965,9 +965,10 @@ static int perf_sample__fprintf_brstacksym(struct perf= _sample *sample, return 0; =20 for (i =3D 0; i < br->nr; i++) { + struct addr_location alf, alt; =20 - memset(&alf, 0, sizeof(alf)); - memset(&alt, 0, sizeof(alt)); + addr_location__init(&alf); + addr_location__init(&alt); from =3D entries[i].from; to =3D entries[i].to; =20 @@ -982,6 +983,8 @@ static int perf_sample__fprintf_brstacksym(struct perf_= sample *sample, if (PRINT_FIELD(DSO)) printed +=3D map__fprintf_dsoname_dsoff(alt.map, PRINT_FIELD(DSOFF), al= t.addr, fp); printed +=3D print_bstack_flags(fp, entries + i); + addr_location__exit(&alt); + addr_location__exit(&alf); } =20 return printed; @@ -993,7 +996,6 @@ static int perf_sample__fprintf_brstackoff(struct perf_= sample *sample, { struct branch_stack *br =3D sample->branch_stack; struct branch_entry *entries =3D perf_sample__branch_entries(sample); - struct addr_location alf, alt; u64 i, from, to; int printed =3D 0; =20 @@ -1001,9 +1003,10 @@ static int perf_sample__fprintf_brstackoff(struct pe= rf_sample *sample, return 0; =20 for (i =3D 0; i < br->nr; i++) { + struct addr_location alf, alt; =20 - memset(&alf, 0, sizeof(alf)); - memset(&alt, 0, sizeof(alt)); + addr_location__init(&alf); + addr_location__init(&alt); from =3D entries[i].from; to =3D entries[i].to; =20 @@ -1022,6 +1025,8 @@ static int perf_sample__fprintf_brstackoff(struct per= f_sample *sample, if (PRINT_FIELD(DSO)) printed +=3D map__fprintf_dsoname_dsoff(alt.map, PRINT_FIELD(DSOFF), al= t.addr, fp); printed +=3D print_bstack_flags(fp, entries + i); + addr_location__exit(&alt); + addr_location__exit(&alf); } =20 return printed; @@ -1036,6 +1041,7 @@ static int grab_bb(u8 *buffer, u64 start, u64 end, struct addr_location al; bool kernel; struct dso *dso; + int ret =3D 0; =20 if (!start || !end) return 0; @@ -1057,7 +1063,6 @@ static int grab_bb(u8 *buffer, u64 start, u64 end, return -ENXIO; } =20 - memset(&al, 0, sizeof(al)); if (end - start > MAXBB - MAXINSN) { if (last) pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64= ")\n", start, end); @@ -1066,13 +1071,14 @@ static int grab_bb(u8 *buffer, u64 start, u64 end, return 0; } =20 + addr_location__init(&al); if (!thread__find_map(thread, *cpumode, start, &al) || (dso =3D map__dso(= al.map)) =3D=3D NULL) { pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end); - return 0; + goto out; } if (dso->data.status =3D=3D DSO_DATA_STATUS_ERROR) { pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end); - return 0; + goto out; } =20 /* Load maps to ensure dso->is_64_bit has been updated */ @@ -1086,7 +1092,10 @@ static int grab_bb(u8 *buffer, u64 start, u64 end, if (len <=3D 0) pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n", start, end); - return len; + ret =3D len; +out: + addr_location__exit(&al); + return ret; } =20 static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struc= t srccode_state *state) @@ -1137,14 +1146,16 @@ static int print_srccode(struct thread *thread, u8 = cpumode, uint64_t addr) struct addr_location al; int ret =3D 0; =20 - memset(&al, 0, sizeof(al)); + addr_location__init(&al); thread__find_map(thread, cpumode, addr, &al); if (!al.map) - return 0; + goto out; ret =3D map__fprintf_srccode(al.map, al.addr, stdout, thread__srccode_state(thread)); if (ret) ret +=3D printf("\n"); +out: + addr_location__exit(&al); return ret; } =20 @@ -1179,14 +1190,13 @@ static int ip__fprintf_sym(uint64_t addr, struct th= read *thread, struct perf_event_attr *attr, FILE *fp) { struct addr_location al; - int off, printed =3D 0; - - memset(&al, 0, sizeof(al)); + int off, printed =3D 0, ret =3D 0; =20 + addr_location__init(&al); thread__find_map(thread, cpumode, addr, &al); =20 if ((*lastsym) && al.addr >=3D (*lastsym)->start && al.addr < (*lastsym)-= >end) - return 0; + goto out; =20 al.cpu =3D cpu; al.sym =3D NULL; @@ -1194,7 +1204,7 @@ static int ip__fprintf_sym(uint64_t addr, struct thre= ad *thread, al.sym =3D map__find_symbol(al.map, al.addr); =20 if (!al.sym) - return 0; + goto out; =20 if (al.addr < al.sym->end) off =3D al.addr - al.sym->start; @@ -1209,7 +1219,10 @@ static int ip__fprintf_sym(uint64_t addr, struct thr= ead *thread, printed +=3D fprintf(fp, "\n"); *lastsym =3D al.sym; =20 - return printed; + ret =3D printed; +out: + addr_location__exit(&al); + return ret; } =20 static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample, @@ -1371,6 +1384,7 @@ static int perf_sample__fprintf_addr(struct perf_samp= le *sample, struct addr_location al; int printed =3D fprintf(fp, "%16" PRIx64, sample->addr); =20 + addr_location__init(&al); if (!sample_addr_correlates_sym(attr)) goto out; =20 @@ -1387,6 +1401,7 @@ static int perf_sample__fprintf_addr(struct perf_samp= le *sample, if (PRINT_FIELD(DSO)) printed +=3D map__fprintf_dsoname_dsoff(al.map, PRINT_FIELD(DSOFF), al.a= ddr, fp); out: + addr_location__exit(&al); return printed; } =20 @@ -2338,8 +2353,8 @@ static int process_sample_event(struct perf_tool *too= l, int ret =3D 0; =20 /* Set thread to NULL to indicate addr_al and al are not initialized */ - addr_al.thread =3D NULL; - al.thread =3D NULL; + addr_location__init(&al); + addr_location__init(&addr_al); =20 ret =3D dlfilter__filter_event_early(dlfilter, event, sample, evsel, mach= ine, &al, &addr_al); if (ret) { @@ -2405,8 +2420,8 @@ static int process_sample_event(struct perf_tool *too= l, } =20 out_put: - if (al.thread) - addr_location__put(&al); + addr_location__exit(&addr_al); + addr_location__exit(&al); return ret; } =20 diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c index 829d99fecfd0..19d4542ea18a 100644 --- a/tools/perf/builtin-timechart.c +++ b/tools/perf/builtin-timechart.c @@ -498,7 +498,6 @@ static const char *cat_backtrace(union perf_event *even= t, char *p =3D NULL; size_t p_len; u8 cpumode =3D PERF_RECORD_MISC_USER; - struct addr_location tal; struct ip_callchain *chain =3D sample->callchain; FILE *f =3D open_memstream(&p, &p_len); =20 @@ -507,6 +506,7 @@ static const char *cat_backtrace(union perf_event *even= t, return NULL; } =20 + addr_location__init(&al); if (!chain) goto exit; =20 @@ -518,6 +518,7 @@ static const char *cat_backtrace(union perf_event *even= t, =20 for (i =3D 0; i < chain->nr; i++) { u64 ip; + struct addr_location tal; =20 if (callchain_param.order =3D=3D ORDER_CALLEE) ip =3D chain->ips[i]; @@ -544,20 +545,22 @@ static const char *cat_backtrace(union perf_event *ev= ent, * Discard all. */ zfree(&p); - goto exit_put; + goto exit; } continue; } =20 + addr_location__init(&tal); tal.filtered =3D 0; if (thread__find_symbol(al.thread, cpumode, ip, &tal)) fprintf(f, "..... %016" PRIx64 " %s\n", ip, tal.sym->name); else fprintf(f, "..... %016" PRIx64 "\n", ip); + + addr_location__exit(&tal); } -exit_put: - addr_location__put(&al); exit: + addr_location__exit(&al); fclose(f); =20 return p; diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 9d3cbebb9b79..99010dfa5760 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -773,8 +773,9 @@ static void perf_event__process_sample(struct perf_tool= *tool, if (event->header.misc & PERF_RECORD_MISC_EXACT_IP) top->exact_samples++; =20 + addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) - return; + goto out; =20 if (top->stitch_lbr) thread__set_lbr_stitch_enable(al.thread, true); @@ -848,7 +849,8 @@ static void perf_event__process_sample(struct perf_tool= *tool, mutex_unlock(&hists->lock); } =20 - addr_location__put(&al); +out: + addr_location__exit(&al); } =20 static void diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 4c9bec39423b..6a1e75f06832 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -2418,13 +2418,15 @@ static int trace__resolve_callchain(struct trace *t= race, struct evsel *evsel, int max_stack =3D evsel->core.attr.sample_max_stack ? evsel->core.attr.sample_max_stack : trace->max_stack; - int err; + int err =3D -1; =20 + addr_location__init(&al); if (machine__resolve(trace->host, &al, sample) < 0) - return -1; + goto out; =20 err =3D thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL,= NULL, max_stack); - addr_location__put(&al); +out: + addr_location__exit(&al); return err; } =20 @@ -2893,6 +2895,7 @@ static int trace__pgfault(struct trace *trace, int err =3D -1; int callchain_ret =3D 0; =20 + addr_location__init(&al); thread =3D machine__findnew_thread(trace->host, sample->pid, sample->tid); =20 if (sample->callchain) { @@ -2953,6 +2956,7 @@ static int trace__pgfault(struct trace *trace, err =3D 0; out_put: thread__put(thread); + addr_location__exit(&al); return err; } =20 diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-readin= g.c index 9d8eefbebd48..2a7b2b6f5286 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -241,6 +241,7 @@ static int read_object_code(u64 addr, size_t len, u8 cp= umode, =20 pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr); =20 + addr_location__init(&al); 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"); @@ -366,7 +367,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); + addr_location__exit(&al); return err; } =20 diff --git a/tools/perf/tests/hists_cumulate.c b/tools/perf/tests/hists_cum= ulate.c index 62b9c6461ea6..71dacb0fec4d 100644 --- a/tools/perf/tests/hists_cumulate.c +++ b/tools/perf/tests/hists_cumulate.c @@ -8,8 +8,8 @@ #include "util/evsel.h" #include "util/evlist.h" #include "util/machine.h" -#include "util/thread.h" #include "util/parse-events.h" +#include "util/thread.h" #include "tests/tests.h" #include "tests/hists_common.h" #include @@ -84,6 +84,7 @@ static int add_hist_entries(struct hists *hists, struct m= achine *machine) struct perf_sample sample =3D { .period =3D 1000, }; size_t i; =20 + addr_location__init(&al); for (i =3D 0; i < ARRAY_SIZE(fake_samples); i++) { struct hist_entry_iter iter =3D { .evsel =3D evsel, @@ -107,20 +108,22 @@ static int add_hist_entries(struct hists *hists, stru= ct machine *machine) =20 if (hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack, NULL) < 0) { - addr_location__put(&al); goto out; } =20 - fake_samples[i].thread =3D al.thread; + thread__put(fake_samples[i].thread); + fake_samples[i].thread =3D thread__get(al.thread); map__put(fake_samples[i].map); - fake_samples[i].map =3D al.map; + fake_samples[i].map =3D map__get(al.map); fake_samples[i].sym =3D al.sym; } =20 + addr_location__exit(&al); return TEST_OK; =20 out: pr_debug("Not enough memory for adding a hist entry\n"); + addr_location__exit(&al); return TEST_FAIL; } =20 @@ -152,8 +155,10 @@ static void put_fake_samples(void) { size_t i; =20 - for (i =3D 0; i < ARRAY_SIZE(fake_samples); i++) - map__put(fake_samples[i].map); + for (i =3D 0; i < ARRAY_SIZE(fake_samples); i++) { + map__zput(fake_samples[i].map); + thread__zput(fake_samples[i].thread); + } } =20 typedef int (*test_fn_t)(struct evsel *, struct machine *); diff --git a/tools/perf/tests/hists_filter.c b/tools/perf/tests/hists_filte= r.c index 98eff5935a1c..4b2e4f2fbe48 100644 --- a/tools/perf/tests/hists_filter.c +++ b/tools/perf/tests/hists_filter.c @@ -8,6 +8,7 @@ #include "util/evlist.h" #include "util/machine.h" #include "util/parse-events.h" +#include "util/thread.h" #include "tests/tests.h" #include "tests/hists_common.h" #include @@ -53,6 +54,7 @@ static int add_hist_entries(struct evlist *evlist, struct perf_sample sample =3D { .period =3D 100, }; size_t i; =20 + addr_location__init(&al); /* * each evsel will have 10 samples but the 4th sample * (perf [perf] main) will be collapsed to an existing entry @@ -84,21 +86,22 @@ static int add_hist_entries(struct evlist *evlist, al.socket =3D fake_samples[i].socket; if (hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack, NULL) < 0) { - addr_location__put(&al); goto out; } =20 - fake_samples[i].thread =3D al.thread; + thread__put(fake_samples[i].thread); + fake_samples[i].thread =3D thread__get(al.thread); map__put(fake_samples[i].map); - fake_samples[i].map =3D al.map; + fake_samples[i].map =3D map__get(al.map); fake_samples[i].sym =3D al.sym; } } - + addr_location__exit(&al); return 0; =20 out: pr_debug("Not enough memory for adding a hist entry\n"); + addr_location__exit(&al); return TEST_FAIL; } =20 diff --git a/tools/perf/tests/hists_link.c b/tools/perf/tests/hists_link.c index 141e2972e34f..12bad8840699 100644 --- a/tools/perf/tests/hists_link.c +++ b/tools/perf/tests/hists_link.c @@ -8,6 +8,7 @@ #include "machine.h" #include "map.h" #include "parse-events.h" +#include "thread.h" #include "hists_common.h" #include "util/mmap.h" #include @@ -70,6 +71,7 @@ static int add_hist_entries(struct evlist *evlist, struct= machine *machine) struct perf_sample sample =3D { .period =3D 1, .weight =3D 1, }; size_t i =3D 0, k; =20 + addr_location__init(&al); /* * each evsel will have 10 samples - 5 common and 5 distinct. * However the second evsel also has a collapsed entry for @@ -90,13 +92,13 @@ static int add_hist_entries(struct evlist *evlist, stru= ct machine *machine) he =3D hists__add_entry(hists, &al, NULL, NULL, NULL, NULL, &sample, true); if (he =3D=3D NULL) { - addr_location__put(&al); goto out; } =20 - fake_common_samples[k].thread =3D al.thread; + thread__put(fake_common_samples[k].thread); + fake_common_samples[k].thread =3D thread__get(al.thread); map__put(fake_common_samples[k].map); - fake_common_samples[k].map =3D al.map; + fake_common_samples[k].map =3D map__get(al.map); fake_common_samples[k].sym =3D al.sym; } =20 @@ -110,20 +112,22 @@ static int add_hist_entries(struct evlist *evlist, st= ruct machine *machine) he =3D hists__add_entry(hists, &al, NULL, NULL, NULL, NULL, &sample, true); if (he =3D=3D NULL) { - addr_location__put(&al); goto out; } =20 - fake_samples[i][k].thread =3D al.thread; - fake_samples[i][k].map =3D al.map; + thread__put(fake_samples[i][k].thread); + fake_samples[i][k].thread =3D thread__get(al.thread); + map__put(fake_samples[i][k].map); + fake_samples[i][k].map =3D map__get(al.map); fake_samples[i][k].sym =3D al.sym; } i++; } =20 + addr_location__exit(&al); return 0; - out: + addr_location__exit(&al); pr_debug("Not enough memory for adding a hist entry\n"); return -1; } diff --git a/tools/perf/tests/hists_output.c b/tools/perf/tests/hists_outpu= t.c index cd2094c13e1e..ba1cccf57049 100644 --- a/tools/perf/tests/hists_output.c +++ b/tools/perf/tests/hists_output.c @@ -54,6 +54,7 @@ static int add_hist_entries(struct hists *hists, struct m= achine *machine) struct perf_sample sample =3D { .period =3D 100, }; size_t i; =20 + addr_location__init(&al); for (i =3D 0; i < ARRAY_SIZE(fake_samples); i++) { struct hist_entry_iter iter =3D { .evsel =3D evsel, @@ -73,20 +74,21 @@ static int add_hist_entries(struct hists *hists, struct= machine *machine) =20 if (hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack, NULL) < 0) { - addr_location__put(&al); goto out; } =20 fake_samples[i].thread =3D al.thread; map__put(fake_samples[i].map); - fake_samples[i].map =3D al.map; + fake_samples[i].map =3D map__get(al.map); fake_samples[i].sym =3D al.sym; } =20 + addr_location__exit(&al); return TEST_OK; =20 out: pr_debug("Not enough memory for adding a hist entry\n"); + addr_location__exit(&al); return TEST_FAIL; } =20 @@ -118,8 +120,10 @@ static void put_fake_samples(void) { size_t i; =20 - for (i =3D 0; i < ARRAY_SIZE(fake_samples); i++) + for (i =3D 0; i < ARRAY_SIZE(fake_samples); i++) { map__put(fake_samples[i].map); + fake_samples[i].map =3D NULL; + } } =20 typedef int (*test_fn_t)(struct evsel *, struct machine *); diff --git a/tools/perf/tests/mmap-thread-lookup.c b/tools/perf/tests/mmap-= thread-lookup.c index 898eda55b7a8..3891a2a3b46f 100644 --- a/tools/perf/tests/mmap-thread-lookup.c +++ b/tools/perf/tests/mmap-thread-lookup.c @@ -187,6 +187,7 @@ static int mmap_events(synth_cb synth) struct addr_location al; struct thread *thread; =20 + addr_location__init(&al); thread =3D machine__findnew_thread(machine, getpid(), td->tid); =20 pr_debug("looking for map %p\n", td->map); @@ -199,11 +200,12 @@ static int mmap_events(synth_cb synth) if (!al.map) { pr_debug("failed, couldn't find map\n"); err =3D -1; + addr_location__exit(&al); break; } =20 pr_debug("map %p, addr %" PRIx64 "\n", al.map, map__start(al.map)); - map__put(al.map); + addr_location__exit(&al); } =20 machine__delete_threads(machine); diff --git a/tools/perf/util/addr_location.c b/tools/perf/util/addr_locatio= n.c index c73fc2aa236c..51825ef8c0ab 100644 --- a/tools/perf/util/addr_location.c +++ b/tools/perf/util/addr_location.c @@ -1,16 +1,44 @@ // SPDX-License-Identifier: GPL-2.0 #include "addr_location.h" #include "map.h" +#include "maps.h" #include "thread.h" =20 +void addr_location__init(struct addr_location *al) +{ + al->thread =3D NULL; + al->maps =3D NULL; + al->map =3D NULL; + al->sym =3D NULL; + al->srcline =3D NULL; + al->addr =3D 0; + al->level =3D 0; + al->filtered =3D 0; + al->cpumode =3D 0; + al->cpu =3D 0; + al->socket =3D 0; +} + /* * The preprocess_sample method will return with reference counts for the * in it, when done using (and perhaps getting ref counts if needing to * keep a pointer to one of those entries) it must be paired with * addr_location__put(), so that the refcounts can be decremented. */ -void addr_location__put(struct addr_location *al) +void addr_location__exit(struct addr_location *al) { map__zput(al->map); thread__zput(al->thread); + maps__zput(al->maps); +} + +void addr_location__copy(struct addr_location *dst, struct addr_location *= src) +{ + thread__put(dst->thread); + maps__put(dst->maps); + map__put(dst->map); + *dst =3D *src; + dst->thread =3D thread__get(src->thread); + dst->maps =3D maps__get(src->maps); + dst->map =3D map__get(src->map); } diff --git a/tools/perf/util/addr_location.h b/tools/perf/util/addr_locatio= n.h index 7dfa7417c0fe..d8ac0428dff2 100644 --- a/tools/perf/util/addr_location.h +++ b/tools/perf/util/addr_location.h @@ -23,6 +23,9 @@ struct addr_location { s32 socket; }; =20 -void addr_location__put(struct addr_location *al); +void addr_location__init(struct addr_location *al); +void addr_location__exit(struct addr_location *al); + +void addr_location__copy(struct addr_location *dst, struct addr_location *= src); =20 #endif /* __PERF_ADDR_LOCATION */ diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index 06a8cd88cbef..36728222a5b4 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -58,9 +58,11 @@ int build_id__mark_dso_hit(struct perf_tool *tool __mayb= e_unused, return -1; } =20 + addr_location__init(&al); if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) map__dso(al.map)->hit =3D 1; =20 + addr_location__exit(&al); thread__put(thread); return 0; } diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c index b550c7393155..416f2ddc3895 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -910,33 +910,35 @@ static u32 cs_etm__mem_access(struct cs_etm_queue *et= mq, u8 trace_chan_id, struct addr_location al; struct dso *dso; struct cs_etm_traceid_queue *tidq; + int ret =3D 0; =20 if (!etmq) return 0; =20 + addr_location__init(&al); machine =3D etmq->etm->machine; cpumode =3D cs_etm__cpu_mode(etmq, address); tidq =3D cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id); if (!tidq) - return 0; + goto out; =20 thread =3D tidq->thread; if (!thread) { if (cpumode !=3D PERF_RECORD_MISC_KERNEL) - return 0; + goto out; thread =3D etmq->etm->unknown_thread; } =20 if (!thread__find_map(thread, cpumode, address, &al)) - return 0; + goto out; =20 dso =3D map__dso(al.map); if (!dso) - return 0; + goto out; =20 if (dso->data.status =3D=3D DSO_DATA_STATUS_ERROR && dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE)) - return 0; + goto out; =20 offset =3D map__map_ip(al.map, address); =20 @@ -953,10 +955,12 @@ static u32 cs_etm__mem_access(struct cs_etm_queue *et= mq, u8 trace_chan_id, dso->long_name ? dso->long_name : "Unknown"); dso->auxtrace_warned =3D true; } - return 0; + goto out; } - - return len; + ret =3D len; +out: + addr_location__exit(&al); + return ret; } =20 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *et= m, diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-con= vert-json.c index 291591e303cd..5bb3c2ba95ca 100644 --- a/tools/perf/util/data-convert-json.c +++ b/tools/perf/util/data-convert-json.c @@ -154,12 +154,14 @@ static int process_sample_event(struct perf_tool *too= l, { struct convert_json *c =3D container_of(tool, struct convert_json, tool); FILE *out =3D c->out; - struct addr_location al, tal; + struct addr_location al; u64 sample_type =3D __evlist__combined_sample_type(evsel->evlist); u8 cpumode =3D PERF_RECORD_MISC_USER; =20 + addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { pr_err("Sample resolution failed!\n"); + addr_location__exit(&al); return -1; } =20 @@ -190,6 +192,7 @@ static int process_sample_event(struct perf_tool *tool, =20 for (i =3D 0; i < sample->callchain->nr; ++i) { u64 ip =3D sample->callchain->ips[i]; + struct addr_location tal; =20 if (ip >=3D PERF_CONTEXT_MAX) { switch (ip) { @@ -215,8 +218,10 @@ static int process_sample_event(struct perf_tool *tool, else fputc(',', out); =20 + addr_location__init(&tal); ok =3D thread__find_symbol(al.thread, cpumode, ip, &tal); output_sample_callchain_entry(tool, ip, ok ? &tal : NULL); + addr_location__exit(&tal); } } else { output_sample_callchain_entry(tool, sample->ip, &al); @@ -245,6 +250,7 @@ static int process_sample_event(struct perf_tool *tool, } #endif output_json_format(out, false, 2, "}"); + addr_location__exit(&al); return 0; } =20 diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c index 751fd53bfd93..6184696dc266 100644 --- a/tools/perf/util/db-export.c +++ b/tools/perf/util/db-export.c @@ -239,16 +239,17 @@ static struct call_path *call_path_from_sample(struct= db_export *dbe, struct addr_location al; u64 dso_db_id =3D 0, sym_db_id =3D 0, offset =3D 0; =20 - memset(&al, 0, sizeof(al)); =20 node =3D callchain_cursor_current(&callchain_cursor); if (!node) break; + /* * Handle export of symbol and dso for this node by * constructing an addr_location struct and then passing it to * db_ids_from_al() to perform the export. */ + addr_location__init(&al); al.sym =3D node->ms.sym; al.map =3D node->ms.map; al.maps =3D thread__maps(thread); @@ -265,6 +266,7 @@ static struct call_path *call_path_from_sample(struct d= b_export *dbe, kernel_start); =20 callchain_cursor_advance(&callchain_cursor); + addr_location__exit(&al); } =20 /* Reset the callchain order to its prior value. */ diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c index 8016f21dc0b8..46f74b2344db 100644 --- a/tools/perf/util/dlfilter.c +++ b/tools/perf/util/dlfilter.c @@ -258,6 +258,7 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip,= void *buf, __u32 len) struct addr_location a; struct map *map; u64 offset; + __s32 ret; =20 if (!d->ctx_valid) return -1; @@ -272,16 +273,22 @@ static __s32 dlfilter__object_code(void *ctx, __u64 i= p, void *buf, __u32 len) machine__kernel_ip(d->machine, ip) =3D=3D machine__kernel_ip(d->machi= ne, d->sample->ip)) goto have_map; =20 + addr_location__init(&a); thread__find_map_fb(al->thread, d->sample->cpumode, ip, &a); - if (!a.map) - return -1; + if (!a.map) { + ret =3D -1; + goto out; + } =20 map =3D a.map; have_map: 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); + ret =3D dso__data_read_offset(map__dso(map), d->machine, offset, buf, len= ); +out: + addr_location__exit(&a); + return ret; } =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 6ee23145ee7e..2fcfba38fc48 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -486,6 +486,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *= event, struct machine *ma if (machine) { struct addr_location al; =20 + addr_location__init(&al); 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); @@ -493,7 +494,7 @@ size_t perf_event__fprintf_text_poke(union perf_event *= event, struct machine *ma if (al.sym) ret +=3D symbol__fprintf_symname_offs(al.sym, &al, fp); } - map__put(al.map); + addr_location__exit(&al); } ret +=3D fprintf(fp, " old len %u new len %u\n", tp->old_len, tp->new_len= ); old =3D true; @@ -577,8 +578,10 @@ struct map *thread__find_map(struct thread *thread, u8= cpumode, u64 addr, struct machine *machine =3D maps__machine(maps); bool load_map =3D false; =20 - al->maps =3D maps; - al->thread =3D thread; + maps__zput(al->maps); + map__zput(al->map); + thread__zput(al->thread); + al->addr =3D addr; al->cpumode =3D cpumode; al->filtered =3D 0; @@ -590,13 +593,13 @@ struct map *thread__find_map(struct thread *thread, u= 8 cpumode, u64 addr, =20 if (cpumode =3D=3D PERF_RECORD_MISC_KERNEL && perf_host) { al->level =3D 'k'; - al->maps =3D maps =3D machine__kernel_maps(machine); + maps =3D machine__kernel_maps(machine); load_map =3D true; } else if (cpumode =3D=3D PERF_RECORD_MISC_USER && perf_host) { al->level =3D '.'; } else if (cpumode =3D=3D PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { al->level =3D 'g'; - al->maps =3D maps =3D machine__kernel_maps(machine); + maps =3D machine__kernel_maps(machine); load_map =3D true; } else if (cpumode =3D=3D PERF_RECORD_MISC_GUEST_USER && perf_guest) { al->level =3D 'u'; @@ -615,7 +618,8 @@ struct map *thread__find_map(struct thread *thread, u8 = cpumode, u64 addr, =20 return NULL; } - + al->maps =3D maps__get(maps); + al->thread =3D thread__get(thread); al->map =3D map__get(maps__find(maps, al->addr)); if (al->map !=3D NULL) { /* diff --git a/tools/perf/util/evsel_fprintf.c b/tools/perf/util/evsel_fprint= f.c index a1655fd7ed9b..cf45ca0e768f 100644 --- a/tools/perf/util/evsel_fprintf.c +++ b/tools/perf/util/evsel_fprintf.c @@ -128,8 +128,6 @@ int sample__fprintf_callchain(struct perf_sample *sampl= e, int left_alignment, bool first =3D true; =20 if (sample->callchain) { - struct addr_location node_al; - callchain_cursor_commit(cursor); =20 while (1) { @@ -159,9 +157,12 @@ int sample__fprintf_callchain(struct perf_sample *samp= le, int left_alignment, printed +=3D fprintf(fp, "%c%16" PRIx64, s, node->ip); =20 if (print_sym) { + struct addr_location node_al; + + addr_location__init(&node_al); printed +=3D fprintf(fp, " "); node_al.addr =3D addr; - node_al.map =3D map; + node_al.map =3D map__get(map); =20 if (print_symoffset) { printed +=3D __symbol__fprintf_symname_offs(sym, &node_al, @@ -171,6 +172,7 @@ int sample__fprintf_callchain(struct perf_sample *sampl= e, int left_alignment, printed +=3D __symbol__fprintf_symname(sym, &node_al, print_unknown_as_addr, fp); } + addr_location__exit(&node_al); } =20 if (print_dso && (!sym || !sym->inlined)) diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 4bc3affbe891..a4c1b617f6e4 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -588,7 +588,7 @@ static void hist_entry__add_callchain_period(struct his= t_entry *he, u64 period) =20 static struct hist_entry *hists__findnew_entry(struct hists *hists, struct hist_entry *entry, - struct addr_location *al, + const struct addr_location *al, bool sample_self) { struct rb_node **p; @@ -927,8 +927,10 @@ iter_next_branch_entry(struct hist_entry_iter *iter, s= truct addr_location *al) if (iter->curr >=3D iter->total) return 0; =20 - al->maps =3D bi[i].to.ms.maps; - al->map =3D bi[i].to.ms.map; + maps__put(al->maps); + al->maps =3D maps__get(bi[i].to.ms.maps); + map__put(al->map); + al->map =3D map__get(bi[i].to.ms.map); al->sym =3D bi[i].to.ms.sym; al->addr =3D bi[i].to.addr; return 1; diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c index 45c7e7722916..783ce61c6d25 100644 --- a/tools/perf/util/intel-pt.c +++ b/tools/perf/util/intel-pt.c @@ -754,13 +754,15 @@ static int intel_pt_walk_next_insn(struct intel_pt_in= sn *intel_pt_insn, struct addr_location al; unsigned char buf[INTEL_PT_INSN_BUF_SZ]; ssize_t len; - int x86_64; + int x86_64, ret =3D 0; u8 cpumode; u64 offset, start_offset, start_ip; u64 insn_cnt =3D 0; bool one_map =3D true; bool nr; =20 + + addr_location__init(&al); intel_pt_insn->length =3D 0; =20 if (to_ip && *ip =3D=3D to_ip) @@ -773,19 +775,22 @@ static int intel_pt_walk_next_insn(struct intel_pt_in= sn *intel_pt_insn, if (ptq->pt->have_guest_sideband) { if (!ptq->guest_machine || ptq->guest_machine_pid !=3D ptq->pid) { intel_pt_log("ERROR: guest sideband but no guest machine\n"); - return -EINVAL; + ret =3D -EINVAL; + goto out_ret; } } else if ((!symbol_conf.guest_code && cpumode !=3D PERF_RECORD_MISC_GUE= ST_KERNEL) || intel_pt_get_guest(ptq)) { intel_pt_log("ERROR: no guest machine\n"); - return -EINVAL; + ret =3D -EINVAL; + goto out_ret; } machine =3D ptq->guest_machine; thread =3D ptq->guest_thread; if (!thread) { if (cpumode !=3D PERF_RECORD_MISC_GUEST_KERNEL) { intel_pt_log("ERROR: no guest thread\n"); - return -EINVAL; + ret =3D -EINVAL; + goto out_ret; } thread =3D ptq->unknown_guest_thread; } @@ -794,7 +799,8 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn= *intel_pt_insn, if (!thread) { if (cpumode !=3D PERF_RECORD_MISC_KERNEL) { intel_pt_log("ERROR: no thread\n"); - return -EINVAL; + ret =3D -EINVAL; + goto out_ret; } thread =3D ptq->pt->unknown_thread; } @@ -808,13 +814,17 @@ static int intel_pt_walk_next_insn(struct intel_pt_in= sn *intel_pt_insn, 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; + addr_location__exit(&al); + ret =3D -EINVAL; + goto out_ret; } dso =3D map__dso(al.map); =20 if (dso->data.status =3D=3D DSO_DATA_STATUS_ERROR && - dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE)) - return -ENOENT; + dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE)) { + ret =3D -ENOENT; + goto out_ret; + } =20 offset =3D map__map_ip(al.map, *ip); =20 @@ -833,7 +843,8 @@ static int intel_pt_walk_next_insn(struct intel_pt_insn= *intel_pt_insn, intel_pt_insn->rel =3D e->rel; memcpy(intel_pt_insn->buf, e->insn, INTEL_PT_INSN_BUF_SZ); intel_pt_log_insn_no_data(intel_pt_insn, *ip); - return 0; + ret =3D 0; + goto out_ret; } } =20 @@ -854,11 +865,14 @@ static int intel_pt_walk_next_insn(struct intel_pt_in= sn *intel_pt_insn, offset); if (intel_pt_enable_logging) dso__fprintf(dso, intel_pt_log_fp()); - return -EINVAL; + ret =3D -EINVAL; + goto out_ret; } =20 - if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn)) - return -EINVAL; + if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn)) { + ret =3D -EINVAL; + goto out_ret; + } =20 intel_pt_log_insn(intel_pt_insn, *ip); =20 @@ -909,17 +923,20 @@ static int intel_pt_walk_next_insn(struct intel_pt_in= sn *intel_pt_insn, =20 e =3D intel_pt_cache_lookup(map__dso(al.map), machine, start_offset); if (e) - return 0; + goto out_ret; } =20 /* Ignore cache errors */ intel_pt_cache_add(map__dso(al.map), machine, start_offset, insn_cnt, *ip - start_ip, intel_pt_insn); =20 - return 0; +out_ret: + addr_location__exit(&al); + return ret; =20 out_no_cache: *insn_cnt_ptr =3D insn_cnt; + addr_location__exit(&al); return 0; } =20 @@ -968,6 +985,7 @@ static int __intel_pt_pgd_ip(uint64_t ip, void *data) struct addr_location al; u8 cpumode; u64 offset; + int res; =20 if (ptq->state->to_nr) { if (intel_pt_guest_kernel_ip(ip)) @@ -984,12 +1002,15 @@ static int __intel_pt_pgd_ip(uint64_t ip, void *data) if (!thread) return -EINVAL; =20 + addr_location__init(&al); if (!thread__find_map(thread, cpumode, ip, &al) || !map__dso(al.map)) return -EINVAL; =20 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); + res =3D intel_pt_match_pgd_ip(ptq->pt, ip, offset, map__dso(al.map)->long= _name); + addr_location__exit(&al); + return res; } =20 static bool intel_pt_pgd_ip(uint64_t ip, void *data) @@ -3372,20 +3393,22 @@ static int intel_pt_text_poke(struct intel_pt *pt, = union perf_event *event) /* Assume text poke begins in a basic block no more than 4096 bytes */ int cnt =3D 4096 + event->text_poke.new_len; struct thread *thread =3D pt->unknown_thread; - struct addr_location al =3D { .map =3D NULL }; + struct addr_location al; struct machine *machine =3D pt->machine; struct intel_pt_cache_entry *e; u64 offset; + int ret =3D 0; =20 + addr_location__init(&al); if (!event->text_poke.new_len) - return 0; + goto out; =20 for (; cnt; cnt--, addr--) { struct dso *dso; =20 if (intel_pt_find_map(thread, cpumode, addr, &al)) { if (addr < event->text_poke.addr) - return 0; + goto out; continue; } =20 @@ -3406,15 +3429,16 @@ static int intel_pt_text_poke(struct intel_pt *pt, = union perf_event *event) * branch instruction before the text poke address. */ if (e->branch !=3D INTEL_PT_BR_NO_BRANCH) - return 0; + goto out; } else { intel_pt_cache_invalidate(dso, machine, offset); intel_pt_log("Invalidated instruction cache for %s at %#"PRIx64"\n", dso->long_name, addr); } } - - return 0; +out: + addr_location__exit(&al); + return ret; } =20 static int intel_pt_process_event(struct perf_session *session, diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 8972c852d3bd..9fcf357a4d53 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -2221,7 +2221,7 @@ static void ip__resolve_ams(struct thread *thread, { struct addr_location al; =20 - memset(&al, 0, sizeof(al)); + addr_location__init(&al); /* * We cannot use the header.misc hint to determine whether a * branch stack address is user, kernel, guest, hypervisor. @@ -2234,11 +2234,12 @@ static void ip__resolve_ams(struct thread *thread, ams->addr =3D ip; ams->al_addr =3D al.addr; ams->al_level =3D al.level; - ams->ms.maps =3D al.maps; + ams->ms.maps =3D maps__get(al.maps); ams->ms.sym =3D al.sym; - ams->ms.map =3D al.map; + ams->ms.map =3D map__get(al.map); ams->phys_addr =3D 0; ams->data_page_size =3D 0; + addr_location__exit(&al); } =20 static void ip__resolve_data(struct thread *thread, @@ -2247,18 +2248,19 @@ static void ip__resolve_data(struct thread *thread, { struct addr_location al; =20 - memset(&al, 0, sizeof(al)); + addr_location__init(&al); =20 thread__find_symbol(thread, m, addr, &al); =20 ams->addr =3D addr; ams->al_addr =3D al.addr; ams->al_level =3D al.level; - ams->ms.maps =3D al.maps; + ams->ms.maps =3D maps__get(al.maps); ams->ms.sym =3D al.sym; - ams->ms.map =3D al.map; + ams->ms.map =3D map__get(al.map); ams->phys_addr =3D phys_addr; ams->data_page_size =3D daddr_page_size; + addr_location__exit(&al); } =20 struct mem_info *sample__resolve_mem(struct perf_sample *sample, @@ -2319,10 +2321,11 @@ static int add_callchain_ip(struct thread *thread, { struct map_symbol ms; struct addr_location al; - int nr_loop_iter =3D 0, err; + int nr_loop_iter =3D 0, err =3D 0; u64 iter_cycles =3D 0; const char *srcline =3D NULL; =20 + addr_location__init(&al); al.filtered =3D 0; al.sym =3D NULL; al.srcline =3D NULL; @@ -2348,9 +2351,10 @@ static int add_callchain_ip(struct thread *thread, * Discard all. */ callchain_cursor_reset(cursor); - return 1; + err =3D 1; + goto out; } - return 0; + goto out; } thread__find_symbol(thread, *cpumode, ip, &al); } @@ -2363,31 +2367,32 @@ static int add_callchain_ip(struct thread *thread, symbol__match_regex(al.sym, &ignore_callees_regex)) { /* Treat this symbol as the root, forgetting its callees. */ - *root_al =3D al; + addr_location__copy(root_al, &al); callchain_cursor_reset(cursor); } } =20 if (symbol_conf.hide_unresolved && al.sym =3D=3D NULL) - return 0; + goto out; =20 if (iter) { nr_loop_iter =3D iter->nr_loop_iter; iter_cycles =3D iter->cycles; } =20 - ms.maps =3D al.maps; - ms.map =3D al.map; + ms.maps =3D maps__get(al.maps); + ms.map =3D map__get(al.map); ms.sym =3D al.sym; =20 if (!branch && append_inlines(cursor, &ms, ip) =3D=3D 0) - return 0; + goto out; =20 srcline =3D callchain_srcline(&ms, al.addr); err =3D callchain_cursor_append(cursor, ip, &ms, branch, flags, nr_loop_iter, iter_cycles, branch_from, srcline); - map__put(al.map); +out: + addr_location__exit(&al); return err; } =20 diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index f3d262e871ac..d7c99028c6e6 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -469,9 +469,11 @@ static PyObject *python_process_callchain(struct perf_= sample *sample, struct addr_location node_al; unsigned long offset; =20 + addr_location__init(&node_al); node_al.addr =3D map__map_ip(map, node->ip); - node_al.map =3D map; + node_al.map =3D map__get(map); offset =3D get_offset(node->ms.sym, &node_al); + addr_location__exit(&node_al); =20 pydict_set_item_string_decref( pyelem, "sym_off", @@ -539,6 +541,7 @@ static PyObject *python_process_brstack(struct perf_sam= ple *sample, pydict_set_item_string_decref(pyelem, "cycles", PyLong_FromUnsignedLongLong(entries[i].flags.cycles)); =20 + addr_location__init(&al); thread__find_map_fb(thread, sample->cpumode, entries[i].from, &al); dsoname =3D get_dsoname(al.map); @@ -551,6 +554,7 @@ static PyObject *python_process_brstack(struct perf_sam= ple *sample, pydict_set_item_string_decref(pyelem, "to_dsoname", _PyUnicode_FromString(dsoname)); =20 + addr_location__exit(&al); PyList_Append(pylist, pyelem); Py_DECREF(pyelem); } @@ -594,7 +598,6 @@ static PyObject *python_process_brstacksym(struct perf_= sample *sample, PyObject *pylist; u64 i; char bf[512]; - struct addr_location al; =20 pylist =3D PyList_New(0); if (!pylist) @@ -605,7 +608,9 @@ static PyObject *python_process_brstacksym(struct perf_= sample *sample, =20 for (i =3D 0; i < br->nr; i++) { PyObject *pyelem; + struct addr_location al; =20 + addr_location__init(&al); pyelem =3D PyDict_New(); if (!pyelem) Py_FatalError("couldn't create Python dictionary"); @@ -644,6 +649,7 @@ static PyObject *python_process_brstacksym(struct perf_= sample *sample, =20 PyList_Append(pylist, pyelem); Py_DECREF(pyelem); + addr_location__exit(&al); } =20 exit: diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 9a1db3be6436..bee4ac1051ee 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -432,18 +432,25 @@ int thread__memcpy(struct thread *thread, struct mach= ine *machine, if (machine__kernel_ip(machine, ip)) cpumode =3D PERF_RECORD_MISC_KERNEL; =20 - if (!thread__find_map(thread, cpumode, ip, &al)) - return -1; + addr_location__init(&al); + if (!thread__find_map(thread, cpumode, ip, &al)) { + addr_location__exit(&al); + return -1; + } =20 dso =3D map__dso(al.map); =20 - if( !dso || dso->data.status =3D=3D DSO_DATA_STATUS_ERROR || map__load(al= .map) < 0) + if (!dso || dso->data.status =3D=3D DSO_DATA_STATUS_ERROR || map__load(al= .map) < 0) { + addr_location__exit(&al); return -1; + } =20 offset =3D map__map_ip(al.map, ip); if (is64bit) *is64bit =3D dso->is_64_bit; =20 + addr_location__exit(&al); + return dso__data_read_offset(dso, machine, offset, buf, len); } =20 diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c index 3723b5e31b2a..83eea968482e 100644 --- a/tools/perf/util/unwind-libdw.c +++ b/tools/perf/util/unwind-libdw.c @@ -90,8 +90,12 @@ static int __report_module(struct addr_location *al, u64= ip, static int report_module(u64 ip, struct unwind_info *ui) { struct addr_location al; + int res; =20 - return __report_module(&al, ip, ui); + addr_location__init(&al); + res =3D __report_module(&al, ip, ui); + addr_location__exit(&al); + return res; } =20 /* @@ -104,8 +108,11 @@ static int entry(u64 ip, struct unwind_info *ui) struct unwind_entry *e =3D &ui->entries[ui->idx++]; struct addr_location al; =20 - if (__report_module(&al, ip, ui)) + addr_location__init(&al); + if (__report_module(&al, ip, ui)) { + addr_location__exit(&al); return -1; + } =20 e->ip =3D ip; e->ms.maps =3D al.maps; @@ -116,6 +123,7 @@ static int entry(u64 ip, struct unwind_info *ui) al.sym ? al.sym->name : "''", ip, al.map ? map__map_ip(al.map, ip) : (u64) 0); + addr_location__exit(&al); return 0; } =20 @@ -136,17 +144,22 @@ static int access_dso_mem(struct unwind_info *ui, Dwa= rf_Addr addr, ssize_t size; struct dso *dso; =20 + addr_location__init(&al); 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; + goto out_fail; } dso =3D map__dso(al.map); if (!dso) - return -1; + goto out_fail; =20 size =3D dso__data_read_addr(dso, al.map, ui->machine, addr, (u8 *) data,= sizeof(*data)); =20 + addr_location__exit(&al); return !(size =3D=3D sizeof(*data)); +out_fail: + addr_location__exit(&al); + return -1; } =20 static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_= Word *result, diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unw= ind-libunwind-local.c index 11f3fc95aa11..36bf5100bad2 100644 --- a/tools/perf/util/unwind-libunwind-local.c +++ b/tools/perf/util/unwind-libunwind-local.c @@ -416,7 +416,12 @@ static int read_unwind_spec_debug_frame(struct dso *ds= o, static struct map *find_map(unw_word_t ip, struct unwind_info *ui) { struct addr_location al; - return thread__find_map(ui->thread, PERF_RECORD_MISC_USER, ip, &al); + struct map *ret; + + addr_location__init(&al); + ret =3D thread__find_map(ui->thread, PERF_RECORD_MISC_USER, ip, &al); + addr_location__exit(&al); + return ret; } =20 static int @@ -631,7 +636,9 @@ static int entry(u64 ip, struct thread *thread, { struct unwind_entry e; struct addr_location al; + int ret; =20 + addr_location__init(&al); e.ms.sym =3D thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al); e.ip =3D ip; e.ms.map =3D al.map; @@ -642,7 +649,9 @@ static int entry(u64 ip, struct thread *thread, ip, al.map ? map__map_ip(al.map, ip) : (u64) 0); =20 - return cb(&e, arg); + ret =3D cb(&e, arg); + addr_location__exit(&al); + return ret; } =20 static void display_error(int err) --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 049FDC7EE23 for ; Thu, 8 Jun 2023 23:30:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237457AbjFHXaU (ORCPT ); Thu, 8 Jun 2023 19:30:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237180AbjFHX3e (ORCPT ); Thu, 8 Jun 2023 19:29:34 -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 A60B930D6 for ; Thu, 8 Jun 2023 16:29:29 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-565d1b86a64so15356697b3.3 for ; Thu, 08 Jun 2023 16:29:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266969; x=1688858969; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=eGH8bmanmxADlfMPAGLnyXLKr5GaMQcYO5iL0xpxLsM=; b=k+lyD9FRxliSp9Kk+y2zxZt7+UP/DipjPrHL8ilw+nKbmTMcsg2r+ySxNi+6Wrdk2C E/8WTezpx3jjCeqzo8KLXFBzXSYoEHL+zrlj0cV1O1NtpESpZYbh6vXNhK95O2luq+Mo ArOWLH9LYKYCj6VJR/B8z7lh/VXtg3HRh4kV1/JUhiNYo3L0E3ipvXfE6k2Xhx96DYL5 Hm9YNKyX7M0eFzMYc1dogAZNe9hxLSbiBPU80mHLovx8FNnaDpvw70cAvL3CiIm2IRc9 CvaiWA4LYuKdTLHmRrR+tbwU9CPGN7l1XFKzrk/WSaHw2ascPjAVTxy8hDxDHbsdOsv5 4kkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266969; x=1688858969; h=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=eGH8bmanmxADlfMPAGLnyXLKr5GaMQcYO5iL0xpxLsM=; b=T2/ved9CiBwL9gLh7jI8jSD6ABU/aWQpiZJBJkrLtx+TdbutGnGwpeZteaY1m8JULq gBqGMSeyyIHu6mrG7fFYShR2GgpANKBdwLQD3Lzvz9nrdzbIpbk5u6enjIHV9AANc1zO Yf4mIZqFYYZAZyn1/S4FlXP110HYjEtNmBf7GziOwIe7KjOCcRNqJ+D5qzzMOyG2DExk M/iiW2Ut7zxSRJzMsIehBy01O+UUcgQixseqchyb6xcK/b8unQsN10GteXGkb7YzYAPM 5BShsqGe4dtpWCqTeb3zf8ax9sMZ4FQHqW0Cesp57o50+j4Xz9uOoBmXi2DpU67FRmR7 lAxA== X-Gm-Message-State: AC+VfDyXTmdOl2WJgq7NT29AkoyPjiLtUVHOVA5QU+NV3t45sWBL/yhj DgzhzbdJOCJBppcskwJ3Zh/DTv1M3x52 X-Google-Smtp-Source: ACHHUZ6Ig1BCueYoQcVhW16qPJnDywZCzj8H7BT4EfROYt4YbYyrQNoflNmLVkXGi4CRYTDhjW7Zd8l60b0U X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a81:bc4e:0:b0:569:5003:7d79 with SMTP id b14-20020a81bc4e000000b0056950037d79mr704065ywl.3.1686266968895; Thu, 08 Jun 2023 16:29:28 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:04 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-8-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 07/26] perf thread: Add reference count checking From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Modify struct declaration and accessor functions for the reference count checkers additional layer of indirection. Make sure pid_cmp in builtin-sched.c uses the underlying/original struct in pointer arithmetic, and not the temporary get/put indirection. Signed-off-by: Ian Rogers --- tools/perf/builtin-sched.c | 4 +- tools/perf/tests/hists_link.c | 2 +- tools/perf/ui/hist.c | 5 ++- tools/perf/util/hist.c | 2 +- tools/perf/util/machine.c | 2 +- tools/perf/util/sort.c | 2 +- tools/perf/util/thread.c | 20 +++++---- tools/perf/util/thread.h | 79 ++++++++++++++++++----------------- 8 files changed, 63 insertions(+), 53 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index c75ad82a6729..cd79068200e5 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -1385,7 +1385,7 @@ static int pid_cmp(struct work_atoms *l, struct work_= atoms *r) { pid_t l_tid, r_tid; =20 - if (l->thread =3D=3D r->thread) + if (RC_CHK_ACCESS(l->thread) =3D=3D RC_CHK_ACCESS(r->thread)) return 0; l_tid =3D thread__tid(l->thread); r_tid =3D thread__tid(r->thread); @@ -1393,7 +1393,7 @@ static int pid_cmp(struct work_atoms *l, struct work_= atoms *r) return -1; if (l_tid > r_tid) return 1; - return (int)(l->thread - r->thread); + return (int)(RC_CHK_ACCESS(l->thread) - RC_CHK_ACCESS(r->thread)); } =20 static int avg_cmp(struct work_atoms *l, struct work_atoms *r) diff --git a/tools/perf/tests/hists_link.c b/tools/perf/tests/hists_link.c index 12bad8840699..2d19657ab5e0 100644 --- a/tools/perf/tests/hists_link.c +++ b/tools/perf/tests/hists_link.c @@ -148,7 +148,7 @@ static int find_sample(struct sample *samples, size_t n= r_samples, struct thread *t, struct map *m, struct symbol *s) { while (nr_samples--) { - if (samples->thread =3D=3D t && + if (RC_CHK_ACCESS(samples->thread) =3D=3D RC_CHK_ACCESS(t) && RC_CHK_ACCESS(samples->map) =3D=3D RC_CHK_ACCESS(m) && samples->sym =3D=3D s) return 1; diff --git a/tools/perf/ui/hist.c b/tools/perf/ui/hist.c index f164bd26fc41..2bf959d08354 100644 --- a/tools/perf/ui/hist.c +++ b/tools/perf/ui/hist.c @@ -11,6 +11,7 @@ #include "../util/sort.h" #include "../util/evsel.h" #include "../util/evlist.h" +#include "../util/thread.h" #include "../util/util.h" =20 /* hist period print (hpp) functions */ @@ -274,7 +275,9 @@ static int __hpp__sort_acc(struct hist_entry *a, struct= hist_entry *b, if (ret) return ret; =20 - if (a->thread !=3D b->thread || !hist_entry__has_callchains(a) || !symbo= l_conf.use_callchain) + if ((a->thread =3D=3D NULL ? NULL : RC_CHK_ACCESS(a->thread)) !=3D + (b->thread =3D=3D NULL ? NULL : RC_CHK_ACCESS(b->thread)) || + !hist_entry__has_callchains(a) || !symbol_conf.use_callchain) return 0; =20 ret =3D b->callchain->max_depth - a->callchain->max_depth; diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index a4c1b617f6e4..dfda52d348a3 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -2124,7 +2124,7 @@ static bool hists__filter_entry_by_thread(struct hist= s *hists, struct hist_entry *he) { if (hists->thread_filter !=3D NULL && - he->thread !=3D hists->thread_filter) { + RC_CHK_ACCESS(he->thread) !=3D RC_CHK_ACCESS(hists->thread_filter)) { he->filtered |=3D (1 << HIST_FILTER__THREAD); return true; } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 9fcf357a4d53..261188766307 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -2055,7 +2055,7 @@ static void __machine__remove_thread(struct machine *= machine, struct thread_rb_n if (!nd) nd =3D thread_rb_node__find(th, &threads->entries.rb_root); =20 - if (threads->last_match =3D=3D th) + if (threads->last_match && RC_CHK_ACCESS(threads->last_match) =3D=3D RC_C= HK_ACCESS(th)) threads__set_last_match(threads, NULL); =20 if (lock) diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index 5e45c770f91d..047c3606802f 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c @@ -128,7 +128,7 @@ static int hist_entry__thread_filter(struct hist_entry = *he, int type, const void if (type !=3D HIST_FILTER__THREAD) return -1; =20 - return th && he->thread !=3D th; + return th && RC_CHK_ACCESS(he->thread) !=3D RC_CHK_ACCESS(th); } =20 struct sort_entry sort_thread =3D { diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index bee4ac1051ee..0b166404c5c3 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -41,9 +41,10 @@ struct thread *thread__new(pid_t pid, pid_t tid) { char *comm_str; struct comm *comm; - struct thread *thread =3D zalloc(sizeof(*thread)); + RC_STRUCT(thread) *_thread =3D zalloc(sizeof(*_thread)); + struct thread *thread; =20 - if (thread !=3D NULL) { + if (ADD_RC_CHK(thread, _thread) !=3D NULL) { thread__set_pid(thread, pid); thread__set_tid(thread, tid); thread__set_ppid(thread, -1); @@ -68,7 +69,7 @@ struct thread *thread__new(pid_t pid, pid_t tid) list_add(&comm->list, thread__comm_list(thread)); refcount_set(thread__refcnt(thread), 1); /* Thread holds first ref to nsdata. */ - thread->nsinfo =3D nsinfo__new(pid); + RC_CHK_ACCESS(thread)->nsinfo =3D nsinfo__new(pid); srccode_state_init(thread__srccode_state(thread)); } =20 @@ -105,26 +106,31 @@ void thread__delete(struct thread *thread) } up_write(thread__comm_lock(thread)); =20 - nsinfo__zput(thread->nsinfo); + nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo); srccode_state_free(thread__srccode_state(thread)); =20 exit_rwsem(thread__namespaces_lock(thread)); exit_rwsem(thread__comm_lock(thread)); thread__free_stitch_list(thread); - free(thread); + RC_CHK_FREE(thread); } =20 struct thread *thread__get(struct thread *thread) { - if (thread) + struct thread *result; + + if (RC_CHK_GET(result, thread)) refcount_inc(thread__refcnt(thread)); - return thread; + + return result; } =20 void thread__put(struct thread *thread) { if (thread && refcount_dec_and_test(thread__refcnt(thread))) thread__delete(thread); + else + RC_CHK_PUT(thread); } =20 static struct namespaces *__thread__namespaces(struct thread *thread) diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index b103992c3831..9068a21ce0fa 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -15,6 +15,7 @@ #include "rwsem.h" #include "event.h" #include "callchain.h" +#include =20 struct addr_location; struct map; @@ -34,7 +35,7 @@ struct thread_rb_node { struct thread *thread; }; =20 -struct thread { +DECLARE_RC_STRUCT(thread) { struct maps *maps; pid_t pid_; /* Not all tools update this */ pid_t tid; @@ -123,192 +124,192 @@ int thread__memcpy(struct thread *thread, struct ma= chine *machine, =20 static inline struct maps *thread__maps(struct thread *thread) { - return thread->maps; + return RC_CHK_ACCESS(thread)->maps; } =20 static inline void thread__set_maps(struct thread *thread, struct maps *ma= ps) { - thread->maps =3D maps; + RC_CHK_ACCESS(thread)->maps =3D maps; } =20 static inline pid_t thread__pid(const struct thread *thread) { - return thread->pid_; + return RC_CHK_ACCESS(thread)->pid_; } =20 static inline void thread__set_pid(struct thread *thread, pid_t pid_) { - thread->pid_ =3D pid_; + RC_CHK_ACCESS(thread)->pid_ =3D pid_; } =20 static inline pid_t thread__tid(const struct thread *thread) { - return thread->tid; + return RC_CHK_ACCESS(thread)->tid; } =20 static inline void thread__set_tid(struct thread *thread, pid_t tid) { - thread->tid =3D tid; + RC_CHK_ACCESS(thread)->tid =3D tid; } =20 static inline pid_t thread__ppid(const struct thread *thread) { - return thread->ppid; + return RC_CHK_ACCESS(thread)->ppid; } =20 static inline void thread__set_ppid(struct thread *thread, pid_t ppid) { - thread->ppid =3D ppid; + RC_CHK_ACCESS(thread)->ppid =3D ppid; } =20 static inline int thread__cpu(const struct thread *thread) { - return thread->cpu; + return RC_CHK_ACCESS(thread)->cpu; } =20 static inline void thread__set_cpu(struct thread *thread, int cpu) { - thread->cpu =3D cpu; + RC_CHK_ACCESS(thread)->cpu =3D cpu; } =20 static inline int thread__guest_cpu(const struct thread *thread) { - return thread->guest_cpu; + return RC_CHK_ACCESS(thread)->guest_cpu; } =20 static inline void thread__set_guest_cpu(struct thread *thread, int guest_= cpu) { - thread->guest_cpu =3D guest_cpu; + RC_CHK_ACCESS(thread)->guest_cpu =3D guest_cpu; } =20 static inline refcount_t *thread__refcnt(struct thread *thread) { - return &thread->refcnt; + return &RC_CHK_ACCESS(thread)->refcnt; } =20 static inline bool thread__comm_set(const struct thread *thread) { - return thread->comm_set; + return RC_CHK_ACCESS(thread)->comm_set; } =20 static inline void thread__set_comm_set(struct thread *thread, bool set) { - thread->comm_set =3D set; + RC_CHK_ACCESS(thread)->comm_set =3D set; } =20 static inline int thread__var_comm_len(const struct thread *thread) { - return thread->comm_len; + return RC_CHK_ACCESS(thread)->comm_len; } =20 static inline void thread__set_comm_len(struct thread *thread, int len) { - thread->comm_len =3D len; + RC_CHK_ACCESS(thread)->comm_len =3D len; } =20 static inline struct list_head *thread__namespaces_list(struct thread *thr= ead) { - return &thread->namespaces_list; + return &RC_CHK_ACCESS(thread)->namespaces_list; } =20 static inline int thread__namespaces_list_empty(const struct thread *threa= d) { - return list_empty(&thread->namespaces_list); + return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list); } =20 static inline struct rw_semaphore *thread__namespaces_lock(struct thread *= thread) { - return &thread->namespaces_lock; + return &RC_CHK_ACCESS(thread)->namespaces_lock; } =20 static inline struct list_head *thread__comm_list(struct thread *thread) { - return &thread->comm_list; + return &RC_CHK_ACCESS(thread)->comm_list; } =20 static inline struct rw_semaphore *thread__comm_lock(struct thread *thread) { - return &thread->comm_lock; + return &RC_CHK_ACCESS(thread)->comm_lock; } =20 static inline u64 thread__db_id(const struct thread *thread) { - return thread->db_id; + return RC_CHK_ACCESS(thread)->db_id; } =20 static inline void thread__set_db_id(struct thread *thread, u64 db_id) { - thread->db_id =3D db_id; + RC_CHK_ACCESS(thread)->db_id =3D db_id; } =20 static inline void *thread__priv(struct thread *thread) { - return thread->priv; + return RC_CHK_ACCESS(thread)->priv; } =20 static inline void thread__set_priv(struct thread *thread, void *p) { - thread->priv =3D p; + RC_CHK_ACCESS(thread)->priv =3D p; } =20 static inline struct thread_stack *thread__ts(struct thread *thread) { - return thread->ts; + return RC_CHK_ACCESS(thread)->ts; } =20 static inline void thread__set_ts(struct thread *thread, struct thread_sta= ck *ts) { - thread->ts =3D ts; + RC_CHK_ACCESS(thread)->ts =3D ts; } =20 static inline struct nsinfo *thread__nsinfo(struct thread *thread) { - return thread->nsinfo; + return RC_CHK_ACCESS(thread)->nsinfo; } =20 static inline struct srccode_state *thread__srccode_state(struct thread *t= hread) { - return &thread->srccode_state; + return &RC_CHK_ACCESS(thread)->srccode_state; } =20 static inline bool thread__filter(const struct thread *thread) { - return thread->filter; + return RC_CHK_ACCESS(thread)->filter; } =20 static inline void thread__set_filter(struct thread *thread, bool filter) { - thread->filter =3D filter; + RC_CHK_ACCESS(thread)->filter =3D filter; } =20 static inline int thread__filter_entry_depth(const struct thread *thread) { - return thread->filter_entry_depth; + return RC_CHK_ACCESS(thread)->filter_entry_depth; } =20 static inline void thread__set_filter_entry_depth(struct thread *thread, i= nt depth) { - thread->filter_entry_depth =3D depth; + RC_CHK_ACCESS(thread)->filter_entry_depth =3D depth; } =20 static inline bool thread__lbr_stitch_enable(const struct thread *thread) { - return thread->lbr_stitch_enable; + return RC_CHK_ACCESS(thread)->lbr_stitch_enable; } =20 static inline void thread__set_lbr_stitch_enable(struct thread *thread, bo= ol en) { - thread->lbr_stitch_enable =3D en; + RC_CHK_ACCESS(thread)->lbr_stitch_enable =3D en; } =20 static inline struct lbr_stitch *thread__lbr_stitch(struct thread *thread) { - return thread->lbr_stitch; + return RC_CHK_ACCESS(thread)->lbr_stitch; } =20 static inline void thread__set_lbr_stitch(struct thread *thread, struct lb= r_stitch *lbrs) { - thread->lbr_stitch =3D lbrs; + RC_CHK_ACCESS(thread)->lbr_stitch =3D lbrs; } =20 static inline bool thread__is_filtered(struct thread *thread) --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 57B21C7EE45 for ; Thu, 8 Jun 2023 23:30:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237307AbjFHXaK (ORCPT ); Thu, 8 Jun 2023 19:30:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237182AbjFHX3e (ORCPT ); Thu, 8 Jun 2023 19:29:34 -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 4F0B430DF for ; Thu, 8 Jun 2023 16:29:32 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5651d8acfe2so16060677b3.2 for ; Thu, 08 Jun 2023 16:29:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266971; x=1688858971; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=YK7vivTUvVnngeLWzmeZQ8bqT/OVbRyCOXzMTcRsZMs=; b=h0zjH+hUAGfTVAZ7FUCUWS+twdPiVH3SkQmolEIxSW6NKYWlfbX93GzrWc0xPE87rf GCtKAcWHTFnmrRx0x6g+aMCnF7/smB+83LTuyDWpYDRcBrXRI/peA8iy6uqEdPuoCgj1 mPwWGkuikA6MRqCdj7bkDOG2NI8ry+HbmvIEhZhn1iD9NXqlyOaZpSxwAplOp0GWqF7z h5TYTJfZ7Jeg/PmuzM79qNA7lNueWIaXYZdQqBZO3NpBdb7VYJ4HTbfCk4DNYH3QTFZW 3PgUNJ4Hc4UT5cofyQPEGCkxVMzE1BLJp/+LuT/CPLshhnrbL8XBMMKqqmuVgW9aQ3vW 4uZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266971; x=1688858971; h=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=YK7vivTUvVnngeLWzmeZQ8bqT/OVbRyCOXzMTcRsZMs=; b=h8daY1fh1vckIMv9Kd+tW6fo3Tpwqj2eI7X7PnisT+qvvtrpbJsz8dht5xQMcGsSUv IffkUu5TJhih1CAv1A3peahcfaNP18Ecrr3Gs9y16H0HwxrlMazcXAB5fmCRej26o3iY +Kkc1eoYxjQTEKAfcy/ig6e1etao9lqQDh0j5AETwxR8FC78UO+j68Q3EzZ4LU7L4kuW uww9096R5/ipOLt8Rtn8ThljWV6X5c8zqHJHrhAhVe87f715Dw4cJflhu5ebBtuCHvmj txpNQcCIYaoT85eTrGAF9swKEwn5LjzwirVBlAQylLKDNRpJfPjwhpukGa4pznwgqIPB EFdA== X-Gm-Message-State: AC+VfDxlkPhu4y/5npVI9xS9WSYGFHMUZfnH5aUuP7G06zYD/RnXJrNl ppaCBjlMpBzeVJX3vZr0FAHdJKDAcZCW X-Google-Smtp-Source: ACHHUZ7lFo/l4NRGaS+SLLEGSLVGTsSlh5Hy8OpV3peTtU6WbuCKJ2Yl1mIVlrFEjceAhvhpmJc2Y0yNelZO X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a81:ad59:0:b0:561:c9c0:98d9 with SMTP id l25-20020a81ad59000000b00561c9c098d9mr673787ywk.4.1686266971518; Thu, 08 Jun 2023 16:29:31 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:05 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-9-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 08/26] perf machine: Make delete_threads part of machine__exit From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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 code required threads to be deleted before machine__exit was called or the threads would be leaked. This was error prone so move the delete_threads into machine__exit. Signed-off-by: Ian Rogers --- tools/perf/tests/code-reading.c | 1 - tools/perf/tests/dwarf-unwind.c | 1 - tools/perf/tests/mmap-thread-lookup.c | 1 - tools/perf/tests/symbols.c | 1 - tools/perf/util/machine.c | 1 + tools/perf/util/session.c | 6 ------ 6 files changed, 1 insertion(+), 10 deletions(-) diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-readin= g.c index 2a7b2b6f5286..ed3815163d1b 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -721,7 +721,6 @@ static int do_test_code_reading(bool try_kcore) evlist__delete(evlist); perf_cpu_map__put(cpus); perf_thread_map__put(threads); - machine__delete_threads(machine); machine__delete(machine); =20 return err; diff --git a/tools/perf/tests/dwarf-unwind.c b/tools/perf/tests/dwarf-unwin= d.c index ee983b677a6a..d01aa931fe81 100644 --- a/tools/perf/tests/dwarf-unwind.c +++ b/tools/perf/tests/dwarf-unwind.c @@ -235,7 +235,6 @@ noinline int test__dwarf_unwind(struct test_suite *test= __maybe_unused, thread__put(thread); =20 out: - machine__delete_threads(machine); machine__delete(machine); return err; } diff --git a/tools/perf/tests/mmap-thread-lookup.c b/tools/perf/tests/mmap-= thread-lookup.c index 3891a2a3b46f..ddd1da9a4ba9 100644 --- a/tools/perf/tests/mmap-thread-lookup.c +++ b/tools/perf/tests/mmap-thread-lookup.c @@ -208,7 +208,6 @@ static int mmap_events(synth_cb synth) addr_location__exit(&al); } =20 - machine__delete_threads(machine); machine__delete(machine); return err; } diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c index 2d1aa42d36a9..16e1c5502b09 100644 --- a/tools/perf/tests/symbols.c +++ b/tools/perf/tests/symbols.c @@ -38,7 +38,6 @@ 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 diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 261188766307..46af5e9748c9 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -256,6 +256,7 @@ void machine__exit(struct machine *machine) zfree(&machine->current_tid); zfree(&machine->kallsyms_filename); =20 + machine__delete_threads(machine); for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { struct threads *threads =3D &machine->threads[i]; =20 diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 65ac9f7fdf7e..00d18c74c090 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -278,11 +278,6 @@ struct perf_session *__perf_session__new(struct perf_d= ata *data, return ERR_PTR(ret); } =20 -static void perf_session__delete_threads(struct perf_session *session) -{ - machine__delete_threads(&session->machines.host); -} - static void perf_decomp__release_events(struct decomp *next) { struct decomp *decomp; @@ -305,7 +300,6 @@ void perf_session__delete(struct perf_session *session) auxtrace__free(session); auxtrace_index__free(&session->auxtrace_index); perf_session__destroy_kernel_maps(session); - perf_session__delete_threads(session); perf_decomp__release_events(session->decomp_data.decomp); perf_env__exit(&session->header.env); machines__exit(&session->machines); --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 07B7AC7EE45 for ; Thu, 8 Jun 2023 23:30:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237389AbjFHXaQ (ORCPT ); Thu, 8 Jun 2023 19:30:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237273AbjFHX3r (ORCPT ); Thu, 8 Jun 2023 19:29:47 -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 E0CF730E4 for ; Thu, 8 Jun 2023 16:29:34 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-56938733c13so14476647b3.1 for ; Thu, 08 Jun 2023 16:29:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266974; x=1688858974; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=lMdtNGTBh3+qqarzoiXGjMMQ5QKS2zkBmCpcE9AaZG8=; b=qvrNwAa6I8cb8TBsqGY45Cz9ZY5hg90lpgYn1T72mITq/cH1zTVmGZFFIfHSrQzepJ MDlAHp3MdRqDh7CrwO9hV6e2y+x7NM8TaojgbiBmaUcEM3FwPhq4ciB6ctzUKbpor/2l aPyb8HRkdWMaFXWVLuOgsDjkQjIWdlo8BNNCKX1/P80tufvQYnogwiRxMJFx81lGbKk7 GcAkvRCWL2LQc7GTtWtVcbuYKt/hWkscrxD2oe48rtQ8W1objUdTtp2QBQNbRfCwRhHJ CMLcFdB/6T4HUmmcL7QoCzQvG3eb0YQOl6xXhNxG4SWvdPpyoo0dpGi5TRVHv2Rulyph cQbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266974; x=1688858974; h=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=lMdtNGTBh3+qqarzoiXGjMMQ5QKS2zkBmCpcE9AaZG8=; b=bub67kUaTnE0YZbbvkoBYEGly4w51gs1iWQgD4L36FjFEO6qy73GryoVtotbJ5qTKJ fppNVmdBdDtn8iaDFalBQKVtNK+WFoLHUooUwrUdn35gCu3dIOJATvsOkziId06yV5R4 HpjfxAGWyafsdvuDI/VMvM/kMihd5IHb7FhK/U7EZ1nTrEiNotDubPdImN4GyO3vdJws Uza4+fojEst9SB4Hok1EWOtseDyqZl5s+h2v4q3/+HgKEGwoKhFudZ2acyjqlZuYED1R wD0rlyJwpe63JOhSm1yNoNXSIkhO6/h+GmSrcJ8Mj1S0XvyKXNTPogWJW0h5wMz3v/31 9U7Q== X-Gm-Message-State: AC+VfDzqHGsit1hOimuMqL8UzpHQYXjZVZxXz81H+42h0yBG7VGc2wv3 BChkNOlaW6R2ivV+a7LXtABX2JBp8PvA X-Google-Smtp-Source: ACHHUZ7umrASYnYvCzdFpWOm1U1RbfrT9D+RXK03wWYWPutn42WIip4GkHo95/6dlPdNDq9S81nCc3kqD1Pz X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a25:c585:0:b0:bad:e8b:17d4 with SMTP id v127-20020a25c585000000b00bad0e8b17d4mr335389ybe.7.1686266974158; Thu, 08 Jun 2023 16:29:34 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:06 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-10-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 09/26] perf report: Avoid thread leak From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Caught with address sanitizer and reference count checking. Signed-off-by: Ian Rogers --- tools/perf/builtin-report.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 0b091a8983a5..a31a23af5547 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -839,6 +839,7 @@ static struct task *tasks_list(struct task *task, struc= t machine *machine) return ERR_PTR(-ENOENT); =20 parent_task =3D thread__priv(parent_thread); + thread__put(parent_thread); list_add_tail(&task->list, &parent_task->children); return tasks_list(parent_task, machine); } --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 E5238C7EE37 for ; Thu, 8 Jun 2023 23:30:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237516AbjFHXa0 (ORCPT ); Thu, 8 Jun 2023 19:30:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237363AbjFHX3s (ORCPT ); Thu, 8 Jun 2023 19:29:48 -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 BCD3330FD for ; Thu, 8 Jun 2023 16:29:37 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5655d99da53so25389627b3.0 for ; Thu, 08 Jun 2023 16:29:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266976; x=1688858976; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=HYQaZtuHl/SxeGLPx3KYJtDk+ilAjOX/gkAdD1/27NA=; b=fBJl3wcz+FnbBgSbRGxXixBnK3jNLSuIKoFf166vI/z37Q4wIcVYPmW50NKtSp9UgF rvr7Vycm6Wb545G0pPRanvDdHAxrKWNJYkGc8Cmt8RYCACXwUGF/iIgTa75YwhW6PV8M mKbs+2jj1XRyRcyBNZLzm3S68U4QfnBSrHCRrxCTe8VZcEJxXqnwaqFgKSsS9LhNsvdM tl5boNkQ9DOfVLIFy8QP8zuGyB+ZRB6QpW44Y8AgMfKg/dSL2Zi7fb7dxcr7cS8GtvBr vUG+miDiAkxObR0Wne3r2jVUorOhIbFbcFXuzN7/NEbTmZrBQnQK7BN7zEnfzQDZf8N8 FOdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266976; x=1688858976; h=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=HYQaZtuHl/SxeGLPx3KYJtDk+ilAjOX/gkAdD1/27NA=; b=a0J5PFvRSZ+AeNTXMv4tPWYDONmwo7VPS4LfVeTJgCJ6I3Cd8KQO6edaUROR96yvD8 AjvVCD3cWYsEO5hknXgmx0dF2Zcu8VvVdDhsB8W16IkqUEO2Tu82jgeqiEyKyhniTFif KFkJt0UAkxW4+/dJWtYZh3V1R01YngsJcnDZFjz/lAYF0J6DX1T1Zj422yDaR6RPO/7R RZfNFBtsJVz8QjrTO4LD2oF7qDob6ukCSIz+pZA8hJ0BEMhT1IX6RvT52z1xOLv5rZzL v+/MQDIbcIhaHXhPnzo9acU8nXy+g20tWQ9cROO0uiYI5FyOcnXoUclJjp3y8W4zlHxD +cnA== X-Gm-Message-State: AC+VfDwlYX4J70XabdsQ4jXB30e0qcOVvu0MIUkhDPK6NWYRjeQ7wueu PA75b4xR7qU6y4svXP0k23NjxM4L2BlX X-Google-Smtp-Source: ACHHUZ7PFDauXoTjxwgQr/iJ4YUhlHM8t/3S93+ayPur0CSD8Tq5qW6atfbI5j1wzr7n1r7hPNg/mO9mlqzf X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a05:690c:2901:b0:55d:9e7c:72c0 with SMTP id eg1-20020a05690c290100b0055d9e7c72c0mr648158ywb.0.1686266976710; Thu, 08 Jun 2023 16:29:36 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:07 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-11-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 10/26] perf header: Ensure bitmaps are freed From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" memory_node bitmaps need a bitmap_free to avoid memory leaks. Caught by leak sanitizer. Signed-off-by: Ian Rogers --- tools/perf/util/header.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index d85b39079c31..3db7c1fae71e 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -1389,6 +1389,14 @@ static int memory_node__read(struct memory_node *n, = unsigned long idx) return 0; } =20 +static void memory_node__delete_nodes(struct memory_node *nodesp, u64 cnt) +{ + for (u64 i =3D 0; i < cnt; i++) + bitmap_free(nodesp[i].set); + + free(nodesp); +} + static int memory_node__sort(const void *a, const void *b) { const struct memory_node *na =3D a; @@ -1449,7 +1457,7 @@ static int build_mem_topology(struct memory_node **no= desp, u64 *cntp) *nodesp =3D nodes; qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort); } else - free(nodes); + memory_node__delete_nodes(nodes, cnt); =20 return ret; } @@ -1516,7 +1524,7 @@ static int write_mem_topology(struct feat_fd *ff __ma= ybe_unused, } =20 out: - free(nodes); + memory_node__delete_nodes(nodes, nr); return ret; } =20 --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 4464BC7EE23 for ; Thu, 8 Jun 2023 23:30:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237539AbjFHXab (ORCPT ); Thu, 8 Jun 2023 19:30:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237398AbjFHX3s (ORCPT ); Thu, 8 Jun 2023 19:29:48 -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 BA3233586 for ; Thu, 8 Jun 2023 16:29:39 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-569fee67d9dso24997827b3.1 for ; Thu, 08 Jun 2023 16:29:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266979; x=1688858979; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=aFeBvXSDSdHQDEP4y6c+chE/HON9+ZqSLzpectqZUCI=; b=CLN02UU5+WWzs/zPaD+MM1F2zbmnBKDuiddMFb9fykdDg8v+BAKC13PIbVSZQ6g9ly oTUlgCMkciNJoajBVI0uGTHyF/vmH7VJ4+Sb5R4dLOQkddMLf1VuCqiFZwdxSuAItV4L DxWB+919dwyqi59uSrIKdxJN5rQTc15QIw+PQ6PuSrI7oqBp/YHq7dWYhAtlfld5BbNk sAHJvW+pf00vZhaCk85of58remxSTvBSBHGMPn4FXEcFieVNaE+1T8cIn9DiOzGn+jNb tUbPL+XZ2UiXIVrWL8Xyw+Lh+o3cdPXrehaQmYAr5NmvLNK9padtYiFhZACY7jOfLEPT qJ3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266979; x=1688858979; h=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=aFeBvXSDSdHQDEP4y6c+chE/HON9+ZqSLzpectqZUCI=; b=IQ3+5u0aaQSqflNFQ5UMNbxPe1p6v/8DwM29Cr9VzQveqe+vt1Lcn9K1Mes5jCL1mA NKr5t7cX6FUTE3xxCR3OTJ7wZarc4rFedcdTLzTDv4rB6UCIK8G5XO4xwnjLOfmU5KEz t8L743cnAHW/aP0Cl5eTtEnMgZ900lEqrDn8fXKIuE/3T4WXXZqMxnvRMxbDUrXpJPoi S4DTgYjhD/kKnxV6mUgIwjpbcOS3QO9PlGXNIdkoKu5wpP03J+6rHcnU7jzDUq0Z2YYh tyzTNWyi6kJ4JVE/lWIMpN1bR8HTdD0YYOtbJfuto+zgV7IWzjTMwLJo1i/WRrgLJJpZ hrgg== X-Gm-Message-State: AC+VfDwvx/sm4peh6bsRhOlW5vRkkHkR4vjDLzflqj4DFxB2BHAGXKbq KyQPxgImBnY+DUuKBa1zmcyBpFIuCvrL X-Google-Smtp-Source: ACHHUZ5/rW08nMMKbcCklsjhO+BefYDtbEPUgG16zWlSlryuKZJhC19hBweHC5TODNtYqaCE3NLDu2QjlebL X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a05:690c:2901:b0:55d:9e7c:72c0 with SMTP id eg1-20020a05690c290100b0055d9e7c72c0mr648209ywb.0.1686266978828; Thu, 08 Jun 2023 16:29:38 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:08 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-12-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 11/26] perf stat: Avoid evlist leak From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Free evlist before overwriting in "perf stat report" mode. Detected using leak sanitizer. Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index c87c6897edc9..fc615bdeed4f 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -2427,6 +2427,7 @@ static int __cmd_report(int argc, const char **argv) =20 perf_stat.session =3D session; stat_config.output =3D stderr; + evlist__delete(evsel_list); evsel_list =3D session->evlist; =20 ret =3D perf_session__process_events(session); --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 67FD3C7EE23 for ; Thu, 8 Jun 2023 23:30:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237549AbjFHXae (ORCPT ); Thu, 8 Jun 2023 19:30:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237479AbjFHX3v (ORCPT ); Thu, 8 Jun 2023 19:29:51 -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 321E13598 for ; Thu, 8 Jun 2023 16:29:42 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-568bc5db50dso47624807b3.0 for ; Thu, 08 Jun 2023 16:29:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266981; x=1688858981; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=cxVt6j8z1zIEddbGRdbnpWEVyu2s2AhOUuVVlGcUcvA=; b=ZDaKU3gh0whGBinBkCTBD7l56ny7TFWktGEZvsQMsPvV9tisV+0JT41exFAx8f6gPi zL3CSPldjAK2Tum+6BzTspyaRIu5wm+1PoXPeSjrfOL1NY7BMfHgvo9JlyLedqUh8Oi+ ya3aNjwm6T7pwaa/Uik0jkBtYS9Z0Nz1ywYTRuab3h9NhtucJbP8VbA2TzT5gIYfYcsA 6Cfor+XWTdxq+SohC7OZmWQA7FEak49o/YJEpOAAvnoT1BhGSQrtYsCAaqala9qpeEOC z60k3sB64OJ+I4/JV0EuFtLt0nJqfzd8OBui4LSHZMvwTUyUfT8rxZ9oJawMBrse0Ljf qpIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266981; x=1688858981; h=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=cxVt6j8z1zIEddbGRdbnpWEVyu2s2AhOUuVVlGcUcvA=; b=bDNl2LbOhkGQqNt01WxTh96NrDkW+F192f6e//2T4jDTSbjA2Wn1zkTWnGueCFkcit BHqHHe5/htwj37GT+QbTtk3zZYKYgKmEYTtsCzn8ar4rpZBVYabDVcXbxtGtrf9o2jU1 a6mPnnleHO5tryE0R5xSX6P3Cs04Nr56Q5awcMDX0QmgubP5KUW9q2BaeHAFjhwRXXUs I+ZGLZJ+X+URw845SjUYmpg9DY2gVu0v//eq+BB1jNVWf/kNOywDdTWV8T7OB7xKDCN9 WjZJTVUn4oQWY8XQJaZP8bMs5xiqzyn28+hgKYWjnL0dURodWfZflxHnPS6DP2MzU/QU ceaw== X-Gm-Message-State: AC+VfDyRUhZyWFjUXpw8dYnrpoH7AnpIsh2hlXdUy2krcuF9qGCgaTQB p6+EzgT99u/KoEBf7QKn4zJQ8KmV/cL4 X-Google-Smtp-Source: ACHHUZ7n4Icj81DhdrrgIFwHFE3eedXZ++Aq+OOgMSXdAebV/QYv7OE0ML9Jkqv1obQo2p7dDDiW5LZxa8Rz X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a05:690c:2b8f:b0:561:4723:2088 with SMTP id en15-20020a05690c2b8f00b0056147232088mr590334ywb.4.1686266981201; Thu, 08 Jun 2023 16:29:41 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:09 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-13-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 12/26] perf intel-pt: Fix missed put and leak From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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 missing put and free, detected with leak sanitizer. Signed-off-by: Ian Rogers --- tools/perf/util/intel-pt.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c index 783ce61c6d25..dbf0bc71a63b 100644 --- a/tools/perf/util/intel-pt.c +++ b/tools/perf/util/intel-pt.c @@ -1280,6 +1280,7 @@ static void intel_pt_add_br_stack(struct intel_pt *pt, pt->kernel_start); =20 sample->branch_stack =3D pt->br_stack; + thread__put(thread); } =20 /* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */ @@ -3580,6 +3581,7 @@ static void intel_pt_free(struct perf_session *sessio= n) zfree(&pt->chain); zfree(&pt->filter); zfree(&pt->time_ranges); + zfree(&pt->br_stack); free(pt); } =20 --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 7735DC7EE45 for ; Thu, 8 Jun 2023 23:30:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237194AbjFHXai (ORCPT ); Thu, 8 Jun 2023 19:30:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237538AbjFHX3x (ORCPT ); Thu, 8 Jun 2023 19:29:53 -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 DE83C35AC for ; Thu, 8 Jun 2023 16:29:44 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-568bc5db50dso47625507b3.0 for ; Thu, 08 Jun 2023 16:29:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266984; x=1688858984; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=KPF7IjZ8365DLvNifZS3V/NQW6xDgZgKvmfjNlf3DkQ=; b=itwn2UF1L5IRvMQmodMDp3qI2lqRvmKdaS107elk7C12AWXzh8ahltNoZ+uIedzSao Z5nPTNZ39DfV2wj2fEMWthmk8ZWqECcA1HmdKnFbGZThP+yFsU6w9K4r4iMuSwC+rTer EcWSBl/KxyzJ5qXuvLjL6H+bggq364mlGKr2MYXkh3Kbi5DfjeZ35f/2sga9p88CsyCJ 1x5Lgt+wRgAoGBGVX9eiMHGMbDcOxWi1Q+eWrmJUCXLEtW/vElNsBPp2KO980YmYQPtT c5VyCdiu8CZ0cZrP6Tqt4GU1zJpDIwoZzVB/grwp7PWp0LLEfV//Yr6DY+3xUFBbfGqk yrkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266984; x=1688858984; h=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=KPF7IjZ8365DLvNifZS3V/NQW6xDgZgKvmfjNlf3DkQ=; b=TLXWXlpl10HAm7UAJ7SmdIf/wU0xCiGliDB1loJS9RHnaAYHs/sETP5TgOx1wwory9 RlYi2VHPvQnB6I6XzowLJ2p+DUW5UK3D6fodVXzPmtJV+DJwUE+mbSb6bAugtD9+5o7K tcprpu6sf39AtKW1vh/wBPb0Mh4wA7wV7hJJdtWR8zhhuXs0gGz/JPjs68zepQCnsOtw vfMiAqtLDp10JGTPthHw8mKTzq130wPyHR5KIB5LNn5mZi2QywrAU+6u02+rc6I2I4qW mA25QObLyS25PkLFMK+eizfKd6mRAFsmaCFxV7VrMqOsIJvENq+Q9+7EN8s7zwFJpDC0 DZjw== X-Gm-Message-State: AC+VfDzCCPgeKZW8dCAJJ2XL/RMLEUNIIZA3Msxl3hAJ0Kru5TKRmwdm 9F9vKS0aUB/8aerSomzezygPY19y6NjL X-Google-Smtp-Source: ACHHUZ4KUthDyR45xo5gd3C4Cm0xs+E5OTaPloOE/qHPF2T0/EN5ljbLzp/gTLV/MuBSrD9FlGxtf1FaRYl7 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a81:ae19:0:b0:561:8c16:2b66 with SMTP id m25-20020a81ae19000000b005618c162b66mr75982ywh.4.1686266983934; Thu, 08 Jun 2023 16:29:43 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:10 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-14-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 13/26] perf evlist: Free stats in all evlist destruction From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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 is no evsel free stats, freeing in the evlist__delete ensures memory leaks are avoided. Issues detected with "perf stat report" and leak sanitizer, perf stat uses perf_session__delete to free the evlist. Add dummy symbol for python build. Signed-off-by: Ian Rogers --- tools/perf/util/evlist.c | 2 ++ tools/perf/util/python.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 82c0b3d0c822..7ef43f72098e 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -31,6 +31,7 @@ #include "util/pmu.h" #include "util/sample.h" #include "util/bpf-filter.h" +#include "util/stat.h" #include "util/util.h" #include #include @@ -171,6 +172,7 @@ void evlist__delete(struct evlist *evlist) if (evlist =3D=3D NULL) return; =20 + evlist__free_stats(evlist); evlist__munmap(evlist); evlist__close(evlist); evlist__purge(evlist); diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 8de1b759bbaa..a7b2cb05dc86 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -1494,3 +1494,7 @@ void test_attr__open(struct perf_event_attr *attr, pi= d_t pid, struct perf_cpu cp int fd, int group_fd, unsigned long flags) { } + +void evlist__free_stats(struct evlist *evlist) +{ +} --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 59CB4C7EE25 for ; Thu, 8 Jun 2023 23:30:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237594AbjFHXan (ORCPT ); Thu, 8 Jun 2023 19:30:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237609AbjFHXaA (ORCPT ); Thu, 8 Jun 2023 19:30:00 -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 CDE9F30D8 for ; Thu, 8 Jun 2023 16:29:47 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-ba8337ade1cso1546529276.2 for ; Thu, 08 Jun 2023 16:29:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266986; x=1688858986; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=y53qLEHCoCMUqnY97AKj5OPiYKzQLuS6tjfhZxn5cao=; b=BWtzVLrqKoYJkSLm4xm24dV+YG4tQHyElrZuiGFxyxxUpwkdM8hK6Yhml3VsH4z5Z6 3MLwEVGcbfnE2o12xZXJpiEq2AyD0GGwb5fjRx6qe3PK/6uXhHcU+Vp0hL8QMOcSPUHb VWcfJCljAE445drlVaMjFp3LzmE+ldlRFKk5DiHtIgvxkMWoNAbzNMvRu2c1v6ZJlXVo Q/SvbwhBi9qb5VEewCYKaURAKA/gr26s2zE4cVmEEL2VuKZ9aXIv+BNsUDOrrvzn4QBG MIw9GfUgPmu0kUHOCZ6Vnpn7bqh4rUXtUhyfvjO/wuKjtWszxn3htihXp+h2u3rbkzVR Qs8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266986; x=1688858986; h=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=y53qLEHCoCMUqnY97AKj5OPiYKzQLuS6tjfhZxn5cao=; b=Rkwy1KnBKAVZjDCfEHDC74NaIQtxs20ykL9EBr6BYcS3cWdfgKA2BNZC28cMkcL53Z RcQQaDZdpnC+p8cRLCpuizQ6nkeLz5xmmTmpG532U5xeL/nofvg/NVbpptU71FlmMpmC /cLBDkynz5Jr3lLr5qbRkQzjkGYYi8n+112lh9byYAzK1ffIL9Y2MyA2oKivbE3OpOqI O2QMzqy4zGgSo5RG/5gx0vTQhz7V25ZpnzINcVX9Kgp2gfefPzUTSQ4dVmiF3YlXTfiW jdPHVpLYV27L0KiR0lYD25GR4lpIcyLIeNIGaG7fbSvUkEOSmIJPcS9y/CZsO6C+qa6U V74Q== X-Gm-Message-State: AC+VfDwhtIcnp0yZl8W1ybWg9oI1mPsljy4mJnByGTkAlflHH8VnJhZo 3Zpj65K07Het/R9vPAXcrhWqWdQtht6j X-Google-Smtp-Source: ACHHUZ5DzYfT4hI1PXDA8oBQEX/6jVDDSD3WmZq420hLxCfPkjKadqGgW4rFwIrQydNs80FcrAShZHWOaoZ8 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a25:abb2:0:b0:bad:14ac:f22e with SMTP id v47-20020a25abb2000000b00bad14acf22emr347019ybi.5.1686266986565; Thu, 08 Jun 2023 16:29:46 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:11 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-15-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 14/26] perf python: Avoid 2 leak sanitizer issues From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Leak sanitizer complains about the variable size bf allocation and store to bf if sized 0. Signed-off-by: Ian Rogers --- tools/perf/util/scripting-engines/trace-event-python.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index d7c99028c6e6..d96e5c0fef45 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -735,6 +735,9 @@ static void regs_map(struct regs_dump *regs, uint64_t m= ask, const char *arch, ch unsigned int i =3D 0, r; int printed =3D 0; =20 + if (size <=3D 0) + return; + bf[0] =3D 0; =20 if (!regs || !regs->regs) @@ -764,7 +767,7 @@ static void set_regs_in_dict(PyObject *dict, * 10 chars is for register name. */ int size =3D __sw_hweight64(attr->sample_regs_intr) * 28; - char bf[size]; + char *bf =3D malloc(size); =20 regs_map(&sample->intr_regs, attr->sample_regs_intr, arch, bf, sizeof(bf)= ); =20 @@ -775,6 +778,7 @@ static void set_regs_in_dict(PyObject *dict, =20 pydict_set_item_string_decref(dict, "uregs", _PyUnicode_FromString(bf)); + free(bf); } =20 static void set_sym_in_dict(PyObject *dict, struct addr_location *al, --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 72558C7EE45 for ; Thu, 8 Jun 2023 23:30:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237368AbjFHXar (ORCPT ); Thu, 8 Jun 2023 19:30:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237118AbjFHXaG (ORCPT ); Thu, 8 Jun 2023 19:30:06 -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 4A6F93A84 for ; Thu, 8 Jun 2023 16:29:50 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-568a8704f6dso15415717b3.1 for ; Thu, 08 Jun 2023 16:29:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266989; x=1688858989; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=/2LYKvHE8++IbYuq2taMyw/mM4bdVG+zrMSa7VFSz/Q=; b=TzbIJ3uKa81+rbLJkY63DCqEspBrctev4zihyaioZdgdzF2s9eiAawlQ2rtBvTpcK4 OKa438YpLrFDHiUgPR8wQIWLAw5Fa7+9vO/XqxYqscHc2eARkG/XHu4aNtjwjqfC5fdf o4p8ANN0LoHEJF0BdH019q3qRdUASdoVJofuu+Jeca0kfZbD4N8TTyI9R71fQrI2G7Jy 5LpwsrNkjW/VPcvOEIzeEiTSVVfZ9n4v+9eWDO1AODUSRvYnWqsQcvfvhN6yCirSMo5u hX9Pr7weJ4y1KL9S6Nuw4QY2+/48Ury50XWQINKQlXClBzfcxY1s9p+n19rFEtcTyE2H rCzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266989; x=1688858989; h=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=/2LYKvHE8++IbYuq2taMyw/mM4bdVG+zrMSa7VFSz/Q=; b=JvtDsVsxUzYyh08Ae0WlFzxy6rw7b7i7IAyJ6ETk3m3KGccD3w3STXxh0tq3G68N/u yH8mZdzVsALjrRpSCGLjskS2cH+bkzNqvFWMQSuRZCMp/LY1yEyV3GVFJab47zCp2tlL vAVzcL+hLN7k2l933JdO2foUoqDt7FiRyoXKkVdKd+OhotKz6OdjxTgx2B+tP0Ujk/A2 3W6pSBP+6/bvW9V6p7p8WX+lVUP+dlYR51nlRNi1d9gMFojMh2V1iKSVnzXTcijlJVhn 8oNrtbyhdzT+vif+gr+9i+Ok/B6noprsDCtpMMJVkfEZ20nsp5yBDKh9Q7zTA94Grj/5 hKjg== X-Gm-Message-State: AC+VfDwwyX2APPnpDm7iCLmeyriqZehp/qMgL7pAhr8yNDSxGNflGQtJ 0fMqpABrYqK22q2xn3v0z+p8zCxeKXFm X-Google-Smtp-Source: ACHHUZ5fZsV7EmshvlIw+kk99S5h/NVh0r5rwl0Jo3LCmFqB69mG7iqRhyVa6eIIBq1xAgoAmjMk5CkQjnrI X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a05:690c:702:b0:565:ebd4:304d with SMTP id bs2-20020a05690c070200b00565ebd4304dmr639592ywb.4.1686266989075; Thu, 08 Jun 2023 16:29:49 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:12 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-16-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 15/26] perf jit: Fix two thread leaks From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" As reported by leak sanitizer with reference count checking. Signed-off-by: Ian Rogers --- tools/perf/util/jitdump.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c index 2380b41a4caa..6b2b96c16ccd 100644 --- a/tools/perf/util/jitdump.c +++ b/tools/perf/util/jitdump.c @@ -800,6 +800,7 @@ static void jit_add_pid(struct machine *machine, pid_t = pid) } =20 thread__set_priv(thread, (void *)true); + thread__put(thread); } =20 static bool jit_has_pid(struct machine *machine, pid_t pid) @@ -811,6 +812,7 @@ static bool jit_has_pid(struct machine *machine, pid_t = pid) return false; =20 priv =3D thread__priv(thread); + thread__put(thread); return (bool)priv; } =20 --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 76003C7EE23 for ; Thu, 8 Jun 2023 23:31:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237447AbjFHXbB (ORCPT ); Thu, 8 Jun 2023 19:31:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237348AbjFHXaO (ORCPT ); Thu, 8 Jun 2023 19:30:14 -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 3C09930F7 for ; Thu, 8 Jun 2023 16:29:52 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bb3cb542875so1662165276.1 for ; Thu, 08 Jun 2023 16:29:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266991; x=1688858991; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=HiAGlNZo6LXveOEFDnkkT89wkMT4puWq+yhAGi6vKP8=; b=u+3QXdLeECEvsv94B0MruS/YOXHDhV0YcI12nIU2fPum5/LYqTXJMF1kvVgDLAhd69 TB+RJB7OC/HHWpeh7KmqrHAPzNPPTDPs/OmNWO/o2KRujpqU/fSwzhmFr09RuWuqyC/T BzZ0/9JGdYaugJO93/OcTlcUa5DARHOIMgR1ocJWkQzbY1sSYakwLv32CJlgOR6HBARC gz455L472xGEXQfKqdk8T6z6OfDB3KCJWYfUVI/PGvFTO5mLQXvHOBkfeWWeIOYweQM1 /mrxG3oQpV1hhk+hqqFyeAEW0j9hksyd5d3ot94/g5G8yPxOtDBBg8z3j82W+cBjX+b7 8aNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266991; x=1688858991; h=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=HiAGlNZo6LXveOEFDnkkT89wkMT4puWq+yhAGi6vKP8=; b=MUeVz9iRdk4cpBx6ATYuyK+7lrc9MQDlV6UjgrQrNTpZSjHHZVYhg9IQwQC2pbtTyi K68WEc3qSSgjq8OPE+Di8RBhCvKmIPsMWdGdW3GA7QACFejwGjPmgecgZgMh7sTcgPup 2CwWXZLVfJeyEoI3zSx4dFC0Z2vqnhGQ6MBDzFVuNcnysmeyDUDZFXJYpOqbxOYl7agc zUb047cqy6TJMllwE4upo0dy/mpM6t/JSdBmnADaUWQArgaEmdungv1oHiCckK6mNIn2 PbxN9AC9ltdkH1hgNh+Ha1ROrtfBMQQflKBTHKCJmnVpT9ePwfEZKL006owT6G12OSA2 R2tQ== X-Gm-Message-State: AC+VfDx5tCQ29MyWQkr+yjv0fNB0yEK+XCpQ4uZlb/YjXIl1AZ6dzU/S ojilfTXZsOr0vmYGphVo3a6YDKkrEbMM X-Google-Smtp-Source: ACHHUZ4V8g7YSjAMhsnMLI/N9elrTcMGdXKg6JQGvTOe1tUTMtyI07H4+/X7cQCtnKGmMy/p1PmmgQPPI/lf X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a25:588:0:b0:ba8:3e2d:58f8 with SMTP id 130-20020a250588000000b00ba83e2d58f8mr611466ybf.5.1686266991630; Thu, 08 Jun 2023 16:29:51 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:13 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-17-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 16/26] perf symbol-elf: Correct holding a reference From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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 a reference is held, don't put it as this will confuse reference count checking. Signed-off-by: Ian Rogers --- tools/perf/util/symbol-elf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 63882a4db5c7..e6493d1cc251 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1389,11 +1389,11 @@ static int dso__process_kernel_symbol(struct dso *d= so, struct map *map, /* Ensure maps are correctly ordered */ if (kmaps) { int err; + struct map *tmp =3D map__get(map); =20 - map__get(map); maps__remove(kmaps, map); err =3D maps__insert(kmaps, map); - map__put(map); + map__put(tmp); if (err) return err; } --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 A504AC7EE25 for ; Thu, 8 Jun 2023 23:31:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237471AbjFHXbG (ORCPT ); Thu, 8 Jun 2023 19:31:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237417AbjFHXaS (ORCPT ); Thu, 8 Jun 2023 19:30:18 -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 3054E3584 for ; Thu, 8 Jun 2023 16:29:54 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bb05f0e6ef9so1607543276.1 for ; Thu, 08 Jun 2023 16:29:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266994; x=1688858994; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=dsHeOnEMfHd9s4DOJfCHWE1AXrbWG8qUbRavLpQo6Y4=; b=u/Qq25sb4E3B/YN9x8AfhOptxhinrbUgxpyj6i2VnVhF2falloUPeEVua2xI7eTgZ6 9Y8toDrNHLnYKnplX5n9qqM33bvduJatdErNYGQESFU0FadeY7tKHF1ahdrkv1yFddlL pBErliZx0UsK/W9S1qORmBxXTTCtDepCNQwf5bwE/uLFWIr+VSqant5bxtav5gTlrcaA F/9rW8q3tuQS+6PEX6dtl46wyFTCxHPwHHMr0gxri3Ghal/QZje/aOk408AxeUO8F0Qh udCQ04uab2R7IuY1HP8r9ROZBB4/Mt1ChYs+oD+J3Rg1CRWI+cA8UMoUJ4cb+g9hNUig TXfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266994; x=1688858994; h=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=dsHeOnEMfHd9s4DOJfCHWE1AXrbWG8qUbRavLpQo6Y4=; b=kMFwQeVEd02X+PftJyfpVmuTfqA7FNqS3Aqr5baMzxvEYTxJdu0eyvEZdw4ArYRCcY 9ohGJ3gklI86svx9ASFHHSWLoVGCpnOZqMtG/gOTWbQvCBhc+wKlkO9AO/uUHmTxm1RY b49IgLlF0wzQFhmI8tPOaMyF5o8JJubOxeDZAt9eso4egl9KJceay4NoUDHfOWtYyWKY 2P2KOmlaEtWhHXJNzDojb4eg0zDyKeEfmb3QTazgZfjNa36ygTuApGzic8ivvKIQjLTX R7id35fCG+Mp0ktkH+S+YaEOqYjmy/mQGxIpaq/WkXghRGlpP6NW26/lNTvvWAQAAn+/ i5kg== X-Gm-Message-State: AC+VfDyWdHiAd+wLVkw/IQMbH6hgXkdagMoHAV4cdrTskWxTOrygZ282 uhc3MxRjhcKyxRr0uZuihZmfna+STDAD X-Google-Smtp-Source: ACHHUZ72ScCy5hQCMHWZ9ZJ+Q/Hjmqux3MK2aSfNgD4ALpiCGxXMG3yuJqiTHTTc+k5GHvXKXHBsUuFK+hDJ X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a25:240f:0:b0:bad:600:1833 with SMTP id k15-20020a25240f000000b00bad06001833mr605870ybk.0.1686266993757; Thu, 08 Jun 2023 16:29:53 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:14 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-18-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 17/26] perf maps: Fix overlapping memory leak From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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 a missed free detected by leak sanitizer. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 5206a6433117..233438c95b53 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -374,6 +374,7 @@ int maps__fixup_overlappings(struct maps *maps, struct = map *map, FILE *fp) } put_map: map__put(pos->map); + free(pos); } up_write(maps__lock(maps)); return err; --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 9048FC7EE25 for ; Thu, 8 Jun 2023 23:31:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237678AbjFHXbM (ORCPT ); Thu, 8 Jun 2023 19:31:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237476AbjFHXaW (ORCPT ); Thu, 8 Jun 2023 19:30:22 -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 D3E3B3A8B for ; Thu, 8 Jun 2023 16:29:57 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-565a66a5d84so15734087b3.0 for ; Thu, 08 Jun 2023 16:29:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266996; x=1688858996; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=yGUf69TvsdEnNaIsR3ddGXV6UHIPZliOJXiSi9AkMd8=; b=pj+cfLWhmvN+fFVnoiQM42+qIryqDUimhVDEqfmtvHnlEMal5DUHbUXirbPSFWbrXl TVxKULuQgeyTD8ezUOpRIuKywDDAPZ8xE0fFiawIFwduHPH9FORZrnn6WUXqVFweowYF ehf8+d1JTUDjZgUFYCGWN2yKCSFLiLgosTtMM/ZBrrlBbSh1AesSWwqpYkBaNx5UgBEl bf3jN2Y+kOmSQ0gOGeCvq6cHlWjE2fKo5bzeUGhJU5DZy9UNpmCWwcYxZ1p2q9PHq1dx SEDb6U4MeNf7OqW2QvuRJ262VrHkjKsRUyHli3nBlXxIkbyqp99m569COtsbWx0pwBIQ thMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266996; x=1688858996; h=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=yGUf69TvsdEnNaIsR3ddGXV6UHIPZliOJXiSi9AkMd8=; b=P64KFamc8u2Clkp5liu9WZaoaQo9VHyDCB+qhEUeGqC23UUqKdjfKEB76xQCNXSX1Q 0iaU7fFNMWbagOIQKS+hdtI3X+bnNiaExpxBzHr9XXtjLhIPuAZUY6hDOVF82GIqrEer j5oWhKJzf8JgciAFxJ0MVKUcSLY/GzcZuiBRKNg5uC/X4vO6KwzoH467KzQde/FntYhR NryTpDvGnxmOH6GAjbqWbOrq7oJ2CZVY65THgY55sjd4QoaVxt2MAWjhxe5hVJCexFIT D/k1twAH8fSKaXaUIKzXZoZ/vkBVfTPYXOReFixe/jFSoiKTOfHpkoS13n3r9Q6iRIG/ N5GQ== X-Gm-Message-State: AC+VfDxg7Bbls8DfalDUZrvOLmUKLl3brIwRdjL6NFjM3xtiguHZ4VkO Vn2cJVN4PBiTacpTmlnc0PqOUltvVoMs X-Google-Smtp-Source: ACHHUZ7M+koLA/Ia+fdnv+WBR7V4mfq9IowZ5DMaWuIoGfb9NIMwoa5UssIGW7+f/lEvCFDdeBHQIZHNM5Mp X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a81:cf02:0:b0:555:cd45:bc3a with SMTP id u2-20020a81cf02000000b00555cd45bc3amr656760ywi.9.1686266996318; Thu, 08 Jun 2023 16:29:56 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:15 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-19-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 18/26] perf machine: Fix leak of kernel dso From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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 kernel dso may be found by searching dsos or allocating if not found. The allocation returns with a reference count of 2, once for the dsos list and once for the returned value. The list search has a reference count of 1, once for the dsos list. To make the reference counts consistent, increase the dsos list search reference count to 2 with a dso__get, and do a put when the scope ends for either the allocated or found dso. This issue was found with leak sanitizer and reference count checking. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 46af5e9748c9..f8e6c07f0048 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1868,7 +1868,7 @@ static int machine__process_kernel_mmap_event(struct = machine *machine, continue; =20 =20 - kernel =3D dso; + kernel =3D dso__get(dso); break; } =20 @@ -1913,6 +1913,7 @@ static int machine__process_kernel_mmap_event(struct = machine *machine, */ dso__load(kernel, machine__kernel_map(machine)); } + dso__put(kernel); } else if (perf_event__is_extra_kernel_mmap(machine, xm)) { return machine__process_extra_kernel_map(machine, xm); } --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 8CB6BC7EE45 for ; Thu, 8 Jun 2023 23:31:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237538AbjFHXbW (ORCPT ); Thu, 8 Jun 2023 19:31:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237551AbjFHXah (ORCPT ); Thu, 8 Jun 2023 19:30:37 -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 DC09F3592 for ; Thu, 8 Jun 2023 16:29:59 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-ba87bd29e9dso1648318276.3 for ; Thu, 08 Jun 2023 16:29:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686266999; x=1688858999; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=lkToW/IxAxIJSXDgl3LW19tfDO+EnTd79xT+CYyJZYQ=; b=ePUYqN7ZPxjl/iomWSLKPkhrn5dsM9dC2KVd28TGs3cL6teDeifYm9LxIGIKjrkZl9 BlV/0s3aD9lr9ZuGvZyfqd1lG3pKdJInvG28YBH7LQOlWwvYUdbo3BBKD9CeGAFtobIR zG+MK/W+kqJLQ+UY7cv7Z6RyZ25OSeLp+kvE0hYaTsSdRu9Hc+FR/1Q9+uc43YG+QYi/ VN6cgDX6cbI4P7pKQeRHEmb+q4LFZO5aVbVIYm7TL15AwTwKMtl5GnWYKXfn9m/v/PLY ZopqDHGQq1KlHwKMmX8ens3+2sZP/wlvgK6rTBCndAz8G+J+TsKDaDYFA4qrEUT+eyXd UnNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686266999; x=1688858999; h=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=lkToW/IxAxIJSXDgl3LW19tfDO+EnTd79xT+CYyJZYQ=; b=fEMhH62JjYdV04JF3LkjW1+nupVi+bqPk9U1oTJa8TEqUmH0Xoi96oeWCQIBj8dLuI BpilNF1vNY1PHPnQGNg0wjg0Ishq9yISVzKg/juPD+JitfPIKMFxp5BpOK2jzt90bNuD TWbrO8TZn+bPOhZPkWxYPUCb/LbTtio7BLbRg/7TSOlvvdBPwlhSFkWeWC/5Z/A2ixg2 X8aBKynxJzEVG4ZFgcKYZgX0U8jIQijRCBTuX3ms9LUw2U8m2s1ARgTa/Mt53Gz9GtDi 7uqIpcdsyFMQtYWa2nJU/Vai7QBAfIHwMksURPvrFDolC4IpNrBdQtG0lVAjuG/xbanO DYFA== X-Gm-Message-State: AC+VfDyTYoFqVLU43bDW5n7G8PEkXbx70b1sVsCRTPhDeeqBHEIPAxtw a2yP1kHmEJuyPLvrTqjsQRo/GcAIWEvZ X-Google-Smtp-Source: ACHHUZ5tPuMIGgflPqSJvwXISvhb5nmU8SoragnEA/O5sf2CLUp8sLIrrQXOwOYy3OBX9Za0AGMnCWLsX7EO X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a25:ad8e:0:b0:bb1:f26d:b18d with SMTP id z14-20020a25ad8e000000b00bb1f26db18dmr584028ybi.13.1686266999057; Thu, 08 Jun 2023 16:29:59 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:16 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-20-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 19/26] perf machine: Don't leak module maps From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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__addnew_module_map requires a put on its result. Add this and narrow the scope of map to make the correctness more obvious. This leak was caught with leak sanitizer and the reference count checker. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index f8e6c07f0048..359ef6b4e840 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1797,7 +1797,6 @@ static int machine__process_kernel_mmap_event(struct = machine *machine, struct extra_kernel_map *xm, struct build_id *bid) { - struct map *map; enum dso_space_type dso_space; bool is_kernel_mmap; const char *mmap_name =3D machine->mmap_name; @@ -1823,8 +1822,8 @@ static int machine__process_kernel_mmap_event(struct = machine *machine, } if (xm->name[0] =3D=3D '/' || (!is_kernel_mmap && xm->name[0] =3D=3D '[')) { - map =3D machine__addnew_module_map(machine, xm->start, - xm->name); + struct map *map =3D machine__addnew_module_map(machine, xm->start, xm->n= ame); + if (map =3D=3D NULL) goto out_problem; =20 @@ -1833,6 +1832,7 @@ static int machine__process_kernel_mmap_event(struct = machine *machine, if (build_id__is_defined(bid)) dso__set_build_id(map__dso(map), bid); =20 + map__put(map); } else if (is_kernel_mmap) { const char *symbol_name =3D xm->name + strlen(mmap_name); /* --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 E4C87C7EE37 for ; Thu, 8 Jun 2023 23:31:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237394AbjFHXb1 (ORCPT ); Thu, 8 Jun 2023 19:31:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236296AbjFHXai (ORCPT ); Thu, 8 Jun 2023 19:30:38 -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 8F5F73598 for ; Thu, 8 Jun 2023 16:30:02 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-ba81b24b878so1544451276.3 for ; Thu, 08 Jun 2023 16:30:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686267002; x=1688859002; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ICM4PzzdQpiNY9bNg8tMI2f6UHi152aJK3ihYGkS2Lc=; b=W4SQ7+xcYX6EMFelWxpkyco/G8WU+FJndMiWcsdzQGvTkiq0YyhE5WzDP2lp3BeAjm /xVv2zYOCCep5CHUk9+kYjRV2oA1/l+ATXVWHowmJxfA255rYr/Zouqj3Wtwl/gQ3Plk Hxd8FLXrmTzv/hb+AW4EyEpEya9b9Pnly0PDcVGeWSjr9EsQy7AFUYDIZEBjvTqexbAO vfmDalsbl0euAryMTdGZ0FoxeVln2gYnljOnOEPpup9FvsDg+ByXYuZA+T3miWW2k/cZ H01/5ZgSqD6l74GL7JVfvN/uiOkQ3ypeDQGIrBOIFWhuEVR6f30++SD/Z4XXSz9Skna1 EQQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686267002; x=1688859002; h=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=ICM4PzzdQpiNY9bNg8tMI2f6UHi152aJK3ihYGkS2Lc=; b=UJ9+Yn20LZ1zvwsM/T9QRNPfzXX8LCsVFNvaN7js3LEZjFHBq5VUrUWsENziybpxGw Ds+dq5tXCXHGfr96k82iEdV1tHHcEepAYIHmAhrAZ06k/6j/C4f1B1mR3Xs9APLF7/8T WI1GYJvZ99WYgR0SRP81YAQgGuLLz8s3/N0uS6Wh/TaR63KYe3uE00BQYEb5lzOuDQ2J ijpV9Qxf8aZxfH96hz2NCQSSkOTKSnqM5oLeGxGzOK/upFrP+A2Gw6uhPm3zAO1vbeau nzO3ooJ0yQHnL0kCbRyc3wSIYzdRz2LX18c3U3qtzO7g4Gp+ovDWOCI6t+QAcV7weMEt yQsg== X-Gm-Message-State: AC+VfDwwwkERJiYGE27G6gmgQ3wj77nulimiPVIzVDXRvGa61MLvg0nQ 7E6EchDpZjGTTHCzy8NNRnWLgDhrvmpX X-Google-Smtp-Source: ACHHUZ4AZm4uKUIg8KKxAHmokqtp8JqUnmhXm0gePazMoCbE9TBXxkaQBbhvWmzgMSkIsOrL28dplH6fOi9k X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a25:ae01:0:b0:bb6:c0d2:2451 with SMTP id a1-20020a25ae01000000b00bb6c0d22451mr348578ybj.6.1686267001758; Thu, 08 Jun 2023 16:30:01 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:17 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-21-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 20/26] perf map/maps/thread: Changes to reference counting From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Fix missed reference count gets and puts as detected with leak sanitizer and reference count checking. Signed-off-by: Ian Rogers --- tools/perf/util/callchain.c | 28 ++++++++++++++++++++++------ tools/perf/util/event.c | 3 +++ tools/perf/util/hist.c | 6 ++++-- tools/perf/util/machine.c | 29 +++++++++++++++++------------ 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index b0dafc758173..909f62b3b266 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -590,6 +590,7 @@ fill_node(struct callchain_node *node, struct callchain= _cursor *cursor) call->ip =3D cursor_node->ip; call->ms =3D cursor_node->ms; call->ms.map =3D map__get(call->ms.map); + call->ms.maps =3D maps__get(call->ms.maps); call->srcline =3D cursor_node->srcline; =20 if (cursor_node->branch) { @@ -649,6 +650,7 @@ add_child(struct callchain_node *parent, list_for_each_entry_safe(call, tmp, &new->val, list) { list_del_init(&call->list); map__zput(call->ms.map); + maps__zput(call->ms.maps); free(call); } free(new); @@ -1010,10 +1012,16 @@ merge_chain_branch(struct callchain_cursor *cursor, int err =3D 0; =20 list_for_each_entry_safe(list, next_list, &src->val, list) { - callchain_cursor_append(cursor, list->ip, &list->ms, - false, NULL, 0, 0, 0, list->srcline); + struct map_symbol ms =3D { + .maps =3D maps__get(list->ms.maps), + .map =3D map__get(list->ms.map), + }; + callchain_cursor_append(cursor, list->ip, &ms, false, NULL, 0, 0, 0, lis= t->srcline); list_del_init(&list->list); + map__zput(ms.map); + maps__zput(ms.maps); map__zput(list->ms.map); + maps__zput(list->ms.maps); free(list); } =20 @@ -1065,9 +1073,11 @@ int callchain_cursor_append(struct callchain_cursor = *cursor, } =20 node->ip =3D ip; + maps__zput(node->ms.maps); map__zput(node->ms.map); node->ms =3D *ms; - node->ms.map =3D map__get(node->ms.map); + node->ms.maps =3D maps__get(ms->maps); + node->ms.map =3D map__get(ms->map); node->branch =3D branch; node->nr_loop_iter =3D nr_loop_iter; node->iter_cycles =3D iter_cycles; @@ -1114,7 +1124,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; + maps__put(al->maps); + al->maps =3D maps__get(node->ms.maps); map__put(al->map); al->map =3D map__get(node->ms.map); al->sym =3D node->ms.sym; @@ -1127,7 +1138,7 @@ 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(machine)) { + if (RC_CHK_ACCESS(al->maps) =3D=3D RC_CHK_ACCESS(machine__kernel_maps(mac= hine))) { if (machine__is_host(machine)) { al->cpumode =3D PERF_RECORD_MISC_KERNEL; al->level =3D 'k'; @@ -1460,12 +1471,14 @@ static void free_callchain_node(struct callchain_no= de *node) list_for_each_entry_safe(list, tmp, &node->parent_val, list) { list_del_init(&list->list); map__zput(list->ms.map); + maps__zput(list->ms.maps); free(list); } =20 list_for_each_entry_safe(list, tmp, &node->val, list) { list_del_init(&list->list); map__zput(list->ms.map); + maps__zput(list->ms.maps); free(list); } =20 @@ -1551,6 +1564,7 @@ int callchain_node__make_parent_list(struct callchain= _node *node) list_for_each_entry_safe(chain, new, &head, list) { list_del_init(&chain->list); map__zput(chain->ms.map); + maps__zput(chain->ms.maps); free(chain); } return -ENOMEM; @@ -1596,8 +1610,10 @@ void callchain_cursor_reset(struct callchain_cursor = *cursor) cursor->nr =3D 0; cursor->last =3D &cursor->first; =20 - for (node =3D cursor->first; node !=3D NULL; node =3D node->next) + for (node =3D cursor->first; node !=3D NULL; node =3D node->next) { map__zput(node->ms.map); + maps__zput(node->ms.maps); + } } =20 void callchain_param_setup(u64 sample_type, const char *arch) diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 2fcfba38fc48..3860b0c74829 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -711,6 +711,9 @@ int machine__resolve(struct machine *machine, struct ad= dr_location *al, if (thread__is_filtered(thread)) al->filtered |=3D (1 << HIST_FILTER__THREAD); =20 + thread__put(thread); + thread =3D NULL; + al->sym =3D NULL; al->cpu =3D sample->cpu; al->socket =3D -1; diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index dfda52d348a3..fb218b3e8a7c 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -450,6 +450,7 @@ static int hist_entry__init(struct hist_entry *he, memset(&he->stat, 0, sizeof(he->stat)); } =20 + he->ms.maps =3D maps__get(he->ms.maps); he->ms.map =3D map__get(he->ms.map); =20 if (he->branch_info) { @@ -497,7 +498,7 @@ static int hist_entry__init(struct hist_entry *he, } =20 INIT_LIST_HEAD(&he->pairs.node); - thread__get(he->thread); + he->thread =3D thread__get(he->thread); he->hroot_in =3D RB_ROOT_CACHED; he->hroot_out =3D RB_ROOT_CACHED; =20 @@ -523,6 +524,7 @@ static int hist_entry__init(struct hist_entry *he, map__put(he->mem_info->daddr.ms.map); } err: + maps__zput(he->ms.maps); map__zput(he->ms.map); zfree(&he->stat_acc); return -ENOMEM; @@ -611,7 +613,6 @@ static struct hist_entry *hists__findnew_entry(struct h= ists *hists, * keys were used. */ cmp =3D hist_entry__cmp(he, entry); - if (!cmp) { if (sample_self) { he_stat__add_period(&he->stat, period); @@ -1309,6 +1310,7 @@ void hist_entry__delete(struct hist_entry *he) struct hist_entry_ops *ops =3D he->ops; =20 thread__zput(he->thread); + maps__zput(he->ms.maps); map__zput(he->ms.map); =20 if (he->branch_info) { diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 359ef6b4e840..bdad4b8bf77d 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -539,7 +539,7 @@ static void machine__update_thread_pid(struct machine *= machine, goto out_err; =20 if (thread__maps(th) =3D=3D thread__maps(leader)) - return; + goto out_put; =20 if (thread__maps(th)) { /* @@ -579,7 +579,7 @@ __threads__get_last_match(struct threads *threads, stru= ct machine *machine, machine__update_thread_pid(machine, th, pid); return thread__get(th); } - + thread__put(threads->last_match); threads->last_match =3D NULL; } =20 @@ -601,7 +601,8 @@ threads__get_last_match(struct threads *threads, struct= machine *machine, static void __threads__set_last_match(struct threads *threads, struct thread *th) { - threads->last_match =3D th; + thread__put(threads->last_match); + threads->last_match =3D thread__get(th); } =20 static void @@ -664,7 +665,6 @@ static struct thread *____machine__findnew_thread(struc= t machine *machine, =20 rb_link_node(&nd->rb_node, parent, p); rb_insert_color_cached(&nd->rb_node, &threads->entries, leftmost); - /* * We have to initialize maps separately after rb tree is updated. * @@ -673,6 +673,7 @@ static struct thread *____machine__findnew_thread(struc= t machine *machine, * the rb tree. */ if (thread__init_maps(th, machine)) { + pr_err("Thread init failed thread %d\n", pid); rb_erase_cached(&nd->rb_node, &threads->entries); RB_CLEAR_NODE(&nd->rb_node); free(nd); @@ -682,11 +683,10 @@ static struct thread *____machine__findnew_thread(str= uct machine *machine, /* * It is now in the rbtree, get a ref */ - thread__get(th); threads__set_last_match(threads, th); ++threads->nr; =20 - return th; + return thread__get(th); } =20 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pi= d, pid_t tid) @@ -2321,7 +2321,7 @@ static int add_callchain_ip(struct thread *thread, struct iterations *iter, u64 branch_from) { - struct map_symbol ms; + struct map_symbol ms =3D {}; struct addr_location al; int nr_loop_iter =3D 0, err =3D 0; u64 iter_cycles =3D 0; @@ -2395,6 +2395,8 @@ static int add_callchain_ip(struct thread *thread, iter_cycles, branch_from, srcline); out: addr_location__exit(&al); + maps__put(ms.maps); + map__put(ms.map); return err; } =20 @@ -3089,6 +3091,7 @@ static int append_inlines(struct callchain_cursor *cu= rsor, struct map_symbol *ms struct dso *dso; u64 addr; int ret =3D 1; + struct map_symbol ilist_ms; =20 if (!symbol_conf.inline_name || !map || !sym) return ret; @@ -3105,18 +3108,20 @@ static int append_inlines(struct callchain_cursor *= cursor, struct map_symbol *ms inlines__tree_insert(&dso->inlined_nodes, inline_node); } =20 + ilist_ms =3D (struct map_symbol) { + .maps =3D maps__get(ms->maps), + .map =3D map__get(map), + }; list_for_each_entry(ilist, &inline_node->val, list) { - struct map_symbol ilist_ms =3D { - .maps =3D ms->maps, - .map =3D map, - .sym =3D ilist->symbol, - }; + ilist_ms.sym =3D ilist->symbol; ret =3D callchain_cursor_append(cursor, ip, &ilist_ms, false, NULL, 0, 0, 0, ilist->srcline); =20 if (ret !=3D 0) return ret; } + map__put(ilist_ms.map); + maps__put(ilist_ms.maps); =20 return ret; } --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 64F21C7EE23 for ; Thu, 8 Jun 2023 23:31:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237408AbjFHXb3 (ORCPT ); Thu, 8 Jun 2023 19:31:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237235AbjFHXai (ORCPT ); Thu, 8 Jun 2023 19:30:38 -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 9753730D6 for ; Thu, 8 Jun 2023 16:30:04 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bac6a453dd5so1656157276.2 for ; Thu, 08 Jun 2023 16:30:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686267004; x=1688859004; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=/XZkwenxaaHVyn1hVE7P7MuCL0PTGSRSdV23z9MHnOc=; b=75tmqSXX65bSoRA5vlcyAF43ZjhIM0r5BM3xBN28av2EJVL53fBWeBvuFQ0ohIFV41 V3e/SfmjxZDxFllZWY+DfzkPwq5EuwupokhRmWxhsB+cabmeNVtyY+rArxf9IpdCeymO VjRW1RuE/6ehKGlfxe+QEbCmweVU8ffBiyN/2T/IzUjUVf8kKGxl4/CPvzmG6TALsD37 scAps/eFy6y1VLMpZ7G0O1AbiCez+FkMnTroSZQ/C6xlu2nYF28x/1tRUWH0MH+nJmqp 90NcTyaFWyB7rJgedrkuHPCs1hLxw0udDBCYzMnxUVICERgPpbmS7u4kcIaE4MMnEoPU R+sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686267004; x=1688859004; h=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=/XZkwenxaaHVyn1hVE7P7MuCL0PTGSRSdV23z9MHnOc=; b=GLpPbcMrPG4vsq+6o3GBb8PEDV5CmWVwqXszzu27AKZ0RcXkqeT3yADTIrJlV+wsBb XwlImkwGhtSJWa2qb2i7Rk1ITRLhBlgSuaykj3mBIHFehFkjP1XJ0m9oD/FjDN4FyiVS hjs7XUZ56bHbJDwTF0viysFzn71uzBi4TjewaMd/SbEOJKsENWGYDPEjXlnNj//DyYjT BnItYWv9oyxgSARYmSr87tX8cpyj3U8EpmyT68yDAQqOpm+nkO4cqn0URyuWoyThmhUa 9NA7KDB5trGq5o/SEW3Oxe3J9Q1zL3+Sa033c84H+uB8purB+OuIQ2GW2BkhLNgaiSNC Kj0Q== X-Gm-Message-State: AC+VfDzQVK/u/b08c0ZtOyOYfmCAuQigyPxwukqH6l/MOrHdGo4X2L0K 3KwI2DESSjENHK6j9gvZWRA1l+GURMOg X-Google-Smtp-Source: ACHHUZ4p6MK7UPxQ9Y6RWB+kMCGrMnATf9tWGxq4NXy8eTZomZQ5exYqGqOKFmxU9NXUDopUSDfZylb4u6F2 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a5b:784:0:b0:bad:99d:f087 with SMTP id b4-20020a5b0784000000b00bad099df087mr609214ybq.6.1686267003834; Thu, 08 Jun 2023 16:30:03 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:18 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-22-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 21/26] perf annotate: Fix parse_objdump_line memory leak From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" fileloc is used to hold a previous line, before overwriting it ensure the previous contents is freed. Free the storage once done in symbol__disassemble. Signed-off-by: Ian Rogers --- tools/perf/util/annotate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index b708bbc49c9e..fc5f44535ebe 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1524,6 +1524,7 @@ static int symbol__parse_objdump_line(struct symbol *= sym, /* /filename:linenr ? Save line number and ignore. */ if (regexec(&file_lineno, parsed_line, 2, match, 0) =3D=3D 0) { *line_nr =3D atoi(parsed_line + match[1].rm_so); + free(*fileloc); *fileloc =3D strdup(parsed_line); return 0; } @@ -1572,7 +1573,6 @@ static int symbol__parse_objdump_line(struct symbol *= sym, } =20 annotation_line__add(&dl->al, ¬es->src->source); - return 0; } =20 @@ -2114,6 +2114,7 @@ static int symbol__disassemble(struct symbol *sym, st= ruct annotate_args *args) nline++; } free(line); + free(fileloc); =20 err =3D finish_command(&objdump_process); if (err) --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 84149C7EE23 for ; Thu, 8 Jun 2023 23:31:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237432AbjFHXbm (ORCPT ); Thu, 8 Jun 2023 19:31:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237597AbjFHXan (ORCPT ); Thu, 8 Jun 2023 19:30:43 -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 025D530D7 for ; Thu, 8 Jun 2023 16:30:06 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5655d99da53so25396257b3.0 for ; Thu, 08 Jun 2023 16:30:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686267006; x=1688859006; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=Pl84Wb/J0XwyUQzqil2jyo7Ij9NgOb19MY5gzo850pI=; b=xnSakeo5Yb7wwdtDMtyb7HVmj8YIuug35kGbTZONK5jWOa3frI8ut/fr93Fc8/JTwX kvPVpJEjW0sAh9sgPXMO7hyOJi/3vdDxkAi+slwHAwCqrDu0YARCS7yvVlRIkeo5nA06 C15nsVuVNrmKDz9sWZFnqATC9K8cVorJcxIOokWO13z2/1LDCTGY/s1RQBTMMhreyVgg 83aqZxGUcA1c/VNd055oUlTQF1Lva5eiYUU+EMHdqdWbR/KXkbdHAGtQFr9pIqdCSeqs LmvvrblYw88CmiAOK+DgWcGAsQbyYkW6/SP95piwYMv/4lRiHtmDhC7d3H3k9+1MYT+n Mfsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686267006; x=1688859006; h=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=Pl84Wb/J0XwyUQzqil2jyo7Ij9NgOb19MY5gzo850pI=; b=Q/iplsoqKVgrJGsiEkyQb3VlkoSjWFEAGUhEPGLJCBBMyfIv0Fmp0ZEv3uIcFoFNVs 2QUhwpSBpY/BqgAtKBAp7tCMiXt9DBNPeSY1WKD0/UZg/SM69OcPR4KI1H8ILnycfF30 uN+ep2khtQQmQK9a/XJ418bB+RrYun48OMa891qYExHuHou9+VgmDIwIf+6k1iLsXw22 BAP3MdgqGxvxba8QKNJP6ywbYG4+B6gQzDtI8wYyP7uyvqMumoQ27gob1E985LoXLeTS piojhZtOd7rljcOJn0fABXviVmeSyO31Tpw/5EXGnZ7uhA7nVK2/r7ErPi3AC+vwoVVe 7/EA== X-Gm-Message-State: AC+VfDyFuZkxDtppWuVH6Ils9Jw7mHSTVIl1Fg4RtUT/7eUVS9U9DCtQ L7bPnNV9sPU4nM0S7hb1FTfvv8gwsiI0 X-Google-Smtp-Source: ACHHUZ6XZQubQMzg154P6DYDd+lHosdgRP3GkxT70tkfupa8BerI7CJLgpzGAucDVJ4zIZScOONMhWdG/Fqr X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a05:690c:2e0f:b0:569:ea0e:b450 with SMTP id et15-20020a05690c2e0f00b00569ea0eb450mr590468ywb.5.1686267005998; Thu, 08 Jun 2023 16:30:05 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:19 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-23-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 22/26] perf top: Add exit routine for main thread From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org 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 exit_process_thread that reverses init_process_thread. This avoids leak sanitizer reporting memory leaks. Signed-off-by: Ian Rogers --- tools/perf/builtin-top.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 99010dfa5760..c363c04e16df 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -392,7 +392,7 @@ static void prompt_percent(int *target, const char *msg) =20 static void perf_top__prompt_symbol(struct perf_top *top, const char *msg) { - char *buf =3D malloc(0), *p; + char *buf =3D NULL, *p; struct hist_entry *syme =3D top->sym_filter_entry, *n, *found =3D NULL; struct hists *hists =3D evsel__hists(top->sym_evsel); struct rb_node *next; @@ -1227,6 +1227,14 @@ static void init_process_thread(struct perf_top *top) cond_init(&top->qe.cond); } =20 +static void exit_process_thread(struct perf_top *top) +{ + ordered_events__free(&top->qe.data[0]); + ordered_events__free(&top->qe.data[1]); + mutex_destroy(&top->qe.mutex); + cond_destroy(&top->qe.cond); +} + static int __cmd_top(struct perf_top *top) { struct record_opts *opts =3D &top->record_opts; @@ -1357,6 +1365,7 @@ static int __cmd_top(struct perf_top *top) cond_signal(&top->qe.cond); pthread_join(thread_process, NULL); perf_set_singlethreaded(); + exit_process_thread(top); return ret; } =20 --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 89002C7EE25 for ; Thu, 8 Jun 2023 23:31:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237639AbjFHXbg (ORCPT ); Thu, 8 Jun 2023 19:31:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237350AbjFHXao (ORCPT ); Thu, 8 Jun 2023 19:30:44 -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 0C35E35A3 for ; Thu, 8 Jun 2023 16:30:09 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-568fa455b8fso15305717b3.3 for ; Thu, 08 Jun 2023 16:30:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686267008; x=1688859008; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=N0KYgNGPVbef0yxFQzrQIBdU6DpVCEwayIrlyOXU5ig=; b=2tVEOfRMr9BaVlInmIrt6A8s/2SaJVEmRq0V2ofDi/ugL0FqWxUFVaSl7oU/tIkKNr iyYTqNN6gaPCB6BDG4Oar1eMUgEJzu1pJfmbSSAH9QS6oDqzi3hP3T3BIaaDXdYGtylM fslrT25m/eatU6xm7/7y2HZJh13Mp1sDozMho5OtVtalzcgWIZ3XV36U4WJJKsgxsaJM zBvMGzzY+BgMcHash2dFDF0vYoz3jRmw1vSdOAGPhbOly7L0MM94sh6yjpCMMdW1LX4+ UrgKwN0Y2LcUS3wa/aKLU2Q4oZzn3p68Qr9hudfcH23XckDQYZnScPNcvMfzPW+NzpDe I8bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686267008; x=1688859008; h=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=N0KYgNGPVbef0yxFQzrQIBdU6DpVCEwayIrlyOXU5ig=; b=WaO3DggsotZrjNksvgCpf11NzIpv1x+Pi7Vhk5pAujAUXiIjyDMLXfLd/y3jI0vVjq sr780WQ2ZhE6vLcJtZlGeo8GZW8sMRBC3PSQkWyAxISCpbIyLXarUQuvWfd23bsfdfJm DMOJ7bbyW9834oG+lBqLK9ep5lr0SNtM5RiyMS32j/x/sxXADxQKqTvnO+49+KQxrivc z171XIsAw0Qof3mEDLphENZzHikFSmXgwdVG2OUO1NGbnDnlJj3xlzzYmjvl816sj11n u86luNdFX3tJmAnWdBeKCguDdwT/PL894GhaM0jU/t/3Uf0r7IKmzKGBH6/OnPWJ0R5t VqSQ== X-Gm-Message-State: AC+VfDwzENaksz8mK0MI3/SYsLMC7Sgbji6F8MUbIm8Bqgic9s1dcP05 YN3+LZx/+sjLYsm8Bvqo+eYabmIO8J/y X-Google-Smtp-Source: ACHHUZ4xbu2nLPZfhvS6QmOV47vNfVRrhKEnpGCx8uSLcUdi8ahGG+FywCSAEA5a+AEVc8L9iatZo1ygXjai X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a81:ac43:0:b0:568:9bcc:5e16 with SMTP id z3-20020a81ac43000000b005689bcc5e16mr629708ywj.2.1686267008171; Thu, 08 Jun 2023 16:30:08 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:20 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-24-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 23/26] perf header: Avoid out-of-bounds read From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" intel-pt tests were failing: ``` ... --- Test virtual LBR --- Linux [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.126 MB /tmp/perf-test-intel-pt-sh.FW57C= XnCqQ/test-perf.data ] Failed with virtual lbr ... ``` The root cause is an out-of-bounds read in header (where maxbrstack.py is from test_intel_pt.sh): ``` $ perf --no-pager script --itrace=3DL -s maxbrstack.py =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D3907930=3D=3DERROR: AddressSanitizer: heap-buffer-overflow on address= 0x6020000095a8 at pc 0x563c26c840bb bp 0x7fff43582710 sp 0x7fff43582708 READ of size 4 at 0x6020000095a8 thread T0 #0 0x563c26c840ba in process_group_desc util/header.c:2847 #1 0x563c26c8bc78 in perf_file_section__process util/header.c:4037 #2 0x563c26c8aa9b in perf_header__process_sections util/header.c:3813 #3 0x563c26c8d028 in perf_session__read_header util/header.c:4286 #4 0x563c26cbab29 in perf_session__open util/session.c:113 #5 0x563c26cbb3d0 in __perf_session__new util/session.c:221 #6 0x563c26aacb14 in perf_session__new util/session.h:73 #7 0x563c26acf7f1 in cmd_script tools/perf/builtin-script.c:4212 #8 0x563c26bb58ff in run_builtin tools/perf/perf.c:323 #9 0x563c26bb5e70 in handle_internal_command tools/perf/perf.c:377 #10 0x563c26bb6238 in run_argv tools/perf/perf.c:421 #11 0x563c26bb67a0 in main tools/perf/perf.c:537 #12 0x7f34bde46189 in __libc_start_call_main ../sysdeps/nptl/libc_start= _call_main.h:58 #13 0x7f34bde46244 in __libc_start_main_impl ../csu/libc-start.c:381 #14 0x563c26a33390 in _start (/tmp/perf/perf+0x1eb390) 0x6020000095a8 is located 8 bytes to the right of 16-byte region [0x6020000= 09590,0x6020000095a0) allocated by thread T0 here: #0 0x7f34beeb83b7 in __interceptor_calloc ../../../../src/libsanitizer/= asan/asan_malloc_linux.cpp:77 #1 0x563c26c83df8 in process_group_desc util/header.c:2824 #2 0x563c26c8bc78 in perf_file_section__process util/header.c:4037 #3 0x563c26c8aa9b in perf_header__process_sections util/header.c:3813 #4 0x563c26c8d028 in perf_session__read_header util/header.c:4286 #5 0x563c26cbab29 in perf_session__open util/session.c:113 #6 0x563c26cbb3d0 in __perf_session__new util/session.c:221 #7 0x563c26aacb14 in perf_session__new util/session.h:73 #8 0x563c26acf7f1 in cmd_script tools/perf/builtin-script.c:4212 #9 0x563c26bb58ff in run_builtin tools/perf/perf.c:323 #10 0x563c26bb5e70 in handle_internal_command tools/perf/perf.c:377 #11 0x563c26bb6238 in run_argv tools/perf/perf.c:421 #12 0x563c26bb67a0 in main tools/perf/perf.c:537 #13 0x7f34bde46189 in __libc_start_call_main ../sysdeps/nptl/libc_start= _call_main.h:58 ``` Avoid the out-of-bounds read checking for the leader. Leave the 'nr' check intact as nr will be 0 or the counting down and evsel be a group member. Signed-off-by: Ian Rogers --- tools/perf/util/header.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 3db7c1fae71e..52fbf526fe74 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -2844,7 +2844,7 @@ static int process_group_desc(struct feat_fd *ff, voi= d *data __maybe_unused) =20 i =3D nr =3D 0; evlist__for_each_entry(session->evlist, evsel) { - if (evsel->core.idx =3D=3D (int) desc[i].leader_idx) { + if (i < nr_groups && evsel->core.idx =3D=3D (int) desc[i].leader_idx) { evsel__set_leader(evsel, evsel); /* {anon_group} is a dummy name */ if (strcmp(desc[i].name, "{anon_group}")) { --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 56F0CC7EE45 for ; Thu, 8 Jun 2023 23:31:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237478AbjFHXbq (ORCPT ); Thu, 8 Jun 2023 19:31:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237652AbjFHXa7 (ORCPT ); Thu, 8 Jun 2023 19:30:59 -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 28EB73A97 for ; Thu, 8 Jun 2023 16:30:12 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5618857518dso15406307b3.2 for ; Thu, 08 Jun 2023 16:30:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686267011; x=1688859011; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=Fdzksl/kkftTB43GevdApw1PAO/LnxMdJnRBtlfxhEE=; b=jUUrlAd0jFtx3gT8HHmiIn6ov/tGeo1Py3GP8BHc6orX8wxf2GSx0d3z64vOZamkLl l/Rn2B157YaRbKvWQKYRUxyu9DF/kxOScXlwbieTmGChV7a9VFKNzVv5yqmgtjZOZLvI EIgCMRTqv33YJZNKnwQEDnI7vuoO7RVCt3t3Crrp2tDmTVse1oCbPFF7ztH06ZD7nTWM QNbHvW0h1qNsM012dkqb6aTN0M6Ht/kvOC1mhjtakrw7ep9PlK44Jazk2Hyl4HsUB2t/ /4rthPvbW9t9Rc/T7jAdig/pzIkyWS4B7eM4VfPrdD2U0HGx/Jn8/yzoWwqjtS9ggVmP nXOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686267011; x=1688859011; h=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=Fdzksl/kkftTB43GevdApw1PAO/LnxMdJnRBtlfxhEE=; b=RG3Es0no6C4UA8bK2xDnE0k5lGpIQisxsRWSzuUhfOj7ahKWBz2mblet6qWacI2QZK OlcYVnAbEnyaEhmTeBPuw+Ok/7kjEw2BYbX9uQ5BMaILwLOX0zMh6TEjw0fSDZPpdPmk a8CPuMUenAuUT5T13vpdwxZyqWDrcTbq66j//vRwvB5D7ykNkuifC07etVDfbcvl/+GD nOisMhiW9IaAMJoT8agq6BZ0wVsGHmzead3ddpsF54v0JQ3WN6gUI+cO6zt/B1YhftaI nno1u4wiBxn40HAKkGRk6E/2mkjukgvJ7eAHAKJ7U8mHxPsWmrk38dJdWfdI2stK1Jnd 1RxA== X-Gm-Message-State: AC+VfDzUvZ5wjh/wLA6k11PS6S2DKJQ3z/SPkD71E8Xckw4l9OXkMl/X SxKMS7s+WPRLNYXeYbwqz6JAYk0LczSn X-Google-Smtp-Source: ACHHUZ5g5uyVZlOsr7pjdyZV6VH+phjABBlaux+DG9mdlabFHUh/Yg+4aAt5onkwPIjHgENJ5dKopd2fJFO0 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a81:bc4e:0:b0:569:5003:7d79 with SMTP id b14-20020a81bc4e000000b0056950037d79mr704640ywl.3.1686267011178; Thu, 08 Jun 2023 16:30:11 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:21 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-25-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 24/26] perf callchain: Use pthread keys for tls callchain_cursor From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Pthread keys are more portable than __thread and allow the association of a destructor with the key. Use the destructor to clean up TLS callchain cursors to aid understanding memory leaks. Signed-off-by: Ian Rogers --- tools/perf/builtin-c2c.c | 4 +- tools/perf/builtin-script.c | 24 ++++++----- tools/perf/util/callchain.c | 40 ++++++++++++++++++- tools/perf/util/callchain.h | 4 +- tools/perf/util/db-export.c | 10 +++-- tools/perf/util/hist.c | 29 ++++++++------ .../scripting-engines/trace-event-python.c | 10 +++-- 7 files changed, 86 insertions(+), 35 deletions(-) diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c index 530a44a59f41..a4cf9de7a7b5 100644 --- a/tools/perf/builtin-c2c.c +++ b/tools/perf/builtin-c2c.c @@ -284,6 +284,7 @@ static int process_sample_event(struct perf_tool *tool = __maybe_unused, struct hist_entry *he; struct addr_location al; struct mem_info *mi, *mi_dup; + struct callchain_cursor *cursor; int ret; =20 addr_location__init(&al); @@ -297,7 +298,8 @@ static int process_sample_event(struct perf_tool *tool = __maybe_unused, if (c2c.stitch_lbr) thread__set_lbr_stitch_enable(al.thread, true); =20 - ret =3D sample__resolve_callchain(sample, &callchain_cursor, NULL, + cursor =3D get_tls_callchain_cursor(); + ret =3D sample__resolve_callchain(sample, cursor, NULL, evsel, &al, sysctl_perf_event_max_stack); if (ret) goto out; diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 784d478c2e05..e3f435e6a7d0 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -1557,11 +1557,13 @@ static int perf_sample__fprintf_bts(struct perf_sam= ple *sample, unsigned int print_opts =3D output[type].print_ip_opts; struct callchain_cursor *cursor =3D NULL; =20 - if (symbol_conf.use_callchain && sample->callchain && - thread__resolve_callchain(al->thread, &callchain_cursor, evsel, - sample, NULL, NULL, scripting_max_stack) =3D=3D 0) - cursor =3D &callchain_cursor; - + if (symbol_conf.use_callchain && sample->callchain) { + cursor =3D get_tls_callchain_cursor(); + if (thread__resolve_callchain(al->thread, cursor, evsel, + sample, NULL, NULL, + scripting_max_stack)) + cursor =3D NULL; + } if (cursor =3D=3D NULL) { printed +=3D fprintf(fp, " "); if (print_opts & EVSEL__PRINT_SRCLINE) { @@ -2203,11 +2205,13 @@ static void process_event(struct perf_script *scrip= t, if (script->stitch_lbr) thread__set_lbr_stitch_enable(al->thread, true); =20 - if (symbol_conf.use_callchain && sample->callchain && - thread__resolve_callchain(al->thread, &callchain_cursor, evsel, - sample, NULL, NULL, scripting_max_stack) =3D=3D 0) - cursor =3D &callchain_cursor; - + if (symbol_conf.use_callchain && sample->callchain) { + cursor =3D get_tls_callchain_cursor(); + if (thread__resolve_callchain(al->thread, cursor, evsel, + sample, NULL, NULL, + scripting_max_stack)) + cursor =3D NULL; + } fputc(cursor ? '\n' : ' ', fp); sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, symbol_conf.bt_stop_list, fp); diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index 909f62b3b266..7e9bd3b6be9f 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -58,7 +58,8 @@ struct callchain_param callchain_param_default =3D { CALLCHAIN_PARAM_DEFAULT }; =20 -__thread struct callchain_cursor callchain_cursor; +/* Used for thread-local struct callchain_cursor. */ +static pthread_key_t callchain_cursor; =20 int parse_callchain_record_opt(const char *arg, struct callchain_param *pa= ram) { @@ -1116,7 +1117,7 @@ int hist_entry__append_callchain(struct hist_entry *h= e, struct perf_sample *samp if ((!symbol_conf.use_callchain || sample->callchain =3D=3D NULL) && !symbol_conf.show_branchflag_count) return 0; - return callchain_append(he->callchain, &callchain_cursor, sample->period); + return callchain_append(he->callchain, get_tls_callchain_cursor(), sample= ->period); } =20 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_= node *node, @@ -1570,6 +1571,41 @@ int callchain_node__make_parent_list(struct callchai= n_node *node) return -ENOMEM; } =20 +static void callchain_cursor__delete(void *vcursor) +{ + struct callchain_cursor *cursor =3D vcursor; + struct callchain_cursor_node *node, *next; + + callchain_cursor_reset(cursor); + for (node =3D cursor->first; node !=3D NULL; node =3D next) { + next =3D node->next; + free(node); + } + free(cursor); +} + +static void init_callchain_cursor_key(void) +{ + if (pthread_key_create(&callchain_cursor, callchain_cursor__delete)) { + pr_err("callchain cursor creation failed"); + abort(); + } +} + +struct callchain_cursor *get_tls_callchain_cursor(void) +{ + static pthread_once_t once_control =3D PTHREAD_ONCE_INIT; + struct callchain_cursor *cursor; + + pthread_once(&once_control, init_callchain_cursor_key); + cursor =3D pthread_getspecific(callchain_cursor); + if (!cursor) { + cursor =3D zalloc(sizeof(*cursor)); + pthread_setspecific(callchain_cursor, cursor); + } + return cursor; +} + int callchain_cursor__copy(struct callchain_cursor *dst, struct callchain_cursor *src) { diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h index d95615daed73..ce9410018cf7 100644 --- a/tools/perf/util/callchain.h +++ b/tools/perf/util/callchain.h @@ -168,8 +168,6 @@ struct callchain_cursor { struct callchain_cursor_node *curr; }; =20 -extern __thread struct callchain_cursor callchain_cursor; - static inline void callchain_init(struct callchain_root *root) { INIT_LIST_HEAD(&root->node.val); @@ -231,6 +229,8 @@ static inline void callchain_cursor_advance(struct call= chain_cursor *cursor) cursor->pos++; } =20 +struct callchain_cursor *get_tls_callchain_cursor(void); + int callchain_cursor__copy(struct callchain_cursor *dst, struct callchain_cursor *src); =20 diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c index 6184696dc266..b9fb71ab7a73 100644 --- a/tools/perf/util/db-export.c +++ b/tools/perf/util/db-export.c @@ -215,6 +215,7 @@ static struct call_path *call_path_from_sample(struct d= b_export *dbe, u64 kernel_start =3D machine__kernel_start(machine); struct call_path *current =3D &dbe->cpr->call_path; enum chain_order saved_order =3D callchain_param.order; + struct callchain_cursor *cursor; int err; =20 if (!symbol_conf.use_callchain || !sample->callchain) @@ -226,13 +227,14 @@ static struct call_path *call_path_from_sample(struct= db_export *dbe, * the callchain starting with the root node and ending with the leaf. */ callchain_param.order =3D ORDER_CALLER; - err =3D thread__resolve_callchain(thread, &callchain_cursor, evsel, + cursor =3D get_tls_callchain_cursor(); + err =3D thread__resolve_callchain(thread, cursor, evsel, sample, NULL, NULL, PERF_MAX_STACK_DEPTH); if (err) { callchain_param.order =3D saved_order; return NULL; } - callchain_cursor_commit(&callchain_cursor); + callchain_cursor_commit(cursor); =20 while (1) { struct callchain_cursor_node *node; @@ -240,7 +242,7 @@ static struct call_path *call_path_from_sample(struct d= b_export *dbe, u64 dso_db_id =3D 0, sym_db_id =3D 0, offset =3D 0; =20 =20 - node =3D callchain_cursor_current(&callchain_cursor); + node =3D callchain_cursor_current(cursor); if (!node) break; =20 @@ -265,7 +267,7 @@ static struct call_path *call_path_from_sample(struct d= b_export *dbe, al.sym, node->ip, kernel_start); =20 - callchain_cursor_advance(&callchain_cursor); + callchain_cursor_advance(cursor); addr_location__exit(&al); } =20 diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index fb218b3e8a7c..4004c0915e4f 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -1029,15 +1029,16 @@ iter_prepare_cumulative_entry(struct hist_entry_ite= r *iter, struct addr_location *al __maybe_unused) { struct hist_entry **he_cache; + struct callchain_cursor *cursor =3D get_tls_callchain_cursor(); =20 - callchain_cursor_commit(&callchain_cursor); + callchain_cursor_commit(cursor); =20 /* * This is for detecting cycles or recursions so that they're * cumulated only one time to prevent entries more than 100% * overhead. */ - he_cache =3D malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1)); + he_cache =3D malloc(sizeof(*he_cache) * (cursor->nr + 1)); if (he_cache =3D=3D NULL) return -ENOMEM; =20 @@ -1072,7 +1073,7 @@ iter_add_single_cumulative_entry(struct hist_entry_it= er *iter, * We need to re-initialize the cursor since callchain_append() * advanced the cursor to the end. */ - callchain_cursor_commit(&callchain_cursor); + callchain_cursor_commit(get_tls_callchain_cursor()); =20 hists__inc_nr_samples(hists, he->filtered); =20 @@ -1085,7 +1086,7 @@ iter_next_cumulative_entry(struct hist_entry_iter *it= er, { struct callchain_cursor_node *node; =20 - node =3D callchain_cursor_current(&callchain_cursor); + node =3D callchain_cursor_current(get_tls_callchain_cursor()); if (node =3D=3D NULL) return 0; =20 @@ -1131,12 +1132,12 @@ iter_add_next_cumulative_entry(struct hist_entry_it= er *iter, .raw_size =3D sample->raw_size, }; int i; - struct callchain_cursor cursor; + struct callchain_cursor cursor, *tls_cursor =3D get_tls_callchain_cursor(= ); bool fast =3D hists__has(he_tmp.hists, sym); =20 - callchain_cursor_snapshot(&cursor, &callchain_cursor); + callchain_cursor_snapshot(&cursor, tls_cursor); =20 - callchain_cursor_advance(&callchain_cursor); + callchain_cursor_advance(tls_cursor); =20 /* * Check if there's duplicate entries in the callchain. @@ -1222,7 +1223,7 @@ int hist_entry_iter__add(struct hist_entry_iter *iter= , struct addr_location *al, if (al) alm =3D map__get(al->map); =20 - err =3D sample__resolve_callchain(iter->sample, &callchain_cursor, &iter-= >parent, + err =3D sample__resolve_callchain(iter->sample, get_tls_callchain_cursor(= ), &iter->parent, iter->evsel, al, max_stack_depth); if (err) { map__put(alm); @@ -1568,8 +1569,10 @@ static int hists__hierarchy_insert_entry(struct hist= s *hists, =20 if (hist_entry__has_callchains(new_he) && symbol_conf.use_callchain) { - callchain_cursor_reset(&callchain_cursor); - if (callchain_merge(&callchain_cursor, + struct callchain_cursor *cursor =3D get_tls_callchain_cursor(); + + callchain_cursor_reset(cursor); + if (callchain_merge(cursor, new_he->callchain, he->callchain) < 0) ret =3D -1; @@ -1610,8 +1613,10 @@ static int hists__collapse_insert_entry(struct hists= *hists, he_stat__add_stat(iter->stat_acc, he->stat_acc); =20 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) { - callchain_cursor_reset(&callchain_cursor); - if (callchain_merge(&callchain_cursor, + struct callchain_cursor *cursor =3D get_tls_callchain_cursor(); + + callchain_cursor_reset(cursor); + if (callchain_merge(cursor, iter->callchain, he->callchain) < 0) ret =3D -1; diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index d96e5c0fef45..59063ec98619 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -417,6 +417,7 @@ static PyObject *python_process_callchain(struct perf_s= ample *sample, struct addr_location *al) { PyObject *pylist; + struct callchain_cursor *cursor; =20 pylist =3D PyList_New(0); if (!pylist) @@ -425,19 +426,20 @@ static PyObject *python_process_callchain(struct perf= _sample *sample, if (!symbol_conf.use_callchain || !sample->callchain) goto exit; =20 - if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel, + cursor =3D get_tls_callchain_cursor(); + if (thread__resolve_callchain(al->thread, cursor, evsel, sample, NULL, NULL, scripting_max_stack) !=3D 0) { pr_err("Failed to resolve callchain. Skipping\n"); goto exit; } - callchain_cursor_commit(&callchain_cursor); + callchain_cursor_commit(cursor); =20 =20 while (1) { PyObject *pyelem; struct callchain_cursor_node *node; - node =3D callchain_cursor_current(&callchain_cursor); + node =3D callchain_cursor_current(cursor); if (!node) break; =20 @@ -493,7 +495,7 @@ static PyObject *python_process_callchain(struct perf_s= ample *sample, _PyUnicode_FromString(dsoname)); } =20 - callchain_cursor_advance(&callchain_cursor); + callchain_cursor_advance(cursor); PyList_Append(pylist, pyelem); Py_DECREF(pyelem); } --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 5E470C7EE25 for ; Thu, 8 Jun 2023 23:31:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237700AbjFHXbv (ORCPT ); Thu, 8 Jun 2023 19:31:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237664AbjFHXbB (ORCPT ); Thu, 8 Jun 2023 19:31:01 -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 B48623A81 for ; Thu, 8 Jun 2023 16:30:18 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-561ceb5b584so16142307b3.3 for ; Thu, 08 Jun 2023 16:30:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686267013; x=1688859013; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ivPelAwC3y4XSVWu/g/woCSsh93mZn6kVLmUE8y8n1c=; b=o/8C7MEPCtvtxe0KTNOp2hvuO21OtSyIqnCUGXCxoDBVZ1hM0UTDfOuLLu9wmXoDgU NupUf/lsgNze0GEP6mGhpeYIvSgXIly86oh9z2Em05rAYNrvq4FCzwyKmNFSaIShua41 3weNIDbvhelyAr+f3kRdO8Kq/ZZkpbKIZI84VAmPVqEsnlWzKcNR4rEVotPszMSNakud XJQHVny/tO6wTL5odn0Z71wVTEZQYhdd7EYfW3ZZqN05eddIHrVyDKXC7K5eGqyv8S1W IJV79rf3qBoAK0LtD2+roC7xthkMZNkAG9KPPjPL2yN6WYDofyTHc6lW29zb02e60YCU aADg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686267013; x=1688859013; h=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=ivPelAwC3y4XSVWu/g/woCSsh93mZn6kVLmUE8y8n1c=; b=W3XnYYZSkWR2BJPN4rnFVxXkpfm8EQA1L9UOOTnezfDiYEUSLi41GAZWvLfyZYlLGW 5QDaq+IALCT1e7b94983g+MdtrBcd6IkNAThUjkDVlJZa9hFen8ApdPggtlWRBSHKa/e TCyn2+SSZCWgj0m18HG6LYBklkdO/aiQ2cpn3wu7RWbtO19p1Joj9lCgEi4ukvvRC8o/ BJQc+BoPJOtWox/DItKEdAFkCPxLbqxHD3kWv41Nu4aD7NOxgrgAnfFl6a4YaJKvaURQ KNYi4hHqsR6nhz5VWaHbDaygHwxgKIFIePAhDpHJbiVYzCq3HgeaELOJgn4uoiQ3AclF sl6w== X-Gm-Message-State: AC+VfDyT+W0PeMUP3UT8xsksLDlC3narHFFmQxjFair2yGHJNSXj38yy 73kdXxzxJLaJnd5j8/Njs/EuAFk2OXNx X-Google-Smtp-Source: ACHHUZ62wofB/jabYpP+aum/UbQ9ZFFxwtAKK2GM4KUJZ12ge6l2beqKO3P18e/K5xM1ekEKjDs0EnsTUyPv X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a81:ac4b:0:b0:565:c0e3:d2d7 with SMTP id z11-20020a81ac4b000000b00565c0e3d2d7mr697104ywj.1.1686267013537; Thu, 08 Jun 2023 16:30:13 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:22 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-26-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 25/26] perf srcline: Change free_srcline to zfree_srcline From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Make use after free more unlikely. Signed-off-by: Ian Rogers --- tools/perf/builtin-diff.c | 4 ++-- tools/perf/util/annotate.c | 2 +- tools/perf/util/block-info.c | 4 ++-- tools/perf/util/hist.c | 6 +++--- tools/perf/util/map.c | 2 +- tools/perf/util/srcline.c | 15 ++++++++++----- tools/perf/util/srcline.h | 2 +- 7 files changed, 20 insertions(+), 15 deletions(-) diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c index ca39657ee407..eec89567ae48 100644 --- a/tools/perf/builtin-diff.c +++ b/tools/perf/builtin-diff.c @@ -1387,8 +1387,8 @@ static int cycles_printf(struct hist_entry *he, struc= t hist_entry *pair, bi->start, bi->end, block_he->diff.cycles); } =20 - free_srcline(start_line); - free_srcline(end_line); + zfree_srcline(&start_line); + zfree_srcline(&end_line); =20 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf); } diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index fc5f44535ebe..58fc5fa00ecd 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1196,7 +1196,7 @@ static void annotation_line__init(struct annotation_l= ine *al, =20 static void annotation_line__exit(struct annotation_line *al) { - free_srcline(al->path); + zfree_srcline(&al->path); zfree(&al->line); } =20 diff --git a/tools/perf/util/block-info.c b/tools/perf/util/block-info.c index 16a7b4adcf18..08279b1b65e5 100644 --- a/tools/perf/util/block-info.c +++ b/tools/perf/util/block-info.c @@ -305,8 +305,8 @@ static int block_range_entry(struct perf_hpp_fmt *fmt, = struct perf_hpp *hpp, bi->start, bi->end); } =20 - free_srcline(start_line); - free_srcline(end_line); + zfree_srcline(&start_line); + zfree_srcline(&end_line); =20 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf); } diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 4004c0915e4f..77cb2cc83bb9 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -1317,8 +1317,8 @@ void hist_entry__delete(struct hist_entry *he) if (he->branch_info) { map__zput(he->branch_info->from.ms.map); map__zput(he->branch_info->to.ms.map); - free_srcline(he->branch_info->srcline_from); - free_srcline(he->branch_info->srcline_to); + zfree_srcline(&he->branch_info->srcline_from); + zfree_srcline(&he->branch_info->srcline_to); zfree(&he->branch_info); } =20 @@ -1336,7 +1336,7 @@ void hist_entry__delete(struct hist_entry *he) =20 zfree(&he->res_samples); zfree(&he->stat_acc); - free_srcline(he->srcline); + zfree_srcline(&he->srcline); if (he->srcfile && he->srcfile[0]) zfree(&he->srcfile); free_callchain(he->callchain); diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index ae1d54d4880a..c77e2fce6a37 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -498,7 +498,7 @@ int map__fprintf_srcline(struct map *map, u64 addr, con= st char *prefix, 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); - free_srcline(srcline); + zfree_srcline(&srcline); } return ret; } diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c index cfca03abd6f8..b8e596528d7e 100644 --- a/tools/perf/util/srcline.c +++ b/tools/perf/util/srcline.c @@ -804,10 +804,15 @@ char *get_srcline_split(struct dso *dso, u64 addr, un= signed *line) return NULL; } =20 -void free_srcline(char *srcline) +void zfree_srcline(char **srcline) { - if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) !=3D 0) - free(srcline); + if (*srcline =3D=3D NULL) + return; + + if (strcmp(*srcline, SRCLINE_UNKNOWN)) + free(*srcline); + + *srcline =3D NULL; } =20 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym, @@ -880,7 +885,7 @@ void srcline__tree_delete(struct rb_root_cached *tree) pos =3D rb_entry(next, struct srcline_node, rb_node); next =3D rb_next(&pos->rb_node); rb_erase_cached(&pos->rb_node, tree); - free_srcline(pos->srcline); + zfree_srcline(&pos->srcline); zfree(&pos); } } @@ -903,7 +908,7 @@ void inline_node__delete(struct inline_node *node) =20 list_for_each_entry_safe(ilist, tmp, &node->val, list) { list_del_init(&ilist->list); - free_srcline(ilist->srcline); + zfree_srcline(&ilist->srcline); /* only the inlined symbols are owned by the list */ if (ilist->symbol && ilist->symbol->inlined) symbol__delete(ilist->symbol); diff --git a/tools/perf/util/srcline.h b/tools/perf/util/srcline.h index b11a0aaaa676..a15c7db9058e 100644 --- a/tools/perf/util/srcline.h +++ b/tools/perf/util/srcline.h @@ -15,7 +15,7 @@ char *get_srcline(struct dso *dso, u64 addr, struct symbo= l *sym, char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym, bool show_sym, bool show_addr, bool unwind_inlines, u64 ip); -void free_srcline(char *srcline); +void zfree_srcline(char **srcline); char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line); =20 /* insert the srcline into the DSO, which will take ownership */ --=20 2.41.0.162.gfafddb0af9-goog From nobody Sun Feb 8 06:05:17 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 5B1A4C7EE25 for ; Thu, 8 Jun 2023 23:32:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237703AbjFHXb7 (ORCPT ); Thu, 8 Jun 2023 19:31:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237308AbjFHXbV (ORCPT ); Thu, 8 Jun 2023 19:31:21 -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 A4FB53AB2 for ; Thu, 8 Jun 2023 16:30:28 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-565ef5a707dso15472497b3.3 for ; Thu, 08 Jun 2023 16:30:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686267016; x=1688859016; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=tvDaWeTNK3WSoU7RxteMJWYjgKCzqqKrMU8SOHl6IsA=; b=zq7n0FG6q55kI1lknHAHE1lHuS0TVc2SIL7xiqJwpHFzQgHoHR7VyUNJvMJLM7LqZK PReM7k5azlxan7GYT1OEQt6A9BGBbAC3OZKpRnVe2PD57AM/SvI0WBHohv4qVe4lmE8e XM8+/NASVm3hFIJF3NtAK4Z98KBICfnux+nLsiXJ4NUp463Ykl0ZkWzFvMFlk/BFoSD9 DPIiedqbcOjh/XwcM6s3MbzY8xp8RCXeqbF2SA9G2Lb7D5ayo8igY25wrzJmVtqrLy4K akYi8T8ZNXtjwn6ejlcluxF5wrXbxLo1nhT5TB0nexOVLZO/9jIWD3tBE3eT80vTS7OY rL3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686267016; x=1688859016; h=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=tvDaWeTNK3WSoU7RxteMJWYjgKCzqqKrMU8SOHl6IsA=; b=bYoFG20lpIrHD+RohVdFHwio8uf8f3E9waV6uRJdKpl+FSnPY/tOl/U1BzmbQs/vsR BcYUvN+JSrtkB6xLJrmTo8Ff9rgTnnRWuaKBo4DCxJvWUXpQeeG3/rZVuW06RoOMYZW/ wYBhJfpuzV4ZN/2/imi9akJyvRhArVFZkSm+ZYmzKpxWa7V+Y821f2/n/Kl4rSGNQOgr 4mRJwHqP3kTTKKusK1c++YvYyPO9T9mPltFiTyPgaPCh0ML1HD8dfSuxspMdCRKUPgfA u2O0iTGTUlBkBcgEC/VMtlV0nqsTe09cvRXTDRS8E2hsMuWRu5xbp7CBWct7MvvZ0NjJ KovA== X-Gm-Message-State: AC+VfDzmtBgetWqAbd8S8X95yBUTOSooBm5ki7HmbnHWxLyg40Sxtqr7 UA28yYUnuVOP0Q9U19MwHxDuLQLaKNJQ X-Google-Smtp-Source: ACHHUZ5mehvIfiWPsR3FMIB5AYIB4oykjMARcMG88ABn1UYKXC7tDYxPh/EBCaYSpCh78GjCUjEvdjauAAmr X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:c3e5:ebc6:61e5:c73f]) (user=irogers job=sendgmr) by 2002:a25:b116:0:b0:ba8:4c16:78b7 with SMTP id g22-20020a25b116000000b00ba84c1678b7mr600676ybj.12.1686267015833; Thu, 08 Jun 2023 16:30:15 -0700 (PDT) Date: Thu, 8 Jun 2023 16:28:23 -0700 In-Reply-To: <20230608232823.4027869-1-irogers@google.com> Message-Id: <20230608232823.4027869-27-irogers@google.com> Mime-Version: 1.0 References: <20230608232823.4027869-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 26/26] perf hist: Fix srcline memory leak From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Suzuki K Poulose , "Naveen N. Rao" , Kan Liang , German Gomez , Ali Saidi , Jing Zhang , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Andi Kleen , "Steinar H. Gunderson" , Yuan Can , Brian Robbins , liuwenyu , Ivan Babrou , Fangrui Song , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" srcline isn't freed if it is SRCLINE_UNKNOWN. Avoid strduping in this case as such strdups are redundant and leak memory. Signed-off-by: Ian Rogers Acked-by: Ian Rogers --- tools/perf/util/hist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 77cb2cc83bb9..cc6f7f51faa5 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -484,7 +484,7 @@ static int hist_entry__init(struct hist_entry *he, goto err_infos; } =20 - if (he->srcline) { + if (he->srcline && strcmp(he->srcline, SRCLINE_UNKNOWN)) { he->srcline =3D strdup(he->srcline); if (he->srcline =3D=3D NULL) goto err_rawdata; --=20 2.41.0.162.gfafddb0af9-goog