Skip to contentSkip to conversation

accel/amdxdna: Fully initialize map structure before registering notifier

PATCH In review Lizhi Hou 2 participants 3 messages Sep 11 – Sep 17

Subsystems (2)
lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

PATCH 1/1 accel/amdxdna: Fully initialize map structure before registering notifier

The map structure is currently only partially initialized when the notifier is registered. Although holding mmap_write_lock() prevents the notifier callback from running before the remaining initialization is complete, the ordering can be confusing during code review.

Fully initialize the map structure before registering the notifier so that the structure is completely constructed before it is published.

---
 drivers/accel/amdxdna/amdxdna_gem.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/accel/amdxdna/amdxdna_gem.c +9 -9
index 5a1a2ee261b7..e861db6f9369 100644
@@ -415,6 +415,15 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo,
 		goto free_map;
 	}
 
+	mapp->range.notifier = &mapp->notifier;
+	mapp->range.start = vma->vm_start;
+	mapp->range.end = vma->vm_end;
+	mapp->range.default_flags = HMM_PFN_REQ_FAULT;
+	mapp->abo = abo;
+	kref_init(&mapp->refcnt);
+
+	INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work);
+
 	ret = mmu_interval_notifier_insert_locked(&mapp->notifier,
 						  current->mm,
 						  addr,
@@ -425,15 +434,6 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo,
 		goto free_pfns;
 	}
 
-	mapp->range.notifier = &mapp->notifier;
-	mapp->range.start = vma->vm_start;
-	mapp->range.end = vma->vm_end;
-	mapp->range.default_flags = HMM_PFN_REQ_FAULT;
-	mapp->abo = abo;
-	kref_init(&mapp->refcnt);
-
-	INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work);
-
 	down_write(&xdna->notifier_lock);
 	if (list_empty(&abo->mem.umap_list))
 		abo->mem.uva = addr;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Lizhi Hou

The map structure is currently only partially initialized when the notifier is registered. Although holding mmap_write_lock() prevents the notifier callback from running before the remaining initialization is complete, the ordering can be confusing during code review.

Fully initialize the map structure before registering the notifier so that the structure is completely constructed before it is published.

Quoted 40 lines
---
  drivers/accel/amdxdna/amdxdna_gem.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
drivers/accel/amdxdna/amdxdna_gem.c +9 -9
index 5a1a2ee261b7..e861db6f9369 100644
@@ -415,6 +415,15 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo,
  		goto free_map;
  	}
 
+	mapp->range.notifier = &mapp->notifier;
+	mapp->range.start = vma->vm_start;
+	mapp->range.end = vma->vm_end;
+	mapp->range.default_flags = HMM_PFN_REQ_FAULT;
+	mapp->abo = abo;
+	kref_init(&mapp->refcnt);
+
+	INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work);
+
  	ret = mmu_interval_notifier_insert_locked(&mapp->notifier,
  						  current->mm,
  						  addr,
@@ -425,15 +434,6 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo,
  		goto free_pfns;
  	}
 
-	mapp->range.notifier = &mapp->notifier;
-	mapp->range.start = vma->vm_start;
-	mapp->range.end = vma->vm_end;
-	mapp->range.default_flags = HMM_PFN_REQ_FAULT;
-	mapp->abo = abo;
-	kref_init(&mapp->refcnt);
-
-	INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work);
-
  	down_write(&xdna->notifier_lock);
  	if (list_empty(&abo->mem.umap_list))
  		abo->mem.uva = addr;
author maintainer Applied
#2 lore raw

Applied to drm-misc-next

Quoted 54 lines · Max Zhen
Quoted 9 lines · Lizhi Hou

The map structure is currently only partially initialized when the notifier is registered. Although holding mmap_write_lock() prevents the notifier callback from running before the remaining initialization is complete, the ordering can be confusing during code review.

Fully initialize the map structure before registering the notifier so that the structure is completely constructed before it is published.

Quoted 40 lines

---
  drivers/accel/amdxdna/amdxdna_gem.c | 18 +++++++++---------

  1 file changed, 9 insertions(+), 9 deletions(-)
drivers/accel/amdxdna/amdxdna_gem.c +8 -0
index 5a1a2ee261b7..e861db6f9369 100644
@@ -415,6 +415,15 @@ static int amdxdna_hmm_register(struct 
 amdxdna_gem_obj *abo,
           goto free_map;
       }
   +    mapp->range.notifier = &mapp->notifier;
+    mapp->range.start = vma->vm_start;
+    mapp->range.end = vma->vm_end;
+    mapp->range.default_flags = HMM_PFN_REQ_FAULT;
+    mapp->abo = abo;
+    kref_init(&mapp->refcnt);
+
+    INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work);
+
       ret = mmu_interval_notifier_insert_locked(&mapp->notifier,
                             current->mm,

                            addr,

drivers/accel/amdxdna/amdxdna_gem.c +0 -8
@@ -425,15 +434,6 @@ static int amdxdna_hmm_register(struct 
 amdxdna_gem_obj *abo,
           goto free_pfns;
       }
   -    mapp->range.notifier = &mapp->notifier;
-    mapp->range.start = vma->vm_start;
-    mapp->range.end = vma->vm_end;
-    mapp->range.default_flags = HMM_PFN_REQ_FAULT;
-    mapp->abo = abo;
-    kref_init(&mapp->refcnt);
-
-    INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work);
-
       down_write(&xdna->notifier_lock);
       if (list_empty(&abo->mem.umap_list))

          abo->mem.uva = addr;