From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57B2FC433EF for ; Tue, 28 Jun 2022 02:20:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243600AbiF1CUw (ORCPT ); Mon, 27 Jun 2022 22:20:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243527AbiF1CU3 (ORCPT ); Mon, 27 Jun 2022 22:20:29 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4ACCB2495F; Mon, 27 Jun 2022 19:19:59 -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 A4234617D9; Tue, 28 Jun 2022 02:19:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DDFA6C385A5; Tue, 28 Jun 2022 02:19:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1656382798; bh=2RvOCUttCJce1i+gUQTcZb/oD0fI5ZPODIDnk1o4vqA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kyOhvOAWeIaAov3IDVAkbO3rn7BbIAXhpaaQuOQR9twRVHTar0ORj3Mm9XZESAIp9 n+xuo5V3ZjR76Y2HTxgCJYpuS8hSQMUFz3FXP+kIkeS74pw0XBAoiNnc1wnVqm+Lae 4VS0BZPSRsk/Xrqw/+zGyzfCMYIZXM8yIbgkQON0ACrKaD9QuiT7C5xullwWXAB/HI olx/6+u5XPV9NLMKW2SGZrxwD361dUAqaEvmR+qyH6u/M1mwkpm0C73RoSW+A3Pb5U tBubsHDZLA2qkye6UFDRHBE3jh7xEn3UzAXDj1j8kA8xrHta538EjSdEYm0MR9sO/k y+4relQhZeDrA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ian Rogers , Kajol Jain , Adrian Hunter , Alexander Shishkin , Andi Kleen , Anshuman Khandual , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Peter Zijlstra , Rob Herring , Stephane Eranian , Arnaldo Carvalho de Melo , Sasha Levin , acme@kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH AUTOSEL 5.18 26/53] libperf evsel: Open shouldn't leak fd on failure Date: Mon, 27 Jun 2022 22:18:12 -0400 Message-Id: <20220628021839.594423-26-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220628021839.594423-1-sashal@kernel.org> References: <20220628021839.594423-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ian Rogers [ Upstream commit 94725994cfd768b9ee1bd06f15c252694b1e9b89 ] If perf_event_open() fails the fd is opened but it is only freed by closing (not by delete). Typically when an open fails you don't call close and so this results in a memory leak. To avoid this, add a close when open fails. Signed-off-by: Ian Rogers Reviewed-By: Kajol Jain Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Andi Kleen Cc: Anshuman Khandual Cc: Ingo Molnar Cc: Jiri Olsa Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Rob Herring Cc: Stephane Eranian Link: https://lore.kernel.org/r/20220609052355.1300162-2-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/lib/perf/evsel.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c index 210ea7c06ce8..acf8a215dca3 100644 --- a/tools/lib/perf/evsel.c +++ b/tools/lib/perf/evsel.c @@ -149,23 +149,30 @@ int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus, int fd, group_fd, *evsel_fd; evsel_fd = FD(evsel, idx, thread); - if (evsel_fd == NULL) - return -EINVAL; + if (evsel_fd == NULL) { + err = -EINVAL; + goto out; + } err = get_group_fd(evsel, idx, thread, &group_fd); if (err < 0) - return err; + goto out; fd = sys_perf_event_open(&evsel->attr, threads->map[thread].pid, cpu, group_fd, 0); - if (fd < 0) - return -errno; + if (fd < 0) { + err = -errno; + goto out; + } *evsel_fd = fd; } } +out: + if (err) + perf_evsel__close(evsel); return err; } -- 2.35.1