sched.h 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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. #ifdef CONFIG_NUMA_BALANCING
  340. unsigned int nr_numa_running;
  341. unsigned int nr_preferred_running;
  342. #endif
  343. #define CPU_LOAD_IDX_MAX 5
  344. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  345. unsigned long last_load_update_tick;
  346. #ifdef CONFIG_NO_HZ_COMMON
  347. u64 nohz_stamp;
  348. unsigned long nohz_flags;
  349. #endif
  350. #ifdef CONFIG_NO_HZ_FULL
  351. unsigned long last_sched_tick;
  352. #endif
  353. int skip_clock_update;
  354. /* capture load from *all* tasks on this cpu: */
  355. struct load_weight load;
  356. unsigned long nr_load_updates;
  357. u64 nr_switches;
  358. struct cfs_rq cfs;
  359. struct rt_rq rt;
  360. #ifdef CONFIG_FAIR_GROUP_SCHED
  361. /* list of leaf cfs_rq on this cpu: */
  362. struct list_head leaf_cfs_rq_list;
  363. #endif /* CONFIG_FAIR_GROUP_SCHED */
  364. #ifdef CONFIG_RT_GROUP_SCHED
  365. struct list_head leaf_rt_rq_list;
  366. #endif
  367. /*
  368. * This is part of a global counter where only the total sum
  369. * over all CPUs matters. A task can increase this counter on
  370. * one CPU and if it got migrated afterwards it may decrease
  371. * it on another CPU. Always updated under the runqueue lock:
  372. */
  373. unsigned long nr_uninterruptible;
  374. struct task_struct *curr, *idle, *stop;
  375. unsigned long next_balance;
  376. struct mm_struct *prev_mm;
  377. u64 clock;
  378. u64 clock_task;
  379. atomic_t nr_iowait;
  380. #ifdef CONFIG_SMP
  381. struct root_domain *rd;
  382. struct sched_domain *sd;
  383. unsigned long cpu_power;
  384. unsigned char idle_balance;
  385. /* For active balancing */
  386. int post_schedule;
  387. int active_balance;
  388. int push_cpu;
  389. struct cpu_stop_work active_balance_work;
  390. /* cpu of this runqueue: */
  391. int cpu;
  392. int online;
  393. struct list_head cfs_tasks;
  394. u64 rt_avg;
  395. u64 age_stamp;
  396. u64 idle_stamp;
  397. u64 avg_idle;
  398. /* This is used to determine avg_idle's max value */
  399. u64 max_idle_balance_cost;
  400. #endif
  401. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  402. u64 prev_irq_time;
  403. #endif
  404. #ifdef CONFIG_PARAVIRT
  405. u64 prev_steal_time;
  406. #endif
  407. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  408. u64 prev_steal_time_rq;
  409. #endif
  410. /* calc_load related fields */
  411. unsigned long calc_load_update;
  412. long calc_load_active;
  413. #ifdef CONFIG_SCHED_HRTICK
  414. #ifdef CONFIG_SMP
  415. int hrtick_csd_pending;
  416. struct call_single_data hrtick_csd;
  417. #endif
  418. struct hrtimer hrtick_timer;
  419. #endif
  420. #ifdef CONFIG_SCHEDSTATS
  421. /* latency stats */
  422. struct sched_info rq_sched_info;
  423. unsigned long long rq_cpu_time;
  424. /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
  425. /* sys_sched_yield() stats */
  426. unsigned int yld_count;
  427. /* schedule() stats */
  428. unsigned int sched_count;
  429. unsigned int sched_goidle;
  430. /* try_to_wake_up() stats */
  431. unsigned int ttwu_count;
  432. unsigned int ttwu_local;
  433. #endif
  434. #ifdef CONFIG_SMP
  435. struct llist_head wake_list;
  436. #endif
  437. struct sched_avg avg;
  438. };
  439. static inline int cpu_of(struct rq *rq)
  440. {
  441. #ifdef CONFIG_SMP
  442. return rq->cpu;
  443. #else
  444. return 0;
  445. #endif
  446. }
  447. DECLARE_PER_CPU(struct rq, runqueues);
  448. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  449. #define this_rq() (&__get_cpu_var(runqueues))
  450. #define task_rq(p) cpu_rq(task_cpu(p))
  451. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  452. #define raw_rq() (&__raw_get_cpu_var(runqueues))
  453. static inline u64 rq_clock(struct rq *rq)
  454. {
  455. return rq->clock;
  456. }
  457. static inline u64 rq_clock_task(struct rq *rq)
  458. {
  459. return rq->clock_task;
  460. }
  461. #ifdef CONFIG_NUMA_BALANCING
  462. extern void sched_setnuma(struct task_struct *p, int node);
  463. extern int migrate_task_to(struct task_struct *p, int cpu);
  464. extern int migrate_swap(struct task_struct *, struct task_struct *);
  465. #endif /* CONFIG_NUMA_BALANCING */
  466. #ifdef CONFIG_SMP
  467. #define rcu_dereference_check_sched_domain(p) \
  468. rcu_dereference_check((p), \
  469. lockdep_is_held(&sched_domains_mutex))
  470. /*
  471. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  472. * See detach_destroy_domains: synchronize_sched for details.
  473. *
  474. * The domain tree of any CPU may only be accessed from within
  475. * preempt-disabled sections.
  476. */
  477. #define for_each_domain(cpu, __sd) \
  478. for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
  479. __sd; __sd = __sd->parent)
  480. #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
  481. /**
  482. * highest_flag_domain - Return highest sched_domain containing flag.
  483. * @cpu: The cpu whose highest level of sched domain is to
  484. * be returned.
  485. * @flag: The flag to check for the highest sched_domain
  486. * for the given cpu.
  487. *
  488. * Returns the highest sched_domain of a cpu which contains the given flag.
  489. */
  490. static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
  491. {
  492. struct sched_domain *sd, *hsd = NULL;
  493. for_each_domain(cpu, sd) {
  494. if (!(sd->flags & flag))
  495. break;
  496. hsd = sd;
  497. }
  498. return hsd;
  499. }
  500. static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
  501. {
  502. struct sched_domain *sd;
  503. for_each_domain(cpu, sd) {
  504. if (sd->flags & flag)
  505. break;
  506. }
  507. return sd;
  508. }
  509. DECLARE_PER_CPU(struct sched_domain *, sd_llc);
  510. DECLARE_PER_CPU(int, sd_llc_size);
  511. DECLARE_PER_CPU(int, sd_llc_id);
  512. DECLARE_PER_CPU(struct sched_domain *, sd_numa);
  513. struct sched_group_power {
  514. atomic_t ref;
  515. /*
  516. * CPU power of this group, SCHED_LOAD_SCALE being max power for a
  517. * single CPU.
  518. */
  519. unsigned int power, power_orig;
  520. unsigned long next_update;
  521. int imbalance; /* XXX unrelated to power but shared group state */
  522. /*
  523. * Number of busy cpus in this group.
  524. */
  525. atomic_t nr_busy_cpus;
  526. unsigned long cpumask[0]; /* iteration mask */
  527. };
  528. struct sched_group {
  529. struct sched_group *next; /* Must be a circular list */
  530. atomic_t ref;
  531. unsigned int group_weight;
  532. struct sched_group_power *sgp;
  533. /*
  534. * The CPUs this group covers.
  535. *
  536. * NOTE: this field is variable length. (Allocated dynamically
  537. * by attaching extra space to the end of the structure,
  538. * depending on how many CPUs the kernel has booted up with)
  539. */
  540. unsigned long cpumask[0];
  541. };
  542. static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
  543. {
  544. return to_cpumask(sg->cpumask);
  545. }
  546. /*
  547. * cpumask masking which cpus in the group are allowed to iterate up the domain
  548. * tree.
  549. */
  550. static inline struct cpumask *sched_group_mask(struct sched_group *sg)
  551. {
  552. return to_cpumask(sg->sgp->cpumask);
  553. }
  554. /**
  555. * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
  556. * @group: The group whose first cpu is to be returned.
  557. */
  558. static inline unsigned int group_first_cpu(struct sched_group *group)
  559. {
  560. return cpumask_first(sched_group_cpus(group));
  561. }
  562. extern int group_balance_cpu(struct sched_group *sg);
  563. #endif /* CONFIG_SMP */
  564. #include "stats.h"
  565. #include "auto_group.h"
  566. #ifdef CONFIG_CGROUP_SCHED
  567. /*
  568. * Return the group to which this tasks belongs.
  569. *
  570. * We cannot use task_css() and friends because the cgroup subsystem
  571. * changes that value before the cgroup_subsys::attach() method is called,
  572. * therefore we cannot pin it and might observe the wrong value.
  573. *
  574. * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
  575. * core changes this before calling sched_move_task().
  576. *
  577. * Instead we use a 'copy' which is updated from sched_move_task() while
  578. * holding both task_struct::pi_lock and rq::lock.
  579. */
  580. static inline struct task_group *task_group(struct task_struct *p)
  581. {
  582. return p->sched_task_group;
  583. }
  584. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  585. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  586. {
  587. #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
  588. struct task_group *tg = task_group(p);
  589. #endif
  590. #ifdef CONFIG_FAIR_GROUP_SCHED
  591. p->se.cfs_rq = tg->cfs_rq[cpu];
  592. p->se.parent = tg->se[cpu];
  593. #endif
  594. #ifdef CONFIG_RT_GROUP_SCHED
  595. p->rt.rt_rq = tg->rt_rq[cpu];
  596. p->rt.parent = tg->rt_se[cpu];
  597. #endif
  598. }
  599. #else /* CONFIG_CGROUP_SCHED */
  600. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  601. static inline struct task_group *task_group(struct task_struct *p)
  602. {
  603. return NULL;
  604. }
  605. #endif /* CONFIG_CGROUP_SCHED */
  606. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  607. {
  608. set_task_rq(p, cpu);
  609. #ifdef CONFIG_SMP
  610. /*
  611. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  612. * successfuly executed on another CPU. We must ensure that updates of
  613. * per-task data have been completed by this moment.
  614. */
  615. smp_wmb();
  616. task_thread_info(p)->cpu = cpu;
  617. p->wake_cpu = cpu;
  618. #endif
  619. }
  620. /*
  621. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  622. */
  623. #ifdef CONFIG_SCHED_DEBUG
  624. # include <linux/static_key.h>
  625. # define const_debug __read_mostly
  626. #else
  627. # define const_debug const
  628. #endif
  629. extern const_debug unsigned int sysctl_sched_features;
  630. #define SCHED_FEAT(name, enabled) \
  631. __SCHED_FEAT_##name ,
  632. enum {
  633. #include "features.h"
  634. __SCHED_FEAT_NR,
  635. };
  636. #undef SCHED_FEAT
  637. #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
  638. static __always_inline bool static_branch__true(struct static_key *key)
  639. {
  640. return static_key_true(key); /* Not out of line branch. */
  641. }
  642. static __always_inline bool static_branch__false(struct static_key *key)
  643. {
  644. return static_key_false(key); /* Out of line branch. */
  645. }
  646. #define SCHED_FEAT(name, enabled) \
  647. static __always_inline bool static_branch_##name(struct static_key *key) \
  648. { \
  649. return static_branch__##enabled(key); \
  650. }
  651. #include "features.h"
  652. #undef SCHED_FEAT
  653. extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
  654. #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
  655. #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
  656. #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  657. #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
  658. #ifdef CONFIG_NUMA_BALANCING
  659. #define sched_feat_numa(x) sched_feat(x)
  660. #ifdef CONFIG_SCHED_DEBUG
  661. #define numabalancing_enabled sched_feat_numa(NUMA)
  662. #else
  663. extern bool numabalancing_enabled;
  664. #endif /* CONFIG_SCHED_DEBUG */
  665. #else
  666. #define sched_feat_numa(x) (0)
  667. #define numabalancing_enabled (0)
  668. #endif /* CONFIG_NUMA_BALANCING */
  669. static inline u64 global_rt_period(void)
  670. {
  671. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  672. }
  673. static inline u64 global_rt_runtime(void)
  674. {
  675. if (sysctl_sched_rt_runtime < 0)
  676. return RUNTIME_INF;
  677. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  678. }
  679. static inline int task_current(struct rq *rq, struct task_struct *p)
  680. {
  681. return rq->curr == p;
  682. }
  683. static inline int task_running(struct rq *rq, struct task_struct *p)
  684. {
  685. #ifdef CONFIG_SMP
  686. return p->on_cpu;
  687. #else
  688. return task_current(rq, p);
  689. #endif
  690. }
  691. #ifndef prepare_arch_switch
  692. # define prepare_arch_switch(next) do { } while (0)
  693. #endif
  694. #ifndef finish_arch_switch
  695. # define finish_arch_switch(prev) do { } while (0)
  696. #endif
  697. #ifndef finish_arch_post_lock_switch
  698. # define finish_arch_post_lock_switch() do { } while (0)
  699. #endif
  700. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  701. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  702. {
  703. #ifdef CONFIG_SMP
  704. /*
  705. * We can optimise this out completely for !SMP, because the
  706. * SMP rebalancing from interrupt is the only thing that cares
  707. * here.
  708. */
  709. next->on_cpu = 1;
  710. #endif
  711. }
  712. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  713. {
  714. #ifdef CONFIG_SMP
  715. /*
  716. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  717. * We must ensure this doesn't happen until the switch is completely
  718. * finished.
  719. */
  720. smp_wmb();
  721. prev->on_cpu = 0;
  722. #endif
  723. #ifdef CONFIG_DEBUG_SPINLOCK
  724. /* this is a valid case when another task releases the spinlock */
  725. rq->lock.owner = current;
  726. #endif
  727. /*
  728. * If we are tracking spinlock dependencies then we have to
  729. * fix up the runqueue lock - which gets 'carried over' from
  730. * prev into current:
  731. */
  732. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  733. raw_spin_unlock_irq(&rq->lock);
  734. }
  735. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  736. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  737. {
  738. #ifdef CONFIG_SMP
  739. /*
  740. * We can optimise this out completely for !SMP, because the
  741. * SMP rebalancing from interrupt is the only thing that cares
  742. * here.
  743. */
  744. next->on_cpu = 1;
  745. #endif
  746. raw_spin_unlock(&rq->lock);
  747. }
  748. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  749. {
  750. #ifdef CONFIG_SMP
  751. /*
  752. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  753. * We must ensure this doesn't happen until the switch is completely
  754. * finished.
  755. */
  756. smp_wmb();
  757. prev->on_cpu = 0;
  758. #endif
  759. local_irq_enable();
  760. }
  761. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  762. /*
  763. * wake flags
  764. */
  765. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  766. #define WF_FORK 0x02 /* child wakeup after fork */
  767. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  768. /*
  769. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  770. * of tasks with abnormal "nice" values across CPUs the contribution that
  771. * each task makes to its run queue's load is weighted according to its
  772. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  773. * scaled version of the new time slice allocation that they receive on time
  774. * slice expiry etc.
  775. */
  776. #define WEIGHT_IDLEPRIO 3
  777. #define WMULT_IDLEPRIO 1431655765
  778. /*
  779. * Nice levels are multiplicative, with a gentle 10% change for every
  780. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  781. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  782. * that remained on nice 0.
  783. *
  784. * The "10% effect" is relative and cumulative: from _any_ nice level,
  785. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  786. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  787. * If a task goes up by ~10% and another task goes down by ~10% then
  788. * the relative distance between them is ~25%.)
  789. */
  790. static const int prio_to_weight[40] = {
  791. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  792. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  793. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  794. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  795. /* 0 */ 1024, 820, 655, 526, 423,
  796. /* 5 */ 335, 272, 215, 172, 137,
  797. /* 10 */ 110, 87, 70, 56, 45,
  798. /* 15 */ 36, 29, 23, 18, 15,
  799. };
  800. /*
  801. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  802. *
  803. * In cases where the weight does not change often, we can use the
  804. * precalculated inverse to speed up arithmetics by turning divisions
  805. * into multiplications:
  806. */
  807. static const u32 prio_to_wmult[40] = {
  808. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  809. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  810. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  811. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  812. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  813. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  814. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  815. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  816. };
  817. #define ENQUEUE_WAKEUP 1
  818. #define ENQUEUE_HEAD 2
  819. #ifdef CONFIG_SMP
  820. #define ENQUEUE_WAKING 4 /* sched_class::task_waking was called */
  821. #else
  822. #define ENQUEUE_WAKING 0
  823. #endif
  824. #define DEQUEUE_SLEEP 1
  825. struct sched_class {
  826. const struct sched_class *next;
  827. void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
  828. void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
  829. void (*yield_task) (struct rq *rq);
  830. bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
  831. void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
  832. struct task_struct * (*pick_next_task) (struct rq *rq);
  833. void (*put_prev_task) (struct rq *rq, struct task_struct *p);
  834. #ifdef CONFIG_SMP
  835. int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
  836. void (*migrate_task_rq)(struct task_struct *p, int next_cpu);
  837. void (*pre_schedule) (struct rq *this_rq, struct task_struct *task);
  838. void (*post_schedule) (struct rq *this_rq);
  839. void (*task_waking) (struct task_struct *task);
  840. void (*task_woken) (struct rq *this_rq, struct task_struct *task);
  841. void (*set_cpus_allowed)(struct task_struct *p,
  842. const struct cpumask *newmask);
  843. void (*rq_online)(struct rq *rq);
  844. void (*rq_offline)(struct rq *rq);
  845. #endif
  846. void (*set_curr_task) (struct rq *rq);
  847. void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
  848. void (*task_fork) (struct task_struct *p);
  849. void (*switched_from) (struct rq *this_rq, struct task_struct *task);
  850. void (*switched_to) (struct rq *this_rq, struct task_struct *task);
  851. void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
  852. int oldprio);
  853. unsigned int (*get_rr_interval) (struct rq *rq,
  854. struct task_struct *task);
  855. #ifdef CONFIG_FAIR_GROUP_SCHED
  856. void (*task_move_group) (struct task_struct *p, int on_rq);
  857. #endif
  858. };
  859. #define sched_class_highest (&stop_sched_class)
  860. #define for_each_class(class) \
  861. for (class = sched_class_highest; class; class = class->next)
  862. extern const struct sched_class stop_sched_class;
  863. extern const struct sched_class rt_sched_class;
  864. extern const struct sched_class fair_sched_class;
  865. extern const struct sched_class idle_sched_class;
  866. #ifdef CONFIG_SMP
  867. extern void update_group_power(struct sched_domain *sd, int cpu);
  868. extern void trigger_load_balance(struct rq *rq, int cpu);
  869. extern void idle_balance(int this_cpu, struct rq *this_rq);
  870. extern void idle_enter_fair(struct rq *this_rq);
  871. extern void idle_exit_fair(struct rq *this_rq);
  872. #else /* CONFIG_SMP */
  873. static inline void idle_balance(int cpu, struct rq *rq)
  874. {
  875. }
  876. #endif
  877. extern void sysrq_sched_debug_show(void);
  878. extern void sched_init_granularity(void);
  879. extern void update_max_interval(void);
  880. extern void init_sched_rt_class(void);
  881. extern void init_sched_fair_class(void);
  882. extern void resched_task(struct task_struct *p);
  883. extern void resched_cpu(int cpu);
  884. extern struct rt_bandwidth def_rt_bandwidth;
  885. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  886. extern void update_idle_cpu_load(struct rq *this_rq);
  887. extern void init_task_runnable_average(struct task_struct *p);
  888. #ifdef CONFIG_PARAVIRT
  889. static inline u64 steal_ticks(u64 steal)
  890. {
  891. if (unlikely(steal > NSEC_PER_SEC))
  892. return div_u64(steal, TICK_NSEC);
  893. return __iter_div_u64_rem(steal, TICK_NSEC, &steal);
  894. }
  895. #endif
  896. static inline void inc_nr_running(struct rq *rq)
  897. {
  898. rq->nr_running++;
  899. #ifdef CONFIG_NO_HZ_FULL
  900. if (rq->nr_running == 2) {
  901. if (tick_nohz_full_cpu(rq->cpu)) {
  902. /* Order rq->nr_running write against the IPI */
  903. smp_wmb();
  904. smp_send_reschedule(rq->cpu);
  905. }
  906. }
  907. #endif
  908. }
  909. static inline void dec_nr_running(struct rq *rq)
  910. {
  911. rq->nr_running--;
  912. }
  913. static inline void rq_last_tick_reset(struct rq *rq)
  914. {
  915. #ifdef CONFIG_NO_HZ_FULL
  916. rq->last_sched_tick = jiffies;
  917. #endif
  918. }
  919. extern void update_rq_clock(struct rq *rq);
  920. extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
  921. extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
  922. extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
  923. extern const_debug unsigned int sysctl_sched_time_avg;
  924. extern const_debug unsigned int sysctl_sched_nr_migrate;
  925. extern const_debug unsigned int sysctl_sched_migration_cost;
  926. static inline u64 sched_avg_period(void)
  927. {
  928. return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
  929. }
  930. #ifdef CONFIG_SCHED_HRTICK
  931. /*
  932. * Use hrtick when:
  933. * - enabled by features
  934. * - hrtimer is actually high res
  935. */
  936. static inline int hrtick_enabled(struct rq *rq)
  937. {
  938. if (!sched_feat(HRTICK))
  939. return 0;
  940. if (!cpu_active(cpu_of(rq)))
  941. return 0;
  942. return hrtimer_is_hres_active(&rq->hrtick_timer);
  943. }
  944. void hrtick_start(struct rq *rq, u64 delay);
  945. #else
  946. static inline int hrtick_enabled(struct rq *rq)
  947. {
  948. return 0;
  949. }
  950. #endif /* CONFIG_SCHED_HRTICK */
  951. #ifdef CONFIG_SMP
  952. extern void sched_avg_update(struct rq *rq);
  953. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
  954. {
  955. rq->rt_avg += rt_delta;
  956. sched_avg_update(rq);
  957. }
  958. #else
  959. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
  960. static inline void sched_avg_update(struct rq *rq) { }
  961. #endif
  962. extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
  963. #ifdef CONFIG_SMP
  964. #ifdef CONFIG_PREEMPT
  965. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
  966. /*
  967. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  968. * way at the expense of forcing extra atomic operations in all
  969. * invocations. This assures that the double_lock is acquired using the
  970. * same underlying policy as the spinlock_t on this architecture, which
  971. * reduces latency compared to the unfair variant below. However, it
  972. * also adds more overhead and therefore may reduce throughput.
  973. */
  974. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  975. __releases(this_rq->lock)
  976. __acquires(busiest->lock)
  977. __acquires(this_rq->lock)
  978. {
  979. raw_spin_unlock(&this_rq->lock);
  980. double_rq_lock(this_rq, busiest);
  981. return 1;
  982. }
  983. #else
  984. /*
  985. * Unfair double_lock_balance: Optimizes throughput at the expense of
  986. * latency by eliminating extra atomic operations when the locks are
  987. * already in proper order on entry. This favors lower cpu-ids and will
  988. * grant the double lock to lower cpus over higher ids under contention,
  989. * regardless of entry order into the function.
  990. */
  991. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  992. __releases(this_rq->lock)
  993. __acquires(busiest->lock)
  994. __acquires(this_rq->lock)
  995. {
  996. int ret = 0;
  997. if (unlikely(!raw_spin_trylock(&busiest->lock))) {
  998. if (busiest < this_rq) {
  999. raw_spin_unlock(&this_rq->lock);
  1000. raw_spin_lock(&busiest->lock);
  1001. raw_spin_lock_nested(&this_rq->lock,
  1002. SINGLE_DEPTH_NESTING);
  1003. ret = 1;
  1004. } else
  1005. raw_spin_lock_nested(&busiest->lock,
  1006. SINGLE_DEPTH_NESTING);
  1007. }
  1008. return ret;
  1009. }
  1010. #endif /* CONFIG_PREEMPT */
  1011. /*
  1012. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1013. */
  1014. static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1015. {
  1016. if (unlikely(!irqs_disabled())) {
  1017. /* printk() doesn't work good under rq->lock */
  1018. raw_spin_unlock(&this_rq->lock);
  1019. BUG_ON(1);
  1020. }
  1021. return _double_lock_balance(this_rq, busiest);
  1022. }
  1023. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  1024. __releases(busiest->lock)
  1025. {
  1026. raw_spin_unlock(&busiest->lock);
  1027. lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
  1028. }
  1029. static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
  1030. {
  1031. if (l1 > l2)
  1032. swap(l1, l2);
  1033. spin_lock(l1);
  1034. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1035. }
  1036. static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
  1037. {
  1038. if (l1 > l2)
  1039. swap(l1, l2);
  1040. raw_spin_lock(l1);
  1041. raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1042. }
  1043. /*
  1044. * double_rq_lock - safely lock two runqueues
  1045. *
  1046. * Note this does not disable interrupts like task_rq_lock,
  1047. * you need to do so manually before calling.
  1048. */
  1049. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1050. __acquires(rq1->lock)
  1051. __acquires(rq2->lock)
  1052. {
  1053. BUG_ON(!irqs_disabled());
  1054. if (rq1 == rq2) {
  1055. raw_spin_lock(&rq1->lock);
  1056. __acquire(rq2->lock); /* Fake it out ;) */
  1057. } else {
  1058. if (rq1 < rq2) {
  1059. raw_spin_lock(&rq1->lock);
  1060. raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
  1061. } else {
  1062. raw_spin_lock(&rq2->lock);
  1063. raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
  1064. }
  1065. }
  1066. }
  1067. /*
  1068. * double_rq_unlock - safely unlock two runqueues
  1069. *
  1070. * Note this does not restore interrupts like task_rq_unlock,
  1071. * you need to do so manually after calling.
  1072. */
  1073. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1074. __releases(rq1->lock)
  1075. __releases(rq2->lock)
  1076. {
  1077. raw_spin_unlock(&rq1->lock);
  1078. if (rq1 != rq2)
  1079. raw_spin_unlock(&rq2->lock);
  1080. else
  1081. __release(rq2->lock);
  1082. }
  1083. #else /* CONFIG_SMP */
  1084. /*
  1085. * double_rq_lock - safely lock two runqueues
  1086. *
  1087. * Note this does not disable interrupts like task_rq_lock,
  1088. * you need to do so manually before calling.
  1089. */
  1090. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1091. __acquires(rq1->lock)
  1092. __acquires(rq2->lock)
  1093. {
  1094. BUG_ON(!irqs_disabled());
  1095. BUG_ON(rq1 != rq2);
  1096. raw_spin_lock(&rq1->lock);
  1097. __acquire(rq2->lock); /* Fake it out ;) */
  1098. }
  1099. /*
  1100. * double_rq_unlock - safely unlock two runqueues
  1101. *
  1102. * Note this does not restore interrupts like task_rq_unlock,
  1103. * you need to do so manually after calling.
  1104. */
  1105. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1106. __releases(rq1->lock)
  1107. __releases(rq2->lock)
  1108. {
  1109. BUG_ON(rq1 != rq2);
  1110. raw_spin_unlock(&rq1->lock);
  1111. __release(rq2->lock);
  1112. }
  1113. #endif
  1114. extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
  1115. extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
  1116. extern void print_cfs_stats(struct seq_file *m, int cpu);
  1117. extern void print_rt_stats(struct seq_file *m, int cpu);
  1118. extern void init_cfs_rq(struct cfs_rq *cfs_rq);
  1119. extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
  1120. extern void cfs_bandwidth_usage_inc(void);
  1121. extern void cfs_bandwidth_usage_dec(void);
  1122. #ifdef CONFIG_NO_HZ_COMMON
  1123. enum rq_nohz_flag_bits {
  1124. NOHZ_TICK_STOPPED,
  1125. NOHZ_BALANCE_KICK,
  1126. };
  1127. #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
  1128. #endif
  1129. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  1130. DECLARE_PER_CPU(u64, cpu_hardirq_time);
  1131. DECLARE_PER_CPU(u64, cpu_softirq_time);
  1132. #ifndef CONFIG_64BIT
  1133. DECLARE_PER_CPU(seqcount_t, irq_time_seq);
  1134. static inline void irq_time_write_begin(void)
  1135. {
  1136. __this_cpu_inc(irq_time_seq.sequence);
  1137. smp_wmb();
  1138. }
  1139. static inline void irq_time_write_end(void)
  1140. {
  1141. smp_wmb();
  1142. __this_cpu_inc(irq_time_seq.sequence);
  1143. }
  1144. static inline u64 irq_time_read(int cpu)
  1145. {
  1146. u64 irq_time;
  1147. unsigned seq;
  1148. do {
  1149. seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
  1150. irq_time = per_cpu(cpu_softirq_time, cpu) +
  1151. per_cpu(cpu_hardirq_time, cpu);
  1152. } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
  1153. return irq_time;
  1154. }
  1155. #else /* CONFIG_64BIT */
  1156. static inline void irq_time_write_begin(void)
  1157. {
  1158. }
  1159. static inline void irq_time_write_end(void)
  1160. {
  1161. }
  1162. static inline u64 irq_time_read(int cpu)
  1163. {
  1164. return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
  1165. }
  1166. #endif /* CONFIG_64BIT */
  1167. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */