439b44
From c4caa5b973f9cdb4c43edea3c32cda62cdf15b3b Mon Sep 17 00:00:00 2001
439b44
From: Karel Zak <kzak@redhat.com>
439b44
Date: Thu, 1 Oct 2020 11:20:01 +0200
439b44
Subject: [PATCH 44/55] chrt: don't restrict --reset-on-fork, add more info to
439b44
 man page
439b44
439b44
The flag works (= kernel accepts it) for all scheduling policies
439b44
and sched_getattr() returns the flag for all policies.
439b44
439b44
There is no reason for userspace to be more smart than kernel or hide
439b44
the flag when it prints sched_getattr()/sched_getscheduler() results.
439b44
439b44
 # chrt -v --reset-on-fork --batch 0 /bin/true
439b44
 pid 1315019's new scheduling policy: SCHED_BATCH|SCHED_RESET_ON_FORK
439b44
439b44
 # chrt -v --reset-on-fork --fifo 1 /bin/true
439b44
 pid 1315055's new scheduling policy: SCHED_FIFO|SCHED_RESET_ON_FORK
439b44
439b44
 # chrt -v --reset-on-fork --deadline --sched-period 10000 0 /bin/true
439b44
 pid 1315182's new scheduling policy: SCHED_DEADLINE|SCHED_RESET_ON_FORK
439b44
439b44
 # chrt -v --reset-on-fork --idle 0 /bin/true
439b44
 pid 1315247's new scheduling policy: SCHED_IDLE|SCHED_RESET_ON_FORK
439b44
439b44
 # chrt -v --reset-on-fork --rr 1 /bin/true
439b44
 pid 1315275's new scheduling policy: SCHED_RR|SCHED_RESET_ON_FORK
439b44
439b44
 # chrt -v --reset-on-fork --other 0 /bin/true
439b44
 pid 1315311's new scheduling policy: SCHED_OTHER|SCHED_RESET_ON_FORK
439b44
439b44
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1884194
439b44
Signed-off-by: Karel Zak <kzak@redhat.com>
439b44
---
439b44
 schedutils/chrt.1 | 32 ++++++++++++++++++++++++++++----
439b44
 schedutils/chrt.c | 18 +++++-------------
439b44
 2 files changed, 33 insertions(+), 17 deletions(-)
439b44
439b44
diff --git a/schedutils/chrt.1 b/schedutils/chrt.1
439b44
index 4b8b1e9da..a9adfb316 100644
439b44
--- a/schedutils/chrt.1
439b44
+++ b/schedutils/chrt.1
439b44
@@ -92,13 +92,37 @@ Specifies period parameter for SCHED_DEADLINE policy (Linux-specific).
439b44
 Specifies deadline parameter for SCHED_DEADLINE policy (Linux-specific).
439b44
 .TP
439b44
 \fB\-R\fR, \fB\-\-reset-on-fork\fR
439b44
-Add
439b44
+Use
439b44
 .B SCHED_RESET_ON_FORK
439b44
-flag to the
439b44
+or
439b44
+.B SCHED_FLAG_RESET_ON_FORK
439b44
+flag.  Linux-specific, supported since 2.6.31.
439b44
+
439b44
+Each thread has a reset-on-fork scheduling flag.  When this flag is set, children created by
439b44
+.BR fork (2)
439b44
+do not inherit privileged scheduling policies.  After the reset-on-fork flag has been enabled,
439b44
+it can be reset only if the thread has the
439b44
+.BR CAP_SYS_NICE
439b44
+capability.  This flag is disabled in child processes created by
439b44
+.BR fork (2).
439b44
+
439b44
+More precisely, if the reset-on-fork flag is set,
439b44
+the following rules apply for subsequently created children:
439b44
+.RS
439b44
+.IP * 3
439b44
+If the calling thread has a scheduling policy of
439b44
 .B SCHED_FIFO
439b44
 or
439b44
-.B SCHED_RR
439b44
-scheduling policy (Linux-specific, supported since 2.6.31).
439b44
+.BR SCHED_RR ,
439b44
+the policy is reset to
439b44
+.BR SCHED_OTHER
439b44
+in child processes.
439b44
+.IP *
439b44
+If the calling process has a negative nice value,
439b44
+the nice value is reset to zero in child processes.
439b44
+.RE
439b44
+
439b44
+
439b44
 
