sched.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. #include <linux/sched.h>
  2. #include <linux/sched/sysctl.h>
  3. #include <linux/sched/rt.h>
  4. #include <linux/sched/deadline.h>
  5. #include <linux/mutex.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/stop_machine.h>
  8. #include <linux/tick.h>
  9. #include <linux/slab.h>
  10. #include "cpupri.h"
  11. #include "cpudeadline.h"
  12. #include "cpuacct.h"
  13. struct rq;
  14. extern __read_mostly int scheduler_running;
  15. extern unsigned long calc_load_update;
  16. extern atomic_long_t calc_load_tasks;
  17. extern long calc_load_fold_active(struct rq *this_rq);
  18. extern void update_cpu_load_active(struct rq *this_rq);
  19. /*
  20. * Helpers for converting nanosecond timing to jiffy resolution
  21. */
  22. #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
  23. /*
  24. * Increase resolution of nice-level calculations for 64-bit architectures.
  25. * The extra resolution improves shares distribution and load balancing of
  26. * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
  27. * hierarchies, especially on larger systems. This is not a user-visible change
  28. * and does not change the user-interface for setting shares/weights.
  29. *
  30. * We increase resolution only if we have enough bits to allow this increased
  31. * resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution
  32. * when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the
  33. * increased costs.
  34. */
  35. #if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */
  36. # define SCHED_LOAD_RESOLUTION 10
  37. # define scale_load(w) ((w) << SCHED_LOAD_RESOLUTION)
  38. # define scale_load_down(w) ((w) >> SCHED_LOAD_RESOLUTION)
  39. #else
  40. # define SCHED_LOAD_RESOLUTION 0
  41. # define scale_load(w) (w)
  42. # define scale_load_down(w) (w)
  43. #endif
  44. #define SCHED_LOAD_SHIFT (10 + SCHED_LOAD_RESOLUTION)
  45. #define SCHED_LOAD_SCALE (1L << SCHED_LOAD_SHIFT)
  46. #define NICE_0_LOAD SCHED_LOAD_SCALE
  47. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  48. /*
  49. * Single value that decides SCHED_DEADLINE internal math precision.
  50. * 10 -> just above 1us
  51. * 9 -> just above 0.5us
  52. */
  53. #define DL_SCALE (10)
  54. /*
  55. * These are the 'tuning knobs' of the scheduler:
  56. */
  57. /*
  58. * single value that denotes runtime == period, ie unlimited time.
  59. */
  60. #define RUNTIME_INF ((u64)~0ULL)
  61. static inline int fair_policy(int policy)
  62. {
  63. return policy == SCHED_NORMAL || policy == SCHED_BATCH;
  64. }
  65. static inline int rt_policy(int policy)
  66. {
  67. return policy == SCHED_FIFO || policy == SCHED_RR;
  68. }
  69. static inline int dl_policy(int policy)
  70. {
  71. return policy == SCHED_DEADLINE;
  72. }
  73. static inline int task_has_rt_policy(struct task_struct *p)
  74. {
  75. return rt_policy(p->policy);
  76. }
  77. static inline int task_has_dl_policy(struct task_struct *p)
  78. {
  79. return dl_policy(p->policy);
  80. }
  81. static inline bool dl_time_before(u64 a, u64 b)
  82. {
  83. return (s64)(a - b) < 0;
  84. }
  85. /*
  86. * Tells if entity @a should preempt entity @b.
  87. */
  88. static inline bool
  89. dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
  90. {
  91. return dl_time_before(a->deadline, b->deadline);
  92. }
  93. /*
  94. * This is the priority-queue data structure of the RT scheduling class:
  95. */
  96. struct rt_prio_array {
  97. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  98. struct list_head queue[MAX_RT_PRIO];
  99. };
  100. struct rt_bandwidth {
  101. /* nests inside the rq lock: */
  102. raw_spinlock_t rt_runtime_lock;
  103. ktime_t rt_period;
  104. u64 rt_runtime;
  105. struct hrtimer rt_period_timer;
  106. };
  107. /*
  108. * To keep the bandwidth of -deadline tasks and groups under control
  109. * we need some place where:
  110. * - store the maximum -deadline bandwidth of the system (the group);
  111. * - cache the fraction of that bandwidth that is currently allocated.
  112. *
  113. * This is all done in the data structure below. It is similar to the
  114. * one used for RT-throttling (rt_bandwidth), with the main difference
  115. * that, since here we are only interested in admission control, we
  116. * do not decrease any runtime while the group "executes", neither we
  117. * need a timer to replenish it.
  118. *
  119. * With respect to SMP, the bandwidth is given on a per-CPU basis,
  120. * meaning that:
  121. * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
  122. * - dl_total_bw array contains, in the i-eth element, the currently
  123. * allocated bandwidth on the i-eth CPU.
  124. * Moreover, groups consume bandwidth on each CPU, while tasks only
  125. * consume bandwidth on the CPU they're running on.
  126. * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
  127. * that will be shown the next time the proc or cgroup controls will
  128. * be red. It on its turn can be changed by writing on its own
  129. * control.
  130. */
  131. struct dl_bandwidth {
  132. raw_spinlock_t dl_runtime_lock;
  133. u64 dl_runtime;
  134. u64 dl_period;
  135. };
  136. static inline int dl_bandwidth_enabled(void)
  137. {
  138. return sysctl_sched_rt_runtime >= 0;
  139. }
  140. extern struct dl_bw *dl_bw_of(int i);
  141. struct dl_bw {
  142. raw_spinlock_t lock;
  143. u64 bw, total_bw;
  144. };
  145. extern struct mutex sched_domains_mutex;
  146. #ifdef CONFIG_CGROUP_SCHED
  147. #include <linux/cgroup.h>
  148. struct cfs_rq;
  149. struct rt_rq;
  150. extern struct list_head task_groups;
  151. struct cfs_bandwidth {
  152. #ifdef CONFIG_CFS_BANDWIDTH
  153. raw_spinlock_t lock;
  154. ktime_t period;
  155. u64 quota, runtime;
  156. s64 hierarchal_quota;
  157. u64 runtime_expires;
  158. int idle, timer_active;
  159. struct hrtimer period_timer, slack_timer;
  160. struct list_head throttled_cfs_rq;
  161. /* statistics */
  162. int nr_periods, nr_throttled;
  163. u64 throttled_time;
  164. #endif
  165. };
  166. /* task group related information */
  167. struct task_group {
  168. struct cgroup_subsys_state css;
  169. #ifdef CONFIG_FAIR_GROUP_SCHED
  170. /* schedulable entities of this group on each cpu */
  171. struct sched_entity **se;
  172. /* runqueue "owned" by this group on each cpu */
  173. struct cfs_rq **cfs_rq;
  174. unsigned long shares;
  175. #ifdef CONFIG_SMP
  176. atomic_long_t load_avg;
  177. atomic_t runnable_avg;
  178. #endif
  179. #endif
  180. #ifdef CONFIG_RT_GROUP_SCHED
  181. struct sched_rt_entity **rt_se;
  182. struct rt_rq **rt_rq;
  183. struct rt_bandwidth rt_bandwidth;
  184. #endif
  185. struct rcu_head rcu;
  186. struct list_head list;
  187. struct task_group *parent;
  188. struct list_head siblings;
  189. struct list_head children;
  190. #ifdef CONFIG_SCHED_AUTOGROUP
  191. struct autogroup *autogroup;
  192. #endif
  193. struct cfs_bandwidth cfs_bandwidth;
  194. };
  195. #ifdef CONFIG_FAIR_GROUP_SCHED
  196. #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
  197. /*
  198. * A weight of 0 or 1 can cause arithmetics problems.
  199. * A weight of a cfs_rq is the sum of weights of which entities
  200. * are queued on this cfs_rq, so a weight of a entity should not be
  201. * too large, so as the shares value of a task group.
  202. * (The default weight is 1024 - so there's no practical
  203. * limitation from this.)
  204. */
  205. #define MIN_SHARES (1UL << 1)
  206. #define MAX_SHARES (1UL << 18)
  207. #endif
  208. typedef int (*tg_visitor)(struct task_group *, void *);
  209. extern int walk_tg_tree_from(struct task_group *from,
  210. tg_visitor down, tg_visitor up, void *data);
  211. /*
  212. * Iterate the full tree, calling @down when first entering a node and @up when
  213. * leaving it for the final time.
  214. *
  215. * Caller must hold rcu_lock or sufficient equivalent.
  216. */
  217. static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
  218. {
  219. return walk_tg_tree_from(&root_task_group, down, up, data);
  220. }
  221. extern int tg_nop(struct task_group *tg, void *data);
  222. extern void free_fair_sched_group(struct task_group *tg);
  223. extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
  224. extern void unregister_fair_sched_group(struct task_group *tg, int cpu);
  225. extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  226. struct sched_entity *se, int cpu,
  227. struct sched_entity *parent);
  228. extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
  229. extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
  230. extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
  231. extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b, bool force);
  232. extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
  233. extern void free_rt_sched_group(struct task_group *tg);
  234. extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
  235. extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  236. struct sched_rt_entity *rt_se, int cpu,
  237. struct sched_rt_entity *parent);
  238. extern struct task_group *sched_create_group(struct task_group *parent);
  239. extern void sched_online_group(struct task_group *tg,
  240. struct task_group *parent);
  241. extern void sched_destroy_group(struct task_group *tg);
  242. extern void sched_offline_group(struct task_group *tg);
  243. extern void sched_move_task(struct task_struct *tsk);
  244. #ifdef CONFIG_FAIR_GROUP_SCHED
  245. extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
  246. #endif
  247. #else /* CONFIG_CGROUP_SCHED */
  248. struct cfs_bandwidth { };
  249. #endif /* CONFIG_CGROUP_SCHED */
  250. /* CFS-related fields in a runqueue */
  251. struct cfs_rq {
  252. struct load_weight load;
  253. unsigned int nr_running, h_nr_running;
  254. u64 exec_clock;
  255. u64 min_vruntime;
  256. #ifndef CONFIG_64BIT
  257. u64 min_vruntime_copy;
  258. #endif
  259. struct rb_root tasks_timeline;
  260. struct rb_node *rb_leftmost;
  261. /*
  262. * 'curr' points to currently running entity on this cfs_rq.
  263. * It is set to NULL otherwise (i.e when none are currently running).
  264. */
  265. struct sched_entity *curr, *next, *last, *skip;
  266. #ifdef CONFIG_SCHED_DEBUG
  267. unsigned int nr_spread_over;
  268. #endif
  269. #ifdef CONFIG_SMP
  270. /*
  271. * CFS Load tracking
  272. * Under CFS, load is tracked on a per-entity basis and aggregated up.
  273. * This allows for the description of both thread and group usage (in
  274. * the FAIR_GROUP_SCHED case).
  275. */
  276. unsigned long runnable_load_avg, blocked_load_avg;
  277. atomic64_t decay_counter;
  278. u64 last_decay;
  279. atomic_long_t removed_load;
  280. #ifdef CONFIG_FAIR_GROUP_SCHED
  281. /* Required to track per-cpu representation of a task_group */
  282. u32 tg_runnable_contrib;
  283. unsigned long tg_load_contrib;
  284. /*
  285. * h_load = weight * f(tg)
  286. *
  287. * Where f(tg) is the recursive weight fraction assigned to
  288. * this group.
  289. */
  290. unsigned long h_load;
  291. u64 last_h_load_update;
  292. struct sched_entity *h_load_next;
  293. #endif /* CONFIG_FAIR_GROUP_SCHED */
  294. #endif /* CONFIG_SMP */
  295. #ifdef CONFIG_FAIR_GROUP_SCHED
  296. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  297. /*
  298. * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  299. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  300. * (like users, containers etc.)
  301. *
  302. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  303. * list is used during load balance.
  304. */
  305. int on_list;
  306. struct list_head leaf_cfs_rq_list;
  307. struct task_group *tg; /* group that "owns" this runqueue */
  308. #ifdef CONFIG_CFS_BANDWIDTH
  309. int runtime_enabled;
  310. u64 runtime_expires;
  311. s64 runtime_remaining;
  312. u64 throttled_clock, throttled_clock_task;
  313. u64 throttled_clock_task_time;
  314. int throttled, throttle_count;
  315. struct list_head throttled_list;
  316. #endif /* CONFIG_CFS_BANDWIDTH */
  317. #endif /* CONFIG_FAIR_GROUP_SCHED */
  318. };
  319. static inline int rt_bandwidth_enabled(void)
  320. {
  321. return sysctl_sched_rt_runtime >= 0;
  322. }
  323. /* Real-Time classes' related field in a runqueue: */
  324. struct rt_rq {
  325. struct rt_prio_array active;
  326. unsigned int rt_nr_running;
  327. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  328. struct {
  329. int curr; /* highest queued rt task prio */
  330. #ifdef CONFIG_SMP
  331. int next; /* next highest */
  332. #endif
  333. } highest_prio;
  334. #endif
  335. #ifdef CONFIG_SMP
  336. unsigned long rt_nr_migratory;
  337. unsigned long rt_nr_total;
  338. int overloaded;
  339. struct plist_head pushable_tasks;
  340. #endif
  341. int rt_queued;
  342. int rt_throttled;
  343. u64 rt_time;
  344. u64 rt_runtime;
  345. /* Nests inside the rq lock: */
  346. raw_spinlock_t rt_runtime_lock;
  347. #ifdef CONFIG_RT_GROUP_SCHED
  348. unsigned long rt_nr_boosted;
  349. struct rq *rq;
  350. struct task_group *tg;
  351. #endif
  352. };
  353. /* Deadline class' related fields in a runqueue */
  354. struct dl_rq {
  355. /* runqueue is an rbtree, ordered by deadline */
  356. struct rb_root rb_root;
  357. struct rb_node *rb_leftmost;
  358. unsigned long dl_nr_running;
  359. #ifdef CONFIG_SMP
  360. /*
  361. * Deadline values of the currently executing and the
  362. * earliest ready task on this rq. Caching these facilitates
  363. * the decision wether or not a ready but not running task
  364. * should migrate somewhere else.
  365. */
  366. struct {
  367. u64 curr;
  368. u64 next;
  369. } earliest_dl;
  370. unsigned long dl_nr_migratory;
  371. int overloaded;
  372. /*
  373. * Tasks on this rq that can be pushed away. They are kept in
  374. * an rb-tree, ordered by tasks' deadlines, with caching
  375. * of the leftmost (earliest deadline) element.
  376. */
  377. struct rb_root pushable_dl_tasks_root;
  378. struct rb_node *pushable_dl_tasks_leftmost;
  379. #else
  380. struct dl_bw dl_bw;
  381. #endif
  382. };
  383. #ifdef CONFIG_SMP
  384. /*
  385. * We add the notion of a root-domain which will be used to define per-domain
  386. * variables. Each exclusive cpuset essentially defines an island domain by
  387. * fully partitioning the member cpus from any other cpuset. Whenever a new
  388. * exclusive cpuset is created, we also create and attach a new root-domain
  389. * object.
  390. *
  391. */
  392. struct root_domain {
  393. atomic_t refcount;
  394. atomic_t rto_count;
  395. struct rcu_head rcu;
  396. cpumask_var_t span;
  397. cpumask_var_t online;
  398. /* Indicate more than one runnable task for any CPU */
  399. bool overload;
  400. /*
  401. * The bit corresponding to a CPU gets set here if such CPU has more
  402. * than one runnable -deadline task (as it is below for RT tasks).
  403. */
  404. cpumask_var_t dlo_mask;
  405. atomic_t dlo_count;
  406. struct dl_bw dl_bw;
  407. struct cpudl cpudl;
  408. /*
  409. * The "RT overload" flag: it gets set if a CPU has more than
  410. * one runnable RT task.
  411. */
  412. cpumask_var_t rto_mask;
  413. struct cpupri cpupri;
  414. };
  415. extern struct root_domain def_root_domain;
  416. #endif /* CONFIG_SMP */
  417. /*
  418. * This is the main, per-CPU runqueue data structure.
  419. *
  420. * Locking rule: those places that want to lock multiple runqueues
  421. * (such as the load balancing or the thread migration code), lock
  422. * acquire operations must be ordered by ascending &runqueue.
  423. */
  424. struct rq {
  425. /* runqueue lock: */
  426. raw_spinlock_t lock;
  427. /*
  428. * nr_running and cpu_load should be in the same cacheline because
  429. * remote CPUs use both these fields when doing load calculation.
  430. */
  431. unsigned int nr_running;
  432. #ifdef CONFIG_NUMA_BALANCING
  433. unsigned int nr_numa_running;
  434. unsigned int nr_preferred_running;
  435. #endif
  436. #define CPU_LOAD_IDX_MAX 5
  437. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  438. unsigned long last_load_update_tick;
  439. #ifdef CONFIG_NO_HZ_COMMON
  440. u64 nohz_stamp;
  441. unsigned long nohz_flags;
  442. #endif
  443. #ifdef CONFIG_NO_HZ_FULL
  444. unsigned long last_sched_tick;
  445. #endif
  446. int skip_clock_update;
  447. /* capture load from *all* tasks on this cpu: */
  448. struct load_weight load;
  449. unsigned long nr_load_updates;
  450. u64 nr_switches;
  451. struct cfs_rq cfs;
  452. struct rt_rq rt;
  453. struct dl_rq dl;
  454. #ifdef CONFIG_FAIR_GROUP_SCHED
  455. /* list of leaf cfs_rq on this cpu: */
  456. struct list_head leaf_cfs_rq_list;
  457. struct sched_avg avg;
  458. #endif /* CONFIG_FAIR_GROUP_SCHED */
  459. /*
  460. * This is part of a global counter where only the total sum
  461. * over all CPUs matters. A task can increase this counter on
  462. * one CPU and if it got migrated afterwards it may decrease
  463. * it on another CPU. Always updated under the runqueue lock:
  464. */
  465. unsigned long nr_uninterruptible;
  466. struct task_struct *curr, *idle, *stop;
  467. unsigned long next_balance;
  468. struct mm_struct *prev_mm;
  469. u64 clock;
  470. u64 clock_task;
  471. atomic_t nr_iowait;
  472. #ifdef CONFIG_SMP
  473. struct root_domain *rd;
  474. struct sched_domain *sd;
  475. unsigned long cpu_capacity;
  476. unsigned char idle_balance;
  477. /* For active balancing */
  478. int post_schedule;
  479. int active_balance;
  480. int push_cpu;
  481. struct cpu_stop_work active_balance_work;
  482. /* cpu of this runqueue: */
  483. int cpu;
  484. int online;
  485. struct list_head cfs_tasks;
  486. u64 rt_avg;
  487. u64 age_stamp;
  488. u64 idle_stamp;
  489. u64 avg_idle;
  490. /* This is used to determine avg_idle's max value */
  491. u64 max_idle_balance_cost;
  492. #endif
  493. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  494. u64 prev_irq_time;
  495. #endif
  496. #ifdef CONFIG_PARAVIRT
  497. u64 prev_steal_time;
  498. #endif
  499. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  500. u64 prev_steal_time_rq;
  501. #endif
  502. /* calc_load related fields */
  503. unsigned long calc_load_update;
  504. long calc_load_active;
  505. #ifdef CONFIG_SCHED_HRTICK
  506. #ifdef CONFIG_SMP
  507. int hrtick_csd_pending;
  508. struct call_single_data hrtick_csd;
  509. #endif
  510. struct hrtimer hrtick_timer;
  511. #endif
  512. #ifdef CONFIG_SCHEDSTATS
  513. /* latency stats */
  514. struct sched_info rq_sched_info;
  515. unsigned long long rq_cpu_time;
  516. /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
  517. /* sys_sched_yield() stats */
  518. unsigned int yld_count;
  519. /* schedule() stats */
  520. unsigned int sched_count;
  521. unsigned int sched_goidle;
  522. /* try_to_wake_up() stats */
  523. unsigned int ttwu_count;
  524. unsigned int ttwu_local;
  525. #endif
  526. #ifdef CONFIG_SMP
  527. struct llist_head wake_list;
  528. #endif
  529. };
  530. static inline int cpu_of(struct rq *rq)
  531. {
  532. #ifdef CONFIG_SMP
  533. return rq->cpu;
  534. #else
  535. return 0;
  536. #endif
  537. }
  538. DECLARE_PER_CPU(struct rq, runqueues);
  539. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  540. #define this_rq() (&__get_cpu_var(runqueues))
  541. #define task_rq(p) cpu_rq(task_cpu(p))
  542. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  543. #define raw_rq() (&__raw_get_cpu_var(runqueues))
  544. static inline u64 rq_clock(struct rq *rq)
  545. {
  546. return rq->clock;
  547. }
  548. static inline u64 rq_clock_task(struct rq *rq)
  549. {
  550. return rq->clock_task;
  551. }
  552. #ifdef CONFIG_NUMA_BALANCING
  553. extern void sched_setnuma(struct task_struct *p, int node);
  554. extern int migrate_task_to(struct task_struct *p, int cpu);
  555. extern int migrate_swap(struct task_struct *, struct task_struct *);
  556. #endif /* CONFIG_NUMA_BALANCING */
  557. #ifdef CONFIG_SMP
  558. extern void sched_ttwu_pending(void);
  559. #define rcu_dereference_check_sched_domain(p) \
  560. rcu_dereference_check((p), \
  561. lockdep_is_held(&sched_domains_mutex))
  562. /*
  563. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  564. * See detach_destroy_domains: synchronize_sched for details.
  565. *
  566. * The domain tree of any CPU may only be accessed from within
  567. * preempt-disabled sections.
  568. */
  569. #define for_each_domain(cpu, __sd) \
  570. for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
  571. __sd; __sd = __sd->parent)
  572. #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
  573. /**
  574. * highest_flag_domain - Return highest sched_domain containing flag.
  575. * @cpu: The cpu whose highest level of sched domain is to
  576. * be returned.
  577. * @flag: The flag to check for the highest sched_domain
  578. * for the given cpu.
  579. *
  580. * Returns the highest sched_domain of a cpu which contains the given flag.
  581. */
  582. static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
  583. {
  584. struct sched_domain *sd, *hsd = NULL;
  585. for_each_domain(cpu, sd) {
  586. if (!(sd->flags & flag))
  587. break;
  588. hsd = sd;
  589. }
  590. return hsd;
  591. }
  592. static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
  593. {
  594. struct sched_domain *sd;
  595. for_each_domain(cpu, sd) {
  596. if (sd->flags & flag)
  597. break;
  598. }
  599. return sd;
  600. }
  601. DECLARE_PER_CPU(struct sched_domain *, sd_llc);
  602. DECLARE_PER_CPU(int, sd_llc_size);
  603. DECLARE_PER_CPU(int, sd_llc_id);
  604. DECLARE_PER_CPU(struct sched_domain *, sd_numa);
  605. DECLARE_PER_CPU(struct sched_domain *, sd_busy);
  606. DECLARE_PER_CPU(struct sched_domain *, sd_asym);
  607. struct sched_group_capacity {
  608. atomic_t ref;
  609. /*
  610. * CPU capacity of this group, SCHED_LOAD_SCALE being max capacity
  611. * for a single CPU.
  612. */
  613. unsigned int capacity, capacity_orig;
  614. unsigned long next_update;
  615. int imbalance; /* XXX unrelated to capacity but shared group state */
  616. /*
  617. * Number of busy cpus in this group.
  618. */
  619. atomic_t nr_busy_cpus;
  620. unsigned long cpumask[0]; /* iteration mask */
  621. };
  622. struct sched_group {
  623. struct sched_group *next; /* Must be a circular list */
  624. atomic_t ref;
  625. unsigned int group_weight;
  626. struct sched_group_capacity *sgc;
  627. /*
  628. * The CPUs this group covers.
  629. *
  630. * NOTE: this field is variable length. (Allocated dynamically
  631. * by attaching extra space to the end of the structure,
  632. * depending on how many CPUs the kernel has booted up with)
  633. */
  634. unsigned long cpumask[0];
  635. };
  636. static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
  637. {
  638. return to_cpumask(sg->cpumask);
  639. }
  640. /*
  641. * cpumask masking which cpus in the group are allowed to iterate up the domain
  642. * tree.
  643. */
  644. static inline struct cpumask *sched_group_mask(struct sched_group *sg)
  645. {
  646. return to_cpumask(sg->sgc->cpumask);
  647. }
  648. /**
  649. * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
  650. * @group: The group whose first cpu is to be returned.
  651. */
  652. static inline unsigned int group_first_cpu(struct sched_group *group)
  653. {
  654. return cpumask_first(sched_group_cpus(group));
  655. }
  656. extern int group_balance_cpu(struct sched_group *sg);
  657. #else
  658. static inline void sched_ttwu_pending(void) { }
  659. #endif /* CONFIG_SMP */
  660. #include "stats.h"
  661. #include "auto_group.h"
  662. #ifdef CONFIG_CGROUP_SCHED
  663. /*
  664. * Return the group to which this tasks belongs.
  665. *
  666. * We cannot use task_css() and friends because the cgroup subsystem
  667. * changes that value before the cgroup_subsys::attach() method is called,
  668. * therefore we cannot pin it and might observe the wrong value.
  669. *
  670. * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
  671. * core changes this before calling sched_move_task().
  672. *
  673. * Instead we use a 'copy' which is updated from sched_move_task() while
  674. * holding both task_struct::pi_lock and rq::lock.
  675. */
  676. static inline struct task_group *task_group(struct task_struct *p)
  677. {
  678. return p->sched_task_group;
  679. }
  680. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  681. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  682. {
  683. #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
  684. struct task_group *tg = task_group(p);
  685. #endif
  686. #ifdef CONFIG_FAIR_GROUP_SCHED
  687. p->se.cfs_rq = tg->cfs_rq[cpu];
  688. p->se.parent = tg->se[cpu];
  689. #endif
  690. #ifdef CONFIG_RT_GROUP_SCHED
  691. p->rt.rt_rq = tg->rt_rq[cpu];
  692. p->rt.parent = tg->rt_se[cpu];
  693. #endif
  694. }
  695. #else /* CONFIG_CGROUP_SCHED */
  696. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  697. static inline struct task_group *task_group(struct task_struct *p)
  698. {
  699. return NULL;
  700. }
  701. #endif /* CONFIG_CGROUP_SCHED */
  702. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  703. {
  704. set_task_rq(p, cpu);
  705. #ifdef CONFIG_SMP
  706. /*
  707. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  708. * successfuly executed on another CPU. We must ensure that updates of
  709. * per-task data have been completed by this moment.
  710. */
  711. smp_wmb();
  712. task_thread_info(p)->cpu = cpu;
  713. p->wake_cpu = cpu;
  714. #endif
  715. }
  716. /*
  717. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  718. */
  719. #ifdef CONFIG_SCHED_DEBUG
  720. # include <linux/static_key.h>
  721. # define const_debug __read_mostly
  722. #else
  723. # define const_debug const
  724. #endif
  725. extern const_debug unsigned int sysctl_sched_features;
  726. #define SCHED_FEAT(name, enabled) \
  727. __SCHED_FEAT_##name ,
  728. enum {
  729. #include "features.h"
  730. __SCHED_FEAT_NR,
  731. };
  732. #undef SCHED_FEAT
  733. #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
  734. #define SCHED_FEAT(name, enabled) \
  735. static __always_inline bool static_branch_##name(struct static_key *key) \
  736. { \
  737. return static_key_##enabled(key); \
  738. }
  739. #include "features.h"
  740. #undef SCHED_FEAT
  741. extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
  742. #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
  743. #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
  744. #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  745. #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
  746. #ifdef CONFIG_NUMA_BALANCING
  747. #define sched_feat_numa(x) sched_feat(x)
  748. #ifdef CONFIG_SCHED_DEBUG
  749. #define numabalancing_enabled sched_feat_numa(NUMA)
  750. #else
  751. extern bool numabalancing_enabled;
  752. #endif /* CONFIG_SCHED_DEBUG */
  753. #else
  754. #define sched_feat_numa(x) (0)
  755. #define numabalancing_enabled (0)
  756. #endif /* CONFIG_NUMA_BALANCING */
  757. static inline u64 global_rt_period(void)
  758. {
  759. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  760. }
  761. static inline u64 global_rt_runtime(void)
  762. {
  763. if (sysctl_sched_rt_runtime < 0)
  764. return RUNTIME_INF;
  765. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  766. }
  767. static inline int task_current(struct rq *rq, struct task_struct *p)
  768. {
  769. return rq->curr == p;
  770. }
  771. static inline int task_running(struct rq *rq, struct task_struct *p)
  772. {
  773. #ifdef CONFIG_SMP
  774. return p->on_cpu;
  775. #else
  776. return task_current(rq, p);
  777. #endif
  778. }
  779. #ifndef prepare_arch_switch
  780. # define prepare_arch_switch(next) do { } while (0)
  781. #endif
  782. #ifndef finish_arch_switch
  783. # define finish_arch_switch(prev) do { } while (0)
  784. #endif
  785. #ifndef finish_arch_post_lock_switch
  786. # define finish_arch_post_lock_switch() do { } while (0)
  787. #endif
  788. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  789. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  790. {
  791. #ifdef CONFIG_SMP
  792. /*
  793. * We can optimise this out completely for !SMP, because the
  794. * SMP rebalancing from interrupt is the only thing that cares
  795. * here.
  796. */
  797. next->on_cpu = 1;
  798. #endif
  799. }
  800. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  801. {
  802. #ifdef CONFIG_SMP
  803. /*
  804. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  805. * We must ensure this doesn't happen until the switch is completely
  806. * finished.
  807. */
  808. smp_wmb();
  809. prev->on_cpu = 0;
  810. #endif
  811. #ifdef CONFIG_DEBUG_SPINLOCK
  812. /* this is a valid case when another task releases the spinlock */
  813. rq->lock.owner = current;
  814. #endif
  815. /*
  816. * If we are tracking spinlock dependencies then we have to
  817. * fix up the runqueue lock - which gets 'carried over' from
  818. * prev into current:
  819. */
  820. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  821. raw_spin_unlock_irq(&rq->lock);
  822. }
  823. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  824. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  825. {
  826. #ifdef CONFIG_SMP
  827. /*
  828. * We can optimise this out completely for !SMP, because the
  829. * SMP rebalancing from interrupt is the only thing that cares
  830. * here.
  831. */
  832. next->on_cpu = 1;
  833. #endif
  834. raw_spin_unlock(&rq->lock);
  835. }
  836. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  837. {
  838. #ifdef CONFIG_SMP
  839. /*
  840. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  841. * We must ensure this doesn't happen until the switch is completely
  842. * finished.
  843. */
  844. smp_wmb();
  845. prev->on_cpu = 0;
  846. #endif
  847. local_irq_enable();
  848. }
  849. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  850. /*
  851. * wake flags
  852. */
  853. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  854. #define WF_FORK 0x02 /* child wakeup after fork */
  855. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  856. /*
  857. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  858. * of tasks with abnormal "nice" values across CPUs the contribution that
  859. * each task makes to its run queue's load is weighted according to its
  860. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  861. * scaled version of the new time slice allocation that they receive on time
  862. * slice expiry etc.
  863. */
  864. #define WEIGHT_IDLEPRIO 3
  865. #define WMULT_IDLEPRIO 1431655765
  866. /*
  867. * Nice levels are multiplicative, with a gentle 10% change for every
  868. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  869. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  870. * that remained on nice 0.
  871. *
  872. * The "10% effect" is relative and cumulative: from _any_ nice level,
  873. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  874. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  875. * If a task goes up by ~10% and another task goes down by ~10% then
  876. * the relative distance between them is ~25%.)
  877. */
  878. static const int prio_to_weight[40] = {
  879. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  880. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  881. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  882. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  883. /* 0 */ 1024, 820, 655, 526, 423,
  884. /* 5 */ 335, 272, 215, 172, 137,
  885. /* 10 */ 110, 87, 70, 56, 45,
  886. /* 15 */ 36, 29, 23, 18, 15,
  887. };
  888. /*
  889. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  890. *
  891. * In cases where the weight does not change often, we can use the
  892. * precalculated inverse to speed up arithmetics by turning divisions
  893. * into multiplications:
  894. */
  895. static const u32 prio_to_wmult[40] = {
  896. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  897. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  898. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  899. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  900. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  901. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  902. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  903. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  904. };
  905. #define ENQUEUE_WAKEUP 1
  906. #define ENQUEUE_HEAD 2
  907. #ifdef CONFIG_SMP
  908. #define ENQUEUE_WAKING 4 /* sched_class::task_waking was called */
  909. #else
  910. #define ENQUEUE_WAKING 0
  911. #endif
  912. #define ENQUEUE_REPLENISH 8
  913. #define DEQUEUE_SLEEP 1
  914. #define RETRY_TASK ((void *)-1UL)
  915. struct sched_class {
  916. const struct sched_class *next;
  917. void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
  918. void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
  919. void (*yield_task) (struct rq *rq);
  920. bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
  921. void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
  922. /*
  923. * It is the responsibility of the pick_next_task() method that will
  924. * return the next task to call put_prev_task() on the @prev task or
  925. * something equivalent.
  926. *
  927. * May return RETRY_TASK when it finds a higher prio class has runnable
  928. * tasks.
  929. */
  930. struct task_struct * (*pick_next_task) (struct rq *rq,
  931. struct task_struct *prev);
  932. void (*put_prev_task) (struct rq *rq, struct task_struct *p);
  933. #ifdef CONFIG_SMP
  934. int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
  935. void (*migrate_task_rq)(struct task_struct *p, int next_cpu);
  936. void (*post_schedule) (struct rq *this_rq);
  937. void (*task_waking) (struct task_struct *task);
  938. void (*task_woken) (struct rq *this_rq, struct task_struct *task);
  939. void (*set_cpus_allowed)(struct task_struct *p,
  940. const struct cpumask *newmask);
  941. void (*rq_online)(struct rq *rq);
  942. void (*rq_offline)(struct rq *rq);
  943. #endif
  944. void (*set_curr_task) (struct rq *rq);
  945. void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
  946. void (*task_fork) (struct task_struct *p);
  947. void (*task_dead) (struct task_struct *p);
  948. void (*switched_from) (struct rq *this_rq, struct task_struct *task);
  949. void (*switched_to) (struct rq *this_rq, struct task_struct *task);
  950. void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
  951. int oldprio);
  952. unsigned int (*get_rr_interval) (struct rq *rq,
  953. struct task_struct *task);
  954. #ifdef CONFIG_FAIR_GROUP_SCHED
  955. void (*task_move_group) (struct task_struct *p, int on_rq);
  956. #endif
  957. };
  958. static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
  959. {
  960. prev->sched_class->put_prev_task(rq, prev);
  961. }
  962. #define sched_class_highest (&stop_sched_class)
  963. #define for_each_class(class) \
  964. for (class = sched_class_highest; class; class = class->next)
  965. extern const struct sched_class stop_sched_class;
  966. extern const struct sched_class dl_sched_class;
  967. extern const struct sched_class rt_sched_class;
  968. extern const struct sched_class fair_sched_class;
  969. extern const struct sched_class idle_sched_class;
  970. #ifdef CONFIG_SMP
  971. extern void update_group_capacity(struct sched_domain *sd, int cpu);
  972. extern void trigger_load_balance(struct rq *rq);
  973. extern void idle_enter_fair(struct rq *this_rq);
  974. extern void idle_exit_fair(struct rq *this_rq);
  975. #else
  976. static inline void idle_enter_fair(struct rq *rq) { }
  977. static inline void idle_exit_fair(struct rq *rq) { }
  978. #endif
  979. extern void sysrq_sched_debug_show(void);
  980. extern void sched_init_granularity(void);
  981. extern void update_max_interval(void);
  982. extern void init_sched_dl_class(void);
  983. extern void init_sched_rt_class(void);
  984. extern void init_sched_fair_class(void);
  985. extern void init_sched_dl_class(void);
  986. extern void resched_curr(struct rq *rq);
  987. extern void resched_cpu(int cpu);
  988. extern struct rt_bandwidth def_rt_bandwidth;
  989. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  990. extern struct dl_bandwidth def_dl_bandwidth;
  991. extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
  992. extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
  993. unsigned long to_ratio(u64 period, u64 runtime);
  994. extern void update_idle_cpu_load(struct rq *this_rq);
  995. extern void init_task_runnable_average(struct task_struct *p);
  996. static inline void add_nr_running(struct rq *rq, unsigned count)
  997. {
  998. unsigned prev_nr = rq->nr_running;
  999. rq->nr_running = prev_nr + count;
  1000. if (prev_nr < 2 && rq->nr_running >= 2) {
  1001. #ifdef CONFIG_SMP
  1002. if (!rq->rd->overload)
  1003. rq->rd->overload = true;
  1004. #endif
  1005. #ifdef CONFIG_NO_HZ_FULL
  1006. if (tick_nohz_full_cpu(rq->cpu)) {
  1007. /*
  1008. * Tick is needed if more than one task runs on a CPU.
  1009. * Send the target an IPI to kick it out of nohz mode.
  1010. *
  1011. * We assume that IPI implies full memory barrier and the
  1012. * new value of rq->nr_running is visible on reception
  1013. * from the target.
  1014. */
  1015. tick_nohz_full_kick_cpu(rq->cpu);
  1016. }
  1017. #endif
  1018. }
  1019. }
  1020. static inline void sub_nr_running(struct rq *rq, unsigned count)
  1021. {
  1022. rq->nr_running -= count;
  1023. }
  1024. static inline void rq_last_tick_reset(struct rq *rq)
  1025. {
  1026. #ifdef CONFIG_NO_HZ_FULL
  1027. rq->last_sched_tick = jiffies;
  1028. #endif
  1029. }
  1030. extern void update_rq_clock(struct rq *rq);
  1031. extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
  1032. extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
  1033. extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
  1034. extern const_debug unsigned int sysctl_sched_time_avg;
  1035. extern const_debug unsigned int sysctl_sched_nr_migrate;
  1036. extern const_debug unsigned int sysctl_sched_migration_cost;
  1037. static inline u64 sched_avg_period(void)
  1038. {
  1039. return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
  1040. }
  1041. #ifdef CONFIG_SCHED_HRTICK
  1042. /*
  1043. * Use hrtick when:
  1044. * - enabled by features
  1045. * - hrtimer is actually high res
  1046. */
  1047. static inline int hrtick_enabled(struct rq *rq)
  1048. {
  1049. if (!sched_feat(HRTICK))
  1050. return 0;
  1051. if (!cpu_active(cpu_of(rq)))
  1052. return 0;
  1053. return hrtimer_is_hres_active(&rq->hrtick_timer);
  1054. }
  1055. void hrtick_start(struct rq *rq, u64 delay);
  1056. #else
  1057. static inline int hrtick_enabled(struct rq *rq)
  1058. {
  1059. return 0;
  1060. }
  1061. #endif /* CONFIG_SCHED_HRTICK */
  1062. #ifdef CONFIG_SMP
  1063. extern void sched_avg_update(struct rq *rq);
  1064. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
  1065. {
  1066. rq->rt_avg += rt_delta;
  1067. sched_avg_update(rq);
  1068. }
  1069. #else
  1070. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
  1071. static inline void sched_avg_update(struct rq *rq) { }
  1072. #endif
  1073. extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
  1074. #ifdef CONFIG_SMP
  1075. #ifdef CONFIG_PREEMPT
  1076. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
  1077. /*
  1078. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  1079. * way at the expense of forcing extra atomic operations in all
  1080. * invocations. This assures that the double_lock is acquired using the
  1081. * same underlying policy as the spinlock_t on this architecture, which
  1082. * reduces latency compared to the unfair variant below. However, it
  1083. * also adds more overhead and therefore may reduce throughput.
  1084. */
  1085. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1086. __releases(this_rq->lock)
  1087. __acquires(busiest->lock)
  1088. __acquires(this_rq->lock)
  1089. {
  1090. raw_spin_unlock(&this_rq->lock);
  1091. double_rq_lock(this_rq, busiest);
  1092. return 1;
  1093. }
  1094. #else
  1095. /*
  1096. * Unfair double_lock_balance: Optimizes throughput at the expense of
  1097. * latency by eliminating extra atomic operations when the locks are
  1098. * already in proper order on entry. This favors lower cpu-ids and will
  1099. * grant the double lock to lower cpus over higher ids under contention,
  1100. * regardless of entry order into the function.
  1101. */
  1102. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1103. __releases(this_rq->lock)
  1104. __acquires(busiest->lock)
  1105. __acquires(this_rq->lock)
  1106. {
  1107. int ret = 0;
  1108. if (unlikely(!raw_spin_trylock(&busiest->lock))) {
  1109. if (busiest < this_rq) {
  1110. raw_spin_unlock(&this_rq->lock);
  1111. raw_spin_lock(&busiest->lock);
  1112. raw_spin_lock_nested(&this_rq->lock,
  1113. SINGLE_DEPTH_NESTING);
  1114. ret = 1;
  1115. } else
  1116. raw_spin_lock_nested(&busiest->lock,
  1117. SINGLE_DEPTH_NESTING);
  1118. }
  1119. return ret;
  1120. }
  1121. #endif /* CONFIG_PREEMPT */
  1122. /*
  1123. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1124. */
  1125. static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1126. {
  1127. if (unlikely(!irqs_disabled())) {
  1128. /* printk() doesn't work good under rq->lock */
  1129. raw_spin_unlock(&this_rq->lock);
  1130. BUG_ON(1);
  1131. }
  1132. return _double_lock_balance(this_rq, busiest);
  1133. }
  1134. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  1135. __releases(busiest->lock)
  1136. {
  1137. raw_spin_unlock(&busiest->lock);
  1138. lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
  1139. }
  1140. static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
  1141. {
  1142. if (l1 > l2)
  1143. swap(l1, l2);
  1144. spin_lock(l1);
  1145. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1146. }
  1147. static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
  1148. {
  1149. if (l1 > l2)
  1150. swap(l1, l2);
  1151. spin_lock_irq(l1);
  1152. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1153. }
  1154. static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
  1155. {
  1156. if (l1 > l2)
  1157. swap(l1, l2);
  1158. raw_spin_lock(l1);
  1159. raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1160. }
  1161. /*
  1162. * double_rq_lock - safely lock two runqueues
  1163. *
  1164. * Note this does not disable interrupts like task_rq_lock,
  1165. * you need to do so manually before calling.
  1166. */
  1167. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1168. __acquires(rq1->lock)
  1169. __acquires(rq2->lock)
  1170. {
  1171. BUG_ON(!irqs_disabled());
  1172. if (rq1 == rq2) {
  1173. raw_spin_lock(&rq1->lock);
  1174. __acquire(rq2->lock); /* Fake it out ;) */
  1175. } else {
  1176. if (rq1 < rq2) {
  1177. raw_spin_lock(&rq1->lock);
  1178. raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
  1179. } else {
  1180. raw_spin_lock(&rq2->lock);
  1181. raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
  1182. }
  1183. }
  1184. }
  1185. /*
  1186. * double_rq_unlock - safely unlock two runqueues
  1187. *
  1188. * Note this does not restore interrupts like task_rq_unlock,
  1189. * you need to do so manually after calling.
  1190. */
  1191. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1192. __releases(rq1->lock)
  1193. __releases(rq2->lock)
  1194. {
  1195. raw_spin_unlock(&rq1->lock);
  1196. if (rq1 != rq2)
  1197. raw_spin_unlock(&rq2->lock);
  1198. else
  1199. __release(rq2->lock);
  1200. }
  1201. #else /* CONFIG_SMP */
  1202. /*
  1203. * double_rq_lock - safely lock two runqueues
  1204. *
  1205. * Note this does not disable interrupts like task_rq_lock,
  1206. * you need to do so manually before calling.
  1207. */
  1208. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1209. __acquires(rq1->lock)
  1210. __acquires(rq2->lock)
  1211. {
  1212. BUG_ON(!irqs_disabled());
  1213. BUG_ON(rq1 != rq2);
  1214. raw_spin_lock(&rq1->lock);
  1215. __acquire(rq2->lock); /* Fake it out ;) */
  1216. }
  1217. /*
  1218. * double_rq_unlock - safely unlock two runqueues
  1219. *
  1220. * Note this does not restore interrupts like task_rq_unlock,
  1221. * you need to do so manually after calling.
  1222. */
  1223. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1224. __releases(rq1->lock)
  1225. __releases(rq2->lock)
  1226. {
  1227. BUG_ON(rq1 != rq2);
  1228. raw_spin_unlock(&rq1->lock);
  1229. __release(rq2->lock);
  1230. }
  1231. #endif
  1232. extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
  1233. extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
  1234. extern void print_cfs_stats(struct seq_file *m, int cpu);
  1235. extern void print_rt_stats(struct seq_file *m, int cpu);
  1236. extern void init_cfs_rq(struct cfs_rq *cfs_rq);
  1237. extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
  1238. extern void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq);
  1239. extern void cfs_bandwidth_usage_inc(void);
  1240. extern void cfs_bandwidth_usage_dec(void);
  1241. #ifdef CONFIG_NO_HZ_COMMON
  1242. enum rq_nohz_flag_bits {
  1243. NOHZ_TICK_STOPPED,
  1244. NOHZ_BALANCE_KICK,
  1245. };
  1246. #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
  1247. #endif
  1248. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  1249. DECLARE_PER_CPU(u64, cpu_hardirq_time);
  1250. DECLARE_PER_CPU(u64, cpu_softirq_time);
  1251. #ifndef CONFIG_64BIT
  1252. DECLARE_PER_CPU(seqcount_t, irq_time_seq);
  1253. static inline void irq_time_write_begin(void)
  1254. {
  1255. __this_cpu_inc(irq_time_seq.sequence);
  1256. smp_wmb();
  1257. }
  1258. static inline void irq_time_write_end(void)
  1259. {
  1260. smp_wmb();
  1261. __this_cpu_inc(irq_time_seq.sequence);
  1262. }
  1263. static inline u64 irq_time_read(int cpu)
  1264. {
  1265. u64 irq_time;
  1266. unsigned seq;
  1267. do {
  1268. seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
  1269. irq_time = per_cpu(cpu_softirq_time, cpu) +
  1270. per_cpu(cpu_hardirq_time, cpu);
  1271. } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
  1272. return irq_time;
  1273. }
  1274. #else /* CONFIG_64BIT */
  1275. static inline void irq_time_write_begin(void)
  1276. {
  1277. }
  1278. static inline void irq_time_write_end(void)
  1279. {
  1280. }
  1281. static inline u64 irq_time_read(int cpu)
  1282. {
  1283. return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
  1284. }
  1285. #endif /* CONFIG_64BIT */
  1286. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */