sched.h 40 KB

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