From patchwork Mon Feb 5 10:46:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Jihong X-Patchwork-Id: 196729 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp793670dyb; Mon, 5 Feb 2024 02:51:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IGyaXJEr3+RxilXWW389fA1T0b3K7df2m8aE9Gyw3L6bq84Rp07wP6HF+iTKOn0jOBLD3v0 X-Received: by 2002:a17:906:eb5b:b0:a37:9de6:5370 with SMTP id mc27-20020a170906eb5b00b00a379de65370mr2415571ejb.0.1707130318433; Mon, 05 Feb 2024 02:51:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707130318; cv=pass; d=google.com; s=arc-20160816; b=ivmxR8V3UVcQkPy4b7XXh/s6nGTfoEONVnkqvbYy7bShAZZ5PSDmMWsRC/j4AuWcUM +Ps/lojv7Yp0VXYbqmCsPlL85mUKN6qomNAkUlxlSfqQHsc0g6qvG9whftAXO6/CVkK3 Tuvxsb+zlgQqZJEEZi+QFdeajuT2iRH/vcjXRZt46g9efOqeG+Ddov9JXaViCySPe6hQ +XOi2effKRHTiAuCoubYL0WDrfGOMngaKBrQqgyiexZgQPYYjExCpxNbprV/CA+90MQ+ X7WzWOTToZDh27JDaQbBZdouztl9O7bILO1kg+w3xZkWPKJm3n3C4Un/EPAo3bhvt1qt PX8A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=6hMgtE+VLE8fUIyUiIEBq+MWzTpXjrltrxxvXdQgW8Q=; fh=YuQ2IZZ/bEjumPt0Jx6fKIHTkMWq87RWvMavvJumcAk=; b=D/GaZ9EOSIOT69DkHElzTDhQHPFNqicTMp8r4wToGDyEzN0Bomgq5Z6dFzv2LWJW1t S7Kkye1OQU3BaCGjBpfZIBaVF7I9HGnBJlnWsTFt1bxRAp6VrJdM+IJ2FPTGvmZ9cdTb /36b88Dq85i+hEZDUTiJUlbNRlLcl8rhZOusFdShlIQ5EDWZInq3K6apAh6g9Lj12pBg 60/UHfPEYvXj3177ivhjV9r0nI8liswJXjjSSK4xeX99ti5futWlI1EHeVMmxL7w85V5 7VPzSpW9NGuTcrSoYqPRkoxnHUwYMQkkfarrOUZncPXFXRuxn/h7KQ/oZUsMGPNdLg6+ eD2w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-52434-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52434-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com X-Forwarded-Encrypted: i=1; AJvYcCVK4B4nyrfVjHOnbBz68mLyvPkiRmzfaTIBiKApmUigqBjFTu99KUOMlLAsPPPqcN5e+01iwPeBhL51Vj4TxjJy0M0f3w== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id lo22-20020a170906fa1600b00a35dce64af7si3968584ejb.211.2024.02.05.02.51.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 02:51:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52434-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-52434-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52434-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0FD021F21C67 for ; Mon, 5 Feb 2024 10:51:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 27AE117C95; Mon, 5 Feb 2024 10:51:20 +0000 (UTC) Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0AD9B1758C; Mon, 5 Feb 2024 10:51:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707130278; cv=none; b=fyzK+ZSc+wZTIxPm+xorlaVBVce+MHc9KRMlig+jIgLfhxKnLjC4VnoeKbMje2msIxUCoGQxa1sPMJbbtZfjS6XryTp8Uy1YtHaThAqbqVV4g5yiR9qRc940vmt7grY+ZukP8tMWg0kJvpvSuqGCxkMoGpwEaXDIeYEBV2M4qDI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707130278; c=relaxed/simple; bh=JjeSrbemceHFE/nambjoDi3fcqhVMNeiizzzXuFlnPs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qXUKg4nkc8+Oqa6VTx8vj9U34KsY6M0OpbTQCw4NJH+6LzTcb7RjVKB6yUUEWDvxQqJ08TcZKn0zaB+219N2F+A/Txq1zTl632fzs9i7tbt2JoXsUR1jtnCsbjcjSW68W+B5LgTAo0quW3KR9isykHx778H9pQLG35RIuBnQ22U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4TT37p37zpz1vsVw; Mon, 5 Feb 2024 18:50:46 +0800 (CST) Received: from kwepemm600003.china.huawei.com (unknown [7.193.23.202]) by mail.maildlp.com (Postfix) with ESMTPS id ABC9B140153; Mon, 5 Feb 2024 18:51:13 +0800 (CST) Received: from ubuntu2204.huawei.com (10.67.174.22) by kwepemm600003.china.huawei.com (7.193.23.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 5 Feb 2024 18:51:13 +0800 From: Yang Jihong To: , , , , , , , , , , CC: Subject: [PATCH 2/5] perf sched: Fix memory leak in perf_sched__map() Date: Mon, 5 Feb 2024 10:46:13 +0000 Message-ID: <20240205104616.132417-3-yangjihong1@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240205104616.132417-1-yangjihong1@huawei.com> References: <20240205104616.132417-1-yangjihong1@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemm600003.china.huawei.com (7.193.23.202) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790055881074202520 X-GMAIL-MSGID: 1790055881074202520 perf_sched__map() needs to free memory of map_cpus, color_pids and color_cpus in normal path and rollback allocated memory in error path. Signed-off-by: Yang Jihong --- tools/perf/builtin-sched.c | 41 ++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 08dec557e6be..26dbfa4aab61 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -3208,8 +3208,6 @@ static int perf_sched__lat(struct perf_sched *sched) static int setup_map_cpus(struct perf_sched *sched) { - struct perf_cpu_map *map; - sched->max_cpu.cpu = sysconf(_SC_NPROCESSORS_CONF); if (sched->map.comp) { @@ -3218,16 +3216,15 @@ static int setup_map_cpus(struct perf_sched *sched) return -1; } - if (!sched->map.cpus_str) - return 0; - - map = perf_cpu_map__new(sched->map.cpus_str); - if (!map) { - pr_err("failed to get cpus map from %s\n", sched->map.cpus_str); - return -1; + if (sched->map.cpus_str) { + sched->map.cpus = perf_cpu_map__new(sched->map.cpus_str); + if (!sched->map.cpus) { + pr_err("failed to get cpus map from %s\n", sched->map.cpus_str); + free(sched->map.comp_cpus); + return -1; + } } - sched->map.cpus = map; return 0; } @@ -3267,20 +3264,34 @@ static int setup_color_cpus(struct perf_sched *sched) static int perf_sched__map(struct perf_sched *sched) { + int rc = -1; + if (setup_map_cpus(sched)) - return -1; + return rc; if (setup_color_pids(sched)) - return -1; + goto out_free_map_cpus; if (setup_color_cpus(sched)) - return -1; + goto out_free_color_pids; setup_pager(); if (perf_sched__read_events(sched)) - return -1; + goto out_free_color_cpus; + + rc = 0; print_bad_events(sched); - return 0; + +out_free_color_cpus: + perf_cpu_map__put(sched->map.color_cpus); + +out_free_color_pids: + perf_thread_map__put(sched->map.color_pids); + +out_free_map_cpus: + free(sched->map.comp_cpus); + perf_cpu_map__put(sched->map.cpus); + return rc; } static int perf_sched__replay(struct perf_sched *sched)