xref: /linux/kernel/sched/syscalls.c (revision c8d430db8eec7d4fd13a6bea27b7086a54eda6da)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  kernel/sched/syscalls.c
4  *
5  *  Core kernel scheduler syscalls related code
6  *
7  *  Copyright (C) 1991-2002  Linus Torvalds
8  *  Copyright (C) 1998-2024  Ingo Molnar, Red Hat
9  */
10 #include <linux/sched.h>
11 #include <linux/cpuset.h>
12 #include <linux/sched/debug.h>
13 
14 #include <uapi/linux/sched/types.h>
15 
16 #include "sched.h"
17 #include "autogroup.h"
18 
19 static inline int __normal_prio(int policy, int rt_prio, int nice)
20 {
21 	int prio;
22 
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;
27 	else
28 		prio = NICE_TO_PRIO(nice);
29 
30 	return prio;
31 }
32 
33 /*
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.
39  */
40 static inline int normal_prio(struct task_struct *p)
41 {
42 	return __normal_prio(p->policy, p->rt_priority, PRIO_TO_NICE(p->static_prio));
43 }
44 
45 /*
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.
51  */
52 static int effective_prio(struct task_struct *p)
53 {
54 	p->normal_prio = normal_prio(p);
55 	/*
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:
59 	 */
60 	if (!rt_or_dl_prio(p->prio))
61 		return p->normal_prio;
62 	return p->prio;
63 }
64 
65 void set_user_nice(struct task_struct *p, long nice)
66 {
67 	bool queued, running;
68 	struct rq *rq;
69 	int old_prio;
70 
71 	if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
72 		return;
73 	/*
74 	 * We have to be careful, if called from sys_setpriority(),
75 	 * the task might be in the middle of scheduling on another CPU.
76 	 */
77 	CLASS(task_rq_lock, rq_guard)(p);
78 	rq = rq_guard.rq;
79 
80 	update_rq_clock(rq);
81 
82 	/*
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:
87 	 */
88 	if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
89 		p->static_prio = NICE_TO_PRIO(nice);
90 		return;
91 	}
92 
93 	queued = task_on_rq_queued(p);
94 	running = task_current(rq, p);
95 	if (queued)
96 		dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
97 	if (running)
98 		put_prev_task(rq, p);
99 
100 	p->static_prio = NICE_TO_PRIO(nice);
101 	set_load_weight(p, true);
102 	old_prio = p->prio;
103 	p->prio = effective_prio(p);
104 
105 	if (queued)
106 		enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
107 	if (running)
108 		set_next_task(rq, p);
109 
110 	/*
111 	 * If the task increased its priority or is running and
112 	 * lowered its priority, then reschedule its CPU:
113 	 */
114 	p->sched_class->prio_changed(rq, p, old_prio);
115 }
116 EXPORT_SYMBOL(set_user_nice);
117 
118 /*
119  * is_nice_reduction - check if nice value is an actual reduction
120  *
121  * Similar to can_nice() but does not perform a capability check.
122  *
123  * @p: task
124  * @nice: nice value
125  */
126 static bool is_nice_reduction(const struct task_struct *p, const int nice)
127 {
128 	/* Convert nice value [19,-20] to rlimit style value [1,40]: */
129 	int nice_rlim = nice_to_rlimit(nice);
130 
131 	return (nice_rlim <= task_rlimit(p, RLIMIT_NICE));
132 }
133 
134 /*
135  * can_nice - check if a task can reduce its nice value
136  * @p: task
137  * @nice: nice value
138  */
139 int can_nice(const struct task_struct *p, const int nice)
140 {
141 	return is_nice_reduction(p, nice) || capable(CAP_SYS_NICE);
142 }
143 
144 #ifdef __ARCH_WANT_SYS_NICE
145 
146 /*
147  * sys_nice - change the priority of the current process.
148  * @increment: priority increment
149  *
150  * sys_setpriority is a more generic, but much slower function that
151  * does similar things.
152  */
153 SYSCALL_DEFINE1(nice, int, increment)
154 {
155 	long nice, retval;
156 
157 	/*
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.
161 	 */
162 	increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
163 	nice = task_nice(current) + increment;
164 
165 	nice = clamp_val(nice, MIN_NICE, MAX_NICE);
166 	if (increment < 0 && !can_nice(current, nice))
167 		return -EPERM;
168 
169 	retval = security_task_setnice(current, nice);
170 	if (retval)
171 		return retval;
172 
173 	set_user_nice(current, nice);
174 	return 0;
175 }
176 
177 #endif
178 
179 /**
180  * task_prio - return the priority value of a given task.
181  * @p: the task in question.
182  *
183  * Return: The priority value as seen by users in /proc.
184  *
185  * sched policy         return value   kernel prio    user prio/nice
186  *
187  * normal, batch, idle     [0 ... 39]  [100 ... 139]          0/[-20 ... 19]
188  * fifo, rr             [-2 ... -100]     [98 ... 0]  [1 ... 99]
189  * deadline                     -101             -1           0
190  */
191 int task_prio(const struct task_struct *p)
192 {
193 	return p->prio - MAX_RT_PRIO;
194 }
195 
196 /**
197  * idle_cpu - is a given CPU idle currently?
198  * @cpu: the processor in question.
199  *
200  * Return: 1 if the CPU is currently idle. 0 otherwise.
201  */
202 int idle_cpu(int cpu)
203 {
204 	struct rq *rq = cpu_rq(cpu);
205 
206 	if (rq->curr != rq->idle)
207 		return 0;
208 
209 	if (rq->nr_running)
210 		return 0;
211 
212 #ifdef CONFIG_SMP
213 	if (rq->ttwu_pending)
214 		return 0;
215 #endif
216 
217 	return 1;
218 }
219 
220 /**
221  * available_idle_cpu - is a given CPU idle for enqueuing work.
222  * @cpu: the CPU in question.
223  *
224  * Return: 1 if the CPU is currently idle. 0 otherwise.
225  */
226 int available_idle_cpu(int cpu)
227 {
228 	if (!idle_cpu(cpu))
229 		return 0;
230 
231 	if (vcpu_is_preempted(cpu))
232 		return 0;
233 
234 	return 1;
235 }
236 
237 /**
238  * idle_task - return the idle task for a given CPU.
239  * @cpu: the processor in question.
240  *
241  * Return: The idle task for the CPU @cpu.
242  */
243 struct task_struct *idle_task(int cpu)
244 {
245 	return cpu_rq(cpu)->idle;
246 }
247 
248 #ifdef CONFIG_SCHED_CORE
249 int sched_core_idle_cpu(int cpu)
250 {
251 	struct rq *rq = cpu_rq(cpu);
252 
253 	if (sched_core_enabled(rq) && rq->curr == rq->idle)
254 		return 1;
255 
256 	return idle_cpu(cpu);
257 }
258 
259 #endif
260 
261 /**
262  * find_process_by_pid - find a process with a matching PID value.
263  * @pid: the pid in question.
264  *
265  * The task of @pid, if found. %NULL otherwise.
266  */
267 static struct task_struct *find_process_by_pid(pid_t pid)
268 {
269 	return pid ? find_task_by_vpid(pid) : current;
270 }
271 
272 static struct task_struct *find_get_task(pid_t pid)
273 {
274 	struct task_struct *p;
275 	guard(rcu)();
276 
277 	p = find_process_by_pid(pid);
278 	if (likely(p))
279 		get_task_struct(p);
280 
281 	return p;
282 }
283 
284 DEFINE_CLASS(find_get_task, struct task_struct *, if (_T) put_task_struct(_T),
285 	     find_get_task(pid), pid_t pid)
286 
287 /*
288  * sched_setparam() passes in -1 for its policy, to let the functions
289  * it calls know not to change it.
290  */
291 #define SETPARAM_POLICY	-1
292 
293 static void __setscheduler_params(struct task_struct *p,
294 		const struct sched_attr *attr)
295 {
296 	int policy = attr->sched_policy;
297 
298 	if (policy == SETPARAM_POLICY)
299 		policy = p->policy;
300 
301 	p->policy = policy;
302 
303 	if (dl_policy(policy)) {
304 		__setparam_dl(p, attr);
305 	} else if (fair_policy(policy)) {
306 		p->static_prio = NICE_TO_PRIO(attr->sched_nice);
307 		if (attr->sched_runtime) {
308 			p->se.custom_slice = 1;
309 			p->se.slice = clamp_t(u64, attr->sched_runtime,
310 					      NSEC_PER_MSEC/10,   /* HZ=1000 * 10 */
311 					      NSEC_PER_MSEC*100); /* HZ=100  / 10 */
312 		} else {
313 			p->se.custom_slice = 0;
314 			p->se.slice = sysctl_sched_base_slice;
315 		}
316 	}
317 
318 	/* rt-policy tasks do not have a timerslack */
319 	if (rt_or_dl_task_policy(p)) {
320 		p->timer_slack_ns = 0;
321 	} else if (p->timer_slack_ns == 0) {
322 		/* when switching back to non-rt policy, restore timerslack */
323 		p->timer_slack_ns = p->default_timer_slack_ns;
324 	}
325 
326 	/*
327 	 * __sched_setscheduler() ensures attr->sched_priority == 0 when
328 	 * !rt_policy. Always setting this ensures that things like
329 	 * getparam()/getattr() don't report silly values for !rt tasks.
330 	 */
331 	p->rt_priority = attr->sched_priority;
332 	p->normal_prio = normal_prio(p);
333 	set_load_weight(p, true);
334 }
335 
336 /*
337  * Check the target process has a UID that matches the current process's:
338  */
339 static bool check_same_owner(struct task_struct *p)
340 {
341 	const struct cred *cred = current_cred(), *pcred;
342 	guard(rcu)();
343 
344 	pcred = __task_cred(p);
345 	return (uid_eq(cred->euid, pcred->euid) ||
346 		uid_eq(cred->euid, pcred->uid));
347 }
348 
349 #ifdef CONFIG_UCLAMP_TASK
350 
351 static int uclamp_validate(struct task_struct *p,
352 			   const struct sched_attr *attr)
353 {
354 	int util_min = p->uclamp_req[UCLAMP_MIN].value;
355 	int util_max = p->uclamp_req[UCLAMP_MAX].value;
356 
357 	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) {
358 		util_min = attr->sched_util_min;
359 
360 		if (util_min + 1 > SCHED_CAPACITY_SCALE + 1)
361 			return -EINVAL;
362 	}
363 
364 	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) {
365 		util_max = attr->sched_util_max;
366 
367 		if (util_max + 1 > SCHED_CAPACITY_SCALE + 1)
368 			return -EINVAL;
369 	}
370 
371 	if (util_min != -1 && util_max != -1 && util_min > util_max)
372 		return -EINVAL;
373 
374 	/*
375 	 * We have valid uclamp attributes; make sure uclamp is enabled.
376 	 *
377 	 * We need to do that here, because enabling static branches is a
378 	 * blocking operation which obviously cannot be done while holding
379 	 * scheduler locks.
380 	 */
381 	static_branch_enable(&sched_uclamp_used);
382 
383 	return 0;
384 }
385 
386 static bool uclamp_reset(const struct sched_attr *attr,
387 			 enum uclamp_id clamp_id,
388 			 struct uclamp_se *uc_se)
389 {
390 	/* Reset on sched class change for a non user-defined clamp value. */
391 	if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)) &&
392 	    !uc_se->user_defined)
393 		return true;
394 
395 	/* Reset on sched_util_{min,max} == -1. */
396 	if (clamp_id == UCLAMP_MIN &&
397 	    attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN &&
398 	    attr->sched_util_min == -1) {
399 		return true;
400 	}
401 
402 	if (clamp_id == UCLAMP_MAX &&
403 	    attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX &&
404 	    attr->sched_util_max == -1) {
405 		return true;
406 	}
407 
408 	return false;
409 }
410 
411 static void __setscheduler_uclamp(struct task_struct *p,
412 				  const struct sched_attr *attr)
413 {
414 	enum uclamp_id clamp_id;
415 
416 	for_each_clamp_id(clamp_id) {
417 		struct uclamp_se *uc_se = &p->uclamp_req[clamp_id];
418 		unsigned int value;
419 
420 		if (!uclamp_reset(attr, clamp_id, uc_se))
421 			continue;
422 
423 		/*
424 		 * RT by default have a 100% boost value that could be modified
425 		 * at runtime.
426 		 */
427 		if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN))
428 			value = sysctl_sched_uclamp_util_min_rt_default;
429 		else
430 			value = uclamp_none(clamp_id);
431 
432 		uclamp_se_set(uc_se, value, false);
433 
434 	}
435 
436 	if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)))
437 		return;
438 
439 	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN &&
440 	    attr->sched_util_min != -1) {
441 		uclamp_se_set(&p->uclamp_req[UCLAMP_MIN],
442 			      attr->sched_util_min, true);
443 	}
444 
445 	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX &&
446 	    attr->sched_util_max != -1) {
447 		uclamp_se_set(&p->uclamp_req[UCLAMP_MAX],
448 			      attr->sched_util_max, true);
449 	}
450 }
451 
452 #else /* !CONFIG_UCLAMP_TASK: */
453 
454 static inline int uclamp_validate(struct task_struct *p,
455 				  const struct sched_attr *attr)
456 {
457 	return -EOPNOTSUPP;
458 }
459 static void __setscheduler_uclamp(struct task_struct *p,
460 				  const struct sched_attr *attr) { }
461 #endif
462 
463 /*
464  * Allow unprivileged RT tasks to decrease priority.
465  * Only issue a capable test if needed and only once to avoid an audit
466  * event on permitted non-privileged operations:
467  */
468 static int user_check_sched_setscheduler(struct task_struct *p,
469 					 const struct sched_attr *attr,
470 					 int policy, int reset_on_fork)
471 {
472 	if (fair_policy(policy)) {
473 		if (attr->sched_nice < task_nice(p) &&
474 		    !is_nice_reduction(p, attr->sched_nice))
475 			goto req_priv;
476 	}
477 
478 	if (rt_policy(policy)) {
479 		unsigned long rlim_rtprio = task_rlimit(p, RLIMIT_RTPRIO);
480 
481 		/* Can't set/change the rt policy: */
482 		if (policy != p->policy && !rlim_rtprio)
483 			goto req_priv;
484 
485 		/* Can't increase priority: */
486 		if (attr->sched_priority > p->rt_priority &&
487 		    attr->sched_priority > rlim_rtprio)
488 			goto req_priv;
489 	}
490 
491 	/*
492 	 * Can't set/change SCHED_DEADLINE policy at all for now
493 	 * (safest behavior); in the future we would like to allow
494 	 * unprivileged DL tasks to increase their relative deadline
495 	 * or reduce their runtime (both ways reducing utilization)
496 	 */
497 	if (dl_policy(policy))
498 		goto req_priv;
499 
500 	/*
501 	 * Treat SCHED_IDLE as nice 20. Only allow a switch to
502 	 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
503 	 */
504 	if (task_has_idle_policy(p) && !idle_policy(policy)) {
505 		if (!is_nice_reduction(p, task_nice(p)))
506 			goto req_priv;
507 	}
508 
509 	/* Can't change other user's priorities: */
510 	if (!check_same_owner(p))
511 		goto req_priv;
512 
513 	/* Normal users shall not reset the sched_reset_on_fork flag: */
514 	if (p->sched_reset_on_fork && !reset_on_fork)
515 		goto req_priv;
516 
517 	return 0;
518 
519 req_priv:
520 	if (!capable(CAP_SYS_NICE))
521 		return -EPERM;
522 
523 	return 0;
524 }
525 
526 int __sched_setscheduler(struct task_struct *p,
527 			 const struct sched_attr *attr,
528 			 bool user, bool pi)
529 {
530 	int oldpolicy = -1, policy = attr->sched_policy;
531 	int retval, oldprio, newprio, queued, running;
532 	const struct sched_class *prev_class;
533 	struct balance_callback *head;
534 	struct rq_flags rf;
535 	int reset_on_fork;
536 	int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
537 	struct rq *rq;
538 	bool cpuset_locked = false;
539 
540 	/* The pi code expects interrupts enabled */
541 	BUG_ON(pi && in_interrupt());
542 recheck:
543 	/* Double check policy once rq lock held: */
544 	if (policy < 0) {
545 		reset_on_fork = p->sched_reset_on_fork;
546 		policy = oldpolicy = p->policy;
547 	} else {
548 		reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
549 
550 		if (!valid_policy(policy))
551 			return -EINVAL;
552 	}
553 
554 	if (attr->sched_flags & ~(SCHED_FLAG_ALL | SCHED_FLAG_SUGOV))
555 		return -EINVAL;
556 
557 	/*
558 	 * Valid priorities for SCHED_FIFO and SCHED_RR are
559 	 * 1..MAX_RT_PRIO-1, valid priority for SCHED_NORMAL,
560 	 * SCHED_BATCH and SCHED_IDLE is 0.
561 	 */
562 	if (attr->sched_priority > MAX_RT_PRIO-1)
563 		return -EINVAL;
564 	if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
565 	    (rt_policy(policy) != (attr->sched_priority != 0)))
566 		return -EINVAL;
567 
568 	if (user) {
569 		retval = user_check_sched_setscheduler(p, attr, policy, reset_on_fork);
570 		if (retval)
571 			return retval;
572 
573 		if (attr->sched_flags & SCHED_FLAG_SUGOV)
574 			return -EINVAL;
575 
576 		retval = security_task_setscheduler(p);
577 		if (retval)
578 			return retval;
579 	}
580 
581 	/* Update task specific "requested" clamps */
582 	if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) {
583 		retval = uclamp_validate(p, attr);
584 		if (retval)
585 			return retval;
586 	}
587 
588 	/*
589 	 * SCHED_DEADLINE bandwidth accounting relies on stable cpusets
590 	 * information.
591 	 */
592 	if (dl_policy(policy) || dl_policy(p->policy)) {
593 		cpuset_locked = true;
594 		cpuset_lock();
595 	}
596 
597 	/*
598 	 * Make sure no PI-waiters arrive (or leave) while we are
599 	 * changing the priority of the task:
600 	 *
601 	 * To be able to change p->policy safely, the appropriate
602 	 * runqueue lock must be held.
603 	 */
604 	rq = task_rq_lock(p, &rf);
605 	update_rq_clock(rq);
606 
607 	/*
608 	 * Changing the policy of the stop threads its a very bad idea:
609 	 */
610 	if (p == rq->stop) {
611 		retval = -EINVAL;
612 		goto unlock;
613 	}
614 
615 	retval = scx_check_setscheduler(p, policy);
616 	if (retval)
617 		goto unlock;
618 
619 	/*
620 	 * If not changing anything there's no need to proceed further,
621 	 * but store a possible modification of reset_on_fork.
622 	 */
623 	if (unlikely(policy == p->policy)) {
624 		if (fair_policy(policy) &&
625 		    (attr->sched_nice != task_nice(p) ||
626 		     (attr->sched_runtime != p->se.slice)))
627 			goto change;
628 		if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
629 			goto change;
630 		if (dl_policy(policy) && dl_param_changed(p, attr))
631 			goto change;
632 		if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)
633 			goto change;
634 
635 		p->sched_reset_on_fork = reset_on_fork;
636 		retval = 0;
637 		goto unlock;
638 	}
639 change:
640 
641 	if (user) {
642 #ifdef CONFIG_RT_GROUP_SCHED
643 		/*
644 		 * Do not allow real-time tasks into groups that have no runtime
645 		 * assigned.
646 		 */
647 		if (rt_bandwidth_enabled() && rt_policy(policy) &&
648 				task_group(p)->rt_bandwidth.rt_runtime == 0 &&
649 				!task_group_is_autogroup(task_group(p))) {
650 			retval = -EPERM;
651 			goto unlock;
652 		}
653 #endif
654 #ifdef CONFIG_SMP
655 		if (dl_bandwidth_enabled() && dl_policy(policy) &&
656 				!(attr->sched_flags & SCHED_FLAG_SUGOV)) {
657 			cpumask_t *span = rq->rd->span;
658 
659 			/*
660 			 * Don't allow tasks with an affinity mask smaller than
661 			 * the entire root_domain to become SCHED_DEADLINE. We
662 			 * will also fail if there's no bandwidth available.
663 			 */
664 			if (!cpumask_subset(span, p->cpus_ptr) ||
665 			    rq->rd->dl_bw.bw == 0) {
666 				retval = -EPERM;
667 				goto unlock;
668 			}
669 		}
670 #endif
671 	}
672 
673 	/* Re-check policy now with rq lock held: */
674 	if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
675 		policy = oldpolicy = -1;
676 		task_rq_unlock(rq, p, &rf);
677 		if (cpuset_locked)
678 			cpuset_unlock();
679 		goto recheck;
680 	}
681 
682 	/*
683 	 * If setscheduling to SCHED_DEADLINE (or changing the parameters
684 	 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
685 	 * is available.
686 	 */
687 	if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) {
688 		retval = -EBUSY;
689 		goto unlock;
690 	}
691 
692 	p->sched_reset_on_fork = reset_on_fork;
693 	oldprio = p->prio;
694 
695 	newprio = __normal_prio(policy, attr->sched_priority, attr->sched_nice);
696 	if (pi) {
697 		/*
698 		 * Take priority boosted tasks into account. If the new
699 		 * effective priority is unchanged, we just store the new
700 		 * normal parameters and do not touch the scheduler class and
701 		 * the runqueue. This will be done when the task deboost
702 		 * itself.
703 		 */
704 		newprio = rt_effective_prio(p, newprio);
705 		if (newprio == oldprio)
706 			queue_flags &= ~DEQUEUE_MOVE;
707 	}
708 
709 	queued = task_on_rq_queued(p);
710 	running = task_current(rq, p);
711 	if (queued)
712 		dequeue_task(rq, p, queue_flags);
713 	if (running)
714 		put_prev_task(rq, p);
715 
716 	prev_class = p->sched_class;
717 
718 	if (!(attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)) {
719 		__setscheduler_params(p, attr);
720 		__setscheduler_prio(p, newprio);
721 	}
722 	__setscheduler_uclamp(p, attr);
723 	check_class_changing(rq, p, prev_class);
724 
725 	if (queued) {
726 		/*
727 		 * We enqueue to tail when the priority of a task is
728 		 * increased (user space view).
729 		 */
730 		if (oldprio < p->prio)
731 			queue_flags |= ENQUEUE_HEAD;
732 
733 		enqueue_task(rq, p, queue_flags);
734 	}
735 	if (running)
736 		set_next_task(rq, p);
737 
738 	check_class_changed(rq, p, prev_class, oldprio);
739 
740 	/* Avoid rq from going away on us: */
741 	preempt_disable();
742 	head = splice_balance_callbacks(rq);
743 	task_rq_unlock(rq, p, &rf);
744 
745 	if (pi) {
746 		if (cpuset_locked)
747 			cpuset_unlock();
748 		rt_mutex_adjust_pi(p);
749 	}
750 
751 	/* Run balance callbacks after we've adjusted the PI chain: */
752 	balance_callbacks(rq, head);
753 	preempt_enable();
754 
755 	return 0;
756 
757 unlock:
758 	task_rq_unlock(rq, p, &rf);
759 	if (cpuset_locked)
760 		cpuset_unlock();
761 	return retval;
762 }
763 
764 static int _sched_setscheduler(struct task_struct *p, int policy,
765 			       const struct sched_param *param, bool check)
766 {
767 	struct sched_attr attr = {
768 		.sched_policy   = policy,
769 		.sched_priority = param->sched_priority,
770 		.sched_nice	= PRIO_TO_NICE(p->static_prio),
771 	};
772 
773 	if (p->se.custom_slice)
774 		attr.sched_runtime = p->se.slice;
775 
776 	/* Fixup the legacy SCHED_RESET_ON_FORK hack. */
777 	if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
778 		attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
779 		policy &= ~SCHED_RESET_ON_FORK;
780 		attr.sched_policy = policy;
781 	}
782 
783 	return __sched_setscheduler(p, &attr, check, true);
784 }
785 /**
786  * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
787  * @p: the task in question.
788  * @policy: new policy.
789  * @param: structure containing the new RT priority.
790  *
791  * Use sched_set_fifo(), read its comment.
792  *
793  * Return: 0 on success. An error code otherwise.
794  *
795  * NOTE that the task may be already dead.
796  */
797 int sched_setscheduler(struct task_struct *p, int policy,
798 		       const struct sched_param *param)
799 {
800 	return _sched_setscheduler(p, policy, param, true);
801 }
802 
803 int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
804 {
805 	return __sched_setscheduler(p, attr, true, true);
806 }
807 
808 int sched_setattr_nocheck(struct task_struct *p, const struct sched_attr *attr)
809 {
810 	return __sched_setscheduler(p, attr, false, true);
811 }
812 EXPORT_SYMBOL_GPL(sched_setattr_nocheck);
813 
814 /**
815  * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernel-space.
816  * @p: the task in question.
817  * @policy: new policy.
818  * @param: structure containing the new RT priority.
819  *
820  * Just like sched_setscheduler, only don't bother checking if the
821  * current context has permission.  For example, this is needed in
822  * stop_machine(): we create temporary high priority worker threads,
823  * but our caller might not have that capability.
824  *
825  * Return: 0 on success. An error code otherwise.
826  */
827 int sched_setscheduler_nocheck(struct task_struct *p, int policy,
828 			       const struct sched_param *param)
829 {
830 	return _sched_setscheduler(p, policy, param, false);
831 }
832 
833 /*
834  * SCHED_FIFO is a broken scheduler model; that is, it is fundamentally
835  * incapable of resource management, which is the one thing an OS really should
836  * be doing.
837  *
838  * This is of course the reason it is limited to privileged users only.
839  *
840  * Worse still; it is fundamentally impossible to compose static priority
841  * workloads. You cannot take two correctly working static prio workloads
842  * and smash them together and still expect them to work.
843  *
844  * For this reason 'all' FIFO tasks the kernel creates are basically at:
845  *
846  *   MAX_RT_PRIO / 2
847  *
848  * The administrator _MUST_ configure the system, the kernel simply doesn't
849  * know enough information to make a sensible choice.
850  */
851 void sched_set_fifo(struct task_struct *p)
852 {
853 	struct sched_param sp = { .sched_priority = MAX_RT_PRIO / 2 };
854 	WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0);
855 }
856 EXPORT_SYMBOL_GPL(sched_set_fifo);
857 
858 /*
859  * For when you don't much care about FIFO, but want to be above SCHED_NORMAL.
860  */
861 void sched_set_fifo_low(struct task_struct *p)
862 {
863 	struct sched_param sp = { .sched_priority = 1 };
864 	WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0);
865 }
866 EXPORT_SYMBOL_GPL(sched_set_fifo_low);
867 
868 void sched_set_normal(struct task_struct *p, int nice)
869 {
870 	struct sched_attr attr = {
871 		.sched_policy = SCHED_NORMAL,
872 		.sched_nice = nice,
873 	};
874 	WARN_ON_ONCE(sched_setattr_nocheck(p, &attr) != 0);
875 }
876 EXPORT_SYMBOL_GPL(sched_set_normal);
877 
878 static int
879 do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
880 {
881 	struct sched_param lparam;
882 
883 	if (!param || pid < 0)
884 		return -EINVAL;
885 	if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
886 		return -EFAULT;
887 
888 	CLASS(find_get_task, p)(pid);
889 	if (!p)
890 		return -ESRCH;
891 
892 	return sched_setscheduler(p, policy, &lparam);
893 }
894 
895 /*
896  * Mimics kernel/events/core.c perf_copy_attr().
897  */
898 static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *attr)
899 {
900 	u32 size;
901 	int ret;
902 
903 	/* Zero the full structure, so that a short copy will be nice: */
904 	memset(attr, 0, sizeof(*attr));
905 
906 	ret = get_user(size, &uattr->size);
907 	if (ret)
908 		return ret;
909 
910 	/* ABI compatibility quirk: */
911 	if (!size)
912 		size = SCHED_ATTR_SIZE_VER0;
913 	if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE)
914 		goto err_size;
915 
916 	ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
917 	if (ret) {
918 		if (ret == -E2BIG)
919 			goto err_size;
920 		return ret;
921 	}
922 
923 	if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) &&
924 	    size < SCHED_ATTR_SIZE_VER1)
925 		return -EINVAL;
926 
927 	/*
928 	 * XXX: Do we want to be lenient like existing syscalls; or do we want
929 	 * to be strict and return an error on out-of-bounds values?
930 	 */
931 	attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
932 
933 	return 0;
934 
935 err_size:
936 	put_user(sizeof(*attr), &uattr->size);
937 	return -E2BIG;
938 }
939 
940 static void get_params(struct task_struct *p, struct sched_attr *attr)
941 {
942 	if (task_has_dl_policy(p)) {
943 		__getparam_dl(p, attr);
944 	} else if (task_has_rt_policy(p)) {
945 		attr->sched_priority = p->rt_priority;
946 	} else {
947 		attr->sched_nice = task_nice(p);
948 		attr->sched_runtime = p->se.slice;
949 	}
950 }
951 
952 /**
953  * sys_sched_setscheduler - set/change the scheduler policy and RT priority
954  * @pid: the pid in question.
955  * @policy: new policy.
956  * @param: structure containing the new RT priority.
957  *
958  * Return: 0 on success. An error code otherwise.
959  */
960 SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, struct sched_param __user *, param)
961 {
962 	if (policy < 0)
963 		return -EINVAL;
964 
965 	return do_sched_setscheduler(pid, policy, param);
966 }
967 
968 /**
969  * sys_sched_setparam - set/change the RT priority of a thread
970  * @pid: the pid in question.
971  * @param: structure containing the new RT priority.
972  *
973  * Return: 0 on success. An error code otherwise.
974  */
975 SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
976 {
977 	return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
978 }
979 
980 /**
981  * sys_sched_setattr - same as above, but with extended sched_attr
982  * @pid: the pid in question.
983  * @uattr: structure containing the extended parameters.
984  * @flags: for future extension.
985  */
986 SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
987 			       unsigned int, flags)
988 {
989 	struct sched_attr attr;
990 	int retval;
991 
992 	if (!uattr || pid < 0 || flags)
993 		return -EINVAL;
994 
995 	retval = sched_copy_attr(uattr, &attr);
996 	if (retval)
997 		return retval;
998 
999 	if ((int)attr.sched_policy < 0)
1000 		return -EINVAL;
1001 	if (attr.sched_flags & SCHED_FLAG_KEEP_POLICY)
1002 		attr.sched_policy = SETPARAM_POLICY;
1003 
1004 	CLASS(find_get_task, p)(pid);
1005 	if (!p)
1006 		return -ESRCH;
1007 
1008 	if (attr.sched_flags & SCHED_FLAG_KEEP_PARAMS)
1009 		get_params(p, &attr);
1010 
1011 	return sched_setattr(p, &attr);
1012 }
1013 
1014 /**
1015  * sys_sched_getscheduler - get the policy (scheduling class) of a thread
1016  * @pid: the pid in question.
1017  *
1018  * Return: On success, the policy of the thread. Otherwise, a negative error
1019  * code.
1020  */
1021 SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
1022 {
1023 	struct task_struct *p;
1024 	int retval;
1025 
1026 	if (pid < 0)
1027 		return -EINVAL;
1028 
1029 	guard(rcu)();
1030 	p = find_process_by_pid(pid);
1031 	if (!p)
1032 		return -ESRCH;
1033 
1034 	retval = security_task_getscheduler(p);
1035 	if (!retval) {
1036 		retval = p->policy;
1037 		if (p->sched_reset_on_fork)
1038 			retval |= SCHED_RESET_ON_FORK;
1039 	}
1040 	return retval;
1041 }
1042 
1043 /**
1044  * sys_sched_getparam - get the RT priority of a thread
1045  * @pid: the pid in question.
1046  * @param: structure containing the RT priority.
1047  *
1048  * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
1049  * code.
1050  */
1051 SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
1052 {
1053 	struct sched_param lp = { .sched_priority = 0 };
1054 	struct task_struct *p;
1055 	int retval;
1056 
1057 	if (!param || pid < 0)
1058 		return -EINVAL;
1059 
1060 	scoped_guard (rcu) {
1061 		p = find_process_by_pid(pid);
1062 		if (!p)
1063 			return -ESRCH;
1064 
1065 		retval = security_task_getscheduler(p);
1066 		if (retval)
1067 			return retval;
1068 
1069 		if (task_has_rt_policy(p))
1070 			lp.sched_priority = p->rt_priority;
1071 	}
1072 
1073 	/*
1074 	 * This one might sleep, we cannot do it with a spinlock held ...
1075 	 */
1076 	return copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
1077 }
1078 
1079 /*
1080  * Copy the kernel size attribute structure (which might be larger
1081  * than what user-space knows about) to user-space.
1082  *
1083  * Note that all cases are valid: user-space buffer can be larger or
1084  * smaller than the kernel-space buffer. The usual case is that both
1085  * have the same size.
1086  */
1087 static int
1088 sched_attr_copy_to_user(struct sched_attr __user *uattr,
1089 			struct sched_attr *kattr,
1090 			unsigned int usize)
1091 {
1092 	unsigned int ksize = sizeof(*kattr);
1093 
1094 	if (!access_ok(uattr, usize))
1095 		return -EFAULT;
1096 
1097 	/*
1098 	 * sched_getattr() ABI forwards and backwards compatibility:
1099 	 *
1100 	 * If usize == ksize then we just copy everything to user-space and all is good.
1101 	 *
1102 	 * If usize < ksize then we only copy as much as user-space has space for,
1103 	 * this keeps ABI compatibility as well. We skip the rest.
1104 	 *
1105 	 * If usize > ksize then user-space is using a newer version of the ABI,
1106 	 * which part the kernel doesn't know about. Just ignore it - tooling can
1107 	 * detect the kernel's knowledge of attributes from the attr->size value
1108 	 * which is set to ksize in this case.
1109 	 */
1110 	kattr->size = min(usize, ksize);
1111 
1112 	if (copy_to_user(uattr, kattr, kattr->size))
1113 		return -EFAULT;
1114 
1115 	return 0;
1116 }
1117 
1118 /**
1119  * sys_sched_getattr - similar to sched_getparam, but with sched_attr
1120  * @pid: the pid in question.
1121  * @uattr: structure containing the extended parameters.
1122  * @usize: sizeof(attr) for fwd/bwd comp.
1123  * @flags: for future extension.
1124  */
1125 SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
1126 		unsigned int, usize, unsigned int, flags)
1127 {
1128 	struct sched_attr kattr = { };
1129 	struct task_struct *p;
1130 	int retval;
1131 
1132 	if (!uattr || pid < 0 || usize > PAGE_SIZE ||
1133 	    usize < SCHED_ATTR_SIZE_VER0 || flags)
1134 		return -EINVAL;
1135 
1136 	scoped_guard (rcu) {
1137 		p = find_process_by_pid(pid);
1138 		if (!p)
1139 			return -ESRCH;
1140 
1141 		retval = security_task_getscheduler(p);
1142 		if (retval)
1143 			return retval;
1144 
1145 		kattr.sched_policy = p->policy;
1146 		if (p->sched_reset_on_fork)
1147 			kattr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
1148 		get_params(p, &kattr);
1149 		kattr.sched_flags &= SCHED_FLAG_ALL;
1150 
1151 #ifdef CONFIG_UCLAMP_TASK
1152 		/*
1153 		 * This could race with another potential updater, but this is fine
1154 		 * because it'll correctly read the old or the new value. We don't need
1155 		 * to guarantee who wins the race as long as it doesn't return garbage.
1156 		 */
1157 		kattr.sched_util_min = p->uclamp_req[UCLAMP_MIN].value;
1158 		kattr.sched_util_max = p->uclamp_req[UCLAMP_MAX].value;
1159 #endif
1160 	}
1161 
1162 	return sched_attr_copy_to_user(uattr, &kattr, usize);
1163 }
1164 
1165 #ifdef CONFIG_SMP
1166 int dl_task_check_affinity(struct task_struct *p, const struct cpumask *mask)
1167 {
1168 	/*
1169 	 * If the task isn't a deadline task or admission control is
1170 	 * disabled then we don't care about affinity changes.
1171 	 */
1172 	if (!task_has_dl_policy(p) || !dl_bandwidth_enabled())
1173 		return 0;
1174 
1175 	/*
1176 	 * Since bandwidth control happens on root_domain basis,
1177 	 * if admission test is enabled, we only admit -deadline
1178 	 * tasks allowed to run on all the CPUs in the task's
1179 	 * root_domain.
1180 	 */
1181 	guard(rcu)();
1182 	if (!cpumask_subset(task_rq(p)->rd->span, mask))
1183 		return -EBUSY;
1184 
1185 	return 0;
1186 }
1187 #endif /* CONFIG_SMP */
1188 
1189 int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx)
1190 {
1191 	int retval;
1192 	cpumask_var_t cpus_allowed, new_mask;
1193 
1194 	if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL))
1195 		return -ENOMEM;
1196 
1197 	if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
1198 		retval = -ENOMEM;
1199 		goto out_free_cpus_allowed;
1200 	}
1201 
1202 	cpuset_cpus_allowed(p, cpus_allowed);
1203 	cpumask_and(new_mask, ctx->new_mask, cpus_allowed);
1204 
1205 	ctx->new_mask = new_mask;
1206 	ctx->flags |= SCA_CHECK;
1207 
1208 	retval = dl_task_check_affinity(p, new_mask);
1209 	if (retval)
1210 		goto out_free_new_mask;
1211 
1212 	retval = __set_cpus_allowed_ptr(p, ctx);
1213 	if (retval)
1214 		goto out_free_new_mask;
1215 
1216 	cpuset_cpus_allowed(p, cpus_allowed);
1217 	if (!cpumask_subset(new_mask, cpus_allowed)) {
1218 		/*
1219 		 * We must have raced with a concurrent cpuset update.
1220 		 * Just reset the cpumask to the cpuset's cpus_allowed.
1221 		 */
1222 		cpumask_copy(new_mask, cpus_allowed);
1223 
1224 		/*
1225 		 * If SCA_USER is set, a 2nd call to __set_cpus_allowed_ptr()
1226 		 * will restore the previous user_cpus_ptr value.
1227 		 *
1228 		 * In the unlikely event a previous user_cpus_ptr exists,
1229 		 * we need to further restrict the mask to what is allowed
1230 		 * by that old user_cpus_ptr.
1231 		 */
1232 		if (unlikely((ctx->flags & SCA_USER) && ctx->user_mask)) {
1233 			bool empty = !cpumask_and(new_mask, new_mask,
1234 						  ctx->user_mask);
1235 
1236 			if (WARN_ON_ONCE(empty))
1237 				cpumask_copy(new_mask, cpus_allowed);
1238 		}
1239 		__set_cpus_allowed_ptr(p, ctx);
1240 		retval = -EINVAL;
1241 	}
1242 
1243 out_free_new_mask:
1244 	free_cpumask_var(new_mask);
1245 out_free_cpus_allowed:
1246 	free_cpumask_var(cpus_allowed);
1247 	return retval;
1248 }
1249 
1250 long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
1251 {
1252 	struct affinity_context ac;
1253 	struct cpumask *user_mask;
1254 	int retval;
1255 
1256 	CLASS(find_get_task, p)(pid);
1257 	if (!p)
1258 		return -ESRCH;
1259 
1260 	if (p->flags & PF_NO_SETAFFINITY)
1261 		return -EINVAL;
1262 
1263 	if (!check_same_owner(p)) {
1264 		guard(rcu)();
1265 		if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
1266 			return -EPERM;
1267 	}
1268 
1269 	retval = security_task_setscheduler(p);
1270 	if (retval)
1271 		return retval;
1272 
1273 	/*
1274 	 * With non-SMP configs, user_cpus_ptr/user_mask isn't used and
1275 	 * alloc_user_cpus_ptr() returns NULL.
1276 	 */
1277 	user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE);
1278 	if (user_mask) {
1279 		cpumask_copy(user_mask, in_mask);
1280 	} else if (IS_ENABLED(CONFIG_SMP)) {
1281 		return -ENOMEM;
1282 	}
1283 
1284 	ac = (struct affinity_context){
1285 		.new_mask  = in_mask,
1286 		.user_mask = user_mask,
1287 		.flags     = SCA_USER,
1288 	};
1289 
1290 	retval = __sched_setaffinity(p, &ac);
1291 	kfree(ac.user_mask);
1292 
1293 	return retval;
1294 }
1295 
1296 static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
1297 			     struct cpumask *new_mask)
1298 {
1299 	if (len < cpumask_size())
1300 		cpumask_clear(new_mask);
1301 	else if (len > cpumask_size())
1302 		len = cpumask_size();
1303 
1304 	return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
1305 }
1306 
1307 /**
1308  * sys_sched_setaffinity - set the CPU affinity of a process
1309  * @pid: pid of the process
1310  * @len: length in bytes of the bitmask pointed to by user_mask_ptr
1311  * @user_mask_ptr: user-space pointer to the new CPU mask
1312  *
1313  * Return: 0 on success. An error code otherwise.
1314  */
1315 SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
1316 		unsigned long __user *, user_mask_ptr)
1317 {
1318 	cpumask_var_t new_mask;
1319 	int retval;
1320 
1321 	if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
1322 		return -ENOMEM;
1323 
1324 	retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
1325 	if (retval == 0)
1326 		retval = sched_setaffinity(pid, new_mask);
1327 	free_cpumask_var(new_mask);
1328 	return retval;
1329 }
1330 
1331 long sched_getaffinity(pid_t pid, struct cpumask *mask)
1332 {
1333 	struct task_struct *p;
1334 	int retval;
1335 
1336 	guard(rcu)();
1337 	p = find_process_by_pid(pid);
1338 	if (!p)
1339 		return -ESRCH;
1340 
1341 	retval = security_task_getscheduler(p);
1342 	if (retval)
1343 		return retval;
1344 
1345 	guard(raw_spinlock_irqsave)(&p->pi_lock);
1346 	cpumask_and(mask, &p->cpus_mask, cpu_active_mask);
1347 
1348 	return 0;
1349 }
1350 
1351 /**
1352  * sys_sched_getaffinity - get the CPU affinity of a process
1353  * @pid: pid of the process
1354  * @len: length in bytes of the bitmask pointed to by user_mask_ptr
1355  * @user_mask_ptr: user-space pointer to hold the current CPU mask
1356  *
1357  * Return: size of CPU mask copied to user_mask_ptr on success. An
1358  * error code otherwise.
1359  */
1360 SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
1361 		unsigned long __user *, user_mask_ptr)
1362 {
1363 	int ret;
1364 	cpumask_var_t mask;
1365 
1366 	if ((len * BITS_PER_BYTE) < nr_cpu_ids)
1367 		return -EINVAL;
1368 	if (len & (sizeof(unsigned long)-1))
1369 		return -EINVAL;
1370 
1371 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
1372 		return -ENOMEM;
1373 
1374 	ret = sched_getaffinity(pid, mask);
1375 	if (ret == 0) {
1376 		unsigned int retlen = min(len, cpumask_size());
1377 
1378 		if (copy_to_user(user_mask_ptr, cpumask_bits(mask), retlen))
1379 			ret = -EFAULT;
1380 		else
1381 			ret = retlen;
1382 	}
1383 	free_cpumask_var(mask);
1384 
1385 	return ret;
1386 }
1387 
1388 static void do_sched_yield(void)
1389 {
1390 	struct rq_flags rf;
1391 	struct rq *rq;
1392 
1393 	rq = this_rq_lock_irq(&rf);
1394 
1395 	schedstat_inc(rq->yld_count);
1396 	current->sched_class->yield_task(rq);
1397 
1398 	preempt_disable();
1399 	rq_unlock_irq(rq, &rf);
1400 	sched_preempt_enable_no_resched();
1401 
1402 	schedule();
1403 }
1404 
1405 /**
1406  * sys_sched_yield - yield the current processor to other threads.
1407  *
1408  * This function yields the current CPU to other tasks. If there are no
1409  * other threads running on this CPU then this function will return.
1410  *
1411  * Return: 0.
1412  */
1413 SYSCALL_DEFINE0(sched_yield)
1414 {
1415 	do_sched_yield();
1416 	return 0;
1417 }
1418 
1419 /**
1420  * yield - yield the current processor to other threads.
1421  *
1422  * Do not ever use this function, there's a 99% chance you're doing it wrong.
1423  *
1424  * The scheduler is at all times free to pick the calling task as the most
1425  * eligible task to run, if removing the yield() call from your code breaks
1426  * it, it's already broken.
1427  *
1428  * Typical broken usage is:
1429  *
1430  * while (!event)
1431  *	yield();
1432  *
1433  * where one assumes that yield() will let 'the other' process run that will
1434  * make event true. If the current task is a SCHED_FIFO task that will never
1435  * happen. Never use yield() as a progress guarantee!!
1436  *
1437  * If you want to use yield() to wait for something, use wait_event().
1438  * If you want to use yield() to be 'nice' for others, use cond_resched().
1439  * If you still want to use yield(), do not!
1440  */
1441 void __sched yield(void)
1442 {
1443 	set_current_state(TASK_RUNNING);
1444 	do_sched_yield();
1445 }
1446 EXPORT_SYMBOL(yield);
1447 
1448 /**
1449  * yield_to - yield the current processor to another thread in
1450  * your thread group, or accelerate that thread toward the
1451  * processor it's on.
1452  * @p: target task
1453  * @preempt: whether task preemption is allowed or not
1454  *
1455  * It's the caller's job to ensure that the target task struct
1456  * can't go away on us before we can do any checks.
1457  *
1458  * Return:
1459  *	true (>0) if we indeed boosted the target task.
1460  *	false (0) if we failed to boost the target.
1461  *	-ESRCH if there's no task to yield to.
1462  */
1463 int __sched yield_to(struct task_struct *p, bool preempt)
1464 {
1465 	struct task_struct *curr = current;
1466 	struct rq *rq, *p_rq;
1467 	int yielded = 0;
1468 
1469 	scoped_guard (irqsave) {
1470 		rq = this_rq();
1471 
1472 again:
1473 		p_rq = task_rq(p);
1474 		/*
1475 		 * If we're the only runnable task on the rq and target rq also
1476 		 * has only one task, there's absolutely no point in yielding.
1477 		 */
1478 		if (rq->nr_running == 1 && p_rq->nr_running == 1)
1479 			return -ESRCH;
1480 
1481 		guard(double_rq_lock)(rq, p_rq);
1482 		if (task_rq(p) != p_rq)
1483 			goto again;
1484 
1485 		if (!curr->sched_class->yield_to_task)
1486 			return 0;
1487 
1488 		if (curr->sched_class != p->sched_class)
1489 			return 0;
1490 
1491 		if (task_on_cpu(p_rq, p) || !task_is_running(p))
1492 			return 0;
1493 
1494 		yielded = curr->sched_class->yield_to_task(rq, p);
1495 		if (yielded) {
1496 			schedstat_inc(rq->yld_count);
1497 			/*
1498 			 * Make p's CPU reschedule; pick_next_entity
1499 			 * takes care of fairness.
1500 			 */
1501 			if (preempt && rq != p_rq)
1502 				resched_curr(p_rq);
1503 		}
1504 	}
1505 
1506 	if (yielded)
1507 		schedule();
1508 
1509 	return yielded;
1510 }
1511 EXPORT_SYMBOL_GPL(yield_to);
1512 
1513 /**
1514  * sys_sched_get_priority_max - return maximum RT priority.
1515  * @policy: scheduling class.
1516  *
1517  * Return: On success, this syscall returns the maximum
1518  * rt_priority that can be used by a given scheduling class.
1519  * On failure, a negative error code is returned.
1520  */
1521 SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
1522 {
1523 	int ret = -EINVAL;
1524 
1525 	switch (policy) {
1526 	case SCHED_FIFO:
1527 	case SCHED_RR:
1528 		ret = MAX_RT_PRIO-1;
1529 		break;
1530 	case SCHED_DEADLINE:
1531 	case SCHED_NORMAL:
1532 	case SCHED_BATCH:
1533 	case SCHED_IDLE:
1534 	case SCHED_EXT:
1535 		ret = 0;
1536 		break;
1537 	}
1538 	return ret;
1539 }
1540 
1541 /**
1542  * sys_sched_get_priority_min - return minimum RT priority.
1543  * @policy: scheduling class.
1544  *
1545  * Return: On success, this syscall returns the minimum
1546  * rt_priority that can be used by a given scheduling class.
1547  * On failure, a negative error code is returned.
1548  */
1549 SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
1550 {
1551 	int ret = -EINVAL;
1552 
1553 	switch (policy) {
1554 	case SCHED_FIFO:
1555 	case SCHED_RR:
1556 		ret = 1;
1557 		break;
1558 	case SCHED_DEADLINE:
1559 	case SCHED_NORMAL:
1560 	case SCHED_BATCH:
1561 	case SCHED_IDLE:
1562 	case SCHED_EXT:
1563 		ret = 0;
1564 	}
1565 	return ret;
1566 }
1567 
1568 static int sched_rr_get_interval(pid_t pid, struct timespec64 *t)
1569 {
1570 	unsigned int time_slice = 0;
1571 	int retval;
1572 
1573 	if (pid < 0)
1574 		return -EINVAL;
1575 
1576 	scoped_guard (rcu) {
1577 		struct task_struct *p = find_process_by_pid(pid);
1578 		if (!p)
1579 			return -ESRCH;
1580 
1581 		retval = security_task_getscheduler(p);
1582 		if (retval)
1583 			return retval;
1584 
1585 		scoped_guard (task_rq_lock, p) {
1586 			struct rq *rq = scope.rq;
1587 			if (p->sched_class->get_rr_interval)
1588 				time_slice = p->sched_class->get_rr_interval(rq, p);
1589 		}
1590 	}
1591 
1592 	jiffies_to_timespec64(time_slice, t);
1593 	return 0;
1594 }
1595 
1596 /**
1597  * sys_sched_rr_get_interval - return the default time-slice of a process.
1598  * @pid: pid of the process.
1599  * @interval: userspace pointer to the time-slice value.
1600  *
1601  * this syscall writes the default time-slice value of a given process
1602  * into the user-space timespec buffer. A value of '0' means infinity.
1603  *
1604  * Return: On success, 0 and the time-slice is in @interval. Otherwise,
1605  * an error code.
1606  */
1607 SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
1608 		struct __kernel_timespec __user *, interval)
1609 {
1610 	struct timespec64 t;
1611 	int retval = sched_rr_get_interval(pid, &t);
1612 
1613 	if (retval == 0)
1614 		retval = put_timespec64(&t, interval);
1615 
1616 	return retval;
1617 }
1618 
1619 #ifdef CONFIG_COMPAT_32BIT_TIME
1620 SYSCALL_DEFINE2(sched_rr_get_interval_time32, pid_t, pid,
1621 		struct old_timespec32 __user *, interval)
1622 {
1623 	struct timespec64 t;
1624 	int retval = sched_rr_get_interval(pid, &t);
1625 
1626 	if (retval == 0)
1627 		retval = put_old_timespec32(&t, interval);
1628 	return retval;
1629 }
1630 #endif
1631