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