Skip to contentSkip to conversation

drm/amdkfd: Fixed kfd_process cleanup on module exit.

PATCH Superseded David Belanger 4 participants 10 messages Mar 6, 2023 – Mar 8, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdkfd: Fixed kfd_process cleanup on module exit.

Handle case when module is unloaded (kfd_exit) before a process space (mm_struct) is released.

---
 drivers/gpu/drm/amd/amdkfd/kfd_module.c  |  4 ++
 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 57 ++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_module.c +4 -0
index 09b966dc3768..8ef4bd9e4f7d 100644
@@ -26,6 +26,9 @@
 #include "kfd_priv.h"
 #include "amdgpu_amdkfd.h"
 
+void kfd_cleanup_processes(void);
+
+
 static int kfd_init(void)
 {
 	int err;
@@ -77,6 +80,7 @@ static int kfd_init(void)
 
 static void kfd_exit(void)
 {
+	kfd_cleanup_processes();
 	kfd_debugfs_fini();
 	kfd_process_destroy_wq();
 	kfd_procfs_shutdown();
drivers/gpu/drm/amd/amdkfd/kfd_process.c +57 -0

69 lines not on this page · raw message

Signature · 1 line
2.38.1
maintainer
#1 lore raw

Am 2023-03-06 um 16:58 schrieb David Belanger:

Quoted 72 lines

Handle case when module is unloaded (kfd_exit) before a process space (mm_struct) is released.

---
  drivers/gpu/drm/amd/amdkfd/kfd_module.c  |  4 ++
  drivers/gpu/drm/amd/amdkfd/kfd_process.c | 57 ++++++++++++++++++++++++
  2 files changed, 61 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_module.c +4 -0
index 09b966dc3768..8ef4bd9e4f7d 100644
@@ -26,6 +26,9 @@
  #include "kfd_priv.h"
  #include "amdgpu_amdkfd.h"
 
+void kfd_cleanup_processes(void);
+
+
  static int kfd_init(void)
  {
  	int err;
@@ -77,6 +80,7 @@ static int kfd_init(void)
 
  static void kfd_exit(void)
  {
+	kfd_cleanup_processes();
  	kfd_debugfs_fini();
  	kfd_process_destroy_wq();
  	kfd_procfs_shutdown();
drivers/gpu/drm/amd/amdkfd/kfd_process.c +26 -0
index ebabe92f7edb..b5b28a32639d 100644
@@ -1181,6 +1181,17 @@ static void kfd_process_notifier_release(struct mmu_notifier *mn,
  		return;
 
  	mutex_lock(&kfd_processes_mutex);
+	/*
+	 * Do early return if p is not in the table.
+	 *
+	 * This could potentially happen if this function is called concurrently
+	 * by mmu_notifier and by kfd_cleanup_pocesses.
+	 *
+	 */
+	if (!hash_hashed(&p->kfd_processes)) {
+		mutex_unlock(&kfd_processes_mutex);
+		return;
+	}
  	hash_del_rcu(&p->kfd_processes);
  	mutex_unlock(&kfd_processes_mutex);
  	synchronize_srcu(&kfd_processes_srcu);
@@ -1200,6 +1211,52 @@ static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
  	.free_notifier = kfd_process_free_notifier,
  };
 
+
+void kfd_cleanup_processes(void)
+{
+	struct kfd_process *p;
+	unsigned int temp;
+
+	/*
+	 * Iterate over remaining processes in table, calling notifier release
+	 * to free up notifier and process resources.
+	 *
+	 * This code handles the case when driver is unloaded before all mm_struct
+	 * are released.
+	 */
+	int idx = srcu_read_lock(&kfd_processes_srcu);
+

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { · line 1238

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
+		if (p) {
+			/*
+			 * Obtain a reference on p to avoid a late mmu_notifier release
+			 * call triggering freeing the process.
+			 */
+
+			kref_get(&p->ref);
+
+			srcu_read_unlock(&kfd_processes_srcu, idx);

I don't think it's valid to drop the lock in the middle of the loop. You need to hold the lock throughout the loop to protect the consistency of the data structure. I guess you're doing this because you got a deadlock from synchronize_srcu in kfd_process_notifier_release.

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { · line 1240

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2 -0
+
+			kfd_process_notifier_release(&p->mmu_notifier, p->mm);

This calls hash_del_rcu to remove the process from the hash table. To make this safe, you need to hold the kfd_processes_mutex.

Since this is outside the RCU read lock, the entry in the hlist can be freed, which can cause problems when the hash_for_each_rcu loop tries to find the next entry in the hlist.

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { · line 1242

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2 -0
+
+			kfd_unref_process(p);

This schedules a worker that can free the process at any time, which also frees the hlist_node p->kfd_processes that is still needed by hash_for_each_rcu to find the next entry. If you're unlucky, the worker will be scheduled before the next loop iteration, and you can get a kernel oops.

I suggest a safer strategy: Make a loop using hash_for_each_safe to move all the processes into a new hlist. You can do that while holding the kfd_processes_mutex, so you can safely remove all entries from the hash table and move them into your own private hlist.

Then you can safely release all the processes from your private hlist without having to hold either the srcu read lock or the mutex.

Regards,
  Felix

Quoted 20 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +17 -0
+
+			idx = srcu_read_lock(&kfd_processes_srcu);
+		}
+	}
+	srcu_read_unlock(&kfd_processes_srcu, idx);
+
+	/*
+	 * Must be called after all mmu_notifier_put are done and before
+	 * kfd_process_wq is released.
+	 *
+	 * Ensures that all outstanding free_notifier gets called, triggering the release
+	 * of the process.
+	 */
+	mmu_notifier_synchronize();
+}
+
+
  static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep)
  {
  	unsigned long  offset;
#2 lore raw

[AMD Official Use Only - General]

Thanks Felix for your feedback.
I will work on applying your suggested approach and uploaded a second version when it is ready.

David B.

Quoted 160 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Monday, March 6, 2023 6:46 PM
To: [email protected]; Belanger, David <[email protected]>
Subject: Re: [PATCH] drm/amdkfd: Fixed kfd_process cleanup on module exit.

Am 2023-03-06 um 16:58 schrieb David Belanger:

Quoted 7 lines

Handle case when module is unloaded (kfd_exit) before a process space (mm_struct) is released.

---
  drivers/gpu/drm/amd/amdkfd/kfd_module.c  |  4 ++
  drivers/gpu/drm/amd/amdkfd/kfd_process.c | 57

++++++++++++++++++++++++

drivers/gpu/drm/amd/amdkfd/kfd_process.c static void kfd_process_notifier_release(struct

Quoted 31 lines
  2 files changed, 61 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_module.c +4 -0
index 09b966dc3768..8ef4bd9e4f7d 100644
@@ -26,6 +26,9 @@
  #include "kfd_priv.h"
  #include "amdgpu_amdkfd.h"
 
+void kfd_cleanup_processes(void);
+
+
  static int kfd_init(void)
  {
  	int err;
@@ -77,6 +80,7 @@ static int kfd_init(void)
 
  static void kfd_exit(void)
  {
+	kfd_cleanup_processes();
  	kfd_debugfs_fini();
  	kfd_process_destroy_wq();
  	kfd_procfs_shutdown();
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index ebabe92f7edb..b5b28a32639d 100644
@@ -1181,6 +1181,17 @@ static void kfd_process_notifier_release(struct

mmu_notifier *mn,

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops

Quoted 18 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +11 -0
  		return;
 
  	mutex_lock(&kfd_processes_mutex);
+	/*
+	 * Do early return if p is not in the table.
+	 *
+	 * This could potentially happen if this function is called concurrently
+	 * by mmu_notifier and by kfd_cleanup_pocesses.
+	 *
+	 */
+	if (!hash_hashed(&p->kfd_processes)) {
+		mutex_unlock(&kfd_processes_mutex);
+		return;
+	}
  	hash_del_rcu(&p->kfd_processes);
  	mutex_unlock(&kfd_processes_mutex);
  	synchronize_srcu(&kfd_processes_srcu);
@@ -1200,6 +1211,52 @@ static const struct mmu_notifier_ops

kfd_process_mmu_notifier_ops = {

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -0
  	.free_notifier = kfd_process_free_notifier,
  };
 
+

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+void kfd_cleanup_processes(void)
+{
+	struct kfd_process *p;
+	unsigned int temp;
+
+	/*
+	 * Iterate over remaining processes in table, calling notifier release
+	 * to free up notifier and process resources.
+	 *
+	 * This code handles the case when driver is unloaded before all

mm_struct

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +8 -0
+	 * are released.
+	 */
+	int idx = srcu_read_lock(&kfd_processes_srcu);
+
+	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
+		if (p) {
+			/*
+			 * Obtain a reference on p to avoid a late

mmu_notifier release

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { · line 1238

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +6 -0
+			 * call triggering freeing the process.
+			 */
+
+			kref_get(&p->ref);
+
+			srcu_read_unlock(&kfd_processes_srcu, idx);

I don't think it's valid to drop the lock in the middle of the loop. You need to hold the lock throughout the loop to protect the consistency of the data structure. I guess you're doing this because you got a deadlock from synchronize_srcu in kfd_process_notifier_release.

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops · line 1241

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2 -0
+
+			kfd_process_notifier_release(&p->mmu_notifier, p-
 mm);

This calls hash_del_rcu to remove the process from the hash table. To make this safe, you need to hold the kfd_processes_mutex.

Since this is outside the RCU read lock, the entry in the hlist can be freed, which can cause problems when the hash_for_each_rcu loop tries to find the next entry in the hlist.

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops · line 1243

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2 -0
+
+			kfd_unref_process(p);

This schedules a worker that can free the process at any time, which also frees the hlist_node p->kfd_processes that is still needed by hash_for_each_rcu to find the next entry. If you're unlucky, the worker will be scheduled before the next loop iteration, and you can get a kernel oops.

I suggest a safer strategy: Make a loop using hash_for_each_safe to move all the processes into a new hlist. You can do that while holding the kfd_processes_mutex, so you can safely remove all entries from the hash table and move them into your own private hlist.

Then you can safely release all the processes from your private hlist without having to hold either the srcu read lock or the mutex.

Regards,
  Felix

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +11 -0
+
+			idx = srcu_read_lock(&kfd_processes_srcu);
+		}
+	}
+	srcu_read_unlock(&kfd_processes_srcu, idx);
+
+	/*
+	 * Must be called after all mmu_notifier_put are done and before
+	 * kfd_process_wq is released.
+	 *
+	 * Ensures that all outstanding free_notifier gets called, triggering the

release

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +6 -0
+	 * of the process.
+	 */
+	mmu_notifier_synchronize();
+}
+
+
  static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file

*filep)

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
  {

unsigned long offset;

maintainer
#3 lore raw
In reply to David Belanger

Am 06.03.23 um 22:58 schrieb David Belanger:

Quoted 2 lines

Handle case when module is unloaded (kfd_exit) before a process space (mm_struct) is released.

Well that should never ever happen in the first place. It sounds like we are missing grabbing module references.

Regards,
Christian.

Quoted 104 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_module.c  |  4 ++
  drivers/gpu/drm/amd/amdkfd/kfd_process.c | 57 ++++++++++++++++++++++++
  2 files changed, 61 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_module.c +4 -0
index 09b966dc3768..8ef4bd9e4f7d 100644
@@ -26,6 +26,9 @@
  #include "kfd_priv.h"
  #include "amdgpu_amdkfd.h"
 
+void kfd_cleanup_processes(void);
+
+
  static int kfd_init(void)
  {
  	int err;
@@ -77,6 +80,7 @@ static int kfd_init(void)
 
  static void kfd_exit(void)
  {
+	kfd_cleanup_processes();
  	kfd_debugfs_fini();
  	kfd_process_destroy_wq();
  	kfd_procfs_shutdown();
drivers/gpu/drm/amd/amdkfd/kfd_process.c +57 -0
index ebabe92f7edb..b5b28a32639d 100644
@@ -1181,6 +1181,17 @@ static void kfd_process_notifier_release(struct mmu_notifier *mn,
  		return;
 
  	mutex_lock(&kfd_processes_mutex);
+	/*
+	 * Do early return if p is not in the table.
+	 *
+	 * This could potentially happen if this function is called concurrently
+	 * by mmu_notifier and by kfd_cleanup_pocesses.
+	 *
+	 */
+	if (!hash_hashed(&p->kfd_processes)) {
+		mutex_unlock(&kfd_processes_mutex);
+		return;
+	}
  	hash_del_rcu(&p->kfd_processes);
  	mutex_unlock(&kfd_processes_mutex);
  	synchronize_srcu(&kfd_processes_srcu);
@@ -1200,6 +1211,52 @@ static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
  	.free_notifier = kfd_process_free_notifier,
  };
 
+
+void kfd_cleanup_processes(void)
+{
+	struct kfd_process *p;
+	unsigned int temp;
+
+	/*
+	 * Iterate over remaining processes in table, calling notifier release
+	 * to free up notifier and process resources.
+	 *
+	 * This code handles the case when driver is unloaded before all mm_struct
+	 * are released.
+	 */
+	int idx = srcu_read_lock(&kfd_processes_srcu);
+
+	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
+		if (p) {
+			/*
+			 * Obtain a reference on p to avoid a late mmu_notifier release
+			 * call triggering freeing the process.
+			 */
+
+			kref_get(&p->ref);
+
+			srcu_read_unlock(&kfd_processes_srcu, idx);
+
+			kfd_process_notifier_release(&p->mmu_notifier, p->mm);
+
+			kfd_unref_process(p);
+
+			idx = srcu_read_lock(&kfd_processes_srcu);
+		}
+	}
+	srcu_read_unlock(&kfd_processes_srcu, idx);
+
+	/*
+	 * Must be called after all mmu_notifier_put are done and before
+	 * kfd_process_wq is released.
+	 *
+	 * Ensures that all outstanding free_notifier gets called, triggering the release
+	 * of the process.
+	 */
+	mmu_notifier_synchronize();
+}
+
+
  static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep)
  {
  	unsigned long  offset;
#4 lore raw

[AMD Official Use Only - General]

The test case is a python program that will load the driver, do some operations, then unload the driver. When the driver exists, there is still the python process space around holding on the address space. When the python process space exits, the mmu_notifier gets called but the driver has already been unloaded.

The goal of the fix is to address case where there could be outstanding address space / worker threads for process cleanup that needs to be cleared/completed at exit time.

Regards,
David B.

Quoted 132 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, March 7, 2023 2:05 AM
To: Belanger, David <[email protected]>; amd- [email protected]
Subject: Re: [PATCH] drm/amdkfd: Fixed kfd_process cleanup on module exit.

Am 06.03.23 um 22:58 schrieb David Belanger:

Quoted 2 lines

Handle case when module is unloaded (kfd_exit) before a process space (mm_struct) is released.

Well that should never ever happen in the first place. It sounds like we are missing grabbing module references.

Regards,
Christian.

Quoted 5 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_module.c  |  4 ++
  drivers/gpu/drm/amd/amdkfd/kfd_process.c | 57

++++++++++++++++++++++++

drivers/gpu/drm/amd/amdkfd/kfd_process.c static void kfd_process_notifier_release(struct

Quoted 31 lines
  2 files changed, 61 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_module.c +4 -0
index 09b966dc3768..8ef4bd9e4f7d 100644
@@ -26,6 +26,9 @@
  #include "kfd_priv.h"
  #include "amdgpu_amdkfd.h"
 
+void kfd_cleanup_processes(void);
+
+
  static int kfd_init(void)
  {
  	int err;
@@ -77,6 +80,7 @@ static int kfd_init(void)
 
  static void kfd_exit(void)
  {
+	kfd_cleanup_processes();
  	kfd_debugfs_fini();
  	kfd_process_destroy_wq();
  	kfd_procfs_shutdown();
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index ebabe92f7edb..b5b28a32639d 100644
@@ -1181,6 +1181,17 @@ static void kfd_process_notifier_release(struct

mmu_notifier *mn,

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops

Quoted 18 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +11 -0
  		return;
 
  	mutex_lock(&kfd_processes_mutex);
+	/*
+	 * Do early return if p is not in the table.
+	 *
+	 * This could potentially happen if this function is called concurrently
+	 * by mmu_notifier and by kfd_cleanup_pocesses.
+	 *
+	 */
+	if (!hash_hashed(&p->kfd_processes)) {
+		mutex_unlock(&kfd_processes_mutex);
+		return;
+	}
  	hash_del_rcu(&p->kfd_processes);
  	mutex_unlock(&kfd_processes_mutex);
  	synchronize_srcu(&kfd_processes_srcu);
@@ -1200,6 +1211,52 @@ static const struct mmu_notifier_ops

kfd_process_mmu_notifier_ops = {

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -0
  	.free_notifier = kfd_process_free_notifier,
  };
 
+

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+void kfd_cleanup_processes(void)
+{
+	struct kfd_process *p;
+	unsigned int temp;
+
+	/*
+	 * Iterate over remaining processes in table, calling notifier release
+	 * to free up notifier and process resources.
+	 *
+	 * This code handles the case when driver is unloaded before all

mm_struct

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +8 -0
+	 * are released.
+	 */
+	int idx = srcu_read_lock(&kfd_processes_srcu);
+
+	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
+		if (p) {
+			/*
+			 * Obtain a reference on p to avoid a late

mmu_notifier release

Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +11 -0
+			 * call triggering freeing the process.
+			 */
+
+			kref_get(&p->ref);
+
+			srcu_read_unlock(&kfd_processes_srcu, idx);
+
+			kfd_process_notifier_release(&p->mmu_notifier, p-
 mm);
+
+			kfd_unref_process(p);
+

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+			idx = srcu_read_lock(&kfd_processes_srcu);
+		}
+	}
+	srcu_read_unlock(&kfd_processes_srcu, idx);
+
+	/*
+	 * Must be called after all mmu_notifier_put are done and before
+	 * kfd_process_wq is released.
+	 *
+	 * Ensures that all outstanding free_notifier gets called, triggering the

release

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +6 -0
+	 * of the process.
+	 */
+	mmu_notifier_synchronize();
+}
+
+
  static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file

*filep)

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
  {

unsigned long offset;

#5 lore raw

Am 07.03.23 um 16:28 schrieb Belanger, David:

Quoted 4 lines

[AMD Official Use Only - General]

The test case is a python program that will load the driver, do some operations, then unload the driver.

What do you mean with unloading the driver? Removing the module? Or destroying the device?

Quoted 5 lines

When the driver exists, there is still the python process space around holding on the address space. When the python process space exits, the mmu_notifier gets called but the driver has already been unloaded.

The goal of the fix is to address case where there could be outstanding address space / worker threads for process cleanup that needs to be cleared/completed at exit time.

Yeah and when the module is unloaded this is a completely futile effort.

The general upstream approach is to take references on the struct device and module and prevent unloading as long as those references exists.

The device might be non-functional any more (because for example of hot plug), but the driver should never be unloaded before the python program exits.

Regards,
Christian.

Quoted 134 lines

Regards,
David B.

Quoted 130 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, March 7, 2023 2:05 AM
To: Belanger, David <[email protected]>; amd- [email protected]
Subject: Re: [PATCH] drm/amdkfd: Fixed kfd_process cleanup on module exit.

Am 06.03.23 um 22:58 schrieb David Belanger:

Quoted 2 lines

Handle case when module is unloaded (kfd_exit) before a process space (mm_struct) is released.

Well that should never ever happen in the first place. It sounds like we are missing grabbing module references.

Regards,
Christian.

Quoted 4 lines
---
   drivers/gpu/drm/amd/amdkfd/kfd_module.c  |  4 ++
   drivers/gpu/drm/amd/amdkfd/kfd_process.c | 57

++++++++++++++++++++++++

drivers/gpu/drm/amd/amdkfd/kfd_process.c static void kfd_process_notifier_release(struct

Quoted 31 lines
   2 files changed, 61 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_module.c +4 -0
index 09b966dc3768..8ef4bd9e4f7d 100644
@@ -26,6 +26,9 @@
   #include "kfd_priv.h"
   #include "amdgpu_amdkfd.h"
 
+void kfd_cleanup_processes(void);
+
+
   static int kfd_init(void)
   {
   	int err;
@@ -77,6 +80,7 @@ static int kfd_init(void)
 
   static void kfd_exit(void)
   {
+	kfd_cleanup_processes();
   	kfd_debugfs_fini();
   	kfd_process_destroy_wq();
   	kfd_procfs_shutdown();
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index ebabe92f7edb..b5b28a32639d 100644
@@ -1181,6 +1181,17 @@ static void kfd_process_notifier_release(struct

mmu_notifier *mn,

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops

Quoted 18 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +11 -0
   		return;
 
   	mutex_lock(&kfd_processes_mutex);
+	/*
+	 * Do early return if p is not in the table.
+	 *
+	 * This could potentially happen if this function is called concurrently
+	 * by mmu_notifier and by kfd_cleanup_pocesses.
+	 *
+	 */
+	if (!hash_hashed(&p->kfd_processes)) {
+		mutex_unlock(&kfd_processes_mutex);
+		return;
+	}
   	hash_del_rcu(&p->kfd_processes);
   	mutex_unlock(&kfd_processes_mutex);
   	synchronize_srcu(&kfd_processes_srcu);
@@ -1200,6 +1211,52 @@ static const struct mmu_notifier_ops

kfd_process_mmu_notifier_ops = {

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -0
   	.free_notifier = kfd_process_free_notifier,
   };
 
+

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+void kfd_cleanup_processes(void)
+{
+	struct kfd_process *p;
+	unsigned int temp;
+
+	/*
+	 * Iterate over remaining processes in table, calling notifier release
+	 * to free up notifier and process resources.
+	 *
+	 * This code handles the case when driver is unloaded before all

mm_struct

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +8 -0
+	 * are released.
+	 */
+	int idx = srcu_read_lock(&kfd_processes_srcu);
+
+	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
+		if (p) {
+			/*
+			 * Obtain a reference on p to avoid a late

mmu_notifier release

Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +11 -0
+			 * call triggering freeing the process.
+			 */
+
+			kref_get(&p->ref);
+
+			srcu_read_unlock(&kfd_processes_srcu, idx);
+
+			kfd_process_notifier_release(&p->mmu_notifier, p-
 mm);
+
+			kfd_unref_process(p);
+

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+			idx = srcu_read_lock(&kfd_processes_srcu);
+		}
+	}
+	srcu_read_unlock(&kfd_processes_srcu, idx);
+
+	/*
+	 * Must be called after all mmu_notifier_put are done and before
+	 * kfd_process_wq is released.
+	 *
+	 * Ensures that all outstanding free_notifier gets called, triggering the

release

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +6 -0
+	 * of the process.
+	 */
+	mmu_notifier_synchronize();
+}
+
+
   static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file

*filep)

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
   {
   	unsigned long  offset;
#6 lore raw

[AMD Official Use Only - General]

Quoted 13 lines

-----Original Message-----
From: Christian König <[email protected]>
Sent: Wednesday, March 8, 2023 4:08 AM
To: Belanger, David <[email protected]>; Koenig, Christian <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdkfd: Fixed kfd_process cleanup on module exit.

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Am 07.03.23 um 16:28 schrieb Belanger, David:

Quoted 9 lines
Quoted 4 lines

[AMD Official Use Only - General]

The test case is a python program that will load the driver, do some

operations, then unload the driver.

What do you mean with unloading the driver? Removing the module? Or destroying the device?

The python program calls a shell script that does "modprobe amdgpu". Calls some SMI operation to get some events.
Then it calls a shell scripts that does "modprobe -r amdgpu". Then it exits.

There will be ref on the kfd_process that will remain, which will be released only when mmu_notifier ops->release is called. This does not get called until the python process ends.

The test program is definitively not the typical use a general user would do.

Quoted 9 lines
Quoted 1 line

When the driver exists, there is still the python process space around

holding on the address space.

Quoted 1 line

When the python process space exits, the mmu_notifier gets called but the

driver has already been unloaded.

Quoted 3 lines

The goal of the fix is to address case where there could be outstanding address space / worker threads for process cleanup that needs

to be cleared/completed at exit time.

Quoted 8 lines

Yeah and when the module is unloaded this is a completely futile effort.

The general upstream approach is to take references on the struct device and module and prevent unloading as long as those references exists.

The device might be non-functional any more (because for example of hot plug), but the driver should never be unloaded before the python program exits.

Thank you for your feedback, I will investigate that approach.

Quoted 143 lines

Regards,
Christian.

Quoted 139 lines

Regards,
David B.

Quoted 135 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, March 7, 2023 2:05 AM
To: Belanger, David <[email protected]>; amd- [email protected]
Subject: Re: [PATCH] drm/amdkfd: Fixed kfd_process cleanup on module exit.

Am 06.03.23 um 22:58 schrieb David Belanger:

Quoted 3 lines

Handle case when module is unloaded (kfd_exit) before a process space
(mm_struct) is released.

Well that should never ever happen in the first place. It sounds like we are missing grabbing module references.

Regards,
Christian.

Quoted 4 lines
---
   drivers/gpu/drm/amd/amdkfd/kfd_module.c  |  4 ++
   drivers/gpu/drm/amd/amdkfd/kfd_process.c | 57

++++++++++++++++++++++++

Quoted 31 lines
   2 files changed, 61 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_module.c +4 -0
index 09b966dc3768..8ef4bd9e4f7d 100644
@@ -26,6 +26,9 @@
   #include "kfd_priv.h"
   #include "amdgpu_amdkfd.h"
 
+void kfd_cleanup_processes(void);
+
+
   static int kfd_init(void)
   {
    int err;
@@ -77,6 +80,7 @@ static int kfd_init(void)
 
   static void kfd_exit(void)
   {
+   kfd_cleanup_processes();
    kfd_debugfs_fini();
    kfd_process_destroy_wq();
    kfd_procfs_shutdown();
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index ebabe92f7edb..b5b28a32639d 100644
@@ -1181,6 +1181,17 @@ static void
Quoted 1 line

kfd_process_notifier_release(struct

mmu_notifier *mn,

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops

Quoted 18 lines
            return;

    mutex_lock(&kfd_processes_mutex);
drivers/gpu/drm/amd/amdkfd/kfd_process.c +11 -0
+   /*
+    * Do early return if p is not in the table.
+    *
+    * This could potentially happen if this function is called concurrently
+    * by mmu_notifier and by kfd_cleanup_pocesses.
+    *
+    */
+   if (!hash_hashed(&p->kfd_processes)) {
+           mutex_unlock(&kfd_processes_mutex);
+           return;
+   }
    hash_del_rcu(&p->kfd_processes);
    mutex_unlock(&kfd_processes_mutex);
    synchronize_srcu(&kfd_processes_srcu);
@@ -1200,6 +1211,52 @@ static const struct mmu_notifier_ops

kfd_process_mmu_notifier_ops = {

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -0
    .free_notifier = kfd_process_free_notifier,
   };
 
+

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+void kfd_cleanup_processes(void)
+{
+   struct kfd_process *p;
+   unsigned int temp;
+
+   /*
+    * Iterate over remaining processes in table, calling notifier release
+    * to free up notifier and process resources.
+    *
+    * This code handles the case when driver is unloaded before all

mm_struct

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +8 -0
+    * are released.
+    */
+   int idx = srcu_read_lock(&kfd_processes_srcu);
+
+   hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
+           if (p) {
+                   /*
+                    * Obtain a reference on p to avoid a late

mmu_notifier release

Quoted 14 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +13 -0
+                    * call triggering freeing the process.
+                    */
+
+                   kref_get(&p->ref);
+
+                   srcu_read_unlock(&kfd_processes_srcu, idx);
+
+                   kfd_process_notifier_release(&p->mmu_notifier,
+ p-
 mm);
+
+                   kfd_unref_process(p);
+
+                   idx = srcu_read_lock(&kfd_processes_srcu);

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+           }
+   }
+   srcu_read_unlock(&kfd_processes_srcu, idx);
+
+   /*
+    * Must be called after all mmu_notifier_put are done and before
+    * kfd_process_wq is released.
+    *
+    * Ensures that all outstanding free_notifier gets called,
+ triggering the

release

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +6 -0
+    * of the process.
+    */
+   mmu_notifier_synchronize();
+}
+
+
   static int kfd_process_init_cwsr_apu(struct kfd_process *p,

struct file

*filep)

Quoted 2 lines
   {
    unsigned long  offset;
maintainer
#7 lore raw
In reply to Christian König
Quoted 11 lines · Christian König

Am 07.03.23 um 16:28 schrieb Belanger, David:

Quoted 4 lines

[AMD Official Use Only - General]

The test case is a python program that will load the driver, do some operations, then unload the driver.

What do you mean with unloading the driver? Removing the module? Or destroying the device?

Quoted 3 lines

When the driver exists, there is still the python process space around holding on the address space. When the python process space exits, the mmu_notifier gets called but the driver has already been unloaded.

Quoted 6 lines
Quoted 2 lines

The goal of the fix is to address case where there could be outstanding address space / worker threads for process cleanup that needs to be cleared/completed at exit time.

Yeah and when the module is unloaded this is a completely futile effort.

The general upstream approach is to take references on the struct device and module and prevent unloading as long as those references exists.

That's not how it always works. In case of RCU callbacks, the documented strategy is to use rcu_barrier in the module exit function to ensure the grace period and all callbacks have completed (https://www.kernel.org/doc/html/latest/RCU/rcubarrier.html). mmu_notifier_synchronize is meant to do something similar for pending mmu_notifier_put work (https://elixir.bootlin.com/linux/v6.2.2/source/mm/mmu_notifier.c#L1116).

But this implies that we need to call mmu_notifier_put for all the MMU notifiers registered by the module first. I think closing /dev/kfd drops the module reference count, but the MMU notifiers we register for process cleanup persist until the address space is destroyed. We need to trigger that cleanup for any processes that still exist in that state when the module is unloaded. Or we need to find a way to increment the module refcount for every process that registers a KFD cleanup MMU notifier.

Regards,
  Felix

Quoted 145 lines

The device might be non-functional any more (because for example of hot plug), but the driver should never be unloaded before the python program exits.

Regards,
Christian.

Quoted 137 lines

Regards,
David B.

Quoted 133 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, March 7, 2023 2:05 AM
To: Belanger, David <[email protected]>; amd- [email protected]
Subject: Re: [PATCH] drm/amdkfd: Fixed kfd_process cleanup on module exit.

Am 06.03.23 um 22:58 schrieb David Belanger:

Quoted 2 lines

Handle case when module is unloaded (kfd_exit) before a process space (mm_struct) is released.

Well that should never ever happen in the first place. It sounds like we are missing grabbing module references.

Regards,
Christian.

Quoted 4 lines

---
   drivers/gpu/drm/amd/amdkfd/kfd_module.c  |  4 ++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 57

++++++++++++++++++++++++

drivers/gpu/drm/amd/amdkfd/kfd_process.c static void kfd_process_notifier_release(struct

Quoted 31 lines
   2 files changed, 61 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_module.c +4 -0
index 09b966dc3768..8ef4bd9e4f7d 100644
@@ -26,6 +26,9 @@
    #include "kfd_priv.h"
    #include "amdgpu_amdkfd.h"
 
+void kfd_cleanup_processes(void);
+
+
    static int kfd_init(void)
    {
        int err;
@@ -77,6 +80,7 @@ static int kfd_init(void)
 
    static void kfd_exit(void)
    {
+    kfd_cleanup_processes();
        kfd_debugfs_fini();
        kfd_process_destroy_wq();
        kfd_procfs_shutdown();
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index ebabe92f7edb..b5b28a32639d 100644
@@ -1181,6 +1181,17 @@ static void kfd_process_notifier_release(struct

mmu_notifier *mn,

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops

Quoted 19 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -0
            return;
 
        mutex_lock(&kfd_processes_mutex);
+    /*
+     * Do early return if p is not in the table.
+     *
+     * This could potentially happen if this function is called 

concurrently
+     * by mmu_notifier and by kfd_cleanup_pocesses. +     *
+     */
+    if (!hash_hashed(&p->kfd_processes)) { +        mutex_unlock(&kfd_processes_mutex); +        return;
+    }
       hash_del_rcu(&p->kfd_processes); mutex_unlock(&kfd_processes_mutex); synchronize_srcu(&kfd_processes_srcu);

drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
@@ -1200,6 +1211,52 @@ static const struct mmu_notifier_ops

kfd_process_mmu_notifier_ops = {

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +8 -0
        .free_notifier = kfd_process_free_notifier,
    };
 
+
+void kfd_cleanup_processes(void)
+{
+    struct kfd_process *p;
+    unsigned int temp;
+
+    /*
+     * Iterate over remaining processes in table, calling notifier 
Quoted 4 lines

release
+     * to free up notifier and process resources.
+     *
+     * This code handles the case when driver is unloaded before all

mm_struct

Quoted 8 lines

+     * are released.
+     */
+    int idx = srcu_read_lock(&kfd_processes_srcu);
+
+    hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { +        if (p) {
+            /*
+             * Obtain a reference on p to avoid a late

mmu_notifier release

Quoted 22 lines

+             * call triggering freeing the process. +             */

drivers/gpu/drm/amd/amdkfd/kfd_process.c +6 -0
+
+            kref_get(&p->ref);
+
+            srcu_read_unlock(&kfd_processes_srcu, idx);
+
+ kfd_process_notifier_release(&p->mmu_notifier, p-

mm);

drivers/gpu/drm/amd/amdkfd/kfd_process.c +13 -0
+
+            kfd_unref_process(p);
+
+            idx = srcu_read_lock(&kfd_processes_srcu);
+        }
+    }
+    srcu_read_unlock(&kfd_processes_srcu, idx);
+
+    /*
+     * Must be called after all mmu_notifier_put are done and before
+     * kfd_process_wq is released.
+     *
+     * Ensures that all outstanding free_notifier gets called, 
Quoted 1 line

triggering the

release

Quoted 7 lines

+     * of the process.
+     */
+    mmu_notifier_synchronize();

drivers/gpu/drm/amd/amdkfd/kfd_process.c +3 -0
+}
+
+

   static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file

*filep)

Quoted 2 lines

   {
       unsigned long  offset;

#8 lore raw

Am 08.03.23 um 17:17 schrieb Felix Kuehling:

Quoted 15 lines · Christian König
Quoted 14 lines

Am 07.03.23 um 16:28 schrieb Belanger, David:

Quoted 4 lines

[AMD Official Use Only - General]

The test case is a python program that will load the driver, do some operations, then unload the driver.

What do you mean with unloading the driver? Removing the module? Or destroying the device?

Quoted 5 lines

When the driver exists, there is still the python process space around holding on the address space. When the python process space exits, the mmu_notifier gets called but the driver has already been unloaded.

The goal of the fix is to address case where there could be outstanding address space / worker threads for process cleanup that needs to be cleared/completed at exit time.

Quoted 7 lines
Quoted 3 lines

Yeah and when the module is unloaded this is a completely futile effort.

The general upstream approach is to take references on the struct device and module and prevent unloading as long as those references exists.

That's not how it always works. In case of RCU callbacks, the documented strategy is to use rcu_barrier in the module exit function to ensure the grace period and all callbacks have completed (https://www.kernel.org/doc/html/latest/RCU/rcubarrier.html). mmu_notifier_synchronize is meant to do something similar for pending mmu_notifier_put work (https://elixir.bootlin.com/linux/v6.2.2/source/mm/mmu_notifier.c#L1116).

But this implies that we need to call mmu_notifier_put for all the MMU notifiers registered by the module first. I think closing /dev/kfd drops the module reference count, but the MMU notifiers we register for process cleanup persist until the address space is destroyed. We need to trigger that cleanup for any processes that still exist in that state when the module is unloaded. Or we need to find a way to increment the module refcount for every process that registers a KFD cleanup MMU notifier.

The later is what I've meant. Cleaning up when the module unloads is certainly possible as well, but harder to get right.

And I don't really see an use case that we should do the cleanup way.

Regards,
Christian.

Quoted 151 lines

Regards,
  Felix

Quoted 146 lines

The device might be non-functional any more (because for example of hot plug), but the driver should never be unloaded before the python program exits.

Regards,
Christian.

Quoted 138 lines

Regards,
David B.

Quoted 134 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, March 7, 2023 2:05 AM
To: Belanger, David <[email protected]>; amd- [email protected]
Subject: Re: [PATCH] drm/amdkfd: Fixed kfd_process cleanup on module exit.

Am 06.03.23 um 22:58 schrieb David Belanger:

Quoted 2 lines

Handle case when module is unloaded (kfd_exit) before a process space (mm_struct) is released.

Well that should never ever happen in the first place. It sounds like we are missing grabbing module references.

Regards,
Christian.

Quoted 4 lines

---
   drivers/gpu/drm/amd/amdkfd/kfd_module.c  |  4 ++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 57

++++++++++++++++++++++++

Quoted 31 lines
   2 files changed, 61 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_module.c +4 -0
index 09b966dc3768..8ef4bd9e4f7d 100644
@@ -26,6 +26,9 @@
    #include "kfd_priv.h"
    #include "amdgpu_amdkfd.h"
 
+void kfd_cleanup_processes(void);
+
+
    static int kfd_init(void)
    {
        int err;
@@ -77,6 +80,7 @@ static int kfd_init(void)
 
    static void kfd_exit(void)
    {
+    kfd_cleanup_processes();
        kfd_debugfs_fini();
        kfd_process_destroy_wq();
        kfd_procfs_shutdown();
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index ebabe92f7edb..b5b28a32639d 100644
@@ -1181,6 +1181,17 @@ static void 
Quoted 1 line

kfd_process_notifier_release(struct

mmu_notifier *mn,

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops

Quoted 19 lines

           return;

       mutex_lock(&kfd_processes_mutex);
+    /*
+     * Do early return if p is not in the table.
+     *
+     * This could potentially happen if this function is called concurrently
+     * by mmu_notifier and by kfd_cleanup_pocesses.
+     *
+     */
+    if (!hash_hashed(&p->kfd_processes)) {
+        mutex_unlock(&kfd_processes_mutex);
+        return;
+    }
       hash_del_rcu(&p->kfd_processes); mutex_unlock(&kfd_processes_mutex); synchronize_srcu(&kfd_processes_srcu);

drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
@@ -1200,6 +1211,52 @@ static const struct mmu_notifier_ops

kfd_process_mmu_notifier_ops = {

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +8 -0
        .free_notifier = kfd_process_free_notifier,
    };
 
+
+void kfd_cleanup_processes(void)
+{
+    struct kfd_process *p;
+    unsigned int temp;
+
+    /*
+     * Iterate over remaining processes in table, calling 
Quoted 4 lines

notifier release
+     * to free up notifier and process resources.
+     *
+     * This code handles the case when driver is unloaded before all

mm_struct

Quoted 8 lines

+     * are released.
+     */
+    int idx = srcu_read_lock(&kfd_processes_srcu);
+
+    hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { +        if (p) {
+            /*
+             * Obtain a reference on p to avoid a late

mmu_notifier release

Quoted 22 lines

+             * call triggering freeing the process. +             */

drivers/gpu/drm/amd/amdkfd/kfd_process.c +6 -0
+
+            kref_get(&p->ref);
+
+            srcu_read_unlock(&kfd_processes_srcu, idx);
+
+ kfd_process_notifier_release(&p->mmu_notifier, p-

mm);

drivers/gpu/drm/amd/amdkfd/kfd_process.c +13 -0
+
+            kfd_unref_process(p);
+
+            idx = srcu_read_lock(&kfd_processes_srcu);
+        }
+    }
+    srcu_read_unlock(&kfd_processes_srcu, idx);
+
+    /*
+     * Must be called after all mmu_notifier_put are done and before
+     * kfd_process_wq is released.
+     *
+     * Ensures that all outstanding free_notifier gets called, 
Quoted 1 line

triggering the

release

Quoted 7 lines

+     * of the process.
+     */
+    mmu_notifier_synchronize();

drivers/gpu/drm/amd/amdkfd/kfd_process.c +3 -0
+}
+
+

   static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file

*filep)

Quoted 2 lines

   {
       unsigned long  offset;

maintainer
#9 lore raw
Quoted 18 lines · Christian König

Am 08.03.23 um 17:17 schrieb Felix Kuehling:

Quoted 17 lines · Christian König
Quoted 16 lines

Am 07.03.23 um 16:28 schrieb Belanger, David:

Quoted 4 lines

[AMD Official Use Only - General]

The test case is a python program that will load the driver, do some operations, then unload the driver.

What do you mean with unloading the driver? Removing the module? Or destroying the device?

Quoted 5 lines

When the driver exists, there is still the python process space around holding on the address space. When the python process space exits, the mmu_notifier gets called but the driver has already been unloaded.

The goal of the fix is to address case where there could be outstanding address space / worker threads for process cleanup that needs to be cleared/completed at exit time.

Yeah and when the module is unloaded this is a completely futile effort.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

The general upstream approach is to take references on the struct device and module and prevent unloading as long as those references exists.

That's not how it always works. In case of RCU callbacks, the documented strategy is to use rcu_barrier in the module exit function to ensure the grace period and all callbacks have completed (https://www.kernel.org/doc/html/latest/RCU/rcubarrier.html). mmu_notifier_synchronize is meant to do something similar for pending mmu_notifier_put work (https://elixir.bootlin.com/linux/v6.2.2/source/mm/mmu_notifier.c#L1116).

But this implies that we need to call mmu_notifier_put for all the MMU notifiers registered by the module first. I think closing /dev/kfd drops the module reference count, but the MMU notifiers we register for process cleanup persist until the address space is destroyed. We need to trigger that cleanup for any processes that still exist in that state when the module is unloaded. Or we need to find a way to increment the module refcount for every process that registers a KFD cleanup MMU notifier.

The later is what I've meant. Cleaning up when the module unloads is certainly possible as well, but harder to get right.

I think we can get the cleanup right. I suggested a strategy to David in my code review.

Quoted 2 lines

And I don't really see an use case that we should do the cleanup way.

I'm not sure it's a question of use cases. I see it more as a risk trade-off. If we manually add module refcounts for our cleanup notifiers (try_module_get(THIS_MODULE)/module_put(THIS_MODULE)), there is a risk of leaks that could prevent module unloading, or underflows that could allow the module to be unloaded too early.

I guess this particular test (app trying to unload the module after using KFD) would just fail if we add module refcounts. But I agree that this is not a valid usecase.

Regards,
  Felix

Quoted 159 lines

Regards,
Christian.

Quoted 154 lines

Regards,
  Felix

Quoted 149 lines

The device might be non-functional any more (because for example of hot plug), but the driver should never be unloaded before the python program exits.

Regards,
Christian.

Quoted 141 lines

Regards,
David B.

Quoted 137 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, March 7, 2023 2:05 AM
To: Belanger, David <[email protected]>; amd- [email protected]
Subject: Re: [PATCH] drm/amdkfd: Fixed kfd_process cleanup on module exit.

Am 06.03.23 um 22:58 schrieb David Belanger:

Quoted 2 lines

Handle case when module is unloaded (kfd_exit) before a process space (mm_struct) is released.

Well that should never ever happen in the first place. It sounds like we are missing grabbing module references.

Regards,
Christian.

Quoted 4 lines

---
   drivers/gpu/drm/amd/amdkfd/kfd_module.c  |  4 ++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 57

++++++++++++++++++++++++

Quoted 31 lines
   2 files changed, 61 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_module.c +4 -0
index 09b966dc3768..8ef4bd9e4f7d 100644
@@ -26,6 +26,9 @@
    #include "kfd_priv.h"
    #include "amdgpu_amdkfd.h"
 
+void kfd_cleanup_processes(void);
+
+
    static int kfd_init(void)
    {
        int err;
@@ -77,6 +80,7 @@ static int kfd_init(void)
 
    static void kfd_exit(void)
    {
+    kfd_cleanup_processes();
        kfd_debugfs_fini();
        kfd_process_destroy_wq();
        kfd_procfs_shutdown();
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index ebabe92f7edb..b5b28a32639d 100644
@@ -1181,6 +1181,17 @@ static void 
Quoted 1 line

kfd_process_notifier_release(struct

mmu_notifier *mn,

drivers/gpu/drm/amd/amdkfd/kfd_process.c static const struct mmu_notifier_ops

Quoted 19 lines

           return;

       mutex_lock(&kfd_processes_mutex);
+    /*
+     * Do early return if p is not in the table.
+     *
+     * This could potentially happen if this function is called concurrently
+     * by mmu_notifier and by kfd_cleanup_pocesses.
+     *
+     */
+    if (!hash_hashed(&p->kfd_processes)) {
+        mutex_unlock(&kfd_processes_mutex);
+        return;
+    }
       hash_del_rcu(&p->kfd_processes); mutex_unlock(&kfd_processes_mutex); synchronize_srcu(&kfd_processes_srcu);

drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
@@ -1200,6 +1211,52 @@ static const struct mmu_notifier_ops

kfd_process_mmu_notifier_ops = {

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +8 -0
        .free_notifier = kfd_process_free_notifier,
    };
 
+
+void kfd_cleanup_processes(void)
+{
+    struct kfd_process *p;
+    unsigned int temp;
+
+    /*
+     * Iterate over remaining processes in table, calling 
Quoted 5 lines

notifier release
+     * to free up notifier and process resources.
+     *
+     * This code handles the case when driver is unloaded before all

mm_struct

Quoted 9 lines

+     * are released.
+     */
+    int idx = srcu_read_lock(&kfd_processes_srcu); +
+    hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
+        if (p) {
+            /*
+             * Obtain a reference on p to avoid a late

mmu_notifier release

Quoted 19 lines

+             * call triggering freeing the process. +             */

drivers/gpu/drm/amd/amdkfd/kfd_process.c +6 -0
+
+            kref_get(&p->ref);
+
+            srcu_read_unlock(&kfd_processes_srcu, idx);
+
+ kfd_process_notifier_release(&p->mmu_notifier, p-

mm);

drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+
+            kfd_unref_process(p);
+
+            idx = srcu_read_lock(&kfd_processes_srcu);
+        }
+    }
+    srcu_read_unlock(&kfd_processes_srcu, idx);
+
+    /*
+     * Must be called after all mmu_notifier_put are done and 
Quoted 5 lines

before
+     * kfd_process_wq is released.
+     *
+     * Ensures that all outstanding free_notifier gets called, triggering the

release

Quoted 7 lines

+     * of the process.
+     */
+    mmu_notifier_synchronize();

drivers/gpu/drm/amd/amdkfd/kfd_process.c +3 -0
+}
+
+

   static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file

*filep)

Quoted 2 lines

   {
       unsigned long  offset;