439b44
 .SH OPTIONS
439b44
 .TP
439b44
diff --git a/schedutils/chrt.c b/schedutils/chrt.c
439b44
index 15556bbad..2a6f1e151 100644
439b44
--- a/schedutils/chrt.c
439b44
+++ b/schedutils/chrt.c
439b44
@@ -152,7 +152,7 @@ static void __attribute__((__noreturn__)) usage(void)
439b44
 
439b44
 	fputs(USAGE_SEPARATOR, out);
439b44
 	fputs(_("Scheduling options:\n"), out);
439b44
-	fputs(_(" -R, --reset-on-fork       set SCHED_RESET_ON_FORK for FIFO or RR\n"), out);
439b44
+	fputs(_(" -R, --reset-on-fork       set reset-on-fork flag\n"), out);
439b44
 	fputs(_(" -T, --sched-runtime <ns>  runtime parameter for DEADLINE\n"), out);
439b44
 	fputs(_(" -P, --sched-period <ns>   period parameter for DEADLINE\n"), out);
439b44
 	fputs(_(" -D, --sched-deadline <ns> deadline parameter for DEADLINE\n"), out);
439b44
@@ -173,22 +173,19 @@ static void __attribute__((__noreturn__)) usage(void)
439b44
 
439b44
 static const char *get_policy_name(int policy)
439b44
 {
439b44
+#ifdef SCHED_RESET_ON_FORK
439b44
+	policy &= ~SCHED_RESET_ON_FORK;
439b44
+#endif
439b44
 	switch (policy) {
439b44
 	case SCHED_OTHER:
439b44
 		return "SCHED_OTHER";
439b44
 	case SCHED_FIFO:
439b44
-#ifdef SCHED_RESET_ON_FORK
439b44
-	case SCHED_FIFO | SCHED_RESET_ON_FORK:
439b44
-#endif
439b44
 		return "SCHED_FIFO";
439b44
 #ifdef SCHED_IDLE
439b44
 	case SCHED_IDLE:
439b44
 		return "SCHED_IDLE";
439b44
 #endif
439b44
 	case SCHED_RR:
439b44
-#ifdef SCHED_RESET_ON_FORK
439b44
-	case SCHED_RR | SCHED_RESET_ON_FORK:
439b44
-#endif
439b44
 		return "SCHED_RR";
439b44
 #ifdef SCHED_BATCH
439b44
 	case SCHED_BATCH:
439b44
@@ -257,7 +254,7 @@ fallback:
439b44
 		else
439b44
 			prio = sp.sched_priority;
439b44
 # ifdef SCHED_RESET_ON_FORK
439b44
-		if (policy == (SCHED_FIFO|SCHED_RESET_ON_FORK) || policy == (SCHED_BATCH|SCHED_RESET_ON_FORK))
439b44
+		if (policy & SCHED_RESET_ON_FORK)
439b44
 			reset_on_fork = 1;
439b44
 # endif
439b44
 	}
439b44
@@ -515,11 +512,6 @@ int main(int argc, char **argv)
439b44
 	errno = 0;
439b44
 	ctl->priority = strtos32_or_err(argv[optind], _("invalid priority argument"));
439b44
 
439b44
-#ifdef SCHED_RESET_ON_FORK
439b44
-	if (ctl->reset_on_fork && ctl->policy != SCHED_FIFO && ctl->policy != SCHED_RR)
439b44
-		errx(EXIT_FAILURE, _("--reset-on-fork option is supported for "
439b44
-				     "SCHED_FIFO and SCHED_RR policies only"));
439b44
-#endif
439b44
 #ifdef SCHED_DEADLINE
439b44
 	if ((ctl->runtime || ctl->deadline || ctl->period) && ctl->policy != SCHED_DEADLINE)
439b44
 		errx(EXIT_FAILURE, _("--sched-{runtime,deadline,period} options "
439b44
-- 
439b44
2.29.2
439b44