All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Anandu Krishnan E" <anandu.e@oss.qualcomm.com>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v4 3/3] misc: fastrpc: fix context leak and hang on signal-interrupted invoke
Date: Tue, 17 Nov 2026 10:05:46 +0000	[thread overview]
Message-ID: <sashiko-outbox-218568@kernel.org> (raw)
In-Reply-To: <20261117-interrupted-invoke-v4-v4-3-2320a8a7eda2@oss.qualcomm.com>

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

      reply	other threads:[~2026-11-17 10:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-218568@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=anandu.e@oss.qualcomm.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is 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.