sched.h 36 KB

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