Skip to contentSkip to conversation

oom: create a resource limit for oom_adj

PATCH In review Mandeep Singh Baines 4 participants 13 messages Nov 11, 2010 – Nov 16, 2010

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 oom: create a resource limit for oom_adj

For ChromiumOS, we'd like to be able to oom_adj a process up/down as its leaves/enters the foreground. Currently, it is not possible to oom_adj down without CAP_SYS_RESOURCE. This patch creates a new resource limit, RLIMIT_OOMADJ, which is works in a similar fashion to RLIMIT_NICE. This allows a process's oom_adj to be lowered without CAP_SYS_RESOURCE as long as the new value is greater than the resource limit.

Alternative considered:

* a setuid binary
* a daemon with CAP_SYS_RESOURCE

Since you don't wan't all processes to be able to reduce their oom_adj, a setuid or daemon implementation would be complex. The alternatives also have much higher overhead.

---
 fs/proc/base.c                 |   12 ++++++++++--
 include/asm-generic/resource.h |    5 ++++-
 2 files changed, 14 insertions(+), 3 deletions(-)
fs/proc/base.c +10 -2
index f3d02ca..4384013 100644
@@ -462,6 +462,7 @@ static const struct limit_names lnames[RLIM_NLIMITS] = {
 	[RLIMIT_NICE] = {"Max nice priority", NULL},
 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
+	[RLIMIT_OOMADJ] = {"Max OOM adjust", NULL},
 };
 
 /* Display limits for a process */
@@ -1057,8 +1058,15 @@ static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
 	}
 
 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
-		err = -EACCES;
-		goto err_sighand;
+		/* convert oom_adj [15,-17] to rlimit style value [1,33] */
+		long oom_rlim = OOM_ADJUST_MAX + 1 - oom_adjust;
+
+		if (oom_rlim > task->signal->rlim[RLIMIT_OOMADJ].rlim_cur) {
+			unlock_task_sighand(task, &flags);
+			put_task_struct(task);
+			err = -EACCES;
+			goto err_sighand;
+		}
 	}
 
 	if (oom_adjust != task->signal->oom_adj) {
include/asm-generic/resource.h +4 -1
index 587566f..a8640a4 100644
@@ -45,7 +45,9 @@
 					   0-39 for nice level 19 .. -20 */
 #define RLIMIT_RTPRIO		14	/* maximum realtime priority */
 #define RLIMIT_RTTIME		15	/* timeout for RT tasks in us */
-#define RLIM_NLIMITS		16
+#define RLIMIT_OOMADJ		16	/* max oom_adj allowed to lower to
+					   0-32 for oom level 15 .. -17 */
+#define RLIM_NLIMITS		17
 
 /*
  * SuS says limits have to be unsigned.
@@ -86,6 +88,7 @@
 	[RLIMIT_MSGQUEUE]	= {   MQ_BYTES_MAX,   MQ_BYTES_MAX },	\
 	[RLIMIT_NICE]		= { 0, 0 },				\
 	[RLIMIT_RTPRIO]		= { 0, 0 },				\
+	[RLIMIT_OOMADJ]		= { 0, 0 },				\
 	[RLIMIT_RTTIME]		= {  RLIM_INFINITY,  RLIM_INFINITY },	\
 }
 
Signature · 1 line
1.7.3.1
#1 lore raw
Quoted 8 lines · Mandeep Singh Baines

For ChromiumOS, we'd like to be able to oom_adj a process up/down as its leaves/enters the foreground. Currently, it is not possible to oom_adj down without CAP_SYS_RESOURCE. This patch creates a new resource limit, RLIMIT_OOMADJ, which is works in a similar fashion to RLIMIT_NICE. This allows a process's oom_adj to be lowered without CAP_SYS_RESOURCE as long as the new value is greater than the resource limit.

First of all, oom_adj is deprecated and scheduled for removal in a couple of years (see Documentation/feature-removal-schedule.txt) so any work in this area should be targeting oom_score_adj instead.

What is the anticipated use case for this? We know that you want to lower oom_adj without CAP_SYS_RESOURCE, but what's the expected behavior when an app moves from foreground to background? I assume it's something like having an oom_adj of 0 in the background and +15 in the foreground. If so, does /proc/sys/vm/oom_kill_allocating_task get you most of what you're looking for?

I'm wondering if we can avoid yet another resource limit for something like this.

Quoted 9 lines

Alternative considered:

* a setuid binary
* a daemon with CAP_SYS_RESOURCE

Since you don't wan't all processes to be able to reduce their oom_adj, a setuid or daemon implementation would be complex. The alternatives also have much higher overhead.

What do you anticipate will be writing to oom_score_adj with this patch, the app itself?

Quoted 11 lines
---
 fs/proc/base.c                 |   12 ++++++++++--
 include/asm-generic/resource.h |    5 ++++-
 2 files changed, 14 insertions(+), 3 deletions(-)
fs/proc/base.c +0 -0
index f3d02ca..4384013 100644

fs/proc/base.c static const struct limit_names lnames[RLIM_NLIMITS] = { · line 465

Quoted 4 lines
fs/proc/base.c +1 -0
@@ -462,6 +462,7 @@ static const struct limit_names lnames[RLIM_NLIMITS] = {
 	[RLIMIT_NICE] = {"Max nice priority", NULL},
 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
+	[RLIMIT_OOMADJ] = {"Max OOM adjust", NULL},

s/Max/Min, right?

fs/proc/base.c static ssize_t oom_adjust_write(struct file *file, const char __user *buf, · line 1063

Quoted 12 lines
fs/proc/base.c +3 -2
 };
 
 /* Display limits for a process */
@@ -1057,8 +1058,15 @@ static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
 	}
 
 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
