sched.h 30 KB

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