sched.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. #include <linux/sched.h>
  2. #include <linux/mutex.h>
  3. #include <linux/spinlock.h>
  4. #include <linux/stop_machine.h>
  5. #include "cpupri.h"
  6. extern __read_mostly int scheduler_running;
  7. /*
  8. * Convert user-nice values [ -20 ... 0 ... 19 ]
  9. * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  10. * and back.
  11. */
  12. #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  13. #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  14. #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  15. /*
  16. * 'User priority' is the nice value converted to something we
  17. * can work with better when scaling various scheduler parameters,
  18. * it's a [ 0 ... 39 ] range.
  19. */
  20. #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
  21. #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  22. #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  23. /*
  24. * Helpers for converting nanosecond timing to jiffy resolution
  25. */
  26. #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
  27. #define NICE_0_LOAD SCHED_LOAD_SCALE
  28. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  29. /*
  30. * These are the 'tuning knobs' of the scheduler:
  31. */
  32. /*
  33. * single value that denotes runtime == period, ie unlimited time.
  34. */
  35. #define RUNTIME_INF ((u64)~0ULL)
  36. static inline int rt_policy(int policy)
  37. {
  38. if (policy == SCHED_FIFO || policy == SCHED_RR)
  39. return 1;
  40. return 0;
  41. }
  42. static inline int task_has_rt_policy(struct task_struct *p)
  43. {
  44. return rt_policy(p->policy);
  45. }
  46. /*
  47. * This is the priority-queue data structure of the RT scheduling class:
  48. */
  49. struct rt_prio_array {
  50. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  51. struct list_head queue[MAX_RT_PRIO];
  52. };
  53. struct rt_bandwidth {
  54. /* nests inside the rq lock: */
  55. raw_spinlock_t rt_runtime_lock;
  56. ktime_t rt_period;
  57. u64 rt_runtime;
  58. struct hrtimer rt_period_timer;
  59. };
  60. extern struct mutex sched_domains_mutex;
  61. #ifdef CONFIG_CGROUP_SCHED
  62. #include <linux/cgroup.h>
  63. struct cfs_rq;
  64. struct rt_rq;
  65. static LIST_HEAD(task_groups);
  66. struct cfs_bandwidth {
  67. #ifdef CONFIG_CFS_BANDWIDTH
  68. raw_spinlock_t lock;
  69. ktime_t period;
  70. u64 quota, runtime;
  71. s64 hierarchal_quota;
  72. u64 runtime_expires;
  73. int idle, timer_active;
  74. struct hrtimer period_timer, slack_timer;
  75. struct list_head throttled_cfs_rq;
  76. /* statistics */
  77. int nr_periods, nr_throttled;
  78. u64 throttled_time;
  79. #endif
  80. };
  81. /* task group related information */
  82. struct task_group {
  83. struct cgroup_subsys_state css;
  84. #ifdef CONFIG_FAIR_GROUP_SCHED
  85. /* schedulable entities of this group on each cpu */
  86. struct sched_entity **se;
  87. /* runqueue "owned" by this group on each cpu */
  88. struct cfs_rq **cfs_rq;
  89. unsigned long shares;
  90. atomic_t load_weight;
  91. #endif
  92. #ifdef CONFIG_RT_GROUP_SCHED
  93. struct sched_rt_entity **rt_se;
  94. struct rt_rq **rt_rq;
  95. struct rt_bandwidth rt_bandwidth;
  96. #endif
  97. struct rcu_head rcu;
  98. struct list_head list;
  99. struct task_group *parent;
  100. struct list_head siblings;
  101. struct list_head children;
  102. #ifdef CONFIG_SCHED_AUTOGROUP
  103. struct autogroup *autogroup;
  104. #endif
  105. struct cfs_bandwidth cfs_bandwidth;
  106. };
  107. #ifdef CONFIG_FAIR_GROUP_SCHED
  108. #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
  109. /*
  110. * A weight of 0 or 1 can cause arithmetics problems.
  111. * A weight of a cfs_rq is the sum of weights of which entities
  112. * are queued on this cfs_rq, so a weight of a entity should not be
  113. * too large, so as the shares value of a task group.
  114. * (The default weight is 1024 - so there's no practical
  115. * limitation from this.)
  116. */
  117. #define MIN_SHARES (1UL << 1)
  118. #define MAX_SHARES (1UL << 18)
  119. #endif
  120. /* Default task group.
  121. * Every task in system belong to this group at bootup.
  122. */
  123. extern struct task_group root_task_group;
  124. typedef int (*tg_visitor)(struct task_group *, void *);
  125. extern int walk_tg_tree_from(struct task_group *from,
  126. tg_visitor down, tg_visitor up, void *data);
  127. /*
  128. * Iterate the full tree, calling @down when first entering a node and @up when
  129. * leaving it for the final time.
  130. *
  131. * Caller must hold rcu_lock or sufficient equivalent.
  132. */
  133. static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
  134. {
  135. return walk_tg_tree_from(&root_task_group, down, up, data);
  136. }
  137. extern int tg_nop(struct task_group *tg, void *data);
  138. extern void free_fair_sched_group(struct task_group *tg);
  139. extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
  140. extern void unregister_fair_sched_group(struct task_group *tg, int cpu);
  141. extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  142. struct sched_entity *se, int cpu,
  143. struct sched_entity *parent);
  144. extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
  145. extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
  146. extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
  147. extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
  148. extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
  149. extern void free_rt_sched_group(struct task_group *tg);
  150. extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
  151. extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  152. struct sched_rt_entity *rt_se, int cpu,
  153. struct sched_rt_entity *parent);
  154. #else /* CONFIG_CGROUP_SCHED */
  155. struct cfs_bandwidth { };
  156. #endif /* CONFIG_CGROUP_SCHED */
  157. /* CFS-related fields in a runqueue */
  158. struct cfs_rq {
  159. struct load_weight load;
  160. unsigned int nr_running, h_nr_running;
  161. u64 exec_clock;
  162. u64 min_vruntime;
  163. #ifndef CONFIG_64BIT
  164. u64 min_vruntime_copy;
  165. #endif
  166. struct rb_root tasks_timeline;
  167. struct rb_node *rb_leftmost;
  168. /*
  169. * 'curr' points to currently running entity on this cfs_rq.
  170. * It is set to NULL otherwise (i.e when none are currently running).
  171. */
  172. struct sched_entity *curr, *next, *last, *skip;
  173. #ifdef CONFIG_SCHED_DEBUG
  174. unsigned int nr_spread_over;
  175. #endif
  176. #ifdef CONFIG_FAIR_GROUP_SCHED
  177. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  178. /*
  179. * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  180. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  181. * (like users, containers etc.)
  182. *
  183. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  184. * list is used during load balance.
  185. */
  186. int on_list;
  187. struct list_head leaf_cfs_rq_list;
  188. struct task_group *tg; /* group that "owns" this runqueue */
  189. #ifdef CONFIG_SMP
  190. /*
  191. * h_load = weight * f(tg)
  192. *
  193. * Where f(tg) is the recursive weight fraction assigned to
  194. * this group.
  195. */
  196. unsigned long h_load;
  197. /*
  198. * Maintaining per-cpu shares distribution for group scheduling
  199. *
  200. * load_stamp is the last time we updated the load average
  201. * load_last is the last time we updated the load average and saw load
  202. * load_unacc_exec_time is currently unaccounted execution time
  203. */
  204. u64 load_avg;
  205. u64 load_period;
  206. u64 load_stamp, load_last, load_unacc_exec_time;
  207. unsigned long load_contribution;
  208. #endif /* CONFIG_SMP */
  209. #ifdef CONFIG_CFS_BANDWIDTH
  210. int runtime_enabled;
  211. u64 runtime_expires;
  212. s64 runtime_remaining;
  213. u64 throttled_timestamp;
  214. int throttled, throttle_count;
  215. struct list_head throttled_list;
  216. #endif /* CONFIG_CFS_BANDWIDTH */
  217. #endif /* CONFIG_FAIR_GROUP_SCHED */
  218. };
  219. static inline int rt_bandwidth_enabled(void)
  220. {
  221. return sysctl_sched_rt_runtime >= 0;
  222. }
  223. /* Real-Time classes' related field in a runqueue: */
  224. struct rt_rq {
  225. struct rt_prio_array active;
  226. unsigned int rt_nr_running;
  227. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  228. struct {
  229. int curr; /* highest queued rt task prio */
  230. #ifdef CONFIG_SMP
  231. int next; /* next highest */
  232. #endif
  233. } highest_prio;
  234. #endif
  235. #ifdef CONFIG_SMP
  236. unsigned long rt_nr_migratory;
  237. unsigned long rt_nr_total;
  238. int overloaded;
  239. struct plist_head pushable_tasks;
  240. #endif
  241. int rt_throttled;
  242. u64 rt_time;
  243. u64 rt_runtime;
  244. /* Nests inside the rq lock: */
  245. raw_spinlock_t rt_runtime_lock;
  246. #ifdef CONFIG_RT_GROUP_SCHED
  247. unsigned long rt_nr_boosted;
  248. struct rq *rq;
  249. struct list_head leaf_rt_rq_list;
  250. struct task_group *tg;
  251. #endif
  252. };
  253. #ifdef CONFIG_SMP
  254. /*
  255. * We add the notion of a root-domain which will be used to define per-domain
  256. * variables. Each exclusive cpuset essentially defines an island domain by
  257. * fully partitioning the member cpus from any other cpuset. Whenever a new
  258. * exclusive cpuset is created, we also create and attach a new root-domain
  259. * object.
  260. *
  261. */
  262. struct root_domain {
  263. atomic_t refcount;
  264. atomic_t rto_count;
  265. struct rcu_head rcu;
  266. cpumask_var_t span;
  267. cpumask_var_t online;
  268. /*
  269. * The "RT overload" flag: it gets set if a CPU has more than
  270. * one runnable RT task.
  271. */
  272. cpumask_var_t rto_mask;
  273. struct cpupri cpupri;
  274. };
  275. extern struct root_domain def_root_domain;
  276. #endif /* CONFIG_SMP */
  277. /*
  278. * This is the main, per-CPU runqueue data structure.
  279. *
  280. * Locking rule: those places that want to lock multiple runqueues
  281. * (such as the load balancing or the thread migration code), lock
  282. * acquire operations must be ordered by ascending &runqueue.
  283. */
  284. struct rq {
  285. /* runqueue lock: */
  286. raw_spinlock_t lock;
  287. /*
  288. * nr_running and cpu_load should be in the same cacheline because
  289. * remote CPUs use both these fields when doing load calculation.
  290. */
  291. unsigned int nr_running;
  292. #define CPU_LOAD_IDX_MAX 5
  293. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  294. unsigned long last_load_update_tick;
  295. #ifdef CONFIG_NO_HZ
  296. u64 nohz_stamp;
  297. unsigned long nohz_flags;
  298. #endif
  299. int skip_clock_update;
  300. /* capture load from *all* tasks on this cpu: */
  301. struct load_weight load;
  302. unsigned long nr_load_updates;
  303. u64 nr_switches;
  304. struct cfs_rq cfs;
  305. struct rt_rq rt;
  306. #ifdef CONFIG_FAIR_GROUP_SCHED
  307. /* list of leaf cfs_rq on this cpu: */
  308. struct list_head leaf_cfs_rq_list;
  309. #endif
  310. #ifdef CONFIG_RT_GROUP_SCHED
  311. struct list_head leaf_rt_rq_list;
  312. #endif
  313. /*
  314. * This is part of a global counter where only the total sum
  315. * over all CPUs matters. A task can increase this counter on
  316. * one CPU and if it got migrated afterwards it may decrease
  317. * it on another CPU. Always updated under the runqueue lock:
  318. */
  319. unsigned long nr_uninterruptible;
  320. struct task_struct *curr, *idle, *stop;
  321. unsigned long next_balance;
  322. struct mm_struct *prev_mm;
  323. u64 clock;
  324. u64 clock_task;
  325. atomic_t nr_iowait;
  326. #ifdef CONFIG_SMP
  327. struct root_domain *rd;
  328. struct sched_domain *sd;
  329. unsigned long cpu_power;
  330. unsigned char idle_balance;
  331. /* For active balancing */
  332. int post_schedule;
  333. int active_balance;
  334. int push_cpu;
  335. struct cpu_stop_work active_balance_work;
  336. /* cpu of this runqueue: */
  337. int cpu;
  338. int online;
  339. struct list_head cfs_tasks;
  340. u64 rt_avg;
  341. u64 age_stamp;
  342. u64 idle_stamp;
  343. u64 avg_idle;
  344. #endif
  345. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  346. u64 prev_irq_time;
  347. #endif
  348. #ifdef CONFIG_PARAVIRT
  349. u64 prev_steal_time;
  350. #endif
  351. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  352. u64 prev_steal_time_rq;
  353. #endif
  354. /* calc_load related fields */
  355. unsigned long calc_load_update;
  356. long calc_load_active;
  357. #ifdef CONFIG_SCHED_HRTICK
  358. #ifdef CONFIG_SMP
  359. int hrtick_csd_pending;
  360. struct call_single_data hrtick_csd;
  361. #endif
  362. struct hrtimer hrtick_timer;
  363. #endif
  364. #ifdef CONFIG_SCHEDSTATS
  365. /* latency stats */
  366. struct sched_info rq_sched_info;
  367. unsigned long long rq_cpu_time;
  368. /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
  369. /* sys_sched_yield() stats */
  370. unsigned int yld_count;
  371. /* schedule() stats */
  372. unsigned int sched_count;
  373. unsigned int sched_goidle;
  374. /* try_to_wake_up() stats */
  375. unsigned int ttwu_count;
  376. unsigned int ttwu_local;
  377. #endif
  378. #ifdef CONFIG_SMP
  379. struct llist_head wake_list;
  380. #endif
  381. };
  382. static inline int cpu_of(struct rq *rq)
  383. {
  384. #ifdef CONFIG_SMP
  385. return rq->cpu;
  386. #else
  387. return 0;
  388. #endif
  389. }
  390. DECLARE_PER_CPU(struct rq, runqueues);
  391. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  392. #define this_rq() (&__get_cpu_var(runqueues))
  393. #define task_rq(p) cpu_rq(task_cpu(p))
  394. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  395. #define raw_rq() (&__raw_get_cpu_var(runqueues))
  396. #ifdef CONFIG_SMP
  397. #define rcu_dereference_check_sched_domain(p) \
  398. rcu_dereference_check((p), \
  399. lockdep_is_held(&sched_domains_mutex))
  400. /*
  401. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  402. * See detach_destroy_domains: synchronize_sched for details.
  403. *
  404. * The domain tree of any CPU may only be accessed from within
  405. * preempt-disabled sections.
  406. */
  407. #define for_each_domain(cpu, __sd) \
  408. for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
  409. __sd; __sd = __sd->parent)
  410. #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
  411. /**
  412. * highest_flag_domain - Return highest sched_domain containing flag.
  413. * @cpu: The cpu whose highest level of sched domain is to
  414. * be returned.
  415. * @flag: The flag to check for the highest sched_domain
  416. * for the given cpu.
  417. *
  418. * Returns the highest sched_domain of a cpu which contains the given flag.
  419. */
  420. static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
  421. {
  422. struct sched_domain *sd, *hsd = NULL;
  423. for_each_domain(cpu, sd) {
  424. if (!(sd->flags & flag))
  425. break;
  426. hsd = sd;
  427. }
  428. return hsd;
  429. }
  430. DECLARE_PER_CPU(struct sched_domain *, sd_llc);
  431. DECLARE_PER_CPU(int, sd_llc_id);
  432. extern int group_balance_cpu(struct sched_group *sg);
  433. #endif /* CONFIG_SMP */
  434. #include "stats.h"
  435. #include "auto_group.h"
  436. #ifdef CONFIG_CGROUP_SCHED
  437. /*
  438. * Return the group to which this tasks belongs.
  439. *
  440. * We use task_subsys_state_check() and extend the RCU verification with
  441. * pi->lock and rq->lock because cpu_cgroup_attach() holds those locks for each
  442. * task it moves into the cgroup. Therefore by holding either of those locks,
  443. * we pin the task to the current cgroup.
  444. */
  445. static inline struct task_group *task_group(struct task_struct *p)
  446. {
  447. struct task_group *tg;
  448. struct cgroup_subsys_state *css;
  449. css = task_subsys_state_check(p, cpu_cgroup_subsys_id,
  450. lockdep_is_held(&p->pi_lock) ||
  451. lockdep_is_held(&task_rq(p)->lock));
  452. tg = container_of(css, struct task_group, css);
  453. return autogroup_task_group(p, tg);
  454. }
  455. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  456. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  457. {
  458. #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
  459. struct task_group *tg = task_group(p);
  460. #endif
  461. #ifdef CONFIG_FAIR_GROUP_SCHED
  462. p->se.cfs_rq = tg->cfs_rq[cpu];
  463. p->se.parent = tg->se[cpu];
  464. #endif
  465. #ifdef CONFIG_RT_GROUP_SCHED
  466. p->rt.rt_rq = tg->rt_rq[cpu];
  467. p->rt.parent = tg->rt_se[cpu];
  468. #endif
  469. }
  470. #else /* CONFIG_CGROUP_SCHED */
  471. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  472. static inline struct task_group *task_group(struct task_struct *p)
  473. {
  474. return NULL;
  475. }
  476. #endif /* CONFIG_CGROUP_SCHED */
  477. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  478. {
  479. set_task_rq(p, cpu);
  480. #ifdef CONFIG_SMP
  481. /*
  482. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  483. * successfuly executed on another CPU. We must ensure that updates of
  484. * per-task data have been completed by this moment.
  485. */
  486. smp_wmb();
  487. task_thread_info(p)->cpu = cpu;
  488. #endif
  489. }
  490. /*
  491. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  492. */
  493. #ifdef CONFIG_SCHED_DEBUG
  494. # include <linux/static_key.h>
  495. # define const_debug __read_mostly
  496. #else
  497. # define const_debug const
  498. #endif
  499. extern const_debug unsigned int sysctl_sched_features;
  500. #define SCHED_FEAT(name, enabled) \
  501. __SCHED_FEAT_##name ,
  502. enum {
  503. #include "features.h"
  504. __SCHED_FEAT_NR,
  505. };
  506. #undef SCHED_FEAT
  507. #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
  508. static __always_inline bool static_branch__true(struct static_key *key)
  509. {
  510. return static_key_true(key); /* Not out of line branch. */
  511. }
  512. static __always_inline bool static_branch__false(struct static_key *key)
  513. {
  514. return static_key_false(key); /* Out of line branch. */
  515. }
  516. #define SCHED_FEAT(name, enabled) \
  517. static __always_inline bool static_branch_##name(struct static_key *key) \
  518. { \
  519. return static_branch__##enabled(key); \
  520. }
  521. #include "features.h"
  522. #undef SCHED_FEAT
  523. extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
  524. #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
  525. #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
  526. #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  527. #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
  528. static inline u64 global_rt_period(void)
  529. {
  530. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  531. }
  532. static inline u64 global_rt_runtime(void)
  533. {
  534. if (sysctl_sched_rt_runtime < 0)
  535. return RUNTIME_INF;
  536. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  537. }
  538. static inline int task_current(struct rq *rq, struct task_struct *p)
  539. {
  540. return rq->curr == p;
  541. }
  542. static inline int task_running(struct rq *rq, struct task_struct *p)
  543. {
  544. #ifdef CONFIG_SMP
  545. return p->on_cpu;
  546. #else
  547. return task_current(rq, p);
  548. #endif
  549. }
  550. #ifndef prepare_arch_switch
  551. # define prepare_arch_switch(next) do { } while (0)
  552. #endif
  553. #ifndef finish_arch_switch
  554. # define finish_arch_switch(prev) do { } while (0)
  555. #endif
  556. #ifndef finish_arch_post_lock_switch
  557. # define finish_arch_post_lock_switch() do { } while (0)
  558. #endif
  559. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  560. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  561. {
  562. #ifdef CONFIG_SMP
  563. /*
  564. * We can optimise this out completely for !SMP, because the
  565. * SMP rebalancing from interrupt is the only thing that cares
  566. * here.
  567. */
  568. next->on_cpu = 1;
  569. #endif
  570. }
  571. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  572. {
  573. #ifdef CONFIG_SMP
  574. /*
  575. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  576. * We must ensure this doesn't happen until the switch is completely
  577. * finished.
  578. */
  579. smp_wmb();
  580. prev->on_cpu = 0;
  581. #endif
  582. #ifdef CONFIG_DEBUG_SPINLOCK
  583. /* this is a valid case when another task releases the spinlock */
  584. rq->lock.owner = current;
  585. #endif
  586. /*
  587. * If we are tracking spinlock dependencies then we have to
  588. * fix up the runqueue lock - which gets 'carried over' from
  589. * prev into current:
  590. */
  591. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  592. raw_spin_unlock_irq(&rq->lock);
  593. }
  594. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  595. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  596. {
  597. #ifdef CONFIG_SMP
  598. /*
  599. * We can optimise this out completely for !SMP, because the
  600. * SMP rebalancing from interrupt is the only thing that cares
  601. * here.
  602. */
  603. next->on_cpu = 1;
  604. #endif
  605. #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  606. raw_spin_unlock_irq(&rq->lock);
  607. #else
  608. raw_spin_unlock(&rq->lock);
  609. #endif
  610. }
  611. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  612. {
  613. #ifdef CONFIG_SMP
  614. /*
  615. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  616. * We must ensure this doesn't happen until the switch is completely
  617. * finished.
  618. */
  619. smp_wmb();
  620. prev->on_cpu = 0;
  621. #endif
  622. #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  623. local_irq_enable();
  624. #endif
  625. }
  626. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  627. static inline void update_load_add(struct load_weight *lw, unsigned long inc)
  628. {
  629. lw->weight += inc;
  630. lw->inv_weight = 0;
  631. }
  632. static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
  633. {
  634. lw->weight -= dec;
  635. lw->inv_weight = 0;
  636. }
  637. static inline void update_load_set(struct load_weight *lw, unsigned long w)
  638. {
  639. lw->weight = w;
  640. lw->inv_weight = 0;
  641. }
  642. /*
  643. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  644. * of tasks with abnormal "nice" values across CPUs the contribution that
  645. * each task makes to its run queue's load is weighted according to its
  646. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  647. * scaled version of the new time slice allocation that they receive on time
  648. * slice expiry etc.
  649. */
  650. #define WEIGHT_IDLEPRIO 3
  651. #define WMULT_IDLEPRIO 1431655765
  652. /*
  653. * Nice levels are multiplicative, with a gentle 10% change for every
  654. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  655. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  656. * that remained on nice 0.
  657. *
  658. * The "10% effect" is relative and cumulative: from _any_ nice level,
  659. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  660. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  661. * If a task goes up by ~10% and another task goes down by ~10% then
  662. * the relative distance between them is ~25%.)
  663. */
  664. static const int prio_to_weight[40] = {
  665. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  666. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  667. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  668. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  669. /* 0 */ 1024, 820, 655, 526, 423,
  670. /* 5 */ 335, 272, 215, 172, 137,
  671. /* 10 */ 110, 87, 70, 56, 45,
  672. /* 15 */ 36, 29, 23, 18, 15,
  673. };
  674. /*
  675. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  676. *
  677. * In cases where the weight does not change often, we can use the
  678. * precalculated inverse to speed up arithmetics by turning divisions
  679. * into multiplications:
  680. */
  681. static const u32 prio_to_wmult[40] = {
  682. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  683. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  684. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  685. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  686. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  687. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  688. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  689. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  690. };
  691. /* Time spent by the tasks of the cpu accounting group executing in ... */
  692. enum cpuacct_stat_index {
  693. CPUACCT_STAT_USER, /* ... user mode */
  694. CPUACCT_STAT_SYSTEM, /* ... kernel mode */
  695. CPUACCT_STAT_NSTATS,
  696. };
  697. #define sched_class_highest (&stop_sched_class)
  698. #define for_each_class(class) \
  699. for (class = sched_class_highest; class; class = class->next)
  700. extern const struct sched_class stop_sched_class;
  701. extern const struct sched_class rt_sched_class;
  702. extern const struct sched_class fair_sched_class;
  703. extern const struct sched_class idle_sched_class;
  704. #ifdef CONFIG_SMP
  705. extern void trigger_load_balance(struct rq *rq, int cpu);
  706. extern void idle_balance(int this_cpu, struct rq *this_rq);
  707. #else /* CONFIG_SMP */
  708. static inline void idle_balance(int cpu, struct rq *rq)
  709. {
  710. }
  711. #endif
  712. extern void sysrq_sched_debug_show(void);
  713. extern void sched_init_granularity(void);
  714. extern void update_max_interval(void);
  715. extern void update_group_power(struct sched_domain *sd, int cpu);
  716. extern int update_runtime(struct notifier_block *nfb, unsigned long action, void *hcpu);
  717. extern void init_sched_rt_class(void);
  718. extern void init_sched_fair_class(void);
  719. extern void resched_task(struct task_struct *p);
  720. extern void resched_cpu(int cpu);
  721. extern struct rt_bandwidth def_rt_bandwidth;
  722. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  723. extern void update_idle_cpu_load(struct rq *this_rq);
  724. #ifdef CONFIG_CGROUP_CPUACCT
  725. #include <linux/cgroup.h>
  726. /* track cpu usage of a group of tasks and its child groups */
  727. struct cpuacct {
  728. struct cgroup_subsys_state css;
  729. /* cpuusage holds pointer to a u64-type object on every cpu */
  730. u64 __percpu *cpuusage;
  731. struct kernel_cpustat __percpu *cpustat;
  732. };
  733. /* return cpu accounting group corresponding to this container */
  734. static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
  735. {
  736. return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
  737. struct cpuacct, css);
  738. }
  739. /* return cpu accounting group to which this task belongs */
  740. static inline struct cpuacct *task_ca(struct task_struct *tsk)
  741. {
  742. return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
  743. struct cpuacct, css);
  744. }
  745. static inline struct cpuacct *parent_ca(struct cpuacct *ca)
  746. {
  747. if (!ca || !ca->css.cgroup->parent)
  748. return NULL;
  749. return cgroup_ca(ca->css.cgroup->parent);
  750. }
  751. extern void cpuacct_charge(struct task_struct *tsk, u64 cputime);
  752. #else
  753. static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
  754. #endif
  755. static inline void inc_nr_running(struct rq *rq)
  756. {
  757. rq->nr_running++;
  758. }
  759. static inline void dec_nr_running(struct rq *rq)
  760. {
  761. rq->nr_running--;
  762. }
  763. extern void update_rq_clock(struct rq *rq);
  764. extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
  765. extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
  766. extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
  767. extern const_debug unsigned int sysctl_sched_time_avg;
  768. extern const_debug unsigned int sysctl_sched_nr_migrate;
  769. extern const_debug unsigned int sysctl_sched_migration_cost;
  770. static inline u64 sched_avg_period(void)
  771. {
  772. return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
  773. }
  774. void calc_load_account_idle(struct rq *this_rq);
  775. #ifdef CONFIG_SCHED_HRTICK
  776. /*
  777. * Use hrtick when:
  778. * - enabled by features
  779. * - hrtimer is actually high res
  780. */
  781. static inline int hrtick_enabled(struct rq *rq)
  782. {
  783. if (!sched_feat(HRTICK))
  784. return 0;
  785. if (!cpu_active(cpu_of(rq)))
  786. return 0;
  787. return hrtimer_is_hres_active(&rq->hrtick_timer);
  788. }
  789. void hrtick_start(struct rq *rq, u64 delay);
  790. #else
  791. static inline int hrtick_enabled(struct rq *rq)
  792. {
  793. return 0;
  794. }
  795. #endif /* CONFIG_SCHED_HRTICK */
  796. #ifdef CONFIG_SMP
  797. extern void sched_avg_update(struct rq *rq);
  798. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
  799. {
  800. rq->rt_avg += rt_delta;
  801. sched_avg_update(rq);
  802. }
  803. #else
  804. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
  805. static inline void sched_avg_update(struct rq *rq) { }
  806. #endif
  807. extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
  808. #ifdef CONFIG_SMP
  809. #ifdef CONFIG_PREEMPT
  810. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
  811. /*
  812. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  813. * way at the expense of forcing extra atomic operations in all
  814. * invocations. This assures that the double_lock is acquired using the
  815. * same underlying policy as the spinlock_t on this architecture, which
  816. * reduces latency compared to the unfair variant below. However, it
  817. * also adds more overhead and therefore may reduce throughput.
  818. */
  819. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  820. __releases(this_rq->lock)
  821. __acquires(busiest->lock)
  822. __acquires(this_rq->lock)
  823. {
  824. raw_spin_unlock(&this_rq->lock);
  825. double_rq_lock(this_rq, busiest);
  826. return 1;
  827. }
  828. #else
  829. /*
  830. * Unfair double_lock_balance: Optimizes throughput at the expense of
  831. * latency by eliminating extra atomic operations when the locks are
  832. * already in proper order on entry. This favors lower cpu-ids and will
  833. * grant the double lock to lower cpus over higher ids under contention,
  834. * regardless of entry order into the function.
  835. */
  836. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  837. __releases(this_rq->lock)
  838. __acquires(busiest->lock)
  839. __acquires(this_rq->lock)
  840. {
  841. int ret = 0;
  842. if (unlikely(!raw_spin_trylock(&busiest->lock))) {
  843. if (busiest < this_rq) {
  844. raw_spin_unlock(&this_rq->lock);
  845. raw_spin_lock(&busiest->lock);
  846. raw_spin_lock_nested(&this_rq->lock,
  847. SINGLE_DEPTH_NESTING);
  848. ret = 1;
  849. } else
  850. raw_spin_lock_nested(&busiest->lock,
  851. SINGLE_DEPTH_NESTING);
  852. }
  853. return ret;
  854. }
  855. #endif /* CONFIG_PREEMPT */
  856. /*
  857. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  858. */
  859. static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  860. {
  861. if (unlikely(!irqs_disabled())) {
  862. /* printk() doesn't work good under rq->lock */
  863. raw_spin_unlock(&this_rq->lock);
  864. BUG_ON(1);
  865. }
  866. return _double_lock_balance(this_rq, busiest);
  867. }
  868. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  869. __releases(busiest->lock)
  870. {
  871. raw_spin_unlock(&busiest->lock);
  872. lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
  873. }
  874. /*
  875. * double_rq_lock - safely lock two runqueues
  876. *
  877. * Note this does not disable interrupts like task_rq_lock,
  878. * you need to do so manually before calling.
  879. */
  880. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  881. __acquires(rq1->lock)
  882. __acquires(rq2->lock)
  883. {
  884. BUG_ON(!irqs_disabled());
  885. if (rq1 == rq2) {
  886. raw_spin_lock(&rq1->lock);
  887. __acquire(rq2->lock); /* Fake it out ;) */
  888. } else {
  889. if (rq1 < rq2) {
  890. raw_spin_lock(&rq1->lock);
  891. raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
  892. } else {
  893. raw_spin_lock(&rq2->lock);
  894. raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
  895. }
  896. }
  897. }
  898. /*
  899. * double_rq_unlock - safely unlock two runqueues
  900. *
  901. * Note this does not restore interrupts like task_rq_unlock,
  902. * you need to do so manually after calling.
  903. */
  904. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  905. __releases(rq1->lock)
  906. __releases(rq2->lock)
  907. {
  908. raw_spin_unlock(&rq1->lock);
  909. if (rq1 != rq2)
  910. raw_spin_unlock(&rq2->lock);
  911. else
  912. __release(rq2->lock);
  913. }
  914. #else /* CONFIG_SMP */
  915. /*
  916. * double_rq_lock - safely lock two runqueues
  917. *
  918. * Note this does not disable interrupts like task_rq_lock,
  919. * you need to do so manually before calling.
  920. */
  921. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  922. __acquires(rq1->lock)
  923. __acquires(rq2->lock)
  924. {
  925. BUG_ON(!irqs_disabled());
  926. BUG_ON(rq1 != rq2);
  927. raw_spin_lock(&rq1->lock);
  928. __acquire(rq2->lock); /* Fake it out ;) */
  929. }
  930. /*
  931. * double_rq_unlock - safely unlock two runqueues
  932. *
  933. * Note this does not restore interrupts like task_rq_unlock,
  934. * you need to do so manually after calling.
  935. */
  936. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  937. __releases(rq1->lock)
  938. __releases(rq2->lock)
  939. {
  940. BUG_ON(rq1 != rq2);
  941. raw_spin_unlock(&rq1->lock);
  942. __release(rq2->lock);
  943. }
  944. #endif
  945. extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
  946. extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
  947. extern void print_cfs_stats(struct seq_file *m, int cpu);
  948. extern void print_rt_stats(struct seq_file *m, int cpu);
  949. extern void init_cfs_rq(struct cfs_rq *cfs_rq);
  950. extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
  951. extern void unthrottle_offline_cfs_rqs(struct rq *rq);
  952. extern void account_cfs_bandwidth_used(int enabled, int was_enabled);
  953. #ifdef CONFIG_NO_HZ
  954. enum rq_nohz_flag_bits {
  955. NOHZ_TICK_STOPPED,
  956. NOHZ_BALANCE_KICK,
  957. NOHZ_IDLE,
  958. };
  959. #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
  960. #endif