* [PATCH v4 0/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke
@ 2026-11-17 9:39 Anandu Krishnan E
2026-11-17 9:39 ` [PATCH v4 1/3] misc: fastrpc: remove dead context drain in fastrpc_user_free() Anandu Krishnan E
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Anandu Krishnan E @ 2026-11-17 9:39 UTC (permalink / raw)
To: Srinivas Kandagatla, Ekansh Gupta, Arnd Bergmann,
Greg Kroah-Hartman, Jorge Ramirez-Ortiz
Cc: Anandu Krishnan E, linux-arm-msm, dri-devel, linux-kernel, stable,
Srinivas Kandagatla, quic_bkumar, quic_chennak
This series fixes a context leak and DSP hang that occur when a
fastrpc invoke syscall is interrupted by a signal (patch 3), plus two
independent, pre-existing bugs the refcounting audit for that fix
surfaced along the way (patches 1 and 2).
Signed-off-by: Anandu Krishnan E <anandu.e@oss.qualcomm.com>
---
Changes in v4:
- Split the single patch into a 3-patch series. Patches 1 and 2 are
standalone, pre-existing bugs the refcounting audit for this series
surfaced - dead code in fastrpc_user_free() and a worker-reference
leak on SSR for ordinary (never-interrupted) pending contexts - each
with its own Fixes: tag, landing ahead of the actual
interrupted-invoke fix in patch 3.
- Fixed a hang inherited from v1-v3: fastrpc_invoke_send() failure
shared the same bail: label as a post-send wait interruption, so a
message that rpmsg_send() itself never delivered to the DSP could
still be parked as resumable, and a retry would then wait forever
for a response that was never going to arrive. fastrpc_invoke_send()
failure now gets its own dedicated cleanup path instead, without
needing an extra flag to tell the two cases apart.
- fastrpc_device_release() now also accounts for a successful
fastrpc_release_current_dsp_process(): if the DSP confirms it is
done with the whole process, the worker reference on any parked
context is dropped immediately instead of always waiting for a
later event.
- fastrpc_user_free() no longer unlinks fl from cctx->users
unconditionally at fd close; it now does so only once fl truly has
no references left, so a context still awaiting a DSP response
after fd close remains reachable by a later SSR.
- Extended the same unreachable-orphan fix to timed-out kernel
invokes (-ETIMEDOUT), which had the identical problem on
fl->pending.
- fastrpc_context_restore_interrupted() now uses kref_get_unless_zero()
when reclaiming a matched context on retry, closing a race where a
concurrent drain (SSR, fd close, or a delayed DSP response) could
already have dropped the context's last reference and be waiting on
the same lock to unlink it.
- Link to v3: https://lore.kernel.org/all/20261002-anandu-fastrpc-interrupted-invoke-v3-v3-1-31e90439d2ba@oss.qualcomm.com/
Changes in v3:
- fastrpc_context_save_interrupted() no longer drops a reference when
parking the context, fixing a kref double-put that left the context
under-refcounted.
- Restoring an interrupted context now matches on both handle and sc,
not sc alone, so a stale/differently-shaped retry cannot be handed a
buffer layout it didn't allocate.
- A mismatched handle/sc on retry now falls through to allocating a
fresh context instead of returning -EINVAL, so a thread issuing a
different call after an interrupted one is never permanently
wedged.
- Added a put_work_scheduled flag, checked and set under fl->lock, to
guard against fastrpc_notify_users() and fastrpc_rpmsg_callback()
both scheduling put_work for the same context.
- fastrpc_device_release() now drains fl->interrupted and drops the
invoker's reference on each parked context before releasing its own
reference on fl, fixing a leak of both fl and the context if the fd
is closed while a context is still parked.
- fastrpc_device_release()'s and fastrpc_user_free()'s drain loops now
use list_del_init() instead of list_del(), avoiding a poisoned-node
list_empty() check in fastrpc_context_free().
- fastrpc_invoke_send() can return -ERESTARTSYS from rpmsg_send()
itself before the message is ever delivered to the DSP. This is now
distinguished with a `sent` flag so only a completion-wait
interruption after a successful send is parked as resumable.
- ctx->args is now repointed at the retry's args on resume instead of
pointing at the original call's already-freed buffers; an
args_owned flag lets fastrpc_context_free() free ctx->args if a
parked context is drained/abandoned without ever being resumed.
- Link to v2: https://lore.kernel.org/all/805d5de8-477c-41e9-b192-d889d1cca9b6@oss.qualcomm.com/
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/20260525124222.3082420-1-anandu.e@oss.qualcomm.com/
---
To: Srinivas Kandagatla <srini@kernel.org>
To: Ekansh Gupta <ekansh.gupta@oss.qualcomm.com>
To: Arnd Bergmann <arnd@arndb.de>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Jorge Ramirez-Ortiz <jorge.ramirez@oss.qualcomm.com>
Cc: linux-arm-msm@vger.kernel.org
Cc: dri-devel@lists.freedesktop.org
Cc: linux-kernel@vger.kernel.org
Cc: stable@kernel.org
Cc: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>
Cc: quic_bkumar@quicinc.com
Cc: quic_chennak@quicinc.com
---
Anandu Krishnan E (3):
misc: fastrpc: remove dead context drain in fastrpc_user_free()
misc: fastrpc: drop the DSP-response reference on channel removal
misc: fastrpc: fix context leak and hang on signal-interrupted invoke
drivers/misc/fastrpc.c | 187 ++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 146 insertions(+), 41 deletions(-)
---
base-commit: 749a4de370409464cf8501ec5b2451df45c84eb4
change-id: 20261117-interrupted-invoke-v4-f2e5be78eba8
Best regards,
--
Anandu Krishnan E <anandu.e@oss.qualcomm.com>
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v4 1/3] misc: fastrpc: remove dead context drain in fastrpc_user_free()
2026-11-17 9:39 [PATCH v4 0/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke Anandu Krishnan E
@ 2026-11-17 9:39 ` Anandu Krishnan E
2026-11-17 9:52 ` sashiko-bot
2026-11-17 15:35 ` Ekansh Gupta
2026-11-17 9:39 ` [PATCH v4 2/3] misc: fastrpc: drop the DSP-response reference on channel removal Anandu Krishnan E
2026-11-17 9:39 ` [PATCH v4 3/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke Anandu Krishnan E
2 siblings, 2 replies; 9+ messages in thread
From: Anandu Krishnan E @ 2026-11-17 9:39 UTC (permalink / raw)
To: Srinivas Kandagatla, Ekansh Gupta, Arnd Bergmann,
Greg Kroah-Hartman, Jorge Ramirez-Ortiz
Cc: Anandu Krishnan E, linux-arm-msm, dri-devel, linux-kernel, stable,
Srinivas Kandagatla, quic_bkumar, quic_chennak
fastrpc_context_alloc() takes a reference on fl via fastrpc_user_get()
for the lifetime of every context, and fastrpc_context_free() always
unlinks the context from fl->pending before releasing that reference
with fastrpc_user_put(). This means fl->refcount cannot reach zero -
and fastrpc_user_free() cannot run - while any context is still linked
on fl->pending: whichever context's release happens to be the one that
drops the last reference has, by construction, already removed itself
from the list a few lines earlier in the very same call.
The list_for_each_entry_safe() loop over fl->pending in
fastrpc_user_free() can therefore never find anything to iterate. It
has been unreachable since fl's own lifetime was switched from
file-close-synchronous teardown to kref-counted teardown; it was never
updated to reflect that the list it walks is now guaranteed empty by
the time this callback can run. Remove it.
Fixes: e85eb5feca8e2 ("misc: fastrpc: fix use-after-free of fastrpc_user in workqueue context")
Signed-off-by: Anandu Krishnan E <anandu.e@oss.qualcomm.com>
---
drivers/misc/fastrpc.c | 6 ------
1 file changed, 6 deletions(-)
diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index 9e760f68bc..bb0c304196 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -568,17 +568,11 @@ 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);
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v4 2/3] misc: fastrpc: drop the DSP-response reference on channel removal
2026-11-17 9:39 [PATCH v4 0/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke Anandu Krishnan E
2026-11-17 9:39 ` [PATCH v4 1/3] misc: fastrpc: remove dead context drain in fastrpc_user_free() Anandu Krishnan E
@ 2026-11-17 9:39 ` Anandu Krishnan E
2026-11-17 9:55 ` sashiko-bot
2026-11-17 15:49 ` Ekansh Gupta
2026-11-17 9:39 ` [PATCH v4 3/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke Anandu Krishnan E
2 siblings, 2 replies; 9+ messages in thread
From: Anandu Krishnan E @ 2026-11-17 9:39 UTC (permalink / raw)
To: Srinivas Kandagatla, Ekansh Gupta, Arnd Bergmann,
Greg Kroah-Hartman, Jorge Ramirez-Ortiz
Cc: Anandu Krishnan E, linux-arm-msm, dri-devel, linux-kernel, stable,
Srinivas Kandagatla, quic_bkumar, quic_chennak
fastrpc_invoke_send() takes a second reference on the invoke context
before calling rpmsg_send(), to be dropped later by whichever side
consumes the eventual DSP response: fastrpc_rpmsg_callback() for a
real reply, via schedule_work(&ctx->put_work).
fastrpc_notify_users(), called from fastrpc_rpmsg_remove() when the
rpmsg channel goes away (e.g. on subsystem restart), only completes
each pending context with -EPIPE so its waiter can return - it never
schedules the matching put. Since the channel is gone, no response
will ever arrive to trigger it through the normal callback path
either. Every context that was genuinely in flight (successfully
sent, not yet answered) at the time of an SSR therefore leaks that
second reference, and with it the context, its DMA maps, and the
reference it holds on fl and cctx.
Schedule the same put_work that a real response would have
triggered, exactly as fastrpc_rpmsg_callback() does. rpmsg guarantees
.callback and .remove are mutually exclusive for a given device, so
there is no concurrent real response that could race with this.
Fixes: c68cfb718c8f ("misc: fastrpc: Add support for context Invoke method")
Cc: stable@kernel.org
Signed-off-by: Anandu Krishnan E <anandu.e@oss.qualcomm.com>
---
drivers/misc/fastrpc.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index bb0c304196..c5c3679f87 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -2835,6 +2835,13 @@ static void fastrpc_notify_users(struct fastrpc_user *user)
list_for_each_entry(ctx, &user->pending, node) {
ctx->retval = -EPIPE;
complete(&ctx->work);
+ /*
+ * fastrpc_invoke_send() took a second reference for the
+ * DSP response that will now never arrive since the
+ * channel is gone; schedule the matching put the same
+ * way a real response would via fastrpc_rpmsg_callback().
+ */
+ schedule_work(&ctx->put_work);
}
spin_unlock(&user->lock);
}
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v4 3/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke
2026-11-17 9:39 [PATCH v4 0/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke Anandu Krishnan E
2026-11-17 9:39 ` [PATCH v4 1/3] misc: fastrpc: remove dead context drain in fastrpc_user_free() Anandu Krishnan E
2026-11-17 9:39 ` [PATCH v4 2/3] misc: fastrpc: drop the DSP-response reference on channel removal Anandu Krishnan E
@ 2026-11-17 9:39 ` Anandu Krishnan E
2026-11-17 10:05 ` sashiko-bot
2 siblings, 1 reply; 9+ messages in thread
From: Anandu Krishnan E @ 2026-11-17 9:39 UTC (permalink / raw)
To: Srinivas Kandagatla, Ekansh Gupta, Arnd Bergmann,
Greg Kroah-Hartman, Jorge Ramirez-Ortiz
Cc: Anandu Krishnan E, linux-arm-msm, dri-devel, linux-kernel, stable,
Srinivas Kandagatla, quic_bkumar, quic_chennak
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, instead of freeing it or leaving it on
fl->pending. The context keeps the same reference it already held;
only list membership changes, so the refcount stays symmetric no
matter how many times a context is interrupted and retried. When the
same thread retries with a matching handle and sc, the context is
pulled back onto fl->pending and the call jumps straight to the wait,
skipping context allocation and message re-send. A mismatched
handle/sc falls through to a normal fresh allocation, so a thread
that issues a different call after an interrupted one is never
wedged.
A context on fl->interrupted can be matched and reclaimed by
fastrpc_context_restore_interrupted() on a retry while a concurrent
kref_put() from an unrelated drain (SSR, fd close, or a delayed DSP
response) has already dropped its refcount to zero and is waiting on
fl->lock to unlink it in fastrpc_context_free(). Without a check, the
retry can win that lock race, hand back a context already committed
to being freed, and use it after the other side frees it.
kref_get_unless_zero() closes the window: it fails if the refcount is
already zero, so the retry falls through to a fresh allocation
instead of resurrecting a dying context.
Distinguish an interruption of the send itself (rpmsg_send() returning
-ERESTARTSYS before the message ever reaches the DSP) from an
interruption of the wait that follows a successful send: only the
latter is safe to park and resume without resending, since nothing is
actually in flight in the former case. Rather than track this with an
extra flag, give the two failure sites their own cleanup instead of
funnelling both into the same bail: label.
ctx->args is now owned by the context (copied at alloc time) and
freed exactly once by fastrpc_context_free(), rather than aliasing the
caller's array, which could otherwise be freed or reused by the time
a parked context is eventually drained.
A context parked on fl->interrupted is reachable from a resumed retry,
process exit and SSR all at once. fastrpc_context_free() now removes
it from whichever list it is on before releasing its resources, using
list_del_init() (plain list_del() poisons the node, which would
otherwise break this same check if the context is freed via a race
with one of the other drains). Because the DSP response worker
(fastrpc_rpmsg_callback()) and the exit/SSR/fd-close paths can all end
up scheduling the same ctx->put_work for a parked context, a
put_work_scheduled flag checked and set under fl->lock ensures only
one of them actually queues it.
fastrpc_device_release() now also drains fl->interrupted, completing
any parked context with -EPIPE and dropping the invoker's reference,
so a context stuck there at fd close cannot leak it. It cannot drop
the worker reference unconditionally, though: the DSP may still be
using that context's buffers if it hasn't answered yet, and freeing
them under it would be unsafe. fastrpc_release_current_dsp_process(),
already called at the top of fastrpc_device_release(), tells the DSP
to tear down the whole process; if that succeeds, the DSP is known to
be done with everything for this process, so the worker reference is
dropped too. If it doesn't succeed, the context is left exactly where
a real response or a future SSR would still find it: still linked on
fl->interrupted, and fl is left on cctx->users (the unlink moves from
fastrpc_device_release() into fastrpc_user_free(), which only runs
once fl truly has no references left) so fastrpc_notify_users() can
still walk down to it later.
The same unreachable-orphan problem exists for a timed-out kernel
invoke (-ETIMEDOUT, only possible when kernel=true): the bail: path
used to unconditionally unlink the context from fl->pending before
dropping its reference, even though the DSP may still answer later.
Leave it linked on fl->pending in that case instead, so
fastrpc_notify_users() can still find and release it if the channel
is torn down before that answer arrives.
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.
Fixes: 387f625585d1 ("misc: fastrpc: handle interrupted contexts")
Cc: stable@kernel.org
Co-developed-by: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>
Signed-off-by: Anandu Krishnan E <anandu.e@oss.qualcomm.com>
---
drivers/misc/fastrpc.c | 188 ++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 146 insertions(+), 42 deletions(-)
diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index c5c3679f87..dd4d3c88e9 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -268,6 +268,7 @@ struct fastrpc_invoke_ctx {
int pid;
int client_id;
u32 sc;
+ u32 handle;
u64 *fdlist;
u32 *crc;
/* Poll memory that DSP updates */
@@ -278,6 +279,8 @@ struct fastrpc_invoke_ctx {
bool is_work_done;
/* process updates poll memory instead of glink response */
bool is_polled;
+ /* set once put_work has been scheduled for this ctx, guarded by fl->lock */
+ bool put_work_scheduled;
struct kref refcount;
struct list_head node; /* list of ctxs */
struct completion work;
@@ -329,7 +332,6 @@ struct fastrpc_channel_ctx {
u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES];
struct fastrpc_device *secure_fdevice;
struct fastrpc_device *fdevice;
- struct list_head invoke_interrupted_mmaps;
bool secure;
bool unsigned_support;
bool poll_mode_supported;
@@ -349,6 +351,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;
@@ -568,8 +571,15 @@ 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_channel_ctx *cctx = fl->cctx;
struct fastrpc_map *map, *m;
struct fastrpc_buf *buf, *b;
+ unsigned long flags;
+
+ /* fl stays on cctx->users until this point, so an SSR before fd close can still find it. */
+ spin_lock_irqsave(&cctx->lock, flags);
+ list_del(&fl->user);
+ spin_unlock_irqrestore(&cctx->lock, flags);
fastrpc_buf_free(fl->init_mem);
@@ -581,7 +591,7 @@ static void fastrpc_user_free(struct kref *ref)
fastrpc_buf_free(buf);
}
- fastrpc_channel_ctx_put(fl->cctx);
+ fastrpc_channel_ctx_put(cctx);
mutex_destroy(&fl->mutex);
kfree(fl);
}
@@ -608,6 +618,12 @@ static void fastrpc_context_free(struct kref *ref)
cctx = ctx->cctx;
fl = ctx->fl;
+ /* list_del_init(), not list_del(): a concurrent drain may still check list_empty(). */
+ 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]);
@@ -619,6 +635,7 @@ static void fastrpc_context_free(struct kref *ref)
kfree(ctx->maps);
kfree(ctx->olaps);
+ kfree(ctx->args);
kfree(ctx);
/* Release the reference taken in fastrpc_context_alloc() */
@@ -644,6 +661,54 @@ static void fastrpc_context_put_wq(struct work_struct *work)
fastrpc_context_put(ctx);
}
+/* Schedules put_work at most once per ctx; racing callers may both try. */
+static void fastrpc_context_schedule_put_work(struct fastrpc_invoke_ctx *ctx)
+{
+ bool do_schedule;
+
+ spin_lock(&ctx->fl->lock);
+ do_schedule = !ctx->put_work_scheduled;
+ ctx->put_work_scheduled = true;
+ spin_unlock(&ctx->fl->lock);
+
+ if (do_schedule)
+ schedule_work(&ctx->put_work);
+}
+
+/* References held by ctx are left intact; only list membership changes. */
+static void fastrpc_context_save_interrupted(struct fastrpc_invoke_ctx *ctx)
+{
+ spin_lock(&ctx->fl->lock);
+ list_move_tail(&ctx->node, &ctx->fl->interrupted);
+ spin_unlock(&ctx->fl->lock);
+}
+
+/* Matches on handle+sc, not sc alone, to avoid resuming an unrelated call. */
+static struct fastrpc_invoke_ctx *fastrpc_context_restore_interrupted(struct fastrpc_user *fl,
+ u32 handle, u32 sc)
+{
+ struct fastrpc_invoke_ctx *ctx = NULL, *ictx;
+
+ spin_lock(&fl->lock);
+ list_for_each_entry(ictx, &fl->interrupted, node) {
+ if (ictx->pid == current->pid && ictx->handle == handle &&
+ ictx->sc == sc) {
+ /* May already be mid-teardown; don't resurrect it. */
+ if (kref_get_unless_zero(&ictx->refcount)) {
+ ctx = ictx;
+ list_move_tail(&ctx->node, &fl->pending);
+ }
+ break;
+ }
+ }
+ spin_unlock(&fl->lock);
+
+ if (ctx)
+ fastrpc_context_put(ctx);
+
+ return ctx;
+}
+
#define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
static int olaps_cmp(const void *a, const void *b)
{
@@ -694,7 +759,7 @@ static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
}
static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
- struct fastrpc_user *user, u32 kernel, u32 sc,
+ struct fastrpc_user *user, u32 kernel, u32 handle, u32 sc,
struct fastrpc_invoke_args *args)
{
struct fastrpc_channel_ctx *cctx = user->cctx;
@@ -724,7 +789,15 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
kfree(ctx);
return ERR_PTR(-ENOMEM);
}
- ctx->args = args;
+ /* Own copy: ctx may outlive the caller's args if parked on fl->interrupted. */
+ ctx->args = kzalloc_objs(*ctx->args, ctx->nscalars);
+ if (!ctx->args) {
+ kfree(ctx->olaps);
+ kfree(ctx->maps);
+ kfree(ctx);
+ return ERR_PTR(-ENOMEM);
+ }
+ memcpy(ctx->args, args, ctx->nscalars * sizeof(*args));
fastrpc_get_buff_overlaps(ctx);
}
@@ -734,6 +807,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
fastrpc_user_get(user);
ctx->sc = sc;
+ ctx->handle = handle;
ctx->retval = -1;
ctx->pid = current->pid;
ctx->client_id = user->client_id;
@@ -768,6 +842,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
fastrpc_channel_ctx_put(cctx);
kfree(ctx->maps);
kfree(ctx->olaps);
+ kfree(ctx->args);
kfree(ctx);
return ERR_PTR(ret);
@@ -1358,8 +1433,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)
@@ -1373,7 +1446,13 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
return -EPERM;
}
- ctx = fastrpc_context_alloc(fl, kernel, sc, args);
+ /* Resume an interrupted invoke instead of resending to the DSP. */
+ if (!kernel)
+ ctx = fastrpc_context_restore_interrupted(fl, handle, sc);
+ if (ctx)
+ goto wait;
+
+ ctx = fastrpc_context_alloc(fl, kernel, handle, sc, args);
if (IS_ERR(ctx))
return PTR_ERR(ctx);
@@ -1385,8 +1464,15 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
dma_wmb();
/* Send invoke buffer to remote dsp */
err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
- if (err)
- goto bail;
+ if (err) {
+ /* Never sent, so not safe to park and resume like bail: does. */
+ spin_lock(&fl->lock);
+ list_del_init(&ctx->node);
+ spin_unlock(&fl->lock);
+ fastrpc_context_put(ctx);
+ dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
+ return err;
+ }
/*
* Set message context as polled if the call is for a user PD
@@ -1395,7 +1481,16 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
if (handle > FASTRPC_MAX_STATIC_HANDLE && fl->pd == USER_PD && fl->poll_mode)
ctx->is_polled = true;
+wait:
err = fastrpc_wait_for_completion(ctx, kernel);
+ if (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 if (err == -ERESTARTSYS)
+ dev_dbg(fl->sctx->dev,
+ "fastrpc_invoke: INTERRUPTED ctxid=0x%llx handle=0x%x nscalars=%d\n",
+ ctx->ctxid, handle, ctx->nscalars);
if (err)
goto bail;
@@ -1412,21 +1507,16 @@ 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 */
- spin_lock(&fl->lock);
- list_del(&ctx->node);
- spin_unlock(&fl->lock);
- fastrpc_context_put(ctx);
- }
-
if (err == -ERESTARTSYS) {
+ fastrpc_context_save_interrupted(ctx);
+ } else if (err == -ETIMEDOUT) {
+ /* Leave linked: a late DSP answer or notify_users() still needs to reach it. */
+ fastrpc_context_put(ctx);
+ } else {
spin_lock(&fl->lock);
- list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
- list_del(&buf->node);
- list_add_tail(&buf->node, &fl->cctx->invoke_interrupted_mmaps);
- }
+ list_del_init(&ctx->node);
spin_unlock(&fl->lock);
+ fastrpc_context_put(ctx);
}
if (err)
@@ -1728,13 +1818,28 @@ 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;
- unsigned long flags;
+ struct fastrpc_invoke_ctx *ctx, *n;
+ int release_err;
- fastrpc_release_current_dsp_process(fl);
+ release_err = fastrpc_release_current_dsp_process(fl);
- spin_lock_irqsave(&cctx->lock, flags);
- list_del(&fl->user);
- spin_unlock_irqrestore(&cctx->lock, flags);
+ /*
+ * Drop the invoker ref unconditionally: no retry can arrive once the
+ * fd is closed. Drop the worker ref only if the DSP confirmed it is
+ * done with the process; otherwise leave ctx linked so a real answer
+ * or a later SSR can still release it.
+ */
+ spin_lock(&fl->lock);
+ list_for_each_entry_safe(ctx, n, &fl->interrupted, node) {
+ ctx->retval = -EPIPE;
+ complete(&ctx->work);
+ spin_unlock(&fl->lock);
+ if (!release_err)
+ fastrpc_context_schedule_put_work(ctx);
+ fastrpc_context_put(ctx);
+ spin_lock(&fl->lock);
+ }
+ spin_unlock(&fl->lock);
fastrpc_session_free(cctx, fl->sctx);
file->private_data = NULL;
@@ -1765,6 +1870,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);
@@ -2784,7 +2890,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;
@@ -2835,13 +2940,16 @@ static void fastrpc_notify_users(struct fastrpc_user *user)
list_for_each_entry(ctx, &user->pending, node) {
ctx->retval = -EPIPE;
complete(&ctx->work);
- /*
- * fastrpc_invoke_send() took a second reference for the
- * DSP response that will now never arrive since the
- * channel is gone; schedule the matching put the same
- * way a real response would via fastrpc_rpmsg_callback().
- */
- schedule_work(&ctx->put_work);
+ fastrpc_context_schedule_put_work(ctx);
+ }
+ /*
+ * Invoker ref for a parked context is released by fastrpc_user_free()
+ * or fastrpc_device_release(); this only drops the worker ref.
+ */
+ list_for_each_entry(ctx, &user->interrupted, node) {
+ ctx->retval = -EPIPE;
+ complete(&ctx->work);
+ fastrpc_context_schedule_put_work(ctx);
}
spin_unlock(&user->lock);
}
@@ -2849,7 +2957,6 @@ static void fastrpc_notify_users(struct fastrpc_user *user)
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;
@@ -2866,9 +2973,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);
-
fastrpc_remote_heap_release(cctx, rpdev);
fastrpc_cb_devices_destroy(rpdev);
@@ -2932,11 +3036,11 @@ static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
complete(&ctx->work);
/*
- * The DMA buffer associated with the context cannot be freed in
- * interrupt context so schedule it through a worker thread to
- * avoid a kernel BUG.
+ * Can't free the DMA buffer in interrupt context; defer to a
+ * worker. Guarded: notify_users()/device_release() may also be
+ * racing to schedule this for the same ctx.
*/
- schedule_work(&ctx->put_work);
+ fastrpc_context_schedule_put_work(ctx);
return 0;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v4 1/3] misc: fastrpc: remove dead context drain in fastrpc_user_free()
2026-11-17 9:39 ` [PATCH v4 1/3] misc: fastrpc: remove dead context drain in fastrpc_user_free() Anandu Krishnan E
@ 2026-11-17 9:52 ` sashiko-bot
2026-11-17 15:35 ` Ekansh Gupta
1 sibling, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 9:52 UTC (permalink / raw)
To: Anandu Krishnan E; +Cc: dri-devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] misc: fastrpc: unused forward declaration of
fastrpc_context_put()
--
commit 8ed1609862d2b846e6b55b9095d296720f5399b3
Author: Anandu Krishnan E <anandu.e@oss.qualcomm.com>
misc: fastrpc: remove dead context drain in fastrpc_user_free()
This commit removes the unreachable loop over fl->pending in
fastrpc_user_free() because each invoke context holds a reference on fl
and unlinks itself from fl->pending before dropping that reference.
> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index 9e760f68bcaf2..bb0c30419611c 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
> @@ -568,17 +568,11 @@ 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);
> - }
[Severity: Low]
Can the forward declaration of fastrpc_context_put() right above
fastrpc_user_free() also be removed now that fastrpc_user_free() no
longer calls it before its definition?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-interrupted-invoke-v4-v4-0-2320a8a7eda2@oss.qualcomm.com?part=1
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v4 2/3] misc: fastrpc: drop the DSP-response reference on channel removal
2026-11-17 9:39 ` [PATCH v4 2/3] misc: fastrpc: drop the DSP-response reference on channel removal Anandu Krishnan E
@ 2026-11-17 9:55 ` sashiko-bot
2026-11-17 15:49 ` Ekansh Gupta
1 sibling, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 9:55 UTC (permalink / raw)
To: Anandu Krishnan E; +Cc: dri-devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] misc: fastrpc: unconditional put_work in fastrpc_notify_users()
causes UAF
--
commit 26d39b5b31352dcf69828a5e0df69c260f35b447
Author: Anandu Krishnan E <anandu.e@oss.qualcomm.com>
misc: fastrpc: drop the DSP-response reference on channel removal
This commit schedules ctx->put_work in fastrpc_notify_users() for each
context on user->pending when the rpmsg channel is removed, so the
reference taken for the DSP response is released.
> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index bb0c30419611c..c5c3679f87f6f 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
> @@ -2835,6 +2835,13 @@ static void fastrpc_notify_users(struct fastrpc_user *user)
> list_for_each_entry(ctx, &user->pending, node) {
> ctx->retval = -EPIPE;
> complete(&ctx->work);
> + /*
> + * fastrpc_invoke_send() took a second reference for the
> + * DSP response that will now never arrive since the
> + * channel is gone; schedule the matching put the same
> + * way a real response would via fastrpc_rpmsg_callback().
> + */
> + schedule_work(&ctx->put_work);
> }
> spin_unlock(&user->lock);
> }
[Severity: High]
Can scheduling ctx->put_work unconditionally for every entry on
user->pending drop the caller's sole reference and cause a use-after-free
on ctx?
fastrpc_context_alloc() adds ctx->node to user->pending before
fastrpc_get_args() and fastrpc_invoke_send() run, and ctx stays on
user->pending until the bail path of fastrpc_internal_invoke() removes it:
CPU0 CPU1
fastrpc_internal_invoke()
ctx = fastrpc_context_alloc()
list_add_tail(&ctx->node, &user->pending)
kref_init(&ctx->refcount)
fastrpc_get_args(kernel, ctx)
fastrpc_rpmsg_remove()
fastrpc_notify_users()
schedule_work(&ctx->put_work)
fastrpc_context_put_wq()
fastrpc_context_free()
kfree(ctx)
fastrpc_invoke_send(cctx, ...) <-- use-after-free
...
fastrpc_context_put(ctx)
The same mismatch occurs if rpmsg_send() in fastrpc_invoke_send() fails and
immediately drops the second reference while ctx is still on user->pending.
In addition, fastrpc_rpmsg_callback() schedules ctx->put_work without
removing ctx from user->pending. If fastrpc_rpmsg_remove() runs after
fastrpc_rpmsg_callback() while the caller is still in fastrpc_put_args()
(or after an earlier -ERESTARTSYS / -ETIMEDOUT bail left ctx on
user->pending), fastrpc_notify_users() schedules ctx->put_work a second
time, freeing ctx while it is still in use or still linked on
user->pending. A later patch in the series, commit "misc: fastrpc: fix
context leak and hang on signal-interrupted invoke", addresses the
post-callback cases, but the pre-send and failed-send windows remain.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-interrupted-invoke-v4-v4-0-2320a8a7eda2@oss.qualcomm.com?part=2
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v4 3/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke
2026-11-17 9:39 ` [PATCH v4 3/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke Anandu Krishnan E
@ 2026-11-17 10:05 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 10:05 UTC (permalink / raw)
To: Anandu Krishnan E; +Cc: dri-devel
Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] misc: fastrpc: recursive fl->lock deadlock in
fastrpc_notify_users()
- [High] misc: fastrpc: hardirq-to-process fl->lock inversion in rpmsg
callback
- [High] misc: fastrpc: use-after-free of ctx in
fastrpc_rpmsg_callback()
- [High] misc: fastrpc: stale ctx->args reuse in
fastrpc_context_restore_interrupted()
- [High] misc: fastrpc: DSP retval -ERESTARTSYS mishandled as signal in
bail path
- [High] misc: fastrpc: premature ctx put in fastrpc_notify_users()
before send
- [Medium] misc: fastrpc: leaked timed-out contexts and unreclaimed SSR
contexts
- [Medium] misc: fastrpc: ctx_idr exhaustion blocks
fastrpc_device_release()
--
commit 13c73e8b92c7260a0858f27cc1ca5b1e031ffdf3
Author: Anandu Krishnan E <anandu.e@oss.qualcomm.com>
misc: fastrpc: fix context leak and hang on signal-interrupted invoke
This commit parks signal-interrupted FastRPC invoke contexts on a per-user
fl->interrupted list so they can be resumed on retry without resending the
RPC message to the DSP, and updates teardown paths to drain parked
contexts.
> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index c5c3679f87f6f..dd4d3c88e9b97 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
[ ... ]
> @@ -644,6 +661,54 @@ static void fastrpc_context_put_wq(struct work_struct *work)
> fastrpc_context_put(ctx);
> }
>
> +/* Schedules put_work at most once per ctx; racing callers may both try. */
> +static void fastrpc_context_schedule_put_work(struct fastrpc_invoke_ctx *ctx)
> +{
> + bool do_schedule;
> +
> + spin_lock(&ctx->fl->lock);
> + do_schedule = !ctx->put_work_scheduled;
> + ctx->put_work_scheduled = true;
> + spin_unlock(&ctx->fl->lock);
> +
> + if (do_schedule)
> + schedule_work(&ctx->put_work);
> +}
[Severity: High]
Can calling fastrpc_context_schedule_put_work() from
fastrpc_rpmsg_callback() deadlock on fl->lock?
On Qualcomm GLINK and SMD transports, the endpoint callback
fastrpc_rpmsg_callback() runs in hardirq context, while process and
workqueue paths acquire fl->lock with plain spin_lock() and local
interrupts enabled:
fastrpc_context_free()
spin_lock(&fl->lock)
<hardirq>
fastrpc_rpmsg_callback()
fastrpc_context_schedule_put_work()
spin_lock(&ctx->fl->lock) <-- spins forever
> +
> +/* References held by ctx are left intact; only list membership changes. */
> +static void fastrpc_context_save_interrupted(struct fastrpc_invoke_ctx *ctx)
> +{
> + spin_lock(&ctx->fl->lock);
> + list_move_tail(&ctx->node, &ctx->fl->interrupted);
> + spin_unlock(&ctx->fl->lock);
> +}
> +
> +/* Matches on handle+sc, not sc alone, to avoid resuming an unrelated call. */
> +static struct fastrpc_invoke_ctx *fastrpc_context_restore_interrupted(struct fastrpc_user *fl,
> + u32 handle, u32 sc)
> +{
> + struct fastrpc_invoke_ctx *ctx = NULL, *ictx;
> +
> + spin_lock(&fl->lock);
> + list_for_each_entry(ictx, &fl->interrupted, node) {
> + if (ictx->pid == current->pid && ictx->handle == handle &&
> + ictx->sc == sc) {
> + /* May already be mid-teardown; don't resurrect it. */
> + if (kref_get_unless_zero(&ictx->refcount)) {
> + ctx = ictx;
> + list_move_tail(&ctx->node, &fl->pending);
> + }
> + break;
> + }
> + }
> + spin_unlock(&fl->lock);
> +
> + if (ctx)
> + fastrpc_context_put(ctx);
> +
> + return ctx;
> +}
[Severity: High]
What happens if a signal without SA_RESTART interrupts an invoke (returning
-EINTR to user space) and the thread later calls the same (handle, sc)
method with different buffers?
fastrpc_context_restore_interrupted() matches only on pid, handle, and sc,
and fastrpc_internal_invoke() jumps straight to wait: without checking or
updating ctx->args. The new input buffers are ignored, and when the
original DSP call finishes, fastrpc_put_args() copies output buffers via
copy_to_user() to the stale ctx->args[i].ptr addresses from the earlier
interrupted call, corrupting user-space stack or heap memory. In addition,
if user space does not retry after -EINTR, the parked context and its DMA
buffers and ctx_idr slot remain pinned on fl->interrupted until the file
descriptor is closed.
[ ... ]
> @@ -1373,7 +1446,13 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
> return -EPERM;
> }
>
> - ctx = fastrpc_context_alloc(fl, kernel, sc, args);
> + /* Resume an interrupted invoke instead of resending to the DSP. */
> + if (!kernel)
> + ctx = fastrpc_context_restore_interrupted(fl, handle, sc);
> + if (ctx)
> + goto wait;
> +
> + ctx = fastrpc_context_alloc(fl, kernel, handle, sc, args);
> if (IS_ERR(ctx))
> return PTR_ERR(ctx);
>
> @@ -1385,8 +1464,15 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
> dma_wmb();
> /* Send invoke buffer to remote dsp */
> err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
> - if (err)
> - goto bail;
> + if (err) {
> + /* Never sent, so not safe to park and resume like bail: does. */
> + spin_lock(&fl->lock);
> + list_del_init(&ctx->node);
> + spin_unlock(&fl->lock);
> + fastrpc_context_put(ctx);
> + dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
> + return err;
> + }
>
> /*
> * Set message context as polled if the call is for a user PD
> @@ -1395,7 +1481,16 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
> if (handle > FASTRPC_MAX_STATIC_HANDLE && fl->pd == USER_PD && fl->poll_mode)
> ctx->is_polled = true;
>
> +wait:
> err = fastrpc_wait_for_completion(ctx, kernel);
> + if (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 if (err == -ERESTARTSYS)
> + dev_dbg(fl->sctx->dev,
> + "fastrpc_invoke: INTERRUPTED ctxid=0x%llx handle=0x%x nscalars=%d\n",
> + ctx->ctxid, handle, ctx->nscalars);
> if (err)
> goto bail;
>
> @@ -1412,21 +1507,16 @@ 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 */
> - spin_lock(&fl->lock);
> - list_del(&ctx->node);
> - spin_unlock(&fl->lock);
> - fastrpc_context_put(ctx);
> - }
> -
> if (err == -ERESTARTSYS) {
> + fastrpc_context_save_interrupted(ctx);
[Severity: High]
What happens if the DSP completes an invocation with ctx->retval == -512
(-ERESTARTSYS)?
After fastrpc_wait_for_completion() and fastrpc_put_args() have already
completed, err = ctx->retval jumps to bail:, which treats -ERESTARTSYS as a
signal interruption and parks the completed context on fl->interrupted
without dropping the invoker reference. When the syscall restarts and
reclaims ctx, a non-polled call hangs waiting on the already-consumed
ctx->work completion, while a polled call with ctx->is_work_done == true
repeatedly re-runs fastrpc_put_args() and drops map references in
ctx->fdlist again, underflowing mmap->refcount into a use-after-free:
drivers/misc/fastrpc.c:fastrpc_put_args() {
...
/* Clean up fdlist which is updated by DSP */
for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
if (!fdlist[i])
break;
if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false))
fastrpc_map_put(mmap);
}
...
}
> + } else if (err == -ETIMEDOUT) {
> + /* Leave linked: a late DSP answer or notify_users() still needs to reach it. */
> + fastrpc_context_put(ctx);
> + } else {
> spin_lock(&fl->lock);
> - list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
> - list_del(&buf->node);
> - list_add_tail(&buf->node, &fl->cctx->invoke_interrupted_mmaps);
> - }
> + list_del_init(&ctx->node);
> spin_unlock(&fl->lock);
> + fastrpc_context_put(ctx);
> }
>
> if (err)
> @@ -1728,13 +1818,28 @@ 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;
> - unsigned long flags;
> + struct fastrpc_invoke_ctx *ctx, *n;
> + int release_err;
>
> - fastrpc_release_current_dsp_process(fl);
> + release_err = fastrpc_release_current_dsp_process(fl);
>
> - spin_lock_irqsave(&cctx->lock, flags);
> - list_del(&fl->user);
> - spin_unlock_irqrestore(&cctx->lock, flags);
> + /*
> + * Drop the invoker ref unconditionally: no retry can arrive once the
> + * fd is closed. Drop the worker ref only if the DSP confirmed it is
> + * done with the process; otherwise leave ctx linked so a real answer
> + * or a later SSR can still release it.
> + */
> + spin_lock(&fl->lock);
> + list_for_each_entry_safe(ctx, n, &fl->interrupted, node) {
> + ctx->retval = -EPIPE;
> + complete(&ctx->work);
> + spin_unlock(&fl->lock);
> + if (!release_err)
> + fastrpc_context_schedule_put_work(ctx);
> + fastrpc_context_put(ctx);
> + spin_lock(&fl->lock);
> + }
> + spin_unlock(&fl->lock);
[Severity: Medium]
Does fastrpc_device_release() leak timed-out contexts that were left on
fl->pending?
When a kernel invoke times out (-ETIMEDOUT), bail: drops only the invoker
reference and leaves ctx linked on fl->pending holding its worker reference
and its reference on fl. If the file descriptor is then closed and
fastrpc_release_current_dsp_process() succeeds, the DSP process is torn
down so no late reply will arrive, but fastrpc_device_release() only drains
fl->interrupted and never drops the worker reference on contexts in
fl->pending, leaking ctx and fl until the rpmsg channel is removed.
Similarly, if an interrupted invoke is retried after SSR sets cctx->rpdev
to NULL, fastrpc_internal_invoke() returns -EPIPE before calling
fastrpc_context_restore_interrupted(), leaving the parked context
unreclaimed until fd close.
[Severity: Medium]
Can parked contexts on fl->interrupted exhaust cctx->ctx_idr and prevent
fastrpc_release_current_dsp_process() from running?
Each parked context holds one of the 255 slots in cctx->ctx_idr until
fastrpc_context_free() runs, even after the DSP has already answered and
dropped the worker reference. If 255 interrupted contexts accumulate
across the channel before fd close, fastrpc_release_current_dsp_process()
runs before fl->interrupted is drained and fails in fastrpc_context_alloc()
with -ENOSPC, so the DSP is never told to release the process and
fastrpc_device_release() skips scheduling put_work for any unanswered
contexts on fl->interrupted.
[ ... ]
> @@ -2835,13 +2940,16 @@ static void fastrpc_notify_users(struct fastrpc_user *user)
> list_for_each_entry(ctx, &user->pending, node) {
> ctx->retval = -EPIPE;
> complete(&ctx->work);
> - /*
> - * fastrpc_invoke_send() took a second reference for the
> - * DSP response that will now never arrive since the
> - * channel is gone; schedule the matching put the same
> - * way a real response would via fastrpc_rpmsg_callback().
> - */
> - schedule_work(&ctx->put_work);
> + fastrpc_context_schedule_put_work(ctx);
> + }
> + /*
> + * Invoker ref for a parked context is released by fastrpc_user_free()
> + * or fastrpc_device_release(); this only drops the worker ref.
> + */
> + list_for_each_entry(ctx, &user->interrupted, node) {
> + ctx->retval = -EPIPE;
> + complete(&ctx->work);
> + fastrpc_context_schedule_put_work(ctx);
> }
> spin_unlock(&user->lock);
> }
[Severity: High]
Does fastrpc_notify_users() deadlock on user->lock when any context is on
user->pending or user->interrupted?
fastrpc_notify_users() holds spin_lock(&user->lock) while calling
fastrpc_context_schedule_put_work(ctx), which immediately calls
spin_lock(&ctx->fl->lock) on the same spinlock (ctx->fl == user).
[Severity: High]
Can fastrpc_notify_users() drop a worker reference that was never taken for
a context on user->pending?
fastrpc_context_alloc() links ctx onto user->pending with refcount == 1
before fastrpc_get_args() and fastrpc_invoke_send() run, and
fastrpc_invoke_send() only keeps the second reference if rpmsg_send()
succeeds:
CPU0 CPU1
fastrpc_internal_invoke()
fastrpc_context_alloc()
list_add_tail(..., &user->pending)
kref_init(&ctx->refcount)
fastrpc_get_args()
fastrpc_rpmsg_remove()
fastrpc_notify_users()
fastrpc_context_schedule_put_work(ctx)
fastrpc_context_put_wq()
fastrpc_context_put(ctx)
fastrpc_context_free(ctx)
fastrpc_invoke_send() <-- use-after-free
Because ctx is on user->pending before a successful rpmsg_send(),
put_work drops the invoker's sole reference and frees ctx while
fastrpc_internal_invoke() is still using it.
[ ... ]
> @@ -2932,11 +3036,11 @@ static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
> complete(&ctx->work);
>
> /*
> - * The DMA buffer associated with the context cannot be freed in
> - * interrupt context so schedule it through a worker thread to
> - * avoid a kernel BUG.
> + * Can't free the DMA buffer in interrupt context; defer to a
> + * worker. Guarded: notify_users()/device_release() may also be
> + * racing to schedule this for the same ctx.
> */
> - schedule_work(&ctx->put_work);
> + fastrpc_context_schedule_put_work(ctx);
>
> return 0;
> }
[Severity: High]
Can fastrpc_rpmsg_callback() dereference ctx and ctx->fl after they have
been freed by a concurrent drain?
fastrpc_rpmsg_callback() looks up ctx with idr_find(&cctx->ctx_idr, ctxid)
and drops cctx->lock without taking a reference on ctx, while
fastrpc_device_release() can now drop both the worker and invoker
references on an interrupted context (and fastrpc_context_free() leaves ctx
in cctx->ctx_idr while freeing its maps and DMA buffer):
CPU0 CPU1
fastrpc_rpmsg_callback()
spin_lock_irqsave(&cctx->lock)
ctx = idr_find(&cctx->ctx_idr, ...)
spin_unlock_irqrestore(&cctx->lock)
fastrpc_device_release()
fastrpc_context_schedule_put_work(ctx)
fastrpc_context_put(ctx)
fastrpc_context_put_wq()
fastrpc_context_free()
kfree(ctx)
fastrpc_user_put(fl)
kfree(fl)
ctx->retval = rsp->retval; <-- use-after-free
complete(&ctx->work);
fastrpc_context_schedule_put_work(ctx)
spin_lock(&ctx->fl->lock); <-- use-after-free
Even for a context on fl->pending whose put_work already ran from a
teardown path, calling complete(&ctx->work) lets the woken waiter drop the
last reference and free ctx and fl before fastrpc_rpmsg_callback() calls
fastrpc_context_schedule_put_work(ctx).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-interrupted-invoke-v4-v4-0-2320a8a7eda2@oss.qualcomm.com?part=3
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v4 1/3] misc: fastrpc: remove dead context drain in fastrpc_user_free()
2026-11-17 9:39 ` [PATCH v4 1/3] misc: fastrpc: remove dead context drain in fastrpc_user_free() Anandu Krishnan E
2026-11-17 9:52 ` sashiko-bot
@ 2026-11-17 15:35 ` Ekansh Gupta
1 sibling, 0 replies; 9+ messages in thread
From: Ekansh Gupta @ 2026-11-17 15:35 UTC (permalink / raw)
To: Anandu Krishnan E, Srinivas Kandagatla, Arnd Bergmann,
Greg Kroah-Hartman, Jorge Ramirez-Ortiz
Cc: linux-arm-msm, dri-devel, linux-kernel, stable,
Srinivas Kandagatla, quic_bkumar, quic_chennak
On 17-11-2026 15:09, Anandu Krishnan E wrote:
> fastrpc_context_alloc() takes a reference on fl via fastrpc_user_get()
> for the lifetime of every context, and fastrpc_context_free() always
> unlinks the context from fl->pending before releasing that reference
does it? I don't see this in the driver code
> with fastrpc_user_put(). This means fl->refcount cannot reach zero -
> and fastrpc_user_free() cannot run - while any context is still linked
> on fl->pending: whichever context's release happens to be the one that
> drops the last reference has, by construction, already removed itself
> from the list a few lines earlier in the very same call.
>
> The list_for_each_entry_safe() loop over fl->pending in
> fastrpc_user_free() can therefore never find anything to iterate. It
> has been unreachable since fl's own lifetime was switched from
> file-close-synchronous teardown to kref-counted teardown; it was never
> updated to reflect that the list it walks is now guaranteed empty by
> the time this callback can run. Remove it.
>
> Fixes: e85eb5feca8e2 ("misc: fastrpc: fix use-after-free of fastrpc_user in workqueue context")
> Signed-off-by: Anandu Krishnan E <anandu.e@oss.qualcomm.com>
> ---
> drivers/misc/fastrpc.c | 6 ------
> 1 file changed, 6 deletions(-)
>
> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index 9e760f68bc..bb0c304196 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
> @@ -568,17 +568,11 @@ 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);
> - }
> -
you should also remove the forward declaration of fastrpc_context_put().
> list_for_each_entry_safe(map, m, &fl->maps, node)
> fastrpc_map_put(map);
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v4 2/3] misc: fastrpc: drop the DSP-response reference on channel removal
2026-11-17 9:39 ` [PATCH v4 2/3] misc: fastrpc: drop the DSP-response reference on channel removal Anandu Krishnan E
2026-11-17 9:55 ` sashiko-bot
@ 2026-11-17 15:49 ` Ekansh Gupta
1 sibling, 0 replies; 9+ messages in thread
From: Ekansh Gupta @ 2026-11-17 15:49 UTC (permalink / raw)
To: Anandu Krishnan E, Srinivas Kandagatla, Arnd Bergmann,
Greg Kroah-Hartman, Jorge Ramirez-Ortiz
Cc: linux-arm-msm, dri-devel, linux-kernel, stable,
Srinivas Kandagatla, quic_bkumar, quic_chennak
On 17-11-2026 15:09, Anandu Krishnan E wrote:
> fastrpc_invoke_send() takes a second reference on the invoke context
> before calling rpmsg_send(), to be dropped later by whichever side
> consumes the eventual DSP response: fastrpc_rpmsg_callback() for a
> real reply, via schedule_work(&ctx->put_work).
>
> fastrpc_notify_users(), called from fastrpc_rpmsg_remove() when the
> rpmsg channel goes away (e.g. on subsystem restart), only completes
> each pending context with -EPIPE so its waiter can return - it never
> schedules the matching put. Since the channel is gone, no response
> will ever arrive to trigger it through the normal callback path
> either. Every context that was genuinely in flight (successfully
> sent, not yet answered) at the time of an SSR therefore leaks that
> second reference, and with it the context, its DMA maps, and the
> reference it holds on fl and cctx.
>
> Schedule the same put_work that a real response would have
> triggered, exactly as fastrpc_rpmsg_callback() does. rpmsg guarantees
> .callback and .remove are mutually exclusive for a given device, so
> there is no concurrent real response that could race with this.
>
> Fixes: c68cfb718c8f ("misc: fastrpc: Add support for context Invoke method")
> Cc: stable@kernel.org
> Signed-off-by: Anandu Krishnan E <anandu.e@oss.qualcomm.com>
> ---
> drivers/misc/fastrpc.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index bb0c304196..c5c3679f87 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
> @@ -2835,6 +2835,13 @@ static void fastrpc_notify_users(struct fastrpc_user *user)
> list_for_each_entry(ctx, &user->pending, node) {
> ctx->retval = -EPIPE;
> complete(&ctx->work);
> + /*
> + * fastrpc_invoke_send() took a second reference for the
> + * DSP response that will now never arrive since the
> + * channel is gone; schedule the matching put the same
> + * way a real response would via fastrpc_rpmsg_callback().
> + */
> + schedule_work(&ctx->put_work);
Can you check the sashiko comment?
https://lore.kernel.org/all/sashiko-outbox-218551@kernel.org/
> }
> spin_unlock(&user->lock);
> }
>
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-11-17 15:49 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 9:39 [PATCH v4 0/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke Anandu Krishnan E
2026-11-17 9:39 ` [PATCH v4 1/3] misc: fastrpc: remove dead context drain in fastrpc_user_free() Anandu Krishnan E
2026-11-17 9:52 ` sashiko-bot
2026-11-17 15:35 ` Ekansh Gupta
2026-11-17 9:39 ` [PATCH v4 2/3] misc: fastrpc: drop the DSP-response reference on channel removal Anandu Krishnan E
2026-11-17 9:55 ` sashiko-bot
2026-11-17 15:49 ` Ekansh Gupta
2026-11-17 9:39 ` [PATCH v4 3/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke Anandu Krishnan E
2026-11-17 10:05 ` sashiko-bot
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.