From patchwork Thu Nov 28 12:54:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongqiu Han X-Patchwork-Id: 13888043 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 D68A91974FE; Thu, 28 Nov 2024 12:55:17 +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=1732798519; cv=none; b=LR+u4Wmli5jZB+5jQXXUghZ2aKKeEWJXbMc1oE3IjpiHAbfOEWUtFUu8CBBuGSUqetw1uPuUOnoutNwGJchxzAzIJVr9STDWPs2O916m2dvccPfQOfLuLXg/oQtlPl5pjJgBc10U8McbF6HK4iVxTXwEUQ8qU7ETCf261RU36cM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732798519; c=relaxed/simple; bh=JsAoCWQzDKT9XJikk7iuUO7M81cmO0TUGFcx6oxdF2Q=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VvnH8JtNnQ3O+dbg3R6KxBPIc+nLECc5GYLKbuSywEIUdOBkHKZFCJ8UXiXPKrEip6YKMvZZqJdNwV8+TPuwLJNwR3Sqm8LWYJSgNO97EuwBgsM+lncaPosIWiBrQ/klQd6ZL+saF9gA293SuXkHmrBRT9P3SYwy5PTxZ+ttRGE= 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=baBUa5sY; 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="baBUa5sY" Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AS3vZao001764; Thu, 28 Nov 2024 12:54:56 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= 29RZB+i2QIszPt3l7EQh/V4QnzI2GEl5xGd2j6YKF5Y=; b=baBUa5sYxYpHdTwr yv0QYlYAbzxSQswVioh+vvlY3SHS9QAHHpZ9QV6/8zE1thEHrzOF5xvklZH+gck2 /s2lfmfT8y/JOPCwMWo0XenZsx3QmtHj02EPXUeyjC/HJPTOh6QnALbgRYoBvNt4 4nKutabSKbESiIcLGpEMcySbkiBoh8FC7QREQFeKxrL7iJLj8V56TQBz7gJPTtRU ay4XL5Q5AnZFZaRrWYtRn2aTzF7yP2Omh23MYS1QFaZw8KWcwFbjjlM7vkonrZaE I3s+B5YVuYuCz6OVROg/eAp8vRN+RNdgd0cG0uyM5pdSuCYRfuNgwuRFPbsH5xBM 4dpBnQ== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 436h2mhfwv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 28 Nov 2024 12:54:56 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA01.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 4ASCstD5028203 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 28 Nov 2024 12:54:55 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, 28 Nov 2024 04:54:50 -0800 From: Zhongqiu Han To: , , , , , , , , , , , , CC: , , , Subject: [PATCH 1/3] perf header: Fix one memory leakage in process_bpf_btf() Date: Thu, 28 Nov 2024 20:54:30 +0800 Message-ID: <20241128125432.2748981-2-quic_zhonhan@quicinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241128125432.2748981-1-quic_zhonhan@quicinc.com> References: <20241128125432.2748981-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: BIumSVCJ8Kr6pXFiT_yFHoAb2BmEoTaj X-Proofpoint-ORIG-GUID: BIumSVCJ8Kr6pXFiT_yFHoAb2BmEoTaj 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 priorityscore=1501 bulkscore=0 malwarescore=0 phishscore=0 suspectscore=0 adultscore=0 spamscore=0 lowpriorityscore=0 mlxscore=0 impostorscore=0 clxscore=1011 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2411280101 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: 9c51f8788b5d ("perf env: Avoid recursively taking env->bpf_progs.lock") Signed-off-by: Zhongqiu Han --- 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 Nov 28 12:54:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongqiu Han X-Patchwork-Id: 13888042 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 BE092196C7C; Thu, 28 Nov 2024 12:55:16 +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=1732798518; cv=none; b=b75AtPihBclYhoVEqvhfkOhPchqK48z1WaoAOOyBSF3HPnc9JNExdb3mQDkWkMH8F2XFVhFgD655MiJd8zzdpIK86Z3oa6ru7m9wcCcU7CQCyNhOwkIJpCC4F5w6N0auqJMiI22P03CqD6BW7iWgEW6Y3i/qSyasoz/I6CX1OxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732798518; c=relaxed/simple; bh=sEtPShFiLdJcTxDqs3y2IAOYSKdGY4QhhxTPO+e7zUM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uf42xwf2Hh1CK9JrOAtGxO3oN6wG0pD+fH5AAZQ/5mgC9f91hr38NUgXqYeJUJS1pM8DIKuwkjSCYGTADGk3EYCQmfa0jJtxfkyB8bAzfsvOedhl0T9zWIuSPVVuzN9iJOWdPqLE/Z6SPsrv7AeXORb6FxqkAAVgfWCncojJxak= 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=cUsObHI6; 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="cUsObHI6" Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AS7TXka005298; Thu, 28 Nov 2024 12:55:00 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= h7sn6J2udbGejY+AXZz0wtHy+wTWQqAnvHhmeaZjXlc=; b=cUsObHI6fNpjXJG+ Ms+/lvwoPbUKbJtH6mHPkQXZn3wx5nnwMbMGHbQvaKcHppRLJ0gymuRVkqiOS3tH LMLZIGJsSzvHX78vZAUrWSadGVftLkn65W6wNbUnmJMi+HcdFAnp70K9UGXwsOka P4SZWhL/ZYxTrKhvkYhn9Yf44kZAsJzAONz5sI5A/dDwkYge0iazsv1sACk0O9bJ JfSUjRhN69CaFWa9SpwrOB59+2iAmoH+RHY11f6rRNHdYbF8y07ZwSgA8hiXGmXV Oe72iv3yCAu+NlMwQpNh2ll/+jtPgR124V/c8xO9hyIp+VNmF0dBzyz/pAxlujv4 8fit5w== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4366xvtrfq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 28 Nov 2024 12:55:00 +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 4ASCsxQr027959 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 28 Nov 2024 12:54:59 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, 28 Nov 2024 04:54:55 -0800 From: Zhongqiu Han To: , , , , , , , , , , , , CC: , , , Subject: [PATCH 2/3] perf header: Fix one memory leakage in process_bpf_prog_info() Date: Thu, 28 Nov 2024 20:54:31 +0800 Message-ID: <20241128125432.2748981-3-quic_zhonhan@quicinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241128125432.2748981-1-quic_zhonhan@quicinc.com> References: <20241128125432.2748981-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: M_NjtbMCXdP50DfORTtbND3RCwQ-MgZx X-Proofpoint-ORIG-GUID: M_NjtbMCXdP50DfORTtbND3RCwQ-MgZx 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 priorityscore=1501 malwarescore=0 mlxscore=0 lowpriorityscore=0 bulkscore=0 spamscore=0 phishscore=0 suspectscore=0 clxscore=1015 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2411280101 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: 9c51f8788b5d ("perf env: Avoid recursively taking env->bpf_progs.lock") Signed-off-by: Zhongqiu Han --- 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 Nov 28 12:54:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongqiu Han X-Patchwork-Id: 13888045 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 8E4F81A9B51; Thu, 28 Nov 2024 12:55:21 +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=1732798523; cv=none; b=PrVckse01oP269LVV8RrfprhhLMX40gxiZreZDY/kLSA4B5He3gNV8RHVoCrsCG4SIlw0smAlgcTNPXb78y0zgVedxWvc4OpjsZwmtdzTQ2NF+6k7JcDnk8DZB53kXfBfxJp3GXbw3XRJBztbNJoJ7+q29lk3v/op2RPsNtcLg0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732798523; c=relaxed/simple; bh=kmClH6K+ZKxbtSiVWiRLeiqkhHiTkiq5DgAxlIoY11I=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IDjGHmI53Oi/kWHzZvFyf78pil05bdAd1kopUlOaHBPK2PeBU30sS7QkWnFgnAjAjQV8kmi/ZEoAyLp3eQrYZur2Cgd+FNd5lKKXhx139s7EF7F1VvjV3HWrVgSXZyR5yVGuvWDf04yPSydr7l4aaWzeBS8K/Fl7TDMRzyXIKdQ= 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=L9aiU8iH; 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="L9aiU8iH" Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AS8bMQB022932; Thu, 28 Nov 2024 12:55:04 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= 2L0CerSr3kMsoq1YLCEchm17fKUPWuJk+2P/A5PgZiM=; b=L9aiU8iHm/t3jY7x F4n+OWWdNaUOt++Cz23qQcqZIh62roE2DFFtlkMKzbdmwl7Gh3E/BhEZyRJK/jT6 DdKOdiDcM7O/2Mfzb0uiJNvQEv4DXBth9vaBCjsVlJPIfScP/udC5cU5mHSk58EB bdNTumgSeRmyDpC/Ej5rRlMzlVtwRngketTvJMoF/51/+leiIsVhNteNhdflILXm FvisVkLQFzpyXpewYHYwZZ3AEb6L++uv3d9SfvKUhycL/BMHK0nmDIRvvnLo2zbq 7OX7nchCLSe3v7lhpQZGWLcbJoTER+uYxHRWatvMyyGRiVFrjy1VHOA0/5/XzqmQ GmwARQ== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4366xwjqpk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 28 Nov 2024 12:55:04 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA01.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 4ASCt3id028923 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 28 Nov 2024 12:55:03 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, 28 Nov 2024 04:54:59 -0800 From: Zhongqiu Han To: , , , , , , , , , , , , CC: , , , Subject: [PATCH 3/3] perf bpf: Fix two memory leakages when calling perf_env__insert_bpf_prog_info() Date: Thu, 28 Nov 2024 20:54:32 +0800 Message-ID: <20241128125432.2748981-4-quic_zhonhan@quicinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241128125432.2748981-1-quic_zhonhan@quicinc.com> References: <20241128125432.2748981-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: R1vVtqECV7v4n1oGNtvC-IJyQPzlTA0V X-Proofpoint-GUID: R1vVtqECV7v4n1oGNtvC-IJyQPzlTA0V 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 phishscore=0 adultscore=0 lowpriorityscore=0 spamscore=0 malwarescore=0 mlxlogscore=999 mlxscore=0 impostorscore=0 suspectscore=0 priorityscore=1501 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2411280101 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: 9c51f8788b5d ("perf env: Avoid recursively taking env->bpf_progs.lock") Signed-off-by: Zhongqiu Han --- tools/perf/util/bpf-event.c | 10 ++++++++-- tools/perf/util/env.c | 7 +++++-- tools/perf/util/env.h | 2 +- 3 files changed, 14 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..38401a289c24 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -24,12 +24,15 @@ 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 = true; down_write(&env->bpf_progs.lock); - __perf_env__insert_bpf_prog_info(env, info_node); + if (!__perf_env__insert_bpf_prog_info(env, info_node)) + ret = false; 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);