From nobody Sun Nov 24 18:48:55 2024 Received: from out30-133.freemail.mail.aliyun.com (out30-133.freemail.mail.aliyun.com [115.124.30.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 922E61BFE10; Mon, 4 Nov 2024 11:27:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730719678; cv=none; b=HpRjucJJBx9ta+tvBfvKIwfJijAfY/6lPfKWkN5ZPkfrkk6a5wRnENtfweFjr/mVV9TGHrmLNF8qbccUUWIVPuU6ewtY5mCkYx+ssvUs3poNdAgnCViI5Bvyi6z1HlN4cRAdmjFtaGtCgr1P3kLe+680+iKcx2nBTFd1P/poC9E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730719678; c=relaxed/simple; bh=DaSIEBgG2arTZrXAqthhtGFZjVD7oESyK8ikcDhZAaI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DnSK8Gb7JrAjFeQvKf+g4uWUGrvjxGAwSPV63C8BycQYf93dsNdumKu8SuAymxbEF/qB0axwqU0LFaANEboPi2KUBpeItGKwAhYNFX0ep4hpTRpYiMKhAnqgjTYQmx44gkf3MZnA2ovaoSrQx3zQIFf8p73UrjrHOSERNwznxBg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=CpPx29cC; arc=none smtp.client-ip=115.124.30.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="CpPx29cC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1730719673; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=Ymfli/aj8w7Alz6lpVhVsP42jBq57YRIsah1Dm91/4g=; b=CpPx29cCDqPSflkKH30V52ycHYh8Cfpii3RL3o+KDAxyrTZGVhoIh+dfHsAQB8EK5fajjoz/wwLdPXft7BEAwjK8KrnDI0qztQm3osjx2wCV4XZEJncZo8fg8MZWlxMgVoN8oAeL9Kd9Fjc7XEp24YTmMAkHoKA9kYYzjWYTPvM= Received: from localhost.localdomain(mailfrom:xueshuai@linux.alibaba.com fp:SMTPD_---0WIgekVk_1730719670 cluster:ay36) by smtp.aliyun-inc.com; Mon, 04 Nov 2024 19:27:51 +0800 From: Shuai Xue To: stable@vger.kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org, gregkh@linuxfoundation.org Cc: adrian.hunter@intel.com, alexander.shishkin@linux.intel.com, irogers@google.com, mark.rutland@arm.com, namhyung@kernel.org, peterz@infradead.org, acme@redhat.com, kprateek.nayak@amd.com, ravi.bangoria@amd.com, sandipan.das@amd.com, anshuman.khandual@arm.com, german.gomez@arm.com, james.clark@arm.com, terrelln@fb.com, seanjc@google.com, changbin.du@huawei.com, liuwenyu7@huawei.com, yangjihong1@huawei.com, mhiramat@kernel.org, ojeda@kernel.org, song@kernel.org, leo.yan@linaro.org, kjain@linux.ibm.com, ak@linux.intel.com, kan.liang@linux.intel.com, atrajeev@linux.vnet.ibm.com, siyanteng@loongson.cn, liam.howlett@oracle.com, pbonzini@redhat.com, jolsa@kernel.org Subject: [PATCH 5.10.y 1/2] Revert "perf hist: Add missing puts to hist__account_cycles" Date: Mon, 4 Nov 2024 19:27:35 +0800 Message-ID: <20241104112736.28554-2-xueshuai@linux.alibaba.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20241104112736.28554-1-xueshuai@linux.alibaba.com> References: <20241104112736.28554-1-xueshuai@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Revert "perf hist: Add missing puts to hist__account_cycles" This reverts commit a83fc293acd5c5050a4828eced4a71d2b2fffdd3. On x86 platform, kernel v5.10.228, perf-report command aborts due to "free(= ): invalid pointer" when perf-record command is run with taken branch stack sampling enabled. This regression can be reproduced with the following step= s: - sudo perf record -b - sudo perf report The root cause is that bi[i].to.ms.maps does not always point to thread->ma= ps, which is a buffer dynamically allocated by maps_new(). Instead, it may poin= t to &machine->kmaps, while kmaps is not a pointer but a variable. The original upstream commit c1149037f65b ("perf hist: Add missing puts to hist__account_cycles") worked well because machine->kmaps had been refactor= ed to a pointer by the previous commit 1a97cee604dc ("perf maps: Use a pointer for kmaps"). To this end, just revert commit a83fc293acd5c5050a4828eced4a71d2b2fffdd3. It is worth noting that the memory leak issue, which the reverted patch int= ended to fix, has been solved by commit cf96b8e45a9b ("perf session: Add missing evlist__delete when deleting a session"). The root cause is that the evlist= is not being deleted on exit in perf-report, perf-script, and perf-data. Consequently, the reference count of the thread increased by thread__get() = in hist_entry__init() is not decremented in hist_entry__delete(). As a result, thread->maps is not properly freed. Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ian Rogers Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Arnaldo Carvalho de Melo Cc: K Prateek Nayak Cc: Ravi Bangoria Cc: Sandipan Das Cc: Anshuman Khandual Cc: German Gomez Cc: James Clark Cc: Nick Terrell Cc: Sean Christopherson Cc: Changbin Du Cc: liuwenyu Cc: Yang Jihong Cc: Masami Hiramatsu Cc: Miguel Ojeda Cc: Song Liu Cc: Leo Yan Cc: Kajol Jain Cc: Andi Kleen Cc: Kan Liang Cc: Athira Rajeev Cc: Yanteng Si Cc: Liam Howlett Cc: Paolo Bonzini Cc: stable@vger.kernel.org # 5.10.228 Signed-off-by: Shuai Xue --- tools/perf/util/hist.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index c78d8813811c..8a793e4c9400 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -2624,6 +2624,8 @@ void hist__account_cycles(struct branch_stack *bs, st= ruct addr_location *al, =20 /* If we have branch cycles always annotate them. */ if (bs && bs->nr && entries[0].flags.cycles) { + int i; + bi =3D sample__resolve_bstack(sample, al); if (bi) { struct addr_map_symbol *prev =3D NULL; @@ -2638,7 +2640,7 @@ void hist__account_cycles(struct branch_stack *bs, st= ruct addr_location *al, * Note that perf stores branches reversed from * program order! */ - for (int i =3D bs->nr - 1; i >=3D 0; i--) { + for (i =3D bs->nr - 1; i >=3D 0; i--) { addr_map_symbol__account_cycles(&bi[i].from, nonany_branch_mode ? NULL : prev, bi[i].flags.cycles); @@ -2647,12 +2649,6 @@ void hist__account_cycles(struct branch_stack *bs, s= truct addr_location *al, if (total_cycles) *total_cycles +=3D bi[i].flags.cycles; } - for (unsigned int i =3D 0; i < bs->nr; i++) { - map__put(bi[i].to.ms.map); - maps__put(bi[i].to.ms.maps); - map__put(bi[i].from.ms.map); - maps__put(bi[i].from.ms.maps); - } free(bi); } } --=20 2.39.3