aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/fork.c
diff options
context:
space:
mode:
authorYing Han <yinghan@google.com>2010-10-26 17:21:23 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-10-26 19:52:05 -0400
commit3d5992d2ac7dc09aed8ab537cba074589f0f0a52 (patch)
tree0fbb51026e8aa13ddd7a6516c04471ceb97bc277 /kernel/fork.c
parent0f4d208f1975f16f269134cee5f44c1f048581da (diff)
oom: add per-mm oom disable count
It's pointless to kill a task if another thread sharing its mm cannot be killed to allow future memory freeing. A subsequent patch will prevent kills in such cases, but first it's necessary to have a way to flag a task that shares memory with an OOM_DISABLE task that doesn't incur an additional tasklist scan, which would make select_bad_process() an O(n^2) function. This patch adds an atomic counter to struct mm_struct that follows how many threads attached to it have an oom_score_adj of OOM_SCORE_ADJ_MIN. They cannot be killed by the kernel, so their memory cannot be freed in oom conditions. This only requires task_lock() on the task that we're operating on, it does not require mm->mmap_sem since task_lock() pins the mm and the operation is atomic. [rientjes@google.com: changelog and sys_unshare() code] [rientjes@google.com: protect oom_disable_count with task_lock in fork] [rientjes@google.com: use old_mm for oom_disable_count in exec] Signed-off-by: Ying Han <yinghan@google.com> Signed-off-by: David Rientjes <rientjes@google.com> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Cc: Rik van Riel <riel@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'kernel/fork.c')
-rw-r--r--kernel/fork.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/kernel/fork.c b/kernel/fork.c
index c445f8cc408d..e87aaaaf5131 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -65,6 +65,7 @@
65#include <linux/perf_event.h> 65#include <linux/perf_event.h>
66#include <linux/posix-timers.h> 66#include <linux/posix-timers.h>
67#include <linux/user-return-notifier.h> 67#include <linux/user-return-notifier.h>
68#include <linux/oom.h>
68 69
69#include <asm/pgtable.h> 70#include <asm/pgtable.h>
70#include <asm/pgalloc.h> 71#include <asm/pgalloc.h>
@@ -488,6 +489,7 @@ static struct mm_struct * mm_init(struct mm_struct * mm, struct task_struct *p)
488 mm->cached_hole_size = ~0UL; 489 mm->cached_hole_size = ~0UL;
489 mm_init_aio(mm); 490 mm_init_aio(mm);
490 mm_init_owner(mm, p); 491 mm_init_owner(mm, p);
492 atomic_set(&mm->oom_disable_count, 0);
491 493
492 if (likely(!mm_alloc_pgd(mm))) { 494 if (likely(!mm_alloc_pgd(mm))) {
493 mm->def_flags = 0; 495 mm->def_flags = 0;
@@ -741,6 +743,8 @@ good_mm:
741 /* Initializing for Swap token stuff */ 743 /* Initializing for Swap token stuff */
742 mm->token_priority = 0; 744 mm->token_priority = 0;
743 mm->last_interval = 0; 745 mm->last_interval = 0;
746 if (tsk->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
747 atomic_inc(&mm->oom_disable_count);
744 748
745 tsk->mm = mm; 749 tsk->mm = mm;
746 tsk->active_mm = mm; 750 tsk->active_mm = mm;
@@ -1299,8 +1303,13 @@ bad_fork_cleanup_io:
1299bad_fork_cleanup_namespaces: 1303bad_fork_cleanup_namespaces:
1300 exit_task_namespaces(p); 1304 exit_task_namespaces(p);
1301bad_fork_cleanup_mm: 1305bad_fork_cleanup_mm:
1302 if (p->mm) 1306 if (p->mm) {
1307 task_lock(p);
1308 if (p->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1309 atomic_dec(&p->mm->oom_disable_count);
1310 task_unlock(p);
1303 mmput(p->mm); 1311 mmput(p->mm);
1312 }
1304bad_fork_cleanup_signal: 1313bad_fork_cleanup_signal:
1305 if (!(clone_flags & CLONE_THREAD)) 1314 if (!(clone_flags & CLONE_THREAD))
1306 free_signal_struct(p->signal); 1315 free_signal_struct(p->signal);
@@ -1693,6 +1702,10 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
1693 active_mm = current->active_mm; 1702 active_mm = current->active_mm;
1694 current->mm = new_mm; 1703 current->mm = new_mm;
1695 current->active_mm = new_mm; 1704 current->active_mm = new_mm;
1705 if (current->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) {
1706 atomic_dec(&mm->oom_disable_count);
1707 atomic_inc(&new_mm->oom_disable_count);
1708 }
1696 activate_mm(active_mm, new_mm); 1709 activate_mm(active_mm, new_mm);
1697 new_mm = mm; 1710 new_mm = mm;
1698 } 1711 }