Commit b8ea3783 authored by Felix Kuehling's avatar Felix Kuehling Committed by Alex Deucher

drm/amdgpu: Fix race condition in amdgpu_mn_unregister

Exchange locking order of adev->mn_lock and mm_sem, so that
rmn->mm->mmap_sem can be taken safely, protected by adev->mn_lock,
when amdgpu_mn_destroy runs concurrently.
Signed-off-by: default avatarFelix Kuehling <Felix.Kuehling@amd.com>
Reviewed-by: default avatarChristian König <christian.koenig@amd.com>
parent 418aa0c2
...@@ -71,12 +71,11 @@ static void amdgpu_mn_destroy(struct work_struct *work) ...@@ -71,12 +71,11 @@ static void amdgpu_mn_destroy(struct work_struct *work)
struct amdgpu_mn_node *node, *next_node; struct amdgpu_mn_node *node, *next_node;
struct amdgpu_bo *bo, *next_bo; struct amdgpu_bo *bo, *next_bo;
down_write(&rmn->mm->mmap_sem);
mutex_lock(&adev->mn_lock); mutex_lock(&adev->mn_lock);
down_write(&rmn->mm->mmap_sem);
hash_del(&rmn->node); hash_del(&rmn->node);
rbtree_postorder_for_each_entry_safe(node, next_node, &rmn->objects, rbtree_postorder_for_each_entry_safe(node, next_node, &rmn->objects,
it.rb) { it.rb) {
interval_tree_remove(&node->it, &rmn->objects); interval_tree_remove(&node->it, &rmn->objects);
list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) { list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
bo->mn = NULL; bo->mn = NULL;
...@@ -84,8 +83,8 @@ static void amdgpu_mn_destroy(struct work_struct *work) ...@@ -84,8 +83,8 @@ static void amdgpu_mn_destroy(struct work_struct *work)
} }
kfree(node); kfree(node);
} }
mutex_unlock(&adev->mn_lock);
up_write(&rmn->mm->mmap_sem); up_write(&rmn->mm->mmap_sem);
mutex_unlock(&adev->mn_lock);
mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm); mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm);
kfree(rmn); kfree(rmn);
} }
...@@ -182,8 +181,8 @@ static struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev) ...@@ -182,8 +181,8 @@ static struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
struct amdgpu_mn *rmn; struct amdgpu_mn *rmn;
int r; int r;
down_write(&mm->mmap_sem);
mutex_lock(&adev->mn_lock); mutex_lock(&adev->mn_lock);
down_write(&mm->mmap_sem);
hash_for_each_possible(adev->mn_hash, rmn, node, (unsigned long)mm) hash_for_each_possible(adev->mn_hash, rmn, node, (unsigned long)mm)
if (rmn->mm == mm) if (rmn->mm == mm)
...@@ -207,14 +206,14 @@ static struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev) ...@@ -207,14 +206,14 @@ static struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
hash_add(adev->mn_hash, &rmn->node, (unsigned long)mm); hash_add(adev->mn_hash, &rmn->node, (unsigned long)mm);
release_locks: release_locks:
mutex_unlock(&adev->mn_lock);
up_write(&mm->mmap_sem); up_write(&mm->mmap_sem);
mutex_unlock(&adev->mn_lock);
return rmn; return rmn;
free_rmn: free_rmn:
mutex_unlock(&adev->mn_lock);
up_write(&mm->mmap_sem); up_write(&mm->mmap_sem);
mutex_unlock(&adev->mn_lock);
kfree(rmn); kfree(rmn);
return ERR_PTR(r); return ERR_PTR(r);
...@@ -288,14 +287,18 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr) ...@@ -288,14 +287,18 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
void amdgpu_mn_unregister(struct amdgpu_bo *bo) void amdgpu_mn_unregister(struct amdgpu_bo *bo)
{ {
struct amdgpu_device *adev = bo->adev; struct amdgpu_device *adev = bo->adev;
struct amdgpu_mn *rmn = bo->mn; struct amdgpu_mn *rmn;
struct list_head *head; struct list_head *head;
if (rmn == NULL) mutex_lock(&adev->mn_lock);
rmn = bo->mn;
if (rmn == NULL) {
mutex_unlock(&adev->mn_lock);
return; return;
}
down_write(&rmn->mm->mmap_sem); down_write(&rmn->mm->mmap_sem);
mutex_lock(&adev->mn_lock);
/* save the next list entry for later */ /* save the next list entry for later */
head = bo->mn_list.next; head = bo->mn_list.next;
...@@ -310,6 +313,6 @@ void amdgpu_mn_unregister(struct amdgpu_bo *bo) ...@@ -310,6 +313,6 @@ void amdgpu_mn_unregister(struct amdgpu_bo *bo)
kfree(node); kfree(node);
} }
mutex_unlock(&adev->mn_lock);
up_write(&rmn->mm->mmap_sem); up_write(&rmn->mm->mmap_sem);
mutex_unlock(&adev->mn_lock);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment