Skip to contentSkip to conversation

drm/amdkfd: Fix svm_bo use after free in svm_range_clone

PATCH In review Philip Yang 3 participants 3 messages Oct 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 drm/amdkfd: Fix svm_bo use after free in svm_range_clone

prange->svm_bo stays set after the range drops its reference in svm_range_vram_node_free(), it is only cleared later by svm_range_bo_release(). Taking a new reference based on old->svm_bo can therefore hit a svm_bo with refcount 0 and svm_range_bo_release_async() pending, leaving the clone with a freed svm_bo.

Use svm_bo_ref_unless_zero() under old->lock to only share the svm_bo if it is still alive, same as svm_range_validate_svm_bo() does.

A range only holds a svm_bo reference while ttm_res is set, so drop the reference again if old had already dropped its own. The clone keeps the svm_bo pointer and stays on svm_bo->range_list, so svm_range_bo_release() clears the pointer when the svm_bo goes away. The unref must be done outside old->lock because svm_range_bo_release() takes the lock of every range on the list, including old.

v3: drop the reference if ttm_res is not set, don't leak it.
v2: use svm_bo_ref_unless_zero instead of checking old->ttm_res, to keep
sharing the svm_bo when the range dropped its own reference (Felix).

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +11 -2
index cfb428c72686..5dda163cf4ae 100644
@@ -2158,14 +2158,23 @@ static struct svm_range *svm_range_clone(struct svm_range *old)
 		svm_range_free(new, false);
 		return NULL;
 	}
-	if (old->svm_bo) {
+
+	/* old->lock keeps old->svm_bo alive while taking the reference */
+	mutex_lock(&old->lock);
+	if (svm_bo_ref_unless_zero(old->svm_bo)) {
 		new->ttm_res = old->ttm_res;
 		new->offset = old->offset;
-		new->svm_bo = svm_range_bo_ref(old->svm_bo);
+		new->svm_bo = old->svm_bo;
 		spin_lock(&new->svm_bo->list_lock);
 		list_add(&new->svm_bo_list, &new->svm_bo->range_list);
 		spin_unlock(&new->svm_bo->list_lock);
 	}
+	mutex_unlock(&old->lock);
+
+	/* prange only holds an svm_bo reference while ttm_res is set */
+	if (new->svm_bo && !new->ttm_res)
+		svm_range_bo_unref(new->svm_bo);
+
 	new->flags = old->flags;
 	new->preferred_loc = old->preferred_loc;
 	new->prefetch_loc = old->prefetch_loc;
Signature · 1 line
2.50.1
maintainer Reviewed-by on v3 1/1
#1 lore raw
Quoted 9 lines · Philip Yang

prange->svm_bo stays set after the range drops its reference in svm_range_vram_node_free(), it is only cleared later by svm_range_bo_release(). Taking a new reference based on old->svm_bo can therefore hit a svm_bo with refcount 0 and svm_range_bo_release_async() pending, leaving the clone with a freed svm_bo.

Use svm_bo_ref_unless_zero() under old->lock to only share the svm_bo if it is still alive, same as svm_range_validate_svm_bo() does.

Quoted 8 lines

A range only holds a svm_bo reference while ttm_res is set, so drop the reference again if old had already dropped its own. The clone keeps the svm_bo pointer and stays on svm_bo->range_list, so svm_range_bo_release() clears the pointer when the svm_bo goes away. The unref must be done outside old->lock because svm_range_bo_release() takes the lock of every range on the list, including old.

v3: drop the reference if ttm_res is not set, don't leak it.

Good catch. See one nit-pick inline.

Quoted 25 lines

v2: use svm_bo_ref_unless_zero instead of checking old->ttm_res, to keep
sharing the svm_bo when the range dropped its own reference (Felix).

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 13 +++++++++++--
  1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -2
index cfb428c72686..5dda163cf4ae 100644
@@ -2158,14 +2158,23 @@ static struct svm_range *svm_range_clone(struct svm_range *old)
  		svm_range_free(new, false);
  		return NULL;
  	}
