scheduler: introduce SCHED_RESET_ON_FORK scheduling policy flag, fourth try
PATCH 1/1 scheduler: introduce SCHED_RESET_ON_FORK scheduling policy flag, fourth try
(this is the fourth version of the patch, only change is that I added the missing Signed-off-by line, as Ingo requested)
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, ¶m);
by this:
sched_setscheduler(pid, SCHED_FIFO|SCHED_RESET_ON_FORK, ¶m);
Lennart
--- include/linux/sched.h | 6 ++++++ kernel/sched.c | 47 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 9 deletions(-)
include/linux/sched.h +6 -0
@@ -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
[tip:sched/core] sched: Introduce SCHED_RESET_ON_FORK scheduling policy flag
Commit-ID: ca94c442535a44d508c99a77e54f21a59f4fc462 Gitweb: http://git.kernel.org/tip/ca94c442535a44d508c99a77e54f21a59f4fc462 Author: Lennart Poettering <[email protected]> AuthorDate: Mon, 15 Jun 2009 17:17:47 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Mon, 15 Jun 2009 17:31:59 +0200
sched: Introduce SCHED_RESET_ON_FORK scheduling policy flag
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, ¶m);
by this:
sched_setscheduler(pid, SCHED_FIFO|SCHED_RESET_ON_FORK, ¶m);
LKML-Reference: <[email protected]>
--- include/linux/sched.h | 6 ++++++ kernel/sched.c | 49 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 9 deletions(-)
include/linux/sched.h +6 -0
@@ -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 +40 -9
95 lines not on this page · raw message
Quoted 19 lines · Lennart Poettering
(this is the fourth version of the patch, only change is that I added the missing Signed-off-by line, as Ingo requested)
This patch introduces a new flag
SCHED_RESET_ON_FORKwhich can be passed to the kernel viasched_setscheduler(), ORed in the policy parameter. If set this will make sure that when the process forks a) the scheduling priority is reset toDEFAULT_PRIOif it was higher and b) the scheduling policy is reset toSCHED_NORMALif it was eitherSCHED_FIFOorSCHED_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_RTTIMEuseful for security purposes, because it makes it impossible to use a fork bomb to circumvent the per-processRLIMIT_RTTIMEaccounting.
Quoted 3 lines
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.
That didn't work for me, reniced tasks with the flag set retained their -nice status. See patchlet and comments below.
kernel/sched.c void sched_fork(struct task_struct *p, int clone_flags) · line 2618
Quoted 13 lines
kernel/sched.c +2
-2@@ -2613,12 +2613,26 @@ void sched_fork(struct task_struct *p, int clone_flags)set_task_cpu (p, cpu);/* -* Make sure we do not leak PI boosting priority to the child: +* Revert to default priority/policy on fork if requested. Make sure we +* do not leak PI boosting priority to the child. */ - p->prio = current->normal_prio;
Nit: that comment/assignment was placed there to make sure that readers knew that this specific line was critical to PI. Now, it's mixed in with something unrelated.
kernel/sched.c void sched_fork(struct task_struct *p, int clone_flags) · line 2628
Quoted 10 lines
kernel/sched.c +10
-0+if (current->sched_reset_on_fork && + (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR )) + p->policy =SCHED_NORMAL ; + +if (current->sched_reset_on_fork && + (current->normal_prio <DEFAULT_PRIO )) + p->prio =DEFAULT_PRIO ; +else + p->prio = current->normal_prio; +
I think it's cleaner to keep reset_on_fork functionality separate. Thoughts on the below?
Make SCHED_RESET_ON_FORK to DTRT for reniced tasks, and make the sched_fork() SCHED_RESET_ON_FORK bits a self-contained unlikely code block.
kernel/sched.c | 39 +++++++++++++++++++++++---------------- 1 files changed, 23 insertions(+), 16 deletions(-)
kernel/sched.c +23 -16
@@ -2613,28 +2613,35 @@ void sched_fork(struct task_struct *p, int clone_flags)set_task_cpu (p, cpu);/* -* Revert to default priority/policy on fork if requested. Make sure we -* do not leak PI boosting priority to the child. +* Make sure we do not leak PI boosting priority to the child. */ -if (current->sched_reset_on_fork && - (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR )) - p->policy =SCHED_NORMAL ; + p->prio = current->normal_prio; -if (current->sched_reset_on_fork && - (current->normal_prio <DEFAULT_PRIO )) - p->prio =DEFAULT_PRIO ; -else - p->prio = current->normal_prio; +/* +* Revert to default priority/policy on fork if requested. +*/ +if (unlikely (p->sched_reset_on_fork)) { +if (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR ) + p->policy =SCHED_NORMAL ; + +if (p->normal_prio <DEFAULT_PRIO ) + p->prio =DEFAULT_PRIO ; + +if (PRIO_TO_NICE (p->static_prio) <0 ) { + p->static_prio =NICE_TO_PRIO (0 ); +set_load_weight (p); + } + +/* +* We don't need the reset flag anymore after the fork. It has +* fulfilled its duty: +*/ + p->sched_reset_on_fork =0 ; + }if (!rt_prio (p->prio)) p->sched_class = &fair_sched_class; -/* -* We don't need the reset flag anymore after the fork. It has -* fulfilled its duty: -*/ - p->sched_reset_on_fork =0 ; -#if defined(CONFIG_SCHEDSTATS ) || defined(CONFIG_TASK_DELAY_ACCT )if (likely (sched_info_on ()))memset (&p->sched_info,0 ,sizeof (p->sched_info));
Quoted 2 lines · Mike Galbraith
I think it's cleaner to keep
reset_on_forkfunctionality separate. Thoughts on the below?
Looks good to me!
Lennart
Quoted 55 lines
Make
SCHED_RESET_ON_FORKto DTRT for reniced tasks, and make thesched_fork()SCHED_RESET_ON_FORKbits a self-contained unlikely code block.Signed-off-by Mike Galbraith <[email protected]>kernel/sched.c | 39 +++++++++++++++++++++++---------------- 1 files changed, 23 insertions(+), 16 deletions(-)kernel/sched.c +23
-10@@ -2613,28 +2613,35 @@ void sched_fork(struct task_struct *p, int clone_flags)set_task_cpu (p, cpu);/* -* Revert to default priority/policy on fork if requested. Make sure we -* do not leak PI boosting priority to the child. +* Make sure we do not leak PI boosting priority to the child. */ -if (current->sched_reset_on_fork && - (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR )) - p->policy =SCHED_NORMAL ; + p->prio = current->normal_prio; -if (current->sched_reset_on_fork && - (current->normal_prio <DEFAULT_PRIO )) - p->prio =DEFAULT_PRIO ; -else - p->prio = current->normal_prio; +/* +* Revert to default priority/policy on fork if requested. +*/ +if (unlikely (p->sched_reset_on_fork)) { +if (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR ) + p->policy =SCHED_NORMAL ; + +if (p->normal_prio <DEFAULT_PRIO ) + p->prio =DEFAULT_PRIO ; + +if (PRIO_TO_NICE (p->static_prio) <0 ) { + p->static_prio =NICE_TO_PRIO (0 ); +set_load_weight (p); + } + +/* +* We don't need the reset flag anymore after the fork. It has +* fulfilled its duty: +*/ + p->sched_reset_on_fork =0 ; + }if (!rt_prio (p->prio)) p->sched_class = &fair_sched_class;
kernel/sched.c void sched_fork(struct task_struct *p, int clone_flags) · line 2647
Quoted 10 lines
kernel/sched.c +0
-6-/* -* We don't need the reset flag anymore after the fork. It has -* fulfilled its duty: -*/ - p->sched_reset_on_fork =0 ; -#if defined(CONFIG_SCHEDSTATS ) || defined(CONFIG_TASK_DELAY_ACCT )if (likely (sched_info_on ()))memset (&p->sched_info,0 ,sizeof (p->sched_info));
Lennart
Signature · 3 lines
Lennart Poettering Red Hat, Inc. lennart [at] poettering [dot] net http://0pointer.net/lennart/ GnuPG 0x1A015CC4
Quoted 6 lines · Lennart Poettering
Quoted 2 lines · Mike Galbraith
I think it's cleaner to keep
reset_on_forkfunctionality separate. Thoughts on the below?Looks good to me!
OK, I'll submit then, splitting cleanup from functionality addition.
commit 58e1710a8a525e0fb1f4c608d5f73377fb56c893
Author: Mike Galbraith <[email protected]> Date: Wed Jun 17 10:16:26 2009 +0200
sched: SCHED_RESET_ON_FORK cleanup.
Make SCHED_RESET_ON_FORK sched_fork() bits a self-contained unlikely code path.
LKML-Reference: <new-submission>
--- kernel/sched.c | 34 ++++++++++++++++++---------------- 1 files changed, 18 insertions(+), 16 deletions(-)
kernel/sched.c +18 -16
@@ -2613,28 +2613,30 @@ void sched_fork(struct task_struct *p, int clone_flags)set_task_cpu (p, cpu);/* -* Revert to default priority/policy on fork if requested. Make sure we -* do not leak PI boosting priority to the child. +* Make sure we do not leak PI boosting priority to the child. */ -if (current->sched_reset_on_fork && - (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR )) - p->policy =SCHED_NORMAL ; + p->prio = current->normal_prio; -if (current->sched_reset_on_fork && - (current->normal_prio <DEFAULT_PRIO )) - p->prio =DEFAULT_PRIO ; -else - p->prio = current->normal_prio; +/* +* Revert to default priority/policy on fork if requested. +*/ +if (unlikely (p->sched_reset_on_fork)) { +if (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR ) + p->policy =SCHED_NORMAL ; + +if (p->normal_prio <DEFAULT_PRIO ) + p->prio =DEFAULT_PRIO ; + +/* +* We don't need the reset flag anymore after the fork. It has +* fulfilled its duty: +*/ + p->sched_reset_on_fork =0 ; + }if (!rt_prio (p->prio)) p->sched_class = &fair_sched_class; -/* -* We don't need the reset flag anymore after the fork. It has -* fulfilled its duty: -*/ - p->sched_reset_on_fork =0 ; -#if defined(CONFIG_SCHEDSTATS ) || defined(CONFIG_TASK_DELAY_ACCT )if (likely (sched_info_on ()))memset (&p->sched_info,0 ,sizeof (p->sched_info));
commit 50f3ff67c7fbcd37f0a135113ecd56f724d9b081
Author: Mike Galbraith <[email protected]> Date: Wed Jun 17 10:30:19 2009 +0200
sched: Add SCHED_RESET_ON_FORK functionality for nice < 0 tasks.
LKML-Reference: <new-submission>
--- kernel/sched.c | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)
kernel/sched.c +5 -0
@@ -2627,6 +2627,11 @@ void sched_fork(struct task_struct *p, int clone_flags)if (p->normal_prio <DEFAULT_PRIO ) p->prio =DEFAULT_PRIO ; +if (PRIO_TO_NICE (p->static_prio) <0 ) { + p->static_prio =NICE_TO_PRIO (0 ); +set_load_weight (p); + } +/* * We don't need the reset flag anymore after the fork. It has * fulfilled its duty:
[tip:sched/core] sched: Add SCHED_RESET_ON_FORK functionality for nice < 0 tasks
Commit-ID: 6c697bdf08a09ce461e305a22362973036e95db3 Gitweb: http://git.kernel.org/tip/6c697bdf08a09ce461e305a22362973036e95db3 Author: Mike Galbraith <[email protected]> AuthorDate: Wed, 17 Jun 2009 10:48:02 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 17 Jun 2009 18:34:18 +0200
sched: Add SCHED_RESET_ON_FORK functionality for nice < 0 tasks
LKML-Reference: <[email protected]>
--- kernel/sched.c | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)
kernel/sched.c +5 -0
@@ -2627,6 +2627,11 @@ void sched_fork(struct task_struct *p, int clone_flags)if (p->normal_prio <DEFAULT_PRIO ) p->prio =DEFAULT_PRIO ; +if (PRIO_TO_NICE (p->static_prio) <0 ) { + p->static_prio =NICE_TO_PRIO (0 ); +set_load_weight (p); + } +/* * We don't need the reset flag anymore after the fork. It has * fulfilled its duty:
[tip:sched/core] sched: Clean up SCHED_RESET_ON_FORK
In reply to Mike GalbraithCommit-ID: b9dc29e72fd3dc2a739ce8eafd958220d0745734 Gitweb: http://git.kernel.org/tip/b9dc29e72fd3dc2a739ce8eafd958220d0745734 Author: Mike Galbraith <[email protected]> AuthorDate: Wed, 17 Jun 2009 10:46:01 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 17 Jun 2009 18:34:17 +0200
sched: Clean up SCHED_RESET_ON_FORK
Make SCHED_RESET_ON_FORK sched_fork() bits a self-contained unlikely code path.
LKML-Reference: <[email protected]>
--- kernel/sched.c | 34 ++++++++++++++++++---------------- 1 files changed, 18 insertions(+), 16 deletions(-)
kernel/sched.c +18 -16
@@ -2613,28 +2613,30 @@ void sched_fork(struct task_struct *p, int clone_flags)set_task_cpu (p, cpu);/* -* Revert to default priority/policy on fork if requested. Make sure we -* do not leak PI boosting priority to the child. +* Make sure we do not leak PI boosting priority to the child. */ -if (current->sched_reset_on_fork && - (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR )) - p->policy =SCHED_NORMAL ; + p->prio = current->normal_prio; -if (current->sched_reset_on_fork && - (current->normal_prio <DEFAULT_PRIO )) - p->prio =DEFAULT_PRIO ; -else - p->prio = current->normal_prio; +/* +* Revert to default priority/policy on fork if requested. +*/ +if (unlikely (p->sched_reset_on_fork)) { +if (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR ) + p->policy =SCHED_NORMAL ; + +if (p->normal_prio <DEFAULT_PRIO ) + p->prio =DEFAULT_PRIO ; + +/* +* We don't need the reset flag anymore after the fork. It has +* fulfilled its duty: +*/ + p->sched_reset_on_fork =0 ; + }if (!rt_prio (p->prio)) p->sched_class = &fair_sched_class; -/* -* We don't need the reset flag anymore after the fork. It has -* fulfilled its duty: -*/ - p->sched_reset_on_fork =0 ; -#if defined(CONFIG_SCHEDSTATS ) || defined(CONFIG_TASK_DELAY_ACCT )if (likely (sched_info_on ()))memset (&p->sched_info,0 ,sizeof (p->sched_info));
[tip:sched/core] sched: Introduce SCHED_RESET_ON_FORK scheduling policy flag, fix
In reply to Mike GalbraithCommit-ID: e53438963eacc5624b61293f64e9f93c71f71198 Gitweb: http://git.kernel.org/tip/e53438963eacc5624b61293f64e9f93c71f71198 Author: Mike Galbraith <[email protected]> AuthorDate: Tue, 16 Jun 2009 11:05:08 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 17 Jun 2009 09:01:51 +0200
sched: Introduce SCHED_RESET_ON_FORK scheduling policy flag, fix
Make SCHED_RESET_ON_FORK to DTRT for reniced tasks, and make the sched_fork() SCHED_RESET_ON_FORK bits a self-contained unlikely code block.
LKML-Reference: <[email protected]>
kernel/sched.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-)
--- kernel/sched.c | 39 +++++++++++++++++++++++---------------- 1 files changed, 23 insertions(+), 16 deletions(-)
kernel/sched.c +23 -16
@@ -2613,28 +2613,35 @@ void sched_fork(struct task_struct *p, int clone_flags)set_task_cpu (p, cpu);/* -* Revert to default priority/policy on fork if requested. Make sure we -* do not leak PI boosting priority to the child. +* Make sure we do not leak PI boosting priority to the child. */ -if (current->sched_reset_on_fork && - (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR )) - p->policy =SCHED_NORMAL ; + p->prio = current->normal_prio; -if (current->sched_reset_on_fork && - (current->normal_prio <DEFAULT_PRIO )) - p->prio =DEFAULT_PRIO ; -else - p->prio = current->normal_prio; +/* +* Revert to default priority/policy on fork if requested. +*/ +if (unlikely (p->sched_reset_on_fork)) { +if (p->policy ==SCHED_FIFO || p->policy ==SCHED_RR ) + p->policy =SCHED_NORMAL ; + +if (p->normal_prio <DEFAULT_PRIO ) + p->prio =DEFAULT_PRIO ; + +if (PRIO_TO_NICE (p->static_prio) <0 ) { + p->static_prio =NICE_TO_PRIO (0 ); +set_load_weight (p); + } + +/* +* We don't need the reset flag anymore after the fork. It has +* fulfilled its duty: +*/ + p->sched_reset_on_fork =0 ; + }if (!rt_prio (p->prio)) p->sched_class = &fair_sched_class; -/* -* We don't need the reset flag anymore after the fork. It has -* fulfilled its duty: -*/ - p->sched_reset_on_fork =0 ; -#if defined(CONFIG_SCHEDSTATS ) || defined(CONFIG_TASK_DELAY_ACCT )if (likely (sched_info_on ()))memset (&p->sched_info,0 ,sizeof (p->sched_info));