Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4371363ybi; Mon, 15 Jul 2019 07:59:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqwb5eY5HfZwVZfCgxspglMIArrBwi6Ta7uo7zvNS+2XvVEkFbC5+04GZyuuJIqkf4SR1dvQ X-Received: by 2002:a17:902:a514:: with SMTP id s20mr27413073plq.162.1563202767410; Mon, 15 Jul 2019 07:59:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563202767; cv=none; d=google.com; s=arc-20160816; b=nwsRdJnz+G6iperlgGcOCFRq2/YB8addIdCHCTMn5hgPjXlCsXsnCaaUjNGMmhVVpm 7ikK6475q4R4lExQ3ELuZ6VmukJkQ7x5GECoYiERm4lDrg1XXxW9BXM5VdhNnrbxPUyR K063/0kPGybwiL50EnIOd+O8+cJ/+vgKUMlP5VTpw9c/O+FxSCZJfE/7WW8ycOBZzsl7 Ear7x1TqCTRCchgGBOyeNbZdsE777MByJzcd71hgwwgmW560TgLP4OCdp8UPDU/Y6b7G QrZnPghEfeF501YE/NJUAdaPo51/l45bz0FBG5vYeU9jUjH/KRZnnxXHFMNxqJqUtuTV 2qkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=hpUG424zLKtf0hl14vyA9TWId6HoMzUaWzE0O4uii+0=; b=XnA5gEZGUvjj8u9j1GZjIm0qcCt+BcDsuFvOk1AiSdLAqV1R3iMIePUiUXQaBYGwd+ n1LEsxbUnMWk0bVNOC4aZKvS8EkQknTt5Jse9WKJIdWxjezHTqMdMCG+bKi1sLDajGrV Juiy1taN6EF2g0DK7mdBXv1kihCDXehRUaDO3jBAIja7hoN74YruKs7bfFHHkbl8usH+ rDf0gtCZCUP9aqZadKNfpLuYtFKk5UkFVwOs0PXPhC0uXujGdW+OKNkjpKKOIz59jBEb foS+NSdmuA2JbDI6HU1WsoAHO+XtAcqqRNVNeLx0965mgq+7K4+h8W8B86zGGH3jpbzb naew== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c23si17262883pfo.168.2019.07.15.07.59.11; Mon, 15 Jul 2019 07:59:27 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389388AbfGOOVf (ORCPT + 99 others); Mon, 15 Jul 2019 10:21:35 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53628 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390043AbfGOOVZ (ORCPT ); Mon, 15 Jul 2019 10:21:25 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3A3AE308621E; Mon, 15 Jul 2019 14:21:25 +0000 (UTC) Received: from krava (unknown [10.40.205.8]) by smtp.corp.redhat.com (Postfix) with SMTP id 5D5B75B681; Mon, 15 Jul 2019 14:21:22 +0000 (UTC) Date: Mon, 15 Jul 2019 16:21:21 +0200 From: Jiri Olsa To: Arnaldo Carvalho de Melo Cc: Numfor Mbiziwo-Tiapo , Stephane Eranian , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Alexander Shishkin , Namhyung Kim , Song Liu , mbd@fb.com, LKML , Ian Rogers Subject: [PATCH] perf stat: Fix segfault for event group in repeat mode Message-ID: <20190715142121.GC6032@krava> References: <20190710204540.176495-1-nums@google.com> <20190714204432.GA8120@krava> <20190714205505.GB8120@krava> <20190715075912.GA2821@krava> <20190715083105.GB2821@krava> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190715083105.GB2821@krava> User-Agent: Mutt/1.12.0 (2019-05-25) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Mon, 15 Jul 2019 14:21:25 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Link: http://lkml.kernel.org/n/tip-07t75chgdhcr00uerh51hb6j@git.kernel.org Signed-off-by: Jiri Olsa --- 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 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, -- 2.21.0