From: "Renê de Souza Pinto" <rene@renesp.com.br>
To: Srinivas Kandagatla <srini@kernel.org>
Cc: "Ekansh Gupta" <ekansh.gupta@oss.qualcomm.com>,
"Arnd Bergmann" <arnd@arndb.de>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Anandu Krishnan E" <anandu.e@oss.qualcomm.com>,
linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org,
linux-kernel@vger.kernel.org, stable@vger.kernel.org,
"Renê de Souza Pinto" <rene@renesp.com.br>
Subject: [PATCH v2] misc: fastrpc: fix fastrpc_user leak on abnormal process exit
Date: Tue, 24 Nov 2026 15:18:43 +0100 [thread overview]
Message-ID: <20261124141843.3967523-1-rene@renesp.com.br> (raw)
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 instead of in
fastrpc_user_free(). 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>
---
Changes in v2:
- Move the pending cleanup from fastrpc_user_free() to release instead of
duplicating it, and drop the forward declaration of
fastrpc_context_put() (Sashiko)
- Link to v1: https://lore.kernel.org/r/20261124122011.2912030-1-rene@renesp.com.br
Not changed from Sashiko's review of v1:
- fastrpc_session_free() stays at release, as before this patch. Moving
it to fastrpc_user_free() makes a context the DSP never answers keep
its session forever: on QCS8275 this lost two of the three ADSP
sessions after an adsprpcd init failure. Reclaiming unanswered
contexts is left for a separate change.
- Handle maps not released in fastrpc_context_free() is pre-existing:
they stay on fl->maps and are freed at close, which this patch makes
happen again on abnormal exit.
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 | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index cdc2627bdaf2..23a5035654a5 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -554,22 +554,14 @@ static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
}
-static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx);
-
static void fastrpc_user_free(struct kref *ref)
{
struct fastrpc_user *fl = container_of(ref, struct fastrpc_user, refcount);
- struct fastrpc_invoke_ctx *ctx, *n;
struct fastrpc_map *map, *m;
struct fastrpc_buf *buf, *b;
fastrpc_buf_free(fl->init_mem);
- list_for_each_entry_safe(ctx, n, &fl->pending, node) {
- list_del(&ctx->node);
- fastrpc_context_put(ctx);
- }
-
list_for_each_entry_safe(map, m, &fl->maps, node)
fastrpc_map_put(map);
@@ -1725,7 +1717,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 +1727,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
reply other threads:[~2026-11-24 14:19 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124141843.3967523-1-rene@renesp.com.br \
--to=rene@renesp.com.br \
--cc=anandu.e@oss.qualcomm.com \
--cc=arnd@arndb.de \
--cc=dri-devel@lists.freedesktop.org \
--cc=ekansh.gupta@oss.qualcomm.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=srini@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®