-	if (old->svm_bo) {
+
+	/* old->lock keeps old->svm_bo alive while taking the reference */
+	mutex_lock(&old->lock);
+	if (svm_bo_ref_unless_zero(old->svm_bo)) {
  		new->ttm_res = old->ttm_res;
  		new->offset = old->offset;
-		new->svm_bo = svm_range_bo_ref(old->svm_bo);

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static struct svm_range *svm_range_clone(struct svm_range *old) · line 2176

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +6 -0
+		new->svm_bo = old->svm_bo;
  		spin_lock(&new->svm_bo->list_lock);
  		list_add(&new->svm_bo_list, &new->svm_bo->range_list);
  		spin_unlock(&new->svm_bo->list_lock);
  	}
+	mutex_unlock(&old->lock);
+
+	/* prange only holds an svm_bo reference while ttm_res is set */
+	if (new->svm_bo && !new->ttm_res)
+		svm_range_bo_unref(new->svm_bo);

I guess you're doing this after mutex_unlock to prevent recursive locking in case svm_range_bo_release gets called. Maybe mention that in the comment. With that, the patch is

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -0
+
  	new->flags = old->flags;
  	new->preferred_loc = old->preferred_loc;
  	new->prefetch_loc = old->prefetch_loc;
#2 lore raw
In reply to Philip Yang
Quoted 31 lines · Philip Yang

prange->svm_bo stays set after the range drops its reference in svm_range_vram_node_free(), it is only cleared later by svm_range_bo_release(). Taking a new reference based on old->svm_bo can therefore hit a svm_bo with refcount 0 and svm_range_bo_release_async() pending, leaving the clone with a freed svm_bo.

Use svm_bo_ref_unless_zero() under old->lock to only share the svm_bo if it is still alive, same as svm_range_validate_svm_bo() does.

A range only holds a svm_bo reference while ttm_res is set, so drop the reference again if old had already dropped its own. The clone keeps the svm_bo pointer and stays on svm_bo->range_list, so svm_range_bo_release() clears the pointer when the svm_bo goes away. The unref must be done outside old->lock because svm_range_bo_release() takes the lock of every range on the list, including old.

v3: drop the reference if ttm_res is not set, don't leak it.
v2: use svm_bo_ref_unless_zero instead of checking old->ttm_res, to keep
sharing the svm_bo when the range dropped its own reference (Felix).

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 13 +++++++++++--
  1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
index cfb428c72686..5dda163cf4ae 100644

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static struct svm_range *svm_range_clone(struct svm_range *old) · line 2164

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -1
@@ -2158,14 +2158,23 @@ static struct svm_range *svm_range_clone(struct svm_range *old)
  		svm_range_free(new, false);
  		return NULL;
  	}
-	if (old->svm_bo) {
+
+	/* old->lock keeps old->svm_bo alive while taking the reference */
+	mutex_lock(&old->lock);
+	if (svm_bo_ref_unless_zero(old->svm_bo)) {

The following checking if new->ttm_res is null, you drop ref of new->svm_bo that was just got.

If old->ttm_res is null old prange has no reference on svm_bo, so new prange(cloned from old). Can following checking is ok?

+ if (old->ttm_res && svm_bo_ref_unless_zero(old->svm_bo)) {

If old->ttm_res is null, new prange will not hold any vram buf metadata since new is cloned from old. Then do not need following checking to drop ref of svm_bo that was just got here since new->ttm_res is valid.

Regards

Xiaogang

Quoted 17 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -1
  		new->ttm_res = old->ttm_res;
  		new->offset = old->offset;
-		new->svm_bo = svm_range_bo_ref(old->svm_bo);
+		new->svm_bo = old->svm_bo;
  		spin_lock(&new->svm_bo->list_lock);
  		list_add(&new->svm_bo_list, &new->svm_bo->range_list);
  		spin_unlock(&new->svm_bo->list_lock);
  	}
+	mutex_unlock(&old->lock);
+
+	/* prange only holds an svm_bo reference while ttm_res is set */
+	if (new->svm_bo && !new->ttm_res)
+		svm_range_bo_unref(new->svm_bo);
+
  	new->flags = old->flags;
  	new->preferred_loc = old->preferred_loc;
  	new->prefetch_loc = old->prefetch_loc;