Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/amdgpu: missing root PD lock in
  amdgpu_vmid_alloc_spm_gfxhubs()
--

commit 4216e97db26fd6fc889673c452d2434887bc13af
Author: James Zhu <[email protected]>

drm/amdgpu: reserve and bind the SPM VMID on every GFXHUB instance

This commit reserves, binds, and unbinds the SPM VMID on every GFXHUB
instance rather than only on AMDGPU_GFXHUB(0).

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c
> index 9c879ac683520..a4403e5cc3443 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c
> @@ -651,12 +651,78 @@ void amdgpu_vmid_free_spm(struct amdgpu_device *adev, 
> struct amdgpu_vm *vm,
>               id_mgr->reserved_vmid = false;
>  
>               if (adev->vm_manager.spm_vmid && adev->gfxhub.funcs)
> -                     amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(0),
> +                     amdgpu_gmc_bind_vmid(adev, vmhub,
>                                            adev->vm_manager.spm_vmid, 0);
>       }
>       mutex_unlock(&id_mgr->lock);
>  }
>  
> +/*
> + * amdgpu_vmid_alloc_spm_gfxhubs - reserve the SPM VMID on a set of GFXHUBs
> + * @adev: amdgpu device structure
> + * @vm: the VM to reserve the ID for
> + * @xcc_mask: XCC instances on whose GFXHUBs to reserve
> + *
> + * Reserve the SPM VMID on the GFXHUB of every XCC in @xcc_mask, so that
> + * the reservation covers every hub the VM's jobs can run on.
> + *
> + * Returns: 0 for success, the first per-hub error otherwise. On an error
> + * only the hubs this call reserved are released again, after waiting for
> + * the jobs which might have grabbed the reserved VMID in the meantime;
> + * hubs @vm already held before the call stay reserved.
> + */
> +int amdgpu_vmid_alloc_spm_gfxhubs(struct amdgpu_device *adev,
> +                               struct amdgpu_vm *vm, uint32_t xcc_mask)
> +{
> +     uint32_t allocated = 0;
> +     int i, r;
> +
> +     for_each_inst(i, xcc_mask) {
> +             bool held = amdgpu_vmid_uses_reserved(vm, AMDGPU_GFXHUB(i));
> +
> +             r = amdgpu_vmid_alloc_spm(adev, vm, AMDGPU_GFXHUB(i));
> +             if (r)
> +                     goto error_free;
> +             if (!held)
> +                     allocated |= BIT(i);
> +     }
> +
> +     return 0;
> +
> +error_free:
> +     /*
> +      * A job may already have grabbed a freshly reserved VMID, give it
> +      * the wait amdgpu_vmid_free_spm() demands before the rollback
> +      * unbinds the VMID again.
> +      */
> +     if (allocated) {
> +             dma_resv_wait_timeout(vm->root.bo->tbo.base.resv,
> +                                   DMA_RESV_USAGE_READ, false,
> +                                   MAX_SCHEDULE_TIMEOUT);
> +             amdgpu_vmid_free_spm_gfxhubs(adev, vm, allocated);
> +     }
> +     return r;
> +}
[ ... ]
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index 94b1cfe05e3e0..f64020162a4bf 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
[ ... ]
> @@ -3015,10 +3028,12 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void 
> *data, struct drm_file *filp)
>  
>       switch (args->in.op) {
>       case AMDGPU_VM_OP_RESERVE_VMID: {
> +             /* logical XCCs, the GFXHUB index space; xcc_mask is physical */
> +             uint32_t gfxhub_mask = GENMASK(NUM_XCC(adev->gfx.xcc_mask) - 1, 
> 0);
>               int r;
>  
>               /* We only have requirement to reserve vmid from gfxhub */
> -             r = amdgpu_vmid_alloc_spm(adev, vm, AMDGPU_GFXHUB(0));
> +             r = amdgpu_vmid_alloc_spm_gfxhubs(adev, vm, gfxhub_mask);
>               if (!r && !amdgpu_bo_reserve(vm->root.bo, true)) {
>                       amdgpu_vm_spm_update(adev, vm);
>                       amdgpu_bo_unreserve(vm->root.bo);
>               }
>               return r;
>       }

[Severity: Medium]
Can the error rollback in amdgpu_vmid_alloc_spm_gfxhubs() unbind a newly
reserved SPM VMID while a concurrent CS job is still using it?

Unlike AMDGPU_VM_OP_UNRESERVE_VMID, AMDGPU_VM_OP_RESERVE_VMID calls
amdgpu_vmid_alloc_spm_gfxhubs() without holding the vm->root.bo
reservation lock:

CPU0                              CPU1
amdgpu_vm_ioctl(RESERVE_VMID)
  amdgpu_vmid_alloc_spm_gfxhubs()
    amdgpu_vmid_alloc_spm(hub 0)
      vm->reserved_vmid[hub 0] = id
    amdgpu_vmid_alloc_spm(hub 1)
      -> -ENOENT
    dma_resv_wait_timeout(root)
                                  amdgpu_cs_ioctl()
                                    /* reserves root, adds fence */
                                  amdgpu_job_prepare_job()
                                    amdgpu_vmid_grab()
                                      /* grabs reserved VMID on hub 0 */
    amdgpu_vmid_free_spm(hub 0)
      amdgpu_gmc_bind_vmid(..., 0)  <-- unbinds VMID under active job

Because vm->root.bo is not reserved across the wait and free, a command
submission on the same VM can attach a new fence and grab the reserved
VMID on hub 0 after dma_resv_wait_timeout() returns and right before
amdgpu_vmid_free_spm() unbinds its page-table-base register to 0, causing
GPU page faults for the in-flight job.

Running without vm->root.bo reserved also leaves held =
amdgpu_vmid_uses_reserved() unsynchronized: amdgpu_vmid_alloc_spm()
returns 0 when vm->reserved_vmid[vmhub] is already set, so two concurrent
AMDGPU_VM_OP_RESERVE_VMID calls on the same VM can both observe held ==
false on hub 0 and set allocated |= BIT(0), causing a failure on a later
hub in one thread to free the reservation held by the other.  Similarly,
concurrent RESERVE_VMID and UNRESERVE_VMID calls can interleave across
hubs, and if amdgpu_bo_reserve(vm->root.bo, true) fails after
amdgpu_vmid_alloc_spm_gfxhubs() succeeds, amdgpu_vm_spm_update() is
skipped while returning 0.

Reserving vm->root.bo in AMDGPU_VM_OP_RESERVE_VMID before calling
amdgpu_vmid_alloc_spm_gfxhubs() and holding it across
amdgpu_vm_spm_update() (matching AMDGPU_VM_OP_UNRESERVE_VMID) would
serialize per-VM reservation state and prevent new CS jobs during rollback.

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/[email protected]?part=2

Reply via email to