Skip to contentSkip to conversation

scheduler: introduce SCHED_RESET_ON_FORK scheduling policy flag

PATCH In review Lennart Poettering 2 participants 2 messages Jun 15, 2009 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scheduler: introduce SCHED_RESET_ON_FORK scheduling policy flag

(This is the third version of the patch, only change is to move the reset_on_fork flag into a bit hole in the task struct and a rebase on current master, Peter or Ingo, may I ask you to merge that into your trees?)

This patch introduces a new flag SCHED_RESET_ON_FORK which can be passed to the kernel via sched_setscheduler(), ORed in the policy parameter. If set this will make sure that when the process forks a) the scheduling priority is reset to DEFAULT_PRIO if it was higher and b) the scheduling policy is reset to SCHED_NORMAL if it was either SCHED_FIFO or SCHED_RR.

Why have this?

Currently, if a process is real-time scheduled this will 'leak' to all its child processes. For security reasons it is often (always?) a good idea to make sure that if a process acquires RT scheduling this is confined to this process and only this process. More specifically this makes the per-process resource limit RLIMIT_RTTIME useful for security purposes, because it makes it impossible to use a fork bomb to circumvent the per-process RLIMIT_RTTIME accounting.

This feature is also useful for tools like 'renice' which can then change the nice level of a process without having this spill to all its child processes.

Why expose this via sched_setscheduler() and not other syscalls such as prctl() or sched_setparam()?

prctl() does not take a pid parameter. Due to that it would be impossible to modify this flag for other processes than the current one.

The struct passed to sched_setparam() can unfortunately not be extended without breaking compatibility, since sched_setparam() lacks a size parameter.

How to use this from userspace? In your RT program simply replace this:

  sched_setscheduler(pid, SCHED_FIFO, &param);

by this:

  sched_setscheduler(pid, SCHED_FIFO|SCHED_RESET_ON_FORK, &param);

Lennart

---
 include/linux/sched.h |    6 ++++++
 kernel/sched.c        |   47 ++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 44 insertions(+), 9 deletions(-)
include/linux/sched.h +6 -0
index 83f3564..bd503ce 100644
@@ -38,6 +38,8 @@
 #define SCHED_BATCH		3
 /* SCHED_ISO: reserved but not implemented yet */
 #define SCHED_IDLE		5
+/* Can be ORed in to make sure the process is reverted back to SCHED_NORMAL on fork */
+#define SCHED_RESET_ON_FORK     0x40000000
 
 #ifdef __KERNEL__
 
@@ -1209,6 +1211,10 @@ struct task_struct {
 	unsigned did_exec:1;
 	unsigned in_execve:1;	/* Tell the LSMs that the process is doing an
 				 * execve */
+
+	/* Revert to default priority/policy when forking */
+	unsigned sched_reset_on_fork:1;
+
 	pid_t pid;
 	pid_t tgid;
 
kernel/sched.c +38 -9

93 lines not on this page · raw message

Signature · 10 lines
1.6.2.2



Lennart

-- 
Lennart Poettering                        Red Hat, Inc.
lennart [at] poettering [dot] net
http://0pointer.net/lennart/           GnuPG 0x1A015CC4
#1 lore raw
Quoted 4 lines · Lennart Poettering

(This is the third version of the patch, only change is to move the reset_on_fork flag into a bit hole in the task struct and a rebase on current master, Peter or Ingo, may I ask you to merge that into your trees?)

Could you please send your signed-off-by line?

Thanks,

Ingo