Skip to contentSkip to conversation

accel/amdxdna: Fix potential amdxdna_umap lifetime race

PATCH In review Lizhi Hou 2 participants 3 messages Jul 6 – Jul 7

Subsystems (2)
lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

PATCH 1/1 accel/amdxdna: Fix potential amdxdna_umap lifetime race

amdxdna_umap_release() calls the blocking mmu_interval_notifier_remove() before removing the object from abo->mem.umap_list. If aie2_populate_range() runs concurrently, it may obtain a reference to an amdxdna_umap that is being released, leading to a potential use-after-free.

Use refcount_inc_not_zero() in aie2_populate_range() when acquiring a reference. If the reference count has already dropped to zero, release is in progress and the entry is skipped.

Fixes e486147c912f ("accel/amdxdna: Add BO import and export")
---
 drivers/accel/amdxdna/aie2_ctx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +2 -2
index 408ff7e2a272..7bf635634e64 100644
@@ -1039,7 +1039,7 @@ static int aie2_populate_range(struct amdxdna_gem_obj *abo)
 	found = false;
 	down_write(&xdna->notifier_lock);
 	list_for_each_entry(mapp, &abo->mem.umap_list, node) {
-		if (mapp->invalid) {
+		if (mapp->invalid && kref_get_unless_zero(&mapp->refcnt)) {
 			found = true;
 			break;
 		}
@@ -1050,7 +1050,7 @@ static int aie2_populate_range(struct amdxdna_gem_obj *abo)
 		up_write(&xdna->notifier_lock);
 		return 0;
 	}
-	kref_get(&mapp->refcnt);
+
 	up_write(&xdna->notifier_lock);
 
 	mm = mapp->notifier.mm;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Lizhi Hou

amdxdna_umap_release() calls the blocking mmu_interval_notifier_remove() before removing the object from abo->mem.umap_list. If aie2_populate_range() runs concurrently, it may obtain a reference to an amdxdna_umap that is being released, leading to a potential use-after-free.

Use refcount_inc_not_zero() in aie2_populate_range() when acquiring a reference. If the reference count has already dropped to zero, release is in progress and the entry is skipped.

Fixes e486147c912f ("accel/amdxdna: Add BO import and export")

The patch description mentioned refcount_inc_not_zero(), but the real fix calls kref_get_unless_zero() which is a wrapper of refcount_inc_not_zero(). It might be better to fix the description to match the fix in the code.

Thanks,
Max

Quoted 26 lines
---
  drivers/accel/amdxdna/aie2_ctx.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +2 -2
index 408ff7e2a272..7bf635634e64 100644
@@ -1039,7 +1039,7 @@ static int aie2_populate_range(struct amdxdna_gem_obj *abo)
  	found = false;
  	down_write(&xdna->notifier_lock);
  	list_for_each_entry(mapp, &abo->mem.umap_list, node) {
-		if (mapp->invalid) {
+		if (mapp->invalid && kref_get_unless_zero(&mapp->refcnt)) {
  			found = true;
  			break;
  		}
@@ -1050,7 +1050,7 @@ static int aie2_populate_range(struct amdxdna_gem_obj *abo)
  		up_write(&xdna->notifier_lock);
  		return 0;
  	}
-	kref_get(&mapp->refcnt);
+
  	up_write(&xdna->notifier_lock);
 
  	mm = mapp->notifier.mm;
author maintainer Applied
#2 lore raw

Applied to drm-misc-fixes

Quoted 48 lines · Max Zhen
Quoted 11 lines · Lizhi Hou

amdxdna_umap_release() calls the blocking mmu_interval_notifier_remove() before removing the object from abo->mem.umap_list. If aie2_populate_range() runs concurrently, it may obtain a reference to an amdxdna_umap that is being released, leading to a potential use-after-free.

Use refcount_inc_not_zero() in aie2_populate_range() when acquiring a reference. If the reference count has already dropped to zero, release is in progress and the entry is skipped.

Fixes e486147c912f ("accel/amdxdna: Add BO import and export")

The patch description mentioned refcount_inc_not_zero(), but the real fix calls kref_get_unless_zero() which is a wrapper of refcount_inc_not_zero(). It might be better to fix the description to match the fix in the code.

Thanks,
Max

Quoted 27 lines

---
  drivers/accel/amdxdna/aie2_ctx.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +1 -1
index 408ff7e2a272..7bf635634e64 100644
@@ -1039,7 +1039,7 @@ static int aie2_populate_range(struct 
 amdxdna_gem_obj *abo)
       found = false;
       down_write(&xdna->notifier_lock);
       list_for_each_entry(mapp, &abo->mem.umap_list, node) {
-        if (mapp->invalid) {
+        if (mapp->invalid && kref_get_unless_zero(&mapp->refcnt)) {
               found = true;
               break;

          }

drivers/accel/amdxdna/aie2_ctx.c +1 -1
@@ -1050,7 +1050,7 @@ static int aie2_populate_range(struct 
 amdxdna_gem_obj *abo)
           up_write(&xdna->notifier_lock);
           return 0;
       }
-    kref_get(&mapp->refcnt);
+
       up_write(&xdna->notifier_lock);
         mm = mapp->notifier.mm;