-		err = -EACCES;
-		goto err_sighand;
+		/* convert oom_adj [15,-17] to rlimit style value [1,33] */
+		long oom_rlim = OOM_ADJUST_MAX + 1 - oom_adjust;
+

Ouch, that's a rather unfortunate mapping.

fs/proc/base.c static ssize_t oom_adjust_write(struct file *file, const char __user *buf, · line 1068

Quoted 5 lines
fs/proc/base.c +5 -0
+		if (oom_rlim > task->signal->rlim[RLIMIT_OOMADJ].rlim_cur) {
+			unlock_task_sighand(task, &flags);
+			put_task_struct(task);
+			err = -EACCES;
+			goto err_sighand;

err_sighand has duplicate unlock_task_sighand() and put_task_struct(); since you're missing the task_unlock(task) here, just using goto err_sighand would suffice.

Quoted 4 lines
fs/proc/base.c +1 -0
+		}
 	}
 
 	if (oom_adjust != task->signal->oom_adj) {
#2 lore raw
Quoted 11 lines · David Rientjes
Quoted 8 lines · Mandeep Singh Baines

For ChromiumOS, we'd like to be able to oom_adj a process up/down as its leaves/enters the foreground. Currently, it is not possible to oom_adj down without CAP_SYS_RESOURCE. This patch creates a new resource limit, RLIMIT_OOMADJ, which is works in a similar fashion to RLIMIT_NICE. This allows a process's oom_adj to be lowered without CAP_SYS_RESOURCE as long as the new value is greater than the resource limit.

Quoted 4 lines

First of all, oom_adj is deprecated and scheduled for removal in a couple of years (see Documentation/feature-removal-schedule.txt) so any work in this area should be targeting oom_score_adj instead.

Ah. Thanks for the pointer.

Quoted 3 lines

What is the anticipated use case for this? We know that you want to lower oom_adj without CAP_SYS_RESOURCE, but what's the expected behavior when an app moves from foreground to background? I assume it's something like

The focus here is the web browser's tabs. In our case, each is a process. If OOM is going to kill a process, you'd rather it kill the tab you looked at hours ago instead of the one you're looking at now. So you'd like to have a policy where the LRU tab gets killed first. We'd like to use oom_score_adj as the mechanism to implement an LRU policy like this.

Quoted 4 lines

having an oom_adj of 0 in the background and +15 in the foreground. If so, does /proc/sys/vm/oom_kill_allocating_task get you most of what you're looking for?

As explained above, oom_kill_allocating_task won't give us what we want.

Quoted 8 lines

I'm wondering if we can avoid yet another resource limit for something like this.

Quoted 5 lines

Alternative considered:

* a setuid binary
* a daemon with CAP_SYS_RESOURCE

Quoted 8 lines
Quoted 4 lines

Since you don't wan't all processes to be able to reduce their oom_adj, a setuid or daemon implementation would be complex. The alternatives also have much higher overhead.

What do you anticipate will be writing to oom_score_adj with this patch, the app itself?

A process in the browser session will do the adusting. We'd rather not give it CAP_SYS_RESOURCE. It should only be allowed to change oom_score_adj up and down within the bounds set by the administrator. Analagous to renice() which we also do using a similar policy.

Quoted 6 lines
Quoted 6 lines
---
 fs/proc/base.c                 |   12 ++++++++++--
 include/asm-generic/resource.h |    5 ++++-
 2 files changed, 14 insertions(+), 3 deletions(-)
Quoted 12 lines

fs/proc/base.c static const struct limit_names lnames[RLIM_NLIMITS] = { · line 465

Quoted 9 lines
fs/proc/base.c +1 -0
index f3d02ca..4384013 100644
@@ -462,6 +462,7 @@ static const struct limit_names lnames[RLIM_NLIMITS] = {
 	[RLIMIT_NICE] = {"Max nice priority", NULL},
 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
+	[RLIMIT_OOMADJ] = {"Max OOM adjust", NULL},

s/Max/Min, right?

This is a MAX value because of how resource limits work. On the other hand, it is really controlling the minimum oom_adj. So its a toss up for me. More than happy to change if you prefer Min.

Quoted 6 lines
Quoted 6 lines
fs/proc/base.c +0 -0
 };
 
 /* Display limits for a process */
@@ -1057,8 +1058,15 @@ static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
 	}
 
Quoted 9 lines

fs/proc/base.c static ssize_t oom_adjust_write(struct file *file, const char __user *buf, · line 1063

Quoted 6 lines
fs/proc/base.c +3 -2
 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
-		err = -EACCES;
-		goto err_sighand;
+		/* convert oom_adj [15,-17] to rlimit style value [1,33] */
+		long oom_rlim = OOM_ADJUST_MAX + 1 - oom_adjust;
+

Ouch, that's a rather unfortunate mapping.

Unfortunate but unavoidable. The resource limit code checks to see if the new limit is greater than the limit. This code was based on the can_nice() code in sched.c.

Quoted 10 lines

fs/proc/base.c static ssize_t oom_adjust_write(struct file *file, const char __user *buf, · line 1068

Quoted 5 lines
fs/proc/base.c +5 -0
+		if (oom_rlim > task->signal->rlim[RLIMIT_OOMADJ].rlim_cur) {
+			unlock_task_sighand(task, &flags);
+			put_task_struct(task);
+			err = -EACCES;
+			goto err_sighand;

err_sighand has duplicate unlock_task_sighand() and put_task_struct(); since you're missing the task_unlock(task) here, just using goto err_sighand would suffice.

D'oh. Forward port error. I should be more careful. Thanks for catching:)

Quoted 4 lines

fs/proc/base.c static ssize_t oom_adjust_write(struct file *file, const char __user *buf, · line 1072

Quoted 4 lines
fs/proc/base.c +1 -0
+		}
 	}
 
 	if (oom_adjust != task->signal->oom_adj) {

Thank you for reviewing this patch.

Should I send an updated oom_score_adj patch?

Regards,
Mandeep

#3 lore raw
Quoted 10 lines · Mandeep Singh Baines
Quoted 3 lines

What is the anticipated use case for this? We know that you want to lower oom_adj without CAP_SYS_RESOURCE, but what's the expected behavior when an app moves from foreground to background? I assume it's something like

The focus here is the web browser's tabs. In our case, each is a process. If OOM is going to kill a process, you'd rather it kill the tab you looked at hours ago instead of the one you're looking at now. So you'd like to have a policy where the LRU tab gets killed first. We'd like to use oom_score_adj as the mechanism to implement an LRU policy like this.

Hmm, at first glance that seems potentially dangerous if the current tab generates a burt of memory allocations and it ends up killing all other tabs before finally targeting the culprit whereas currently the heuristic should do a good job of finding this problematic tab and killing it instantly.

Perhaps that can't happen and it probably doesn't even matter:
oom_score_adj allows users to determine which process to kill regardless of the underlying reason.

Quoted 8 lines
Quoted 2 lines

What do you anticipate will be writing to oom_score_adj with this patch, the app itself?

A process in the browser session will do the adusting. We'd rather not give it CAP_SYS_RESOURCE. It should only be allowed to change oom_score_adj up and down within the bounds set by the administrator. Analagous to renice() which we also do using a similar policy.

So as more and more tabs get used, the least recently used tab gets its oom_score_adj raised higher and higher until it is reused itself and then it gets reset back to 0 for the current tab?

Is there a reason you don't want to give the underlying browser session process CAP_SYS_RESOURCE? Will it not be enforcing resource limits to ensure tabs don't deplete all memory when certain sites are opened? Are you concerned that it may deplete all memory itself (for which case you could raise its own oom_score_adj, which is a proportion of available memory so you can define where that point of depletiong is)?

#4 lore raw
Quoted 13 lines · David Rientjes
Quoted 10 lines · Mandeep Singh Baines
Quoted 3 lines

What is the anticipated use case for this? We know that you want to lower oom_adj without CAP_SYS_RESOURCE, but what's the expected behavior when an app moves from foreground to background? I assume it's something like

The focus here is the web browser's tabs. In our case, each is a process. If OOM is going to kill a process, you'd rather it kill the tab you looked at hours ago instead of the one you're looking at now. So you'd like to have a policy where the LRU tab gets killed first. We'd like to use oom_score_adj as the mechanism to implement an LRU policy like this.

Quoted 6 lines

Hmm, at first glance that seems potentially dangerous if the current tab generates a burt of memory allocations and it ends up killing all other tabs before finally targeting the culprit whereas currently the heuristic should do a good job of finding this problematic tab and killing it instantly.

If you're watching a movie, video chatting, playing a game, etc. What would you rather have killed: the current tab you are interacting with or some tab you opened a while back and are no longer interacting with.

Quoted 13 lines

Perhaps that can't happen and it probably doesn't even matter:
oom_score_adj allows users to determine which process to kill regardless of the underlying reason.

Quoted 8 lines
Quoted 2 lines

What do you anticipate will be writing to oom_score_adj with this patch, the app itself?

A process in the browser session will do the adusting. We'd rather not give it CAP_SYS_RESOURCE. It should only be allowed to change oom_score_adj up and down within the bounds set by the administrator. Analagous to renice() which we also do using a similar policy.

Quoted 4 lines

So as more and more tabs get used, the least recently used tab gets its oom_score_adj raised higher and higher until it is reused itself and then it gets reset back to 0 for the current tab?

Exactly.

Quoted 2 lines

Is there a reason you don't want to give the underlying browser session process CAP_SYS_RESOURCE? Will it not be enforcing resource limits to

Security. We want to use the least-privilege possible. We really want to avoid giving special privileges to the browser. You shouldn't need administrative privileges to run the browser. We'd like for oom_score_adj to work the same as nice. An unprivileged user can nice up and down as long as the new setting is within the administratively configured resource limit: ulimit -e.

Quoted 4 lines

ensure tabs don't deplete all memory when certain sites are opened? Are you concerned that it may deplete all memory itself (for which case you could raise its own oom_score_adj, which is a proportion of available memory so you can define where that point of depletiong is)?

#5 lore raw
Quoted 11 lines · Mandeep Singh Baines
Quoted 6 lines

Hmm, at first glance that seems potentially dangerous if the current tab generates a burt of memory allocations and it ends up killing all other tabs before finally targeting the culprit whereas currently the heuristic should do a good job of finding this problematic tab and killing it instantly.

If you're watching a movie, video chatting, playing a game, etc. What would you rather have killed: the current tab you are interacting with or some tab you opened a while back and are no longer interacting with.

Well, it's a tangential point, but I'd personally prefer that my existing tabs that I've decided to leave open are guaranteed to remain open regardless of where I'm browsing next (they could hold valuable data that I can't easily get back) and avoid having all of them sacrificed out from under me for the newly opened tab. I can always go back and close those tabs for more memory if I know I don't need them anymore and then retry the failed allocation.

Quoted 7 lines
Quoted 4 lines

So as more and more tabs get used, the least recently used tab gets its oom_score_adj raised higher and higher until it is reused itself and then it gets reset back to 0 for the current tab?

Exactly.

We don't necessarily want arbitrary tasks to be able to decrease their oom_score_adj back to 0 if a CAP_SYS_RESOURCE thread has elevated it, that's part of the reason for the restriction (in addition to decreasing your own oom_score_adj all the way to OOM_SCORE_ADJ_MIN).

Would it suffice to allow a task to decrease its oom_score_adj back to the highest value that a CAP_SYS_RESOURCE thread set it or its inherited value at fork? Assuming the thread that has forked it has oom_score_adj of 0, each tab could decrease it back from 0 upon activation unless a CAP_SYS_RESOURCE thread elevated it to something higher.

To do this, we'd need to save the highest oom_score_adj set by a CAP_SYS_RESOURCE in struct signal_struct.

#6 lore raw
Quoted 35 lines · David Rientjes
Quoted 11 lines · Mandeep Singh Baines
Quoted 6 lines

Hmm, at first glance that seems potentially dangerous if the current tab generates a burt of memory allocations and it ends up killing all other tabs before finally targeting the culprit whereas currently the heuristic should do a good job of finding this problematic tab and killing it instantly.

If you're watching a movie, video chatting, playing a game, etc. What would you rather have killed: the current tab you are interacting with or some tab you opened a while back and are no longer interacting with.

Well, it's a tangential point, but I'd personally prefer that my existing tabs that I've decided to leave open are guaranteed to remain open regardless of where I'm browsing next (they could hold valuable data that I can't easily get back) and avoid having all of them sacrificed out from under me for the newly opened tab. I can always go back and close those tabs for more memory if I know I don't need them anymore and then retry the failed allocation.

Quoted 7 lines
Quoted 4 lines

So as more and more tabs get used, the least recently used tab gets its oom_score_adj raised higher and higher until it is reused itself and then it gets reset back to 0 for the current tab?

Exactly.

We don't necessarily want arbitrary tasks to be able to decrease their oom_score_adj back to 0 if a CAP_SYS_RESOURCE thread has elevated it, that's part of the reason for the restriction (in addition to decreasing your own oom_score_adj all the way to OOM_SCORE_ADJ_MIN).

Quoted 8 lines

Would it suffice to allow a task to decrease its oom_score_adj back to the highest value that a CAP_SYS_RESOURCE thread set it or its inherited value at fork? Assuming the thread that has forked it has oom_score_adj of 0, each tab could decrease it back from 0 upon activation unless a CAP_SYS_RESOURCE thread elevated it to something higher.

To do this, we'd need to save the highest oom_score_adj set by a CAP_SYS_RESOURCE in struct signal_struct.

Sounds good to me. I'll start working on this patch.

Thanks,
Mandeep

#7 lore raw

PATCH 1/1 oom: allow a non-CAP_SYS_RESOURCE proces to oom_score_adj down

We'd like to be able to oom_score_adj a process up/down as its enters/leaves the foreground. Currently, it is not possible to oom_adj down without CAP_SYS_RESOURCE. This patch allows a task to decrease its oom_score_adj back to the value that a CAP_SYS_RESOURCE thread set it or its inherited value at fork. Assuming the thread that has forked it has oom_score_adj of 0, each tab could decrease it back from 0 upon activation unless a CAP_SYS_RESOURCE thread elevated it to something higher.

Alternative considered:

* a setuid binary
* a daemon with CAP_SYS_RESOURCE

Since you don't wan't all processes to be able to reduce their oom_adj, a setuid or daemon implementation would be complex. The alternatives also have much higher overhead.

This patch updated based on feedback from
David Rientjes <[email protected]>.

Change-Id If8f52363fd6c156e1730f43148aee987260e9c72
---
 fs/proc/base.c        |    4 +++-
 include/linux/sched.h |    2 ++
 2 files changed, 5 insertions(+), 1 deletions(-)
fs/proc/base.c +3 -1
index f3d02ca..e617413 100644
@@ -1164,7 +1164,7 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 		goto err_task_lock;
 	}
 
-	if (oom_score_adj < task->signal->oom_score_adj &&
+	if (oom_score_adj < task->signal->oom_score_adj_min &&
 			!capable(CAP_SYS_RESOURCE)) {
 		err = -EACCES;
 		goto err_sighand;
@@ -1177,6 +1177,8 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 			atomic_dec(&task->mm->oom_disable_count);
 	}
 	task->signal->oom_score_adj = oom_score_adj;
+	if (capable(CAP_SYS_RESOURCE))
+		task->signal->oom_score_adj_min = oom_score_adj;
 	/*
 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
 	 * always attainable.
include/linux/sched.h +2 -0
index f53cdf2..2a71ee0 100644
@@ -626,6 +626,8 @@ struct signal_struct {
 
 	int oom_adj;		/* OOM kill score adjustment (bit shift) */
 	int oom_score_adj;	/* OOM kill score adjustment */
+	int oom_score_adj_min;	/* OOM kill score adjustment minimum value.
+				 * Only settable by CAP_SYS_RESOURCE. */
 
 	struct mutex cred_guard_mutex;	/* guard against foreign influences on
 					 * credential calculations
Signature · 1 line
1.7.3.1
#8 lore raw

Re: [PATCH] oom: allow a non-CAP_SYS_RESOURCE proces to oom_score_adj down

Quoted 9 lines · Mandeep Singh Baines

We'd like to be able to oom_score_adj a process up/down as its enters/leaves the foreground. Currently, it is not possible to oom_adj down without CAP_SYS_RESOURCE. This patch allows a task to decrease its oom_score_adj back to the value that a CAP_SYS_RESOURCE thread set it or its inherited value at fork. Assuming the thread that has forked it has oom_score_adj of 0, each tab could decrease it back from 0 upon activation unless a CAP_SYS_RESOURCE thread elevated it to something higher.

oom_score_adj_min doesn't appear to be inherited at fork in your patch.

Quoted 9 lines

Alternative considered:

* a setuid binary
* a daemon with CAP_SYS_RESOURCE

Since you don't wan't all processes to be able to reduce their oom_adj, a setuid or daemon implementation would be complex. The alternatives also have much higher overhead.

This behavior should be documented in Documentation/filesystems/proc.txt.

Quoted 4 lines

This patch updated based on feedback from
David Rientjes <[email protected]>.

Change-Id: If8f52363fd6c156e1730f43148aee987260e9c72

I know what a Change-Id is , but nobody else here does :)

Quoted 41 lines
---
 fs/proc/base.c        |    4 +++-
 include/linux/sched.h |    2 ++
 2 files changed, 5 insertions(+), 1 deletions(-)
fs/proc/base.c +3 -1
index f3d02ca..e617413 100644
@@ -1164,7 +1164,7 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 		goto err_task_lock;
 	}
 
-	if (oom_score_adj < task->signal->oom_score_adj &&
+	if (oom_score_adj < task->signal->oom_score_adj_min &&
 			!capable(CAP_SYS_RESOURCE)) {
 		err = -EACCES;
 		goto err_sighand;
@@ -1177,6 +1177,8 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 			atomic_dec(&task->mm->oom_disable_count);
 	}
 	task->signal->oom_score_adj = oom_score_adj;
+	if (capable(CAP_SYS_RESOURCE))
+		task->signal->oom_score_adj_min = oom_score_adj;
 	/*
 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
 	 * always attainable.
include/linux/sched.h +2 -0
index f53cdf2..2a71ee0 100644
@@ -626,6 +626,8 @@ struct signal_struct {
 
 	int oom_adj;		/* OOM kill score adjustment (bit shift) */
 	int oom_score_adj;	/* OOM kill score adjustment */
+	int oom_score_adj_min;	/* OOM kill score adjustment minimum value.
+				 * Only settable by CAP_SYS_RESOURCE. */
 
 	struct mutex cred_guard_mutex;	/* guard against foreign influences on
 					 * credential calculations
#12 lore raw
In reply to Mandeep Singh Baines

Hi Mandeep,

Quoted 17 lines

For ChromiumOS, we'd like to be able to oom_adj a process up/down as its leaves/enters the foreground. Currently, it is not possible to oom_adj down without CAP_SYS_RESOURCE. This patch creates a new resource limit, RLIMIT_OOMADJ, which is works in a similar fashion to RLIMIT_NICE. This allows a process's oom_adj to be lowered without CAP_SYS_RESOURCE as long as the new value is greater than the resource limit.

Alternative considered:

* a setuid binary
* a daemon with CAP_SYS_RESOURCE

Since you don't wan't all processes to be able to reduce their oom_adj, a setuid or daemon implementation would be complex. The alternatives also have much higher overhead.

Quoted 5 lines
---
 fs/proc/base.c                 |   12 ++++++++++--
 include/asm-generic/resource.h |    5 ++++-
 2 files changed, 14 insertions(+), 3 deletions(-)

This concept sound useful for embedeed. but I dislike this interface a bit. Why don't you create /proc/{pid}/oom_adj_lower_bound or similar? It is more straight forward because oom_adj are already using /proc.

I also think 15..-17 to 0-32 convertion is a bit user unfriendly.

Quoted 14 lines
fs/proc/base.c +1 -0
index f3d02ca..4384013 100644
@@ -462,6 +462,7 @@ static const struct limit_names lnames[RLIM_NLIMITS] = {
 	[RLIMIT_NICE] = {"Max nice priority", NULL},
 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
+	[RLIMIT_OOMADJ] = {"Max OOM adjust", NULL},
 };
 
 /* Display limits for a process */

fs/proc/base.c static ssize_t oom_adjust_write(struct file *file, const char __user *buf, · line 1064

Quoted 9 lines
fs/proc/base.c +4 -2
@@ -1057,8 +1058,15 @@ static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
 	}
 
 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
-		err = -EACCES;
-		goto err_sighand;
+		/* convert oom_adj [15,-17] to rlimit style value [1,33] */
+		long oom_rlim = OOM_ADJUST_MAX + 1 - oom_adjust;
+
+		if (oom_rlim > task->signal->rlim[RLIMIT_OOMADJ].rlim_cur) {

two points.

1) task->signal->rlim[RLIMIT_OOMADJ].rlim_cur is incorrect. please use task_rlimit().

2) If process has CAP_SYS_RESOURCE, we should ignore RLIMIT_OOMADJ for backword compatibility. CAP_NICE do so. (see below)

------------------------------------------------------------------
int can_nice(const struct task_struct *p, const int nice)

{
        /* convert nice value [19,-20] to rlimit style value [1,40] */
        int nice_rlim = 20 - nice;

        return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
                capable(CAP_SYS_NICE));
}

------------------------------------------------------------------

Quoted 24 lines
fs/proc/base.c +5 -0
+			unlock_task_sighand(task, &flags);
+			put_task_struct(task);
+			err = -EACCES;
+			goto err_sighand;
+		}
 	}
 
 	if (oom_adjust != task->signal->oom_adj) {
include/asm-generic/resource.h +3 -1
index 587566f..a8640a4 100644
@@ -45,7 +45,9 @@
 					   0-39 for nice level 19 .. -20 */
 #define RLIMIT_RTPRIO		14	/* maximum realtime priority */
 #define RLIMIT_RTTIME		15	/* timeout for RT tasks in us */
-#define RLIM_NLIMITS		16
+#define RLIMIT_OOMADJ		16	/* max oom_adj allowed to lower to
+					   0-32 for oom level 15 .. -17 */
+#define RLIM_NLIMITS		17
 
 /*
  * SuS says limits have to be unsigned.

include/asm-generic/resource.h line 91

Quoted 4 lines
include/asm-generic/resource.h +1 -0
@@ -86,6 +88,7 @@
 	[RLIMIT_MSGQUEUE]	= {   MQ_BYTES_MAX,   MQ_BYTES_MAX },	\
 	[RLIMIT_NICE]		= { 0, 0 },				\
 	[RLIMIT_RTPRIO]		= { 0, 0 },				\
+	[RLIMIT_OOMADJ]		= { 0, 0 },				\

I don't think 0 is good initial value because 0 mean oom_adj==15.

Quoted 6 lines
include/asm-generic/resource.h +0 -0
 	[RLIMIT_RTTIME]		= {  RLIM_INFINITY,  RLIM_INFINITY },	\
 }
 
Signature · 1 line
1.7.3.1
#9 lore raw

PATCH v2 1/1 oom: allow a non-CAP_SYS_RESOURCE proces to oom_score_adj down

In reply to David Rientjes

Hi,

Attached is V2 of this patch. Since V1:

* Added documentation in Documentation/filesystems/proc.txt
* Copy oom_score_adj_min value across a fork

An alternative to this patch would be to expose oom_score_adj_min via proc (suggested by Kosaki Motohiro). You could allow non-CAP_SYS_RESOURCE processes to irreversibly increase the minimum. That would give you similar semantics to a resource limit and would give you a bit more control but it means adding another proc variable.

Regards,
Mandeep

---

We'd like to be able to oom_score_adj a process up/down as its enters/leaves the foreground. Currently, it is not possible to oom_adj down without CAP_SYS_RESOURCE. This patch allows a task to decrease its oom_score_adj back to the value that a CAP_SYS_RESOURCE thread set it or its inherited value at fork. Assuming the thread that has forked it has oom_score_adj of 0, each tab process could decrease it back from 0 upon activation unless a CAP_SYS_RESOURCE thread elevated it to something higher.

Alternative considered:

* a setuid binary
* a daemon with CAP_SYS_RESOURCE

Since you don't wan't all processes to be able to reduce their oom_adj, a setuid or daemon implementation would be complex. The alternatives also have much higher overhead.

This patch updated from original patch based on feedback from David Rientjes <[email protected]>.

---
 Documentation/filesystems/proc.txt |    4 ++++
 fs/proc/base.c                     |    4 +++-
 include/linux/sched.h              |    2 ++
 kernel/fork.c                      |    1 +
 4 files changed, 10 insertions(+), 1 deletions(-)
Documentation/filesystems/proc.txt +4 -0
index e73df27..7139c50 100644
@@ -1296,6 +1296,10 @@ scaled linearly with /proc/<pid>/oom_score_adj.
 Writing to /proc/<pid>/oom_score_adj or /proc/<pid>/oom_adj will change the
 other with its scaled value.
 
+The value of /proc/<pid>/oom_score_adj may be reduced no lower than the last
+value set by a CAP_SYS_RESOURCE process. To reduce the value any lower
+requires CAP_SYS_RESOURCE.
+
 NOTICE: /proc/<pid>/oom_adj is deprecated and will be removed, please see
 Documentation/feature-removal-schedule.txt.
 
fs/proc/base.c +3 -1
index f3d02ca..e617413 100644
@@ -1164,7 +1164,7 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 		goto err_task_lock;
 	}
 
-	if (oom_score_adj < task->signal->oom_score_adj &&
+	if (oom_score_adj < task->signal->oom_score_adj_min &&
 			!capable(CAP_SYS_RESOURCE)) {
 		err = -EACCES;
 		goto err_sighand;
@@ -1177,6 +1177,8 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 			atomic_dec(&task->mm->oom_disable_count);
 	}
 	task->signal->oom_score_adj = oom_score_adj;
+	if (capable(CAP_SYS_RESOURCE))
+		task->signal->oom_score_adj_min = oom_score_adj;
 	/*
 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
 	 * always attainable.
include/linux/sched.h +2 -0
index f53cdf2..2a71ee0 100644
@@ -626,6 +626,8 @@ struct signal_struct {
 
 	int oom_adj;		/* OOM kill score adjustment (bit shift) */
 	int oom_score_adj;	/* OOM kill score adjustment */
+	int oom_score_adj_min;	/* OOM kill score adjustment minimum value.
+				 * Only settable by CAP_SYS_RESOURCE. */
 
 	struct mutex cred_guard_mutex;	/* guard against foreign influences on
 					 * credential calculations
kernel/fork.c +1 -0
index 3b159c5..0979527 100644
@@ -907,6 +907,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
 
 	sig->oom_adj = current->signal->oom_adj;
 	sig->oom_score_adj = current->signal->oom_score_adj;
+	sig->oom_score_adj_min = current->signal->oom_score_adj_min;
 
 	mutex_init(&sig->cred_guard_mutex);
 
Signature · 1 line
1.7.3.1
Acked-by on v2 1/1
#10 lore raw

Re: [PATCH v2] oom: allow a non-CAP_SYS_RESOURCE proces to oom_score_adj down

Quoted 14 lines · Mandeep Singh Baines
fs/proc/base.c +1 -1
index f3d02ca..e617413 100644
@@ -1164,7 +1164,7 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 		goto err_task_lock;
 	}
 
-	if (oom_score_adj < task->signal->oom_score_adj &&
+	if (oom_score_adj < task->signal->oom_score_adj_min &&
 			!capable(CAP_SYS_RESOURCE)) {
 		err = -EACCES;
 		goto err_sighand;

fs/proc/base.c static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, · line 1184

Quoted 8 lines
fs/proc/base.c +2 -0
@@ -1177,6 +1177,8 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 			atomic_dec(&task->mm->oom_disable_count);
 	}
 	task->signal->oom_score_adj = oom_score_adj;
+	if (capable(CAP_SYS_RESOURCE))
+		task->signal->oom_score_adj_min = oom_score_adj;
 	/*
 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
 	 * always attainable.

This should be has_capability_noaudit(current, CAP_SYS_RESOURCE) instead, we don't want an audit message to be emitted when checking if oom_score_adj_min should be set.

Other than that:

#11 lore raw

PATCH v3 1/1 oom: allow a non-CAP_SYS_RESOURCE proces to oom_score_adj down

We'd like to be able to oom_score_adj a process up/down as its enters/leaves the foreground. Currently, it is not possible to oom_adj down without CAP_SYS_RESOURCE. This patch allows a task to decrease its oom_score_adj back to the value that a CAP_SYS_RESOURCE thread set it or its inherited value at fork. Assuming the thread that has forked it has oom_score_adj of 0, each tab process could decrease it back from 0 upon activation unless a CAP_SYS_RESOURCE thread elevated it to something higher.

Alternative considered:

* a setuid binary
* a daemon with CAP_SYS_RESOURCE

Since you don't wan't all processes to be able to reduce their oom_adj, a setuid or daemon implementation would be complex. The alternatives also have much higher overhead.

This patch updated from original patch based on feedback from David Rientjes <[email protected]>.

---
 Documentation/filesystems/proc.txt |    4 ++++
 fs/proc/base.c                     |    4 +++-
 include/linux/sched.h              |    2 ++
 kernel/fork.c                      |    1 +
 4 files changed, 10 insertions(+), 1 deletions(-)
Documentation/filesystems/proc.txt +4 -0
index e73df27..7139c50 100644
@@ -1296,6 +1296,10 @@ scaled linearly with /proc/<pid>/oom_score_adj.
 Writing to /proc/<pid>/oom_score_adj or /proc/<pid>/oom_adj will change the
 other with its scaled value.
 
+The value of /proc/<pid>/oom_score_adj may be reduced no lower than the last
+value set by a CAP_SYS_RESOURCE process. To reduce the value any lower
+requires CAP_SYS_RESOURCE.
+
 NOTICE: /proc/<pid>/oom_adj is deprecated and will be removed, please see
 Documentation/feature-removal-schedule.txt.
 
fs/proc/base.c +3 -1
index f3d02ca..7b1a9df 100644
@@ -1164,7 +1164,7 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 		goto err_task_lock;
 	}
 
-	if (oom_score_adj < task->signal->oom_score_adj &&
+	if (oom_score_adj < task->signal->oom_score_adj_min &&
 			!capable(CAP_SYS_RESOURCE)) {
 		err = -EACCES;
 		goto err_sighand;
@@ -1177,6 +1177,8 @@ static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
 			atomic_dec(&task->mm->oom_disable_count);
 	}
 	task->signal->oom_score_adj = oom_score_adj;
+	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
+		task->signal->oom_score_adj_min = oom_score_adj;
 	/*
 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
 	 * always attainable.
include/linux/sched.h +2 -0
index f53cdf2..2a71ee0 100644
@@ -626,6 +626,8 @@ struct signal_struct {
 
 	int oom_adj;		/* OOM kill score adjustment (bit shift) */
 	int oom_score_adj;	/* OOM kill score adjustment */
+	int oom_score_adj_min;	/* OOM kill score adjustment minimum value.
+				 * Only settable by CAP_SYS_RESOURCE. */
 
 	struct mutex cred_guard_mutex;	/* guard against foreign influences on
 					 * credential calculations
kernel/fork.c +1 -0
index 3b159c5..0979527 100644
@@ -907,6 +907,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
 
 	sig->oom_adj = current->signal->oom_adj;
 	sig->oom_score_adj = current->signal->oom_score_adj;
+	sig->oom_score_adj_min = current->signal->oom_score_adj_min;
 
 	mutex_init(&sig->cred_guard_mutex);
 
Signature · 1 line
1.7.3.1