Skip to contentSkip to conversation

Kill existing current task quickly

PATCH NAK Minchan Kim 2 participants 5 messages Feb 16, 2010 – Feb 17, 2010

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

PATCH 1/1 Kill existing current task quickly

I am not sure why didn't we break the loop until now. As looking git log, I found it is removed by Nick at b78483a. He mentioned "introduced a problem". If I miss something, pz, correct me.

== CUT_HERE ==

[PATCH -mm] Kill existing current task quickly

If we found current task is existing but didn't set TIF_MEMDIE during OOM victim selection, let's stop unnecessary looping for getting high badness score task and go ahead for killing current.

This patch would make side effect skip OOM_DISABLE test. But It's okay since the task is existing and oom_kill_process doesn't show any killing message since __oom_kill_task will interrupt it in oom_kill_process.

---
 mm/oom_kill.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
mm/oom_kill.c +0 -0
index 3618be3..5c21398 100644
@@ -295,6 +295,7 @@ static struct task_struct

*select_bad_process(unsigned long *ppoints,

chosen = p;
*ppoints = ULONG_MAX;
+ break;
}

if (p->signal->oom_adj == OOM_DISABLE)

Signature · 14 lines
1.6.5



-- 
Kind regards,
Minchan Kim


--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 27 lines · Minchan Kim

If we found current task is existing but didn't set TIF_MEMDIE during OOM victim selection, let's stop unnecessary looping for getting high badness score task and go ahead for killing current.

This patch would make side effect skip OOM_DISABLE test. But It's okay since the task is existing and oom_kill_process doesn't show any killing message since __oom_kill_task will interrupt it in oom_kill_process.

---
 mm/oom_kill.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
mm/oom_kill.c +1 -0
index 3618be3..5c21398 100644
@@ -295,6 +295,7 @@ static struct task_struct
 *select_bad_process(unsigned long *ppoints,
 
 			chosen = p;
 			*ppoints = ULONG_MAX;
+			break;
 		}
 
Quoted 1 line

if (p->signal->oom_adj == OOM_DISABLE)

No, we don't want to break because there may be other candidate tasks that have TIF_MEMDIE set that will be detected if we keep scanning. Returning ERR_PTR(-1UL) from select_bad_process() has a special meaning: it means we return to the page allocator without doing anything. We don't want more than one candidate task to ever have TIF_MEMDIE at a time, otherwise they can deplete all memory reserves and not make any forward progress. So we always have to iterate the entire tasklist unless we find an already oom killed task with access to memory reserves (to prevent needlessly killing additional tasks before the first had a chance to exit and free its memory) or a different candidate task is exiting so we'll be freeing memory shortly (or it will be invoking the oom killer itself as current and then get chosen as the victim).

#2 lore raw
Quoted 29 lines · David Rientjes
Quoted 27 lines · Minchan Kim

If we found current task is existing but didn't set TIF_MEMDIE during OOM victim selection, let's stop unnecessary looping for getting high badness score task and go ahead for killing current.

This patch would make side effect skip OOM_DISABLE test. But It's okay since the task is existing and oom_kill_process doesn't show any killing message since __oom_kill_task will interrupt it in oom_kill_process.

---
 mm/oom_kill.c |    1 +

 1 files changed, 1 insertions(+), 0 deletions(-)
mm/oom_kill.c +1 -0
index 3618be3..5c21398 100644
@@ -295,6 +295,7 @@ static struct task_struct
 *select_bad_process(unsigned long *ppoints,
 
                       chosen = p;
                       *ppoints = ULONG_MAX;
+                     break;
               }
 
Quoted 15 lines
Quoted 1 line

              if (p->signal->oom_adj == OOM_DISABLE)

No, we don't want to break because there may be other candidate tasks that have TIF_MEMDIE set that will be detected if we keep scanning.  Returning ERR_PTR(-1UL) from select_bad_process() has a special meaning: it means we return to the page allocator without doing anything.  We don't want more than one candidate task to ever have TIF_MEMDIE at a time, otherwise they can deplete all memory reserves and not make any forward progress.  So we always have to iterate the entire tasklist unless we find an already oom killed task with access to memory reserves (to prevent needlessly killing additional tasks before the first had a chance to exit and free its memory) or a different candidate task is exiting so we'll be freeing memory shortly (or it will be invoking the oom killer itself as current and then get chosen as the victim).

Thanks you very much for the kind explanation, David.

How about this?

