misc: fastrpc: fix fastrpc_user leak on abnormal process exit
PATCH 1/1 misc: fastrpc: fix fastrpc_user leak on abnormal process exit
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.
---
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(+)
drivers/misc/fastrpc.c +16 -0
@@ -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 */
Signature · 1 line
2.53.0
[email protected]
AI reviewThank 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…
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 <[email protected]>
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.
Quoted 21 lines
drivers/misc/fastrpc.c +5
-0@@ -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
drivers/misc/fastrpc.c static int fastrpc_device_release(struct inode *inode, struct file *file) · line 1759
Quoted 10 lines
drivers/misc/fastrpc.c +10
-0+* 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.
drivers/misc/fastrpc.c static int fastrpc_device_release(struct inode *inode, struct file *file) · line 1754
Quoted 4 lines
drivers/misc/fastrpc.c +1
-0+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
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 6 lines · Renê de Souza Pinto
Since commit
e85eb5feca8e("misc: fastrpc: fix use-after-free offastrpc_userin workqueue context"), each invoke context holds a reference on itsfastrpc_user, while contexts left on the pending list (interrupted or timed out invocations) are only freed byfastrpc_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:
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.
Quoted 3 lines
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.
drivers/misc/fastrpc.c static int fastrpc_device_release(struct inode *inode, struct file *file) · line 1750
Quoted 4 lines
drivers/misc/fastrpc.c +4
-0+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
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ê
Quoted 84 lines · Murali Krishna
Quoted 6 lines · Renê de Souza Pinto
Since commit
e85eb5feca8e("misc: fastrpc: fix use-after-free offastrpc_userin workqueue context"), each invoke context holds a reference on itsfastrpc_user, while contexts left on the pending list (interrupted or timed out invocations) are only freed byfastrpc_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()takesfastrpc_channel_ctx_get(cctx)andfastrpc_user_get(user), links the context ontofl->pending, and kref_init()s the context.
-fastrpc_invoke_send()takes a second context reference for the DSP, released byput_workfromfastrpc_rpmsg_callback(), or immediately ifrpmsg_send()fails.
-fastrpc_internal_invoke()unlinks the context and drops the first reference in its bail path, but only when err is neither-ERESTARTSYSnor-ETIMEDOUT.
-fastrpc_context_free()never touchesfl->pending; it ends withfastrpc_user_put(fl)andfastrpc_channel_ctx_put(cctx).
fl->pendingis referenced in only four places in the file: theINIT_LIST_HEAD()infastrpc_device_open(), thelist_add_tail()infastrpc_context_alloc(), the drain loop infastrpc_user_free(), andfastrpc_notify_users(), which sets-EPIPEand 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:
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.
Quoted 3 lines
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, fromfastrpc_context_free(). The maps are attached to the session device (dma_buf_attach(map->buf, sess->dev)infastrpc_map_create()) and the attachment is only torn down infastrpc_free_map().fastrpc_session_free()runs immediately after your loop and only setssession->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.drivers/misc/fastrpc.c static int fastrpc_device_release(struct inode *inode, struct file *file) · line 1750
Quoted 4 lines
drivers/misc/fastrpc.c +4
-0+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 offfl->pending, andlist_for_each_entry_safe()does not need them removed, so thelist_del_init()looks redundant.For what it is worth, the patch applies cleanly to char-misc-next at
917fcbf03and builds there onx86_64with 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