Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3458054ybi; Sun, 14 Jul 2019 14:38:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqw64fkiwGaCpJh1U/P8Cmq/Yz3DQhp1N7kS5T4fwkifUWR8YcIg8Aldd2aqofoYigzpGFEp X-Received: by 2002:a63:f13:: with SMTP id e19mr23364769pgl.132.1563140289300; Sun, 14 Jul 2019 14:38:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563140289; cv=none; d=google.com; s=arc-20160816; b=jcB3oaEQS9bLA9xn0kvOa9nMcM5niTs3f7mXDVhbUwh8/Lx93BUfj3ubSXDbzU32cg VUh/BA5bchQwbsWpqmvYAtkZNkbES5sFhcKTDM6LlWyEa25GFeMOiv3IwFqquKODeGbb mOXufUPfd4LFEXPHLlGBeH9f3e2nHC5ClSZJVEJnsy4Q1g8y2gQlfSyWO/zhXqUy1ZuX oljcaww+FBWogl470pbexYScEqXjnrufOz6seDnM5RK5xX65KGI8LKUjfZwSGEw3i6G0 U0JMvsSTgDci0XgySGrBGHEUJENQ5C6vX5Jsichav5e7DxYxvGq9kjI3nPGEsTmaQiR3 WiFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=OqXaW1SBlO4ypk862puJP9ZlK8OkDZ0/srasv2LX6t0=; b=rWPCyie+LxkipR+QTtT6XguvXzvXGmYIhTTYzs5AzeCkgc2OlIQJAK8fgUu8txdgA/ CjYsp496sTb5Xa6ytHK8VDSJMeQsgIeOinVXq9SPrBbabGyEjU8KW/j2iVOH6yo7l8tM rEdT+FuvueQsthZWpNMnhLFMiZylSpedYo5+m/NprzGBzRQPbig5iUnkVtaK/Ciui0t8 yQ8R/1AU1lVKGx2I+swLBqS6FiICofc2AEPPMkuo7PNfmwyyz0MAhl84DE8k07P6/VZK rIVzIWPaVwsH8cJ8a+L5od2cG/aFo67E/tpqpHFd+l/CzU8mxELnDjaNVDFQ2LNh5Ogw AWYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ZFgb0hy1; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i3si13185549plt.306.2019.07.14.14.37.52; Sun, 14 Jul 2019 14:38:09 -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=@google.com header.s=20161025 header.b=ZFgb0hy1; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728867AbfGNVgz (ORCPT + 99 others); Sun, 14 Jul 2019 17:36:55 -0400 Received: from mail-io1-f67.google.com ([209.85.166.67]:39464 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728701AbfGNVgz (ORCPT ); Sun, 14 Jul 2019 17:36:55 -0400 Received: by mail-io1-f67.google.com with SMTP id f4so31101496ioh.6 for ; Sun, 14 Jul 2019 14:36:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=OqXaW1SBlO4ypk862puJP9ZlK8OkDZ0/srasv2LX6t0=; b=ZFgb0hy1hkaeTSQUqs9lXyHsiFuWqx6gh5YP3fEJehzrLCR6MjEL8iwhOlwxoLXaEW zxQbfSLsBgWNCahrjfC3wIHCFDCnl4R+uQ7MavD0QD7Oyc/9m/jm/me38n1Ax9ovZ3pj WylOFqR/h9JbOmyLnIRleSRzv1sPFYbM/2iewLMAKddcxJtBv3cMZ7nORkUe+gBCqWpF Kiq1w9gf07CjTuDJNycwdWQQzln/Hm+zy+cjiegwrUeyWIw8LbjCjGUdBSLaCAircNuP PTvx9aJ+1CouE+xdDBk44bGi3UJwojeHFlBPLcXVzJJZBp8iRZAhYSV5CNtSH7iaPPKu +X2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=OqXaW1SBlO4ypk862puJP9ZlK8OkDZ0/srasv2LX6t0=; b=AOBbDIYPONc3IYLM5QaLgMetbyoW9MevCH7LaUZCfYO5AxcV+U5fGJBhtH/DQnUAt+ XmsbifU0G5/knvMwSSOeN5Vn4/iepJCHbsj7Ioa2SvqDCwBaHT9p30Jp8TRV9wjMqlFE 2RrFQ/dNN0+rNF2pUtu6QO6SFwGq/l7eRlvlURRpN7YlSAbrN+qBShkQupqOiSvGvMdX 9KoTStt46qkpCbg2P3hPxkVoIe3DHfocLcEiTvoIjYAFr4Sz8kugNu4Ja2ydvVUjVaYK 51D7C5VICYG8ZQreZv4CjMYRBP07POsYgZS7wyzTA+qFkxMByzEj8BmcwnUYxMg1wqVB 7SRg== X-Gm-Message-State: APjAAAWGh8MiNIhssAl63tajfnXkNPiPNl35SMCMchFhkEey+17F/mlV 7KKH3hMteSm+j+qOFgs631tixg+QoA/uXFuYUDX+3g== X-Received: by 2002:a02:c646:: with SMTP id k6mr15220352jan.134.1563140213957; Sun, 14 Jul 2019 14:36:53 -0700 (PDT) MIME-Version: 1.0 References: <20190710204540.176495-1-nums@google.com> <20190714204432.GA8120@krava> <20190714205505.GB8120@krava> In-Reply-To: <20190714205505.GB8120@krava> From: Stephane Eranian Date: Sun, 14 Jul 2019 14:36:42 -0700 Message-ID: Subject: Re: [PATCH] Fix perf stat repeat segfault To: Jiri Olsa Cc: Numfor Mbiziwo-Tiapo , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Alexander Shishkin , Namhyung Kim , Song Liu , mbd@fb.com, LKML , Ian Rogers Content-Type: text/plain; charset="UTF-8" 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 1:55 PM Jiri Olsa wrote: > > 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 > not sure, that would realloc on each iteration of the repeats. > > 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,