Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3427294ybi; Sun, 14 Jul 2019 13:55:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqx1dv2PtFcU8+IX8f20/DoDH6xW/op8en2LjC9Yv9qPbJuEb6JfguHYfSNZ7MMoxp5dGdh3 X-Received: by 2002:a63:121b:: with SMTP id h27mr7723733pgl.335.1563137752322; Sun, 14 Jul 2019 13:55:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563137752; cv=none; d=google.com; s=arc-20160816; b=I2yP0QMKex3jX2L6H2fs5iN1JrGr1R2McHk8vxvr274xb6Bei6WZ4CgN7VccwNalBc z9+Est8ezf4oFCFObmOoq5HW7dHFEuTLTtjPGjxOwhWUT5xev1+K3MbJ87bRyfHK5Zpx unZGV3UIDCfHQ9oUXC+HydcqRZIiGdmpfQy42Uq7t4gFRDup0wSjSLBQY3P9Q+7js0BK kj3LlpkOOCTFlu8iEvOGiTTLECPMog2o+WOVNJeCcoSrZURLlxZnAOShsn9L0M+NTjGr qIzBEPELCnEST1IQgHcduDs2+gQxxMhyXitgeTwrcNdaRXaH7S5h36Etd6kxjoVOJ7++ kaNQ== 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=iNzgWmY9o5DTCG+8+YQsV4I3CO2FNnochJc8lMRM1Ag=; b=fezpLyBj8SaiSBr1/MevXp13d+/1xPj8r3KFDx12enmOqt8vNpafzyzYOy2UvYMbJk nGiYxJ56UdUPrxKf1j1S5f0kJf1W04pwD4aBW+C890FguN9k+Da2Rb8x3rEjd2W6TcOI GxOrz4ztIqVCkzuDLvgX9P+bqzH6I9hgX+xmvBivpkgyPU1cPiEUhXQUOVOnq7SqphSK OU1os29fcbI449sOoDtYfgfwBNzZRvgfcYWhr0q7fvyLPvPArtgYuHVs3oflMVraFE+f FDg2+xtvcFhUohr4lTVoTxtJtsoAIXW1YXNnqMqH/w3HqYn81A4nF8xEm6TaYBMYpOgA Mmsg== 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 f17si14055534pgv.338.2019.07.14.13.55.31; Sun, 14 Jul 2019 13:55:52 -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 S1728742AbfGNUzJ (ORCPT + 99 others); Sun, 14 Jul 2019 16:55:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42968 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728442AbfGNUzJ (ORCPT ); Sun, 14 Jul 2019 16:55:09 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 85F3330862BE; Sun, 14 Jul 2019 20:55:08 +0000 (UTC) Received: from krava (ovpn-204-23.brq.redhat.com [10.40.204.23]) by smtp.corp.redhat.com (Postfix) with SMTP id 3F2A960BFB; Sun, 14 Jul 2019 20:55:06 +0000 (UTC) Date: Sun, 14 Jul 2019 22:55:05 +0200 From: Jiri Olsa To: Numfor Mbiziwo-Tiapo Cc: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, alexander.shishkin@linux.intel.com, namhyung@kernel.org, songliubraving@fb.com, mbd@fb.com, linux-kernel@vger.kernel.org, irogers@google.com, eranian@google.com Subject: Re: [PATCH] Fix perf stat repeat segfault Message-ID: <20190714205505.GB8120@krava> References: <20190710204540.176495-1-nums@google.com> <20190714204432.GA8120@krava> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190714204432.GA8120@krava> User-Agent: Mutt/1.12.0 (2019-05-25) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Sun, 14 Jul 2019 20:55:08 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Jul 14, 2019 at 10:44:36PM +0200, Jiri Olsa wrote: > On Wed, Jul 10, 2019 at 01:45:40PM -0700, Numfor Mbiziwo-Tiapo wrote: > > When perf stat is called with event groups and the repeat option, > > a segfault occurs because the cpu ids are stored on each iteration > > of the repeat, when they should only be stored on the first iteration, > > which causes a buffer overflow. > > > > This can be replicated by running (from the tip directory): > > > > make -C tools/perf > > > > then running: > > > > tools/perf/perf stat -e '{cycles,instructions}' -r 10 ls > > > > Since run_idx keeps track of the current iteration of the repeat, > > only storing the cpu ids on the first iteration (when run_idx < 1) > > fixes this issue. > > > > Signed-off-by: Numfor Mbiziwo-Tiapo > > --- > > tools/perf/builtin-stat.c | 7 ++++--- > > 1 file changed, 4 insertions(+), 3 deletions(-) > > > > diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c > > index 63a3afc7f32b..92d6694367e4 100644 > > --- a/tools/perf/builtin-stat.c > > +++ b/tools/perf/builtin-stat.c > > @@ -378,9 +378,10 @@ static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf > > workload_exec_errno = info->si_value.sival_int; > > } > > > > -static bool perf_evsel__should_store_id(struct perf_evsel *counter) > > +static bool perf_evsel__should_store_id(struct perf_evsel *counter, int run_idx) > > { > > - return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID; > > + return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID > > + && run_idx < 1; > > we create counters for every iteration, so this can't be > based on iteration > > I think that's just a workaround for memory corruption, > that's happening for repeating groupped events stats, > I'll check on this how about something like this? we did not cleanup ids on evlist close, so it kept on raising and causing corruption in next iterations jirka --- 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,