From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF2A0C7618F for ; Mon, 22 Jul 2019 17:41:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A8B1F21955 for ; Mon, 22 Jul 2019 17:41:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563817279; bh=zkc0AxRVk2GB+h/SsQ+smgKflL9ocWfJ7X0W37xKxpg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=r0vo8FsAmXdIXiE2t530Z7IP2A7KPpznkr9PAjM3wk2RP+b1yWySQegOruluJ0RY7 nW5hWoGjneRySGYxVEr44uR2IOQMA1qs/UE9XNmyFGy7OWBHXp8uLlBmOaURnZTzcf z6f9mNz6y9lYv+SvDKqFOYUCPYry86RI/888/ocU= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731865AbfGVRlS (ORCPT ); Mon, 22 Jul 2019 13:41:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:46796 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726236AbfGVRlS (ORCPT ); Mon, 22 Jul 2019 13:41:18 -0400 Received: from quaco.ghostprotocols.net (unknown [190.15.121.82]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2E7F621901; Mon, 22 Jul 2019 17:41:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563817277; bh=zkc0AxRVk2GB+h/SsQ+smgKflL9ocWfJ7X0W37xKxpg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JACHSfuMYG1DpUdBQjxJj34HG9gX3ZE/WZK1/KIIdU16zmc8MS/04zrERbjytbvZu z6i2cGN8p0RdZZDEKg1Om8xHC+/ej2Sa3RgitklNOLGMLOdRx7tjBs28Ko5mMfh94y R+KSJYatPx8q+tKJV4qKm6NUBEFRSQf/VT4ylJk4= From: Arnaldo Carvalho de Melo To: Ingo Molnar , Thomas Gleixner Cc: Jiri Olsa , Namhyung Kim , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Jiri Olsa , Numfor Mbiziwo-Tiapo , Alexander Shishkin , Ian Rogers , Mark Drayton , Peter Zijlstra , Song Liu , Stephane Eranian , Arnaldo Carvalho de Melo Subject: [PATCH 20/37] perf stat: Fix segfault for event group in repeat mode Date: Mon, 22 Jul 2019 14:38:22 -0300 Message-Id: <20190722173839.22898-21-acme@kernel.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190722173839.22898-1-acme@kernel.org> References: <20190722173839.22898-1-acme@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jiri Olsa Numfor Mbiziwo-Tiapo reported segfault on stat of event group in repeat mode: # perf stat -e '{cycles,instructions}' -r 10 ls It's caused by memory corruption due to not cleaned evsel's id array and index, which needs to be rebuilt in every stat iteration. Currently the ids index grows, while the array (which is also not freed) has the same size. Fixing this by releasing id array and zeroing ids index in perf_evsel__close function. We also need to keep the evsel_list alive for stat record (which is disabled in repeat mode). Reported-by: Numfor Mbiziwo-Tiapo Signed-off-by: Jiri Olsa Cc: Alexander Shishkin Cc: Ian Rogers Cc: Mark Drayton Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Song Liu Cc: Stephane Eranian Link: http://lkml.kernel.org/r/20190715142121.GC6032@krava Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-stat.c | 9 ++++++++- tools/perf/util/evsel.c | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index b55a534b4de0..352cf39d7c2f 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -607,7 +607,13 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx) * group leaders. */ read_counters(&(struct timespec) { .tv_nsec = t1-t0 }); - perf_evlist__close(evsel_list); + + /* + * We need to keep evsel_list alive, because it's processed + * later the evsel_list will be closed after. + */ + if (!STAT_RECORD) + perf_evlist__close(evsel_list); return WEXITSTATUS(status); } @@ -1997,6 +2003,7 @@ int cmd_stat(int argc, const char **argv) perf_session__write_header(perf_stat.session, evsel_list, fd, true); } + perf_evlist__close(evsel_list); perf_session__delete(perf_stat.session); } diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index b787ec778049..7d1757a2ec46 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1292,6 +1292,7 @@ static void perf_evsel__free_id(struct perf_evsel *evsel) xyarray__delete(evsel->sample_id); evsel->sample_id = NULL; zfree(&evsel->id); + evsel->ids = 0; } static void perf_evsel__free_config_terms(struct perf_evsel *evsel) @@ -2078,6 +2079,7 @@ void perf_evsel__close(struct perf_evsel *evsel) perf_evsel__close_fd(evsel); perf_evsel__free_fd(evsel); + perf_evsel__free_id(evsel); } int perf_evsel__open_per_cpu(struct perf_evsel *evsel, -- 2.21.0