Skip to contentSkip to conversation

drm/amdkfd: change kfd process kref count at creation

PATCH Superseded Xiaogang.Chen 3 participants 9 messages Oct 11, 2024 – Oct 18, 2024

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drm/amdkfd: change kfd process kref count at creation

From: Xiaogang Chen <[email protected]>

kfd process kref count(process->ref) is initialized to 1 by kref_init. After it is created not need to increaes its kref. Instad add kfd process kref at kfd process mmu notifier allocation since we decrease the ref at free_notifier of mmu_notifier_ops, so pair them.

When user process opens kfd node multiple times the kfd process kref is increased each time to balance kfd node close operation.

---
 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -5
index d07acf1b2f93..78bf918abf92 100644
@@ -850,8 +850,10 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
 		goto out;
 	}
 
-	/* A prior open of /dev/kfd could have already created the process. */
-	process = find_process(thread, false);
+	/* A prior open of /dev/kfd could have already created the process.
+	 * find_process will increase process kref in this case
+	 */
+	process = find_process(thread, true);
 	if (process) {
 		pr_debug("Process already found\n");
 	} else {
@@ -899,8 +901,6 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
 		init_waitqueue_head(&process->wait_irq_drain);
 	}
 out:
-	if (!IS_ERR(process))
-		kref_get(&process->ref);
 	mutex_unlock(&kfd_processes_mutex);
 	mmput(thread->mm);
 
@@ -1191,7 +1191,12 @@ static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm)
 
 	srcu_read_unlock(&kfd_processes_srcu, idx);
 
-	return p ? &p->mmu_notifier : ERR_PTR(-ESRCH);
+	if (p) {
+		kref_get(&p->ref);
+		return &p->mmu_notifier;
+	}
+
+	return ERR_PTR(-ESRCH);
 }
 
 static void kfd_process_free_notifier(struct mmu_notifier *mn)
Signature · 1 line
2.25.1
#1 lore raw
Quoted 42 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

kfd process kref count(process->ref) is initialized to 1 by kref_init. After it is created not need to increaes its kref. Instad add kfd process kref at kfd process mmu notifier allocation since we decrease the ref at free_notifier of mmu_notifier_ops, so pair them.

When user process opens kfd node multiple times the kfd process kref is increased each time to balance kfd node close operation.

---
 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -4
index d07acf1b2f93..78bf918abf92 100644
@@ -850,8 +850,10 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
 		goto out;
 	}
 
-	/* A prior open of /dev/kfd could have already created the process. */
-	process = find_process(thread, false);
+	/* A prior open of /dev/kfd could have already created the process.
+	 * find_process will increase process kref in this case
+	 */
+	process = find_process(thread, true);
 	if (process) {
 		pr_debug("Process already found\n");
 	} else {
@@ -899,8 +901,6 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
 		init_waitqueue_head(&process->wait_irq_drain);
 	}
 out:
-	if (!IS_ERR(process))
-		kref_get(&process->ref);
 	mutex_unlock(&kfd_processes_mutex);
 	mmput(thread->mm);
 

drivers/gpu/drm/amd/amdkfd/kfd_process.c static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm) · line 1199

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +6 -1
@@ -1191,7 +1191,12 @@ static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm)
 
 	srcu_read_unlock(&kfd_processes_srcu, idx);
 
-	return p ? &p->mmu_notifier : ERR_PTR(-ESRCH);
+	if (p) {
+		kref_get(&p->ref);
+		return &p->mmu_notifier;
+	}
+
+	return ERR_PTR(-ESRCH);

this cb should only allocate the notifier (here it returns an existing notifier ), so I am not sure this is a better place to increase the kref, it seems coupling two low correlated routines.

kref is decreased in the free notifier, but not mean it has to be increased in alloc notifier.

Thanks
Lingshan

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
 static void kfd_process_free_notifier(struct mmu_notifier *mn)
#2 lore raw
Quoted 57 lines · Zhu Lingshan
Quoted 42 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

kfd process kref count(process->ref) is initialized to 1 by kref_init. After it is created not need to increaes its kref. Instad add kfd process kref at kfd process mmu notifier allocation since we decrease the ref at free_notifier of mmu_notifier_ops, so pair them.

