sched.h 38 KB

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