syscalls.c 39 KB

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