drm/amdkfd: change kfd process kref count at creation
Subsystems (3)
PATCH 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.
--- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +5 -3
@@ -899,8 +899,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 +1189,11 @@ 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
Felix Kuehling
maintainerQuoted 6 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
kfd process kref
count(process->ref)is initialized to 1 bykref_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 atfree_notifierofmmu_notifier_ops, so pair them.
That's not correct. kfd_create_process returns a struct kfd_process * reference. That gets stored by the caller in filep->private_data. That requires incrementing the reference count. You can have multiple references to the same struct kfd_process if user mode opens /dev/kfd multiple times. The reference is released in kfd_release. Your change breaks that use case.
The reference released in kfd_process_free_notifier is only one per process and it's the reference created by kref_init.
Regards,
Felix
Quoted 32 lines
Signed-off-by Xiaogang Chen <[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_process.c +5
-3@@ -899,8 +899,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 +1189,11 @@ 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)
Quoted 10 lines · Felix Kuehling
Quoted 6 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
kfd process kref
count(process->ref)is initialized to 1 bykref_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 atfree_notifierofmmu_notifier_ops, so pair them.That's not correct.
kfd_create_processreturns a structkfd_process* reference. That gets stored by the caller infilep->private_data. That requires incrementing the reference count. You can have multiple references to the same structkfd_processif user mode opens/dev/kfdmultiple times. The reference is released inkfd_release. Your change breaks that use case.
ok, if user mode open and close /dev/kfd multiple times(current Thunk only allows user process open the kfd node once) the change will break this use case.
Quoted 1 line
The reference released in
kfd_process_free_notifieris only one per process and it's the reference created bykref_init.
I think we can increase kref if find_process return true(the user process already created kfd process). If find_process return false do not increase kref since kref_init has been set to 1.
Or change find_process(thread, false) to find_process(thread, true) that will increase kref if it finds kfd process has been created.
The idea is to pair kref update between alloc_notifier and free_notifier of mmu_notifier_ops for same process(mm). That would seem natural.
Regards
Xiaogang
Quoted 34 lines
Regards,
FelixQuoted 29 lines
Signed-off-by Xiaogang Chen <[email protected]>---
drivers/gpu/drm/amd/amdkfd/kfd_process.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_process.c +0
-0@@ -899,8 +899,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 +1189,11 @@ static structmmu_notifier*kfd_process_alloc_notifier(structmm_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;
+ }
+ returnERR_PTR(-ESRCH);
}
static voidkfd_process_free_notifier(struct mmu_notifier *mn)
Felix Kuehling
maintainerQuoted 12 lines · Xiaogang
Quoted 10 lines · Felix Kuehling
Quoted 6 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
kfd process kref
count(process->ref)is initialized to 1 bykref_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 atfree_notifierofmmu_notifier_ops, so pair them.That's not correct.
kfd_create_processreturns a structkfd_process* reference. That gets stored by the caller infilep->private_data. That requires incrementing the reference count. You can have multiple references to the same structkfd_processif user mode opens/dev/kfdmultiple times. The reference is released inkfd_release. Your change breaks that use case.
Quoted 8 lines
ok, if user mode open and close
/dev/kfdmultiple times(current Thunk only allows user process open the kfd node once) the change will break this use case.Quoted 1 line
The reference released in
kfd_process_free_notifieris only one per process and it's the reference created bykref_init.I think we can increase kref if
find_processreturn true(the user process already created kfd process). Iffind_processreturn false do not increase kref sincekref_inithas been set to 1.Or change
find_process(thread, false)tofind_process(thread, true)that will increase kref if it finds kfd process has been created.The idea is to pair kref update between
alloc_notifierandfree_notifierofmmu_notifier_opsfor same process(mm). That would seem natural.
What's the problem you're trying to solve? Is it just a cosmetic issue? The MMU notifier is registered in create_process (not kfd_create_process). If you add a kref_get in kfd_process_alloc_notifier you need to kfd_unref_process somewhere in create_process. I don't think it's worth the trouble and only risks introducing more reference counting bugs.
Regards,
Felix
Quoted 40 lines
Regards
Xiaogang
Quoted 34 lines
Regards,
FelixQuoted 29 lines
Signed-off-by Xiaogang Chen <[email protected]>---
drivers/gpu/drm/amd/amdkfd/kfd_process.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_process.c +0
-0@@ -899,8 +899,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 +1189,11 @@ static structmmu_notifier*kfd_process_alloc_notifier(structmm_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;
+ }
+ returnERR_PTR(-ESRCH);
}
static voidkfd_process_free_notifier(struct mmu_notifier *mn)
Quoted 17 lines · Felix Kuehling
Quoted 15 lines · Xiaogang
Quoted 10 lines · Felix Kuehling
Quoted 6 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
kfd process kref
count(process->ref)is initialized to 1 bykref_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 atfree_notifierofmmu_notifier_ops, so pair them.That's not correct.
kfd_create_processreturns a structkfd_process* reference. That gets stored by the caller infilep->private_data. That requires incrementing the reference count. You can have multiple references to the same structkfd_processif user mode opens/dev/kfdmultiple times. The reference is released inkfd_release. Your change breaks that use case.ok, if user mode open and close
/dev/kfdmultiple times(current Thunk only allows user process open the kfd node once) the change will break this use case.Quoted 1 line
The reference released in
kfd_process_free_notifieris only one per process and it's the reference created bykref_init.
Quoted 7 lines
Quoted 5 lines
I think we can increase kref if
find_processreturn true(the user process already created kfd process). Iffind_processreturn false do not increase kref sincekref_inithas been set to 1.Or change
find_process(thread, false)tofind_process(thread, true)that will increase kref if it finds kfd process has been created.The idea is to pair kref update between
alloc_notifierandfree_notifierofmmu_notifier_opsfor same process(mm). That would seem natural.What's the problem you're trying to solve? Is it just a cosmetic issue? The MMU notifier is registered in
create_process(notkfd_create_process). If you add akref_getinkfd_process_alloc_notifieryou need tokfd_unref_processsomewhere increate_process. I don't think it's worth the trouble and only risks introducing more reference counting bugs.
It is for making code cleaner or natural to read. mmu_notifier_get is the last call at create_process. If mmu_notifier_get fail the process is freed: kfree(process). If create_process success kfd_create_process return that process anyway(after create_process kfd_create_process creates sys entries that not affect return created kfd process). The finally result is same that kref is 2: one for kfd process creation, one for mmu notifier allocation.
Regards
Xiaogang
Quoted 44 lines
Regards,
FelixQuoted 40 lines
Regards
Xiaogang
Quoted 34 lines
Regards,
FelixQuoted 29 lines
Signed-off-by Xiaogang Chen <[email protected]>---
drivers/gpu/drm/amd/amdkfd/kfd_process.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_process.c +0
-0@@ -899,8 +899,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 +1189,11 @@ static structmmu_notifier*kfd_process_alloc_notifier(structmm_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;
+ }
+ returnERR_PTR(-ESRCH);
}
static voidkfd_process_free_notifier(struct mmu_notifier *mn)
Felix Kuehling
maintainerQuoted 21 lines · Xiaogang
Quoted 19 lines · Felix Kuehling
Quoted 17 lines · Xiaogang
Quoted 10 lines · Felix Kuehling
Quoted 6 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
kfd process kref
count(process->ref)is initialized to 1 bykref_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 atfree_notifierofmmu_notifier_ops, so pair them.That's not correct.
kfd_create_processreturns a structkfd_process* reference. That gets stored by the caller infilep->private_data. That requires incrementing the reference count. You can have multiple references to the same structkfd_processif user mode opens/dev/kfdmultiple times. The reference is released inkfd_release. Your change breaks that use case.ok, if user mode open and close
/dev/kfdmultiple times(current Thunk only allows user process open the kfd node once) the change will break this use case.Quoted 1 line
The reference released in
kfd_process_free_notifieris only one per process and it's the reference created bykref_init.I think we can increase kref if
find_processreturn true(the user process already created kfd process). Iffind_processreturn false do not increase kref sincekref_inithas been set to 1.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Or change
find_process(thread, false)tofind_process(thread, true)that will increase kref if it finds kfd process has been created.The idea is to pair kref update between
alloc_notifierandfree_notifierofmmu_notifier_opsfor same process(mm). That would seem natural.What's the problem you're trying to solve? Is it just a cosmetic issue? The MMU notifier is registered in
create_process(notkfd_create_process). If you add akref_getinkfd_process_alloc_notifieryou need tokfd_unref_processsomewhere increate_process. I don't think it's worth the trouble and only risks introducing more reference counting bugs.It is for making code cleaner or natural to read.
mmu_notifier_getis the last call atcreate_process. Ifmmu_notifier_getfail the process is freed: kfree(process). Ifcreate_processsuccesskfd_create_processreturn that process anyway(aftercreate_processkfd_create_processcreates sys entries that not affect return created kfd process). The finally result is same that kref is 2: one for kfd process creation, one for mmu notifier allocation.
Currently, when you call kfd_create_process for the first time, it returns with kref=2. One reference for the MMU notifier, and one for file->private_data.
Subsequent invocations of kfd_create_process when the process already exists should increment the kref by one to track the additional reference put into the new file->private_data.
If you can come up with a patch that preserves this logic _and makes the code simpler and more readable_, I will consider approving it. Also keep in mind that your patch would need to be ported to the DKMS branch, where there are two different code paths to support older kernels that don't have mmu_notifier_get/put.
Regards,
Felix
Quoted 49 lines
Regards
Xiaogang
Quoted 44 lines
Regards,
FelixQuoted 40 lines
Regards
Xiaogang
Quoted 34 lines
Regards,
FelixQuoted 29 lines
Signed-off-by Xiaogang Chen <[email protected]>---
drivers/gpu/drm/amd/amdkfd/kfd_process.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_process.c +0
-0@@ -899,8 +899,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 +1189,11 @@ static structmmu_notifier*kfd_process_alloc_notifier(structmm_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;
+ }
+ returnERR_PTR(-ESRCH);
}
static voidkfd_process_free_notifier(struct mmu_notifier *mn)
Quoted 27 lines · Felix Kuehling
Quoted 25 lines · Xiaogang
Quoted 23 lines · Felix Kuehling
Quoted 20 lines · Xiaogang
Quoted 10 lines · Felix Kuehling
Quoted 6 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
kfd process kref
count(process->ref)is initialized to 1 bykref_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 atfree_notifierofmmu_notifier_ops, so pair them.That's not correct.
kfd_create_processreturns a structkfd_process* reference. That gets stored by the caller infilep->private_data. That requires incrementing the reference count. You can have multiple references to the same structkfd_processif user mode opens/dev/kfdmultiple times. The reference is released inkfd_release. Your change breaks that use case.ok, if user mode open and close
/dev/kfdmultiple times(current Thunk only allows user process open the kfd node once) the change will break this use case.Quoted 1 line
The reference released in
kfd_process_free_notifieris only one per process and it's the reference created bykref_init.I think we can increase kref if
find_processreturn true(the user process already created kfd process). Iffind_processreturn false do not increase kref sincekref_inithas been set to 1.Or change
find_process(thread, false)tofind_process(thread, true)that will increase kref if it finds kfd process has been created.The idea is to pair kref update between
alloc_notifierandfree_notifierofmmu_notifier_opsfor same process(mm). That would seem natural.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
What's the problem you're trying to solve? Is it just a cosmetic issue? The MMU notifier is registered in
create_process(notkfd_create_process). If you add akref_getinkfd_process_alloc_notifieryou need tokfd_unref_processsomewhere increate_process. I don't think it's worth the trouble and only risks introducing more reference counting bugs.It is for making code cleaner or natural to read.
mmu_notifier_getis the last call atcreate_process. Ifmmu_notifier_getfail the process is freed: kfree(process). Ifcreate_processsuccesskfd_create_processreturn that process anyway(aftercreate_processkfd_create_processcreates sys entries that not affect return created kfd process). The finally result is same that kref is 2: one for kfd process creation, one for mmu notifier allocation.Currently, when you call
kfd_create_processfor the first time, it returns with kref=2. One reference for the MMU notifier, and one forfile->private_data.Subsequent invocations of
kfd_create_processwhen the process already exists should increment the kref by one to track the additional reference put into the newfile->private_data.
one ways is changing find_process(thread, false) to find_process(thread, true) at kfd_create_process. When kfd process already exist find_process will call kref_get(&p->ref);
Quoted 4 lines
If you can come up with a patch that preserves this logic
_andmakes the code simpler and morereadable_, I will consider approving it. Also keep in mind that your patch would need to be ported to the DKMS branch, where there are two different code paths to support older kernels that don't have mmu_notifier_get/put.
At DKMS branch alloc_notifier and free_notifer either exist together or both not exist. So when HAVE_MMU_NOTIFIER_PUT is defined(new kernel) it is ok.
#ifdef HAVE_MMU_NOTIFIER_PUT
.alloc_notifier = kfd_process_alloc_notifier, .free_notifier = kfd_process_free_notifier, #endif
but when HAVE_MMU_NOTIFIER_PUT is not defined we need change kfd_process_destroy_delayed since since it call kfd_unref_process(p);
static void kfd_process_destroy_delayed(struct rcu_head *rcu)
{
struct kfd_process *p = container_of(rcu, struct kfd_process, rcu);
kfd_unref_process(p);
}
That means if port this patch to dkms branch when HAVE_MMU_NOTIFIER_PUT is not defined(old kernel) we do not need call kfd_process_destroy_delayed or remove mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed) at kfd_process_notifier_release_internal. I think that make thing simpler for old kernel.
So it needs additional handling for old kernel on dkms branch. I do not know who port patch to dkms branch, or I should change that on dkms branch.
Regards
Xiaogang
Quoted 53 lines
Regards,
FelixQuoted 49 lines
Regards
Xiaogang
Quoted 44 lines
Regards,
FelixQuoted 40 lines
Regards
Xiaogang
Quoted 34 lines
Regards,
FelixQuoted 29 lines
Signed-off-by Xiaogang Chen <[email protected]>---
drivers/gpu/drm/amd/amdkfd/kfd_process.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_process.c +0
-0@@ -899,8 +899,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 +1189,11 @@ static structmmu_notifier*kfd_process_alloc_notifier(structmm_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;
+ }
+ returnERR_PTR(-ESRCH);
}
static voidkfd_process_free_notifier(struct mmu_notifier *mn)
Felix Kuehling
maintainerQuoted 50 lines · Xiaogang
Quoted 27 lines · Felix Kuehling
Quoted 25 lines · Xiaogang
Quoted 23 lines · Felix Kuehling
Quoted 20 lines · Xiaogang
Quoted 10 lines · Felix Kuehling
Quoted 6 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
kfd process kref
count(process->ref)is initialized to 1 bykref_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 atfree_notifierofmmu_notifier_ops, so pair them.That's not correct.
kfd_create_processreturns a structkfd_process* reference. That gets stored by the caller infilep->private_data. That requires incrementing the reference count. You can have multiple references to the same structkfd_processif user mode opens/dev/kfdmultiple times. The reference is released inkfd_release. Your change breaks that use case.ok, if user mode open and close
/dev/kfdmultiple times(current Thunk only allows user process open the kfd node once) the change will break this use case.Quoted 1 line
The reference released in
kfd_process_free_notifieris only one per process and it's the reference created bykref_init.I think we can increase kref if
find_processreturn true(the user process already created kfd process). Iffind_processreturn false do not increase kref sincekref_inithas been set to 1.Or change
find_process(thread, false)tofind_process(thread, true)that will increase kref if it finds kfd process has been created.The idea is to pair kref update between
alloc_notifierandfree_notifierofmmu_notifier_opsfor same process(mm). That would seem natural.Quoted 7 lines
Quoted 3 lines
Quoted 1 line
What's the problem you're trying to solve? Is it just a cosmetic issue? The MMU notifier is registered in
create_process(notkfd_create_process). If you add akref_getinkfd_process_alloc_notifieryou need tokfd_unref_processsomewhere increate_process. I don't think it's worth the trouble and only risks introducing more reference counting bugs.It is for making code cleaner or natural to read.
mmu_notifier_getis the last call atcreate_process. Ifmmu_notifier_getfail the process is freed: kfree(process). Ifcreate_processsuccesskfd_create_processreturn that process anyway(aftercreate_processkfd_create_processcreates sys entries that not affect return created kfd process). The finally result is same that kref is 2: one for kfd process creation, one for mmu notifier allocation.Currently, when you call
kfd_create_processfor the first time, it returns with kref=2. One reference for the MMU notifier, and one forfile->private_data.Subsequent invocations of
kfd_create_processwhen the process already exists should increment the kref by one to track the additional reference put into the newfile->private_data.one ways is changing
find_process(thread, false)tofind_process(thread, true)atkfd_create_process. When kfd process already existfind_processwill callkref_get(&p->ref);Quoted 4 lines
If you can come up with a patch that preserves this logic
_andmakes the code simpler and morereadable_, I will consider approving it. Also keep in mind that your patch would need to be ported to the DKMS branch, where there are two different code paths to support older kernels that don't have mmu_notifier_get/put.At DKMS branch
alloc_notifierandfree_notifereither exist together or both not exist. So whenHAVE_MMU_NOTIFIER_PUTis defined(new kernel) it is ok.#ifdef
HAVE_MMU_NOTIFIER_PUT
.alloc_notifier =kfd_process_alloc_notifier, .free_notifier =kfd_process_free_notifier, #endifbut when
HAVE_MMU_NOTIFIER_PUTis not defined we need changekfd_process_destroy_delayedsince since it callkfd_unref_process(p);
Quoted 8 lines
static void
kfd_process_destroy_delayed(struct rcu_head *rcu)
{
structkfd_process*p =container_of(rcu, struct kfd_process, rcu);
kfd_unref_process(p);
}That means if port this patch to dkms branch when
HAVE_MMU_NOTIFIER_PUTis not defined(old kernel) we do not need callkfd_process_destroy_delayedor removemmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed)atkfd_process_notifier_release_internal. I think that make thing simpler for old kernel.
No, we still need to destroy the kref that belongs to the process when the mm_struct is destroyed. We can't do that in kfd_process_notifier_release_internal because it leads to LOCKDEP issues. So we still need kfd_process_destroy_delayed.
Regards,
Felix
Quoted 61 lines
So it needs additional handling for old kernel on dkms branch. I do not know who port patch to dkms branch, or I should change that on dkms branch.
Regards
Xiaogang
Quoted 53 lines
Regards,
FelixQuoted 49 lines
Regards
Xiaogang
Quoted 44 lines
Regards,
FelixQuoted 40 lines
Regards
Xiaogang
Quoted 34 lines
Regards,
FelixQuoted 29 lines
Signed-off-by Xiaogang Chen <[email protected]>---
drivers/gpu/drm/amd/amdkfd/kfd_process.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_process.c +0
-0@@ -899,8 +899,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 +1189,11 @@ static structmmu_notifier*kfd_process_alloc_notifier(structmm_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;
+ }
+ returnERR_PTR(-ESRCH);
}
static voidkfd_process_free_notifier(struct mmu_notifier *mn)
Quoted 55 lines · Felix Kuehling
Quoted 54 lines · Xiaogang
Quoted 27 lines · Felix Kuehling
Quoted 25 lines · Xiaogang
Quoted 23 lines · Felix Kuehling
Quoted 20 lines · Xiaogang
Quoted 10 lines · Felix Kuehling
Quoted 6 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
kfd process kref
count(process->ref)is initialized to 1 bykref_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 atfree_notifierofmmu_notifier_ops, so pair them.That's not correct.
kfd_create_processreturns a structkfd_process* reference. That gets stored by the caller infilep->private_data. That requires incrementing the reference count. You can have multiple references to the same structkfd_processif user mode opens/dev/kfdmultiple times. The reference is released inkfd_release. Your change breaks that use case.ok, if user mode open and close
/dev/kfdmultiple times(current Thunk only allows user process open the kfd node once) the change will break this use case.Quoted 1 line
The reference released in
kfd_process_free_notifieris only one per process and it's the reference created bykref_init.I think we can increase kref if
find_processreturn true(the user process already created kfd process). Iffind_processreturn false do not increase kref sincekref_inithas been set to 1.Or change
find_process(thread, false)tofind_process(thread, true)that will increase kref if it finds kfd process has been created.The idea is to pair kref update between
alloc_notifierandfree_notifierofmmu_notifier_opsfor same process(mm). That would seem natural.Quoted 7 lines
Quoted 3 lines
Quoted 1 line
What's the problem you're trying to solve? Is it just a cosmetic issue? The MMU notifier is registered in
create_process(notkfd_create_process). If you add akref_getinkfd_process_alloc_notifieryou need tokfd_unref_processsomewhere increate_process. I don't think it's worth the trouble and only risks introducing more reference counting bugs.It is for making code cleaner or natural to read.
mmu_notifier_getis the last call atcreate_process. Ifmmu_notifier_getfail the process is freed: kfree(process). Ifcreate_processsuccesskfd_create_processreturn that process anyway(aftercreate_processkfd_create_processcreates sys entries that not affect return created kfd process). The finally result is same that kref is 2: one for kfd process creation, one for mmu notifier allocation.Currently, when you call
kfd_create_processfor the first time, it returns with kref=2. One reference for the MMU notifier, and one forfile->private_data.Subsequent invocations of
kfd_create_processwhen the process already exists should increment the kref by one to track the additional reference put into the newfile->private_data.one ways is changing
find_process(thread, false)tofind_process(thread, true)atkfd_create_process. When kfd process already existfind_processwill callkref_get(&p->ref);Quoted 4 lines
If you can come up with a patch that preserves this logic
_andmakes the code simpler and morereadable_, I will consider approving it. Also keep in mind that your patch would need to be ported to the DKMS branch, where there are two different code paths to support older kernels that don't have mmu_notifier_get/put.At DKMS branch
alloc_notifierandfree_notifereither exist together or both not exist. So whenHAVE_MMU_NOTIFIER_PUTis defined(new kernel) it is ok.#ifdef
HAVE_MMU_NOTIFIER_PUT
.alloc_notifier =kfd_process_alloc_notifier, .free_notifier =kfd_process_free_notifier, #endifbut when
HAVE_MMU_NOTIFIER_PUTis not defined we need changekfd_process_destroy_delayedsince since it callkfd_unref_process(p);static void
kfd_process_destroy_delayed(struct rcu_head *rcu)
{
structkfd_process*p =container_of(rcu, struc mmu_notifier_registert kfd_process, rcu);
Quoted 6 lines
Quoted 4 lines
kfd_unref_process(p);
}That means if port this patch to dkms branch when
HAVE_MMU_NOTIFIER_PUTis not defined(old kernel) we do not need callkfd_process_destroy_delayedor removemmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed)atkfd_process_notifier_release_internal. I think that make thing simpler for old kernel.No, we still need to destroy the kref that belongs to the process when the
mm_structis destroyed. We can't do that inkfd_process_notifier_release_internalbecause it leads to LOCKDEP issues. So we still needkfd_process_destroy_delayed.
For old kernel we use mmu_notifier_register that does not update kfd process kref. It register process->mmu_notifier to mmu. The release function of process->mmu_notifier got called when mm structure got destroyed.
The new kernel uses get/put flow. At kfd process creation we use mmu_notifier_get that calls alloc_notifier. So this patch increases kfd process kref at alloc_notifier since mmu refers kfd process and we already decrease the kref at free_notifier.
For old kernel, when port this patch to dkms branch we either do not decrease kfd process kref during mm destruction since we did not increase kfd process kref during creation, or manually increase this kref after mmu_notifier_register, then keep kfd_process_destroy_delayed.
Regards
Xiaogang
Quoted 66 lines
Regards,
FelixQuoted 61 lines
So it needs additional handling for old kernel on dkms branch. I do not know who port patch to dkms branch, or I should change that on dkms branch.
Regards
Xiaogang
Quoted 53 lines
Regards,
FelixQuoted 49 lines
Regards
Xiaogang
Quoted 44 lines
Regards,
FelixQuoted 40 lines
Regards
Xiaogang
Quoted 34 lines
Regards,
FelixQuoted 29 lines
Signed-off-by Xiaogang Chen <[email protected]>---
drivers/gpu/drm/amd/amdkfd/kfd_process.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_process.c +0
-0@@ -899,8 +899,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 +1189,11 @@ static structmmu_notifier*kfd_process_alloc_notifier(structmm_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;
+ }
+ returnERR_PTR(-ESRCH);
}
static voidkfd_process_free_notifier(struct mmu_notifier *mn)
Felix Kuehling
maintainerQuoted 57 lines · Xiaogang
Quoted 55 lines · Felix Kuehling
Quoted 54 lines · Xiaogang
Quoted 27 lines · Felix Kuehling
Quoted 25 lines · Xiaogang
Quoted 23 lines · Felix Kuehling
Quoted 20 lines · Xiaogang
Quoted 10 lines · Felix Kuehling
Quoted 6 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
kfd process kref
count(process->ref)is initialized to 1 bykref_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 atfree_notifierofmmu_notifier_ops, so pair them.That's not correct.
kfd_create_processreturns a structkfd_process* reference. That gets stored by the caller infilep->private_data. That requires incrementing the reference count. You can have multiple references to the same structkfd_processif user mode opens/dev/kfdmultiple times. The reference is released inkfd_release. Your change breaks that use case.ok, if user mode open and close
/dev/kfdmultiple times(current Thunk only allows user process open the kfd node once) the change will break this use case.Quoted 1 line
The reference released in
kfd_process_free_notifieris only one per process and it's the reference created bykref_init.I think we can increase kref if
find_processreturn true(the user process already created kfd process). Iffind_processreturn false do not increase kref sincekref_inithas been set to 1.Or change
find_process(thread, false)tofind_process(thread, true)that will increase kref if it finds kfd process has been created.The idea is to pair kref update between
alloc_notifierandfree_notifierofmmu_notifier_opsfor same process(mm). That would seem natural.Quoted 7 lines
Quoted 3 lines
Quoted 1 line
What's the problem you're trying to solve? Is it just a cosmetic issue? The MMU notifier is registered in
create_process(notkfd_create_process). If you add akref_getinkfd_process_alloc_notifieryou need tokfd_unref_processsomewhere increate_process. I don't think it's worth the trouble and only risks introducing more reference counting bugs.It is for making code cleaner or natural to read.
mmu_notifier_getis the last call atcreate_process. Ifmmu_notifier_getfail the process is freed: kfree(process). Ifcreate_processsuccesskfd_create_processreturn that process anyway(aftercreate_processkfd_create_processcreates sys entries that not affect return created kfd process). The finally result is same that kref is 2: one for kfd process creation, one for mmu notifier allocation.Currently, when you call
kfd_create_processfor the first time, it returns with kref=2. One reference for the MMU notifier, and one forfile->private_data.Subsequent invocations of
kfd_create_processwhen the process already exists should increment the kref by one to track the additional reference put into the newfile->private_data.one ways is changing
find_process(thread, false)tofind_process(thread, true)atkfd_create_process. When kfd process already existfind_processwill callkref_get(&p->ref);Quoted 4 lines
If you can come up with a patch that preserves this logic
_andmakes the code simpler and morereadable_, I will consider approving it. Also keep in mind that your patch would need to be ported to the DKMS branch, where there are two different code paths to support older kernels that don't have mmu_notifier_get/put.At DKMS branch
alloc_notifierandfree_notifereither exist together or both not exist. So whenHAVE_MMU_NOTIFIER_PUTis defined(new kernel) it is ok.#ifdef
HAVE_MMU_NOTIFIER_PUT
.alloc_notifier =kfd_process_alloc_notifier, .free_notifier =kfd_process_free_notifier, #endifbut when
HAVE_MMU_NOTIFIER_PUTis not defined we need changekfd_process_destroy_delayedsince since it callkfd_unref_process(p);static void
kfd_process_destroy_delayed(struct rcu_head *rcu)
{
structkfd_process*p =container_of(rcu, struc mmu_notifier_registert kfd_process, rcu);
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
kfd_unref_process(p);
}That means if port this patch to dkms branch when
HAVE_MMU_NOTIFIER_PUTis not defined(old kernel) we do not need callkfd_process_destroy_delayedor removemmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed)atkfd_process_notifier_release_internal. I think that make thing simpler for old kernel.No, we still need to destroy the kref that belongs to the process when the
mm_structis destroyed. We can't do that inkfd_process_notifier_release_internalbecause it leads to LOCKDEP issues. So we still needkfd_process_destroy_delayed.For old kernel we use
mmu_notifier_registerthat does not update kfd process kref. It registerprocess->mmu_notifierto mmu. The release function ofprocess->mmu_notifiergot called when mm structure got destroyed.
We register the MMU notifier to handle cleanup when the process is destroyed. There must be a reference that gets decremented when that MMU notifier runs so that the kfd_process structure doesn't get destroyed too early.
Quoted 2 lines
The new kernel uses get/put flow. At kfd process creation we use
mmu_notifier_getthat callsalloc_notifier. So this patch increases kfd process kref atalloc_notifiersince mmu refers kfd process and we already decrease the kref atfree_notifier.
The notifier has the exact same purpose, no matter how it gets registered or allocated.
Currently the reference that is decremented by the MMU notifier is the reference that got initialized by kref_init. I propose to leave it that way for old and new kernels for simplicity.
If you decide to change it, you must ensure that you don't break the process reference counting. If you leak a process reference, we won't cleanup process resources and eventually run out of memory. If you decrement the last process reference too early, you may get use-after-free problems in the MMU notifier or change the semantics for processes that open and close KFD multiple times (e.g. KFDTest).
Regards,
Felix
Quoted 72 lines
For old kernel, when port this patch to dkms branch we either do not decrease kfd process kref during mm destruction since we did not increase kfd process kref during creation, or manually increase this kref after
mmu_notifier_register, then keepkfd_process_destroy_delayed.Regards
Xiaogang
Quoted 65 lines
Regards,
FelixQuoted 60 lines
So it needs additional handling for old kernel on dkms branch. I do not know who port patch to dkms branch, or I should change that on dkms branch.
Regards
Xiaogang
Quoted 52 lines
Regards,
FelixQuoted 48 lines
Regards
Xiaogang
Quoted 43 lines
Regards,
FelixQuoted 39 lines
Regards
Xiaogang
Quoted 33 lines
Regards,
FelixQuoted 28 lines
Signed-off-by Xiaogang Chen <[email protected]>---
drivers/gpu/drm/amd/amdkfd/kfd_process.c| 8 +++++---1 file changed, 5 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_process.c +0
-0@@ -899,8 +899,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 +1189,11 @@ static structmmu_notifier*kfd_process_alloc_notifier(structmm_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;
+ }
+ returnERR_PTR(-ESRCH);
}
static voidkfd_process_free_notifier(struct mmu_notifier *mn)