1 // SPDX-License-Identifier: GPL-2.0-only
3 * kernel/sched/syscalls.c
5 * Core kernel scheduler syscalls related code
7 * Copyright (C) 1991-2002 Linus Torvalds
8 * Copyright (C) 1998-2024 Ingo Molnar, Red Hat
10 #include <linux/sched.h>
11 #include <linux/cpuset.h>
12 #include <linux/sched/debug.h>
14 #include <uapi/linux/sched/types.h>
17 #include "autogroup.h"
19 static inline int __normal_prio(int policy, int rt_prio, int nice)
23 if (dl_policy(policy))
24 prio = MAX_DL_PRIO - 1;
25 else if (rt_policy(policy))
26 prio = MAX_RT_PRIO - 1 - rt_prio;
28 prio = NICE_TO_PRIO(nice);
34 * Calculate the expected normal priority: i.e. priority
35 * without taking RT-inheritance into account. Might be
36 * boosted by interactivity modifiers. Changes upon fork,
37 * setprio syscalls, and whenever the interactivity
38 * estimator recalculates.
40 static inline int normal_prio(struct task_struct *p)
42 return __normal_prio(p->policy, p->rt_priority, PRIO_TO_NICE(p->static_prio));
46 * Calculate the current priority, i.e. the priority
47 * taken into account by the scheduler. This value might
48 * be boosted by RT tasks, or might be boosted by
49 * interactivity modifiers. Will be RT if the task got
50 * RT-boosted. If not then it returns p->normal_prio.
52 static int effective_prio(struct task_struct *p)
54 p->normal_prio = normal_prio(p);
56 * If we are RT tasks or we were boosted to RT priority,
57 * keep the priority unchanged. Otherwise, update priority
58 * to the normal priority:
60 if (!rt_or_dl_prio(p->prio))
61 return p->normal_prio;
65 void set_user_nice(struct task_struct *p, long nice)
71 if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
74 * We have to be careful, if called from sys_setpriority(),
75 * the task might be in the middle of scheduling on another CPU.
77 CLASS(task_rq_lock, rq_guard)(p);
83 * The RT priorities are set via sched_setscheduler(), but we still
84 * allow the 'normal' nice value to be set - but as expected
85 * it won't have any effect on scheduling until the task is
86 * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
88 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
89 p->static_prio = NICE_TO_PRIO(nice);
93 queued = task_on_rq_queued(p);
94 running = task_current_donor(rq, p);
96 dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
100 p->static_prio = NICE_TO_PRIO(nice);
101 set_load_weight(p, true);
103 p->prio = effective_prio(p);
106 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
108 set_next_task(rq, p);
111 * If the task increased its priority or is running and
112 * lowered its priority, then reschedule its CPU:
114 p->sched_class->prio_changed(rq, p, old_prio);
116 EXPORT_SYMBOL(set_user_nice);
119 * is_nice_reduction - check if nice value is an actual reduction
121 * Similar to can_nice() but does not perform a capability check.
126 static bool is_nice_reduction(const struct task_struct *p, const int nice)
128 /* Convert nice value [19,-20] to rlimit style value [1,40]: */
129 int nice_rlim = nice_to_rlimit(nice);
131 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE));
135 * can_nice - check if a task can reduce its nice value
139 int can_nice(const struct task_struct *p, const int nice)
141 return is_nice_reduction(p, nice) || capable(CAP_SYS_NICE);
144 #ifdef __ARCH_WANT_SYS_NICE
147 * sys_nice - change the priority of the current process.
148 * @increment: priority increment
150 * sys_setpriority is a more generic, but much slower function that
151 * does similar things.
153 SYSCALL_DEFINE1(nice, int, increment)
158 * Setpriority might change our priority at the same moment.
159 * We don't have to worry. Conceptually one call occurs first
160 * and we have a single winner.
162 increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
163 nice = task_nice(current) + increment;
165 nice = clamp_val(nice, MIN_NICE, MAX_NICE);
166 if (increment < 0 && !can_nice(current, nice))
169 retval = security_task_setnice(current, nice);
173 set_user_nice(current, nice);
180 * task_prio - return the priority value of a given task.
181 * @p: the task in question.
183 * Return: The priority value as seen by users in /proc.
185 * sched policy return value kernel prio user prio/nice
187 * normal, batch, idle [0 ... 39] [100 ... 139] 0/[-20 ... 19]
188 * fifo, rr [-2 ... -100] [98 ... 0] [1 ... 99]
191 int task_prio(const struct task_struct *p)
193 return p->prio - MAX_RT_PRIO;
197 * idle_cpu - is a given CPU idle currently?
198 * @cpu: the processor in question.
200 * Return: 1 if the CPU is currently idle. 0 otherwise.
202 int idle_cpu(int cpu)
204 struct rq *rq = cpu_rq(cpu);
206 if (rq->curr != rq->idle)
213 if (rq->ttwu_pending)
221 * available_idle_cpu - is a given CPU idle for enqueuing work.
222 * @cpu: the CPU in question.
224 * Return: 1 if the CPU is currently idle. 0 otherwise.
226 int available_idle_cpu(int cpu)
231 if (vcpu_is_preempted(cpu))
238 * idle_task - return the idle task for a given CPU.
239 * @cpu: the processor in question.
241 * Return: The idle task for the CPU @cpu.
243 struct task_struct *idle_task(int cpu)
245 return cpu_rq(cpu)->idle;
248 #ifdef CONFIG_SCHED_CORE
249 int sched_core_idle_cpu(int cpu)
251 struct rq *rq = cpu_rq(cpu);
253 if (sched_core_enabled(rq) && rq->curr == rq->idle)
256 return idle_cpu(cpu);
262 * find_process_by_pid - find a process with a matching PID value.
263 * @pid: the pid in question.
265 * The task of @pid, if found. %NULL otherwise.
267 static struct task_struct *find_process_by_pid(pid_t pid)
269 return pid ? find_task_by_vpid(pid) : current;
272 static struct task_struct *find_get_task(pid_t pid)
274 struct task_struct *p;
277 p = find_process_by_pid(pid);
284 DEFINE_CLASS(find_get_task, struct task_struct *, if (_T) put_task_struct(_T),
285 find_get_task(pid), pid_t pid)
288 * sched_setparam() passes in -1 for its policy, to let the functions
289 * it calls know not to change it.
291 #define SETPARAM_POLICY -1
293 static void __setscheduler_params(struct task_struct *p,
294 const struct sched_attr *attr)
296 int policy = attr->sched_policy;
298 if (policy == SETPARAM_POLICY)
303 if (dl_policy(policy))
304 __setparam_dl(p, attr);
305 else if (fair_policy(policy))
306 __setparam_fair(p, attr);
308 /* rt-policy tasks do not have a timerslack */
309 if (rt_or_dl_task_policy(p)) {
310 p->timer_slack_ns = 0;
311 } else if (p->timer_slack_ns == 0) {
312 /* when switching back to non-rt policy, restore timerslack */
313 p->timer_slack_ns = p->default_timer_slack_ns;
317 * __sched_setscheduler() ensures attr->sched_priority == 0 when
318 * !rt_policy. Always setting this ensures that things like
319 * getparam()/getattr() don't report silly values for !rt tasks.
321 p->rt_priority = attr->sched_priority;
322 p->normal_prio = normal_prio(p);
323 set_load_weight(p, true);
327 * Check the target process has a UID that matches the current process's:
329 static bool check_same_owner(struct task_struct *p)
331 const struct cred *cred = current_cred(), *pcred;
334 pcred = __task_cred(p);
335 return (uid_eq(cred->euid, pcred->euid) ||
336 uid_eq(cred->euid, pcred->uid));
339 #ifdef CONFIG_UCLAMP_TASK
341 static int uclamp_validate(struct task_struct *p,
342 const struct sched_attr *attr)
344 int util_min = p->uclamp_req[UCLAMP_MIN].value;
345 int util_max = p->uclamp_req[UCLAMP_MAX].value;
347 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) {
348 util_min = attr->sched_util_min;
350 if (util_min + 1 > SCHED_CAPACITY_SCALE + 1)
354 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) {
355 util_max = attr->sched_util_max;
357 if (util_max + 1 > SCHED_CAPACITY_SCALE + 1)
361 if (util_min != -1 && util_max != -1 && util_min > util_max)
365 * We have valid uclamp attributes; make sure uclamp is enabled.
367 * We need to do that here, because enabling static branches is a
368 * blocking operation which obviously cannot be done while holding
371 static_branch_enable(&sched_uclamp_used);
376 static bool uclamp_reset(const struct sched_attr *attr,
377 enum uclamp_id clamp_id,
378 struct uclamp_se *uc_se)
380 /* Reset on sched class change for a non user-defined clamp value. */
381 if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)) &&
382 !uc_se->user_defined)
385 /* Reset on sched_util_{min,max} == -1. */
386 if (clamp_id == UCLAMP_MIN &&
387 attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN &&
388 attr->sched_util_min == -1) {
392 if (clamp_id == UCLAMP_MAX &&
393 attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX &&
394 attr->sched_util_max == -1) {
401 static void __setscheduler_uclamp(struct task_struct *p,
402 const struct sched_attr *attr)
404 enum uclamp_id clamp_id;
406 for_each_clamp_id(clamp_id) {
407 struct uclamp_se *uc_se = &p->uclamp_req[clamp_id];
410 if (!uclamp_reset(attr, clamp_id, uc_se))
414 * RT by default have a 100% boost value that could be modified
417 if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN))
418 value = sysctl_sched_uclamp_util_min_rt_default;
420 value = uclamp_none(clamp_id);
422 uclamp_se_set(uc_se, value, false);
426 if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)))
429 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN &&
430 attr->sched_util_min != -1) {
431 uclamp_se_set(&p->uclamp_req[UCLAMP_MIN],
432 attr->sched_util_min, true);
435 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX &&
436 attr->sched_util_max != -1) {
437 uclamp_se_set(&p->uclamp_req[UCLAMP_MAX],
438 attr->sched_util_max, true);
442 #else /* !CONFIG_UCLAMP_TASK: */
444 static inline int uclamp_validate(struct task_struct *p,
445 const struct sched_attr *attr)
449 static void __setscheduler_uclamp(struct task_struct *p,
450 const struct sched_attr *attr) { }
454 * Allow unprivileged RT tasks to decrease priority.
455 * Only issue a capable test if needed and only once to avoid an audit
456 * event on permitted non-privileged operations:
458 static int user_check_sched_setscheduler(struct task_struct *p,
459 const struct sched_attr *attr,
460 int policy, int reset_on_fork)
462 if (fair_policy(policy)) {
463 if (attr->sched_nice < task_nice(p) &&
464 !is_nice_reduction(p, attr->sched_nice))
468 if (rt_policy(policy)) {
469 unsigned long rlim_rtprio = task_rlimit(p, RLIMIT_RTPRIO);
471 /* Can't set/change the rt policy: */
472 if (policy != p->policy && !rlim_rtprio)
475 /* Can't increase priority: */
476 if (attr->sched_priority > p->rt_priority &&
477 attr->sched_priority > rlim_rtprio)
482 * Can't set/change SCHED_DEADLINE policy at all for now
483 * (safest behavior); in the future we would like to allow
484 * unprivileged DL tasks to increase their relative deadline
485 * or reduce their runtime (both ways reducing utilization)
487 if (dl_policy(policy))
491 * Treat SCHED_IDLE as nice 20. Only allow a switch to
492 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
494 if (task_has_idle_policy(p) && !idle_policy(policy)) {
495 if (!is_nice_reduction(p, task_nice(p)))
499 /* Can't change other user's priorities: */
500 if (!check_same_owner(p))
503 /* Normal users shall not reset the sched_reset_on_fork flag: */
504 if (p->sched_reset_on_fork && !reset_on_fork)
510 if (!capable(CAP_SYS_NICE))
516 int __sched_setscheduler(struct task_struct *p,
517 const struct sched_attr *attr,
520 int oldpolicy = -1, policy = attr->sched_policy;
521 int retval, oldprio, newprio, queued, running;
522 const struct sched_class *prev_class, *next_class;
523 struct balance_callback *head;
526 int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
528 bool cpuset_locked = false;
530 /* The pi code expects interrupts enabled */
531 BUG_ON(pi && in_interrupt());
533 /* Double check policy once rq lock held: */
535 reset_on_fork = p->sched_reset_on_fork;
536 policy = oldpolicy = p->policy;
538 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
540 if (!valid_policy(policy))
544 if (attr->sched_flags & ~(SCHED_FLAG_ALL | SCHED_FLAG_SUGOV))
548 * Valid priorities for SCHED_FIFO and SCHED_RR are
549 * 1..MAX_RT_PRIO-1, valid priority for SCHED_NORMAL,
550 * SCHED_BATCH and SCHED_IDLE is 0.
552 if (attr->sched_priority > MAX_RT_PRIO-1)
554 if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
555 (rt_policy(policy) != (attr->sched_priority != 0)))
559 retval = user_check_sched_setscheduler(p, attr, policy, reset_on_fork);
563 if (attr->sched_flags & SCHED_FLAG_SUGOV)
566 retval = security_task_setscheduler(p);
571 /* Update task specific "requested" clamps */
572 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) {
573 retval = uclamp_validate(p, attr);
579 * SCHED_DEADLINE bandwidth accounting relies on stable cpusets
582 if (dl_policy(policy) || dl_policy(p->policy)) {
583 cpuset_locked = true;
588 * Make sure no PI-waiters arrive (or leave) while we are
589 * changing the priority of the task:
591 * To be able to change p->policy safely, the appropriate
592 * runqueue lock must be held.
594 rq = task_rq_lock(p, &rf);
598 * Changing the policy of the stop threads its a very bad idea:
605 retval = scx_check_setscheduler(p, policy);
610 * If not changing anything there's no need to proceed further,
611 * but store a possible modification of reset_on_fork.
613 if (unlikely(policy == p->policy)) {
614 if (fair_policy(policy) &&
615 (attr->sched_nice != task_nice(p) ||
616 (attr->sched_runtime != p->se.slice)))
618 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
620 if (dl_policy(policy) && dl_param_changed(p, attr))
622 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)
625 p->sched_reset_on_fork = reset_on_fork;
632 #ifdef CONFIG_RT_GROUP_SCHED
634 * Do not allow real-time tasks into groups that have no runtime
637 if (rt_bandwidth_enabled() && rt_policy(policy) &&
638 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
639 !task_group_is_autogroup(task_group(p))) {
645 if (dl_bandwidth_enabled() && dl_policy(policy) &&
646 !(attr->sched_flags & SCHED_FLAG_SUGOV)) {
647 cpumask_t *span = rq->rd->span;
650 * Don't allow tasks with an affinity mask smaller than
651 * the entire root_domain to become SCHED_DEADLINE. We
652 * will also fail if there's no bandwidth available.
654 if (!cpumask_subset(span, p->cpus_ptr) ||
655 rq->rd->dl_bw.bw == 0) {
663 /* Re-check policy now with rq lock held: */
664 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
665 policy = oldpolicy = -1;
666 task_rq_unlock(rq, p, &rf);
673 * If setscheduling to SCHED_DEADLINE (or changing the parameters
674 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
677 if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) {
682 p->sched_reset_on_fork = reset_on_fork;
685 newprio = __normal_prio(policy, attr->sched_priority, attr->sched_nice);
688 * Take priority boosted tasks into account. If the new
689 * effective priority is unchanged, we just store the new
690 * normal parameters and do not touch the scheduler class and
691 * the runqueue. This will be done when the task deboost
694 newprio = rt_effective_prio(p, newprio);
695 if (newprio == oldprio)
696 queue_flags &= ~DEQUEUE_MOVE;
699 prev_class = p->sched_class;
700 next_class = __setscheduler_class(policy, newprio);
702 if (prev_class != next_class && p->se.sched_delayed)
703 dequeue_task(rq, p, DEQUEUE_SLEEP | DEQUEUE_DELAYED | DEQUEUE_NOCLOCK);
705 queued = task_on_rq_queued(p);
706 running = task_current_donor(rq, p);
708 dequeue_task(rq, p, queue_flags);
710 put_prev_task(rq, p);
712 if (!(attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)) {
713 __setscheduler_params(p, attr);
714 p->sched_class = next_class;
717 __setscheduler_uclamp(p, attr);
718 check_class_changing(rq, p, prev_class);
722 * We enqueue to tail when the priority of a task is
723 * increased (user space view).
725 if (oldprio < p->prio)
726 queue_flags |= ENQUEUE_HEAD;
728 enqueue_task(rq, p, queue_flags);
731 set_next_task(rq, p);
733 check_class_changed(rq, p, prev_class, oldprio);
735 /* Avoid rq from going away on us: */
737 head = splice_balance_callbacks(rq);
738 task_rq_unlock(rq, p, &rf);
743 rt_mutex_adjust_pi(p);
746 /* Run balance callbacks after we've adjusted the PI chain: */
747 balance_callbacks(rq, head);
753 task_rq_unlock(rq, p, &rf);
759 static int _sched_setscheduler(struct task_struct *p, int policy,
760 const struct sched_param *param, bool check)
762 struct sched_attr attr = {
763 .sched_policy = policy,
764 .sched_priority = param->sched_priority,
765 .sched_nice = PRIO_TO_NICE(p->static_prio),
768 if (p->se.custom_slice)
769 attr.sched_runtime = p->se.slice;
771 /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
772 if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
773 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
774 policy &= ~SCHED_RESET_ON_FORK;
775 attr.sched_policy = policy;
778 return __sched_setscheduler(p, &attr, check, true);
781 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
782 * @p: the task in question.
783 * @policy: new policy.
784 * @param: structure containing the new RT priority.
786 * Use sched_set_fifo(), read its comment.
788 * Return: 0 on success. An error code otherwise.
790 * NOTE that the task may be already dead.
792 int sched_setscheduler(struct task_struct *p, int policy,
793 const struct sched_param *param)
795 return _sched_setscheduler(p, policy, param, true);
798 int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
800 return __sched_setscheduler(p, attr, true, true);
803 int sched_setattr_nocheck(struct task_struct *p, const struct sched_attr *attr)
805 return __sched_setscheduler(p, attr, false, true);
807 EXPORT_SYMBOL_GPL(sched_setattr_nocheck);
810 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernel-space.
811 * @p: the task in question.
812 * @policy: new policy.
813 * @param: structure containing the new RT priority.
815 * Just like sched_setscheduler, only don't bother checking if the
816 * current context has permission. For example, this is needed in
817 * stop_machine(): we create temporary high priority worker threads,
818 * but our caller might not have that capability.
820 * Return: 0 on success. An error code otherwise.
822 int sched_setscheduler_nocheck(struct task_struct *p, int policy,
823 const struct sched_param *param)
825 return _sched_setscheduler(p, policy, param, false);
829 * SCHED_FIFO is a broken scheduler model; that is, it is fundamentally
830 * incapable of resource management, which is the one thing an OS really should
833 * This is of course the reason it is limited to privileged users only.
835 * Worse still; it is fundamentally impossible to compose static priority
836 * workloads. You cannot take two correctly working static prio workloads
837 * and smash them together and still expect them to work.
839 * For this reason 'all' FIFO tasks the kernel creates are basically at:
843 * The administrator _MUST_ configure the system, the kernel simply doesn't
844 * know enough information to make a sensible choice.
846 void sched_set_fifo(struct task_struct *p)
848 struct sched_param sp = { .sched_priority = MAX_RT_PRIO / 2 };
849 WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0);
851 EXPORT_SYMBOL_GPL(sched_set_fifo);
854 * For when you don't much care about FIFO, but want to be above SCHED_NORMAL.
856 void sched_set_fifo_low(struct task_struct *p)
858 struct sched_param sp = { .sched_priority = 1 };
859 WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0);
861 EXPORT_SYMBOL_GPL(sched_set_fifo_low);
863 void sched_set_normal(struct task_struct *p, int nice)
865 struct sched_attr attr = {
866 .sched_policy = SCHED_NORMAL,
869 WARN_ON_ONCE(sched_setattr_nocheck(p, &attr) != 0);
871 EXPORT_SYMBOL_GPL(sched_set_normal);
874 do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
876 struct sched_param lparam;
878 if (!param || pid < 0)
880 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
883 CLASS(find_get_task, p)(pid);
887 return sched_setscheduler(p, policy, &lparam);
891 * Mimics kernel/events/core.c perf_copy_attr().
893 static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *attr)
898 /* Zero the full structure, so that a short copy will be nice: */
899 memset(attr, 0, sizeof(*attr));
901 ret = get_user(size, &uattr->size);
905 /* ABI compatibility quirk: */
907 size = SCHED_ATTR_SIZE_VER0;
908 if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE)
911 ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
918 if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) &&
919 size < SCHED_ATTR_SIZE_VER1)
923 * XXX: Do we want to be lenient like existing syscalls; or do we want
924 * to be strict and return an error on out-of-bounds values?
926 attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
931 put_user(sizeof(*attr), &uattr->size);
935 static void get_params(struct task_struct *p, struct sched_attr *attr)
937 if (task_has_dl_policy(p)) {
938 __getparam_dl(p, attr);
939 } else if (task_has_rt_policy(p)) {
940 attr->sched_priority = p->rt_priority;
942 attr->sched_nice = task_nice(p);
943 attr->sched_runtime = p->se.slice;
948 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
949 * @pid: the pid in question.
950 * @policy: new policy.
951 * @param: structure containing the new RT priority.
953 * Return: 0 on success. An error code otherwise.
955 SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, struct sched_param __user *, param)
960 return do_sched_setscheduler(pid, policy, param);
964 * sys_sched_setparam - set/change the RT priority of a thread
965 * @pid: the pid in question.
966 * @param: structure containing the new RT priority.
968 * Return: 0 on success. An error code otherwise.
970 SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
972 return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
976 * sys_sched_setattr - same as above, but with extended sched_attr
977 * @pid: the pid in question.
978 * @uattr: structure containing the extended parameters.
979 * @flags: for future extension.
981 SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
984 struct sched_attr attr;
987 if (!uattr || pid < 0 || flags)
990 retval = sched_copy_attr(uattr, &attr);
994 if ((int)attr.sched_policy < 0)
996 if (attr.sched_flags & SCHED_FLAG_KEEP_POLICY)
997 attr.sched_policy = SETPARAM_POLICY;
999 CLASS(find_get_task, p)(pid);
1003 if (attr.sched_flags & SCHED_FLAG_KEEP_PARAMS)
1004 get_params(p, &attr);
1006 return sched_setattr(p, &attr);
1010 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
1011 * @pid: the pid in question.
1013 * Return: On success, the policy of the thread. Otherwise, a negative error
1016 SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
1018 struct task_struct *p;
1025 p = find_process_by_pid(pid);
1029 retval = security_task_getscheduler(p);
1032 if (p->sched_reset_on_fork)
1033 retval |= SCHED_RESET_ON_FORK;
1039 * sys_sched_getparam - get the RT priority of a thread
1040 * @pid: the pid in question.
1041 * @param: structure containing the RT priority.
1043 * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
1046 SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
1048 struct sched_param lp = { .sched_priority = 0 };
1049 struct task_struct *p;
1052 if (!param || pid < 0)
1055 scoped_guard (rcu) {
1056 p = find_process_by_pid(pid);
1060 retval = security_task_getscheduler(p);
1064 if (task_has_rt_policy(p))
1065 lp.sched_priority = p->rt_priority;
1069 * This one might sleep, we cannot do it with a spinlock held ...
1071 return copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
1075 * sys_sched_getattr - similar to sched_getparam, but with sched_attr
1076 * @pid: the pid in question.
1077 * @uattr: structure containing the extended parameters.
1078 * @usize: sizeof(attr) for fwd/bwd comp.
1079 * @flags: for future extension.
1081 SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
1082 unsigned int, usize, unsigned int, flags)
1084 struct sched_attr kattr = { };
1085 struct task_struct *p;
1088 if (!uattr || pid < 0 || usize > PAGE_SIZE ||
1089 usize < SCHED_ATTR_SIZE_VER0 || flags)
1092 scoped_guard (rcu) {
1093 p = find_process_by_pid(pid);
1097 retval = security_task_getscheduler(p);
1101 kattr.sched_policy = p->policy;
1102 if (p->sched_reset_on_fork)
1103 kattr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
1104 get_params(p, &kattr);
1105 kattr.sched_flags &= SCHED_FLAG_ALL;
1107 #ifdef CONFIG_UCLAMP_TASK
1109 * This could race with another potential updater, but this is fine
1110 * because it'll correctly read the old or the new value. We don't need
1111 * to guarantee who wins the race as long as it doesn't return garbage.
1113 kattr.sched_util_min = p->uclamp_req[UCLAMP_MIN].value;
1114 kattr.sched_util_max = p->uclamp_req[UCLAMP_MAX].value;
1118 kattr.size = min(usize, sizeof(kattr));
1119 return copy_struct_to_user(uattr, usize, &kattr, sizeof(kattr), NULL);
1123 int dl_task_check_affinity(struct task_struct *p, const struct cpumask *mask)
1126 * If the task isn't a deadline task or admission control is
1127 * disabled then we don't care about affinity changes.
1129 if (!task_has_dl_policy(p) || !dl_bandwidth_enabled())
1133 * The special/sugov task isn't part of regular bandwidth/admission
1134 * control so let userspace change affinities.
1136 if (dl_entity_is_special(&p->dl))
1140 * Since bandwidth control happens on root_domain basis,
1141 * if admission test is enabled, we only admit -deadline
1142 * tasks allowed to run on all the CPUs in the task's
1146 if (!cpumask_subset(task_rq(p)->rd->span, mask))
1151 #endif /* CONFIG_SMP */
1153 int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx)
1156 cpumask_var_t cpus_allowed, new_mask;
1158 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL))
1161 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
1163 goto out_free_cpus_allowed;
1166 cpuset_cpus_allowed(p, cpus_allowed);
1167 cpumask_and(new_mask, ctx->new_mask, cpus_allowed);
1169 ctx->new_mask = new_mask;
1170 ctx->flags |= SCA_CHECK;
1172 retval = dl_task_check_affinity(p, new_mask);
1174 goto out_free_new_mask;
1176 retval = __set_cpus_allowed_ptr(p, ctx);
1178 goto out_free_new_mask;
1180 cpuset_cpus_allowed(p, cpus_allowed);
1181 if (!cpumask_subset(new_mask, cpus_allowed)) {
1183 * We must have raced with a concurrent cpuset update.
1184 * Just reset the cpumask to the cpuset's cpus_allowed.
1186 cpumask_copy(new_mask, cpus_allowed);
1189 * If SCA_USER is set, a 2nd call to __set_cpus_allowed_ptr()
1190 * will restore the previous user_cpus_ptr value.
1192 * In the unlikely event a previous user_cpus_ptr exists,
1193 * we need to further restrict the mask to what is allowed
1194 * by that old user_cpus_ptr.
1196 if (unlikely((ctx->flags & SCA_USER) && ctx->user_mask)) {
1197 bool empty = !cpumask_and(new_mask, new_mask,
1201 cpumask_copy(new_mask, cpus_allowed);
1203 __set_cpus_allowed_ptr(p, ctx);
1208 free_cpumask_var(new_mask);
1209 out_free_cpus_allowed:
1210 free_cpumask_var(cpus_allowed);
1214 long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
1216 struct affinity_context ac;
1217 struct cpumask *user_mask;
1220 CLASS(find_get_task, p)(pid);
1224 if (p->flags & PF_NO_SETAFFINITY)
1227 if (!check_same_owner(p)) {
1229 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
1233 retval = security_task_setscheduler(p);
1238 * With non-SMP configs, user_cpus_ptr/user_mask isn't used and
1239 * alloc_user_cpus_ptr() returns NULL.
1241 user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE);
1243 cpumask_copy(user_mask, in_mask);
1244 } else if (IS_ENABLED(CONFIG_SMP)) {
1248 ac = (struct affinity_context){
1249 .new_mask = in_mask,
1250 .user_mask = user_mask,
1254 retval = __sched_setaffinity(p, &ac);
1255 kfree(ac.user_mask);
1260 static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
1261 struct cpumask *new_mask)
1263 if (len < cpumask_size())
1264 cpumask_clear(new_mask);
1265 else if (len > cpumask_size())
1266 len = cpumask_size();
1268 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
1272 * sys_sched_setaffinity - set the CPU affinity of a process
1273 * @pid: pid of the process
1274 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
1275 * @user_mask_ptr: user-space pointer to the new CPU mask
1277 * Return: 0 on success. An error code otherwise.
1279 SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
1280 unsigned long __user *, user_mask_ptr)
1282 cpumask_var_t new_mask;
1285 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
1288 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
1290 retval = sched_setaffinity(pid, new_mask);
1291 free_cpumask_var(new_mask);
1295 long sched_getaffinity(pid_t pid, struct cpumask *mask)
1297 struct task_struct *p;
1301 p = find_process_by_pid(pid);
1305 retval = security_task_getscheduler(p);
1309 guard(raw_spinlock_irqsave)(&p->pi_lock);
1310 cpumask_and(mask, &p->cpus_mask, cpu_active_mask);
1316 * sys_sched_getaffinity - get the CPU affinity of a process
1317 * @pid: pid of the process
1318 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
1319 * @user_mask_ptr: user-space pointer to hold the current CPU mask
1321 * Return: size of CPU mask copied to user_mask_ptr on success. An
1322 * error code otherwise.
1324 SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
1325 unsigned long __user *, user_mask_ptr)
1330 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
1332 if (len & (sizeof(unsigned long)-1))
1335 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1338 ret = sched_getaffinity(pid, mask);
1340 unsigned int retlen = min(len, cpumask_size());
1342 if (copy_to_user(user_mask_ptr, cpumask_bits(mask), retlen))
1347 free_cpumask_var(mask);
1352 static void do_sched_yield(void)
1357 rq = this_rq_lock_irq(&rf);
1359 schedstat_inc(rq->yld_count);
1360 current->sched_class->yield_task(rq);
1363 rq_unlock_irq(rq, &rf);
1364 sched_preempt_enable_no_resched();
1370 * sys_sched_yield - yield the current processor to other threads.
1372 * This function yields the current CPU to other tasks. If there are no
1373 * other threads running on this CPU then this function will return.
1377 SYSCALL_DEFINE0(sched_yield)
1384 * yield - yield the current processor to other threads.
1386 * Do not ever use this function, there's a 99% chance you're doing it wrong.
1388 * The scheduler is at all times free to pick the calling task as the most
1389 * eligible task to run, if removing the yield() call from your code breaks
1390 * it, it's already broken.
1392 * Typical broken usage is:
1397 * where one assumes that yield() will let 'the other' process run that will
1398 * make event true. If the current task is a SCHED_FIFO task that will never
1399 * happen. Never use yield() as a progress guarantee!!
1401 * If you want to use yield() to wait for something, use wait_event().
1402 * If you want to use yield() to be 'nice' for others, use cond_resched().
1403 * If you still want to use yield(), do not!
1405 void __sched yield(void)
1407 set_current_state(TASK_RUNNING);
1410 EXPORT_SYMBOL(yield);
1413 * yield_to - yield the current processor to another thread in
1414 * your thread group, or accelerate that thread toward the
1415 * processor it's on.
1417 * @preempt: whether task preemption is allowed or not
1419 * It's the caller's job to ensure that the target task struct
1420 * can't go away on us before we can do any checks.
1423 * true (>0) if we indeed boosted the target task.
1424 * false (0) if we failed to boost the target.
1425 * -ESRCH if there's no task to yield to.
1427 int __sched yield_to(struct task_struct *p, bool preempt)
1429 struct task_struct *curr = current;
1430 struct rq *rq, *p_rq;
1433 scoped_guard (raw_spinlock_irqsave, &p->pi_lock) {
1439 * If we're the only runnable task on the rq and target rq also
1440 * has only one task, there's absolutely no point in yielding.
1442 if (rq->nr_running == 1 && p_rq->nr_running == 1)
1445 guard(double_rq_lock)(rq, p_rq);
1446 if (task_rq(p) != p_rq)
1449 if (!curr->sched_class->yield_to_task)
1452 if (curr->sched_class != p->sched_class)
1455 if (task_on_cpu(p_rq, p) || !task_is_running(p))
1458 yielded = curr->sched_class->yield_to_task(rq, p);
1460 schedstat_inc(rq->yld_count);
1462 * Make p's CPU reschedule; pick_next_entity
1463 * takes care of fairness.
1465 if (preempt && rq != p_rq)
1475 EXPORT_SYMBOL_GPL(yield_to);
1478 * sys_sched_get_priority_max - return maximum RT priority.
1479 * @policy: scheduling class.
1481 * Return: On success, this syscall returns the maximum
1482 * rt_priority that can be used by a given scheduling class.
1483 * On failure, a negative error code is returned.
1485 SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
1492 ret = MAX_RT_PRIO-1;
1494 case SCHED_DEADLINE:
1506 * sys_sched_get_priority_min - return minimum RT priority.
1507 * @policy: scheduling class.
1509 * Return: On success, this syscall returns the minimum
1510 * rt_priority that can be used by a given scheduling class.
1511 * On failure, a negative error code is returned.
1513 SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
1522 case SCHED_DEADLINE:
1532 static int sched_rr_get_interval(pid_t pid, struct timespec64 *t)
1534 unsigned int time_slice = 0;
1540 scoped_guard (rcu) {
1541 struct task_struct *p = find_process_by_pid(pid);
1545 retval = security_task_getscheduler(p);
1549 scoped_guard (task_rq_lock, p) {
1550 struct rq *rq = scope.rq;
1551 if (p->sched_class->get_rr_interval)
1552 time_slice = p->sched_class->get_rr_interval(rq, p);
1556 jiffies_to_timespec64(time_slice, t);
1561 * sys_sched_rr_get_interval - return the default time-slice of a process.
1562 * @pid: pid of the process.
1563 * @interval: userspace pointer to the time-slice value.
1565 * this syscall writes the default time-slice value of a given process
1566 * into the user-space timespec buffer. A value of '0' means infinity.
1568 * Return: On success, 0 and the time-slice is in @interval. Otherwise,
1571 SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
1572 struct __kernel_timespec __user *, interval)
1574 struct timespec64 t;
1575 int retval = sched_rr_get_interval(pid, &t);
1578 retval = put_timespec64(&t, interval);
1583 #ifdef CONFIG_COMPAT_32BIT_TIME
1584 SYSCALL_DEFINE2(sched_rr_get_interval_time32, pid_t, pid,
1585 struct old_timespec32 __user *, interval)
1587 struct timespec64 t;
1588 int retval = sched_rr_get_interval(pid, &t);
1591 retval = put_old_timespec32(&t, interval);