When user process opens kfd node multiple times the kfd process kref is increased each time to balance kfd node close operation.

---
  drivers/gpu/drm/amd/amdkfd/kfd_process.c | 15 ++++++++++-----
  1 file changed, 10 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -4
index d07acf1b2f93..78bf918abf92 100644
@@ -850,8 +850,10 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
  		goto out;
  	}
 
-	/* A prior open of /dev/kfd could have already created the process. */
-	process = find_process(thread, false);
+	/* A prior open of /dev/kfd could have already created the process.
+	 * find_process will increase process kref in this case
+	 */
+	process = find_process(thread, true);
  	if (process) {
  		pr_debug("Process already found\n");
  	} else {
@@ -899,8 +901,6 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
  		init_waitqueue_head(&process->wait_irq_drain);
  	}
  out:
-	if (!IS_ERR(process))
-		kref_get(&process->ref);
  	mutex_unlock(&kfd_processes_mutex);
  	mmput(thread->mm);
 

drivers/gpu/drm/amd/amdkfd/kfd_process.c static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm) · line 1199

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +6 -1
@@ -1191,7 +1191,12 @@ static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm)
 
  	srcu_read_unlock(&kfd_processes_srcu, idx);
 
-	return p ? &p->mmu_notifier : ERR_PTR(-ESRCH);
+	if (p) {
+		kref_get(&p->ref);
+		return &p->mmu_notifier;
+	}
+
+	return ERR_PTR(-ESRCH);

this cb should only allocate the notifier (here it returns an existing notifier ), so I am not sure this is a better place to increase the kref, it seems coupling two low correlated routines.

Quoted 1 line

kref is decreased in the free notifier, but not mean it has to be increased in alloc notifier.

Who referring kfd process should also un-referrer it after finish. Any client should not do un-refer if it did not refer. That keeps balance in clean way.

The current way is using  mmu's free notifier to unref kfref that was added by kfd process creation. Ex: if not use mmu notifier there would be extra kref that prevent release kfd process.

The final kref is same. The patch just makes the balance in a logical way.

Regards

Xiaogang

Quoted 6 lines

Thanks
Lingshan

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
  static void kfd_process_free_notifier(struct mmu_notifier *mn)
#3 lore raw
Quoted 55 lines · Xiaogang
Quoted 53 lines · Zhu Lingshan
Quoted 40 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

kfd process kref count(process->ref) is initialized to 1 by kref_init. After it is created not need to increaes its kref. Instad add kfd process kref at kfd process mmu notifier allocation since we decrease the ref at free_notifier of mmu_notifier_ops, so pair them.

When user process opens kfd node multiple times the kfd process kref is increased each time to balance kfd node close operation.

---
  drivers/gpu/drm/amd/amdkfd/kfd_process.c | 15 ++++++++++-----

  1 file changed, 10 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index d07acf1b2f93..78bf918abf92 100644
@@ -850,8 +850,10 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
           goto out;
       }

  -    /* A prior open of /dev/kfd could have already created the process. */ -    process = find_process(thread, false);
+    /* A prior open of /dev/kfd could have already created the process. +     * find_process will increase process kref in this case
+     */
+    process = find_process(thread, true);
      if (process) {
          pr_debug("Process already found\n");
      } else {

drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
@@ -899,8 +901,6 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
           init_waitqueue_head(&process->wait_irq_drain);
       }

  out:
-    if (!IS_ERR(process))
-        kref_get(&process->ref);
      mutex_unlock(&kfd_processes_mutex);
      mmput(thread->mm);
  @@ -1191,7 +1191,12 @@ static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm)

Quoted 8 lines

        srcu_read_unlock(&kfd_processes_srcu, idx); -    return p ? &p->mmu_notifier : ERR_PTR(-ESRCH); +    if (p) {
+        kref_get(&p->ref);
+        return &p->mmu_notifier;
+    }
+
+    return ERR_PTR(-ESRCH);

this cb should only allocate the notifier (here it returns an existing notifier ), so I am not sure this is a better place to increase the kref, it seems coupling two low correlated routines.

Quoted 3 lines
Quoted 1 line

kref is decreased in the free notifier, but not mean it has to be increased in alloc notifier.

