sched.h 35 KB

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