mm/oom_kill: change oom_kill_process() to bump ->mm_count rather than ->mm_users
Subsystems (2)
PATCH 1/1 mm/oom_kill: change oom_kill_process() to bump ->mm_count rather than ->mm_users
oom_kill_process() does atomic_inc(&mm->mm_users) to ensure that this ->mm can't go away and this is wrong, change it to rely on ->mm_count and mmdrop().
Firstly, we do not want to delay exit_mmap/etc if the victim exits before we do mmput(), but this is minor.
More importantly, we simply can not do mmput() in oom_kill_process(), this can deadlock. For example, suppose that access_process_vm(tsk) triggers OOM and oom-killer decides to kill this "tsk". If it exits and does mmput() before us, ksm_exit() called by us may want to want to take the same mmap_sem for writing.
--- mm/oom_kill.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/oom_kill.c +2 -2
@@ -571,7 +571,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,/* Get a reference to safely compare mm after task_unlock(victim) */ mm = victim->mm; -atomic_inc (&mm->mm_users); +atomic_inc (&mm->mm_count);/* * We should send SIGKILL before setting TIF_MEMDIE in order to prevent * the OOM victim from depleting the memory reserves from the user
@@ -609,7 +609,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p, }rcu_read_unlock (); -mmput (mm); +mmdrop (mm);put_task_struct (victim); }#undef K
Signature · 1 line
2.4.3
Quoted 5 lines · Oleg Nesterov
oom_kill_process()doesatomic_inc(&mm->mm_users)to ensure that this ->mm can't go away and this is wrong, change it to rely on ->mm_count andmmdrop().
Quoted 8 lines
Firstly, we do not want to delay exit_mmap/etc if the victim exits before we do
mmput(), but this is minor.More importantly, we simply can not do
mmput()inoom_kill_process(), this can deadlock. For example, suppose thataccess_process_vm(tsk)triggers OOM and oom-killer decides to kill this "tsk". If it exits and doesmmput()before us,ksm_exit()called by us may want to want to take the samemmap_semfor writing.
Self nack to the changelog ;)
I still think the patch is fine, I'll resend it. But the changelog is wrong, in the case above access_process_vm() adds another reference to ->mm_users, so mmput() can never lead to mm_users == 0.
Quoted 30 lines
Signed-off-by Oleg Nesterov <[email protected]>--- mm/oom_kill.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/oom_kill.c +2
-2@@ -571,7 +571,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,/* Get a reference to safely compare mm after task_unlock(victim) */ mm = victim->mm; -atomic_inc (&mm->mm_users); +atomic_inc (&mm->mm_count);/* * We should send SIGKILL before setting TIF_MEMDIE in order to prevent * the OOM victim from depleting the memory reserves from the user @@ -609,7 +609,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p, }rcu_read_unlock (); -mmput (mm); +mmdrop (mm);put_task_struct (victim); }#undef KSignature · 1 line
2.4.3
Quoted 6 lines · Oleg Nesterov
Self nack to the changelog ;)
I still think the patch is fine, I'll resend it. But the changelog is wrong, in the case above
access_process_vm()adds another reference to ->mm_users, sommput()can never lead tomm_users== 0.
Please see v2 with updated changelog I am going to send. However, somehow I forgot that "mm/oom_kill.c: fix potentially killing unrelated process" still sits in -mm, so I guess it would be better to fold this change into mmoom-fix-potentially-killing-unrelated-process.patch.
Oleg.
PATCH 1/1 mmoom-fix-potentially-killing-unrelated-process-fix
In reply to Oleg Nesterovoom_kill_process() does atomic_inc(&mm->mm_users) to ensure that this ->mm can't go away and this is wrong, change it to rely on ->mm_count and mmdrop().
Firstly, we do not want to delay exit_mmap/etc if the victim exits before we do mmput(), but this is minor.
More importantly, we simply can not do mmput() in oom_kill_process(), this can deadlock if (for example) the caller holds i_mmap_rwsem and mmput() actually leads to exit_mmap(); the victim can have this file mmaped and in this case unmap_vmas/free_pgtables paths will take the same lock for writing. And at least huge_pmd_share() does pmd_alloc() under i_mmap_rwsem because VM_HUGETLB memory is not reclaimable.
--- mm/oom_kill.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/oom_kill.c +2 -2
@@ -571,7 +571,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,/* Get a reference to safely compare mm after task_unlock(victim) */ mm = victim->mm; -atomic_inc (&mm->mm_users); +atomic_inc (&mm->mm_count);/* * We should send SIGKILL before setting TIF_MEMDIE in order to prevent * the OOM victim from depleting the memory reserves from the user
@@ -609,7 +609,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p, }rcu_read_unlock (); -mmput (mm); +mmdrop (mm);put_task_struct (victim); }#undef K
Signature · 1 line
2.4.3
Re: [PATCH -mm] mmoom-fix-potentially-killing-unrelated-process-fix
Quoted 7 lines · Oleg Nesterov
oom_kill_process()doesatomic_inc(&mm->mm_users)to ensure that this ->mm can't go away and this is wrong, change it to rely on ->mm_count andmmdrop().Firstly, we do not want to delay exit_mmap/etc if the victim exits before we do
mmput(), but this is minor.
Quoted 6 lines
More importantly, we simply can not do
mmput()inoom_kill_process(), this can deadlock if (for example) the caller holdsi_mmap_rwsemandmmput()actually leads toexit_mmap(); the victim can have this file mmaped and in this case unmap_vmas/free_pgtables paths will take the same lock for writing. And at leasthuge_pmd_share()doespmd_alloc()underi_mmap_rwsembecauseVM_HUGETLBmemory is not reclaimable.
Ouch, I have completely missed this during review! Thanks for catching this. On the second thought it is clear now. We really want to pin the mm_struct not the address space.
Quoted 1 line
Signed-off-by Oleg Nesterov <[email protected]>
Quoted 29 lines
--- mm/oom_kill.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/oom_kill.c +2
-2@@ -571,7 +571,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,/* Get a reference to safely compare mm after task_unlock(victim) */ mm = victim->mm; -atomic_inc (&mm->mm_users); +atomic_inc (&mm->mm_count);/* * We should send SIGKILL before setting TIF_MEMDIE in order to prevent * the OOM victim from depleting the memory reserves from the user @@ -609,7 +609,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p, }rcu_read_unlock (); -mmput (mm); +mmdrop (mm);put_task_struct (victim); }#undef KSignature · 1 line
2.4.3
Signature · 2 lines
Michal Hocko SUSE Labs
Hugh Dickins
Acked-byRe: [PATCH -mm] mmoom-fix-potentially-killing-unrelated-process-fix
Quoted 15 lines · Michal Hocko
Quoted 7 lines · Oleg Nesterov
oom_kill_process()doesatomic_inc(&mm->mm_users)to ensure that this ->mm can't go away and this is wrong, change it to rely on ->mm_count andmmdrop().Firstly, we do not want to delay exit_mmap/etc if the victim exits before we do
mmput(), but this is minor.Quoted 6 lines
More importantly, we simply can not do
mmput()inoom_kill_process(), this can deadlock if (for example) the caller holdsi_mmap_rwsemandmmput()actually leads toexit_mmap(); the victim can have this file mmaped and in this case unmap_vmas/free_pgtables paths will take the same lock for writing. And at leasthuge_pmd_share()doespmd_alloc()underi_mmap_rwsembecauseVM_HUGETLBmemory is not reclaimable.
Quoted 7 lines
Ouch, I have completely missed this during review! Thanks for catching this. On the second thought it is clear now. We really want to pin the
mm_structnot the address space.Quoted 1 line
Signed-off-by Oleg Nesterov <[email protected]>Acked-by Michal Hocko <[email protected]>
Thanks: looks like this is what was behind recent trinity/KSM deadlock,
https://lkml.org/lkml/2015/10/1/563
Quoted 34 lines
Quoted 30 lines
--- mm/oom_kill.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/oom_kill.c +2
-2@@ -571,7 +571,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p,/* Get a reference to safely compare mm after task_unlock(victim) */ mm = victim->mm; -atomic_inc (&mm->mm_users); +atomic_inc (&mm->mm_count);/* * We should send SIGKILL before setting TIF_MEMDIE in order to prevent * the OOM victim from depleting the memory reserves from the user @@ -609,7 +609,7 @@ void oom_kill_process(struct oom_control *oc, struct task_struct *p, }rcu_read_unlock (); -mmput (mm); +mmdrop (mm);put_task_struct (victim); }#undef KSignature · 1 line
2.4.3Signature · 2 lines
Michal Hocko SUSE Labs