sched.h 31 KB

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