Who referring kfd process should also un-referrer it after finish. Any client should not do un-refer if it did not refer. That keeps balance in clean way.

I think we already do so, see any functions call kfd_lookup_process_by_xxx would unref the kref of the kfd_process.

Quoted 2 lines

The current way is using  mmu's free notifier to unref kfref that was added by kfd process creation. Ex: if not use mmu notifier there would be extra kref that prevent release kfd process.

I am not sure this is about paring, current design is to free the last kref when the whole program exits by the mmu free notifier, so it would destroy the kfd_process. MMU free notifier would be certainly invoked since it has been registered.

Thanks
Lingshan

Quoted 12 lines

The final kref is same. The patch just makes the balance in a logical way.

Regards

Xiaogang

Quoted 5 lines

Thanks
Lingshan

Quoted 1 line

    static void kfd_process_free_notifier(struct mmu_notifier *mn)

#4 lore raw
Quoted 55 lines · Zhu Lingshan
Quoted 54 lines · Xiaogang
Quoted 53 lines · Zhu Lingshan
Quoted 40 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

kfd process kref count(process->ref) is initialized to 1 by kref_init. After it is created not need to increaes its kref. Instad add kfd process kref at kfd process mmu notifier allocation since we decrease the ref at free_notifier of mmu_notifier_ops, so pair them.

When user process opens kfd node multiple times the kfd process kref is increased each time to balance kfd node close operation.

---
   drivers/gpu/drm/amd/amdkfd/kfd_process.c | 15 ++++++++++-----
   1 file changed, 10 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -3
index d07acf1b2f93..78bf918abf92 100644
@@ -850,8 +850,10 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
           goto out;
       }
   -    /* A prior open of /dev/kfd could have already created the process. */
-    process = find_process(thread, false);
+    /* A prior open of /dev/kfd could have already created the process.
+     * find_process will increase process kref in this case
+     */
+    process = find_process(thread, true);
       if (process) {
           pr_debug("Process already found\n");
       } else {
@@ -899,8 +901,6 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
           init_waitqueue_head(&process->wait_irq_drain);
       }
   out:
-    if (!IS_ERR(process))
-        kref_get(&process->ref);
       mutex_unlock(&kfd_processes_mutex);
       mmput(thread->mm);
   @@ -1191,7 +1191,12 @@ static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm)
Quoted 8 lines

        srcu_read_unlock(&kfd_processes_srcu, idx); -    return p ? &p->mmu_notifier : ERR_PTR(-ESRCH);
+    if (p) {
+        kref_get(&p->ref);
+        return &p->mmu_notifier;
+    }
+
+    return ERR_PTR(-ESRCH);

this cb should only allocate the notifier (here it returns an existing notifier ), so I am not sure this is a better place to increase the kref, it seems coupling two low correlated routines.

Quoted 6 lines
Quoted 2 lines
Quoted 1 line

kref is decreased in the free notifier, but not mean it has to be increased in alloc notifier.

Who referring kfd process should also un-referrer it after finish. Any client should not do un-refer if it did not refer. That keeps balance in clean way.

I think we already do so, see any functions call kfd_lookup_process_by_xxx would unref the kref of the kfd_process.

Quoted 1 line

The current way is using  mmu's free notifier to unref kfref that was added by kfd process creation. Ex: if not use mmu notifier there would be extra kref that prevent release kfd process.

I am not sure this is about paring, current design is to free the last kref when the whole program exits by the mmu free notifier, so it would destroy the kfd_process. MMU free notifier would be certainly invoked since it has been registered.

This patch is about having "get/put" at correct places, or keeping kref balance in a clean way. We have 'put' kferf at mmu free notifier why not have 'get' kfref at mmu registry(alloc) notifier?

Regards

Xiaogang

Quoted 13 lines

Thanks
Lingshan

Quoted 10 lines

The final kref is same. The patch just makes the balance in a logical way.

Regards

Xiaogang

Quoted 4 lines

Thanks
Lingshan

Quoted 1 line

    static void kfd_process_free_notifier(struct mmu_notifier *mn)

#5 lore raw
Quoted 57 lines · Xiaogang
Quoted 55 lines · Zhu Lingshan
Quoted 54 lines · Xiaogang
Quoted 53 lines · Zhu Lingshan
Quoted 40 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

