]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
chrt: Add uclamp reset support via -1 sentinel
authorFurkan Caliskan <frn1furkan10@gmail.com>
Fri, 15 May 2026 05:37:25 +0000 (08:37 +0300)
committerFurkan Caliskan <frn1furkan10@gmail.com>
Mon, 25 May 2026 11:27:34 +0000 (14:27 +0300)
Passing -1 to --clamp-min or --clamp-max sets the corresponding
sched_attr field to UINT32_MAX, which tells the kernel to reset
the utilization clamp to its system default. Resetting requires
kernel >= 5.11, otherwise the syscall will fail with EINVAL.

Closes: https://github.com/util-linux/util-linux/issues/4339
Suggested-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Signed-off-by: Furkan Caliskan <frn1furkan10@gmail.com>
schedutils/chrt.1.adoc
schedutils/chrt.c

index bd77200237f96cf62dbe64b31b0ed37b9131e88c..5cc78132765c44d26185aa15294be8b837c56586 100644 (file)
@@ -89,10 +89,10 @@ Enables GRUB (Greedy Reclamation of Unused Bandwidth) algorithm. Linux-specific,
 Set *SCHED_FLAG_DL_OVERRUN* to receive *SIGXCPU* when a *SCHED_DEADLINE* task exceeds its runtime budget within a period. Without this flag the task is silently throttled until the next period. Linux-specific, supported since 4.16.
 
 *-U*, *--clamp-min* _value_::
-Set *SCHED_FLAG_UTIL_CLAMP_MIN* to define the minimum utilization hint for the task (range 0-1024, where 1024 represents 100% of maximum CPU capacity). Tells the scheduler to place the task on a cpu with enough capacity. Linux-specific, supported since 5.3.
+Set *SCHED_FLAG_UTIL_CLAMP_MIN* to define the minimum utilization hint for the task (range 0-1024, where 1024 represents 100% of maximum CPU capacity), or *-1* to reset to the system default. Resetting requires kernel >= 5.11, otherwise the syscall will fail with *EINVAL*. Tells the scheduler to place the task on a cpu with enough capacity. Linux-specific, supported since 5.3.
 
 *-X*, *--clamp-max* _value_::
-Set *SCHED_FLAG_UTIL_CLAMP_MAX* to define the maximum utilization cap for the task (range 0-1024). Tells the scheduler not to boost CPU frequency unnecessarily for this task. Linux-specific, supported since 5.3.
+Set *SCHED_FLAG_UTIL_CLAMP_MAX* to define the maximum utilization cap for the task (range 0-1024), or *-1* to reset to the system default. Resetting requires kernel >= 5.11, otherwise the syscall will fail with *EINVAL*. Tells the scheduler not to boost CPU frequency unnecessarily for this task. Linux-specific, supported since 5.3.
 
 *-R*, *--reset-on-fork*::
 Use *SCHED_RESET_ON_FORK* or *SCHED_FLAG_RESET_ON_FORK* flag. Linux-specific, supported since 2.6.31.
index 69de3c0f9fbbc24a150dc05b4024c71d21d92348..f968b269673b923a28a1ba73f473a83781ffbb68 100644 (file)
@@ -37,6 +37,7 @@
 #include "sched_attr.h"
 #include "pidutils.h"
 #include "pidfd-utils.h"
+#include "linux_version.h"
 
 /* control struct */
 struct chrt_ctl {
@@ -100,8 +101,8 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -T, --sched-runtime <ns>  runtime parameter for DEADLINE\n"), out);
        fputs(_(" -P, --sched-period <ns>   period parameter for DEADLINE\n"), out);
        fputs(_(" -D, --sched-deadline <ns> deadline parameter for DEADLINE\n"), out);
-       fputs(_(" -U, --clamp-min <value>   set SCHED_FLAG_UTIL_CLAMP_MIN (0-1024)\n"), out);
-       fputs(_(" -X, --clamp-max <value>   set SCHED_FLAG_UTIL_CLAMP_MAX (0-1024)\n"), out);
+       fputs(_(" -U, --clamp-min <value>   set SCHED_FLAG_UTIL_CLAMP_MIN (0-1024 or -1 to reset)\n"), out);
+       fputs(_(" -X, --clamp-max <value>   set SCHED_FLAG_UTIL_CLAMP_MAX (0-1024 or -1 to reset)\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
        fputs(_("Other options:\n"), out);
@@ -382,6 +383,7 @@ static int set_sched_one(struct chrt_ctl *ctl, pid_t pid)
 static int set_sched_one(struct chrt_ctl *ctl, pid_t pid)
 {
        struct sched_attr sa = { .size = sizeof(struct sched_attr) };
+       int ret;
 
        /* old API is good enough for non-deadline */
        if (!supports_runtime_param(ctl->policy))
@@ -406,7 +408,12 @@ static int set_sched_one(struct chrt_ctl *ctl, pid_t pid)
                sa.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
 # endif
        errno = 0;
-       return sched_setattr(pid, &sa, 0);
+       ret = sched_setattr(pid, &sa, 0);
+       if (ret == -1 && errno == EINVAL
+               && (ctl->util_min == UINT32_MAX || ctl->util_max == UINT32_MAX)
+               && get_linux_version() < KERNEL_VERSION(5, 11, 0))
+                       warnx(_("--clamp-min and --clamp-max reset may be unsupported on Linux < 5.11"));
+       return ret;
 }
 #endif /* HAVE_SCHED_SETATTR */
 
@@ -520,13 +527,21 @@ int main(int argc, char **argv)
                case 'U':
 #ifdef SCHED_FLAG_UTIL_CLAMP_MIN
                        ctl->sched_flags |= SCHED_FLAG_UTIL_CLAMP_MIN;
-                       ctl->util_min = (uint32_t) str2unum_or_err(optarg, 10, _("--clamp-min value must be in range 0-1024"), 1024);
+                       if (strcmp(optarg, "-1") == 0)
+                               ctl->util_min = UINT32_MAX;
+                       else
+                               ctl->util_min = (uint32_t) str2unum_or_err(optarg, 10,
+                                       _("--clamp-min value must be in range 0-1024 or -1 to reset"), 1024);
 #endif
                        break;
                case 'X':
 #ifdef SCHED_FLAG_UTIL_CLAMP_MAX
                        ctl->sched_flags |= SCHED_FLAG_UTIL_CLAMP_MAX;
-                       ctl->util_max = (uint32_t) str2unum_or_err(optarg, 10, _("--clamp-max value must be in range 0-1024"), 1024);
+                       if (strcmp(optarg, "-1") == 0)
+                               ctl->util_max = UINT32_MAX;
+                       else
+                               ctl->util_max = (uint32_t) str2unum_or_err(optarg, 10,
+                                       _("--clamp-max value must be in range 0-1024 or -1 to reset"), 1024);
 #endif
                        break;
                case 'i':