Skip to contentSkip to conversation

misc: fastrpc: fix fastrpc_user leak on abnormal process exit

PATCH New Renê de Souza Pinto 1 participant 1 message Nov 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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 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")
Assisted-by Claude:claude-opus-5-5

---
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/[email protected]

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(-)
drivers/misc/fastrpc.c +16 -8
index cdc2627bdaf2..23a5035654a5 100644
@@ -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 */
Signature · 1 line
2.53.0