kfd process kref count(process->ref) is initialized to 1 by kref_init. After it is created not need to increaes its kref. Instad add kfd process kref at kfd process mmu notifier allocation since we decrease the ref at free_notifier of mmu_notifier_ops, so pair them.

When user process opens kfd node multiple times the kfd process kref is increased each time to balance kfd node close operation.

---
   drivers/gpu/drm/amd/amdkfd/kfd_process.c | 15 ++++++++++-----

   1 file changed, 10 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index d07acf1b2f93..78bf918abf92 100644
@@ -850,8 +850,10 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
            goto out;
        }

   -    /* A prior open of /dev/kfd could have already created the process. */ -    process = find_process(thread, false);
+    /* A prior open of /dev/kfd could have already created the process. +     * find_process will increase process kref in this case
+     */
+    process = find_process(thread, true);
       if (process) {
           pr_debug("Process already found\n");
       } else {

drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
@@ -899,8 +901,6 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
            init_waitqueue_head(&process->wait_irq_drain);
        }

   out:
-    if (!IS_ERR(process))
-        kref_get(&process->ref);
       mutex_unlock(&kfd_processes_mutex);
       mmput(thread->mm);
   @@ -1191,7 +1191,12 @@ static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm)

Quoted 8 lines

         srcu_read_unlock(&kfd_processes_srcu, idx); -    return p ? &p->mmu_notifier : ERR_PTR(-ESRCH); +    if (p) {
+        kref_get(&p->ref);
+        return &p->mmu_notifier;
+    }
+
+    return ERR_PTR(-ESRCH);

this cb should only allocate the notifier (here it returns an existing notifier ), so I am not sure this is a better place to increase the kref, it seems coupling two low correlated routines.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

kref is decreased in the free notifier, but not mean it has to be increased in alloc notifier.

Who referring kfd process should also un-referrer it after finish. Any client should not do un-refer if it did not refer. That keeps balance in clean way.

I think we already do so, see any functions call kfd_lookup_process_by_xxx would unref the kref of the kfd_process.

Quoted 1 line

The current way is using  mmu's free notifier to unref kfref that was added by kfd process creation. Ex: if not use mmu notifier there would be extra kref that prevent release kfd process.

I am not sure this is about paring, current design is to free the last kref when the whole program exits by the mmu free notifier, so it would destroy the kfd_process. MMU free notifier would be certainly invoked since it has been registered.

This patch is about having "get/put" at correct places, or keeping kref balance in a clean way. We have 'put' kferf at mmu free notifier why not have 'get' kfref at mmu registry(alloc) notifier?

If we place increasing kref in mmu alloc notifier, it is still increased at kfd_process creation time, actually no difference, but inexplicitly done. Others need to dive into mmu ops to understand. Current approach  actually has a better readability.

MMU alloc notifier is invoked through locking, it locks the whole mm, so better not to add extra dispensable code there.

Current solution runs for years and this change actually does not fix an issue

Thanks
Lingshan

Quoted 18 lines

Regards

Xiaogang

Quoted 13 lines

Thanks
Lingshan

Quoted 10 lines

The final kref is same. The patch just makes the balance in a logical way.

Regards

Xiaogang

Quoted 4 lines

Thanks
Lingshan

Quoted 1 line

     static void kfd_process_free_notifier(struct mmu_notifier *mn)

#6 lore raw
Quoted 57 lines · Zhu Lingshan
Quoted 56 lines · Xiaogang
Quoted 55 lines · Zhu Lingshan
Quoted 54 lines · Xiaogang
Quoted 53 lines · Zhu Lingshan
Quoted 40 lines · Xiaogang.Chen

From: Xiaogang Chen<[email protected]>

kfd process kref count(process->ref) is initialized to 1 by kref_init. After it is created not need to increaes its kref. Instad add kfd process kref at kfd process mmu notifier allocation since we decrease the ref at free_notifier of mmu_notifier_ops, so pair them.

When user process opens kfd node multiple times the kfd process kref is increased each time to balance kfd node close operation.

---
    drivers/gpu/drm/amd/amdkfd/kfd_process.c | 15 ++++++++++-----
    1 file changed, 10 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -3
