sched.h 36 KB

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