@@ -207,7 +207,7 @@ static void unreserve_mem_limit(struct amdgpu_device *adev,
207207 spin_unlock (& kfd_mem_limit .mem_limit_lock );
208208}
209209
210- void amdgpu_amdkfd_unreserve_memory_limit (struct amdgpu_bo * bo )
210+ void amdgpu_amdkfd_release_notify (struct amdgpu_bo * bo )
211211{
212212 struct amdgpu_device * adev = amdgpu_ttm_adev (bo -> tbo .bdev );
213213 u32 domain = bo -> preferred_domains ;
@@ -219,6 +219,8 @@ void amdgpu_amdkfd_unreserve_memory_limit(struct amdgpu_bo *bo)
219219 }
220220
221221 unreserve_mem_limit (adev , amdgpu_bo_size (bo ), domain , sg );
222+
223+ kfree (bo -> kfd_bo );
222224}
223225
224226
@@ -734,14 +736,19 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
734736 }
735737
736738 /* Add BO to VM internal data structures */
739+ ret = amdgpu_bo_reserve (bo [i ], false);
740+ if (ret ) {
741+ pr_debug ("Unable to reserve BO during memory attach" );
742+ goto unwind ;
743+ }
737744 attachment [i ]-> bo_va = amdgpu_vm_bo_add (adev , vm , bo [i ]);
745+ amdgpu_bo_unreserve (bo [i ]);
738746 if (unlikely (!attachment [i ]-> bo_va )) {
739747 ret = - ENOMEM ;
740748 pr_err ("Failed to add BO object to VM. ret == %d\n" ,
741749 ret );
742750 goto unwind ;
743751 }
744-
745752 attachment [i ]-> va = va ;
746753 attachment [i ]-> pte_flags = get_pte_flags (adev , mem );
747754 attachment [i ]-> adev = adev ;
@@ -757,7 +764,9 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
757764 if (!attachment [i ])
758765 continue ;
759766 if (attachment [i ]-> bo_va ) {
767+ amdgpu_bo_reserve (bo [i ], true);
760768 amdgpu_vm_bo_rmv (adev , attachment [i ]-> bo_va );
769+ amdgpu_bo_unreserve (bo [i ]);
761770 list_del (& attachment [i ]-> list );
762771 }
763772 if (bo [i ])
@@ -1568,12 +1577,12 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
15681577 pr_debug ("Release VA 0x%llx - 0x%llx\n" , mem -> va ,
15691578 mem -> va + bo_size * (1 + mem -> aql_queue ));
15701579
1571- ret = unreserve_bo_and_vms (& ctx , false, false);
1572-
15731580 /* Remove from VM internal data structures */
15741581 list_for_each_entry_safe (entry , tmp , & mem -> attachments , list )
15751582 kfd_mem_detach (entry );
15761583
1584+ ret = unreserve_bo_and_vms (& ctx , false, false);
1585+
15771586 /* Free the sync object */
15781587 amdgpu_sync_free (& mem -> sync );
15791588
@@ -1600,9 +1609,13 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
16001609 drm_vma_node_revoke (& mem -> bo -> tbo .base .vma_node , drm_priv );
16011610 if (mem -> dmabuf )
16021611 dma_buf_put (mem -> dmabuf );
1603- drm_gem_object_put (& mem -> bo -> tbo .base );
16041612 mutex_destroy (& mem -> lock );
1605- kfree (mem );
1613+
1614+ /* If this releases the last reference, it will end up calling
1615+ * amdgpu_amdkfd_release_notify and kfree the mem struct. That's why
1616+ * this needs to be the last call here.
1617+ */
1618+ drm_gem_object_put (& mem -> bo -> tbo .base );
16061619
16071620 return ret ;
16081621}
0 commit comments