Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/panthor: infinite busy-loop in
  panthor_perf_handle_fw_events()
- [Medium] drm/panthor: race between panthor_perf_pool_destroy() and
  unplug
- [Medium] drm/panthor: PRFCNT_EN overwrite in
  panthor_perf_handle_fw_samples()
--

commit b28f9d76443cda266099b6a812924734c3213c0a
Author: Lukas Zapolskas <[email protected]>

drm/panthor: Expose performance counter queries and ioctls

This commit exposes the DRM_PANTHOR_DEV_QUERY_PERF_INFO device query and the
five performance counter session ioctls, and wraps the session ioctls with a
drm_dev_enter()/drm_dev_exit() cleanup class.

> diff --git a/drivers/gpu/drm/panthor/panthor_drv.c 
> b/drivers/gpu/drm/panthor/panthor_drv.c
> index 09cc3f699154a..e6e708d9e18f0 100644
> --- a/drivers/gpu/drm/panthor/panthor_drv.c
> +++ b/drivers/gpu/drm/panthor/panthor_drv.c

[ ... ]

> @@ -1590,6 +1599,110 @@ static int panthor_ioctl_bo_query_info(struct 
> drm_device *ddev, void *data,
>       return 0;
>  }
>  
> +static int panthor_drm_dev_enter(struct drm_device *ddev)
> +{
> +     int cookie;
> +
> +     if (!drm_dev_enter(ddev, &cookie))
> +             return -ENODEV;
> +
> +     return cookie;
> +}
> +
> +static void panthor_drm_dev_exit(int cookie)
> +{
> +     if (cookie >= 0)
> +             drm_dev_exit(cookie);
> +}
> +
> +DEFINE_CLASS(panthor_drm_dev, int, panthor_drm_dev_exit(_T),
> +          panthor_drm_dev_enter(ddev), struct drm_device *ddev)
> +
> +static int panthor_ioctl_perf_session_create(struct drm_device *ddev, void 
> *data,
> +                                          struct drm_file *file)
> +{
> +     int ret;
> +     unsigned long sid = 0;
> +     struct drm_panthor_perf_session_create *args = data;
> +
> +     CLASS(panthor_drm_dev, cookie)(ddev);
> +     if (cookie < 0)
> +             return cookie;
> +
> +     if (args->pad)
> +             return -EINVAL;
> +
> +     ret = panthor_perf_session_create(file, args, &sid);
> +     if (ret)
> +             return ret;
> +
> +     return sid;
> +}

[Severity: Medium]
Once panthor_ioctl_perf_session_create() registers a session in
pfile->sessions, can closing the DRM file race with panthor_device_unplug()
and cause a use-after-free or NULL pointer dereference in
panthor_perf_pool_destroy()?

The session ioctls guard against device unplug with panthor_drm_dev, but
panthor_postclose() -> panthor_perf_pool_destroy() does not call
drm_dev_enter(), and panthor_perf_unplug() does not take perf->op_lock:

CPU0                                  CPU1
panthor_postclose()
  panthor_perf_pool_destroy()
    perf = pfile->ptdev->perf
                                      panthor_device_unplug()
                                        panthor_fw_unplug()
                                          panthor_perf_unplug()
                                            panthor_kernel_bo_destroy(perf->rb)
                                            perf->rb = NULL
                                            ptdev->perf = NULL
                                          /* destroys fw section BOs */
    guard(mutex)(&perf->op_lock)
    perf_disable(perf)
      panthor_perf_fw_stop_sampling()
        /* accesses destroyed glb_iface BO */  <-- use-after-free
    perf_enable(perf)
      panthor_perf_fw_start_sampling()
        panthor_perf_fw_write_config(ptdev->perf)
                                               <-- NULL pointer dereference

> +
> +static int panthor_ioctl_perf_session_start(struct drm_device *ddev, void 
> *data,
> +                                         struct drm_file *file)
> +{
> +     struct panthor_file *pfile = file->driver_priv;
> +     struct drm_panthor_perf_session_start *args = data;
> +
> +     CLASS(panthor_drm_dev, cookie)(ddev);
> +     if (cookie < 0)
> +             return cookie;
> +
> +     if (args->pad0)
> +             return -EINVAL;
> +
> +     return panthor_perf_session_start(pfile, args->handle, args->user_data);
> +}

[Severity: High]
When a session is active or transitioning while a GPU reset is pending, does
panthor_perf_handle_fw_events() spin in an unthrottled busy-loop on the
WQ_HIGHPRI perf->sample_workq workqueue?

drivers/gpu/drm/panthor/panthor_perf.c:panthor_perf_handle_fw_events() {
    ...
        if (panthor_device_reset_is_pending(ptdev)) {
                queue_work(perf->sample_workq, &perf->sample_work);
                return;
        }
    ...
}

Because perf->sample_work is a non-delayed work item executing on a
WQ_HIGHPRI | WQ_UNBOUND workqueue, re-queueing it from inside its own handler
while ptdev->reset.pending is set causes it to re-run immediately at high
priority without yielding, starving normal-priority workers such as
ptdev->reset.wq or spinning until resume if reset.pending was left set by an
aborted reset during suspend.

> +
> +static int panthor_ioctl_perf_session_sample(struct drm_device *ddev, void 
> *data,
> +                                          struct drm_file *file)
> +{
> +     struct panthor_file *pfile = file->driver_priv;
> +     struct drm_panthor_perf_session_sample *args = data;
> +
> +     CLASS(panthor_drm_dev, cookie)(ddev);
> +     if (cookie < 0)
> +             return cookie;
> +
> +     if (args->pad0)
> +             return -EINVAL;
> +
> +     return panthor_perf_session_sample(pfile, args->handle, 
> args->user_data);
> +}

[Severity: Medium]
When panthor_perf_handle_fw_samples() accumulates multiple firmware samples
in one pass (insert_idx - extract_idx > 1), does overwriting
blk->counters[PANTHOR_CTR_PRFCNT_EN] cause session_populate_sample() to leave
unrequested counter values in the user ringbuffer?

drivers/gpu/drm/panthor/panthor_perf.c:panthor_perf_handle_fw_samples() {
    ...
                        blk->counters[PANTHOR_CTR_TIMESTAMP_LO] = 
block[PANTHOR_CTR_TIMESTAMP_LO];
                        blk->counters[PANTHOR_CTR_TIMESTAMP_HI] = 
block[PANTHOR_CTR_TIMESTAMP_HI];
                        blk->counters[PANTHOR_CTR_PRFCNT_EN] = 
block[PANTHOR_CTR_PRFCNT_EN];
    ...
                        if (prfcnt_en == 1)
                                goto advance_block;

                        for (size_t k = PANTHOR_HEADER_COUNTERS;
                             k < ptdev->perf_info.counters_per_block; k++)
                                blk->counters[k] += block[k];
    ...
}

The counter values are summed across all samples in [extract_idx, insert_idx),
but PANTHOR_CTR_PRFCNT_EN is overwritten with the last non-zero prfcnt_en
instead of being bitwise-ORed.  Because session_populate_sample() copies the
entire accumulated perf_sample into the user-mapped session BO with memcpy()
and only zeroes out unrequested counters that are set in the final
PANTHOR_CTR_PRFCNT_EN mask, any counter group enabled in an earlier sample of
the batch but not the last one is not cleared.

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

Reply via email to