sched.h 40 KB

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