Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/msm: unsynchronized fault handler clear in
  msm_drm_kms_uninit()
--

commit e1772847baca24cdc32b6b0a89748cda0bfad94c
Author: Dmitry Baryshkov <[email protected]>

drm/msm: order the display snapshot sources around its worker

This commit moves registration of the KMS IOMMU fault handler to the end of
msm_drm_kms_init(), unregisters it at the beginning of msm_drm_kms_uninit(),
and destroys the display snapshot worker after uninstalling interrupts.

> diff --git a/drivers/gpu/drm/msm/msm_kms.c b/drivers/gpu/drm/msm/msm_kms.c
> index f3e39c3907a93..a94d2888a21e5 100644
> --- a/drivers/gpu/drm/msm/msm_kms.c
> +++ b/drivers/gpu/drm/msm/msm_kms.c
[ ... ]
> @@ -245,6 +241,8 @@ void msm_drm_kms_uninit(struct device *dev)
>  
>       BUG_ON(!kms);
>  
> +     msm_mmu_set_fault_handler(to_msm_vm(kms->vm)->mmu, kms, NULL);
> +
>       /* We must cancel and cleanup any pending vblank enable/disable
>        * work before msm_irq_uninstall() to avoid work re-enabling an
>        * irq after uninstall has disabled it.
> @@ -256,12 +254,12 @@ void msm_drm_kms_uninit(struct device *dev)
>  
>       drm_kms_helper_poll_fini(ddev);
>  
> -     msm_disp_snapshot_destroy(ddev);
> -
>       pm_runtime_get_sync(dev);
>       msm_irq_uninstall(ddev);
>       pm_runtime_put_sync(dev);
>  
> +     msm_disp_snapshot_destroy(ddev);
> +
>       if (kms && kms->funcs)
>               kms->funcs->destroy(kms);
>  }

[Severity: High]
Can an in-flight IOMMU fault handler still race with
msm_disp_snapshot_destroy() and cause a use-after-free on kms->dump_worker
(or a NULL function pointer call)?

msm_mmu_set_fault_handler() does an unlocked store to mmu->handler, and
msm_disp_fault_handler() reads and calls iommu->base.handler without locking
or synchronization:

drivers/gpu/drm/msm/msm_iommu.c:msm_disp_fault_handler() {
        struct msm_iommu *iommu = arg;

        if (iommu->base.handler)
                return iommu->base.handler(iommu->base.arg, iova, flags, NULL);

        return -ENOSYS;
}

Nothing between msm_mmu_set_fault_handler(..., NULL) and
msm_disp_snapshot_destroy() waits for the SMMU context fault threaded IRQ to
finish (msm_irq_uninstall() only frees the display controller IRQ kms->irq,
while the IOMMU domain is destroyed later in kms->funcs->destroy()):

CPU0                                  CPU1
msm_disp_fault_handler()
  if (iommu->base.handler)
                                      msm_drm_kms_uninit()
                                        msm_mmu_set_fault_handler(..., NULL)
                                        ...
                                        msm_disp_snapshot_destroy()
                                          kthread_destroy_worker(
                                            kms->dump_worker)
    msm_kms_fault_handler()
      msm_disp_snapshot_state()
        kthread_queue_work(
          kms->dump_worker, ...)  <-- use-after-free

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/20261127-msm-kms-destroy-fixes-v4-0-242b0a3f8...@oss.qualcomm.com?part=4

Reply via email to