Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp8988323ybi; Tue, 23 Jul 2019 19:32:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqyzDLymPgRZAUZpg+KQHrnkNN87arRzCcW9IoMR2hdXUyVPDek8KW0yMhrJk3JewWGCuRSx X-Received: by 2002:a63:e901:: with SMTP id i1mr62714082pgh.451.1563935569640; Tue, 23 Jul 2019 19:32:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563935569; cv=none; d=google.com; s=arc-20160816; b=qX+x1CUPtPDGyxIwBWkLXUxN2Qiek7JX/g1t55OCUoeTXc83db/qmxk/flSdK0my2G PalQV+d4hpWBo3h2+SyF0BBUOfdqdpfGICiKXojZ93REw5wc5HziHs3MiptzOd1Sleut lcjm6lQaC2OQ21LsWwObDxp2QcnJW4cR+z6XP23S6Id9KA+pbpX6+mIgL95UbhhrrJUm kIzjc2uIPidF8miW0K1HaKn5TIU/RJ5AYpODBaK1gBKC1SLaYPtXizkysk04ZkfdGujg FEHPpr++H1bKfkoqWNkd2ofFTZ2SHr+A4hKCFuxQyD5a2tLYy7ySEDe9aeSa26plW+WI KO2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:dkim-signature:dkim-filter; bh=jEZ7ALSLtumeLXXb+vgVcxrPJSkSgkio94KoNbEl4Ew=; b=k9QjSVIs4ZYekNWGmwZuSqs775DJxq91EGebMk0cIKHP2PgH3b0xo8yReZtQV5PIJc PipJ8DJG4JFR0F2yA64NyH3OJN9a1tff2IkxJM580guJAiHQq9Bi3YhMGsNobvAlINMr 9I+55Be1y19z9dop+7e1jrBhvov/xPvplPfeXI+P3AbPoEhNnsd0OOUe538T76lWTHFG uWtCfts5oqclGft6ezxAFtMhoAdtsXTwwX9aYBWDmh19dapdCd1k0ZeALloLD5eA2ZL2 V3CxyH5wc6oJFc5RAOaY2T3XSktKyQBe+U7eadMT+xEyScwBBCC/rHp0xM8djaBSRL79 hYaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2019071901 header.b=llunZeFS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q199si12488113pfq.112.2019.07.23.19.32.34; Tue, 23 Jul 2019 19:32:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@zytor.com header.s=2019071901 header.b=llunZeFS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2403984AbfGWVuI (ORCPT + 99 others); Tue, 23 Jul 2019 17:50:08 -0400 Received: from terminus.zytor.com ([198.137.202.136]:42629 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731612AbfGWVuH (ORCPT ); Tue, 23 Jul 2019 17:50:07 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id x6NLnJ2K253396 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Tue, 23 Jul 2019 14:49:19 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 terminus.zytor.com x6NLnJ2K253396 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2019071901; t=1563918560; bh=jEZ7ALSLtumeLXXb+vgVcxrPJSkSgkio94KoNbEl4Ew=; h=Date:From:Cc:Reply-To:In-Reply-To:References:To:Subject:From; b=llunZeFSkgQsQV59HA+bgYcTUHzoospJ7Ox8OUtx6gQSgTmRXP2bHcUuN51nAateN 5BeLe1vz/uEHq01ZiXh4hErJJ0qajk1jp+A//KZ1AAb6N4IpHcGfg2sEYI6AUdGbnK gm7i/BVScv77eIGtynKOs9T8PYbqYE8OYYCpIE3F/IP0W+2DE/uJ9fQhH5fkC2+Ynw u1GB0uJkUnmzz2dyiBlC7uhRR81DUZZucM1Pu8PqXtKOc4sFn1iWi8yxqptmcUCDqX HzXZuCYRpkS9Kd/qie04H2pRb+3LcoiXzwR1FS/Qz1bvhYsWJwUEosgm2o4J5Czwjn mE92PZ8EFfVUA== Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id x6NLnIcZ253393; Tue, 23 Jul 2019 14:49:18 -0700 Date: Tue, 23 Jul 2019 14:49:18 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Jiri Olsa Message-ID: Cc: jolsa@redhat.com, eranian@google.com, peterz@infradead.org, songliubraving@fb.com, mbd@fb.com, tglx@linutronix.de, mingo@kernel.org, irogers@google.com, alexander.shishkin@linux.intel.com, nums@google.com, linux-kernel@vger.kernel.org, jolsa@kernel.org, hpa@zytor.com, acme@redhat.com, namhyung@kernel.org Reply-To: peterz@infradead.org, jolsa@redhat.com, eranian@google.com, mingo@kernel.org, tglx@linutronix.de, mbd@fb.com, songliubraving@fb.com, linux-kernel@vger.kernel.org, nums@google.com, irogers@google.com, alexander.shishkin@linux.intel.com, namhyung@kernel.org, acme@redhat.com, hpa@zytor.com, jolsa@kernel.org In-Reply-To: <20190715142121.GC6032@krava> References: <20190715142121.GC6032@krava> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/urgent] perf stat: Fix segfault for event group in repeat mode Git-Commit-ID: 08ef3af1579d0446db1c1bd08e2c42565addf10f X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-0.3 required=5.0 tests=ALL_TRUSTED,BAYES_00, DATE_IN_FUTURE_96_Q,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 08ef3af1579d0446db1c1bd08e2c42565addf10f Gitweb: https://git.kernel.org/tip/08ef3af1579d0446db1c1bd08e2c42565addf10f Author: Jiri Olsa AuthorDate: Mon, 15 Jul 2019 16:21:21 +0200 Committer: Arnaldo Carvalho de Melo CommitDate: Tue, 23 Jul 2019 09:00:05 -0300 perf stat: Fix segfault for event group in repeat mode 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 @@ try_again: * 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 ebb46da4dfe5..52459dd5ad0c 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1291,6 +1291,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) @@ -2077,6 +2078,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,