I can't use smtp port. so at a loss, I have to send patch by attachment in webmail which would mangle my tab space.
Pz, forgive my wrong behavior.
If God help me, below inlined patch isn't mangled. :)

I think exit_mm's new test about TIF_MEMDIE isn't big overhead. That's because is is on cache line due to exit_signal's pending check. And expand task_lock in __oom_kill_task isn't big, I think. That's because __oom_kill_task isn't called frequently.

-- CUT_HERE --

Date: Wed, 17 Feb 2010 23:59:40 +0900
Subject: [PATCH] [PATCH -mm] Kill existing current task quickly

If we found current task is existing but didn't set TIF_MEMDIE during OOM victim selection, let's stop unnecessary looping for getting high badness score task and go ahead for killing current.

For forkbomb scenarion, there are many processes on system so tasklist scanning time might be much big. Sometime we used to use oom_kill_allocating_task to avoid expensive
task list scanning time.

For it, we have to make sure there are not TIF_MEMDIE's another tasks. This patch introduces nr_memdie for counting TIF_MEMDIE's tasks.

This patch would make side effect skip OOM_DISABLE test. But It's okay since the task is existing and oom_kill_process doesn't show any killing message since __oom_kill_task will interrupt it in oom_kill_process.

Cc David Rientjes <[email protected]>
Cc Nick Piggin <[email protected]>
---
 include/linux/oom.h |    2 ++
 kernel/exit.c       |    3 +++
 mm/oom_kill.c       |   12 +++++++++---
 3 files changed, 14 insertions(+), 3 deletions(-)
include/linux/oom.h +2 -0
index 505aa9e..9babcce 100644
@@ -48,5 +48,7 @@ extern int sysctl_panic_on_oom;
 extern int sysctl_oom_kill_allocating_task;
 extern int sysctl_oom_dump_tasks;
 
+extern unsigned int nr_memdie;
+
 #endif /* __KERNEL__*/
 #endif /* _INCLUDE_LINUX_OOM_H */
kernel/exit.c +3 -0
index c5305fc..932c67b 100644
@@ -50,6 +50,7 @@
 #include <linux/perf_event.h>
 #include <trace/events/sched.h>
 #include <linux/hw_breakpoint.h>
+#include <linux/oom.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -685,6 +686,8 @@ static void exit_mm(struct task_struct * tsk)
        /* more a memory barrier than a real lock */
        task_lock(tsk);
        tsk->mm = NULL;
+       if (test_thread_flag(TIF_MEMDIE))
+               nr_memdie--;
        up_read(&mm->mmap_sem);
        enter_lazy_tlb(mm, current);
        /* We don't want this task to be frozen prematurely */
mm/oom_kill.c +2 -0
index 3618be3..d5e3d70 100644
@@ -32,6 +32,8 @@ int sysctl_panic_on_oom;
 int sysctl_oom_kill_allocating_task;
 int sysctl_oom_dump_tasks;
 static DEFINE_SPINLOCK(zone_scan_lock);
+
+unsigned int nr_memdie; /* count of TIF_MEMDIE processes */
 /* #define DEBUG */
 
 /*
@@ -295,6 +297,8 @@ static struct task_struct

*select_bad_process(unsigned long *ppoints,

                        chosen = p;
                        *ppoints = ULONG_MAX;
+                       if (nr_memdie == 0)
+                               break;
                }

                if (p->signal->oom_adj == OOM_DISABLE)
@@ -403,8 +407,6 @@ static void __oom_kill_task(struct task_struct *p,
int verbose)
                       K(p->mm->total_vm),
                       K(get_mm_counter(p->mm, MM_ANONPAGES)),
                       K(get_mm_counter(p->mm, MM_FILEPAGES)));

- task_unlock(p);
-
/*
* We give our sacrificial lamb high priority and access to
* all the memory it needs. That way it should be able to
@@ -412,7 +414,11 @@ static void __oom_kill_task(struct task_struct *p, int verbose)

         */
        p->rt.time_slice = HZ;
        set_tsk_thread_flag(p, TIF_MEMDIE);

-
+ /*
+ * nr_memdie is protected by task_lock.
+ */
+ nr_memdie++;

+       task_unlock(p);
        force_sig(SIGKILL, p);

}

Signature · 7 lines
1.6.0.4



-- 
Kind regards,
Minchan Kim

Date: Wed, 17 Feb 2010 23:59:40 +0900
Subject: [PATCH] [PATCH -mm] Kill existing current task quickly

