dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Renê de Souza Pinto" <rene@renesp.com.br>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH] misc: fastrpc: fix fastrpc_user leak on abnormal process exit
Date: Tue, 24 Nov 2026 12:36:50 +0000	[thread overview]
Message-ID: <sashiko-outbox-231156@kernel.org> (raw)
In-Reply-To: <20261124122011.2912030-1-rene@renesp.com.br>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] misc: fastrpc: premature fastrpc_session_free() before
  fastrpc_user_free()
- [Low] misc: fastrpc: dead fl->pending cleanup loop left in
  fastrpc_user_free()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] misc: fastrpc: memory leak of handle maps in
  fastrpc_context_free() [1]
--

commit de1f1f214d7656b3f51116b41f6e566209ceb0a7
Author: Renê de Souza Pinto <rene@renesp.com.br>

misc: fastrpc: fix fastrpc_user leak on abnormal process exit

This commit drains fl->pending in fastrpc_device_release() and drops each
pending invoke context's reference so that the reference cycle between
fastrpc_invoke_ctx and fastrpc_user is broken when a file descriptor is
closed.

> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index 9e760f68bcaf2..05271beaf821c 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
> @@ -1734,7 +1734,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
>  {
>  	struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
>  	struct fastrpc_channel_ctx *cctx = fl->cctx;
> +	struct fastrpc_invoke_ctx *ctx, *n;
>  	unsigned long flags;
> +	LIST_HEAD(orphans);
>  
>  	fastrpc_release_current_dsp_process(fl);
>  
> @@ -1742,6 +1744,20 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
>  	list_del(&fl->user);
>  	spin_unlock_irqrestore(&cctx->lock, flags);
>  
> +	/*
> +	 * Pending (interrupted or timed out) contexts can't be resumed anymore
> +	 * and each holds a reference on the user: release them here to avoid
> +	 * a cycle.
> +	 */
> +	spin_lock(&fl->lock);
> +	list_splice_init(&fl->pending, &orphans);
> +	spin_unlock(&fl->lock);
> +
> +	list_for_each_entry_safe(ctx, n, &orphans, node) {
> +		list_del_init(&ctx->node);
> +		fastrpc_context_put(ctx);
> +	}

[Severity: Low]
Now that fastrpc_device_release() drains fl->pending and every context on
fl->pending holds a reference on fl until fastrpc_context_free(),
fl->pending is always empty when fastrpc_user_free() runs.  Should the
duplicate fl->pending loop in fastrpc_user_free() and the forward
declaration of fastrpc_context_put() be removed?

[Severity: High]
This problem wasn't introduced by this patch, but can
fastrpc_context_free() leak handle maps [1] when an invocation fails
early or is aborted here before fastrpc_put_args() runs?

fastrpc_create_maps() populates ctx->maps up to ctx->nscalars, attaching
handle maps at indices ctx->nbufs through ctx->nscalars - 1 via
fastrpc_map_attach(), but fastrpc_context_free() only releases entries up
to ctx->nbufs:

drivers/misc/fastrpc.c:fastrpc_context_free() {
	...
	for (i = 0; i < ctx->nbufs; i++)
		fastrpc_map_put(ctx->maps[i]);
	...
}

When the invocation never reaches fastrpc_put_args(), those handle map
references and their pinned DMA buffers are never released.

> +
>  	fastrpc_session_free(cctx, fl->sctx);
>  	file->private_data = NULL;
>  	/* Release the reference taken in fastrpc_device_open */

[Severity: Medium]
Can calling fastrpc_session_free() here still hand the session and its
SMMU context bank to a new process before the old process's buffers are
unmapped?

When an orphaned context on fl->pending is still waiting for a DSP
response (or its ctx->put_work work item has not yet run),
fastrpc_context_get() taken in fastrpc_invoke_send() keeps ctx->refcount
above zero until fastrpc_context_put_wq() runs.  Because ctx holds a
reference on fl until fastrpc_context_free(), and commit e85eb5feca8e
("misc: fastrpc: fix use-after-free of fastrpc_user in workqueue context")
moved the teardown of fl->init_mem, fl->maps, and fl->mmaps into
fastrpc_user_free(), marking fl->sctx->used = false in
fastrpc_device_release() allows fastrpc_session_alloc() to reuse the
session while its IOVA space is still occupied by the exiting user's
mappings.  Should fastrpc_session_free() be called in fastrpc_user_free()
after the user's maps and buffers have been freed instead?

[1] https://sashiko.dev/#/bug/linux-9b1d78f7-f355-4424-acce-4fd9aa48eac5

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124122011.2912030-1-rene@renesp.com.br?part=1

           reply	other threads:[~2026-11-24 12:37 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <20261124122011.2912030-1-rene@renesp.com.br>]

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-231156@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=rene@renesp.com.br \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox