aboutsummaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--fs/exec.c5
-rw-r--r--fs/proc/base.c30
-rw-r--r--include/linux/mm_types.h2
-rw-r--r--kernel/exit.c3
-rw-r--r--kernel/fork.c15
5 files changed, 54 insertions, 1 deletions
diff --git a/fs/exec.c b/fs/exec.c
index 6d2b6f936858..3aa75b8888a1 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -54,6 +54,7 @@
54#include <linux/fsnotify.h> 54#include <linux/fsnotify.h>
55#include <linux/fs_struct.h> 55#include <linux/fs_struct.h>
56#include <linux/pipe_fs_i.h> 56#include <linux/pipe_fs_i.h>
57#include <linux/oom.h>
57 58
58#include <asm/uaccess.h> 59#include <asm/uaccess.h>
59#include <asm/mmu_context.h> 60#include <asm/mmu_context.h>
@@ -759,6 +760,10 @@ static int exec_mmap(struct mm_struct *mm)
759 tsk->mm = mm; 760 tsk->mm = mm;
760 tsk->active_mm = mm; 761 tsk->active_mm = mm;
761 activate_mm(active_mm, mm); 762 activate_mm(active_mm, mm);
763 if (old_mm && tsk->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) {
764 atomic_dec(&old_mm->oom_disable_count);
765 atomic_inc(&tsk->mm->oom_disable_count);
766 }
762 task_unlock(tsk); 767 task_unlock(tsk);
763 arch_pick_mmap_layout(mm); 768 arch_pick_mmap_layout(mm);
764 if (old_mm) { 769 if (old_mm) {
diff --git a/fs/proc/base.c b/fs/proc/base.c
index dc5d5f51f3fe..6e50c8e65513 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -1047,6 +1047,21 @@ static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1047 return -EACCES; 1047 return -EACCES;
1048 } 1048 }
1049 1049
1050 task_lock(task);
1051 if (!task->mm) {
1052 task_unlock(task);
1053 unlock_task_sighand(task, &flags);
1054 put_task_struct(task);
1055 return -EINVAL;
1056 }
1057
1058 if (oom_adjust != task->signal->oom_adj) {
1059 if (oom_adjust == OOM_DISABLE)
1060 atomic_inc(&task->mm->oom_disable_count);
1061 if (task->signal->oom_adj == OOM_DISABLE)
1062 atomic_dec(&task->mm->oom_disable_count);
1063 }
1064
1050 /* 1065 /*
1051 * Warn that /proc/pid/oom_adj is deprecated, see 1066 * Warn that /proc/pid/oom_adj is deprecated, see
1052 * Documentation/feature-removal-schedule.txt. 1067 * Documentation/feature-removal-schedule.txt.
@@ -1065,6 +1080,7 @@ static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1065 else 1080 else
1066 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) / 1081 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1067 -OOM_DISABLE; 1082 -OOM_DISABLE;
1083 task_unlock(task);
1068 unlock_task_sighand(task, &flags); 1084 unlock_task_sighand(task, &flags);
1069 put_task_struct(task); 1085 put_task_struct(task);
1070 1086
@@ -1133,6 +1149,19 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1133 return -EACCES; 1149 return -EACCES;
1134 } 1150 }
1135 1151
1152 task_lock(task);
1153 if (!task->mm) {
1154 task_unlock(task);
1155 unlock_task_sighand(task, &flags);
1156 put_task_struct(task);
1157 return -EINVAL;
1158 }
1159 if (oom_score_adj != task->signal->oom_score_adj) {
1160 if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1161 atomic_inc(&task->mm->oom_disable_count);
1162 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1163 atomic_dec(&task->mm->oom_disable_count);
1164 }
1136 task->signal->oom_score_adj = oom_score_adj; 1165 task->signal->oom_score_adj = oom_score_adj;
1137 /* 1166 /*
1138 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is 1167 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
@@ -1143,6 +1172,7 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1143 else 1172 else
1144 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) / 1173 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1145 OOM_SCORE_ADJ_MAX; 1174 OOM_SCORE_ADJ_MAX;
1175 task_unlock(task);
1146 unlock_task_sighand(task, &flags); 1176 unlock_task_sighand(task, &flags);
1147 put_task_struct(task); 1177 put_task_struct(task);
1148 return count; 1178 return count;
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index cb57d657ce4d..bb7288a782fd 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -310,6 +310,8 @@ struct mm_struct {
310#ifdef CONFIG_MMU_NOTIFIER 310#ifdef CONFIG_MMU_NOTIFIER
311 struct mmu_notifier_mm *mmu_notifier_mm; 311 struct mmu_notifier_mm *mmu_notifier_mm;
312#endif 312#endif
313 /* How many tasks sharing this mm are OOM_DISABLE */
314 atomic_t oom_disable_count;
313}; 315};
314 316
315/* Future-safe accessor for struct mm_struct's cpu_vm_mask. */ 317/* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
diff --git a/kernel/exit.c b/kernel/exit.c
index e2bdf37f9fde..894179a32ec1 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -50,6 +50,7 @@
50#include <linux/perf_event.h> 50#include <linux/perf_event.h>
51#include <trace/events/sched.h> 51#include <trace/events/sched.h>
52#include <linux/hw_breakpoint.h> 52#include <linux/hw_breakpoint.h>
53#include <linux/oom.h>
53 54
54#include <asm/uaccess.h> 55#include <asm/uaccess.h>
55#include <asm/unistd.h> 56#include <asm/unistd.h>
@@ -687,6 +688,8 @@ static void exit_mm(struct task_struct * tsk)
687 enter_lazy_tlb(mm, current); 688 enter_lazy_tlb(mm, current);
688 /* We don't want this task to be frozen prematurely */ 689 /* We don't want this task to be frozen prematurely */
689 clear_freeze_flag(tsk); 690 clear_freeze_flag(tsk);
691 if (tsk->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
692 atomic_dec(&mm->oom_disable_count);
690 task_unlock(tsk); 693 task_unlock(tsk);
691 mm_update_next_owner(mm); 694 mm_update_next_owner(mm);
692 mmput(mm); 695 mmput(mm);
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 }