If we found current task is existing but didn't set TIF_MEMDIE during OOM victim selection, let's stop unnecessary looping for getting high badness score task and go ahead for killing current.

For forkbomb scenarion, there are many processes on system so tasklist scanning time might be much big. Sometime we used to use oom_kill_allocating_task to avoid expensive
task list scanning time.

For it, we have to make sure there are not TIF_MEMDIE's another tasks. This patch introduces nr_memdie for counting TIF_MEMDIE's tasks.

This patch would make side effect skip OOM_DISABLE test. But It's okay since the task is existing and oom_kill_process doesn't show any killing message since __oom_kill_task will interrupt it in oom_kill_process.

Cc David Rientjes <[email protected]>
Cc Nick Piggin <[email protected]>
---
 include/linux/oom.h |    2 ++
 kernel/exit.c       |    3 +++
 mm/oom_kill.c       |   12 +++++++++---
 3 files changed, 14 insertions(+), 3 deletions(-)
include/linux/oom.h +2 -0
index 505aa9e..9babcce 100644
@@ -48,5 +48,7 @@ extern int sysctl_panic_on_oom;
 extern int sysctl_oom_kill_allocating_task;
 extern int sysctl_oom_dump_tasks;
 
+extern unsigned int nr_memdie;
+
 #endif /* __KERNEL__*/
 #endif /* _INCLUDE_LINUX_OOM_H */
kernel/exit.c +3 -0
index c5305fc..932c67b 100644
@@ -50,6 +50,7 @@
 #include <linux/perf_event.h>
 #include <trace/events/sched.h>
 #include <linux/hw_breakpoint.h>
+#include <linux/oom.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -685,6 +686,8 @@ static void exit_mm(struct task_struct * tsk)
 	/* more a memory barrier than a real lock */
 	task_lock(tsk);
 	tsk->mm = NULL;
+	if (test_thread_flag(TIF_MEMDIE))
+		nr_memdie--;
 	up_read(&mm->mmap_sem);
 	enter_lazy_tlb(mm, current);
 	/* We don't want this task to be frozen prematurely */
mm/oom_kill.c +9 -3
index 3618be3..d5e3d70 100644
@@ -32,6 +32,8 @@ int sysctl_panic_on_oom;
 int sysctl_oom_kill_allocating_task;
 int sysctl_oom_dump_tasks;
 static DEFINE_SPINLOCK(zone_scan_lock);
+
+unsigned int nr_memdie; /* count of TIF_MEMDIE processes */
 /* #define DEBUG */
 
 /*
@@ -295,6 +297,8 @@ static struct task_struct *select_bad_process(unsigned long *ppoints,
 
 			chosen = p;
 			*ppoints = ULONG_MAX;
+			if (nr_memdie == 0)
+				break;
 		}
 
 		if (p->signal->oom_adj == OOM_DISABLE)
@@ -403,8 +407,6 @@ static void __oom_kill_task(struct task_struct *p, int verbose)
 		       K(p->mm->total_vm),
 		       K(get_mm_counter(p->mm, MM_ANONPAGES)),
 		       K(get_mm_counter(p->mm, MM_FILEPAGES)));
-	task_unlock(p);
-
 	/*
 	 * We give our sacrificial lamb high priority and access to
 	 * all the memory it needs. That way it should be able to
@@ -412,7 +414,11 @@ static void __oom_kill_task(struct task_struct *p, int verbose)
 	 */
 	p->rt.time_slice = HZ;
 	set_tsk_thread_flag(p, TIF_MEMDIE);
-
+	/*
+	 * nr_memdie is protected by task_lock.
+	 */
+	nr_memdie++;
+	task_unlock(p);
 	force_sig(SIGKILL, p);
 }
 
Signature · 1 line
1.6.0.4
reviewer NAK
#3 lore raw
Quoted 22 lines · Minchan Kim
mm/oom_kill.c +4 -0
index 3618be3..d5e3d70 100644
@@ -32,6 +32,8 @@ int sysctl_panic_on_oom;
 int sysctl_oom_kill_allocating_task;
 int sysctl_oom_dump_tasks;
 static DEFINE_SPINLOCK(zone_scan_lock);
