2000-11-07 16:16:21

by Chris Swiedler

[permalink] [raw]
Subject: [PATCH] protect processes from OOM killer

Here's a small patch to allow a user to protect certain PIDs from death-
by-OOM-killer. It uses the proc entry '/proc/sys/vm/oom_protect'; echo the
PIDs to be protected:

echo 1 516 > /proc/sys/vm/oom_protect

The idea is that sysadmins can mark some daemon processes as off-limits for
the OOM killer. Stuff like syslogd, init, etc. Incidentally, this answers
Andrea's concern about the init process getting killed. In fact, it might
be a good idea to default the list of protected PIDs to be { 1 }.


Things I'd like to add:

- ability to append PIDs. Using the 'echo >>' syntax would be nice, but
/proc
files don't seem to support appending. (is this true?)

- symbolic process names as well as PIDs, maybe process groups too?

- perhaps a more complex interface, where instead of just marking a PID as
absolutely protected, you could specify a 'weight' which factored into the
OOM algorithm. Something like "nice":

-20 : unkillable
-19 to -1: try not to kill
1 to 19: try to kill these first

echo netscape:10 > /proc/sys/vm/oom_protect

...would suggest that "netscape" is a process which is a good candidate
for OOM killing.

I don't think that we should make the OOM heuristic any more complex.
However,
letting the user make suggestions about what should and should not be killed
is a Good Thing.

This is my very first patch, so please be considerate.

Against 2.4.0-test10. Comments and suggestions appreciated!

chris

--- official/linux-2.4.0-test10/mm/oom_kill.c Mon Nov 6 23:40:52 2000
+++ work/linux-2.4.0-test10/mm/oom_kill.c Mon Nov 6 23:37:47 2000
@@ -20,9 +20,32 @@
#include <linux/swap.h>
#include <linux/swapctl.h>
#include <linux/timex.h>
+#include <linux/ctype.h>
+
+#define MAX_OOM_PROTECTS 256
+
+int sysctl_oom_protects[MAX_OOM_PROTECTS];

/* #define DEBUG */

+int is_oom_protected(int pid)
+{
+ int i;
+ for (i = 0; i < MAX_OOM_PROTECTS; i++) {
+ int ppid = sysctl_oom_protects[i];
+
+ #ifdef DEBUG
+ printk("Protected pid: %d\n",ppid);
+ #endif
+
+ if (ppid == pid)
+ return 1;
+ if (ppid == 0)
+ return 0;
+ }
+ return 0;
+}
+
/**
* int_sqrt - oom_kill.c internal function, rough approximation to sqrt
* @x: integer of which to calculate the sqrt
@@ -124,6 +147,19 @@
read_lock(&tasklist_lock);
for_each_task(p)
{
+ #ifdef DEBUG
+ printk("Testing pid %d\n",p->pid);
+ #endif
+
+ if (is_oom_protected(p->pid))
+

+ #ifdef DEBUG
+ printk("Pid %d is protected\n",p->pid);
+ #endif
+
+ continue;
+ }
+
if (p->pid)
points = badness(p);
if (points > maxpoints) {
--- official/linux-2.4.0-test10/kernel/sysctl.c Mon Nov 6 23:40:52 2000
+++ work/linux-2.4.0-test10/kernel/sysctl.c Mon Nov 6 23:30:08 2000
@@ -85,6 +85,8 @@

extern int pgt_cache_water[];

+extern int sysctl_oom_protects [];
+
static int parse_table(int *, int, void *, size_t *, void *, size_t,
ctl_table *, void **);
static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
@@ -241,6 +243,10 @@
&bdflush_min, &bdflush_max},
{VM_OVERCOMMIT_MEMORY, "overcommit_memory", &sysctl_overcommit_memory,
sizeof(sysctl_overcommit_memory), 0644, NULL, &proc_dointvec},
+
+ {VM_OVERCOMMIT_MEMORY, "oom_protect", &sysctl_oom_protects,
+ 256, 0644, NULL, &proc_dointvec},
+
{VM_BUFFERMEM, "buffermem",
&buffer_mem, sizeof(buffer_mem_t), 0644, NULL, &proc_dointvec},
{VM_PAGECACHE, "pagecache",


2000-11-07 20:48:17

by Ingo Oeser

[permalink] [raw]
Subject: Re: [PATCH] protect processes from OOM killer

On Tue, Nov 07, 2000 at 11:19:37AM -0500, Chris Swiedler wrote:
> Here's a small patch to allow a user to protect certain PIDs from death-
> by-OOM-killer. It uses the proc entry '/proc/sys/vm/oom_protect'; echo the
> PIDs to be protected:

Please base it upon my OOM-Killer-API patch.

http://www.tu-chemnitz.de/~ioe/oom_kill_api.patch

This will reduce your patch to an simple module (but you have to
manage refcounting yourself!) and give the user a choice, which
one to use.

If someone provides an OOM-Handler himself, please use my API to
allow better testing and comparing.

PS: Of course it applies cleanly against test10 as well ;-)

Thanks and Regards

Ingo Oeser
--
To the systems programmer, users and applications
serve only to provide a test load.
<esc>:x

2000-11-07 21:45:04

by Frank van Maarseveen

[permalink] [raw]
Subject: Re: [PATCH] protect processes from OOM killer

On Tue, Nov 07, 2000 at 11:19:37AM -0500, Chris Swiedler wrote:
> Here's a small patch to allow a user to protect certain PIDs from death-
> by-OOM-killer. It uses the proc entry '/proc/sys/vm/oom_protect'; echo the
> PIDs to be protected:
>
> echo 1 516 > /proc/sys/vm/oom_protect
Hmm, I'd prefer "echo 1 >/proc/516/oom_protect". Guess that's
out of the question because only /proc/sys should be used for
setting parameters?

Then maybe /proc/sys/proc should be populated so we can do
"echo 1 >/proc/sys/proc/516/oom_protect".

--
Frank

2000-12-04 16:27:32

by hugang

[permalink] [raw]
Subject: Path: for oom_kill.c

Hello all:

old ----> points = p->mm->total_vm;

change to ---> points = p->pid;


I write a shell to test it,

cat > bin/hello
hello
^D

hello
before change it ,kernel will kill some pid, to change it kernel will kill hello(bash).

2000-12-04 17:28:55

by Rik van Riel

[permalink] [raw]
Subject: Re: Path: for oom_kill.c

On Sat, 2 Dec 2000, hugang wrote:

> Hello all:
>
> old ----> points = p->mm->total_vm;
>
> change to ---> points = p->pid;

Ummm, what exactly do you want to achieve with this?

> before change it ,kernel will kill some pid, to change it kernel
> will kill hello(bash).

Before the change, the kernel will try to kill a BIG process
(the obvious suspect for a memory leak). After the change,
the kernel will kill an almost random process ...

regards,

Rik
--
Hollywood goes for world dumbination,
Trailer at 11.

http://www.surriel.com/
http://www.conectiva.com/ http://distro.conectiva.com.br/

2000-12-04 18:22:59

by Jeff Epler

[permalink] [raw]
Subject: Re: Path: for oom_kill.c

On Mon, Dec 04, 2000 at 02:57:34PM -0200, Rik van Riel wrote:
> On Sat, 2 Dec 2000, hugang wrote:
>
> > Hello all:
> >
> > old ----> points = p->mm->total_vm;
> >
> > change to ---> points = p->pid;
>
> Ummm, what exactly do you want to achieve with this?

I suspect that hugang whishes to kill the newest process. However,
this will not work after PID wrap.

Jeff