From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756691AbbIUMgJ (ORCPT ); Mon, 21 Sep 2015 08:36:09 -0400 Received: from mga03.intel.com ([134.134.136.65]:54943 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752695AbbIUMgH (ORCPT ); Mon, 21 Sep 2015 08:36:07 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,567,1437462000"; d="scan'208";a="809759590" Subject: Re: [PATCH] perf tools: session: avoid infinite loop To: Arnaldo Carvalho de Melo , Mark Rutland References: <1442423929-12253-1-git-send-email-mark.rutland@arm.com> <20150916205454.GI11551@kernel.org> <20150917154152.GD12808@leverpostej> <55FBAA8C.2020703@intel.com> <20150918095112.GA21286@leverpostej> <55FBED85.8020603@intel.com> <20150918133708.GA26468@leverpostej> <20150918150018.GS11551@kernel.org> <20150918151851.GC31683@leverpostej> <20150918152957.GU11551@kernel.org> Cc: "linux-kernel@vger.kernel.org" , Ingo Molnar , Jiri Olsa , Peter Zijlstra From: Adrian Hunter Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki Message-ID: <55FFF909.9020107@intel.com> Date: Mon, 21 Sep 2015 15:33:13 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <20150918152957.GU11551@kernel.org> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 18/09/15 18:29, Arnaldo Carvalho de Melo wrote: > Em Fri, Sep 18, 2015 at 04:18:51PM +0100, Mark Rutland escreveu: >> On Fri, Sep 18, 2015 at 04:00:18PM +0100, Arnaldo Carvalho de Melo wrote: >>> After applying your patch it works, had to tweak the commit log to avoid >>> starting lines with ---, breaks git scripts, also added a commiter log, >>> check it, patch is below, after the one I used to not process any >>> samples. > >> Sorry for the '---' problem, I'll bear that in mind in future. > >> Your commit log looks fine, though I'm slightly confused by the >> Reported-by line -- did you mean to add that? > > Sorry, I mean Tested-by:, will replace, guess i can replace the one for > Adrian from Cc: to Tested-by too, right? Yes > > - Arnaldo > >> Mark. >> >>> commit dd486ec4aa33cfca2fd912ef501d49909005de79 >>> Author: Mark Rutland >>> Date: Wed Sep 16 18:18:49 2015 +0100 >>> >>> perf record: Avoid infinite loop at buildid processing with no samples >>> >>> If a session contains no events, we can get stuck in an infinite loop in >>> __perf_session__process_events, with a non-zero file_size and data_offset, but >>> a zero data_size. >>> >>> In this case, we can mmap the entirety of the file (consisting of the file and >>> attribute headers), and fetch_mmaped_event will correctly refuse to read any >>> (unmapped and non-existent) event headers. This causes >>> __perf_session__process_events to unmap the file and retry with the exact same >>> parameters, getting stuck in an infinite loop. >>> >>> This has been observed to result in an exit-time hang when counting >>> rare/unschedulable events with perf record, and can be triggered artificially >>> with the script below: >>> >>> ---- >>> #!/bin/sh >>> printf "REPRO: launching perf\n"; >>> ./perf record -e software/config=9/ sleep 1 & >>> PERF_PID=$!; >>> sleep 0.002; >>> kill -2 $PERF_PID; >>> printf "REPRO: waiting for perf (%d) to exit...\n" "$PERF_PID"; >>> wait $PERF_PID; >>> printf "REPRO: perf exited\n"; >>> ---- >>> >>> To avoid this, have __perf_session__process_events bail out early when >>> the file has no data (i.e. it has no events). >>> >>> Commiter note: >>> >>> I only managed to reproduce this when setting >>> /proc/sys/kernel/kptr_restrict to '1' and changing the code to >>> purposefully not process any samples and no synthesized samples, i.e. >>> kptr_restrict prevents 'record' from synthesizing the kernel mmaps for >>> vmlinux + modules and since it is a workload started from perf, we don't >>> synthesize mmap/comm records for existing threads. >>> >>> Adrian Hunter managed to reproduce it in his environment tho. >>> >>> Reported-by: Arnaldo Carvalho de Melo >>> Signed-off-by: Mark Rutland >>> Cc: Adrian Hunter >>> Cc: Jiri Olsa >>> Cc: Peter Zijlstra >>> Link: http://lkml.kernel.org/r/1442423929-12253-1-git-send-email-mark.rutland@arm.com >>> Signed-off-by: Arnaldo Carvalho de Melo >>> >>> diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c >>> index 8a4537ee9bc3..fc3f7c922f99 100644 >>> --- a/tools/perf/util/session.c >>> +++ b/tools/perf/util/session.c >>> @@ -1580,7 +1580,10 @@ static int __perf_session__process_events(struct perf_session *session, >>> file_offset = page_offset; >>> head = data_offset - page_offset; >>> >>> - if (data_size && (data_offset + data_size < file_size)) >>> + if (data_size == 0) >>> + goto out; >>> + >>> + if (data_offset + data_size < file_size) >>> file_size = data_offset + data_size; >>> >>> ui_progress__init(&prog, file_size, "Processing events..."); >>> >