From mboxrd@z Thu Jan 1 00:00:00 1970 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 66BC935AC36 for ; Wed, 22 Apr 2026 10:32:48 +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=1776853970; cv=none; b=bJesoPPlrWW3u5FkvTTYEOv7r5qMEuY8FNRb5Bb4Oikhe8AExrGqBPtYEJPa2tDExbWPI4523Fuzso83mluSf976d+UR56CowC6EktgtNZadGYEDSQmG5ccDpRsYHzpqhIUaAlAesKP0gwei8OqZdbEcmfxA7NihRxBKPh++9BA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776853970; c=relaxed/simple; bh=dObQM6R/32cW3UHjNKhGo/HPqxPZrcDSPZUqCu5zNdo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=gLQcI+zB0EacJL+VhzhTKdXReDki5ouJho1PPp7VYz+bOYuUER5aH1Q0XtHevxveuryIPyENNjxnzr+KtAVNQoo8ilkZrRcaYTzxbVUR+1i+6lpvGNFNmGUqhoavkFLXC4jqihvYOOKbJc+NADjTyqMv5d6Uaa8K/MdJOm2Kcjw= 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=UGJ9ffXv; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=Lvl71SyG; arc=none smtp.client-ip=205.220.168.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="UGJ9ffXv"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="Lvl71SyG" Received: from pps.filterd (m0279864.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 63M96Ei6664102 for ; Wed, 22 Apr 2026 10:32:47 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= b1+18xNRgn9+Ez7xGrXINoqw7Xt1WZlAbcZjb5rCtck=; b=UGJ9ffXvVT/DFqeV 4aDNynOAHN6FYTD++h3e5vgtbJL1wHnSX9U6rYlTeH4Pn234jUWGj7eefDCNfXQD rFTLUa5JFnXduvm+B4Jcr0SjcT3NwWIQurbj8zsWVNRIlvWTbvqbswwgQ9wSBm3T njScmfMte5VDiNrP7IsdFpOM4NZI/kX8qY1a79djZXBR1Vq2jXMu9nbW0dLxoTqr 9Xjm7VqgloIIIruKa3vZR7u/mR+O/X1OmhU0mOmYyfGKEwyJdZH22E18Jn3uRDEU REUzg9VsPM8kW+Vqa9lvRhh+2FK7yYe69jPvrVUfF9r2LARvr84r5K06gJR1RDcX TLttYA== 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 4dpudgrba9-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Wed, 22 Apr 2026 10:32:47 +0000 (GMT) Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2b458add85aso53654135ad.2 for ; Wed, 22 Apr 2026 03:32:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1776853967; x=1777458767; 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=b1+18xNRgn9+Ez7xGrXINoqw7Xt1WZlAbcZjb5rCtck=; b=Lvl71SyGUB23pJlOv9L4khufVlWJYovkIDPi04N7IugeESp/V9BQmSBcl9/Tp2hLka Ppa7ejbfFez7Ya6B28TclH8TjSHdcM3UAlosCt6rF9bhsumef2YcyPgWESbIJ0Z3czg8 3iNL3X7uRmhrW7Lxsx+8FBjfLrE9TV4Iz4tYqBqdSD0PQaIdYoqi3obEcq8dUHrzzbKY vvS1V8H0qlJ041ZHF0o1f/80Ya04IyTlcyBQ1ZoL8vIY6BfnGLLUWfjZW/8iI3+fuwM4 E2/ZDQH9Gj+bL34KrlciT8FnZzOqCsU9jQz3VC571sRPnAF7/9WnBI/StxoTfrgUJ4Y4 sl1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776853967; x=1777458767; 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=b1+18xNRgn9+Ez7xGrXINoqw7Xt1WZlAbcZjb5rCtck=; b=D8IloWTRWJvYz5wZ1cd8Bd3op7V5jjpoqx+HxM3E8kCtI5WlVHonmp/Nm1IrqM0HP4 cyH59ABlVCXbclxZm319LnIw59O+A9IJSxEkVwB17sAEqTCRcNZs5M52Uice5mgSjZJ7 9rVw+IdKixr0/qbFwSZ4umU/JNQ8ie3YrTOQptAJf9PVxeCt7JshifePHqDIHo1GIDPc 7xYJUoqCum10kXjgirWl6m1llkZholEnnMw7PBMJ0f9SM2JxkdXScmvs8fclTCP4xfDr 2mU10MfKcuWmWzQwLsholIEeEJex6LrEdJn4KJ5EL6eT/KFjAFs8MAOvtRJLqHuZ51W1 0VQQ== X-Forwarded-Encrypted: i=1; AFNElJ/P2zF/oU3rT38uNXXtyYQXSP7kHKrYRQNK5tlxvhiXZsi4CrjV29XMTmxjnRRT9amJP3Ks5Pi4m8U3SI8=@vger.kernel.org X-Gm-Message-State: AOJu0YyhTGp1rCpbHHI2/egK7i5Eqv9iM0a9aa1qesSa1Q9c8fP+W/o1 HXh5uBxTtJ30GSMlQdd+1dMfx5k10mtxBylrCKh6KIrsE+fIa2MaxOO1RqiahfBft0zjuz15B79 5FDnSNz+OEKzffdxCZveoGbocxwCdlwNEhgTB5Ye5RynqwJq5HKH4kWuc/jNAc4I7EfU= X-Gm-Gg: AeBDievi/V0xXs0npoFybtaS2G30xiCQOVXqaDAkqHAFFM6fFxOQGyyA/107YbJ3miw nM00T8WajCITlSlhwb76u2uFfGDk53vjpUea69/CjAmm84mtXoNKCb0oUds2RAxc3wdETKrs/q1 2xxpegDsAXySyf4ErdJdDk7/aK7cfpy11E68xM1sIZcY1DI/q2yUU+GacriSSg4tznFMDNl5ZM2 ZVrOt57h9D70CGLGPHJVPgaRc4MC6OPxsNes7F74KUhdmZJXsXfUC0eRpBuT5+hgrUpuMcZV82R aqsDVmgKYKj/3yGE8OP54AVYXVawHM0ORsY8Eg/JLEuFJF77vVIfRrnYiVLJxUrc4IqYpe4ENvH iJZSQ99mwzqCR9cPLXnMHbQRAIVLKCtfzeWc3qssz2EHqC8JFHGAMgEjr X-Received: by 2002:a17:902:f550:b0:2b2:4611:5dca with SMTP id d9443c01a7336-2b5f9f1c1edmr226337455ad.24.1776853966975; Wed, 22 Apr 2026 03:32:46 -0700 (PDT) X-Received: by 2002:a17:902:f550:b0:2b2:4611:5dca with SMTP id d9443c01a7336-2b5f9f1c1edmr226337085ad.24.1776853966401; Wed, 22 Apr 2026 03:32:46 -0700 (PDT) Received: from [10.206.98.126] ([202.46.23.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5faa16e72sm161353545ad.19.2026.04.22.03.32.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Apr 2026 03:32:46 -0700 (PDT) Message-ID: <6e60a636-2693-4456-a63d-dd4165ec38ee@oss.qualcomm.com> Date: Wed, 22 Apr 2026 16:02:40 +0530 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 v1] misc: fastrpc: Add reference counting for fastrpc_user structure To: Srinivas Kandagatla , linux-arm-msm@vger.kernel.org Cc: gregkh@linuxfoundation.org, quic_bkumar@quicinc.com, linux-kernel@vger.kernel.org, quic_chennak@quicinc.com, dri-devel@lists.freedesktop.org, arnd@arndb.de, ekansh.gupta@oss.qualcomm.com, stable@kernel.org References: <20260226151121.818852-1-anandu.e@oss.qualcomm.com> Content-Language: en-US From: Anandu Krishnan E In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-ORIG-GUID: IoXVINDxKklD_L5qzh0G_N4MRuJqMavM X-Authority-Analysis: v=2.4 cv=c5ibhx9l c=1 sm=1 tr=0 ts=69e8a3cf cx=c_pps a=cmESyDAEBpBGqyK7t0alAg==:117 a=ZePRamnt/+rB5gQjfz0u9A==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=DJpcGTmdVt4CTyJn9g5Z:22 a=VwQbUJbxAAAA:8 a=EUspDBNiAAAA:8 a=e1tWFlzL7xBfjK-YGPgA:9 a=QEXdDO2ut3YA:10 a=1OuFwYUASf3TG4hYMiVC:22 X-Proofpoint-GUID: IoXVINDxKklD_L5qzh0G_N4MRuJqMavM X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDIyMDEwMCBTYWx0ZWRfXwZu23d97HMA3 UTMvQk1BYxlVQwOerSIA0tebj4mDbA7AkSPB0vvdEr79XMuWXi5b7ETSfL54eeiqbD1R+MRx0re FacX5tlLde3QrZzsD5K7qIYZfcYKLpmMZui0k3wZi7Tj8moE3xAHj7pe+eRaAnJYw5F2ufjGouJ VN1t9u/DfWHTATSlg6GdehF9XCQShG5pNa/q8s2lyZwAhok8GifUtD04Ol5Hc6ojaKZjtuo3QtP 46d2GCTN+PfuOcOcMek0370EUxZmm/bkbVH1p/H3YZPYe4JdA1KsbLJmDUftxZdQpTiN2ldERxv JWbdldN40Kyn6ZaqBrl6UlXJwAs5ymUE+n4+pg2/nIQhZx9ZqsaWe86FIFKScIusQ/cD7mBQwCi FZ1p8X5D+ABHD/A23mpFd4dOVv6W/FYwyq4s/o9fOg5qOl7+vLLVwuSae8952ZoofAT4O9Nal43 Is85BEzEznaBSQXEg3A== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-22_01,2026-04-21_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 malwarescore=0 spamscore=0 lowpriorityscore=0 suspectscore=0 adultscore=0 impostorscore=0 phishscore=0 clxscore=1015 priorityscore=1501 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2604200000 definitions=main-2604220100 On 13-04-2026 06:39 pm, Srinivas Kandagatla wrote: > > On 2/26/26 3:11 PM, Anandu Krishnan E wrote: >> Add reference counting using kref to the fastrpc_user structure to >> prevent use-after-free issues when contexts are freed from workqueue >> after device release. >> >> The issue occurs when fastrpc_device_release() frees the user structure >> while invoke contexts are still pending in the workqueue. When the >> workqueue later calls fastrpc_context_free(), it attempts to access >> buf->fl->cctx in fastrpc_buf_free(), leading to a use-after-free: > I guess these are some of the pending invoke requests that are still > processing and receive callback after user closes the device. > >> pc : fastrpc_buf_free+0x38/0x80 [fastrpc] >> lr : fastrpc_context_free+0xa8/0x1b0 [fastrpc] >> ... >> fastrpc_context_free+0xa8/0x1b0 [fastrpc] >> fastrpc_context_put_wq+0x78/0xa0 [fastrpc] >> process_one_work+0x180/0x450 >> worker_thread+0x26c/0x388 >> >> Implement proper reference counting to fix this: >> - Initialize kref in fastrpc_device_open() >> - Take a reference in fastrpc_context_alloc() for each context >> - Release the reference in fastrpc_context_free() when context is freed >> - Release the initial reference in fastrpc_device_release() >> >> This ensures the user structure remains valid as long as there are >> contexts holding references to it, preventing the race condition. >> >> Fixes: 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter") >> Cc: stable@kernel.org >> Signed-off-by: Anandu Krishnan E >> --- >> drivers/misc/fastrpc.c | 35 +++++++++++++++++++++++++++++++---- >> 1 file changed, 31 insertions(+), 4 deletions(-) >> >> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c >> index 47356a5d5804..3ababcf327d7 100644 >> --- a/drivers/misc/fastrpc.c >> +++ b/drivers/misc/fastrpc.c >> @@ -310,6 +310,8 @@ struct fastrpc_user { >> spinlock_t lock; >> /* lock for allocations */ >> struct mutex mutex; >> + /* Reference count */ >> + struct kref refcount; >> }; >> >> /* Extract SMMU PA from consolidated IOVA */ >> @@ -497,15 +499,36 @@ static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx) >> kref_put(&cctx->refcount, fastrpc_channel_ctx_free); >> } >> >> +static void fastrpc_user_free(struct kref *ref) >> +{ >> + struct fastrpc_user *fl = container_of(ref, struct fastrpc_user, refcount); >> + >> + fastrpc_channel_ctx_put(fl->cctx); >> + mutex_destroy(&fl->mutex); >> + kfree(fl); >> +} >> + >> +static void fastrpc_user_get(struct fastrpc_user *fl) >> +{ >> + kref_get(&fl->refcount); >> +} >> + >> +static void fastrpc_user_put(struct fastrpc_user *fl) >> +{ >> + kref_put(&fl->refcount, fastrpc_user_free); >> +} >> + >> static void fastrpc_context_free(struct kref *ref) >> { >> struct fastrpc_invoke_ctx *ctx; >> struct fastrpc_channel_ctx *cctx; >> + struct fastrpc_user *fl; >> unsigned long flags; >> int i; >> >> ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount); >> cctx = ctx->cctx; >> + fl = ctx->fl; >> >> for (i = 0; i < ctx->nbufs; i++) >> fastrpc_map_put(ctx->maps[i]); >> @@ -521,6 +544,8 @@ static void fastrpc_context_free(struct kref *ref) >> kfree(ctx->olaps); >> kfree(ctx); >> >> + /* Release the reference taken in fastrpc_context_alloc() */ >> + fastrpc_user_put(fl); >> fastrpc_channel_ctx_put(cctx); >> } >> >> @@ -628,6 +653,8 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc( >> >> /* Released in fastrpc_context_put() */ >> fastrpc_channel_ctx_get(cctx); >> + /* Take a reference to user, released in fastrpc_context_free() */ >> + fastrpc_user_get(user); >> >> ctx->sc = sc; >> ctx->retval = -1; >> @@ -658,6 +685,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc( >> spin_lock(&user->lock); >> list_del(&ctx->node); >> spin_unlock(&user->lock); >> + fastrpc_user_put(user); >> fastrpc_channel_ctx_put(cctx); >> kfree(ctx->maps); >> kfree(ctx->olaps); >> @@ -1606,11 +1634,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file) >> } >> >> fastrpc_session_free(cctx, fl->sctx); >> - fastrpc_channel_ctx_put(cctx); >> - >> - mutex_destroy(&fl->mutex); >> - kfree(fl); >> file->private_data = NULL; >> + /* Release the reference taken in fastrpc_device_open */ >> + fastrpc_user_put(fl); > Now that we are trying to handle(wait) for the cases where wq will get > callbacks after the fd is closed, how are we making sure that memory > associated with these invoke, init memory and mmaps are not released? I will move the cleanups to fastrpc_user_free so that cleanup wont happen until all user references are released > > > Also now that we have a refcount for the fastrpc_user, is it possible to > remove the refcount on the context, as this seems redundant now, we > could just use the user refcount instead. May be other patch.. I will check on this and if possible to remove, i will share a separate patch -Anandu > > > --srini >> >> return 0; >> } >> @@ -1654,6 +1680,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp) >> spin_lock_irqsave(&cctx->lock, flags); >> list_add_tail(&fl->user, &cctx->users); >> spin_unlock_irqrestore(&cctx->lock, flags); >> + kref_init(&fl->refcount); >> >> return 0; >> }