sysrq: Don't panic_on_oom if the user requested an OOM kill.
PATCH 1/1 sysrq: Don't panic_on_oom if the user requested an OOM kill.
If I specifically request an OOM kill via magic sysrq, I don't want panic_on_oom to apply and crash the machine. I ran into this on a server with panic_on_oom and automatic reboot after panic configured on the assumption that nobody is likely physically present. If I'm able to use magic sysrq, though, I can use that to reboot the machine slightly more safely, if that's what I wanted.
This patch adds an extra parameter to out_of_memory to ignore the value of sysctl_panic_on_oom, and just proceed with the OOM kill.
--- drivers/char/sysrq.c | 2 +- include/linux/oom.h | 3 ++- mm/oom_kill.c | 8 +++++--- mm/page_alloc.c | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-)
drivers/char/sysrq.c +1 -1
@@ -329,7 +329,7 @@ static struct sysrq_key_op sysrq_term_op = {static void moom_callback (struct work_struct *ignored) { -out_of_memory (node_zonelist (0 ,GFP_KERNEL ),GFP_KERNEL ,0 ); +out_of_memory (node_zonelist (0 ,GFP_KERNEL ),GFP_KERNEL ,0 ,1 ); } static DECLARE_WORK(moom_work, moom_callback);
include/linux/oom.h +2 -1
@@ -26,7 +26,8 @@ enum oom_constraint {extern int try_set_zone_oom (struct zonelist *zonelist,gfp_t gfp_flags);extern void clear_zonelist_oom (struct zonelist *zonelist,gfp_t gfp_flags); -extern void out_of_memory (struct zonelist *zonelist,gfp_t gfp_mask,int order); +extern void out_of_memory (struct zonelist *zonelist,gfp_t gfp_mask, +int order,int dontpanic);extern int register_oom_notifier (struct notifier_block *nb);extern int unregister_oom_notifier (struct notifier_block *nb);
mm/oom_kill.c +5 -3
@@ -583,13 +583,15 @@ rest_and_return:* @zonelist: zonelist pointer * @gfp_mask: memory allocation flags * @order: amount of memory being requested as a power of 2 +* @dontpanic: whether to ignore panic_on_oom (used by magic-sysrq) * * If we run out of memory, we have the choice between either * killing a random task (bad), letting the system crash (worse) * OR try to be smart about which process to kill. Note that we * don't have to be perfect here, we just have to be good. */ -void out_of_memory (struct zonelist *zonelist,gfp_t gfp_mask,int order) +void out_of_memory (struct zonelist *zonelist,gfp_t gfp_mask, +int order,int dontpanic) {unsigned long freed =0 ;enum oom_constraint constraint;
@@ -599,7 +601,7 @@ void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order)/* Got some memory back in the last second. */ return ; -if (sysctl_panic_on_oom ==2 ) +if (!dontpanic && sysctl_panic_on_oom ==2 )panic ("out of memory. Compulsory panic_on_oom is selected.\n" );/*
@@ -616,7 +618,7 @@ void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order)break ;case CONSTRAINT_NONE : -if (sysctl_panic_on_oom) +if (!dontpanic && sysctl_panic_on_oom)panic ("out of memory. panic_on_oom is selected\n" );/* Fall-through */ case CONSTRAINT_CPUSET :
mm/page_alloc.c +1 -1
@@ -1625,7 +1625,7 @@ nofail_alloc: goto nopage; } -out_of_memory (zonelist, gfp_mask, order); +out_of_memory (zonelist, gfp_mask, order,0 );clear_zonelist_oom (zonelist, gfp_mask); goto restart; }
Signature · 1 line
1.6.0
Quoted 11 lines · Geoffrey Thomas
If I specifically request an OOM kill via magic sysrq, I don't want
panic_on_oomto apply and crash the machine. I ran into this on a server withpanic_on_oomand automatic reboot after panic configured on the assumption that nobody is likely physically present. If I'm able to use magic sysrq, though, I can use that to reboot the machine slightly more safely, if that's what I wanted.This patch adds an extra parameter to
out_of_memoryto ignore the value ofsysctl_panic_on_oom, and just proceed with the OOM kill.Signed-off-by Geoffrey Thomas <[email protected]>
Nack, it's your own fault for asking for panic_on_oom and then not wanting it to panic on oom.
Disable /proc/sys/vm/panic_on_oom before invoking the oom killer and then reenable it afterwards.