sched.h 36 KB

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