Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] misc: fastrpc: NULL deref and teardown race in
  fastrpc_cap_query_work()
- [High] misc: fastrpc: leak of ctx, DMA buf, fl, and cctx in
  fastrpc_cap_query_work()
- [Medium] misc: fastrpc: race duplicating ADSP sessions after
  misc_register()
- [Medium] misc: fastrpc: misuse of WQ_MEM_RECLAIM on per-channel cap_wq
  in probe()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] misc: fastrpc: NULL pointer dereference in
  fastrpc_remote_heap_alloc() [1]
--

commit 5d26fca6a35553f861e698fe1de404c633098aee
Author: Anandu Krishnan E <[email protected]>

misc: fastrpc: query the dsp capabilities during probe time

This commit moves the DSP capability query to an asynchronous per-channel
workqueue at probe time and registers the FastRPC device nodes once the
query completes.

> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index c515097c3d3ba..59ef755feeaf0 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
[ ... ]
> @@ -2704,6 +2708,159 @@ static int fastrpc_init_reserved_mem(struct 
> fastrpc_channel_ctx *cctx,
>                                 cctx->vmperms, cctx->vmcount);
>  }
>  
> +static int fastrpc_register_devices(struct fastrpc_channel_ctx *cctx,
> +                                 struct rpmsg_device *rpdev)
> +{
> +     struct device *rdev = &rpdev->dev;
> +     int err, i;
> +
> +     switch (cctx->domain_id) {
> +     case ADSP_DOMAIN_ID:
> +     case MDSP_DOMAIN_ID:
> +     case SDSP_DOMAIN_ID:
> +     case HPASS_DOMAIN_ID:
> +             cctx->unsigned_support = false;
> +             err = fastrpc_device_register(rdev, cctx, cctx->secure, 
> cctx->domain);
> +             if (err)
> +                     goto err_depopulate;
> +             break;
[ ... ]
> +     /* Duplicate the last session so multiple processes can share
> +      * the same compute bank.
> +      */
> +     if (cctx->domain_id == ADSP_DOMAIN_ID && cctx->sesscount > 0) {
> +             struct fastrpc_session_ctx *last_sess;
> +             struct fastrpc_session_ctx *dup_sess;
> +             unsigned long flags;
> +
> +             spin_lock_irqsave(&cctx->lock, flags);
> +             last_sess = &cctx->session[cctx->sesscount - 1];
> +             for (i = 0; i < FASTRPC_DUP_SESSIONS; i++) {
> +                     if (cctx->sesscount >= FASTRPC_MAX_SESSIONS)
> +                             break;
> +                     dup_sess = &cctx->session[cctx->sesscount++];
> +                     memcpy(dup_sess, last_sess, sizeof(*dup_sess));
> +             }
> +             spin_unlock_irqrestore(&cctx->lock, flags);
> +     }

[Severity: Medium]
Can an open() right after fastrpc_device_register() cause memcpy() to mark
all duplicated ADSP sessions as used?

Before this patch, fastrpc_device_register() ran before
fastrpc_cb_devices_create(), so cctx->sesscount was 0 when the miscdevice
appeared.  Now cctx->sesscount is already populated when
fastrpc_device_register() exposes the device node:

CPU0 (kworker)                    CPU1 (userspace open)
fastrpc_register_devices()
  fastrpc_device_register()
    misc_register()
                                  fastrpc_device_open()
                                    fastrpc_session_alloc()
                                      session[sesscount - 1].used = true
  spin_lock_irqsave(&cctx->lock)
  last_sess = &cctx->session[cctx->sesscount - 1]
  memcpy(dup_sess, last_sess, ...)
    /* copies used = true into all 4 dup_sess slots */

Those duplicated session slots have no owning fastrpc_user and remain
unallocatable for the lifetime of the channel.  Duplicating the ADSP
sessions before calling fastrpc_device_register() avoids exposing the
partially initialized session table.

[ ... ]

> +static void fastrpc_cap_query_work(struct work_struct *work)
> +{
> +     struct fastrpc_channel_ctx *cctx =
> +             container_of(work, struct fastrpc_channel_ctx, cap_query_work);
> +     struct rpmsg_device *rpdev = cctx->rpdev;
> +     struct device *rdev = &rpdev->dev;
> +     u32 *dsp_attributes;
> +     struct fastrpc_user *fl;
> +     unsigned long flags;
> +     int err;
> +
> +     fl = kzalloc_obj(*fl);
> +     if (!fl)
> +             goto register_devices;
> +
> +     spin_lock_init(&fl->lock);
> +     mutex_init(&fl->mutex);
> +     INIT_LIST_HEAD(&fl->pending);
> +     INIT_LIST_HEAD(&fl->maps);
> +     INIT_LIST_HEAD(&fl->mmaps);
> +     INIT_LIST_HEAD(&fl->user);
> +     fl->cctx = cctx;
> +     fl->pd = ROOT_PD;
> +     kref_init(&fl->refcount);
> +     fastrpc_channel_ctx_get(cctx);
> +
> +     fl->sctx = fastrpc_session_alloc(fl);
> +     if (!fl->sctx) {
> +             dev_warn(rdev, "no session for capability query, skipping\n");
> +             fastrpc_user_put(fl);
> +             goto register_devices;
> +     }
> +
> +     dsp_attributes = kzalloc_objs(*dsp_attributes, 
> FASTRPC_MAX_DSP_ATTRIBUTES);
> +     if (!dsp_attributes)
> +             goto err_cleanup_register;
> +
> +     /* Linked into cctx->users so fastrpc_rpmsg_remove() can abort us
> +      * via fastrpc_notify_users() instead of waiting out the timeout.
> +      */
> +     spin_lock_irqsave(&cctx->lock, flags);
> +     if (!cctx->rpdev) {
> +             spin_unlock_irqrestore(&cctx->lock, flags);
> +             goto err_cleanup_noreg;
> +     }

[Severity: High]
This problem wasn't introduced by this patch, but fastrpc_rpmsg_remove()
clears cctx->rpdev without waiting for in-flight invocations [1],
which can cause a NULL pointer dereference in fastrpc_remote_heap_alloc().

fastrpc_internal_invoke() checks fl->cctx->rpdev locklessly and does not
hold the lock across argument allocation:

CPU0                              CPU1
fastrpc_internal_invoke()
  if (!fl->cctx->rpdev)
                                  fastrpc_rpmsg_remove()
                                    spin_lock_irqsave(&cctx->lock, ...)
                                    cctx->rpdev = NULL
                                    spin_unlock_irqrestore(&cctx->lock, ...)
  fastrpc_get_args()
    fastrpc_remote_heap_alloc()
      rdev = &fl->cctx->rpdev->dev
      __fastrpc_buf_alloc(..., rdev, ...)
        <-- NULL pointer dereference

> +     list_add_tail(&fl->user, &cctx->users);
> +     spin_unlock_irqrestore(&cctx->lock, flags);
> +
> +     err = fastrpc_get_info_from_dsp(fl, dsp_attributes, 
> FASTRPC_MAX_DSP_ATTRIBUTES);
> +
> +     spin_lock_irqsave(&cctx->lock, flags);
> +     list_del(&fl->user);
> +     spin_unlock_irqrestore(&cctx->lock, flags);
> +
> +     if (err == -EPIPE) {
> +             /* Channel is going away; remove() will tear it down. */
> +             goto err_cleanup_noreg;
> +     }

[Severity: High]
Can a concurrent fastrpc_rpmsg_remove() trigger a NULL pointer dereference
or register device nodes on a removed channel in fastrpc_cap_query_work()?

fastrpc_rpmsg_remove() sets cctx->rpdev = NULL and calls
fastrpc_notify_users() once before waiting in
cancel_work_sync(&cctx->cap_query_work).  At the top of
fastrpc_cap_query_work(), rpdev = cctx->rpdev is read without holding
cctx->lock or checking for NULL:

CPU0                              CPU1 (kworker)
fastrpc_rpmsg_remove()
  spin_lock_irqsave(&cctx->lock)
  cctx->rpdev = NULL
  spin_unlock_irqrestore(&cctx->lock)
  cancel_work_sync(&cctx->cap_query_work)
                                  fastrpc_cap_query_work()
                                    rpdev = cctx->rpdev  /* NULL */
                                    fastrpc_session_alloc() -> NULL
                                    goto register_devices
                                    fastrpc_register_devices(cctx, NULL)
                                      fastrpc_device_register(NULL, ...)
                                        <-- NULL pointer dereference

Even when cctx->rpdev is non-NULL at the locked check before
fastrpc_get_info_from_dsp(), dropping cctx->lock allows
fastrpc_rpmsg_remove() to clear cctx->rpdev and run fastrpc_notify_users()
before fastrpc_context_alloc() adds ctx->node to fl->pending;
fastrpc_invoke_send() then dereferences cctx->rpdev->ept while cctx->rpdev
is NULL.

Also, if fastrpc_rpmsg_remove() clears cctx->rpdev after
fastrpc_get_info_from_dsp() has already returned (for example with 0 or
-ETIMEDOUT), err is not -EPIPE, so fastrpc_cap_query_work() falls through
to register_devices and registers the miscdevices during teardown.

[ ... ]

> +err_cleanup_register:
> +     kfree(dsp_attributes);
> +     fastrpc_session_free(cctx, fl->sctx);
> +     fl->sctx = NULL;
> +     fastrpc_user_put(fl);
> +     goto register_devices;
> +
> +err_cleanup_noreg:
> +     kfree(dsp_attributes);
> +     fastrpc_session_free(cctx, fl->sctx);
> +     fl->sctx = NULL;
> +     fastrpc_user_put(fl);
> +     return;

[Severity: High]
Does fastrpc_cap_query_work() leak the invoke context, its DMA buffer, fl,
and cctx when fastrpc_get_info_from_dsp() fails with -ETIMEDOUT or -EPIPE?

fastrpc_context_alloc() takes a reference on fl (released only in
fastrpc_context_free()) and a reference on cctx, and fastrpc_invoke_send()
takes a second reference on ctx for the rpmsg reply callback:

drivers/misc/fastrpc.c:fastrpc_internal_invoke() {
    ...
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);
        }
    ...
}

On -ETIMEDOUT, fastrpc_internal_invoke() leaves ctx on fl->pending without
dropping its reference.  fastrpc_user_put(fl) here only drops fl->refcount
from 2 to 1 because ctx still holds a reference to fl, while fl->pending is
only drained in fastrpc_user_free(), leaving fl and ctx in a reference
cycle.  In addition, fastrpc_session_free(cctx, fl->sctx) releases the
session back to the pool for reuse while ctx->buf is still mapped on that
session's IOMMU context bank.

On -EPIPE from fastrpc_notify_users(), fastrpc_internal_invoke() removes
ctx from fl->pending and calls fastrpc_context_put(ctx) once, dropping
ctx->refcount from 2 to 1.  Because the channel is being removed and the
rpmsg callback will not run, the second reference taken by
fastrpc_invoke_send() is never dropped, so ctx, ctx->buf, fl, and cctx are
never freed.

[ ... ]

> @@ -2804,31 +2933,27 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device 
> *rpdev)
>  
>       err = fastrpc_cb_devices_create(rpdev);
>       if (err)
> -             goto err_deregister_fdev;
> -
> -     if (data->domain_id == ADSP_DOMAIN_ID && data->sesscount > 0) {
> -             struct fastrpc_session_ctx *last_sess;
> -             struct fastrpc_session_ctx *dup_sess;
> -             unsigned long flags;
> +             goto err_unmap_heap;
>  
> -             spin_lock_irqsave(&data->lock, flags);
> -             last_sess = &data->session[data->sesscount - 1];
> -             for (i = 0; i < FASTRPC_DUP_SESSIONS; i++) {
> -                     if (data->sesscount >= FASTRPC_MAX_SESSIONS)
> -                             break;
> -                     dup_sess = &data->session[data->sesscount++];
> -                     memcpy(dup_sess, last_sess, sizeof(*dup_sess));
> -             }
> -             spin_unlock_irqrestore(&data->lock, flags);
> +     /* Probe returns immediately; the capability query and device node
> +      * registration happen asynchronously on this channel's own queue.
> +      */
> +     data->cap_wq = alloc_ordered_workqueue("fastrpc_cap_wq_%s",
> +                                            WQ_HIGHPRI | WQ_MEM_RECLAIM,
> +                                            domain);

[Severity: Medium]
Should this workqueue be allocated without WQ_MEM_RECLAIM, or use a shared
system workqueue instead?

WQ_MEM_RECLAIM spawns a dedicated rescuer kthread per channel that stays
alive until fastrpc_rpmsg_remove(), and requires the work item not to enter
direct memory reclaim.  fastrpc_cap_query_work() is not part of a memory
reclaim path and performs GFP_KERNEL allocations in kzalloc_obj(),
dma_alloc_coherent(), and misc_register().  Queuing cap_query_work on
system_highpri_wq (or system_dfl_wq) would run queries for different
domains concurrently without a per-channel workqueue or rescuer kthread.

[1] https://sashiko.dev/#/bug/linux-94614a33-7b4b-4011-a75a-fc1470b783b0

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/20261125-fastrpc-hpass-support-v1-0-cb1d46b29...@oss.qualcomm.com?part=3

Reply via email to