Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2280223imm; Mon, 28 May 2018 05:18:10 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrLCDsha2FHNrasjyMNjRQWqKSbA0ZNdRUB3tr+uOxpIS2TFlweeXsxUDRRKKZE11T1F+2J X-Received: by 2002:a17:902:7008:: with SMTP id y8-v6mr13251631plk.141.1527509890150; Mon, 28 May 2018 05:18:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527509890; cv=none; d=google.com; s=arc-20160816; b=V27H9AI0+X/SmAIqbsDJpt0c8Y1XHVFtw09aSMsIS8GgdFBWuigC8ZnULOVyj7YF4l Nv7aAuGLjUXrvzLef0XFLUpCs9UEm+OYfLKTZQU++S2jefaQw4pQBG3v0s4isaBmIALz mQzWP/1z9MWYmFsT1hn/8mmVK6ntSm5+0q0vjGr44Q5JhEI6Tz6FSo9fYkbgg/xXAJQh j3U1Ury2oclC8h2DaWhlGZRNPKq5ZDjHu4+XQEodLI9BlPZcLLQwx6eE878Egj/jEruu ZzOtAH1//DkwEC8aIIATMxF79cDdzO8g3Dz/jcG8nNZqrcCCqaKCgBuEN93jcqX3rrTP ti4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=scu38EbDX0wRDphUUmtvuAveHaYqvjo71jxJugCgWnk=; b=SOF0B+qJe6Z7Yykb36i5LHcsnKNOeUoW3LDGC40kymASfghkpd9DFHl6lcpJ/ONcZL GD+OzPbJ9U7xZ6WzNlDZ5KwHRi/kmEbG9vNiJt3hGhW5jy80G4vvtK/1SJ//vrzspg3l lis1/3POqrJvnaQYfi8Sa10pMhGqpt0vgCpEP0rdXmm5l56RdCBTvffgOTSswk8JAskv JR7ldMaNskvOlBvuNos2eqebj0Y74AkcDNY+Uu4BiQvrVC15wUoie/XxYXUQryOpaohA mmvhy2NfFfGqtkBUz4X29sofwFjSQQZ4+6/oyhzzYgctVce08wGJ/H3ypWzveet+VpGJ oX3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=d3JCBn+O; 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 bg8-v6si30278996plb.486.2018.05.28.05.17.55; Mon, 28 May 2018 05:18:10 -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=d3JCBn+O; 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 S1423387AbeE1MQk (ORCPT + 99 others); Mon, 28 May 2018 08:16:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:50512 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1164428AbeE1LDO (ORCPT ); Mon, 28 May 2018 07:03:14 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (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 89609208A1; Mon, 28 May 2018 11:03:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527505394; bh=EpSoQjeXNI4mV5yv1n2qxEDIX7kuBtJwfPSlLIJZl7A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d3JCBn+Otty3BTdllCGxVqrmMZL/SaR4ikV9mLTTa1eQv+DGOGrJNBf+Nass/FCoZ XjIuCIRh4jrcFjJL6wm+2QUmVnZY0gY58Q7N6b/t4bsFM+iFjUhPBkwI+XiAvZusdo M967VXhrvbgOG/0eWDYNxLq/QhePifBMERmOaTbA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jiri Olsa , Jin Yao , Alexander Shishkin , Andi Kleen , Kan Liang , Peter Zijlstra , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 4.14 481/496] perf report: Fix memory corruption in --branch-history mode --branch-history Date: Mon, 28 May 2018 12:04:26 +0200 Message-Id: <20180528100340.160067548@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100319.498712256@linuxfoundation.org> References: <20180528100319.498712256@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 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 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jiri Olsa [ Upstream commit e3ebaa465136ecfedf9c6f4671df02bf625f8125 ] Jin Yao reported memory corrupton in perf report with branch info used for stack trace: > Following command lines will cause perf crash. > perf record -j call -g -a > perf report --branch-history > > *** Error in `perf': double free or corruption (!prev): 0x00000000104aa040 *** > ======= Backtrace: ========= > /lib/x86_64-linux-gnu/libc.so.6(+0x77725)[0x7f6b37254725] > /lib/x86_64-linux-gnu/libc.so.6(+0x7ff4a)[0x7f6b3725cf4a] > /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f6b37260abc] > perf[0x51b914] > perf(hist_entry_iter__add+0x1e5)[0x51f305] > perf[0x43cf01] > perf[0x4fa3bf] > perf[0x4fa923] > perf[0x4fd396] > perf[0x4f9614] > perf(perf_session__process_events+0x89e)[0x4fc38e] > perf(cmd_report+0x15d2)[0x43f202] > perf[0x4a059f] > perf(main+0x631)[0x427b71] > /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f6b371fd830] > perf(_start+0x29)[0x427d89] For the cumulative output, we allocate the he_cache array based on the --max-stack option value and populate it with data from 'callchain_cursor'. The --max-stack option value does not ensure now the limit for number of callchain_cursor nodes, so the cumulative iter code will allocate smaller array than it's actually needed and cause above corruption. I think the --max-stack limit does not apply here anyway, because we add callchain data as normal hist entries, while the --max-stack control the limit of single entry callchain depth. Using the callchain_cursor.nr as he_cache array count to fix this. Also removing struct hist_entry_iter::max_stack, because there's no longer any use for it. We need more fixes to ensure that the branch stack code follows properly the logic of --max-stack, which is not the case at the moment. Original-patch-by: Jin Yao Signed-off-by: Jiri Olsa Reported-by: Jin Yao Cc: Alexander Shishkin Cc: Andi Kleen Cc: Jiri Olsa Cc: Kan Liang Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/20180216123619.GA9945@krava Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- tools/perf/util/hist.c | 4 +--- tools/perf/util/hist.h | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -878,7 +878,7 @@ iter_prepare_cumulative_entry(struct his * cumulated only one time to prevent entries more than 100% * overhead. */ - he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1)); + he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1)); if (he_cache == NULL) return -ENOMEM; @@ -1043,8 +1043,6 @@ int hist_entry_iter__add(struct hist_ent if (err) return err; - iter->max_stack = max_stack_depth; - err = iter->ops->prepare_entry(iter, al); if (err) goto out; --- a/tools/perf/util/hist.h +++ b/tools/perf/util/hist.h @@ -107,7 +107,6 @@ struct hist_entry_iter { int curr; bool hide_unresolved; - int max_stack; struct perf_evsel *evsel; struct perf_sample *sample;