* [PATCH] misc: fastrpc: fix fastrpc_user leak on abnormal process exit
@ 2026-11-24 12:20 Renê de Souza Pinto
2026-11-24 12:36 ` sashiko-bot
2026-11-24 14:22 ` Murali Krishna
0 siblings, 2 replies; 4+ messages in thread
From: Renê de Souza Pinto @ 2026-11-24 12:20 UTC (permalink / raw)
To: srini, gregkh, arnd
Cc: ekansh.gupta, anandu.e, linux-arm-msm, dri-devel, stable,
Renê de Souza Pinto
Since commit e85eb5feca8e ("misc: fastrpc: fix use-after-free of
fastrpc_user in workqueue context"), each invoke context holds a
reference on its fastrpc_user, while contexts left on the pending list
(interrupted or timed out invocations) are only freed by
fastrpc_user_free(). When a process dies with invocations in flight,
this cycle leaks the user with all its maps and buffers. As the session
is given back at release, the next process can get the same SMMU
context bank with its IOVA space still in use and fail to map its
buffers.
No thread can be in an ioctl on the file at release time, so drop the
references of the pending contexts there. A context still waiting for
the DSP response is freed when it arrives.
Fixes: e85eb5feca8e ("misc: fastrpc: fix use-after-free of fastrpc_user in workqueue context")
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Renê de Souza Pinto <rene@renesp.com.br>
---
Tested on a Qualcomm IQ-8275 EVK (QCS8275) with a 6.18-based kernel:
a llama.cpp process using the CDSP was killed while invocations were in
flight. Without the fix its ~1.8 GB of buffers stayed attached to its
context bank, and the next process, given the same context bank, failed
to map its own ("failed to map buffer"), leaking more memory on every
retry. With the fix the buffers are released at close and the next
process works. On mainline this is only build-tested (arm64, W=1).
drivers/misc/fastrpc.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index cdc2627bdaf2..09a2963d72f9 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -1725,7 +1725,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
{
struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
struct fastrpc_channel_ctx *cctx = fl->cctx;
+ struct fastrpc_invoke_ctx *ctx, *n;
unsigned long flags;
+ LIST_HEAD(orphans);
fastrpc_release_current_dsp_process(fl);
@@ -1733,6 +1735,20 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
list_del(&fl->user);
spin_unlock_irqrestore(&cctx->lock, flags);
+ /*
+ * Pending (interrupted or timed out) contexts can't be resumed anymore
+ * and each holds a reference on the user: release them here to avoid
+ * a cycle.
+ */
+ spin_lock(&fl->lock);
+ list_splice_init(&fl->pending, &orphans);
+ spin_unlock(&fl->lock);
+
+ list_for_each_entry_safe(ctx, n, &orphans, node) {
+ list_del_init(&ctx->node);
+ fastrpc_context_put(ctx);
+ }
+
fastrpc_session_free(cctx, fl->sctx);
file->private_data = NULL;
/* Release the reference taken in fastrpc_device_open */
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] misc: fastrpc: fix fastrpc_user leak on abnormal process exit 2026-11-24 12:20 [PATCH] misc: fastrpc: fix fastrpc_user leak on abnormal process exit Renê de Souza Pinto @ 2026-11-24 12:36 ` sashiko-bot 2026-11-24 14:22 ` Murali Krishna 1 sibling, 0 replies; 4+ messages in thread From: sashiko-bot @ 2026-11-24 12:36 UTC (permalink / raw) To: Renê de Souza Pinto; +Cc: dri-devel Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] misc: fastrpc: premature fastrpc_session_free() before fastrpc_user_free() - [Low] misc: fastrpc: dead fl->pending cleanup loop left in fastrpc_user_free() It also noticed 1 related pre-existing issue this patch does not introduce: - [High] misc: fastrpc: memory leak of handle maps in fastrpc_context_free() [1] -- commit de1f1f214d7656b3f51116b41f6e566209ceb0a7 Author: Renê de Souza Pinto <rene@renesp.com.br> misc: fastrpc: fix fastrpc_user leak on abnormal process exit This commit drains fl->pending in fastrpc_device_release() and drops each pending invoke context's reference so that the reference cycle between fastrpc_invoke_ctx and fastrpc_user is broken when a file descriptor is closed. > diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c > index 9e760f68bcaf2..05271beaf821c 100644 > --- a/drivers/misc/fastrpc.c > +++ b/drivers/misc/fastrpc.c > @@ -1734,7 +1734,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file) > { > struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; > struct fastrpc_channel_ctx *cctx = fl->cctx; > + struct fastrpc_invoke_ctx *ctx, *n; > unsigned long flags; > + LIST_HEAD(orphans); > > fastrpc_release_current_dsp_process(fl); > > @@ -1742,6 +1744,20 @@ static int fastrpc_device_release(struct inode *inode, struct file *file) > list_del(&fl->user); > spin_unlock_irqrestore(&cctx->lock, flags); > > + /* > + * Pending (interrupted or timed out) contexts can't be resumed anymore > + * and each holds a reference on the user: release them here to avoid > + * a cycle. > + */ > + spin_lock(&fl->lock); > + list_splice_init(&fl->pending, &orphans); > + spin_unlock(&fl->lock); > + > + list_for_each_entry_safe(ctx, n, &orphans, node) { > + list_del_init(&ctx->node); > + fastrpc_context_put(ctx); > + } [Severity: Low] Now that fastrpc_device_release() drains fl->pending and every context on fl->pending holds a reference on fl until fastrpc_context_free(), fl->pending is always empty when fastrpc_user_free() runs. Should the duplicate fl->pending loop in fastrpc_user_free() and the forward declaration of fastrpc_context_put() be removed? [Severity: High] This problem wasn't introduced by this patch, but can fastrpc_context_free() leak handle maps [1] when an invocation fails early or is aborted here before fastrpc_put_args() runs? fastrpc_create_maps() populates ctx->maps up to ctx->nscalars, attaching handle maps at indices ctx->nbufs through ctx->nscalars - 1 via fastrpc_map_attach(), but fastrpc_context_free() only releases entries up to ctx->nbufs: drivers/misc/fastrpc.c:fastrpc_context_free() { ... for (i = 0; i < ctx->nbufs; i++) fastrpc_map_put(ctx->maps[i]); ... } When the invocation never reaches fastrpc_put_args(), those handle map references and their pinned DMA buffers are never released. > + > fastrpc_session_free(cctx, fl->sctx); > file->private_data = NULL; > /* Release the reference taken in fastrpc_device_open */ [Severity: Medium] Can calling fastrpc_session_free() here still hand the session and its SMMU context bank to a new process before the old process's buffers are unmapped? When an orphaned context on fl->pending is still waiting for a DSP response (or its ctx->put_work work item has not yet run), fastrpc_context_get() taken in fastrpc_invoke_send() keeps ctx->refcount above zero until fastrpc_context_put_wq() runs. Because ctx holds a reference on fl until fastrpc_context_free(), and commit e85eb5feca8e ("misc: fastrpc: fix use-after-free of fastrpc_user in workqueue context") moved the teardown of fl->init_mem, fl->maps, and fl->mmaps into fastrpc_user_free(), marking fl->sctx->used = false in fastrpc_device_release() allows fastrpc_session_alloc() to reuse the session while its IOVA space is still occupied by the exiting user's mappings. Should fastrpc_session_free() be called in fastrpc_user_free() after the user's maps and buffers have been freed instead? [1] https://sashiko.dev/#/bug/linux-9b1d78f7-f355-4424-acce-4fd9aa48eac5 -- Sashiko AI review · https://sashiko.dev/#/patchset/20261124122011.2912030-1-rene@renesp.com.br?part=1 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] misc: fastrpc: fix fastrpc_user leak on abnormal process exit 2026-11-24 12:20 [PATCH] misc: fastrpc: fix fastrpc_user leak on abnormal process exit Renê de Souza Pinto 2026-11-24 12:36 ` sashiko-bot @ 2026-11-24 14:22 ` Murali Krishna 2026-11-24 14:37 ` Renê 1 sibling, 1 reply; 4+ messages in thread From: Murali Krishna @ 2026-11-24 14:22 UTC (permalink / raw) To: rene Cc: srini, gregkh, arnd, ekansh.gupta, anandu.e, linux-arm-msm, dri-devel, Murali Krishna On Tue, 24 Nov 2026 13:20:11 +0100, Renê de Souza Pinto wrote: > Since commit e85eb5feca8e ("misc: fastrpc: fix use-after-free of > fastrpc_user in workqueue context"), each invoke context holds a > reference on its fastrpc_user, while contexts left on the pending list > (interrupted or timed out invocations) are only freed by > fastrpc_user_free(). When a process dies with invocations in flight, > this cycle leaks the user with all its maps and buffers. I am new to this driver, so please treat the below as observations rather than a review. I read the context lifetime in char-misc-next (917fcbf03) and it does appear to support your description: - fastrpc_context_alloc() takes fastrpc_channel_ctx_get(cctx) and fastrpc_user_get(user), links the context onto fl->pending, and kref_init()s the context. - fastrpc_invoke_send() takes a second context reference for the DSP, released by put_work from fastrpc_rpmsg_callback(), or immediately if rpmsg_send() fails. - fastrpc_internal_invoke() unlinks the context and drops the first reference in its bail path, but only when err is neither -ERESTARTSYS nor -ETIMEDOUT. - fastrpc_context_free() never touches fl->pending; it ends with fastrpc_user_put(fl) and fastrpc_channel_ctx_put(cctx). fl->pending is referenced in only four places in the file: the INIT_LIST_HEAD() in fastrpc_device_open(), the list_add_tail() in fastrpc_context_alloc(), the drain loop in fastrpc_user_free(), and fastrpc_notify_users(), which sets -EPIPE and completes waiters but neither unlinks nor drops a reference. So for an interrupted or timed out invoke the first reference is never dropped, and the only other place that drops it is the drain loop in fastrpc_user_free(), which cannot run while any context still holds a reference on fl. That matches the cycle you describe. Two things I was unsure about. First, this looks like it overlaps with Anandu Krishnan E's series, now at v4: https://lore.kernel.org/linux-arm-msm/20261117-interrupted-invoke-v4-v4-0-2320a8a7eda2@oss.qualcomm.com/ I see you have Anandu on Cc, so this may already be understood, but as the two fix the same leak in different places it would help to know which is meant to land. > No thread can be in an ioctl on the file at release time, so drop the > references of the pending contexts there. A context still waiting for > the DSP response is freed when it arrives. Second, about the symptom in your test report. If a context is still waiting for the DSP, the put added here only drops the first reference; the DSP reference keeps the context alive, so fastrpc_map_put() on its maps runs later, from fastrpc_context_free(). The maps are attached to the session device (dma_buf_attach(map->buf, sess->dev) in fastrpc_map_create()) and the attachment is only torn down in fastrpc_free_map(). fastrpc_session_free() runs immediately after your loop and only sets session->used = false, so the context bank becomes available to the next process while those attachments are, as far as I can tell, still in place. Does fastrpc_release_current_dsp_process() above guarantee the DSP has drained everything by this point, so that case cannot occur in practice? If not, the window you are aiming at would seem to remain for genuinely in-flight contexts. > + list_for_each_entry_safe(ctx, n, &orphans, node) { > + list_del_init(&ctx->node); > + fastrpc_context_put(ctx); > + } Very minor: after list_splice_init() the entries are already off fl->pending, and list_for_each_entry_safe() does not need them removed, so the list_del_init() looks redundant. For what it is worth, the patch applies cleanly to char-misc-next at 917fcbf03 and builds there on x86_64 with COMPILE_TEST=y under W=1 C=1, with no new warnings from either gcc or sparse against the unpatched file. I have no Qualcomm hardware, so none of it is runtime tested and I could not reproduce your leak; the rest is from reading the code. Thanks, Murali Krishna ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] misc: fastrpc: fix fastrpc_user leak on abnormal process exit 2026-11-24 14:22 ` Murali Krishna @ 2026-11-24 14:37 ` Renê 0 siblings, 0 replies; 4+ messages in thread From: Renê @ 2026-11-24 14:37 UTC (permalink / raw) To: Murali Krishna Cc: srini, gregkh, arnd, ekansh.gupta, anandu.e, linux-arm-msm, dri-devel Hi Murali, Thanks for going through it. On the overlap: I missed Anandu's series when sending this, sorry. v2 is already out, but v4 of Anandu's series fixes the same leak more completely: it also releases the DSP reference when the process teardown succeeds, which covers your second point. Srini, if you prefer that series, please drop mine; it was only meant to fix the leak. Anandu, I can reproduce the leak on a QCS8275 (IQ-8275 EVK) by killing a process with CDSP invocations in flight, and will test your v4 there. Thanks, Renê On 2026/11/24 15:22, Murali Krishna wrote: > On Tue, 24 Nov 2026 13:20:11 +0100, Renê de Souza Pinto wrote: >> Since commit e85eb5feca8e ("misc: fastrpc: fix use-after-free of >> fastrpc_user in workqueue context"), each invoke context holds a >> reference on its fastrpc_user, while contexts left on the pending list >> (interrupted or timed out invocations) are only freed by >> fastrpc_user_free(). When a process dies with invocations in flight, >> this cycle leaks the user with all its maps and buffers. > > I am new to this driver, so please treat the below as observations > rather than a review. I read the context lifetime in char-misc-next > (917fcbf03) and it does appear to support your description: > > - fastrpc_context_alloc() takes fastrpc_channel_ctx_get(cctx) and > fastrpc_user_get(user), links the context onto fl->pending, and > kref_init()s the context. > - fastrpc_invoke_send() takes a second context reference for the DSP, > released by put_work from fastrpc_rpmsg_callback(), or immediately > if rpmsg_send() fails. > - fastrpc_internal_invoke() unlinks the context and drops the first > reference in its bail path, but only when err is neither > -ERESTARTSYS nor -ETIMEDOUT. > - fastrpc_context_free() never touches fl->pending; it ends with > fastrpc_user_put(fl) and fastrpc_channel_ctx_put(cctx). > > fl->pending is referenced in only four places in the file: the > INIT_LIST_HEAD() in fastrpc_device_open(), the list_add_tail() in > fastrpc_context_alloc(), the drain loop in fastrpc_user_free(), and > fastrpc_notify_users(), which sets -EPIPE and completes waiters but > neither unlinks nor drops a reference. > > So for an interrupted or timed out invoke the first reference is never > dropped, and the only other place that drops it is the drain loop in > fastrpc_user_free(), which cannot run while any context still holds a > reference on fl. That matches the cycle you describe. > > Two things I was unsure about. > > First, this looks like it overlaps with Anandu Krishnan E's series, > now at v4: > > > https://lore.kernel.org/linux-arm-msm/20261117-interrupted-invoke-v4-v4-0-2320a8a7eda2@oss.qualcomm.com/ > > I see you have Anandu on Cc, so this may already be understood, but as > the two fix the same leak in different places it would help to know > which is meant to land. > >> No thread can be in an ioctl on the file at release time, so drop the >> references of the pending contexts there. A context still waiting for >> the DSP response is freed when it arrives. > > Second, about the symptom in your test report. If a context is still > waiting for the DSP, the put added here only drops the first reference; > the DSP reference keeps the context alive, so fastrpc_map_put() on its > maps runs later, from fastrpc_context_free(). The maps are attached to > the session device (dma_buf_attach(map->buf, sess->dev) in > fastrpc_map_create()) and the attachment is only torn down in > fastrpc_free_map(). fastrpc_session_free() runs immediately after your > loop and only sets session->used = false, so the context bank becomes > available to the next process while those attachments are, as far as I > can tell, still in place. > > Does fastrpc_release_current_dsp_process() above guarantee the DSP has > drained everything by this point, so that case cannot occur in > practice? If not, the window you are aiming at would seem to remain for > genuinely in-flight contexts. > >> + list_for_each_entry_safe(ctx, n, &orphans, node) { >> + list_del_init(&ctx->node); >> + fastrpc_context_put(ctx); >> + } > > Very minor: after list_splice_init() the entries are already off > fl->pending, and list_for_each_entry_safe() does not need them removed, > so the list_del_init() looks redundant. > > For what it is worth, the patch applies cleanly to char-misc-next at > 917fcbf03 and builds there on x86_64 with COMPILE_TEST=y under W=1 C=1, > with no new warnings from either gcc or sparse against the unpatched > file. I have no Qualcomm hardware, so none of it is runtime tested and > I could not reproduce your leak; the rest is from reading the code. > > Thanks, > Murali Krishna ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-24 14:37 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-24 12:20 [PATCH] misc: fastrpc: fix fastrpc_user leak on abnormal process exit Renê de Souza Pinto 2026-11-24 12:36 ` sashiko-bot 2026-11-24 14:22 ` Murali Krishna 2026-11-24 14:37 ` Renê
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.