From mboxrd@z Thu Jan 1 00:00:00 1970 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 3DBC13CAE66 for ; Tue, 2 Jun 2026 09:41:08 +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=1780393271; cv=none; b=HS/z7RejJLNVfVe7Xso1OILj/iYf6TN/5TWZWzlmo40c2FiOCGMu+nD4CTqgAdGf54ZugM0ihpebX+zlHWLyQVeCww1tC6B3Gj5EcHfDkd6rnaFD2/Bwcnp5g2pQCgdZ0ewUReVt53JlZtQfkiOvy/H5ChCMA7A/F8xL1WtquFU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780393271; c=relaxed/simple; bh=X4PRWbtQNeo0jFMupvsDRUKkIKU7lrhgyz070jxV2tg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Y0ARVi3fTlrjZ1bbHLKvTMpRBboS3WvXsqdQnchXtYtp9GkhbrL3matPYkbHIQ+nfX8sJEnAEKRsAsvreAISowFoTr3UT1pgDxA65XUjq3yhlWHAEbfng4Y4W3M8EEJhXO02PeYqpZLgmFne7yCvEoghd976DncgYTnIUw8Rvu4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=aZbzSY+J; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=dfls4XXz; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="aZbzSY+J"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="dfls4XXz" Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 65245Gdn1938135 for ; Tue, 2 Jun 2026 09:41:08 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= oESplOGIONykfrvVga1RyEzgCLfort4w6L2n3L3CGBs=; b=aZbzSY+Je2M1WrLi kvRRVcmtzeV4Xv7janPqqotTd6cYY9pVwCmDrTk9GvNHE1TgUVepZyEomVu/++Br dMVcjqrV6dlAwxF0Fext7rWKdSESy98j8sjXnVEGOwZZ6h4J1PscJ7ZRZlBsmeNM lNBf0Z6KO3ACyYWuZf9cGexmpJoq8wyWC2tNC9tqAbRPb85JDGSD1kZON35/NQdL K1ingTcOtVEtjz/Fx7tKqNEqE1zC/bj7ceH5TWGZ8Ht3xphVmlAUlcvFSelc0ldY pFUEFd4lnBcYJyOmD60Qtiu5oG8zgCwKGeG8q2j9h3S2A8aD7JMCt54iA94WHnQb sMfozw== Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4ehqubh9hp-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Tue, 02 Jun 2026 09:41:08 +0000 (GMT) Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2bf004bf8beso66775015ad.3 for ; Tue, 02 Jun 2026 02:41:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1780393267; x=1780998067; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=oESplOGIONykfrvVga1RyEzgCLfort4w6L2n3L3CGBs=; b=dfls4XXzcqgvsj52+HhuPVrKS6Fa2VN49WUvBL4w9+9iXOYX0km5g35Fco5ymMhR/y rjQa6RG7GqYHV33jr151SKINHW2KPrXGxN4jCZwPlXu+lXo7ZKS9mB11psYrCE4K8puX XsD2xTw7Pn3SwnmtAUlFTkA8Y5kx/p5J8Ltgpo97o6MHHUGJRcPd/m23FYdV630PJ8hD /oUlfw1kd9XxsCqim3SM/vqXsTae74oT+S5ln9XQuUwTzCyxHDFYnc6f7v1exsD4Rkvq kP888+ku/wpYvc7h6VwxCRgUhSnVdFiJBufTsJNolxmcWWK5zwzoeYurLUHVii9x/Q0y KAjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780393267; x=1780998067; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oESplOGIONykfrvVga1RyEzgCLfort4w6L2n3L3CGBs=; b=jODqUYOnIJZsXK6fSFqwqg1r3kpPwkwXyX065rD3GH3E0JmHhQCEF4JfktfZUAyKtS EDet8N3tcqkjqJa3edxpLMpCTZujJycO2n3B0JzRO0Ux47q86iTT+Ih/A7ylGN+AC+Ao j7QpXDdlMX+atjIv2SypHYXHSIvxGsirgk0VDARmtZ2IL9lqz58EjnssH4JbWFKgm8U8 5GewPkDdyCHwrcrvo7wMx0mYM3IPqD7HWjQoaJlcfx5GRi9qXWzM8wd6/YeHR4H4R8Su 6CVi1frZ4+dB8MY8RWs+1wkdMrTN7hqsHHFYEBzOd/zilbp1VGvVBCmWyul9CQF3jGVI jzsQ== X-Forwarded-Encrypted: i=1; AFNElJ/60C0oqSyPqflKePh8ayJXGEe0FMck0pD1ZmrDQuerW0cSuMBhtNDFiDm/GXoRQA8FHqpDKQjROuE1TZQ=@vger.kernel.org X-Gm-Message-State: AOJu0YycufAEElOBZSXPVDEzEVX/+/JlGYS8EQSjYMhactC9inHtC6e2 UXv1Niv+frFQNHHh9s593vPzRhQ6Ns9o+VjTWhn1mrsrYcTA+jo4sjatmX7dZOQlhPujUjDgnYP na6+f1kFl3egudSYdjAuagY4OwZE1t5KdiFAz/2qw9Go5fh6Z23lfwSue2VYijxVY0YUUUpkW9u uPNw== X-Gm-Gg: Acq92OGja4H5IfWrd11KG7bjEauwSoNy/PY8jSqmFT3am1d2CuI4oIsmmrCxxc/J8rf cqYkYbRtfjSuwhwdBJbWvuvIvtkck4aUm4lXTvUfYrfnXr6VjKLEeXcLrVSWP00m60d+FYOgd8H 7PRsFPzI4IatIwLVuiL5eW8muHYBRTYwdRq7qXsg8/VPyKQGtUMWgecFf70ttfxDcef/Lks9Flq IheAP2qzYQl9BB0rnU4Idvthj/9qT1YtwpkdG0xRHEbqWCGG69M4vQkUVx+KkUGcFvEtY5479Sn /Qyl3OS50JByJY7fr/8Mg07xhti12ce4AumY6nlCqpVWzMVLiWvLKtUZ44z6ho47ZLo6huiIGyk gm2nY1MBS3kWzFM1Lk2pxtuREDbGRbVra4XjIuQWCmSDw3xdvwpuDX2DoWz/ZJkagrRXBKEMjlx zJdJmoEbB6uKi83TAplF2puQ== X-Received: by 2002:a17:903:2389:b0:2b4:656b:aeb0 with SMTP id d9443c01a7336-2bf3685af15mr182356375ad.35.1780393266960; Tue, 02 Jun 2026 02:41:06 -0700 (PDT) X-Received: by 2002:a17:903:2389:b0:2b4:656b:aeb0 with SMTP id d9443c01a7336-2bf3685af15mr182355955ad.35.1780393266399; Tue, 02 Jun 2026 02:41:06 -0700 (PDT) Received: from [10.133.33.123] (tpe-colo-wan-fw-bordernet.qualcomm.com. [103.229.16.4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2bf239fd8cdsm124904775ad.22.2026.06.02.02.41.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 02 Jun 2026 02:41:05 -0700 (PDT) Message-ID: Date: Tue, 2 Jun 2026 17:41:00 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v7 4/5] misc: fastrpc: Allocate entire reserved memory for Audio PD in probe To: Jianping Li , srini@kernel.org, amahesh@qti.qualcomm.com, arnd@arndb.de, gregkh@linuxfoundation.org, abelvesa@kernel.org, jorge.ramirez@oss.qualcomm.com Cc: linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, ekansh.gupta@oss.qualcomm.com, quic_chennak@quicinc.com, stable@kernel.org References: <20260602071750.526-1-jianping.li@oss.qualcomm.com> <20260602071750.526-5-jianping.li@oss.qualcomm.com> Content-Language: en-US From: Jie Gan In-Reply-To: <20260602071750.526-5-jianping.li@oss.qualcomm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-ORIG-GUID: hnnQRyWyqMh1RhMjj9VRjIKMHQ8IDH1K X-Proofpoint-GUID: hnnQRyWyqMh1RhMjj9VRjIKMHQ8IDH1K X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNjAyMDA5MCBTYWx0ZWRfXzFzgBTrwoD++ LQbx6SUOT2nmC+ipQxv8EY8DADJeB+7F/zG7ie1ctsrCt4hXsQe5g50ZUgtL0bxcG5pcpkfXscO ZzMDSKYw9xNizYPhaLpSs4RQv9ffub+Mk8N+xuEwcoUjBXoeoe55g2JRXsoLljxHclutnN8Zwqv bRpOoI+2XSK2Qjc9b2OEvHl9NcErJPXIl0GeKP7RMyWXTWjP13pY+p7kQflqc69+4PZe9tUa2El 453P/1fZqpq37aLsBPj2zZcdhTx0gF0Q6rtYZ9yH8gG8856sZI3im6HM+Zr/2PANw5wA9zS5r22 Y8iQdx/OEDw8U7d61yLvcW5ebGvfAb8oBkEseOJMAuH3gLRapL67RjtMTVmJhH71Ni8z7ZapdZT +KZFqnjuWMVSy79xiRApHMaeDDnPpwijKxDnQ/0p9fisSRflS3CD7Y9cgWSi0sFwLe+/WTRk/rF 2+8zq1lETQzab0Vw6Wg== X-Authority-Analysis: v=2.4 cv=FcoHAp+6 c=1 sm=1 tr=0 ts=6a1ea534 cx=c_pps a=cmESyDAEBpBGqyK7t0alAg==:117 a=nuhDOHQX5FNHPW3J6Bj6AA==:17 a=IkcTkHD0fZMA:10 a=FelO9ux0wxsA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=3WHJM1ZQz_JShphwDgj5:22 a=VwQbUJbxAAAA:8 a=EUspDBNiAAAA:8 a=Hk-PcD55WIolp562rqUA:9 a=QEXdDO2ut3YA:10 a=1OuFwYUASf3TG4hYMiVC:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.125,FMLib:17.12.100.49 definitions=2026-06-02_01,2026-05-28_03,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 phishscore=0 bulkscore=0 impostorscore=0 malwarescore=0 suspectscore=0 lowpriorityscore=0 clxscore=1015 spamscore=0 adultscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2605210000 definitions=main-2606020090 On 6/2/2026 3:17 PM, Jianping Li wrote: > Allocating and freeing Audio PD memory from userspace is unsafe because > the kernel cannot reliably determine when the DSP has finished using the > memory. Userspace may free buffers while they are still in use by the DSP, > and remote free requests cannot be safely trusted. > > Additionally, the current implementation allows userspace to repeatedly > grow the Audio PD heap, but does not support shrinking it. This can lead > to unbounded memory usage over time, effectively causing a memory leak. > > Fix this by allocating the entire Audio PD reserved-memory region during > rpmsg probe and tying its lifetime to the rpmsg channel. This removes > userspace-controlled alloc/free and ensures that memory is reclaimed only > when the DSP process is torn down. > > Fixes: 0871561055e66 ("misc: fastrpc: Add support for audiopd") > Cc: stable@kernel.org > Signed-off-by: Jianping Li > --- > drivers/misc/fastrpc.c | 96 +++++++++++++++++++----------------------- > 1 file changed, 43 insertions(+), 53 deletions(-) > > diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c > index f46a8f53970d..33be8bed6a0b 100644 > --- a/drivers/misc/fastrpc.c > +++ b/drivers/misc/fastrpc.c > @@ -276,6 +276,8 @@ struct fastrpc_channel_ctx { > struct kref refcount; > /* Flag if dsp attributes are cached */ > bool valid_attributes; > + /* Flag if audio PD init mem was allocated */ > + bool audio_init_mem; > u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES]; > struct fastrpc_device *secure_fdevice; > struct fastrpc_device *fdevice; > @@ -1344,15 +1346,16 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, > struct fastrpc_init_create_static init; > struct fastrpc_invoke_args *args; > struct fastrpc_phy_page pages[1]; > + struct fastrpc_channel_ctx *cctx = fl->cctx; > char *name; > int err; > - bool scm_done = false; > struct { > int client_id; > u32 namelen; > u32 pageslen; > } inbuf; > u32 sc; > + unsigned long flags; > > if (!fl->cctx->remote_heap || > !fl->cctx->remote_heap->dma_addr || > @@ -1383,31 +1386,6 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, > inbuf.client_id = fl->client_id; > inbuf.namelen = init.namelen; > inbuf.pageslen = 0; > - if (!fl->cctx->remote_heap) { > - err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen, > - &fl->cctx->remote_heap); > - if (err) > - goto err_name; > - > - /* Map if we have any heap VMIDs associated with this ADSP Static Process. */ > - if (fl->cctx->vmcount) { > - u64 src_perms = BIT(QCOM_SCM_VMID_HLOS); > - > - err = qcom_scm_assign_mem(fl->cctx->remote_heap->dma_addr, > - (u64)fl->cctx->remote_heap->size, > - &src_perms, > - fl->cctx->vmperms, fl->cctx->vmcount); > - if (err) { > - dev_err(fl->sctx->dev, > - "Failed to assign memory with dma_addr %pad size 0x%llx err %d\n", > - &fl->cctx->remote_heap->dma_addr, > - fl->cctx->remote_heap->size, err); > - goto err_map; > - } > - scm_done = true; > - inbuf.pageslen = 1; > - } > - } > > fl->pd = USER_PD; > > @@ -1419,8 +1397,17 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, > args[1].length = inbuf.namelen; > args[1].fd = -1; > > - pages[0].addr = fl->cctx->remote_heap->dma_addr; > - pages[0].size = fl->cctx->remote_heap->size; > + spin_lock_irqsave(&cctx->lock, flags); > + if (!fl->cctx->audio_init_mem) { > + pages[0].addr = fl->cctx->remote_heap->dma_addr; > + pages[0].size = fl->cctx->remote_heap->size; > + fl->cctx->audio_init_mem = true; > + inbuf.pageslen = 1; > + } else { > + pages[0].addr = 0; > + pages[0].size = 0; > + } > + spin_unlock_irqrestore(&cctx->lock, flags); > > args[2].ptr = (u64)(uintptr_t) pages; > args[2].length = sizeof(*pages); > @@ -1438,27 +1425,7 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl, > > return 0; > err_invoke: > - if (fl->cctx->vmcount && scm_done) { > - u64 src_perms = 0; > - struct qcom_scm_vmperm dst_perms; > - u32 i; > - > - for (i = 0; i < fl->cctx->vmcount; i++) > - src_perms |= BIT(fl->cctx->vmperms[i].vmid); > - > - dst_perms.vmid = QCOM_SCM_VMID_HLOS; > - dst_perms.perm = QCOM_SCM_PERM_RWX; > - err = qcom_scm_assign_mem(fl->cctx->remote_heap->dma_addr, > - (u64)fl->cctx->remote_heap->size, > - &src_perms, &dst_perms, 1); > - if (err) > - dev_err(fl->sctx->dev, "Failed to assign memory dma_addr %pad size 0x%llx err %d\n", > - &fl->cctx->remote_heap->dma_addr, fl->cctx->remote_heap->size, err); > - } > -err_map: > - fastrpc_buf_free(fl->cctx->remote_heap); > - fl->cctx->remote_heap = NULL; > -err_name: > + fl->cctx->audio_init_mem = false; > kfree(name); > err: > kfree(args); > @@ -2425,12 +2392,21 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) > } > } > > - if (domain_id == SDSP_DOMAIN_ID) { > + if (domain_id == SDSP_DOMAIN_ID || domain_id == ADSP_DOMAIN_ID) { > struct resource res; > u64 src_perms; > > err = of_reserved_mem_region_to_resource(rdev->of_node, 0, &res); > if (!err) { > + if (domain_id == ADSP_DOMAIN_ID) { > + data->remote_heap = > + kzalloc_obj(*data->remote_heap, GFP_KERNEL); > + if (!data->remote_heap) > + return -ENOMEM; allocated data never free with directly return. goto err_free_data; Beside, we also need free data->remote_heap in err_free_data path as you added new memory allocation. > + > + data->remote_heap->dma_addr = res.start; > + data->remote_heap->size = resource_size(&res); > + } > src_perms = BIT(QCOM_SCM_VMID_HLOS); > > err = qcom_scm_assign_mem(res.start, resource_size(&res), &src_perms, > @@ -2438,7 +2414,6 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) > if (err) > goto err_free_data; > } > - > } > > secure_dsp = !(of_property_read_bool(rdev->of_node, "qcom,non-secure-domain")); > @@ -2519,6 +2494,7 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) > struct fastrpc_buf *buf, *b; > struct fastrpc_user *user; > unsigned long flags; > + int err; > > /* No invocations past this point */ > spin_lock_irqsave(&cctx->lock, flags); > @@ -2536,8 +2512,22 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) > list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node) > list_del(&buf->node); > > - if (cctx->remote_heap) > - fastrpc_buf_free(cctx->remote_heap); after removed the code, the cctx->remote_heap is not freed: 1. cctx->vmcount == 0 2. if (!err) is false we should free the cctx->remote_heap unconditionally if it exists. Thanks, Jie > + if (cctx->remote_heap && cctx->vmcount) { > + u64 src_perms = 0; > + struct qcom_scm_vmperm dst_perms; > + > + for (u32 i = 0; i < cctx->vmcount; i++) > + src_perms |= BIT(cctx->vmperms[i].vmid); > + > + dst_perms.vmid = QCOM_SCM_VMID_HLOS; > + dst_perms.perm = QCOM_SCM_PERM_RWX; > + > + err = qcom_scm_assign_mem(cctx->remote_heap->dma_addr, > + cctx->remote_heap->size, &src_perms, > + &dst_perms, 1); > + if (!err) > + kfree(cctx->remote_heap); > + } > > of_platform_depopulate(&rpdev->dev); >