sched.h 31 KB

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