sched.h 35 KB

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