dcavalca / rpms / util-linux

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