index d07acf1b2f93..78bf918abf92 100644
@@ -850,8 +850,10 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
            goto out;
        }
    -    /* A prior open of /dev/kfd could have already created the process. */
-    process = find_process(thread, false);
+    /* A prior open of /dev/kfd could have already created the process.
+     * find_process will increase process kref in this case
+     */
+    process = find_process(thread, true);
        if (process) {
            pr_debug("Process already found\n");
        } else {
@@ -899,8 +901,6 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
            init_waitqueue_head(&process->wait_irq_drain);
        }
    out:
-    if (!IS_ERR(process))
-        kref_get(&process->ref);
        mutex_unlock(&kfd_processes_mutex);
        mmput(thread->mm);
    @@ -1191,7 +1191,12 @@ static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm)
Quoted 8 lines

         srcu_read_unlock(&kfd_processes_srcu, idx); -    return p ? &p->mmu_notifier : ERR_PTR(-ESRCH);
+    if (p) {
+        kref_get(&p->ref);
+        return &p->mmu_notifier;
+    }
+
+    return ERR_PTR(-ESRCH);

this cb should only allocate the notifier (here it returns an existing notifier ), so I am not sure this is a better place to increase the kref, it seems coupling two low correlated routines.

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

kref is decreased in the free notifier, but not mean it has to be increased in alloc notifier.

Who referring kfd process should also un-referrer it after finish. Any client should not do un-refer if it did not refer. That keeps balance in clean way.

I think we already do so, see any functions call kfd_lookup_process_by_xxx would unref the kref of the kfd_process.

Quoted 1 line

The current way is using  mmu's free notifier to unref kfref that was added by kfd process creation. Ex: if not use mmu notifier there would be extra kref that prevent release kfd process.

I am not sure this is about paring, current design is to free the last kref when the whole program exits by the mmu free notifier, so it would destroy the kfd_process. MMU free notifier would be certainly invoked since it has been registered.

This patch is about having "get/put" at correct places, or keeping kref balance in a clean way. We have 'put' kferf at mmu free notifier why not have 'get' kfref at mmu registry(alloc) notifier?

If we place increasing kref in mmu alloc notifier, it is still increased at kfd_process creation time, actually no difference, but inexplicitly done. Others need to dive into mmu ops to understand. Current approach  actually has a better readability.

I think this patch has better readability that it pairs kref of kfd_process in "get" and "put". People see the kref got added at "alloc_notifier", and decreased at "free_notifier" in same source file, not need to dive into mmu ops. When people see it got decreased at free_notifier they would wonder why the kref is not increased at alloc_notifier.

Quoted 2 lines

MMU alloc notifier is invoked through locking, it locks the whole mm, so better not to add extra dispensable code there.

The change is adding kref for kfd_process , not mm or mmu_notifier at alloc_notifier. MMU free_notifier is more complicated then alloc notifier. free_notifier is triggered by scru callback and we have kfref updated at free_notifier, why not at alloc_notifier?

Quoted 2 lines

Current solution runs for years and this change actually does not fix an issue

As said this patch is having "get/put" at correct places, or keeping kref balance in a clean way. Do you see any regression?

Regards

Xiaogang

Quoted 19 lines

Thanks
Lingshan

Quoted 16 lines

Regards

Xiaogang

Quoted 12 lines

Thanks
Lingshan

Quoted 10 lines

The final kref is same. The patch just makes the balance in a logical way.

Regards

Xiaogang

Quoted 4 lines

Thanks
Lingshan

Quoted 1 line

     static void kfd_process_free_notifier(struct mmu_notifier *mn)

maintainer
#7 lore raw
In reply to Xiaogang.Chen
Quoted 11 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

kfd process kref count(process->ref) is initialized to 1 by kref_init. After it is created not need to increaes its kref. Instad add kfd process kref at kfd process mmu notifier allocation since we decrease the ref at free_notifier of mmu_notifier_ops, so pair them.

When user process opens kfd node multiple times the kfd process kref is increased each time to balance kfd node close operation.

Thanks, this is an elegant solution, IMO. The reference returned by kfd_create_process comes either from find_process or create_process. And the extra reference that gets released by the free_notifier gets allocated by the alloc_notifier. I think there is a race condition, though. See inline.

