From: Furkan Caliskan Date: Fri, 15 May 2026 05:37:25 +0000 (+0300) Subject: chrt: Add uclamp reset support via -1 sentinel X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=d671dbdd1aa2de5762301fca2544213b66903bdc;p=thirdparty%2Futil-linux.git chrt: Add uclamp reset support via -1 sentinel 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 Signed-off-by: Furkan Caliskan --- diff --git a/schedutils/chrt.1.adoc b/schedutils/chrt.1.adoc index bd7720023..5cc781327 100644 --- a/schedutils/chrt.1.adoc +++ b/schedutils/chrt.1.adoc @@ -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. diff --git a/schedutils/chrt.c b/schedutils/chrt.c index 69de3c0f9..f968b2696 100644 --- a/schedutils/chrt.c +++ b/schedutils/chrt.c @@ -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 runtime parameter for DEADLINE\n"), out); fputs(_(" -P, --sched-period period parameter for DEADLINE\n"), out); fputs(_(" -D, --sched-deadline deadline parameter for DEADLINE\n"), out); - fputs(_(" -U, --clamp-min set SCHED_FLAG_UTIL_CLAMP_MIN (0-1024)\n"), out); - fputs(_(" -X, --clamp-max set SCHED_FLAG_UTIL_CLAMP_MAX (0-1024)\n"), out); + fputs(_(" -U, --clamp-min set SCHED_FLAG_UTIL_CLAMP_MIN (0-1024 or -1 to reset)\n"), out); + fputs(_(" -X, --clamp-max 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':