Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] misc: fastrpc: race between fastrpc_cap_query_work() and
  fastrpc_rpmsg_remove()
- [Medium] misc: fastrpc: double misc_deregister() after
  fastrpc_register_devices() error
--

commit 502b28e3abb8ed99c00c841e2045fedc442784d7
Author: Anandu Krishnan E <[email protected]>

misc: fastrpc: query the dsp capabilities during probe time

This commit moves FastRPC device node registration out of
fastrpc_rpmsg_probe() into a per-channel workqueue that first queries
and caches the DSP capabilities before registering the device nodes.

> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index 5375847d8c9a2..48ded6080ddd6 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c

[ ... ]

> @@ -2585,6 +2617,195 @@ static int fastrpc_device_register(struct device 
> *dev, struct fastrpc_channel_ct
>       return err;
>  }
>  
> +static int fastrpc_register_devices(struct fastrpc_channel_ctx *cctx,
> +                                 struct rpmsg_device *rpdev)
> +{
> +     struct device *rdev = &rpdev->dev;
> +     int err, i;

[ ... ]

> +     case CDSP_DOMAIN_ID:
> +     case GDSP_DOMAIN_ID:
> +             cctx->unsigned_support = true;
> +             /* Create both device nodes so that we can allow both Signed 
> and Unsigned PD */
> +             err = fastrpc_device_register(rdev, cctx, true, cctx->domain);
> +             if (err)
> +                     goto err_deregister_fdev;
> +
> +             err = fastrpc_device_register(rdev, cctx, false, cctx->domain);
> +             if (err)
> +                     goto err_deregister_fdev;
> +             break;
> +     default:
> +             err = -EINVAL;
> +             goto err_deregister_fdev;
> +     }
> +
> +     return 0;
> +
> +err_deregister_fdev:
> +     if (cctx->fdevice)
> +             misc_deregister(&cctx->fdevice->miscdev);
> +     if (cctx->secure_fdevice)
> +             misc_deregister(&cctx->secure_fdevice->miscdev);
> +
> +     return err;
> +}

[Severity: Medium]
Can this cause a double misc_deregister() in fastrpc_rpmsg_remove() if the
second fastrpc_device_register() call fails?

Previously, a registration failure aborted fastrpc_rpmsg_probe(), so
fastrpc_rpmsg_remove() never ran.  Now probe returns 0 before
fastrpc_register_devices() runs, and fastrpc_cap_query_work() only logs the
error.  Because err_deregister_fdev does not clear cctx->secure_fdevice or
cctx->fdevice after deregistering them, fastrpc_rpmsg_remove() will call
misc_deregister() on cctx->secure_fdevice->miscdev a second time after its
dynamic minor was already freed and reset to MISC_DYNAMIC_MINOR.

Also, at the register_devices label in fastrpc_cap_query_work(),
fastrpc_register_devices() runs using the initial rpdev snapshot without
re-checking whether fastrpc_rpmsg_remove() has already cleared cctx->rpdev.

> +
> +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;
> +     struct device *rdev;
> +     u32 *dsp_attributes;
> +     struct fastrpc_user *fl;
> +     unsigned long flags;
> +     int err;

[ ... ]

> +     /*
> +      * 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);
> +             kfree(dsp_attributes);
> +             fastrpc_session_free(cctx, fl->sctx);
> +             fl->sctx = NULL;
> +             fastrpc_user_put(fl);
> +             return;
> +     }
> +     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);

[Severity: High]
Can fastrpc_rpmsg_remove() clear cctx->rpdev and finish its
fastrpc_notify_users() pass after cctx->lock is dropped here, but before
fastrpc_get_info_from_dsp() sends the message?

kworker                           CPU0
fastrpc_cap_query_work()
  spin_lock_irqsave(&cctx->lock)
  list_add_tail(&fl->user, ...)
  spin_unlock_irqrestore(&cctx->lock)
  fastrpc_get_info_from_dsp()
    fastrpc_internal_invoke()
      if (!fl->cctx->rpdev)
                                  fastrpc_rpmsg_remove()
                                    spin_lock_irqsave(&cctx->lock)
                                    cctx->rpdev = NULL
                                    fastrpc_notify_users(fl)
                                      /* fl->pending is still empty */
                                    spin_unlock_irqrestore(&cctx->lock)
                                    cancel_work_sync(&cctx->cap_query_work)
      fastrpc_context_alloc()
      ...
      fastrpc_invoke_send()
        rpmsg_send(cctx->rpdev->ept, ...)
          <-- NULL pointer dereference

If fastrpc_rpmsg_remove() runs after fastrpc_context_alloc() adds ctx to
fl->pending (while fastrpc_get_args() sleeps in dma_alloc_coherent()) and
before fastrpc_invoke_send() takes the worker reference with
fastrpc_context_get(ctx), fastrpc_notify_users() also schedules
ctx->put_work on a context with a refcount of 1, freeing ctx while
fastrpc_internal_invoke() is still using it.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to