From patchwork Thu Dec 5 08:44:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongqiu Han X-Patchwork-Id: 13894854 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (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 DA9561D5CE7; Thu, 5 Dec 2024 08:45:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733388357; cv=none; b=O3z4GVW23Dx1FysYk06wWEpaq2fDyvu3sUSnDH2QPl0IzY0v7fMb3s3ympkbhjf2odq+zKaoxok21CPQiee4dqflWmFDR72HOAlPdVfZoNrLKyB/L7wx4xywdk52osTYokk2HsyksWAVGdHXOWszaxvImpXULbex8z3GOaL7Umc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733388357; c=relaxed/simple; bh=TGub+nnAnx+mngP881hWmOhOaQbFve+Of/d7IUxl9rI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=I0eE3lynLT9BRdTt9b/t023qoAai7EaTmCC0dAyXVN7uvcHdFjYbqakh22d3o4fCv5o/j2U0lYSjxAPN1UIH6aVsRE62U+mBMVK55dNNzok53Hyh9u6WYdC8ux8S1iIpxwETBwTR3EkS6ljwB/oq3XwcrSNogwEkulh2FS3EPew= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com; spf=pass smtp.mailfrom=quicinc.com; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b=OwPLMNcM; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=quicinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="OwPLMNcM" Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4B56npgH011461; Thu, 5 Dec 2024 08:45:24 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= dAnrLVoF02cKvKJnUNPdsTtlf1wMvv8SdyQ2JCZLSG8=; b=OwPLMNcMhtm2zrnb kavwhJ1OcQhU1tB1xOQrjhWGEacod9Di9b4YiOKdo//DgVCCF5pb7X0i91/GNAFX xZQUbqehmWX60D4SuP2cwDzOGqrGwzy2ztrI7pcV6hGXcaGSDaag7a2dGYcvRfgN CvMMMUcAD/M5xPVskoH0ELMqv5/X1Z7aCBOcU1YBbijcFt/wdEBpmCHLzxJAkGJ8 ea2L3GSlFPolNByj9ikGfVnDpiHOz2JKsD9FJ+eafBcZWsDt0qgGjRW2V8hbEiM9 DnXMjlK6BSXVmCkMsMnj9muqd56aL3pVyHPVT7G5ZS2Fy5G82oAWmZDToAAPTVQ+ kNuZGg== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 43a4by5kh4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 05 Dec 2024 08:45:24 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA02.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 4B58jNJB003644 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 5 Dec 2024 08:45:23 GMT Received: from zhonhan-gv.qualcomm.com (10.80.80.8) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Thu, 5 Dec 2024 00:45:19 -0800 From: Zhongqiu Han To: , , , , , , , , , , , , CC: , , , Subject: [PATCH v2 1/3] perf header: Fix one memory leakage in process_bpf_btf() Date: Thu, 5 Dec 2024 16:44:58 +0800 Message-ID: <20241205084500.823660-2-quic_zhonhan@quicinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241205084500.823660-1-quic_zhonhan@quicinc.com> References: <20241205084500.823660-1-quic_zhonhan@quicinc.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: IhXAhm7f-ZBUPvRi_adCuRpNsB9LjIvN X-Proofpoint-ORIG-GUID: IhXAhm7f-ZBUPvRi_adCuRpNsB9LjIvN X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-06_09,2024-09-06_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 clxscore=1015 impostorscore=0 suspectscore=0 adultscore=0 phishscore=0 spamscore=0 priorityscore=1501 malwarescore=0 lowpriorityscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412050062 If __perf_env__insert_btf() returns false due to a duplicate btf node insertion, the temporary node will leak. Add a check to ensure the memory is freed if the function returns false. Fixes: a70a1123174a ("perf bpf: Save BTF information as headers to perf.data") Signed-off-by: Zhongqiu Han Reviewed-by: Namhyung Kim --- tools/perf/util/header.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 3451e542b69a..fbba6ffafec4 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -3205,7 +3205,8 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) if (__do_read(ff, node->data, data_size)) goto out; - __perf_env__insert_btf(env, node); + if (!__perf_env__insert_btf(env, node)) + free(node); node = NULL; } From patchwork Thu Dec 5 08:44:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongqiu Han X-Patchwork-Id: 13894852 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (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 04D3A1CEEBA; Thu, 5 Dec 2024 08:45:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733388346; cv=none; b=QF+bWHg5+7NLxu/cO9sf+q6l7SNWpMKEX3dI5RXSEAYJRSj3caWD0aiiWS+CWAch9q9VODJDXBEN8y0uEoxR6iDbGv7/Uuxll1CxDSOiUdHPKwe18yDcOpSWjzNrl6K1vEnalxwPP+AGaZu96KTsu1JFYiu3wsw8uacnpJeycNc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733388346; c=relaxed/simple; bh=iFZ3o1xxnzcVaBDT4+57NLsT9SRj9h7MlvKaAIpVpfA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jaPe9OdJeO4gx5EReCgtFN0VX1sfwED5Lg5dbixJUNba4yBNuy8eitjm14dse0OqNCnOxa3IxRXn8QJmYux8eLo9RfsOCSD7NaLJ3WWPleuHjHvn/fsyr69KDxlZ2wxNaPE0WlSscRE2eal1eRkLQkWx7yL6GyObzM4zSOisuNg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com; spf=pass smtp.mailfrom=quicinc.com; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b=BBWvJf2c; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=quicinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="BBWvJf2c" Received: from pps.filterd (m0279866.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4B4LvCuk031569; Thu, 5 Dec 2024 08:45:28 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= H29Tij9CROql8/xjcaPE5wTAS8wEjhzGuX8aaYvWiUo=; b=BBWvJf2cWImKLoKE icJqewwbSOzdRZORANbSF88vhzikZaJfquyx65ZM/eNf7Jo9PQql//+TjY/FQkYq tREvayKejNc6n8mrHJfsaS8aUC127KHTuL7bm88Og1FHNWI46g/mNKCb5lFjtPzC dBnr+BKUphJLuWJr2XMcwhIuREi5TRv3RQSCbQXF97eBD/XUJdEsHsc79rUUcScp /fI31tlknaaSNSGE6e4J0CerLjxEbb9xFaJr3WFwapIYIO6TKYDs016j8cYLSayZ i73KYp9mW7sp15JAqDuQ750EgmUlmzrF4vemmkpk25qIAB4lQ/X31wWTZxOCgREe /M6nIw== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 43ayem9ar2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 05 Dec 2024 08:45:28 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA05.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 4B58jRCl012520 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 5 Dec 2024 08:45:28 GMT Received: from zhonhan-gv.qualcomm.com (10.80.80.8) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Thu, 5 Dec 2024 00:45:23 -0800 From: Zhongqiu Han To: , , , , , , , , , , , , CC: , , , Subject: [PATCH v2 2/3] perf header: Fix one memory leakage in process_bpf_prog_info() Date: Thu, 5 Dec 2024 16:44:59 +0800 Message-ID: <20241205084500.823660-3-quic_zhonhan@quicinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241205084500.823660-1-quic_zhonhan@quicinc.com> References: <20241205084500.823660-1-quic_zhonhan@quicinc.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: noWfPXCPqgnXOigHQC22Wif7S-BViOg6 X-Proofpoint-ORIG-GUID: noWfPXCPqgnXOigHQC22Wif7S-BViOg6 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-06_09,2024-09-06_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 priorityscore=1501 mlxlogscore=999 phishscore=0 malwarescore=0 mlxscore=0 adultscore=0 lowpriorityscore=0 bulkscore=0 suspectscore=0 spamscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412050062 Function __perf_env__insert_bpf_prog_info() will return without inserting bpf prog info node into perf env again due to a duplicate bpf prog info node insertion, causing the temporary info_linear and info_node memory to leak. Modify the return type of this function to bool and add a check to ensure the memory is freed if the function returns false. Fixes: 606f972b1361 ("perf bpf: Save bpf_prog_info information as headers to perf.data") Signed-off-by: Zhongqiu Han Reviewed-by: Namhyung Kim --- tools/perf/util/env.c | 5 +++-- tools/perf/util/env.h | 2 +- tools/perf/util/header.c | 5 ++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index e2843ca2edd9..d7865ae5f8f5 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -32,7 +32,7 @@ void perf_env__insert_bpf_prog_info(struct perf_env *env, up_write(&env->bpf_progs.lock); } -void __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node) +bool __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node) { __u32 prog_id = info_node->info_linear->info.id; struct bpf_prog_info_node *node; @@ -50,13 +50,14 @@ void __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info p = &(*p)->rb_right; } else { pr_debug("duplicated bpf prog info %u\n", prog_id); - return; + return false; } } rb_link_node(&info_node->rb_node, parent, p); rb_insert_color(&info_node->rb_node, &env->bpf_progs.infos); env->bpf_progs.infos_cnt++; + return true; } struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env, diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h index ae604c4edbb7..9db2e5a625ed 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -176,7 +176,7 @@ const char *perf_env__raw_arch(struct perf_env *env); int perf_env__nr_cpus_avail(struct perf_env *env); void perf_env__init(struct perf_env *env); -void __perf_env__insert_bpf_prog_info(struct perf_env *env, +bool __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node); void perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node); diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index fbba6ffafec4..d06aa86352d3 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -3158,7 +3158,10 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused) /* after reading from file, translate offset to address */ bpil_offs_to_addr(info_linear); info_node->info_linear = info_linear; - __perf_env__insert_bpf_prog_info(env, info_node); + if (!__perf_env__insert_bpf_prog_info(env, info_node)) { + free(info_linear); + free(info_node); + } } up_write(&env->bpf_progs.lock); From patchwork Thu Dec 5 08:45:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongqiu Han X-Patchwork-Id: 13894853 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (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 642601C3C1B; Thu, 5 Dec 2024 08:45:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733388351; cv=none; b=qK34THNsZd6AyYFzMsJb4+v3iOx72ROKHA7P1IdLNHvV253vhopjkAVD2/iI5bVKTtvY38vBTlaljTtIuRvDOnf/9h3mStM6oUCVmThwX90UUIptWP3Jz9J8GKFOLtVF1oCqk2Gm9QjqUZCb6K2m1W3xcjtkBaXo++YSd+jIBb0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733388351; c=relaxed/simple; bh=yBxMDzLUQ+UUqEGx4gccp27vhPftyCQVLW4rYP1jVdM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bufkvKm47C9Ia2dLOYD9V4dyU2w4SliHCA6Fxk19GgZKvVtfqqmGEgMKRIvdeenC7IkGI3rezqlZ6MDl5f7oyqrsGiYOfdV+xQcYa+fsblKnBgCgNvIxPsSkFAfUvbdyJPtjegwXSy62WQspKSYflXiV8TotCMU4M7sQ2Ygliv0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com; spf=pass smtp.mailfrom=quicinc.com; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b=LVqBCr2m; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=quicinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="LVqBCr2m" Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4B55iRSX032114; Thu, 5 Dec 2024 08:45:33 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= bKfgrYa/QFrkOV0zx4Ss0Pk/DtiPxo2P5UZjLA/HY/g=; b=LVqBCr2m70ZUx1eF Pp5pB77RvZPgV15WB5i+7OX7uL6wy90AD8GzKGZei5vGJJdmS7VGzi0dYTlP5Stt Ty4tAgVKK8Ep+Nehf/827/eS1+kMCBByezripwrluGhKxW0CnfjZpVI9bl66z+c2 apIltRM5QRE7x+ILPIyHfYVgJ2oFvhIr8g3iKg7FUsKZO9ssEIU9Q9t5CjvSH9cy eWlWY2Rb6VtT0VzQ37pkSd313so9PlhKXJE6jF/0LOOO7x/C1UNuFiU2aTi7+a/q 9znam1RdGYwg0PxQBH/pjrvIazFFXFjZ+heYg0NlBcjbPL3wPjXeGeA8HnXYmp3v dwS29g== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 43a3fawtvt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 05 Dec 2024 08:45:32 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA05.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 4B58jWnd012553 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 5 Dec 2024 08:45:32 GMT Received: from zhonhan-gv.qualcomm.com (10.80.80.8) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Thu, 5 Dec 2024 00:45:28 -0800 From: Zhongqiu Han To: , , , , , , , , , , , , CC: , , , Subject: [PATCH v2 3/3] perf bpf: Fix two memory leakages when calling perf_env__insert_bpf_prog_info() Date: Thu, 5 Dec 2024 16:45:00 +0800 Message-ID: <20241205084500.823660-4-quic_zhonhan@quicinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241205084500.823660-1-quic_zhonhan@quicinc.com> References: <20241205084500.823660-1-quic_zhonhan@quicinc.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: -GyyUAnOy5XPi3guRMJW6aOqz-G5-Zmv X-Proofpoint-GUID: -GyyUAnOy5XPi3guRMJW6aOqz-G5-Zmv X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-06_09,2024-09-06_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 malwarescore=0 spamscore=0 mlxscore=0 suspectscore=0 adultscore=0 mlxlogscore=999 lowpriorityscore=0 clxscore=1015 priorityscore=1501 phishscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412050062 If perf_env__insert_bpf_prog_info() returns false due to a duplicate bpf prog info node insertion, the temporary info_node and info_linear memory will leak. Add a check to ensure the memory is freed if the function returns false. Fixes: e4378f0cb90b ("perf bpf: Save bpf_prog_info in a rbtree in perf_env") Fixes: d56354dc4909 ("perf tools: Save bpf_prog_info and BTF of new BPF programs") Signed-off-by: Zhongqiu Han Reviewed-by: Namhyung Kim --- tools/perf/util/bpf-event.c | 10 ++++++++-- tools/perf/util/env.c | 8 ++++++-- tools/perf/util/env.h | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index 13608237c50e..c81444059ad0 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -289,7 +289,10 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session, } info_node->info_linear = info_linear; - perf_env__insert_bpf_prog_info(env, info_node); + if (!perf_env__insert_bpf_prog_info(env, info_node)) { + free(info_linear); + free(info_node); + } info_linear = NULL; /* @@ -480,7 +483,10 @@ static void perf_env__add_bpf_info(struct perf_env *env, u32 id) info_node = malloc(sizeof(struct bpf_prog_info_node)); if (info_node) { info_node->info_linear = info_linear; - perf_env__insert_bpf_prog_info(env, info_node); + if (!perf_env__insert_bpf_prog_info(env, info_node)) { + free(info_linear); + free(info_node); + } } else free(info_linear); diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index d7865ae5f8f5..a6321e7f0633 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -24,12 +24,16 @@ struct perf_env perf_env; #include "bpf-utils.h" #include -void perf_env__insert_bpf_prog_info(struct perf_env *env, +bool perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node) { + bool ret; + down_write(&env->bpf_progs.lock); - __perf_env__insert_bpf_prog_info(env, info_node); + ret = __perf_env__insert_bpf_prog_info(env, info_node); up_write(&env->bpf_progs.lock); + + return ret; } bool __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node) diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h index 9db2e5a625ed..da11add761d0 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -178,7 +178,7 @@ int perf_env__nr_cpus_avail(struct perf_env *env); void perf_env__init(struct perf_env *env); bool __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node); -void perf_env__insert_bpf_prog_info(struct perf_env *env, +bool perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node); struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env, __u32 prog_id);