Skip to content

Commit 3d5992d

Browse files
yinghantorvalds
authored andcommitted
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>
1 parent 0f4d208 commit 3d5992d

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

fs/exec.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <linux/fsnotify.h>
5555
#include <linux/fs_struct.h>
5656
#include <linux/pipe_fs_i.h>
57+
#include <linux/oom.h>
5758

5859
#include <asm/uaccess.h>
5960
#include <asm/mmu_context.h>
@@ -759,6 +760,10 @@ static int exec_mmap(struct mm_struct *mm)
759760
tsk->mm = mm;
760761
tsk->active_mm = mm;
761762
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+
}
762767
task_unlock(tsk);
763768
arch_pick_mmap_layout(mm);
764769
if (old_mm) {

fs/proc/base.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,21 @@ static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
10471047
return -EACCES;
10481048
}
10491049

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+
10501065
/*
10511066
* Warn that /proc/pid/oom_adj is deprecated, see
10521067
* Documentation/feature-removal-schedule.txt.
@@ -1065,6 +1080,7 @@ static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
10651080
else
10661081
task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
10671082
-OOM_DISABLE;
1083+
task_unlock(task);
10681084
unlock_task_sighand(task, &flags);
10691085
put_task_struct(task);
10701086

@@ -1133,6 +1149,19 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
11331149
return -EACCES;
11341150
}
11351151

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+
}
11361165
task->signal->oom_score_adj = oom_score_adj;
11371166
/*
11381167
* 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,
11431172
else
11441173
task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
11451174
OOM_SCORE_ADJ_MAX;
1175+
task_unlock(task);
11461176
unlock_task_sighand(task, &flags);
11471177
put_task_struct(task);
11481178
return count;

include/linux/mm_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ struct mm_struct {
310310
#ifdef CONFIG_MMU_NOTIFIER
311311
struct mmu_notifier_mm *mmu_notifier_mm;
312312
#endif
313+
/* How many tasks sharing this mm are OOM_DISABLE */
314+
atomic_t oom_disable_count;
313315
};
314316

315317
/* Future-safe accessor for struct mm_struct's cpu_vm_mask. */

kernel/exit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <linux/perf_event.h>
5151
#include <trace/events/sched.h>
5252
#include <linux/hw_breakpoint.h>
53+
#include <linux/oom.h>
5354

5455
#include <asm/uaccess.h>
5556
#include <asm/unistd.h>
@@ -687,6 +688,8 @@ static void exit_mm(struct task_struct * tsk)
687688
enter_lazy_tlb(mm, current);
688689
/* We don't want this task to be frozen prematurely */
689690
clear_freeze_flag(tsk);
691+
if (tsk->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
692+
atomic_dec(&mm->oom_disable_count);
690693
task_unlock(tsk);
691694
mm_update_next_owner(mm);
692695
mmput(mm);

kernel/fork.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include <linux/perf_event.h>
6666
#include <linux/posix-timers.h>
6767
#include <linux/user-return-notifier.h>
68+
#include <linux/oom.h>
6869

6970
#include <asm/pgtable.h>
7071
#include <asm/pgalloc.h>
@@ -488,6 +489,7 @@ static struct mm_struct * mm_init(struct mm_struct * mm, struct task_struct *p)
488489
mm->cached_hole_size = ~0UL;
489490
mm_init_aio(mm);
490491
mm_init_owner(mm, p);
492+
atomic_set(&mm->oom_disable_count, 0);
491493

492494
if (likely(!mm_alloc_pgd(mm))) {
493495
mm->def_flags = 0;
@@ -741,6 +743,8 @@ static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
741743
/* Initializing for Swap token stuff */
742744
mm->token_priority = 0;
743745
mm->last_interval = 0;
746+
if (tsk->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
747+
atomic_inc(&mm->oom_disable_count);
744748

745749
tsk->mm = mm;
746750
tsk->active_mm = mm;
@@ -1299,8 +1303,13 @@ static struct task_struct *copy_process(unsigned long clone_flags,
12991303
bad_fork_cleanup_namespaces:
13001304
exit_task_namespaces(p);
13011305
bad_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);
13031311
mmput(p->mm);
1312+
}
13041313
bad_fork_cleanup_signal:
13051314
if (!(clone_flags & CLONE_THREAD))
13061315
free_signal_struct(p->signal);
@@ -1693,6 +1702,10 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
16931702
active_mm = current->active_mm;
16941703
current->mm = new_mm;
16951704
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+
}
16961709
activate_mm(active_mm, new_mm);
16971710
new_mm = mm;
16981711
}

0 commit comments

Comments
 (0)