Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1083634ybl; Wed, 14 Aug 2019 10:25:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqyZX5OSuelwq+1Hl2Lu9HwvDRJ9rUp6wbG2Xs72qQCRuH1KAldGf0U4OgX27y/hTHxbOoxV X-Received: by 2002:a17:90a:a789:: with SMTP id f9mr782243pjq.19.1565803545342; Wed, 14 Aug 2019 10:25:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565803545; cv=none; d=google.com; s=arc-20160816; b=Hwt6t6QK3BgcqFlZBMWa7cubcY9p4D39PhKP31r5cAci+9AUwmd2Cr2vt1qEBeT+NA G/kxFeQiaAPIhX8Hr/8yxwZr5J7MxlZxD1VWOxluZsnt7hfYEIprAQ+5B+56q8UnF0nF AtQ1QWf+K/df6ntCtgyasO6BXEclDMNsxUATsJuFnDq3G6hXdwlkbzPR6jQykvWAYdKR nCEB69+bHRhAFUZeFAtyb8Ivi+MKSqmxtb6dOrkpXx/0lwekX3DxUJ7Q35muzb/RqDyL TWfKYI5w8ffrxLXMbbZbEWkuCH5ndtZ1XpFzEMkSG6I/rb0NTyisLVi5+T54GpjdP/5X xZlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=yCofVISbLzpfCq7XxFV3GuCKlGdapH4UGV/jDEVzMsQ=; b=zjj5W8SnIqahmnWf+znEoTndxWx5XJcJmbdzxmztHm91ZMXv/kvkW0gOyPZRqkpIaT OJc5THKdbDN38iSsDhg0BKivp2tfzMdfmT3obsCOy27wMPmFz0EyRZtASuJ7Wea79eVa GpyFm39QFaRSuDzuYMmaXPPivjGd8gyx6c3vM1+1G4IxT7NeG3Vw1uWROUeg/5f07/Vz x8KK90TuV0qAHXD93bJPD2CZPpfD1ozFR4s1IggpTbhcXvyJCPf7CEEu8t/kv0oyx68z mluWAO9ilihYSshuaehWgVVVujlUKMomdfCMvDQM3/TEu1CKYVkfAlQRtTkRc6gPAUrD SRIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GgNRkj3C; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s28si94387pfd.272.2019.08.14.10.25.25; Wed, 14 Aug 2019 10:25:45 -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=@kernel.org header.s=default header.b=GgNRkj3C; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729593AbfHNRGJ (ORCPT + 99 others); Wed, 14 Aug 2019 13:06:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:55184 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729579AbfHNRGG (ORCPT ); Wed, 14 Aug 2019 13:06:06 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 3369821743; Wed, 14 Aug 2019 17:06:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1565802365; bh=d4I6Ceu/1O8p7f12GClQkLGcpFKIXGp9haa/aTigk8A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GgNRkj3CQ5s2SCWbPXvN3//I8BEbeIlZBwHl1ggH9PRDeYVu00D/MNES/Vlg+b/Yh e0V+Jj2X/YPg3tekIIZBvqU8AjybU722JMT9VscmWG6WYAc4lbQ1DbtEHsuHvPJufP iQvLSdSFUbetGHjIu3DqSVsCFFkX3xZp3sU13Q20= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Numfor Mbiziwo-Tiapo , Jiri Olsa , Alexander Shishkin , Ian Rogers , Mark Drayton , Namhyung Kim , Peter Zijlstra , Song Liu , Stephane Eranian , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 5.2 098/144] perf stat: Fix segfault for event group in repeat mode Date: Wed, 14 Aug 2019 19:00:54 +0200 Message-Id: <20190814165803.983934923@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190814165759.466811854@linuxfoundation.org> References: <20190814165759.466811854@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 08ef3af1579d0446db1c1bd08e2c42565addf10f ] 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 Signed-off-by: Sasha Levin --- 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 e28002d905738..c6c550dbb9479 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); } @@ -1922,6 +1928,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 2c46f9aa416c6..b854541604df5 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1282,6 +1282,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) @@ -2074,6 +2075,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.20.1