Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp10753088ybi; Thu, 11 Jul 2019 10:22:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqxEO8dKww0Ya4CzU3709FJCJbPvL7Wk994CqOXncLUlR9s6p4mhg7YW4EeguItANFAde9e+ X-Received: by 2002:a17:90a:b312:: with SMTP id d18mr5930683pjr.35.1562865752115; Thu, 11 Jul 2019 10:22:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562865752; cv=none; d=google.com; s=arc-20160816; b=X93+i+wX8E7RPbdrPE7Q8YLhvl+J5YDud6z0BnNEUm5rTrzYAMr4FVoT4sr/IJ9NY5 hnN2alayHYJ0lZ5ixsFEyVTAuJGS4IS6Lz3GYkIO7h4S//PWd9njfSBGkbldLcIP2XNT 4FNurVs8w4rdLbd3Y9yNVOB7H0d4V1qf1s4g//IZbeIzEABr86GJOJW9jMmIq2/drmBc F1dJEPTkdC1Izj4cLhTwup5vzMTUEjV3rFI97KnfqUDnT2Ui6FDgat14ZTvzMojapQZx v9EsRnHkmRcvmwbSbtKrv2dYRVxTYRMKvAIaMF51tEVE11TGtyqVcsooX8X4rwYQ5RfH CC9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=gOacuLj0WmQfTM/SGEvRKG9q+2Su5RYWQ+JdbWVt3D8=; b=dtORWV9UzrWzWIUIVe9HKhAirPvGbJNRNOXZXFrcndtOd451Cd+PKXqHEH0BjvF5Ku ogqfmrewTerQXwUadFSH4BZo79/nJvjdrOlOfIQnuxlffrzEFcuuL+aErJG/osUbjFmm zSwUx6k0I5ftljZ2dYNr6g4MSpip87Qz0KC3iL82kDSK5dXiHojxplessRJ2QmCOKSid G+WWDZmpyj/9htNHs5DszNnfdPJBMLpG2EMwMrQcC/9WucrJgw7DT86m9nlypa2w3Xfz YHYXkcgqoblp+y5HJWB3HwFq6ooZXjCKMSxkI2SgmBLN0MdefgzxzvvkYJxZmCf0rb0H JT/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cTp8ixgA; 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 z7si6084065pga.409.2019.07.11.10.22.16; Thu, 11 Jul 2019 10:22:32 -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=cTp8ixgA; 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 S1728906AbfGKRV0 (ORCPT + 99 others); Thu, 11 Jul 2019 13:21:26 -0400 Received: from mail-pf1-f201.google.com ([209.85.210.201]:49511 "EHLO mail-pf1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726213AbfGKRV0 (ORCPT ); Thu, 11 Jul 2019 13:21:26 -0400 Received: by mail-pf1-f201.google.com with SMTP id 145so3815991pfw.16 for ; Thu, 11 Jul 2019 10:21:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=gOacuLj0WmQfTM/SGEvRKG9q+2Su5RYWQ+JdbWVt3D8=; b=cTp8ixgA+Kq+0b/GvRG1DaYUMqAOi0uzKAp3ZHgjTG+9KeOcBd3L6FCnaC+ciWqBLA AO43ikFBO228cCtYMrbmPSDZjvuSAajl5HC1fkquebNOuwy+AoCnK6oPzeNGbHq30as5 9D0Ts9pef0nwZ4Py8edQHAI+EAnA97rkIEQ7TcU5cqnHVHtKZ8XAmNkoXTGgmfWcboo9 yeiCTAknlCHZitm544zQ1/sIUDVDEl7TtcfVDLxMj2ynIDMBNUVcjw5gpis+3kNkcHEJ THg3gFOAcLLdfBsai3GocaCWIyXF4Uh2yk91iUbMh+36Axne/8HFOe+CNmWjFPvP3yWu QNNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=gOacuLj0WmQfTM/SGEvRKG9q+2Su5RYWQ+JdbWVt3D8=; b=cFkZ2/+yyestwGM+k95XxQ2k7febtXwtBHd2aXnHUjvZe5+2SZo9KnJrLqj37ylz4b OE2+WAQDnz4xjtqSaP9lVZJ4F1eX6mAmNVKlp+3eagD2Doe4vl5E8hdEHcscmIrLufpP wrTsL86SEBIq2MC9KFqgkSycGSwCTYa5w03+TWLMvlwE+WNhAnxhrDCPX92ynuaDL2iV CIHhwsXEtPDjbEt+/pOM6b7qKbr4iavGiI1h9m3jdoUyyoG44Q2KvOYsOBYLqJvo8E5n PTxPTpdinURo2wLMuME/U5HWnnQTlguhp/TjU8/wtKe+8BLSs2yxBBlvirE1/TMW8vg7 RiaA== X-Gm-Message-State: APjAAAVEwaBLpfdi3VGFNRU6KUm6aKQKx2yS52Tv8dlI/SzVy53Xnr+4 nMxG7/sYETnvpQUK8492AGFe91jQ X-Received: by 2002:a63:1b07:: with SMTP id b7mr5486155pgb.133.1562865685025; Thu, 11 Jul 2019 10:21:25 -0700 (PDT) Date: Thu, 11 Jul 2019 10:21:19 -0700 Message-Id: <20190711172119.236501-1-nums@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH] Fix perf stat repeat segfault From: Numfor Mbiziwo-Tiapo To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org, songliubraving@fb.com, mbd@fb.com Cc: linux-kernel@vger.kernel.org, irogers@google.com, eranian@google.com, Numfor Mbiziwo-Tiapo 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 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..00a13ce17fd9 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; } static bool is_target_alive(struct target *_target, @@ -503,7 +504,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx) if (l > stat_config.unit_width) stat_config.unit_width = l; - if (perf_evsel__should_store_id(counter) && + if (perf_evsel__should_store_id(counter, run_idx) && perf_evsel__store_ids(counter, evsel_list)) return -1; } -- 2.22.0.410.gd8fdbe21b5-goog