misc: fastrpc: fix context leak and hang on signal-interrupted invoke
PATCH v2 1/1 misc: fastrpc: fix context leak and hang on signal-interrupted invoke
fastrpc invokes work by sending an RPC message to the DSP and blocking in wait_for_completion_interruptible() until the DSP responds. If a signal arrives during this wait, the syscall returns -ERESTARTSYS and the invoke context which holds the in-flight DMA buffers and completion state is left stranded in fl->pending.
On the next syscall attempt (either auto-restarted by the kernel via SA_RESTART or manually retried by user-space after EINTR), a fresh context is allocated and the RPC message is re-sent to the DSP. This has two consequences:
- The original context leaks in fl->pending until the file is closed.
- The DSP receives a duplicate invocation. If the DSP was mid-way through processing the first request and had issued a reverse RPC call back to the host, the retry sends a new forward request instead of the expected reverse-RPC response. The DSP thread waiting for that response is never woken, causing a hang.
Fix this by saving the interrupted context to a new fl->interrupted list on -ERESTARTSYS. When the same thread retries the invoke with a matching sc, restore the context and jump directly to the wait, skipping context allocation and message re-send.
Three additional bugs are also fixed:
1. fastrpc_context_save_interrupted() did not drop the extra kref that fastrpc_invoke_send() took, so the worker's put only reached 1 and the context was never freed. Drop the reference in save_interrupted() so the worker's put reaches 0 and triggers context_free.
2. The bail path skipped context cleanup on -ETIMEDOUT, leaking the context. Remove the -ETIMEDOUT exception so timed-out contexts are freed like any other error path.
3. fastrpc_context_free() did not remove the context from fl->interrupted before freeing it. A context freed via a racing channel removal left a dangling node in the list. Add list_del_init() guarded by fl->lock before releasing resources.
Also drain fl->interrupted on process exit, complete any sleeping contexts with -EPIPE and schedule put_wq to drop the worker ref when the rpmsg channel is removed, and add dev_warn() on TIMEOUT and ERESTARTSYS to aid post-mortem debugging.
Remove the obsolete invoke_interrupted_mmaps mechanism from fastrpc_channel_ctx; context resources are now kept alive through the context refcount rather than by migrating mmaps to a channel-level list.
---
This patch fixes a context leak and DSP hang that occur when a fastrpc invoke syscall is interrupted by a signal, along with three follow-on bugs found during review.
Changes in v2:
- Fix kref imbalance: fastrpc_context_save_interrupted() now drops the extra reference taken by fastrpc_invoke_send(), so the worker's put correctly reaches 0 and triggers context_free.
- Fix -ETIMEDOUT context leak: remove the ETIMEDOUT exception from the bail path so timed-out kernel invocations go through the same list_del_init + context_put cleanup as all other error paths.
- Fix dangling node: fastrpc_context_free() now calls list_del_init() under fl->lock before releasing resources, preventing a dangling node in fl->interrupted if a racing channel removal fires put_wq.
- fastrpc_notify_users() now calls schedule_work(&ctx->put_work) for interrupted contexts to drop the worker ref on channel removal.
- Add dev_warn() on TIMEOUT and ERESTARTSYS for post-mortem debugging.
- Use list_del_init() instead of list_del() in the bail path.
Link to v1: https://lore.kernel.org/all/[email protected]/
--- drivers/misc/fastrpc.c | 101 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 19 deletions(-)
drivers/misc/fastrpc.c +82 -19
@@ -280,7 +280,6 @@ struct fastrpc_channel_ctx {struct fastrpc_device *secure_fdevice;struct fastrpc_device *fdevice;struct fastrpc_buf *remote_heap; -struct list_head invoke_interrupted_mmaps;bool secure;bool unsigned_support;u64 dma_mask;
@@ -297,6 +296,7 @@ struct fastrpc_user {struct list_head user;struct list_head maps;struct list_head pending; +struct list_head interrupted;struct list_head mmaps;struct fastrpc_channel_ctx *cctx;
@@ -522,6 +522,11 @@ static void fastrpc_user_free(struct kref *ref)fastrpc_context_put (ctx); } +list_for_each_entry_safe (ctx, n, &fl->interrupted, node) { +list_del (&ctx->node); +fastrpc_context_put (ctx); + } +list_for_each_entry_safe (map, m, &fl->maps, node)fastrpc_map_put (map);
@@ -557,6 +562,12 @@ static void fastrpc_context_free(struct kref *ref) cctx = ctx->cctx; fl = ctx->fl; +/* Remove from fl->interrupted if present; no-op for normal paths. */ +spin_lock (&fl->lock); +if (!list_empty (&ctx->node)) +list_del_init (&ctx->node); +spin_unlock (&fl->lock); +for (i =0 ; i < ctx->nbufs; i++)fastrpc_map_put (ctx->maps[i]);
@@ -594,6 +605,42 @@ static void fastrpc_context_put_wq(struct work_struct *work)fastrpc_context_put (ctx); } +static void fastrpc_context_save_interrupted (struct fastrpc_invoke_ctx *ctx) +{ +spin_lock (&ctx->fl->lock); +list_del (&ctx->node); +list_add_tail (&ctx->node, &ctx->fl->interrupted); +spin_unlock (&ctx->fl->lock); +/* +* invoke_send bumped the kref to 2; the bail path skips the put +* for ERESTARTSYS. Drop it here so the worker's put reaches 0 +* and triggers context_free. +*/ +fastrpc_context_put (ctx); +} + +static struct fastrpc_invoke_ctx *fastrpc_context_restore_interrupted ( +struct fastrpc_user *fl,u32 sc) +{ +struct fastrpc_invoke_ctx *ctx =NULL , *ictx, *n; + +spin_lock (&fl->lock); +list_for_each_entry_safe (ictx, n, &fl->interrupted, node) { +if (ictx->pid != current->pid) +continue ; +if (ictx->sc != sc || ictx->fl != fl) { +spin_unlock (&fl->lock); +return ERR_PTR (-EINVAL ); + } + ctx = ictx; +list_del (&ctx->node); +list_add_tail (&ctx->node, &fl->pending); +break ; + } +spin_unlock (&fl->lock); +return ctx; +} +#define CMP (aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)static int olaps_cmp (const void *a,const void *b) {
@@ -1243,8 +1290,6 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, struct fastrpc_invoke_args *args) {struct fastrpc_invoke_ctx *ctx =NULL ; -struct fastrpc_buf *buf, *b; -int err =0 ;if (!fl->sctx)
@@ -1258,6 +1303,14 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,return -EPERM ; } +if (!kernel) { + ctx =fastrpc_context_restore_interrupted (fl, sc); +if (IS_ERR (ctx)) +return PTR_ERR (ctx); +if (ctx) + goto wait; + } + ctx =fastrpc_context_alloc (fl, kernel, sc, args);if (IS_ERR (ctx))return PTR_ERR (ctx);
@@ -1273,11 +1326,20 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,if (err) goto bail; +wait:if (kernel) { -if (!wait_for_completion_timeout (&ctx->work,10 *HZ )) +if (!wait_for_completion_timeout (&ctx->work,10 *HZ )) { err = -ETIMEDOUT ; +dev_warn (fl->sctx->dev, +"fastrpc_invoke: TIMEOUT ctxid=0x%llx handle=0x%x nscalars=%d\n" , + ctx->ctxid, handle, ctx->nscalars); + } }else { err =wait_for_completion_interruptible (&ctx->work); +if (err == -ERESTARTSYS ) +dev_warn (fl->sctx->dev, +"fastrpc_invoke: INTERRUPTED ctxid=0x%llx handle=0x%x nscalars=%d\n" , + ctx->ctxid, handle, ctx->nscalars); }if (err)
@@ -1296,21 +1358,15 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, goto bail; bail: -if (err != -ERESTARTSYS && err != -ETIMEDOUT ) { -/* We are done with this compute context */ +if (ctx && err == -ERESTARTSYS ) { +fastrpc_context_save_interrupted (ctx); + }else if (ctx) {spin_lock (&fl->lock); -list_del (&ctx->node); +list_del_init (&ctx->node);spin_unlock (&fl->lock);fastrpc_context_put (ctx); } -if (err == -ERESTARTSYS ) { -list_for_each_entry_safe (buf, b, &fl->mmaps, node) { -list_del (&buf->node); -list_add_tail (&buf->node, &fl->cctx->invoke_interrupted_mmaps); - } - } -if (err)dev_dbg (fl->sctx->dev,"Error: Invoke Failed %d\n" , err);
@@ -1662,6 +1718,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)spin_lock_init (&fl->lock);mutex_init (&fl->mutex);INIT_LIST_HEAD (&fl->pending); +INIT_LIST_HEAD (&fl->interrupted);INIT_LIST_HEAD (&fl->maps);INIT_LIST_HEAD (&fl->mmaps);INIT_LIST_HEAD (&fl->user);
@@ -2460,7 +2517,6 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) rdev->dma_mask = &data->dma_mask;dma_set_mask_and_coherent (rdev,DMA_BIT_MASK (32 ));INIT_LIST_HEAD (&data->users); -INIT_LIST_HEAD (&data->invoke_interrupted_mmaps);spin_lock_init (&data->lock);idr_init (&data->ctx_idr); data->domain_id = domain_id;
@@ -2493,13 +2549,23 @@ static void fastrpc_notify_users(struct fastrpc_user *user) ctx->retval = -EPIPE ;complete (&ctx->work); } +/* +* Interrupted contexts hold two refs: one for the invoker and one +* for the async worker from fastrpc_invoke_send. Complete them so +* any blocked retry wakes, and schedule put_wq to drop the worker +* ref -- the invoker ref is released by fastrpc_user_free(). +*/ +list_for_each_entry (ctx, &user->interrupted, node) { + ctx->retval = -EPIPE ; +complete (&ctx->work); +schedule_work (&ctx->put_work); + }spin_unlock (&user->lock); }static void fastrpc_rpmsg_remove (struct rpmsg_device *rpdev) {struct fastrpc_channel_ctx *cctx =dev_get_drvdata (&rpdev->dev); -struct fastrpc_buf *buf, *b;struct fastrpc_user *user;unsigned long flags;
@@ -2516,9 +2582,6 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)if (cctx->secure_fdevice)misc_deregister (&cctx->secure_fdevice->miscdev); -list_for_each_entry_safe (buf, b, &cctx->invoke_interrupted_mmaps, node) -list_del (&buf->node); -if (cctx->remote_heap)fastrpc_buf_free (cctx->remote_heap);
---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260701-master-2ba6246f29d1
Best regards,
Signature · 1 line
Anandu Krishnan E <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] Restoring an interrupted context fails to increment its reference count, leading to a…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] Restoring an interrupted context fails to increment its reference count, leading to a Use-After-Free and Double Free when the syscall completes.
- [Critical] Retrying an interrupted fastrpc invocation with a newly allocated arguments structure forces the kernel to blindly write output data to old, potentially invalidated buffer addresses.
- [High] If a thread's fastrpc call is interrupted and it subsequently attempts a completely different fastrpc call, it will be permanently blocked from using the device.
- [High] Double-scheduling ctx->put_work in fastrpc_notify_users() causes list corruption and a Use-After-Free if the async worker is already executing.
--
commit d067e3f966edecc9e2338c8eea087bb05365a462
Author: Anandu Krishnan E <[email protected]>
misc: fastrpc: fix context leak and hang on signal-interrupted invoke
This commit aims to fix a context leak and hang when a fastrpc invocation is interrupted by a signal. It saves the interrupted context to a new list and restores it on the next syscall attempt.
Quoted 4 lines
drivers/misc/fastrpc.c +0
-0
[ ... ]
Quoted 20 lines
drivers/misc/fastrpc.c +16
-0@@ -594,6 +605,42 @@ static void fastrpc_context_put_wq(struct work_struct *work)fastrpc_context_put (ctx); } +static void fastrpc_context_save_interrupted (struct fastrpc_invoke_ctx *ctx) +{ +spin_lock (&ctx->fl->lock); +list_del (&ctx->node); +list_add_tail (&ctx->node, &ctx->fl->interrupted); +spin_unlock (&ctx->fl->lock); +/* +* invoke_send bumped the kref to 2; the bail path skips the put +* for ERESTARTSYS. Drop it here so the worker's put reaches 0 +* and triggers context_free. +*/ +fastrpc_context_put (ctx); +} + +static struct fastrpc_invoke_ctx *fastrpc_context_restore_interrupted ( +struct fastrpc_user *fl,u32 sc)
drivers/misc/fastrpc.c static void fastrpc_context_put_wq(struct work_struct *work) · line 633
Quoted 10 lines
drivers/misc/fastrpc.c +10
-0+{ +struct fastrpc_invoke_ctx *ctx =NULL , *ictx, *n; + +spin_lock (&fl->lock); +list_for_each_entry_safe (ictx, n, &fl->interrupted, node) { +if (ictx->pid != current->pid) +continue ; +if (ictx->sc != sc || ictx->fl != fl) { +spin_unlock (&fl->lock); +return ERR_PTR (-EINVAL );
[Severity: High]
If a thread gets interrupted and attempts a new fastrpc call with a different sc, this returns -EINVAL but leaves the old context on fl->interrupted.
Will every subsequent fastrpc call by this thread with a different sc hit this same mismatch and instantly fail with -EINVAL, effectively wedging the thread?
drivers/misc/fastrpc.c static void fastrpc_context_put_wq(struct work_struct *work) · line 638
Quoted 5 lines
drivers/misc/fastrpc.c +5
-0+ } + ctx = ictx; +list_del (&ctx->node); +list_add_tail (&ctx->node, &fl->pending); +break ;
[Severity: Critical]
Does restoring the context here require incrementing its reference count via fastrpc_context_get()?
Since the invoker's reference is intentionally dropped in fastrpc_context_save_interrupted(), if the async worker completes the work and drops its reference, could the user thread wake up and unconditionally call fastrpc_context_put() in the bail path on freed memory?
Quoted 5 lines
drivers/misc/fastrpc.c +5
-0+ } +spin_unlock (&fl->lock); +return ctx; +} +
[ ... ]
drivers/misc/fastrpc.c static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, · line 1311
Quoted 10 lines
drivers/misc/fastrpc.c +6
-0@@ -1258,6 +1303,14 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,return -EPERM ; } +if (!kernel) { + ctx =fastrpc_context_restore_interrupted (fl, sc); +if (IS_ERR (ctx)) +return PTR_ERR (ctx); +if (ctx) + goto wait;
[Severity: Critical]
By jumping directly to wait, does this bypass argument extraction for retries?
If a user thread unwinds its stack after a signal and initiates a completely different call that happens to have the same sc, it seems the output data might be copied directly to the old buffer addresses stored in ctx->args, which could now contain other sensitive data.
Quoted 5 lines
drivers/misc/fastrpc.c +2
-0+ } + ctx =fastrpc_context_alloc (fl, kernel, sc, args);if (IS_ERR (ctx))return PTR_ERR (ctx);
[ ... ]
Quoted 4 lines
drivers/misc/fastrpc.c +0
-0@@ -2493,13 +2549,23 @@ static void fastrpc_notify_users(struct fastrpc_user *user) ctx->retval = -EPIPE ;complete (&ctx->work); }
drivers/misc/fastrpc.c static void fastrpc_notify_users(struct fastrpc_user *user) · line 2561
Quoted 10 lines
drivers/misc/fastrpc.c +10
-0+/* +* Interrupted contexts hold two refs: one for the invoker and one +* for the async worker from fastrpc_invoke_send. Complete them so +* any blocked retry wakes, and schedule put_wq to drop the worker +* ref -- the invoker ref is released by fastrpc_user_free(). +*/ +list_for_each_entry (ctx, &user->interrupted, node) { + ctx->retval = -EPIPE ; +complete (&ctx->work); +schedule_work (&ctx->put_work);
[Severity: High]
Is it safe to unconditionally schedule ctx->put_work here?
If a DSP response arrives concurrently and the async worker has already started executing fastrpc_context_put_wq(), its WORK_STRUCT_PENDING flag would be clear.
Could this allow the work to be queued a second time, triggering a use-after-free in the workqueue core once the first execution frees the context?
Quoted 3 lines
drivers/misc/fastrpc.c +1
-0+ }spin_unlock (&user->lock); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 6 lines · On Wed, Jul 01, 2026 at 08:27:21AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] Restoring an interrupted context fails to increment its reference count, leading to a Use-After-Free and Double Free when the syscall completes.
- [Critical] Retrying an interrupted fastrpc invocation with a newly allocated arguments structure forces the kernel to blindly write output data to old, potentially invalidated buffer addresses.
- [High] If a thread's fastrpc call is interrupted and it subsequently attempts a completely different fastrpc call, it will be permanently blocked from using the device.
- [High] Double-schedulingctx->put_workinfastrpc_notify_users()causes list corruption and a Use-After-Free if the async worker is already executing.
--
Please review and at least react on the hose comments.
Signature · 2 lines
With best wishes Dmitry
Srinivas Kandagatla
maintainerQuoted 48 lines · Anandu Krishnan E
fastrpc invokes work by sending an RPC message to the DSP and blocking in
wait_for_completion_interruptible()until the DSP responds. If a signal arrives during this wait, the syscall returns-ERESTARTSYSand the invoke context which holds the in-flight DMA buffers and completion state is left stranded infl->pending.On the next syscall attempt (either auto-restarted by the kernel via
SA_RESTARTor manually retried by user-space after EINTR), a fresh context is allocated and the RPC message is re-sent to the DSP. This has two consequences:- The original context leaks in
fl->pendinguntil the file is closed.
- The DSP receives a duplicate invocation. If the DSP was mid-way through processing the first request and had issued a reverse RPC call back to the host, the retry sends a new forward request instead of the expected reverse-RPC response. The DSP thread waiting for that response is never woken, causing a hang.Fix this by saving the interrupted context to a new
fl->interruptedlist on-ERESTARTSYS. When the same thread retries the invoke with a matching sc, restore the context and jump directly to the wait, skipping context allocation and message re-send.Three additional bugs are also fixed:
1.
fastrpc_context_save_interrupted()did not drop the extra kref thatfastrpc_invoke_send()took, so the worker's put only reached 1 and the context was never freed. Drop the reference insave_interrupted()so the worker's put reaches 0 and triggerscontext_free.2. The bail path skipped context cleanup on
-ETIMEDOUT, leaking the context. Remove the-ETIMEDOUTexception so timed-out contexts are freed like any other error path.3.
fastrpc_context_free()did not remove the context fromfl->interruptedbefore freeing it. A context freed via a racing channel removal left a dangling node in the list. Addlist_del_init()guarded byfl->lockbefore releasing resources.Also drain
fl->interruptedon process exit, complete any sleeping contexts with-EPIPEand scheduleput_wqto drop the worker ref when the rpmsg channel is removed, and adddev_warn()on TIMEOUT and ERESTARTSYS to aid post-mortem debugging.Remove the obsolete
invoke_interrupted_mmapsmechanism fromfastrpc_channel_ctx; context resources are now kept alive through the context refcount rather than by migrating mmaps to a channel-level list.
Quoted 6 lines
Fixes 387f625585d1 ("misc: fastrpc: handle interrupted contexts")Co-developed-by Srinivas Kandagatla <[email protected]>Signed-off-by Srinivas Kandagatla <[email protected]>Signed-off-by Anandu Krishnan E <[email protected]>---
Does not apply to linux-next.
Can you rebase and resend.
--srini
Quoted 244 lines
This patch fixes a context leak and DSP hang that occur when a fastrpc invoke syscall is interrupted by a signal, along with three follow-on bugs found during review.
Changes in v2:
- Fix kref imbalance:fastrpc_context_save_interrupted()now drops the extra reference taken byfastrpc_invoke_send(), so the worker's put correctly reaches 0 and triggerscontext_free.
- Fix-ETIMEDOUTcontext leak: remove the ETIMEDOUT exception from the bail path so timed-out kernel invocations go through the samelist_del_init+context_putcleanup as all other error paths.
- Fix dangling node:fastrpc_context_free()now callslist_del_init()underfl->lockbefore releasing resources, preventing a dangling node infl->interruptedif a racing channel removal firesput_wq.
-fastrpc_notify_users()now callsschedule_work(&ctx->put_work)for interrupted contexts to drop the worker ref on channel removal.
- Adddev_warn()on TIMEOUT and ERESTARTSYS for post-mortem debugging.
- Uselist_del_init()instead oflist_del()in the bail path.Link to v1: https://lore.kernel.org/all/[email protected]/
--- drivers/misc/fastrpc.c | 101 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 19 deletions(-)drivers/misc/fastrpc.c +82
-20@@ -280,7 +280,6 @@ struct fastrpc_channel_ctx {struct fastrpc_device *secure_fdevice;struct fastrpc_device *fdevice;struct fastrpc_buf *remote_heap; -struct list_head invoke_interrupted_mmaps;bool secure;bool unsigned_support;u64 dma_mask;@@ -297,6 +296,7 @@ struct fastrpc_user {struct list_head user;struct list_head maps;struct list_head pending; +struct list_head interrupted;struct list_head mmaps;struct fastrpc_channel_ctx *cctx;@@ -522,6 +522,11 @@ static void fastrpc_user_free(struct kref *ref)fastrpc_context_put (ctx); } +list_for_each_entry_safe (ctx, n, &fl->interrupted, node) { +list_del (&ctx->node); +fastrpc_context_put (ctx); + } +list_for_each_entry_safe (map, m, &fl->maps, node)fastrpc_map_put (map);@@ -557,6 +562,12 @@ static void fastrpc_context_free(struct kref *ref) cctx = ctx->cctx; fl = ctx->fl; +/* Remove from fl->interrupted if present; no-op for normal paths. */ +spin_lock (&fl->lock); +if (!list_empty (&ctx->node)) +list_del_init (&ctx->node); +spin_unlock (&fl->lock); +for (i =0 ; i < ctx->nbufs; i++)fastrpc_map_put (ctx->maps[i]);@@ -594,6 +605,42 @@ static void fastrpc_context_put_wq(struct work_struct *work)fastrpc_context_put (ctx); } +static void fastrpc_context_save_interrupted (struct fastrpc_invoke_ctx *ctx) +{ +spin_lock (&ctx->fl->lock); +list_del (&ctx->node); +list_add_tail (&ctx->node, &ctx->fl->interrupted); +spin_unlock (&ctx->fl->lock); +/* +* invoke_send bumped the kref to 2; the bail path skips the put +* for ERESTARTSYS. Drop it here so the worker's put reaches 0 +* and triggers context_free. +*/ +fastrpc_context_put (ctx); +} + +static struct fastrpc_invoke_ctx *fastrpc_context_restore_interrupted ( +struct fastrpc_user *fl,u32 sc) +{ +struct fastrpc_invoke_ctx *ctx =NULL , *ictx, *n; + +spin_lock (&fl->lock); +list_for_each_entry_safe (ictx, n, &fl->interrupted, node) { +if (ictx->pid != current->pid) +continue ; +if (ictx->sc != sc || ictx->fl != fl) { +spin_unlock (&fl->lock); +return ERR_PTR (-EINVAL ); + } + ctx = ictx; +list_del (&ctx->node); +list_add_tail (&ctx->node, &fl->pending); +break ; + } +spin_unlock (&fl->lock); +return ctx; +} +#define CMP (aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)static int olaps_cmp (const void *a,const void *b) {@@ -1243,8 +1290,6 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, struct fastrpc_invoke_args *args) {struct fastrpc_invoke_ctx *ctx =NULL ; -struct fastrpc_buf *buf, *b; -int err =0 ;if (!fl->sctx)@@ -1258,6 +1303,14 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,return -EPERM ; } +if (!kernel) { + ctx =fastrpc_context_restore_interrupted (fl, sc); +if (IS_ERR (ctx)) +return PTR_ERR (ctx); +if (ctx) + goto wait; + } + ctx =fastrpc_context_alloc (fl, kernel, sc, args);if (IS_ERR (ctx))return PTR_ERR (ctx);@@ -1273,11 +1326,20 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,if (err) goto bail; +wait:if (kernel) { -if (!wait_for_completion_timeout (&ctx->work,10 *HZ )) +if (!wait_for_completion_timeout (&ctx->work,10 *HZ )) { err = -ETIMEDOUT ; +dev_warn (fl->sctx->dev, +"fastrpc_invoke: TIMEOUT ctxid=0x%llx handle=0x%x nscalars=%d\n" , + ctx->ctxid, handle, ctx->nscalars); + } }else { err =wait_for_completion_interruptible (&ctx->work); +if (err == -ERESTARTSYS ) +dev_warn (fl->sctx->dev, +"fastrpc_invoke: INTERRUPTED ctxid=0x%llx handle=0x%x nscalars=%d\n" , + ctx->ctxid, handle, ctx->nscalars); }if (err)@@ -1296,21 +1358,15 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, goto bail; bail: -if (err != -ERESTARTSYS && err != -ETIMEDOUT ) { -/* We are done with this compute context */ +if (ctx && err == -ERESTARTSYS ) { +fastrpc_context_save_interrupted (ctx); + }else if (ctx) {spin_lock (&fl->lock); -list_del (&ctx->node); +list_del_init (&ctx->node);spin_unlock (&fl->lock);fastrpc_context_put (ctx); } -if (err == -ERESTARTSYS ) { -list_for_each_entry_safe (buf, b, &fl->mmaps, node) { -list_del (&buf->node); -list_add_tail (&buf->node, &fl->cctx->invoke_interrupted_mmaps); - } - } -if (err)dev_dbg (fl->sctx->dev,"Error: Invoke Failed %d\n" , err);@@ -1662,6 +1718,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)spin_lock_init (&fl->lock);mutex_init (&fl->mutex);INIT_LIST_HEAD (&fl->pending); +INIT_LIST_HEAD (&fl->interrupted);INIT_LIST_HEAD (&fl->maps);INIT_LIST_HEAD (&fl->mmaps);INIT_LIST_HEAD (&fl->user);@@ -2460,7 +2517,6 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) rdev->dma_mask = &data->dma_mask;dma_set_mask_and_coherent (rdev,DMA_BIT_MASK (32 ));INIT_LIST_HEAD (&data->users); -INIT_LIST_HEAD (&data->invoke_interrupted_mmaps);spin_lock_init (&data->lock);idr_init (&data->ctx_idr); data->domain_id = domain_id;@@ -2493,13 +2549,23 @@ static void fastrpc_notify_users(struct fastrpc_user *user) ctx->retval = -EPIPE ;complete (&ctx->work); } +/* +* Interrupted contexts hold two refs: one for the invoker and one +* for the async worker from fastrpc_invoke_send. Complete them so +* any blocked retry wakes, and schedule put_wq to drop the worker +* ref -- the invoker ref is released by fastrpc_user_free(). +*/ +list_for_each_entry (ctx, &user->interrupted, node) { + ctx->retval = -EPIPE ; +complete (&ctx->work); +schedule_work (&ctx->put_work); + }spin_unlock (&user->lock); }static void fastrpc_rpmsg_remove (struct rpmsg_device *rpdev) {struct fastrpc_channel_ctx *cctx =dev_get_drvdata (&rpdev->dev); -struct fastrpc_buf *buf, *b;struct fastrpc_user *user;unsigned long flags;@@ -2516,9 +2582,6 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)if (cctx->secure_fdevice)misc_deregister (&cctx->secure_fdevice->miscdev); -list_for_each_entry_safe (buf, b, &cctx->invoke_interrupted_mmaps, node) -list_del (&buf->node); -if (cctx->remote_heap)fastrpc_buf_free (cctx->remote_heap); ---base-commit:
dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260701-master-2ba6246f29d1Best regards,
Signature · 1 line
Anandu Krishnan E <[email protected]>
Quoted 56 lines · Srinivas Kandagatla
Quoted 48 lines · Anandu Krishnan E
fastrpc invokes work by sending an RPC message to the DSP and blocking in
wait_for_completion_interruptible()until the DSP responds. If a signal arrives during this wait, the syscall returns-ERESTARTSYSand the invoke context which holds the in-flight DMA buffers and completion state is left stranded infl->pending.On the next syscall attempt (either auto-restarted by the kernel via
SA_RESTARTor manually retried by user-space after EINTR), a fresh context is allocated and the RPC message is re-sent to the DSP. This has two consequences:- The original context leaks in
fl->pendinguntil the file is closed.
- The DSP receives a duplicate invocation. If the DSP was mid-way through processing the first request and had issued a reverse RPC call back to the host, the retry sends a new forward request instead of the expected reverse-RPC response. The DSP thread waiting for that response is never woken, causing a hang.Fix this by saving the interrupted context to a new
fl->interruptedlist on-ERESTARTSYS. When the same thread retries the invoke with a matching sc, restore the context and jump directly to the wait, skipping context allocation and message re-send.Three additional bugs are also fixed:
1.
fastrpc_context_save_interrupted()did not drop the extra kref thatfastrpc_invoke_send()took, so the worker's put only reached 1 and the context was never freed. Drop the reference insave_interrupted()so the worker's put reaches 0 and triggerscontext_free.2. The bail path skipped context cleanup on
-ETIMEDOUT, leaking the context. Remove the-ETIMEDOUTexception so timed-out contexts are freed like any other error path.3.
fastrpc_context_free()did not remove the context fromfl->interruptedbefore freeing it. A context freed via a racing channel removal left a dangling node in the list. Addlist_del_init()guarded byfl->lockbefore releasing resources.Also drain
fl->interruptedon process exit, complete any sleeping contexts with-EPIPEand scheduleput_wqto drop the worker ref when the rpmsg channel is removed, and adddev_warn()on TIMEOUT and ERESTARTSYS to aid post-mortem debugging.Remove the obsolete
invoke_interrupted_mmapsmechanism fromfastrpc_channel_ctx; context resources are now kept alive through the context refcount rather than by migrating mmaps to a channel-level list.Quoted 6 lines
Fixes 387f625585d1 ("misc: fastrpc: handle interrupted contexts")Co-developed-by Srinivas Kandagatla <[email protected]>Signed-off-by Srinivas Kandagatla <[email protected]>Signed-off-by Anandu Krishnan E <[email protected]>---
Quoted 3 lines
Does not apply to linux-next.
Can you rebase and resend.
sure will share
-- anandu
Quoted 247 lines
--srini
Quoted 244 lines
This patch fixes a context leak and DSP hang that occur when a fastrpc invoke syscall is interrupted by a signal, along with three follow-on bugs found during review.
Changes in v2:
- Fix kref imbalance:fastrpc_context_save_interrupted()now drops the extra reference taken byfastrpc_invoke_send(), so the worker's put correctly reaches 0 and triggerscontext_free.
- Fix-ETIMEDOUTcontext leak: remove the ETIMEDOUT exception from the bail path so timed-out kernel invocations go through the samelist_del_init+context_putcleanup as all other error paths.
- Fix dangling node:fastrpc_context_free()now callslist_del_init()underfl->lockbefore releasing resources, preventing a dangling node infl->interruptedif a racing channel removal firesput_wq.
-fastrpc_notify_users()now callsschedule_work(&ctx->put_work)for interrupted contexts to drop the worker ref on channel removal.
- Adddev_warn()on TIMEOUT and ERESTARTSYS for post-mortem debugging.
- Uselist_del_init()instead oflist_del()in the bail path.Link to v1: https://lore.kernel.org/all/[email protected]/
--- drivers/misc/fastrpc.c | 101 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 19 deletions(-)drivers/misc/fastrpc.c +82
-20@@ -280,7 +280,6 @@ struct fastrpc_channel_ctx {struct fastrpc_device *secure_fdevice;struct fastrpc_device *fdevice;struct fastrpc_buf *remote_heap; -struct list_head invoke_interrupted_mmaps;bool secure;bool unsigned_support;u64 dma_mask;@@ -297,6 +296,7 @@ struct fastrpc_user {struct list_head user;struct list_head maps;struct list_head pending; +struct list_head interrupted;struct list_head mmaps;struct fastrpc_channel_ctx *cctx;@@ -522,6 +522,11 @@ static void fastrpc_user_free(struct kref *ref)fastrpc_context_put (ctx); } +list_for_each_entry_safe (ctx, n, &fl->interrupted, node) { +list_del (&ctx->node); +fastrpc_context_put (ctx); + } +list_for_each_entry_safe (map, m, &fl->maps, node)fastrpc_map_put (map);@@ -557,6 +562,12 @@ static void fastrpc_context_free(struct kref *ref) cctx = ctx->cctx; fl = ctx->fl; +/* Remove from fl->interrupted if present; no-op for normal paths. */ +spin_lock (&fl->lock); +if (!list_empty (&ctx->node)) +list_del_init (&ctx->node); +spin_unlock (&fl->lock); +for (i =0 ; i < ctx->nbufs; i++)fastrpc_map_put (ctx->maps[i]);@@ -594,6 +605,42 @@ static void fastrpc_context_put_wq(struct work_struct *work)fastrpc_context_put (ctx); } +static void fastrpc_context_save_interrupted (struct fastrpc_invoke_ctx *ctx) +{ +spin_lock (&ctx->fl->lock); +list_del (&ctx->node); +list_add_tail (&ctx->node, &ctx->fl->interrupted); +spin_unlock (&ctx->fl->lock); +/* +* invoke_send bumped the kref to 2; the bail path skips the put +* for ERESTARTSYS. Drop it here so the worker's put reaches 0 +* and triggers context_free. +*/ +fastrpc_context_put (ctx); +} + +static struct fastrpc_invoke_ctx *fastrpc_context_restore_interrupted ( +struct fastrpc_user *fl,u32 sc) +{ +struct fastrpc_invoke_ctx *ctx =NULL , *ictx, *n; + +spin_lock (&fl->lock); +list_for_each_entry_safe (ictx, n, &fl->interrupted, node) { +if (ictx->pid != current->pid) +continue ; +if (ictx->sc != sc || ictx->fl != fl) { +spin_unlock (&fl->lock); +return ERR_PTR (-EINVAL ); + } + ctx = ictx; +list_del (&ctx->node); +list_add_tail (&ctx->node, &fl->pending); +break ; + } +spin_unlock (&fl->lock); +return ctx; +} +#define CMP (aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)static int olaps_cmp (const void *a,const void *b) {@@ -1243,8 +1290,6 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, struct fastrpc_invoke_args *args) {struct fastrpc_invoke_ctx *ctx =NULL ; -struct fastrpc_buf *buf, *b; -int err =0 ;if (!fl->sctx)@@ -1258,6 +1303,14 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,return -EPERM ; } +if (!kernel) { + ctx =fastrpc_context_restore_interrupted (fl, sc); +if (IS_ERR (ctx)) +return PTR_ERR (ctx); +if (ctx) + goto wait; + } + ctx =fastrpc_context_alloc (fl, kernel, sc, args);if (IS_ERR (ctx))return PTR_ERR (ctx);@@ -1273,11 +1326,20 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,if (err) goto bail; +wait:if (kernel) { -if (!wait_for_completion_timeout (&ctx->work,10 *HZ )) +if (!wait_for_completion_timeout (&ctx->work,10 *HZ )) { err = -ETIMEDOUT ; +dev_warn (fl->sctx->dev, +"fastrpc_invoke: TIMEOUT ctxid=0x%llx handle=0x%x nscalars=%d\n" , + ctx->ctxid, handle, ctx->nscalars); + } }else { err =wait_for_completion_interruptible (&ctx->work); +if (err == -ERESTARTSYS ) +dev_warn (fl->sctx->dev, +"fastrpc_invoke: INTERRUPTED ctxid=0x%llx handle=0x%x nscalars=%d\n" , + ctx->ctxid, handle, ctx->nscalars); }if (err)@@ -1296,21 +1358,15 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, goto bail; bail: -if (err != -ERESTARTSYS && err != -ETIMEDOUT ) { -/* We are done with this compute context */ +if (ctx && err == -ERESTARTSYS ) { +fastrpc_context_save_interrupted (ctx); + }else if (ctx) {spin_lock (&fl->lock); -list_del (&ctx->node); +list_del_init (&ctx->node);spin_unlock (&fl->lock);fastrpc_context_put (ctx); } -if (err == -ERESTARTSYS ) { -list_for_each_entry_safe (buf, b, &fl->mmaps, node) { -list_del (&buf->node); -list_add_tail (&buf->node, &fl->cctx->invoke_interrupted_mmaps); - } - } -if (err)dev_dbg (fl->sctx->dev,"Error: Invoke Failed %d\n" , err);@@ -1662,6 +1718,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)spin_lock_init (&fl->lock);mutex_init (&fl->mutex);INIT_LIST_HEAD (&fl->pending); +INIT_LIST_HEAD (&fl->interrupted);INIT_LIST_HEAD (&fl->maps);INIT_LIST_HEAD (&fl->mmaps);INIT_LIST_HEAD (&fl->user);@@ -2460,7 +2517,6 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) rdev->dma_mask = &data->dma_mask;dma_set_mask_and_coherent (rdev,DMA_BIT_MASK (32 ));INIT_LIST_HEAD (&data->users); -INIT_LIST_HEAD (&data->invoke_interrupted_mmaps);spin_lock_init (&data->lock);idr_init (&data->ctx_idr); data->domain_id = domain_id;@@ -2493,13 +2549,23 @@ static void fastrpc_notify_users(struct fastrpc_user *user) ctx->retval = -EPIPE ;complete (&ctx->work); } +/* +* Interrupted contexts hold two refs: one for the invoker and one +* for the async worker from fastrpc_invoke_send. Complete them so +* any blocked retry wakes, and schedule put_wq to drop the worker +* ref -- the invoker ref is released by fastrpc_user_free(). +*/ +list_for_each_entry (ctx, &user->interrupted, node) { + ctx->retval = -EPIPE ; +complete (&ctx->work); +schedule_work (&ctx->put_work); + }spin_unlock (&user->lock); }static void fastrpc_rpmsg_remove (struct rpmsg_device *rpdev) {struct fastrpc_channel_ctx *cctx =dev_get_drvdata (&rpdev->dev); -struct fastrpc_buf *buf, *b;struct fastrpc_user *user;unsigned long flags;@@ -2516,9 +2582,6 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)if (cctx->secure_fdevice)misc_deregister (&cctx->secure_fdevice->miscdev); -list_for_each_entry_safe (buf, b, &cctx->invoke_interrupted_mmaps, node) -list_del (&buf->node); -if (cctx->remote_heap)fastrpc_buf_free (cctx->remote_heap); ---base-commit:
dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260701-master-2ba6246f29d1Best regards,
Signature · 1 line
Anandu Krishnan E <[email protected]>
Quoted 153 lines · Anandu Krishnan E
fastrpc invokes work by sending an RPC message to the DSP and blocking in
wait_for_completion_interruptible()until the DSP responds. If a signal arrives during this wait, the syscall returns-ERESTARTSYSand the invoke context which holds the in-flight DMA buffers and completion state is left stranded infl->pending.On the next syscall attempt (either auto-restarted by the kernel via
SA_RESTARTor manually retried by user-space after EINTR), a fresh context is allocated and the RPC message is re-sent to the DSP. This has two consequences:- The original context leaks in
fl->pendinguntil the file is closed.
- The DSP receives a duplicate invocation. If the DSP was mid-way through processing the first request and had issued a reverse RPC call back to the host, the retry sends a new forward request instead of the expected reverse-RPC response. The DSP thread waiting for that response is never woken, causing a hang.Fix this by saving the interrupted context to a new
fl->interruptedlist on-ERESTARTSYS. When the same thread retries the invoke with a matching sc, restore the context and jump directly to the wait, skipping context allocation and message re-send.Three additional bugs are also fixed:
1.
fastrpc_context_save_interrupted()did not drop the extra kref thatfastrpc_invoke_send()took, so the worker's put only reached 1 and the context was never freed. Drop the reference insave_interrupted()so the worker's put reaches 0 and triggerscontext_free.2. The bail path skipped context cleanup on
-ETIMEDOUT, leaking the context. Remove the-ETIMEDOUTexception so timed-out contexts are freed like any other error path.3.
fastrpc_context_free()did not remove the context fromfl->interruptedbefore freeing it. A context freed via a racing channel removal left a dangling node in the list. Addlist_del_init()guarded byfl->lockbefore releasing resources.Also drain
fl->interruptedon process exit, complete any sleeping contexts with-EPIPEand scheduleput_wqto drop the worker ref when the rpmsg channel is removed, and adddev_warn()on TIMEOUT and ERESTARTSYS to aid post-mortem debugging.Remove the obsolete
invoke_interrupted_mmapsmechanism fromfastrpc_channel_ctx; context resources are now kept alive through the context refcount rather than by migrating mmaps to a channel-level list.Fixes 387f625585d1 ("misc: fastrpc: handle interrupted contexts")Co-developed-by Srinivas Kandagatla <[email protected]>Signed-off-by Srinivas Kandagatla <[email protected]>Signed-off-by Anandu Krishnan E <[email protected]>---
This patch fixes a context leak and DSP hang that occur when a fastrpc invoke syscall is interrupted by a signal, along with three follow-on bugs found during review.Changes in v2:
- Fix kref imbalance:fastrpc_context_save_interrupted()now drops the extra reference taken byfastrpc_invoke_send(), so the worker's put correctly reaches 0 and triggerscontext_free.
- Fix-ETIMEDOUTcontext leak: remove the ETIMEDOUT exception from the bail path so timed-out kernel invocations go through the samelist_del_init+context_putcleanup as all other error paths.
- Fix dangling node:fastrpc_context_free()now callslist_del_init()underfl->lockbefore releasing resources, preventing a dangling node infl->interruptedif a racing channel removal firesput_wq.
-fastrpc_notify_users()now callsschedule_work(&ctx->put_work)for interrupted contexts to drop the worker ref on channel removal.
- Adddev_warn()on TIMEOUT and ERESTARTSYS for post-mortem debugging.
- Uselist_del_init()instead oflist_del()in the bail path.Link to v1: https://lore.kernel.org/all/[email protected]/
--- drivers/misc/fastrpc.c | 101 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 19 deletions(-)drivers/misc/fastrpc.c +38
-1@@ -280,7 +280,6 @@ struct fastrpc_channel_ctx {struct fastrpc_device *secure_fdevice;struct fastrpc_device *fdevice;struct fastrpc_buf *remote_heap; -struct list_head invoke_interrupted_mmaps;bool secure;bool unsigned_support;u64 dma_mask;@@ -297,6 +296,7 @@ struct fastrpc_user {struct list_head user;struct list_head maps;struct list_head pending; +struct list_head interrupted;struct list_head mmaps;struct fastrpc_channel_ctx *cctx;@@ -522,6 +522,11 @@ static void fastrpc_user_free(struct kref *ref)fastrpc_context_put (ctx); } +list_for_each_entry_safe (ctx, n, &fl->interrupted, node) { +list_del (&ctx->node); +fastrpc_context_put (ctx); + } +list_for_each_entry_safe (map, m, &fl->maps, node)fastrpc_map_put (map);@@ -557,6 +562,12 @@ static void fastrpc_context_free(struct kref *ref) cctx = ctx->cctx; fl = ctx->fl; +/* Remove from fl->interrupted if present; no-op for normal paths. */ +spin_lock (&fl->lock); +if (!list_empty (&ctx->node)) +list_del_init (&ctx->node); +spin_unlock (&fl->lock); +for (i =0 ; i < ctx->nbufs; i++)fastrpc_map_put (ctx->maps[i]);@@ -594,6 +605,42 @@ static void fastrpc_context_put_wq(struct work_struct *work)fastrpc_context_put (ctx); } +static void fastrpc_context_save_interrupted (struct fastrpc_invoke_ctx *ctx) +{ +spin_lock (&ctx->fl->lock); +list_del (&ctx->node); +list_add_tail (&ctx->node, &ctx->fl->interrupted); +spin_unlock (&ctx->fl->lock); +/* +* invoke_send bumped the kref to 2; the bail path skips the put +* for ERESTARTSYS. Drop it here so the worker's put reaches 0 +* and triggers context_free. +*/ +fastrpc_context_put (ctx); +} + +static struct fastrpc_invoke_ctx *fastrpc_context_restore_interrupted ( +struct fastrpc_user *fl,u32 sc) +{ +struct fastrpc_invoke_ctx *ctx =NULL , *ictx, *n; + +spin_lock (&fl->lock); +list_for_each_entry_safe (ictx, n, &fl->interrupted, node) { +if (ictx->pid != current->pid) +continue ; +if (ictx->sc != sc || ictx->fl != fl) { +spin_unlock (&fl->lock); +return ERR_PTR (-EINVAL );
drivers/misc/fastrpc.c static void fastrpc_context_put_wq(struct work_struct *work) · line 643
Quoted 10 lines
drivers/misc/fastrpc.c +10
-0+ } + ctx = ictx; +list_del (&ctx->node); +list_add_tail (&ctx->node, &fl->pending); +break ; + } +spin_unlock (&fl->lock); +return ctx; +} +
fastrpc_context_alloc(): kref_init → ref = 1.fastrpc_invoke_send(): fastrpc_context_get(ctx) before rpmsg_send → ref = 2.
1st -ERESTARTSYS: fastrpc_context_save_interrupted() moves ctx to fl->interrupted and does fastrpc_context_put(ctx) → ref = 1.
Retry: fastrpc_context_restore_interrupted() finds msg and jumps straight to the wait: label — skipping fastrpc_context_alloc() and fastrpc_invoke_send() entirely.
2nd -ERESTARTSYS: fastrpc_context_save_interrupted() runs again on the same ctx and calls fastrpc_context_put(ctx) again → ref = 1 → 0 → fastrpc_context_free() fires.
That frees ctx->buf and ctx->maps (the DMA buffers backing the RPC payload) and removes the ctxid from the idr, while the original rpmsg message is still outstanding and the DSP may still read/write those buffers
Quoted 54 lines
drivers/misc/fastrpc.c +18
-4#define CMP (aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)static int olaps_cmp (const void *a,const void *b) {@@ -1243,8 +1290,6 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, struct fastrpc_invoke_args *args) {struct fastrpc_invoke_ctx *ctx =NULL ; -struct fastrpc_buf *buf, *b; -int err =0 ;if (!fl->sctx)@@ -1258,6 +1303,14 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,return -EPERM ; } +if (!kernel) { + ctx =fastrpc_context_restore_interrupted (fl, sc); +if (IS_ERR (ctx)) +return PTR_ERR (ctx); +if (ctx) + goto wait; + } + ctx =fastrpc_context_alloc (fl, kernel, sc, args);if (IS_ERR (ctx))return PTR_ERR (ctx);@@ -1273,11 +1326,20 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,if (err) goto bail; +wait:if (kernel) { -if (!wait_for_completion_timeout (&ctx->work,10 *HZ )) +if (!wait_for_completion_timeout (&ctx->work,10 *HZ )) { err = -ETIMEDOUT ; +dev_warn (fl->sctx->dev, +"fastrpc_invoke: TIMEOUT ctxid=0x%llx handle=0x%x nscalars=%d\n" , + ctx->ctxid, handle, ctx->nscalars); + } }else { err =wait_for_completion_interruptible (&ctx->work); +if (err == -ERESTARTSYS ) +dev_warn (fl->sctx->dev, +"fastrpc_invoke: INTERRUPTED ctxid=0x%llx handle=0x%x nscalars=%d\n" , + ctx->ctxid, handle, ctx->nscalars); }if (err)@@ -1296,21 +1358,15 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, goto bail; bail: -if (err != -ERESTARTSYS && err != -ETIMEDOUT ) {
drivers/misc/fastrpc.c static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, · line 1368
Quoted 10 lines
drivers/misc/fastrpc.c +4
-2-/* We are done with this compute context */ +if (ctx && err == -ERESTARTSYS ) { +fastrpc_context_save_interrupted (ctx); + }else if (ctx) {spin_lock (&fl->lock); -list_del (&ctx->node); +list_del_init (&ctx->node);spin_unlock (&fl->lock);fastrpc_context_put (ctx); }
fastrpc_context_alloc() takes fastrpc_user_get(user) for every context, released only in fastrpc_context_free(). fastrpc_device_release() removes fl from cctx->users unconditionally before doing anything else, then drops just its own single ref via fastrpc_user_put(fl). If a context is stuck on fl->interrupted at the moment the process exits (some abrupt exit or kill), fl's refcount never reaches zero, fastrpc_user_free() never runs, and because fl is already unlinked from cctx->users, a later SSR-driven fastrpc_notify_users() walk can't find it either.
Quoted 70 lines
drivers/misc/fastrpc.c +12
-13-if (err == -ERESTARTSYS ) { -list_for_each_entry_safe (buf, b, &fl->mmaps, node) { -list_del (&buf->node); -list_add_tail (&buf->node, &fl->cctx->invoke_interrupted_mmaps); - } - } -if (err)dev_dbg (fl->sctx->dev,"Error: Invoke Failed %d\n" , err);@@ -1662,6 +1718,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)spin_lock_init (&fl->lock);mutex_init (&fl->mutex);INIT_LIST_HEAD (&fl->pending); +INIT_LIST_HEAD (&fl->interrupted);INIT_LIST_HEAD (&fl->maps);INIT_LIST_HEAD (&fl->mmaps);INIT_LIST_HEAD (&fl->user);@@ -2460,7 +2517,6 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) rdev->dma_mask = &data->dma_mask;dma_set_mask_and_coherent (rdev,DMA_BIT_MASK (32 ));INIT_LIST_HEAD (&data->users); -INIT_LIST_HEAD (&data->invoke_interrupted_mmaps);spin_lock_init (&data->lock);idr_init (&data->ctx_idr); data->domain_id = domain_id;@@ -2493,13 +2549,23 @@ static void fastrpc_notify_users(struct fastrpc_user *user) ctx->retval = -EPIPE ;complete (&ctx->work); } +/* +* Interrupted contexts hold two refs: one for the invoker and one +* for the async worker from fastrpc_invoke_send. Complete them so +* any blocked retry wakes, and schedule put_wq to drop the worker +* ref -- the invoker ref is released by fastrpc_user_free(). +*/ +list_for_each_entry (ctx, &user->interrupted, node) { + ctx->retval = -EPIPE ; +complete (&ctx->work); +schedule_work (&ctx->put_work); + }spin_unlock (&user->lock); }static void fastrpc_rpmsg_remove (struct rpmsg_device *rpdev) {struct fastrpc_channel_ctx *cctx =dev_get_drvdata (&rpdev->dev); -struct fastrpc_buf *buf, *b;struct fastrpc_user *user;unsigned long flags;@@ -2516,9 +2582,6 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)if (cctx->secure_fdevice)misc_deregister (&cctx->secure_fdevice->miscdev); -list_for_each_entry_safe (buf, b, &cctx->invoke_interrupted_mmaps, node) -list_del (&buf->node); -if (cctx->remote_heap)fastrpc_buf_free (cctx->remote_heap); ---base-commit:
dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260701-master-2ba6246f29d1Best regards,
--
Anandu Krishnan E <[email protected]>
Quoted 6 lines · Anandu Krishnan E
fastrpc invokes work by sending an RPC message to the DSP and blocking in
wait_for_completion_interruptible()until the DSP responds. If a signal arrives during this wait, the syscall returns-ERESTARTSYSand the invoke context which holds the in-flight DMA buffers and completion state is left stranded infl->pending.
Quoted 6 lines
On the next syscall attempt (either auto-restarted by the kernel via
SA_RESTARTor manually retried by user-space after EINTR), a fresh context is allocated and the RPC message is re-sent to the DSP. This has two consequences:[...]
Applied, thanks!
[1/1] misc: fastrpc: fix context leak and hang on signal-interrupted invoke
commit: 3f645f6884b5c753eea253cab37521a432c69105
Best regards,
Signature · 1 line
Srinivas Kandagatla <[email protected]>
Quoted 48 lines · Anandu Krishnan E
fastrpc invokes work by sending an RPC message to the DSP and blocking in
wait_for_completion_interruptible()until the DSP responds. If a signal arrives during this wait, the syscall returns-ERESTARTSYSand the invoke context which holds the in-flight DMA buffers and completion state is left stranded infl->pending.On the next syscall attempt (either auto-restarted by the kernel via
SA_RESTARTor manually retried by user-space after EINTR), a fresh context is allocated and the RPC message is re-sent to the DSP. This has two consequences:- The original context leaks in
fl->pendinguntil the file is closed.
- The DSP receives a duplicate invocation. If the DSP was mid-way through processing the first request and had issued a reverse RPC call back to the host, the retry sends a new forward request instead of the expected reverse-RPC response. The DSP thread waiting for that response is never woken, causing a hang.Fix this by saving the interrupted context to a new
fl->interruptedlist on-ERESTARTSYS. When the same thread retries the invoke with a matching sc, restore the context and jump directly to the wait, skipping context allocation and message re-send.Three additional bugs are also fixed:
1.
fastrpc_context_save_interrupted()did not drop the extra kref thatfastrpc_invoke_send()took, so the worker's put only reached 1 and the context was never freed. Drop the reference insave_interrupted()so the worker's put reaches 0 and triggerscontext_free.2. The bail path skipped context cleanup on
-ETIMEDOUT, leaking the context. Remove the-ETIMEDOUTexception so timed-out contexts are freed like any other error path.3.
fastrpc_context_free()did not remove the context fromfl->interruptedbefore freeing it. A context freed via a racing channel removal left a dangling node in the list. Addlist_del_init()guarded byfl->lockbefore releasing resources.Also drain
fl->interruptedon process exit, complete any sleeping contexts with-EPIPEand scheduleput_wqto drop the worker ref when the rpmsg channel is removed, and adddev_warn()on TIMEOUT and ERESTARTSYS to aid post-mortem debugging.Remove the obsolete
invoke_interrupted_mmapsmechanism fromfastrpc_channel_ctx; context resources are now kept alive through the context refcount rather than by migrating mmaps to a channel-level list.
Quoted 5 lines
Fixes 387f625585d1 ("misc: fastrpc: handle interrupted contexts")Co-developed-by Srinivas Kandagatla <[email protected]>Signed-off-by Srinivas Kandagatla <[email protected]>Signed-off-by Anandu Krishnan E <[email protected]>
This causes a regression on Nord with stopping adsp, and I sent a patch[1] fixing it.
root@qcom-armv8a:~# cat /proc/asound/cards
0 [NORDRIDE ]: nord - NORD-RIDE
NORD-RIDE
root@qcom-armv8a:~# echo stop > /sys/class/remoteproc/remoteproc1/state
[ 69.280356] rcu: INFO: rcu_preempt detected stalls on CPUs/tasks:
[ 69.286634] rcu: 6-...0: (1 GPs behind) idle=6fec/1/0x4000000000000000 softirq=1101/1102 fqs=1051
[ 69.295836] rcu: 16-....: (5256 ticks this GP) idle=6b9c/1/0x4000000000000000 softirq=444/444 fqs=1051
[ 69.305493] rcu: (detected by 9, t=5259 jiffies, g=769, q=3680 ncpus=18)
[ 69.312480] Sending NMI from CPU 9 to CPUs 6:
[ 79.317699] After 10 seconds, these CPUS still haven't responded to the NMI: 6
[ 79.325126] Sending NMI from CPU 9 to CPUs 16:
[ 79.329707] NMI backtrace for cpu 16
[ 79.329718] CPU: 16 UID: 0 PID: 965 Comm: adsprpcd Not tainted 7.4.0-rc4-next-20261127-00140-gff94c7cf1444 #14 PREEMPT(full)
[ 79.329721] Hardware name: Qualcomm Technologies, Inc. SA8797P Ride (DT)
[ 79.329721] pstate: 01400005 (nzcv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
[ 79.329723] pc : queued_spin_lock_slowpath+0x248/0x340
[ 79.329733] lr : _raw_spin_lock+0x5c/0x70
[ 79.329734] sp : ffff80008555bbe0
[ 79.329735] x29: ffff80008555bbe0 x28: ffff800083d35010 x27: ffff0008ada7ef00
[ 79.329737] x26: 0000000000000000 x25: 0000000000000000 x24: ffff0008ada7ef4c
[ 79.329739] x23: 0000000000000000 x22: ffff0008aad45b00 x21: ffff800083d35028
[ 79.329740] x20: ffff0008aad45b74 x19: 00000000ffffffe0 x18: 0000000000000000
[ 79.329742] x17: 0000000000000000 x16: ffffa854539ee710 x15: ffff800083d35200
[ 79.329743] x14: ffff0008811e9080 x13: 0000000000000206 x12: ffff0008811e90a0
[ 79.329745] x11: 00000000000000c0 x10: 074fe19dde1917a1 x9 : ffff0008aad45b76
[ 79.329746] x8 : 0000000000000000 x7 : 0000000000000000 x6 : 0000000000000000
[ 79.329747] x5 : 0000000000440000 x4 : ffff008fa85dbd40 x3 : ffff008fa85dbd40
[ 79.329749] x2 : ffffa854544a9d40 x1 : 0000000000440101 x0 : ffff0008aad45b74
[ 79.329750] Call trace:
[ 79.329753] queued_spin_lock_slowpath+0x248/0x340 (P)
[ 79.329756] fastrpc_internal_invoke+0xa8c/0x13e0 [fastrpc]
[ 79.329764] fastrpc_device_ioctl+0x6e8/0x10c0 [fastrpc]
[ 79.329766] __arm64_sys_ioctl+0xa4/0x100
[ 79.329771] invoke_syscall.constprop.0+0x40/0x100
[ 79.329776] el0_svc_common.constprop.0+0x38/0xe0
[ 79.329777] do_el0_svc+0x1c/0x28
[ 79.329779] el0_svc+0x38/0x1d0
[ 79.329783] el0t_64_sync_handler+0xa0/0xe4
[ 79.329785] el0t_64_sync+0x198/0x19c
[ 79.493951] rcu: rcu_preempt kthread starved for 2542 jiffies! g769 f0x0 RCU_GP_DOING_FQS(6) ->state=R ->cpu=4
[ 79.504231] rcu: Unless rcu_preempt kthread gets sufficient CPU time, OOM is now expected behavior.
[ 79.513615] rcu: RCU grace-period kthread stack dump:
[ 79.518819] task:rcu_preempt state:I stack:0 pid:16 tgid:16 ppid:2 task_flags:0x208040 flags:0x00000010
[ 79.530163] Call trace:
[ 79.532694] __switch_to+0xfc/0x1e0 (T)
[ 79.536656] __schedule+0x3dc/0xe20
[ 79.540250] schedule+0x34/0xdc
[ 79.543489] schedule_timeout+0x80/0x104
[ 79.547540] rcu_gp_fqs_loop+0x10c/0x450
[ 79.551586] rcu_gp_kthread+0x108/0x128
[ 79.555537] kthread+0x10c/0x120
[ 79.558869] ret_from_fork+0x10/0x20
[ 79.562567] rcu: Stack dump where RCU GP kthread last ran:
[ 79.568216] Sending NMI from CPU 9 to CPUs 4:
[ 79.572757] NMI backtrace for cpu 4
[ 79.572770] CPU: 4 UID: 0 PID: 0 Comm: swapper/4 Not tainted 7.4.0-rc4-next-20261127-00140-gff94c7cf1444 #14 PREEMPT(full)
[ 79.572774] Hardware name: Qualcomm Technologies, Inc. SA8797P Ride (DT)
[ 79.572776] pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
[ 79.572780] pc : cpuidle_enter_state+0xb0/0x380
[ 79.572792] lr : cpuidle_enter_state+0xa8/0x380
[ 79.572794] sp : ffff8000801a3d90
[ 79.572795] x29: ffff8000801a3d90 x28: 0000000000000000 x27: 0000000000000000
[ 79.572800] x26: 0000001286e2f8d2 x25: 0000000000000000 x24: 0000000000000000
[ 79.572803] x23: 0000000000000001 x22: ffff000884942080 x21: ffff008fa84a17f8
[ 79.572806] x20: 0000000000000001 x19: 0000001286e7cf05 x18: 0000000000000000
[ 79.572809] x17: 0000000000000005 x16: ffffa854539ee4e0 x15: ffff008fa84a1b34
[ 79.572812] x14: ffff008fa84a1870 x13: 0000000000000000 x12: 0000000000000002
[ 79.572815] x11: ffff008fa84a1b30 x10: 0000000000000001 x9 : 0000000000000001
[ 79.572818] x8 : ffff000884811438 x7 : ffff583b53ffa000 x6 : 0000000017f84251
[ 79.572821] x5 : 00ffffffffffffff x4 : 0000000000000015 x3 : 000000000106cd38
[ 79.572824] x2 : ffff583b53ffa000 x1 : ffff008fa84a2f80 x0 : 0000000000000000
[ 79.572827] Call trace:
[ 79.572829] cpuidle_enter_state+0xb0/0x380 (P)
[ 79.572832] cpuidle_enter+0x34/0x48
[ 79.572841] do_idle+0x148/0x300
[ 79.572847] cpu_startup_entry+0x34/0x40
[ 79.572849] secondary_start_kernel+0x13c/0x15c
[ 79.572857] __secondary_switched+0xc0/0xc4Shawn