Skip to content

Commit 08ab9b1

Browse files
rientjestorvalds
authored andcommitted
mm, oom: force oom kill on sysrq+f
The oom killer chooses not to kill a thread if: - an eligible thread has already been oom killed and has yet to exit, and - an eligible thread is exiting but has yet to free all its memory and is not the thread attempting to currently allocate memory. SysRq+F manually invokes the global oom killer to kill a memory-hogging task. This is normally done as a last resort to free memory when no progress is being made or to test the oom killer itself. For both uses, we always want to kill a thread and never defer. This patch causes SysRq+F to always kill an eligible thread and can be used to force a kill even if another oom killed thread has failed to exit. Signed-off-by: David Rientjes <rientjes@google.com> Acked-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Acked-by: Pekka Enberg <penberg@kernel.org> Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b764375 commit 08ab9b1

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

drivers/tty/sysrq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ static struct sysrq_key_op sysrq_term_op = {
346346

347347
static void moom_callback(struct work_struct *ignored)
348348
{
349-
out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0, NULL);
349+
out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0, NULL, true);
350350
}
351351

352352
static DECLARE_WORK(moom_work, moom_callback);

include/linux/oom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extern int try_set_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_flags);
4949
extern void clear_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_flags);
5050

5151
extern void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
52-
int order, nodemask_t *mask);
52+
int order, nodemask_t *mask, bool force_kill);
5353
extern int register_oom_notifier(struct notifier_block *nb);
5454
extern int unregister_oom_notifier(struct notifier_block *nb);
5555

mm/oom_kill.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static enum oom_constraint constrained_alloc(struct zonelist *zonelist,
310310
*/
311311
static struct task_struct *select_bad_process(unsigned int *ppoints,
312312
unsigned long totalpages, struct mem_cgroup *memcg,
313-
const nodemask_t *nodemask)
313+
const nodemask_t *nodemask, bool force_kill)
314314
{
315315
struct task_struct *g, *p;
316316
struct task_struct *chosen = NULL;
@@ -336,7 +336,8 @@ static struct task_struct *select_bad_process(unsigned int *ppoints,
336336
if (test_tsk_thread_flag(p, TIF_MEMDIE)) {
337337
if (unlikely(frozen(p)))
338338
__thaw_task(p);
339-
return ERR_PTR(-1UL);
339+
if (!force_kill)
340+
return ERR_PTR(-1UL);
340341
}
341342
if (!p->mm)
342343
continue;
@@ -354,7 +355,7 @@ static struct task_struct *select_bad_process(unsigned int *ppoints,
354355
if (p == current) {
355356
chosen = p;
356357
*ppoints = 1000;
357-
} else {
358+
} else if (!force_kill) {
358359
/*
359360
* If this task is not being ptraced on exit,
360361
* then wait for it to finish before killing
@@ -572,7 +573,7 @@ void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask)
572573
check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, 0, NULL);
573574
limit = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT;
574575
read_lock(&tasklist_lock);
575-
p = select_bad_process(&points, limit, memcg, NULL);
576+
p = select_bad_process(&points, limit, memcg, NULL, false);
576577
if (p && PTR_ERR(p) != -1UL)
577578
oom_kill_process(p, gfp_mask, 0, points, limit, memcg, NULL,
578579
"Memory cgroup out of memory");
@@ -687,14 +688,15 @@ static void clear_system_oom(void)
687688
* @gfp_mask: memory allocation flags
688689
* @order: amount of memory being requested as a power of 2
689690
* @nodemask: nodemask passed to page allocator
691+
* @force_kill: true if a task must be killed, even if others are exiting
690692
*
691693
* If we run out of memory, we have the choice between either
692694
* killing a random task (bad), letting the system crash (worse)
693695
* OR try to be smart about which process to kill. Note that we
694696
* don't have to be perfect here, we just have to be good.
695697
*/
696698
void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
697-
int order, nodemask_t *nodemask)
699+
int order, nodemask_t *nodemask, bool force_kill)
698700
{
699701
const nodemask_t *mpol_mask;
700702
struct task_struct *p;
@@ -738,7 +740,8 @@ void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
738740
goto out;
739741
}
740742

741-
p = select_bad_process(&points, totalpages, NULL, mpol_mask);
743+
p = select_bad_process(&points, totalpages, NULL, mpol_mask,
744+
force_kill);
742745
/* Found nothing?!?! Either we hang forever, or we panic. */
743746
if (!p) {
744747
dump_header(NULL, gfp_mask, order, NULL, mpol_mask);
@@ -770,7 +773,7 @@ void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
770773
void pagefault_out_of_memory(void)
771774
{
772775
if (try_set_system_oom()) {
773-
out_of_memory(NULL, 0, 0, NULL);
776+
out_of_memory(NULL, 0, 0, NULL, false);
774777
clear_system_oom();
775778
}
776779
if (!test_thread_flag(TIF_MEMDIE))

mm/page_alloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1968,7 +1968,7 @@ __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
19681968
goto out;
19691969
}
19701970
/* Exhausted what can be done so it's blamo time */
1971-
out_of_memory(zonelist, gfp_mask, order, nodemask);
1971+
out_of_memory(zonelist, gfp_mask, order, nodemask, false);
19721972

19731973
out:
19741974
clear_zonelist_oom(zonelist, gfp_mask);

0 commit comments

Comments
 (0)