From patchwork Tue Jun 6 19:15:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaldo Carvalho de Melo X-Patchwork-Id: 104076 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3623645vqr; Tue, 6 Jun 2023 12:31:32 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5DFHS2Njsaz/zNjwPwiPiSQaMiZOj6rzyY2WoYLCCr79xghvKw+56wwehhT0QyP+xlgx8l X-Received: by 2002:a05:622a:112:b0:3f4:ee64:e30c with SMTP id u18-20020a05622a011200b003f4ee64e30cmr830677qtw.52.1686079892590; Tue, 06 Jun 2023 12:31:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686079892; cv=none; d=google.com; s=arc-20160816; b=KZRGZPlZLJALyFeD8a0fXHYi+eiIVPXr1tXJ3Sr0drIDvPi3w7Pb7Pa+aX6gQlelYi XHF+Sis8tdFV2PQmEADT/iD9FWpKfln1o6q5F/sPt11Fk7d9BuE/HfgD1a/J2uPviq7z nqcgw8I/xXsBzA/kojEwhOjSkgLRUfhFIGUEDDwCtaoHj/etAiM2Wh80cw9pK7HuTlBY fUevrfZL+tpnXIgm/fE4DRTxQOEvOUUU2gqoyTk9sBn4zLY+RbV9ZfMxwnpsLISqD/N9 dTFG/bTKJVdum7FYUq7EgwVHzX7NQLQq9EnSJrcUzmSKe8zrIMEPvHyHWq4LKexG1avc AvYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=OwE656OPSshaTqQ7vJUTovNk49Nnq+6M5PRtvWyMHxY=; b=NHcI18fp7AQrx+pIzRQboGBlipkoApnngYvOk01PU8gqhpjRZe7KcQ7u8sB+wcy/YB tXHsf0FwLKDFww6wtpJLtWDVVmGEHMrdnFEeZ/V0vSBv9cShrKLLef7o+5cA9ksT+p5Z najSoHjUd38cXeHAZiKq9vdC7EwQ7xNi1M+uQ8Nj2mdoY+WZD54GyoIbnUbnWV4tgewv k61GO6lUcQ3BVMn9zenWeI332lXfxA+Jfj9ieix2jgSja9AXy6jVagIc6HO/WSi5V4Ab G3c4aoJFKL+OKCn5yphOrk8Y7Bojdv/Ip2fnBlxIxBBijXM4hWKoHr0LCgI9BFEfyrFK jFsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qLQHN19t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m14-20020ac85b0e000000b003f541337f13si6572794qtw.689.2023.06.06.12.31.18; Tue, 06 Jun 2023 12:31:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qLQHN19t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S238980AbjFFTQI (ORCPT + 99 others); Tue, 6 Jun 2023 15:16:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237630AbjFFTP5 (ORCPT ); Tue, 6 Jun 2023 15:15:57 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9644C1992 for ; Tue, 6 Jun 2023 12:15:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E8AFE63081 for ; Tue, 6 Jun 2023 19:15:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B88AC433D2; Tue, 6 Jun 2023 19:15:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686078934; bh=oDfcC99H7vL4Ph3/RETkW7NPppuo29jYZmhGR4hW7I0=; h=Date:From:To:Cc:Subject:From; b=qLQHN19tTxoYONb6oCJkxwQuZG5qvi1k/kmzTS8nY2Qnnop+MkOH/F0VGO/kG+uoZ 1MKE+8mupT15VCTjLwKtQ/H79M3caQp6/XF81haR4ldQLyDIFL06aiFRBPMAoNRNBm J2jDR3p8o+nLTX8WvX4i3HGJWoa27GFG28O9x5LDjVPVuIdb9pH9q7dzmVnErDLta8 2HfNtBpfpqGOciYJKbeAjveHNmlJhfenGKvuJjVV7LnxiZBO/hPZZgAV6+so+ruIhC EEiEEFCbEVayBxjoiyD9apGWvLvnsnL81IZtepc2PY6KgfkzsumWR0SWjdAplUL07K Y6hcD0a26Q17Q== Received: by quaco.ghostprotocols.net (Postfix, from userid 1000) id 7136A40692; Tue, 6 Jun 2023 16:15:31 -0300 (-03) Date: Tue, 6 Jun 2023 16:15:31 -0300 From: Arnaldo Carvalho de Melo To: Ravi Bangoria Cc: Ian Rogers , Adrian Hunter , Jiri Olsa , Namhyung Kim , Linux Kernel Mailing List Subject: [PATCH 1/1] perf script: Fix allocation of evsel->priv related to per-event dump files Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767982909641653129?= X-GMAIL-MSGID: =?utf-8?q?1767982909641653129?= I'm carrying this in my perf-tools-next, please ack :-) - Arnaldo Tested-by: Ian Rogers --- When printing output we may want to generate per event files, where the --per-event-dump option should be used, creating perf.data.EVENT.dump files instead of printing to stdout. The callback thar processes event thus expects that evsel->priv->fp should point to either the per-event FILE descriptor or to stdout. The a3af66f51bd0bca7 ("perf script: Fix crash because of missing evsel->priv") changeset fixed a case where evsel->priv wasn't setup, thus set to NULL, causing a segfault when trying to access evsel->priv->fp. But it did it for the non --per-event-dump case by allocating a 'struct perf_evsel_script' just to set its ->fp to stdout. Since evsel->priv is only freed when --per-event-dump is used, we ended up with a memory leek, detected using ASAN. Fix it by using the same method as perf_script__setup_per_event_dump(), and reuse that static 'struct perf_evsel_script'. Also check if evsel_script__new() failed. Fixes: a3af66f51bd0bca7 ("perf script: Fix crash because of missing evsel->priv") Reported-by: Ian Rogers Cc: Adrian Hunter Cc: Jiri Olsa Cc: Namhyung Kim Cc: Ravi Bangoria Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-script.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 70549fc93b125394..b02ad386a55baf07 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -2410,6 +2410,9 @@ static int process_sample_event(struct perf_tool *tool, return ret; } +// Used when scr->per_event_dump is not set +static struct evsel_script es_stdout; + static int process_attr(struct perf_tool *tool, union perf_event *event, struct evlist **pevlist) { @@ -2418,7 +2421,6 @@ static int process_attr(struct perf_tool *tool, union perf_event *event, struct evsel *evsel, *pos; u64 sample_type; int err; - static struct evsel_script *es; err = perf_event__process_attr(tool, event, pevlist); if (err) @@ -2428,14 +2430,13 @@ static int process_attr(struct perf_tool *tool, union perf_event *event, evsel = evlist__last(*pevlist); if (!evsel->priv) { - if (scr->per_event_dump) { + if (scr->per_event_dump) { evsel->priv = evsel_script__new(evsel, scr->session->data); - } else { - es = zalloc(sizeof(*es)); - if (!es) + if (!evsel->priv) return -ENOMEM; - es->fp = stdout; - evsel->priv = es; + } else { // Replicate what is done in perf_script__setup_per_event_dump() + es_stdout.fp = stdout; + evsel->priv = &es_stdout; } } @@ -2741,7 +2742,6 @@ static int perf_script__fopen_per_event_dump(struct perf_script *script) static int perf_script__setup_per_event_dump(struct perf_script *script) { struct evsel *evsel; - static struct evsel_script es_stdout; if (script->per_event_dump) return perf_script__fopen_per_event_dump(script);