sched.h 35 KB

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