From nobody Mon Feb 9 05:59:27 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 29BC3C7EE29 for ; Wed, 7 Jun 2023 01:47:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240717AbjFGBrG (ORCPT ); Tue, 6 Jun 2023 21:47:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234873AbjFGBo7 (ORCPT ); Tue, 6 Jun 2023 21:44: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 A87411BD9 for ; Tue, 6 Jun 2023 18:44:56 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-565d1b86a64so85432317b3.3 for ; Tue, 06 Jun 2023 18:44:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686102295; x=1688694295; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=BpqdBOXtu+XA2l9VAqqYrUmzRP4oQxTaswm8YIVLP/c=; b=oyAMvyvKd7H3iN2vs+5b0ZGuiOACYVPYVqF6UfnASiCblIW8XWaFPO+vux0yta4hEO hfQDbKGo944R0aqkreNE0o00KCsErwgAguftl6ZXevwn/x1HtDWumPSrb0Yt+T/MF5M9 jZ24pj9t7MeljL9SyLOhJtEi/AqxlZJSnhXi7KT3kld9blW4geT4ldBkXV2ScxBet8h9 7z7IWlmOO+5scKxUm7y9+AeKPN8dBouxwfJOXpD2QprsQGR1QsLnl/IGqBnBHwVyFrLs pmG3VJDsI9GJqTveB+yM+MQDmfm07ano0FF8TrAZF30D6nC+JeD+dAQCewDB4N6pBia9 9zuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686102295; x=1688694295; 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=BpqdBOXtu+XA2l9VAqqYrUmzRP4oQxTaswm8YIVLP/c=; b=ZVdp0ab4QK/Vsz8e+fSXtiz/gMQEAHdlbVwvVvL4dDpSVxlXk4NanIrh9j2pWKK6Ml a6UF9Fb0COcJRjvgOHrXInQ6rrW/KDS+yfvXCKKxu3YC/zAsQ0/Jwt8mCPOvOhBAzTab K9ZIVb/gGmTonZmwlx+op9mUeOHFUk2o2+ayajJDPajR+1SxJt3qwpj+pV+YxMTgLgco ZzV6+i4tfOu4XOJCaHhcMDjs9pIY6PwJVFMEe+OrKvv8ZNenxKoykoTDzTrfEfr2UqbN GJa3UQ7vB/lyxRNHOfmhDTU2ib19nk2a4yXQ6qJM4dgcDJN1K8wIC7TMoiDeOOtMtYrL xKUw== X-Gm-Message-State: AC+VfDxi3jUMSL8jiN4sd82sQtAdkwF0zkZa/LXe3ihssDRdSXVdlDIG 3kDMYTYHI3vkhdT4tu9cAWt7moNfOrMm X-Google-Smtp-Source: ACHHUZ46Eck1lbC5mSIJzGH2gMBwJxRSh1jBmUauiOGfGoKRuuP34DT5BWMC2bAna9Eip/A8WkG4acWqFPIE X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:3c35:209f:5d38:b7a1]) (user=irogers job=sendgmr) by 2002:a05:6902:183:b0:ba8:1f20:ff4f with SMTP id t3-20020a056902018300b00ba81f20ff4fmr2118729ybh.12.1686102295606; Tue, 06 Jun 2023 18:44:55 -0700 (PDT) Date: Tue, 6 Jun 2023 18:43:53 -0700 In-Reply-To: <20230607014353.3172466-1-irogers@google.com> Message-Id: <20230607014353.3172466-21-irogers@google.com> Mime-Version: 1.0 References: <20230607014353.3172466-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Subject: [PATCH v1 20/20] 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 , "=?UTF-8?q?Martin=20Li=C5=A1ka?=" , Athira Rajeev , Miguel Ojeda , ye xingchen , Liam Howlett , Dmitrii Dolgov <9erthalion6@gmail.com>, "Shawn M. Chapla" , Yang Jihong , K Prateek Nayak , Changbin Du , Ravi Bangoria , Sean Christopherson , Raul Silvera , 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 | 7 +++++-- tools/perf/util/event.c | 3 +++ tools/perf/util/hist.c | 6 ++++-- tools/perf/util/machine.c | 14 ++++++++------ 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index b0dafc758173..437325d19ad3 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c @@ -1065,8 +1065,10 @@ 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.maps =3D maps__get(node->ms.maps); node->ms.map =3D map__get(node->ms.map); node->branch =3D branch; node->nr_loop_iter =3D nr_loop_iter; @@ -1114,7 +1116,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 +1130,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'; 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..16dc49876e87 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) @@ -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 --=20 2.41.0.rc0.172.g3f132b7071-goog