Skip to contentSkip to conversation

drm/amdgpu: move bo_va ref counting to internal funcs

PATCH In review Nirmoy Das 2 participants 3 messages Jan 13, 2022

Subsystems (2)
lore
Share & tools
lore

Nirmoy Das

author
#0 lore raw

PATCH 1/1 drm/amdgpu: move bo_va ref counting to internal funcs

GEM code should not deal with struct amdgpu_bo_va's ref_count. Move ref counting to amdgpu_vm.c.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 11 +++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 38 +++++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  |  2 ++
 3 files changed, 42 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4 -7
index 4a11a2f4fa73..691f0a879c90 100644
@@ -176,12 +176,9 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,
 	if (r)
 		return r;
 
-	bo_va = amdgpu_vm_bo_find(vm, abo);
-	if (!bo_va) {
-		bo_va = amdgpu_vm_bo_add(adev, vm, abo);
-	} else {
-		++bo_va->ref_count;
-	}
+	if (!amdgpu_vm_bo_get(vm, abo))
+		amdgpu_vm_bo_add(adev, vm, abo);
+
 	amdgpu_bo_unreserve(abo);
 	return 0;
 }
@@ -218,7 +215,7 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
 		return;
 	}
 	bo_va = amdgpu_vm_bo_find(vm, bo);
-	if (!bo_va || --bo_va->ref_count)
+	if (!bo_va)
 		goto out_unlock;
 
 	amdgpu_vm_bo_rmv(adev, bo_va);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +36 -2
index b23cb463b106..9d60de6a6697 100644
@@ -1290,16 +1290,49 @@ struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
 				       struct amdgpu_bo *bo)
 {
 	struct amdgpu_vm_bo_base *base;
+	struct amdgpu_bo_va *bo_va = NULL;
 
 	for (base = bo->vm_bo; base; base = base->next) {
 		if (base->vm != vm)
 			continue;
 
-		return container_of(base, struct amdgpu_bo_va, base);
+		bo_va = container_of(base, struct amdgpu_bo_va, base);
 	}
-	return NULL;
+
+	if (bo_va && bo_va->ref_count <= 0)
+		return NULL;
+
+	return bo_va;
 }
 