+
+unsigned int nr_memdie; /* count of TIF_MEMDIE processes */
 /* #define DEBUG */
 
 /*
@@ -295,6 +297,8 @@ static struct task_struct
 *select_bad_process(unsigned long *ppoints,
 
                        chosen = p;
                        *ppoints = ULONG_MAX;
+                       if (nr_memdie == 0)
+                               break;
                }
 
Quoted 1 line
                if (p->signal->oom_adj == OOM_DISABLE)

Nack, finding a candidate task with TIF_MEMDIE set is not the only time we return ERR_PTR(-1UL) from select_bad_process(): we also do it if any other task other than current is PF_EXITING. Thus, we _must_ continue the tasklist scan to avoid needlessly killing current simply because it was the first PF_EXITING task in the tasklist.

Quoted 14 lines
mm/oom_kill.c +0 -2
@@ -403,8 +407,6 @@ static void __oom_kill_task(struct task_struct *p,
 int verbose)
                       K(p->mm->total_vm),
                       K(get_mm_counter(p->mm, MM_ANONPAGES)),
                       K(get_mm_counter(p->mm, MM_FILEPAGES)));
-       task_unlock(p);
-
        /*
         * We give our sacrificial lamb high priority and access to

* all the memory it needs. That way it should be able to

mm/oom_kill.c +0 -0
@@ -412,7 +414,11 @@ static void __oom_kill_task(struct task_struct
 *p, int verbose)
         */

mm/oom_kill.c static void __oom_kill_task(struct task_struct · line 424

Quoted 10 lines
mm/oom_kill.c +5 -1
        p->rt.time_slice = HZ;
        set_tsk_thread_flag(p, TIF_MEMDIE);
-
+       /*
+        * nr_memdie is protected by task_lock.
+        */
+       nr_memdie++;
+       task_unlock(p);
        force_sig(SIGKILL, p);
 }

task_lock() is a per-task entity, i.e. each task_struct has an alloc_lock spinlock. This cannot protect a global variable.

#4 lore raw
Quoted 24 lines · David Rientjes
Quoted 22 lines · Minchan Kim
mm/oom_kill.c +4 -0
index 3618be3..d5e3d70 100644
@@ -32,6 +32,8 @@ int sysctl_panic_on_oom;
 int sysctl_oom_kill_allocating_task;
 int sysctl_oom_dump_tasks;
 static DEFINE_SPINLOCK(zone_scan_lock);
+
+unsigned int nr_memdie; /* count of TIF_MEMDIE processes */
 /* #define DEBUG */
 
 /*
@@ -295,6 +297,8 @@ static struct task_struct
 *select_bad_process(unsigned long *ppoints,
 
                        chosen = p;
                        *ppoints = ULONG_MAX;
+                       if (nr_memdie == 0)
+                               break;
                }
 
Quoted 7 lines
Quoted 1 line
                if (p->signal->oom_adj == OOM_DISABLE)

Nack, finding a candidate task with TIF_MEMDIE set is not the only time we return ERR_PTR(-1UL) from select_bad_process(): we also do it if any other task other than current is PF_EXITING. Thus, we _must_ continue the tasklist scan to avoid needlessly killing current simply because it was the first PF_EXITING task in the tasklist.

Okay. Sorry for missing PF_EXITING tasks.

Quoted 20 lines
Quoted 20 lines
mm/oom_kill.c +0 -2
@@ -403,8 +407,6 @@ static void __oom_kill_task(struct task_struct *p,
 int verbose)
                       K(p->mm->total_vm),
                       K(get_mm_counter(p->mm, MM_ANONPAGES)),
                       K(get_mm_counter(p->mm, MM_FILEPAGES)));
-       task_unlock(p);
-
        /*
         * We give our sacrificial lamb high priority and access to

* all the memory it needs. That way it should be able to

mm/oom_kill.c +3 -1
@@ -412,7 +414,11 @@ static void __oom_kill_task(struct task_struct
 *p, int verbose)
         */
        p->rt.time_slice = HZ;
        set_tsk_thread_flag(p, TIF_MEMDIE);
-
+       /*
+        * nr_memdie is protected by task_lock.
+        */
Quoted 8 lines

mm/oom_kill.c static void __oom_kill_task(struct task_struct *p, int verbose) · line 423

Quoted 5 lines
mm/oom_kill.c +2 -0
+       nr_memdie++;
+       task_unlock(p);
        force_sig(SIGKILL, p);
 }

task_lock() is a per-task entity, i.e. each task_struct has an alloc_lock spinlock. This cannot protect a global variable.

Yes. It was utterly dumb lock usage.
Thanks for the quick reply.

Signature · 9 lines
Kind regards,
Minchan Kim


--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>