Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp1937327ima; Thu, 25 Oct 2018 07:16:57 -0700 (PDT) X-Google-Smtp-Source: AJdET5dWZ4rElYqgerKBXqf8ZllWwSbn/y5H0nhtvw0CVVLrDokK0uL0tkddkW3s9jVrN3zuGEvp X-Received: by 2002:a62:a93:: with SMTP id 19-v6mr1707650pfk.121.1540477016938; Thu, 25 Oct 2018 07:16:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540477016; cv=none; d=google.com; s=arc-20160816; b=aJjEiWwBES0MNyi+gp1L9UE4Y7E3ulm0FvL5ru6qe/z2FutRNg+RHzFVWigT3I5P4H eCZTeLE+7oY1yarD5pGrFWg60IaH7qtg6+z4vigrzCw3Jz5QdRPHFNGTje8XwwVkJJMj FshHS/b/qHBlpcOw7rnEcQu9IUZOwq4RJFjLQcaA3BQAsW6hycKQyysY2rQhdmG2d5Z/ 69s7Utr+GLHOIbIg5s+ej//7XhT4blWE7rCMqNHtGgvwgvrYV6/WXosh64aZQ68z6gwl AI+IXo3pGViHpaSsdO5+5U4+kxmsaQL16s8KN8PlRXDY5JtD6D4wBOfJ3vtgNV1Fzn+9 yRAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=v0s47RLEahdlu3wIESuusFRH8qfPjSCa1JQZ9W371xo=; b=Ip5oApWkSsckgJdax11l3+ZtW+tULfyaE9/MMkHRXlVg4VM71Q/bYgc+2Cc9h596uq RjQDVUJJlejzgjCnvPgJjii1B+zusdJeEcj4HH8fO6JAEn06qSG+51sFLl2Wr9ebeExO Q9VyqlrDVspOp8UGKAteNxA3qw5kKxCAPBPuemwAzPB79nWu1X5BvCdCQEk9z5kWukgJ Aw7qT770tbwh7JOEQn+UW17xzpNsTxXMfH3dr+EK8/i6G/FSa7uyWD36aUsxzdzwNHPS ku6zTvyV1YUtE1eSY2XsZr584kHvwHVjv/NfjNtAYLDJPk0WY1v0MgRDtqdc1WB65t8w pgIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=U9drnlmm; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a15-v6si7977499pfn.248.2018.10.25.07.16.33; Thu, 25 Oct 2018 07:16:56 -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=U9drnlmm; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728024AbeJYWrY (ORCPT + 99 others); Thu, 25 Oct 2018 18:47:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:55398 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727433AbeJYWrY (ORCPT ); Thu, 25 Oct 2018 18:47:24 -0400 Received: from sasha-vm.mshome.net (unknown [167.98.65.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4821620848; Thu, 25 Oct 2018 14:14:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1540476868; bh=ZKdDmys+nQRBq0sJRabr2Eqdg5UasF/q45lbnaasF0A=; h=From:To:Cc:Subject:Date:From; b=U9drnlmmAhalGOLAbXIXGda0b1fNk8pWRrYB+AZVB2nwAZV/Cr90i7VkcHfowUNCC P3wv0uFxmuBWOX4z5xidrxT9awK77KLSu3fienxEnQW/eiNITClef5kx65ZOcz0Aun RnXzl0pfGc5iZ2URi3VNUCxsEt+az/MOD+dJUMtQ= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ravi Bangoria , Alexander Shishkin , Jin Yao , Kim Phillips , "Naveen N . Rao" , Peter Zijlstra , Taeung Song , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH AUTOSEL 4.9 01/98] perf symbols: Fix memory corruption because of zero length symbols Date: Thu, 25 Oct 2018 10:12:46 -0400 Message-Id: <20181025141423.213774-1-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ravi Bangoria [ Upstream commit 331c7cb307971eac38e9470340e10c87855bf4bc ] Perf top is often crashing at very random locations on powerpc. After investigating, I found the crash only happens when sample is of zero length symbol. Powerpc kernel has many such symbols which does not contain length details in vmlinux binary and thus start and end addresses of such symbols are same. Structure struct sym_hist { u64 nr_samples; u64 period; struct sym_hist_entry addr[0]; }; has last member 'addr[]' of size zero. 'addr[]' is an array of addresses that belongs to one symbol (function). If function consist of 100 instructions, 'addr' points to an array of 100 'struct sym_hist_entry' elements. For zero length symbol, it points to the *empty* array, i.e. no members in the array and thus offset 0 is also invalid for such array. static int __symbol__inc_addr_samples(...) { ... offset = addr - sym->start; h = annotation__histogram(notes, evidx); h->nr_samples++; h->addr[offset].nr_samples++; h->period += sample->period; h->addr[offset].period += sample->period; ... } Here, when 'addr' is same as 'sym->start', 'offset' becomes 0, which is valid for normal symbols but *invalid* for zero length symbols and thus updating h->addr[offset] causes memory corruption. Fix this by adding one dummy element for zero length symbols. Link: https://lkml.org/lkml/2016/10/10/148 Fixes: edee44be5919 ("perf annotate: Don't throw error for zero length symbols") Signed-off-by: Ravi Bangoria Acked-by: Jiri Olsa Acked-by: Namhyung Kim Cc: Alexander Shishkin Cc: Jin Yao Cc: Kim Phillips Cc: Naveen N. Rao Cc: Peter Zijlstra Cc: Taeung Song Link: http://lkml.kernel.org/r/1508854806-10542-1-git-send-email-ravi.bangoria@linux.vnet.ibm.com Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/util/annotate.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index a38227eb5450..3336cbc6ec48 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -495,9 +495,19 @@ static struct ins *ins__find(const char *name) int symbol__alloc_hist(struct symbol *sym) { struct annotation *notes = symbol__annotation(sym); - const size_t size = symbol__size(sym); + size_t size = symbol__size(sym); size_t sizeof_sym_hist; + /* + * Add buffer of one element for zero length symbol. + * When sample is taken from first instruction of + * zero length symbol, perf still resolves it and + * shows symbol name in perf report and allows to + * annotate it. + */ + if (size == 0) + size = 1; + /* Check for overflow when calculating sizeof_sym_hist */ if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64)) return -1; -- 2.17.1