Quoted 31 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_process.c | 15 ++++++++++-----
  1 file changed, 10 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -4
index d07acf1b2f93..78bf918abf92 100644
@@ -850,8 +850,10 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
  		goto out;
  	}
 
-	/* A prior open of /dev/kfd could have already created the process. */
-	process = find_process(thread, false);
+	/* A prior open of /dev/kfd could have already created the process.
+	 * find_process will increase process kref in this case
+	 */
+	process = find_process(thread, true);
  	if (process) {
  		pr_debug("Process already found\n");
  	} else {
@@ -899,8 +901,6 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
  		init_waitqueue_head(&process->wait_irq_drain);
  	}
  out:
-	if (!IS_ERR(process))
-		kref_get(&process->ref);
  	mutex_unlock(&kfd_processes_mutex);
  	mmput(thread->mm);
 

drivers/gpu/drm/amd/amdkfd/kfd_process.c static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm) · line 1195

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2 -1
@@ -1191,7 +1191,12 @@ static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm)
 
  	srcu_read_unlock(&kfd_processes_srcu, idx);
 
-	return p ? &p->mmu_notifier : ERR_PTR(-ESRCH);
+	if (p) {
+		kref_get(&p->ref);

This should be inside the srcu. I think you could use kfd_lookup_process_by_mm instead of open-coding the SRCU locking and find_process_by_mm. This does the lookup and reference counting safely already.

Regards,
  Felix

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -0
+		return &p->mmu_notifier;
+	}
+
+	return ERR_PTR(-ESRCH);
  }
 
  static void kfd_process_free_notifier(struct mmu_notifier *mn)
#8 lore raw
Quoted 46 lines · Felix Kuehling
Quoted 11 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

kfd process kref count(process->ref) is initialized to 1 by kref_init. After it is created not need to increaes its kref. Instad add kfd process kref at kfd process mmu notifier allocation since we decrease the ref at free_notifier of mmu_notifier_ops, so pair them.

When user process opens kfd node multiple times the kfd process kref is increased each time to balance kfd node close operation.

Thanks, this is an elegant solution, IMO. The reference returned by kfd_create_process comes either from find_process or create_process. And the extra reference that gets released by the free_notifier gets allocated by the alloc_notifier. I think there is a race condition, though. See inline.

Quoted 29 lines

---
  drivers/gpu/drm/amd/amdkfd/kfd_process.c | 15 ++++++++++-----

  1 file changed, 10 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index d07acf1b2f93..78bf918abf92 100644
@@ -850,8 +850,10 @@ struct kfd_process *kfd_create_process(struct 
 task_struct *thread)
           goto out;
       }

  -    /* A prior open of /dev/kfd could have already created the process. */ -    process = find_process(thread, false);
+    /* A prior open of /dev/kfd could have already created the process. +     * find_process will increase process kref in this case
+     */
+    process = find_process(thread, true);
      if (process) {
          pr_debug("Process already found\n");
      } else {

drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
@@ -899,8 +901,6 @@ struct kfd_process *kfd_create_process(struct 
 task_struct *thread)
           init_waitqueue_head(&process->wait_irq_drain);
       }

  out:
-    if (!IS_ERR(process))
-        kref_get(&process->ref);
      mutex_unlock(&kfd_processes_mutex);

Quoted 9 lines
Quoted 6 lines

      mmput(thread->mm);
  @@ -1191,7 +1191,12 @@ static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm) srcu_read_unlock(&kfd_processes_srcu, idx);
  -    return p ? &p->mmu_notifier : ERR_PTR(-ESRCH);
+    if (p) {
+        kref_get(&p->ref);

This should be inside the srcu. I think you could use kfd_lookup_process_by_mm instead of open-coding the SRCU locking and find_process_by_mm. This does the lookup and reference counting safely already.

ok, understand. Will do it after next week vacation.

Thanks

Xiaogang

Quoted 9 lines

Regards,
  Felix

Quoted 6 lines

+        return &p->mmu_notifier;
+    }
+
+    return ERR_PTR(-ESRCH);
  }
    static void kfd_process_free_notifier(struct mmu_notifier *mn)