+/**
+ * amdgpu_vm_bo_get - find the bo_va for a specific vm & bo and increase
+ * the ref_count
+ *
+ * @vm: requested vm
+ * @bo: requested buffer object
+ *
+ * Find @bo inside the requested vm.
+ * Search inside the @bos vm list for the requested vm
+ * Returns the found bo_va with +1 ref_count or NULL if none is found
+ *
+ * Object has to be reserved!
+ *
+ * Returns:
+ * Found bo_va or NULL.
+ */
+struct amdgpu_bo_va *amdgpu_vm_bo_get(struct amdgpu_vm *vm,
+				      struct amdgpu_bo *bo)
+{
+	struct amdgpu_bo_va *bo_va = amdgpu_vm_bo_find(vm, bo);
+
+	if (bo_va)
+		++bo_va->ref_count;
+
+	return bo_va;
+}
+
+
 /**
  * amdgpu_vm_map_gart - Resolve gart mapping of addr
  *
@@ -2704,6 +2737,7 @@ void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
 	if (bo && bo_va->is_xgmi)
 		amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN);
 
+	--bo_va->ref_count;
 	kfree(bo_va);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +2 -0
index 85fcfb8c5efd..6d936fb1b934 100644
@@ -415,6 +415,8 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr);
 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
 				       struct amdgpu_bo *bo);
+struct amdgpu_bo_va *amdgpu_vm_bo_get(struct amdgpu_vm *vm,
+				       struct amdgpu_bo *bo);
 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
 				      struct amdgpu_vm *vm,
 				      struct amdgpu_bo *bo);
Signature · 1 line
2.33.1
#1 lore raw

Am 13.01.22 um 13:06 schrieb Nirmoy Das:

Quoted 96 lines

GEM code should not deal with struct amdgpu_bo_va's ref_count. Move ref counting to amdgpu_vm.c.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 11 +++----
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 38 +++++++++++++++++++++++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  |  2 ++
  3 files changed, 42 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4 -7
index 4a11a2f4fa73..691f0a879c90 100644
@@ -176,12 +176,9 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,
  	if (r)
  		return r;
 
-	bo_va = amdgpu_vm_bo_find(vm, abo);
-	if (!bo_va) {
-		bo_va = amdgpu_vm_bo_add(adev, vm, abo);
-	} else {
-		++bo_va->ref_count;
-	}
+	if (!amdgpu_vm_bo_get(vm, abo))
+		amdgpu_vm_bo_add(adev, vm, abo);
+
  	amdgpu_bo_unreserve(abo);
  	return 0;
  }
@@ -218,7 +215,7 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
  		return;
  	}
  	bo_va = amdgpu_vm_bo_find(vm, bo);
-	if (!bo_va || --bo_va->ref_count)
+	if (!bo_va)
  		goto out_unlock;
 
  	amdgpu_vm_bo_rmv(adev, bo_va);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +35 -2
index b23cb463b106..9d60de6a6697 100644
@@ -1290,16 +1290,49 @@ struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  				       struct amdgpu_bo *bo)
  {
  	struct amdgpu_vm_bo_base *base;
+	struct amdgpu_bo_va *bo_va = NULL;
 
  	for (base = bo->vm_bo; base; base = base->next) {
  		if (base->vm != vm)
  			continue;
 
-		return container_of(base, struct amdgpu_bo_va, base);
+		bo_va = container_of(base, struct amdgpu_bo_va, base);
  	}
-	return NULL;
+
+	if (bo_va && bo_va->ref_count <= 0)
+		return NULL;
+
+	return bo_va;
  }
 
+/**
+ * amdgpu_vm_bo_get - find the bo_va for a specific vm & bo and increase
+ * the ref_count
+ *
+ * @vm: requested vm
+ * @bo: requested buffer object
+ *
+ * Find @bo inside the requested vm.
+ * Search inside the @bos vm list for the requested vm
+ * Returns the found bo_va with +1 ref_count or NULL if none is found
+ *
+ * Object has to be reserved!
+ *
+ * Returns:
+ * Found bo_va or NULL.
+ */
+struct amdgpu_bo_va *amdgpu_vm_bo_get(struct amdgpu_vm *vm,
+				      struct amdgpu_bo *bo)
+{
+	struct amdgpu_bo_va *bo_va = amdgpu_vm_bo_find(vm, bo);
+
+	if (bo_va)
+		++bo_va->ref_count;
+
+	return bo_va;
+}
+
+
  /**
   * amdgpu_vm_map_gart - Resolve gart mapping of addr
   *

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, · line 2741

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
@@ -2704,6 +2737,7 @@ void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  	if (bo && bo_va->is_xgmi)
  		amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN);
 
+	--bo_va->ref_count;
  	kfree(bo_va);

That here won't work, you are removing and freeing the bo_va even if the refcount is not zero yet.

I suggest to have a matching amdgpu_vm_bo_put() function instead.

Regards,
Christian.

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
  }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +2 -0
index 85fcfb8c5efd..6d936fb1b934 100644
@@ -415,6 +415,8 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr);
  struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  				       struct amdgpu_bo *bo);
+struct amdgpu_bo_va *amdgpu_vm_bo_get(struct amdgpu_vm *vm,
+				       struct amdgpu_bo *bo);
  struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  				      struct amdgpu_vm *vm,
  				      struct amdgpu_bo *bo);
#2 lore raw
Quoted 105 lines · Christian König

Am 13.01.22 um 13:06 schrieb Nirmoy Das:

Quoted 95 lines

GEM code should not deal with struct amdgpu_bo_va's ref_count. Move ref counting to amdgpu_vm.c.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 11 +++---- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 38 +++++++++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  |  2 ++

  3 files changed, 42 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -5
index 4a11a2f4fa73..691f0a879c90 100644
@@ -176,12 +176,9 @@ static int amdgpu_gem_object_open(struct 
 drm_gem_object *obj,
       if (r)
           return r;
   -    bo_va = amdgpu_vm_bo_find(vm, abo);
-    if (!bo_va) {
-        bo_va = amdgpu_vm_bo_add(adev, vm, abo);
-    } else {
-        ++bo_va->ref_count;
-    }
+    if (!amdgpu_vm_bo_get(vm, abo))
+        amdgpu_vm_bo_add(adev, vm, abo);
+
       amdgpu_bo_unreserve(abo);
       return 0;

  }

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
@@ -218,7 +215,7 @@ static void amdgpu_gem_object_close(struct 
 drm_gem_object *obj,
           return;
       }
       bo_va = amdgpu_vm_bo_find(vm, bo);
-    if (!bo_va || --bo_va->ref_count)
+    if (!bo_va)
           goto out_unlock;
         amdgpu_vm_bo_rmv(adev, bo_va);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -1
index b23cb463b106..9d60de6a6697 100644
@@ -1290,16 +1290,49 @@ struct amdgpu_bo_va *amdgpu_vm_bo_find(struct 
 amdgpu_vm *vm,
                          struct amdgpu_bo *bo)
   {
       struct amdgpu_vm_bo_base *base;
+    struct amdgpu_bo_va *bo_va = NULL;
         for (base = bo->vm_bo; base; base = base->next) {
           if (base->vm != vm)
               continue;
   -        return container_of(base, struct amdgpu_bo_va, base);
+        bo_va = container_of(base, struct amdgpu_bo_va, base);
       }
-    return NULL;
+
+    if (bo_va && bo_va->ref_count <= 0)
+        return NULL;
+
+    return bo_va;
   }

  +/**
+ * amdgpu_vm_bo_get - find the bo_va for a specific vm & bo and increase
+ * the ref_count
+ *
+ * @vm: requested vm
+ * @bo: requested buffer object
+ *
+ * Find @bo inside the requested vm.
+ * Search inside the @bos vm list for the requested vm
+ * Returns the found bo_va with +1 ref_count or NULL if none is found
+ *
+ * Object has to be reserved!
+ *
+ * Returns:
+ * Found bo_va or NULL.
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +12 -0
+struct amdgpu_bo_va *amdgpu_vm_bo_get(struct amdgpu_vm *vm,
+                      struct amdgpu_bo *bo)
+{
+    struct amdgpu_bo_va *bo_va = amdgpu_vm_bo_find(vm, bo);
+
+    if (bo_va)
+        ++bo_va->ref_count;
+
+    return bo_va;
+}
+
+

  /**
   * amdgpu_vm_map_gart - Resolve gart mapping of addr *

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, · line 2740

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -2704,6 +2737,7 @@ void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
       if (bo && bo_va->is_xgmi)
           amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN);
   +    --bo_va->ref_count;
       kfree(bo_va);

That here won't work, you are removing and freeing the bo_va even if the refcount is not zero yet.

Quoted 1 line

I suggest to have a matching amdgpu_vm_bo_put() function instead.

Right, let me resend v2.

Thanks,

Nirmoy

Quoted 20 lines

Regards,
Christian.

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
   }

  diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h index 85fcfb8c5efd..6d936fb1b934 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +2 -0
@@ -415,6 +415,8 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device 
 *adev,
   uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr);
   struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
                          struct amdgpu_bo *bo);
+struct amdgpu_bo_va *amdgpu_vm_bo_get(struct amdgpu_vm *vm,
+                       struct amdgpu_bo *bo);
   struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
                         struct amdgpu_vm *vm,

                        struct amdgpu_bo *bo);