Skip to contentSkip to conversation

mm/oom_kill: change oom_kill_process() to bump ->mm_count rather than ->mm_users

PATCH In review Oleg Nesterov 3 participants 6 messages Oct 5, 2015 – Oct 6, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index 034d219..52abb78 100644
@@ -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
#1 lore raw
Quoted 5 lines · Oleg Nesterov

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().

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() 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.

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
---
 mm/oom_kill.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/oom_kill.c +2 -2
index 034d219..52abb78 100644
@@ -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
#2 lore raw
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, so mmput() can never lead to mm_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.

#3 lore raw

PATCH 1/1 mmoom-fix-potentially-killing-unrelated-process-fix

In reply to Oleg Nesterov

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 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
index 034d219..52abb78 100644
@@ -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
Acked-by on v1 1/1
#4 lore raw

Re: [PATCH -mm] mmoom-fix-potentially-killing-unrelated-process-fix

Quoted 7 lines · Oleg Nesterov

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.

Quoted 6 lines

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.

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
Quoted 29 lines
---
 mm/oom_kill.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/oom_kill.c +2 -2
index 034d219..52abb78 100644
@@ -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
Signature · 2 lines
Michal Hocko
SUSE Labs
Acked-by
#5 lore raw

Re: [PATCH -mm] mmoom-fix-potentially-killing-unrelated-process-fix

Quoted 15 lines · Michal Hocko
Quoted 7 lines · Oleg Nesterov

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.

Quoted 6 lines

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.

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_struct not the address space.

Quoted 1 line

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
index 034d219..52abb78 100644
@@ -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
Signature · 2 lines
Michal Hocko
SUSE Labs