Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 246.26 KB | None | 0 0
  1. Index: linux-3.4-ck3/arch/powerpc/platforms/cell/spufs/sched.c
  2. ===================================================================
  3. --- linux-3.4-ck3.orig/arch/powerpc/platforms/cell/spufs/sched.c 2012-01-05 10:55:44.000000000 +1100
  4. +++ linux-3.4-ck3/arch/powerpc/platforms/cell/spufs/sched.c 2012-07-03 14:00:08.121680784 +1000
  5. @@ -63,11 +63,6 @@ static struct timer_list spusched_timer;
  6. static struct timer_list spuloadavg_timer;
  7.  
  8. /*
  9. - * Priority of a normal, non-rt, non-niced'd process (aka nice level 0).
  10. - */
  11. -#define NORMAL_PRIO 120
  12. -
  13. -/*
  14. * Frequency of the spu scheduler tick. By default we do one SPU scheduler
  15. * tick for every 10 CPU scheduler ticks.
  16. */
  17. Index: linux-3.4-ck3/Documentation/scheduler/sched-BFS.txt
  18. ===================================================================
  19. --- /dev/null 1970-01-01 00:00:00.000000000 +0000
  20. +++ linux-3.4-ck3/Documentation/scheduler/sched-BFS.txt 2012-07-03 14:00:08.121680784 +1000
  21. @@ -0,0 +1,347 @@
  22. +BFS - The Brain Fuck Scheduler by Con Kolivas.
  23. +
  24. +Goals.
  25. +
  26. +The goal of the Brain Fuck Scheduler, referred to as BFS from here on, is to
  27. +completely do away with the complex designs of the past for the cpu process
  28. +scheduler and instead implement one that is very simple in basic design.
  29. +The main focus of BFS is to achieve excellent desktop interactivity and
  30. +responsiveness without heuristics and tuning knobs that are difficult to
  31. +understand, impossible to model and predict the effect of, and when tuned to
  32. +one workload cause massive detriment to another.
  33. +
  34. +
  35. +Design summary.
  36. +
  37. +BFS is best described as a single runqueue, O(n) lookup, earliest effective
  38. +virtual deadline first design, loosely based on EEVDF (earliest eligible virtual
  39. +deadline first) and my previous Staircase Deadline scheduler. Each component
  40. +shall be described in order to understand the significance of, and reasoning for
  41. +it. The codebase when the first stable version was released was approximately
  42. +9000 lines less code than the existing mainline linux kernel scheduler (in
  43. +2.6.31). This does not even take into account the removal of documentation and
  44. +the cgroups code that is not used.
  45. +
  46. +Design reasoning.
  47. +
  48. +The single runqueue refers to the queued but not running processes for the
  49. +entire system, regardless of the number of CPUs. The reason for going back to
  50. +a single runqueue design is that once multiple runqueues are introduced,
  51. +per-CPU or otherwise, there will be complex interactions as each runqueue will
  52. +be responsible for the scheduling latency and fairness of the tasks only on its
  53. +own runqueue, and to achieve fairness and low latency across multiple CPUs, any
  54. +advantage in throughput of having CPU local tasks causes other disadvantages.
  55. +This is due to requiring a very complex balancing system to at best achieve some
  56. +semblance of fairness across CPUs and can only maintain relatively low latency
  57. +for tasks bound to the same CPUs, not across them. To increase said fairness
  58. +and latency across CPUs, the advantage of local runqueue locking, which makes
  59. +for better scalability, is lost due to having to grab multiple locks.
  60. +
  61. +A significant feature of BFS is that all accounting is done purely based on CPU
  62. +used and nowhere is sleep time used in any way to determine entitlement or
  63. +interactivity. Interactivity "estimators" that use some kind of sleep/run
  64. +algorithm are doomed to fail to detect all interactive tasks, and to falsely tag
  65. +tasks that aren't interactive as being so. The reason for this is that it is
  66. +close to impossible to determine that when a task is sleeping, whether it is
  67. +doing it voluntarily, as in a userspace application waiting for input in the
  68. +form of a mouse click or otherwise, or involuntarily, because it is waiting for
  69. +another thread, process, I/O, kernel activity or whatever. Thus, such an
  70. +estimator will introduce corner cases, and more heuristics will be required to
  71. +cope with those corner cases, introducing more corner cases and failed
  72. +interactivity detection and so on. Interactivity in BFS is built into the design
  73. +by virtue of the fact that tasks that are waking up have not used up their quota
  74. +of CPU time, and have earlier effective deadlines, thereby making it very likely
  75. +they will preempt any CPU bound task of equivalent nice level. See below for
  76. +more information on the virtual deadline mechanism. Even if they do not preempt
  77. +a running task, because the rr interval is guaranteed to have a bound upper
  78. +limit on how long a task will wait for, it will be scheduled within a timeframe
  79. +that will not cause visible interface jitter.
  80. +
  81. +
  82. +Design details.
  83. +
  84. +Task insertion.
  85. +
  86. +BFS inserts tasks into each relevant queue as an O(1) insertion into a double
  87. +linked list. On insertion, *every* running queue is checked to see if the newly
  88. +queued task can run on any idle queue, or preempt the lowest running task on the
  89. +system. This is how the cross-CPU scheduling of BFS achieves significantly lower
  90. +latency per extra CPU the system has. In this case the lookup is, in the worst
  91. +case scenario, O(n) where n is the number of CPUs on the system.
  92. +
  93. +Data protection.
  94. +
  95. +BFS has one single lock protecting the process local data of every task in the
  96. +global queue. Thus every insertion, removal and modification of task data in the
  97. +global runqueue needs to grab the global lock. However, once a task is taken by
  98. +a CPU, the CPU has its own local data copy of the running process' accounting
  99. +information which only that CPU accesses and modifies (such as during a
  100. +timer tick) thus allowing the accounting data to be updated lockless. Once a
  101. +CPU has taken a task to run, it removes it from the global queue. Thus the
  102. +global queue only ever has, at most,
  103. +
  104. + (number of tasks requesting cpu time) - (number of logical CPUs) + 1
  105. +
  106. +tasks in the global queue. This value is relevant for the time taken to look up
  107. +tasks during scheduling. This will increase if many tasks with CPU affinity set
  108. +in their policy to limit which CPUs they're allowed to run on if they outnumber
  109. +the number of CPUs. The +1 is because when rescheduling a task, the CPU's
  110. +currently running task is put back on the queue. Lookup will be described after
  111. +the virtual deadline mechanism is explained.
  112. +
  113. +Virtual deadline.
  114. +
  115. +The key to achieving low latency, scheduling fairness, and "nice level"
  116. +distribution in BFS is entirely in the virtual deadline mechanism. The one
  117. +tunable in BFS is the rr_interval, or "round robin interval". This is the
  118. +maximum time two SCHED_OTHER (or SCHED_NORMAL, the common scheduling policy)
  119. +tasks of the same nice level will be running for, or looking at it the other
  120. +way around, the longest duration two tasks of the same nice level will be
  121. +delayed for. When a task requests cpu time, it is given a quota (time_slice)
  122. +equal to the rr_interval and a virtual deadline. The virtual deadline is
  123. +offset from the current time in jiffies by this equation:
  124. +
  125. + jiffies + (prio_ratio * rr_interval)
  126. +
  127. +The prio_ratio is determined as a ratio compared to the baseline of nice -20
  128. +and increases by 10% per nice level. The deadline is a virtual one only in that
  129. +no guarantee is placed that a task will actually be scheduled by this time, but
  130. +it is used to compare which task should go next. There are three components to
  131. +how a task is next chosen. First is time_slice expiration. If a task runs out
  132. +of its time_slice, it is descheduled, the time_slice is refilled, and the
  133. +deadline reset to that formula above. Second is sleep, where a task no longer
  134. +is requesting CPU for whatever reason. The time_slice and deadline are _not_
  135. +adjusted in this case and are just carried over for when the task is next
  136. +scheduled. Third is preemption, and that is when a newly waking task is deemed
  137. +higher priority than a currently running task on any cpu by virtue of the fact
  138. +that it has an earlier virtual deadline than the currently running task. The
  139. +earlier deadline is the key to which task is next chosen for the first and
  140. +second cases. Once a task is descheduled, it is put back on the queue, and an
  141. +O(n) lookup of all queued-but-not-running tasks is done to determine which has
  142. +the earliest deadline and that task is chosen to receive CPU next.
  143. +
  144. +The CPU proportion of different nice tasks works out to be approximately the
  145. +
  146. + (prio_ratio difference)^2
  147. +
  148. +The reason it is squared is that a task's deadline does not change while it is
  149. +running unless it runs out of time_slice. Thus, even if the time actually
  150. +passes the deadline of another task that is queued, it will not get CPU time
  151. +unless the current running task deschedules, and the time "base" (jiffies) is
  152. +constantly moving.
  153. +
  154. +Task lookup.
  155. +
  156. +BFS has 103 priority queues. 100 of these are dedicated to the static priority
  157. +of realtime tasks, and the remaining 3 are, in order of best to worst priority,
  158. +SCHED_ISO (isochronous), SCHED_NORMAL, and SCHED_IDLEPRIO (idle priority
  159. +scheduling). When a task of these priorities is queued, a bitmap of running
  160. +priorities is set showing which of these priorities has tasks waiting for CPU
  161. +time. When a CPU is made to reschedule, the lookup for the next task to get
  162. +CPU time is performed in the following way:
  163. +
  164. +First the bitmap is checked to see what static priority tasks are queued. If
  165. +any realtime priorities are found, the corresponding queue is checked and the
  166. +first task listed there is taken (provided CPU affinity is suitable) and lookup
  167. +is complete. If the priority corresponds to a SCHED_ISO task, they are also
  168. +taken in FIFO order (as they behave like SCHED_RR). If the priority corresponds
  169. +to either SCHED_NORMAL or SCHED_IDLEPRIO, then the lookup becomes O(n). At this
  170. +stage, every task in the runlist that corresponds to that priority is checked
  171. +to see which has the earliest set deadline, and (provided it has suitable CPU
  172. +affinity) it is taken off the runqueue and given the CPU. If a task has an
  173. +expired deadline, it is taken and the rest of the lookup aborted (as they are
  174. +chosen in FIFO order).
  175. +
  176. +Thus, the lookup is O(n) in the worst case only, where n is as described
  177. +earlier, as tasks may be chosen before the whole task list is looked over.
  178. +
  179. +
  180. +Scalability.
  181. +
  182. +The major limitations of BFS will be that of scalability, as the separate
  183. +runqueue designs will have less lock contention as the number of CPUs rises.
  184. +However they do not scale linearly even with separate runqueues as multiple
  185. +runqueues will need to be locked concurrently on such designs to be able to
  186. +achieve fair CPU balancing, to try and achieve some sort of nice-level fairness
  187. +across CPUs, and to achieve low enough latency for tasks on a busy CPU when
  188. +other CPUs would be more suited. BFS has the advantage that it requires no
  189. +balancing algorithm whatsoever, as balancing occurs by proxy simply because
  190. +all CPUs draw off the global runqueue, in priority and deadline order. Despite
  191. +the fact that scalability is _not_ the prime concern of BFS, it both shows very
  192. +good scalability to smaller numbers of CPUs and is likely a more scalable design
  193. +at these numbers of CPUs.
  194. +
  195. +It also has some very low overhead scalability features built into the design
  196. +when it has been deemed their overhead is so marginal that they're worth adding.
  197. +The first is the local copy of the running process' data to the CPU it's running
  198. +on to allow that data to be updated lockless where possible. Then there is
  199. +deference paid to the last CPU a task was running on, by trying that CPU first
  200. +when looking for an idle CPU to use the next time it's scheduled. Finally there
  201. +is the notion of "sticky" tasks that are flagged when they are involuntarily
  202. +descheduled, meaning they still want further CPU time. This sticky flag is
  203. +used to bias heavily against those tasks being scheduled on a different CPU
  204. +unless that CPU would be otherwise idle. When a cpu frequency governor is used
  205. +that scales with CPU load, such as ondemand, sticky tasks are not scheduled
  206. +on a different CPU at all, preferring instead to go idle. This means the CPU
  207. +they were bound to is more likely to increase its speed while the other CPU
  208. +will go idle, thus speeding up total task execution time and likely decreasing
  209. +power usage. This is the only scenario where BFS will allow a CPU to go idle
  210. +in preference to scheduling a task on the earliest available spare CPU.
  211. +
  212. +The real cost of migrating a task from one CPU to another is entirely dependant
  213. +on the cache footprint of the task, how cache intensive the task is, how long
  214. +it's been running on that CPU to take up the bulk of its cache, how big the CPU
  215. +cache is, how fast and how layered the CPU cache is, how fast a context switch
  216. +is... and so on. In other words, it's close to random in the real world where we
  217. +do more than just one sole workload. The only thing we can be sure of is that
  218. +it's not free. So BFS uses the principle that an idle CPU is a wasted CPU and
  219. +utilising idle CPUs is more important than cache locality, and cache locality
  220. +only plays a part after that.
  221. +
  222. +When choosing an idle CPU for a waking task, the cache locality is determined
  223. +according to where the task last ran and then idle CPUs are ranked from best
  224. +to worst to choose the most suitable idle CPU based on cache locality, NUMA
  225. +node locality and hyperthread sibling business. They are chosen in the
  226. +following preference (if idle):
  227. +
  228. +* Same core, idle or busy cache, idle threads
  229. +* Other core, same cache, idle or busy cache, idle threads.
  230. +* Same node, other CPU, idle cache, idle threads.
  231. +* Same node, other CPU, busy cache, idle threads.
  232. +* Same core, busy threads.
  233. +* Other core, same cache, busy threads.
  234. +* Same node, other CPU, busy threads.
  235. +* Other node, other CPU, idle cache, idle threads.
  236. +* Other node, other CPU, busy cache, idle threads.
  237. +* Other node, other CPU, busy threads.
  238. +
  239. +This shows the SMT or "hyperthread" awareness in the design as well which will
  240. +choose a real idle core first before a logical SMT sibling which already has
  241. +tasks on the physical CPU.
  242. +
  243. +Early benchmarking of BFS suggested scalability dropped off at the 16 CPU mark.
  244. +However this benchmarking was performed on an earlier design that was far less
  245. +scalable than the current one so it's hard to know how scalable it is in terms
  246. +of both CPUs (due to the global runqueue) and heavily loaded machines (due to
  247. +O(n) lookup) at this stage. Note that in terms of scalability, the number of
  248. +_logical_ CPUs matters, not the number of _physical_ CPUs. Thus, a dual (2x)
  249. +quad core (4X) hyperthreaded (2X) machine is effectively a 16X. Newer benchmark
  250. +results are very promising indeed, without needing to tweak any knobs, features
  251. +or options. Benchmark contributions are most welcome.
  252. +
  253. +
  254. +Features
  255. +
  256. +As the initial prime target audience for BFS was the average desktop user, it
  257. +was designed to not need tweaking, tuning or have features set to obtain benefit
  258. +from it. Thus the number of knobs and features has been kept to an absolute
  259. +minimum and should not require extra user input for the vast majority of cases.
  260. +There are precisely 2 tunables, and 2 extra scheduling policies. The rr_interval
  261. +and iso_cpu tunables, and the SCHED_ISO and SCHED_IDLEPRIO policies. In addition
  262. +to this, BFS also uses sub-tick accounting. What BFS does _not_ now feature is
  263. +support for CGROUPS. The average user should neither need to know what these
  264. +are, nor should they need to be using them to have good desktop behaviour.
  265. +
  266. +rr_interval
  267. +
  268. +There is only one "scheduler" tunable, the round robin interval. This can be
  269. +accessed in
  270. +
  271. + /proc/sys/kernel/rr_interval
  272. +
  273. +The value is in milliseconds, and the default value is set to 6ms. Valid values
  274. +are from 1 to 1000. Decreasing the value will decrease latencies at the cost of
  275. +decreasing throughput, while increasing it will improve throughput, but at the
  276. +cost of worsening latencies. The accuracy of the rr interval is limited by HZ
  277. +resolution of the kernel configuration. Thus, the worst case latencies are
  278. +usually slightly higher than this actual value. BFS uses "dithering" to try and
  279. +minimise the effect the Hz limitation has. The default value of 6 is not an
  280. +arbitrary one. It is based on the fact that humans can detect jitter at
  281. +approximately 7ms, so aiming for much lower latencies is pointless under most
  282. +circumstances. It is worth noting this fact when comparing the latency
  283. +performance of BFS to other schedulers. Worst case latencies being higher than
  284. +7ms are far worse than average latencies not being in the microsecond range.
  285. +Experimentation has shown that rr intervals being increased up to 300 can
  286. +improve throughput but beyond that, scheduling noise from elsewhere prevents
  287. +further demonstrable throughput.
  288. +
  289. +Isochronous scheduling.
  290. +
  291. +Isochronous scheduling is a unique scheduling policy designed to provide
  292. +near-real-time performance to unprivileged (ie non-root) users without the
  293. +ability to starve the machine indefinitely. Isochronous tasks (which means
  294. +"same time") are set using, for example, the schedtool application like so:
  295. +
  296. + schedtool -I -e amarok
  297. +
  298. +This will start the audio application "amarok" as SCHED_ISO. How SCHED_ISO works
  299. +is that it has a priority level between true realtime tasks and SCHED_NORMAL
  300. +which would allow them to preempt all normal tasks, in a SCHED_RR fashion (ie,
  301. +if multiple SCHED_ISO tasks are running, they purely round robin at rr_interval
  302. +rate). However if ISO tasks run for more than a tunable finite amount of time,
  303. +they are then demoted back to SCHED_NORMAL scheduling. This finite amount of
  304. +time is the percentage of _total CPU_ available across the machine, configurable
  305. +as a percentage in the following "resource handling" tunable (as opposed to a
  306. +scheduler tunable):
  307. +
  308. + /proc/sys/kernel/iso_cpu
  309. +
  310. +and is set to 70% by default. It is calculated over a rolling 5 second average
  311. +Because it is the total CPU available, it means that on a multi CPU machine, it
  312. +is possible to have an ISO task running as realtime scheduling indefinitely on
  313. +just one CPU, as the other CPUs will be available. Setting this to 100 is the
  314. +equivalent of giving all users SCHED_RR access and setting it to 0 removes the
  315. +ability to run any pseudo-realtime tasks.
  316. +
  317. +A feature of BFS is that it detects when an application tries to obtain a
  318. +realtime policy (SCHED_RR or SCHED_FIFO) and the caller does not have the
  319. +appropriate privileges to use those policies. When it detects this, it will
  320. +give the task SCHED_ISO policy instead. Thus it is transparent to the user.
  321. +Because some applications constantly set their policy as well as their nice
  322. +level, there is potential for them to undo the override specified by the user
  323. +on the command line of setting the policy to SCHED_ISO. To counter this, once
  324. +a task has been set to SCHED_ISO policy, it needs superuser privileges to set
  325. +it back to SCHED_NORMAL. This will ensure the task remains ISO and all child
  326. +processes and threads will also inherit the ISO policy.
  327. +
  328. +Idleprio scheduling.
  329. +
  330. +Idleprio scheduling is a scheduling policy designed to give out CPU to a task
  331. +_only_ when the CPU would be otherwise idle. The idea behind this is to allow
  332. +ultra low priority tasks to be run in the background that have virtually no
  333. +effect on the foreground tasks. This is ideally suited to distributed computing
  334. +clients (like setiathome, folding, mprime etc) but can also be used to start
  335. +a video encode or so on without any slowdown of other tasks. To avoid this
  336. +policy from grabbing shared resources and holding them indefinitely, if it
  337. +detects a state where the task is waiting on I/O, the machine is about to
  338. +suspend to ram and so on, it will transiently schedule them as SCHED_NORMAL. As
  339. +per the Isochronous task management, once a task has been scheduled as IDLEPRIO,
  340. +it cannot be put back to SCHED_NORMAL without superuser privileges. Tasks can
  341. +be set to start as SCHED_IDLEPRIO with the schedtool command like so:
  342. +
  343. + schedtool -D -e ./mprime
  344. +
  345. +Subtick accounting.
  346. +
  347. +It is surprisingly difficult to get accurate CPU accounting, and in many cases,
  348. +the accounting is done by simply determining what is happening at the precise
  349. +moment a timer tick fires off. This becomes increasingly inaccurate as the
  350. +timer tick frequency (HZ) is lowered. It is possible to create an application
  351. +which uses almost 100% CPU, yet by being descheduled at the right time, records
  352. +zero CPU usage. While the main problem with this is that there are possible
  353. +security implications, it is also difficult to determine how much CPU a task
  354. +really does use. BFS tries to use the sub-tick accounting from the TSC clock,
  355. +where possible, to determine real CPU usage. This is not entirely reliable, but
  356. +is far more likely to produce accurate CPU usage data than the existing designs
  357. +and will not show tasks as consuming no CPU usage when they actually are. Thus,
  358. +the amount of CPU reported as being used by BFS will more accurately represent
  359. +how much CPU the task itself is using (as is shown for example by the 'time'
  360. +application), so the reported values may be quite different to other schedulers.
  361. +Values reported as the 'load' are more prone to problems with this design, but
  362. +per process values are closer to real usage. When comparing throughput of BFS
  363. +to other designs, it is important to compare the actual completed work in terms
  364. +of total wall clock time taken and total work done, rather than the reported
  365. +"cpu usage".
  366. +
  367. +
  368. +Con Kolivas <kernel@kolivas.org> Tue, 5 Apr 2011
  369. Index: linux-3.4-ck3/Documentation/sysctl/kernel.txt
  370. ===================================================================
  371. --- linux-3.4-ck3.orig/Documentation/sysctl/kernel.txt 2012-03-20 17:39:40.000000000 +1100
  372. +++ linux-3.4-ck3/Documentation/sysctl/kernel.txt 2012-07-03 14:00:08.122680802 +1000
  373. @@ -33,6 +33,7 @@ show up in /proc/sys/kernel:
  374. - domainname
  375. - hostname
  376. - hotplug
  377. +- iso_cpu
  378. - kptr_restrict
  379. - kstack_depth_to_print [ X86 only ]
  380. - l2cr [ PPC only ]
  381. @@ -59,6 +60,7 @@ show up in /proc/sys/kernel:
  382. - randomize_va_space
  383. - real-root-dev ==> Documentation/initrd.txt
  384. - reboot-cmd [ SPARC only ]
  385. +- rr_interval
  386. - rtsig-max
  387. - rtsig-nr
  388. - sem
  389. @@ -301,6 +303,16 @@ kernel stack.
  390.  
  391. ==============================================================
  392.  
  393. +iso_cpu: (BFS CPU scheduler only).
  394. +
  395. +This sets the percentage cpu that the unprivileged SCHED_ISO tasks can
  396. +run effectively at realtime priority, averaged over a rolling five
  397. +seconds over the -whole- system, meaning all cpus.
  398. +
  399. +Set to 70 (percent) by default.
  400. +
  401. +==============================================================
  402. +
  403. l2cr: (PPC only)
  404.  
  405. This flag controls the L2 cache of G3 processor boards. If
  406. @@ -517,6 +529,20 @@ rebooting. ???
  407.  
  408. ==============================================================
  409.  
  410. +rr_interval: (BFS CPU scheduler only)
  411. +
  412. +This is the smallest duration that any cpu process scheduling unit
  413. +will run for. Increasing this value can increase throughput of cpu
  414. +bound tasks substantially but at the expense of increased latencies
  415. +overall. Conversely decreasing it will decrease average and maximum
  416. +latencies but at the expense of throughput. This value is in
  417. +milliseconds and the default value chosen depends on the number of
  418. +cpus available at scheduler initialisation with a minimum of 6.
  419. +
  420. +Valid values are from 1-1000.
  421. +
  422. +==============================================================
  423. +
  424. rtsig-max & rtsig-nr:
  425.  
  426. The file rtsig-max can be used to tune the maximum number
  427. Index: linux-3.4-ck3/fs/proc/base.c
  428. ===================================================================
  429. --- linux-3.4-ck3.orig/fs/proc/base.c 2012-06-01 21:37:22.000000000 +1000
  430. +++ linux-3.4-ck3/fs/proc/base.c 2012-07-03 14:00:08.122680802 +1000
  431. @@ -342,7 +342,7 @@ static int proc_pid_stack(struct seq_fil
  432. static int proc_pid_schedstat(struct task_struct *task, char *buffer)
  433. {
  434. return sprintf(buffer, "%llu %llu %lu\n",
  435. - (unsigned long long)task->se.sum_exec_runtime,
  436. + (unsigned long long)tsk_seruntime(task),
  437. (unsigned long long)task->sched_info.run_delay,
  438. task->sched_info.pcount);
  439. }
  440. Index: linux-3.4-ck3/include/linux/init_task.h
  441. ===================================================================
  442. --- linux-3.4-ck3.orig/include/linux/init_task.h 2012-06-01 21:37:22.000000000 +1000
  443. +++ linux-3.4-ck3/include/linux/init_task.h 2012-07-03 14:00:08.123680821 +1000
  444. @@ -132,12 +132,70 @@ extern struct cred init_cred;
  445. # define INIT_PERF_EVENTS(tsk)
  446. #endif
  447.  
  448. -#define INIT_TASK_COMM "swapper"
  449. -
  450. /*
  451. * INIT_TASK is used to set up the first task table, touch at
  452. * your own risk!. Base=0, limit=0x1fffff (=2MB)
  453. */
  454. +#ifdef CONFIG_SCHED_BFS
  455. +#define INIT_TASK_COMM "BFS"
  456. +#define INIT_TASK(tsk) \
  457. +{ \
  458. + .state = 0, \
  459. + .stack = &init_thread_info, \
  460. + .usage = ATOMIC_INIT(2), \
  461. + .flags = PF_KTHREAD, \
  462. + .prio = NORMAL_PRIO, \
  463. + .static_prio = MAX_PRIO-20, \
  464. + .normal_prio = NORMAL_PRIO, \
  465. + .deadline = 0, \
  466. + .policy = SCHED_NORMAL, \
  467. + .cpus_allowed = CPU_MASK_ALL, \
  468. + .mm = NULL, \
  469. + .active_mm = &init_mm, \
  470. + .run_list = LIST_HEAD_INIT(tsk.run_list), \
  471. + .time_slice = HZ, \
  472. + .tasks = LIST_HEAD_INIT(tsk.tasks), \
  473. + INIT_PUSHABLE_TASKS(tsk) \
  474. + .ptraced = LIST_HEAD_INIT(tsk.ptraced), \
  475. + .ptrace_entry = LIST_HEAD_INIT(tsk.ptrace_entry), \
  476. + .real_parent = &tsk, \
  477. + .parent = &tsk, \
  478. + .children = LIST_HEAD_INIT(tsk.children), \
  479. + .sibling = LIST_HEAD_INIT(tsk.sibling), \
  480. + .group_leader = &tsk, \
  481. + RCU_INIT_POINTER(.real_cred, &init_cred), \
  482. + RCU_INIT_POINTER(.cred, &init_cred), \
  483. + .comm = INIT_TASK_COMM, \
  484. + .thread = INIT_THREAD, \
  485. + .fs = &init_fs, \
  486. + .files = &init_files, \
  487. + .signal = &init_signals, \
  488. + .sighand = &init_sighand, \
  489. + .nsproxy = &init_nsproxy, \
  490. + .pending = { \
  491. + .list = LIST_HEAD_INIT(tsk.pending.list), \
  492. + .signal = {{0}}}, \
  493. + .blocked = {{0}}, \
  494. + .alloc_lock = __SPIN_LOCK_UNLOCKED(tsk.alloc_lock), \
  495. + .journal_info = NULL, \
  496. + .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  497. + .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  498. + .timer_slack_ns = 50000, /* 50 usec default slack */ \
  499. + .pids = { \
  500. + [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  501. + [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  502. + [PIDTYPE_SID] = INIT_PID_LINK(PIDTYPE_SID), \
  503. + }, \
  504. + INIT_IDS \
  505. + INIT_PERF_EVENTS(tsk) \
  506. + INIT_TRACE_IRQFLAGS \
  507. + INIT_LOCKDEP \
  508. + INIT_FTRACE_GRAPH \
  509. + INIT_TRACE_RECURSION \
  510. + INIT_TASK_RCU_PREEMPT(tsk) \
  511. +}
  512. +#else /* CONFIG_SCHED_BFS */
  513. +#define INIT_TASK_COMM "swapper"
  514. #define INIT_TASK(tsk) \
  515. { \
  516. .state = 0, \
  517. @@ -201,7 +259,7 @@ extern struct cred init_cred;
  518. INIT_TASK_RCU_PREEMPT(tsk) \
  519. INIT_CPUSET_SEQ \
  520. }
  521. -
  522. +#endif /* CONFIG_SCHED_BFS */
  523.  
  524. #define INIT_CPU_TIMERS(cpu_timers) \
  525. { \
  526. Index: linux-3.4-ck3/include/linux/ioprio.h
  527. ===================================================================
  528. --- linux-3.4-ck3.orig/include/linux/ioprio.h 2012-01-05 10:55:44.000000000 +1100
  529. +++ linux-3.4-ck3/include/linux/ioprio.h 2012-07-03 14:00:08.123680821 +1000
  530. @@ -64,6 +64,8 @@ static inline int task_ioprio_class(stru
  531.  
  532. static inline int task_nice_ioprio(struct task_struct *task)
  533. {
  534. + if (iso_task(task))
  535. + return 0;
  536. return (task_nice(task) + 20) / 5;
  537. }
  538.  
  539. Index: linux-3.4-ck3/include/linux/sched.h
  540. ===================================================================
  541. --- linux-3.4-ck3.orig/include/linux/sched.h 2012-06-01 21:37:23.000000000 +1000
  542. +++ linux-3.4-ck3/include/linux/sched.h 2012-07-03 14:00:42.606326914 +1000
  543. @@ -37,8 +37,15 @@
  544. #define SCHED_FIFO 1
  545. #define SCHED_RR 2
  546. #define SCHED_BATCH 3
  547. -/* SCHED_ISO: reserved but not implemented yet */
  548. +/* SCHED_ISO: Implemented on BFS only */
  549. #define SCHED_IDLE 5
  550. +#define SCHED_IDLEPRIO SCHED_IDLE
  551. +#ifdef CONFIG_SCHED_BFS
  552. +#define SCHED_ISO 4
  553. +#define SCHED_MAX (SCHED_IDLEPRIO)
  554. +#define SCHED_RANGE(policy) ((policy) <= SCHED_MAX)
  555. +#endif
  556. +
  557. /* Can be ORed in to make sure the process is reverted back to SCHED_NORMAL on fork */
  558. #define SCHED_RESET_ON_FORK 0x40000000
  559.  
  560. @@ -268,8 +275,6 @@ extern asmlinkage void schedule_tail(str
  561. extern void init_idle(struct task_struct *idle, int cpu);
  562. extern void init_idle_bootup_task(struct task_struct *idle);
  563.  
  564. -extern int runqueue_is_locked(int cpu);
  565. -
  566. #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ)
  567. extern void select_nohz_load_balancer(int stop_tick);
  568. extern void set_cpu_sd_state_idle(void);
  569. @@ -1270,15 +1275,31 @@ struct task_struct {
  570.  
  571. #ifdef CONFIG_SMP
  572. struct llist_node wake_entry;
  573. - int on_cpu;
  574. #endif
  575. - int on_rq;
  576. +#if defined(CONFIG_SMP) || defined(CONFIG_SCHED_BFS)
  577. + bool on_cpu;
  578. +#endif
  579. +#ifndef CONFIG_SCHED_BFS
  580. + bool on_rq;
  581. +#endif
  582.  
  583. int prio, static_prio, normal_prio;
  584. unsigned int rt_priority;
  585. +#ifdef CONFIG_SCHED_BFS
  586. + int time_slice;
  587. + u64 deadline;
  588. + struct list_head run_list;
  589. + u64 last_ran;
  590. + u64 sched_time; /* sched_clock time spent running */
  591. +#ifdef CONFIG_SMP
  592. + bool sticky; /* Soft affined flag */
  593. +#endif
  594. + unsigned long rt_timeout;
  595. +#else /* CONFIG_SCHED_BFS */
  596. const struct sched_class *sched_class;
  597. struct sched_entity se;
  598. struct sched_rt_entity rt;
  599. +#endif
  600.  
  601. #ifdef CONFIG_PREEMPT_NOTIFIERS
  602. /* list of struct preempt_notifier: */
  603. @@ -1390,6 +1411,9 @@ struct task_struct {
  604. int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
  605.  
  606. cputime_t utime, stime, utimescaled, stimescaled;
  607. +#ifdef CONFIG_SCHED_BFS
  608. + unsigned long utime_pc, stime_pc;
  609. +#endif
  610. cputime_t gtime;
  611. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  612. cputime_t prev_utime, prev_stime;
  613. @@ -1619,6 +1643,64 @@ struct task_struct {
  614. #endif
  615. };
  616.  
  617. +#ifdef CONFIG_SCHED_BFS
  618. +bool grunqueue_is_locked(void);
  619. +void grq_unlock_wait(void);
  620. +void cpu_scaling(int cpu);
  621. +void cpu_nonscaling(int cpu);
  622. +bool above_background_load(void);
  623. +#define tsk_seruntime(t) ((t)->sched_time)
  624. +#define tsk_rttimeout(t) ((t)->rt_timeout)
  625. +
  626. +static inline void tsk_cpus_current(struct task_struct *p)
  627. +{
  628. +}
  629. +
  630. +static inline int runqueue_is_locked(int cpu)
  631. +{
  632. + return grunqueue_is_locked();
  633. +}
  634. +
  635. +void print_scheduler_version(void);
  636. +
  637. +static inline bool iso_task(struct task_struct *p)
  638. +{
  639. + return (p->policy == SCHED_ISO);
  640. +}
  641. +#else /* CFS */
  642. +extern int runqueue_is_locked(int cpu);
  643. +static inline void cpu_scaling(int cpu)
  644. +{
  645. +}
  646. +
  647. +static inline void cpu_nonscaling(int cpu)
  648. +{
  649. +}
  650. +#define tsk_seruntime(t) ((t)->se.sum_exec_runtime)
  651. +#define tsk_rttimeout(t) ((t)->rt.timeout)
  652. +
  653. +static inline void tsk_cpus_current(struct task_struct *p)
  654. +{
  655. + p->rt.nr_cpus_allowed = current->rt.nr_cpus_allowed;
  656. +}
  657. +
  658. +static inline void print_scheduler_version(void)
  659. +{
  660. + printk(KERN_INFO"CFS CPU scheduler.\n");
  661. +}
  662. +
  663. +static inline bool iso_task(struct task_struct *p)
  664. +{
  665. + return false;
  666. +}
  667. +
  668. +/* Anyone feel like implementing this? */
  669. +static inline bool above_background_load(void)
  670. +{
  671. + return false;
  672. +}
  673. +#endif /* CONFIG_SCHED_BFS */
  674. +
  675. /* Future-safe accessor for struct task_struct's cpus_allowed. */
  676. #define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  677.  
  678. @@ -1636,10 +1718,20 @@ struct task_struct {
  679. */
  680.  
  681. #define MAX_USER_RT_PRIO 100
  682. -#define MAX_RT_PRIO MAX_USER_RT_PRIO
  683. +#define MAX_RT_PRIO (MAX_USER_RT_PRIO + 1)
  684. +#define DEFAULT_PRIO (MAX_RT_PRIO + 20)
  685.  
  686. +#ifdef CONFIG_SCHED_BFS
  687. +#define PRIO_RANGE (40)
  688. +#define MAX_PRIO (MAX_RT_PRIO + PRIO_RANGE)
  689. +#define ISO_PRIO (MAX_RT_PRIO)
  690. +#define NORMAL_PRIO (MAX_RT_PRIO + 1)
  691. +#define IDLE_PRIO (MAX_RT_PRIO + 2)
  692. +#define PRIO_LIMIT ((IDLE_PRIO) + 1)
  693. +#else /* CONFIG_SCHED_BFS */
  694. #define MAX_PRIO (MAX_RT_PRIO + 40)
  695. -#define DEFAULT_PRIO (MAX_RT_PRIO + 20)
  696. +#define NORMAL_PRIO DEFAULT_PRIO
  697. +#endif /* CONFIG_SCHED_BFS */
  698.  
  699. static inline int rt_prio(int prio)
  700. {
  701. @@ -2002,7 +2094,7 @@ extern unsigned long long
  702. task_sched_runtime(struct task_struct *task);
  703.  
  704. /* sched_exec is called by processes performing an exec */
  705. -#ifdef CONFIG_SMP
  706. +#if defined(CONFIG_SMP) && !defined(CONFIG_SCHED_BFS)
  707. extern void sched_exec(void);
  708. #else
  709. #define sched_exec() {}
  710. @@ -2705,7 +2797,7 @@ static inline unsigned int task_cpu(cons
  711. return 0;
  712. }
  713.  
  714. -static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
  715. +static inline void set_task_cpu(struct task_struct *p, int cpu)
  716. {
  717. }
  718.  
  719. Index: linux-3.4-ck3/init/Kconfig
  720. ===================================================================
  721. --- linux-3.4-ck3.orig/init/Kconfig 2012-06-01 21:37:23.000000000 +1000
  722. +++ linux-3.4-ck3/init/Kconfig 2012-07-03 14:00:08.124680840 +1000
  723. @@ -29,6 +29,19 @@ config IRQ_WORK
  724.  
  725. menu "General setup"
  726.  
  727. +config SCHED_BFS
  728. + bool "BFS cpu scheduler"
  729. + ---help---
  730. + The Brain Fuck CPU Scheduler for excellent interactivity and
  731. + responsiveness on the desktop and solid scalability on normal
  732. + hardware. Not recommended for 4096 CPUs.
  733. +
  734. + Currently incompatible with the Group CPU scheduler, and RCU TORTURE
  735. + TEST so these options are disabled.
  736. +
  737. + Say Y here.
  738. + default y
  739. +
  740. config EXPERIMENTAL
  741. bool "Prompt for development and/or incomplete code/drivers"
  742. ---help---
  743. @@ -631,6 +644,7 @@ config PROC_PID_CPUSET
  744.  
  745. config CGROUP_CPUACCT
  746. bool "Simple CPU accounting cgroup subsystem"
  747. + depends on !SCHED_BFS
  748. help
  749. Provides a simple Resource Controller for monitoring the
  750. total CPU consumed by the tasks in a cgroup.
  751. @@ -718,6 +732,7 @@ config CGROUP_PERF
  752.  
  753. menuconfig CGROUP_SCHED
  754. bool "Group CPU scheduler"
  755. + depends on !SCHED_BFS
  756. default n
  757. help
  758. This feature lets CPU scheduler recognize task groups and control CPU
  759. @@ -854,6 +869,7 @@ endif # NAMESPACES
  760.  
  761. config SCHED_AUTOGROUP
  762. bool "Automatic process group scheduling"
  763. + depends on !SCHED_BFS
  764. select EVENTFD
  765. select CGROUPS
  766. select CGROUP_SCHED
  767. @@ -1250,38 +1266,8 @@ config COMPAT_BRK
  768.  
  769. On non-ancient distros (post-2000 ones) N is usually a safe choice.
  770.  
  771. -choice
  772. - prompt "Choose SLAB allocator"
  773. - default SLUB
  774. - help
  775. - This option allows to select a slab allocator.
  776. -
  777. -config SLAB
  778. - bool "SLAB"
  779. - help
  780. - The regular slab allocator that is established and known to work
  781. - well in all environments. It organizes cache hot objects in
  782. - per cpu and per node queues.
  783. -
  784. config SLUB
  785. - bool "SLUB (Unqueued Allocator)"
  786. - help
  787. - SLUB is a slab allocator that minimizes cache line usage
  788. - instead of managing queues of cached objects (SLAB approach).
  789. - Per cpu caching is realized using slabs of objects instead
  790. - of queues of objects. SLUB can use memory efficiently
  791. - and has enhanced diagnostics. SLUB is the default choice for
  792. - a slab allocator.
  793. -
  794. -config SLOB
  795. - depends on EXPERT
  796. - bool "SLOB (Simple Allocator)"
  797. - help
  798. - SLOB replaces the stock allocator with a drastically simpler
  799. - allocator. SLOB is generally more space efficient but
  800. - does not perform as well on large systems.
  801. -
  802. -endchoice
  803. + def_bool y
  804.  
  805. config MMAP_ALLOW_UNINITIALIZED
  806. bool "Allow mmapped anonymous memory to be uninitialized"
  807. Index: linux-3.4-ck3/init/main.c
  808. ===================================================================
  809. --- linux-3.4-ck3.orig/init/main.c 2012-06-01 21:37:23.000000000 +1000
  810. +++ linux-3.4-ck3/init/main.c 2012-07-03 14:00:08.124680840 +1000
  811. @@ -807,6 +807,7 @@ static noinline int init_post(void)
  812. system_state = SYSTEM_RUNNING;
  813. numa_default_policy();
  814.  
  815. + print_scheduler_version();
  816.  
  817. current->signal->flags |= SIGNAL_UNKILLABLE;
  818.  
  819. Index: linux-3.4-ck3/kernel/delayacct.c
  820. ===================================================================
  821. --- linux-3.4-ck3.orig/kernel/delayacct.c 2012-01-05 10:55:44.000000000 +1100
  822. +++ linux-3.4-ck3/kernel/delayacct.c 2012-07-03 14:00:08.124680840 +1000
  823. @@ -130,7 +130,7 @@ int __delayacct_add_tsk(struct taskstats
  824. */
  825. t1 = tsk->sched_info.pcount;
  826. t2 = tsk->sched_info.run_delay;
  827. - t3 = tsk->se.sum_exec_runtime;
  828. + t3 = tsk_seruntime(tsk);
  829.  
  830. d->cpu_count += t1;
  831.  
  832. Index: linux-3.4-ck3/kernel/exit.c
  833. ===================================================================
  834. --- linux-3.4-ck3.orig/kernel/exit.c 2012-06-01 21:37:23.000000000 +1000
  835. +++ linux-3.4-ck3/kernel/exit.c 2012-07-03 14:00:08.125680859 +1000
  836. @@ -133,7 +133,7 @@ static void __exit_signal(struct task_st
  837. sig->inblock += task_io_get_inblock(tsk);
  838. sig->oublock += task_io_get_oublock(tsk);
  839. task_io_accounting_add(&sig->ioac, &tsk->ioac);
  840. - sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
  841. + sig->sum_sched_runtime += tsk_seruntime(tsk);
  842. }
  843.  
  844. sig->nr_threads--;
  845. Index: linux-3.4-ck3/kernel/posix-cpu-timers.c
  846. ===================================================================
  847. --- linux-3.4-ck3.orig/kernel/posix-cpu-timers.c 2012-03-20 17:39:43.000000000 +1100
  848. +++ linux-3.4-ck3/kernel/posix-cpu-timers.c 2012-07-03 14:00:08.125680859 +1000
  849. @@ -495,7 +495,7 @@ static void cleanup_timers(struct list_h
  850. void posix_cpu_timers_exit(struct task_struct *tsk)
  851. {
  852. cleanup_timers(tsk->cpu_timers,
  853. - tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
  854. + tsk->utime, tsk->stime, tsk_seruntime(tsk));
  855.  
  856. }
  857. void posix_cpu_timers_exit_group(struct task_struct *tsk)
  858. @@ -504,7 +504,7 @@ void posix_cpu_timers_exit_group(struct
  859.  
  860. cleanup_timers(tsk->signal->cpu_timers,
  861. tsk->utime + sig->utime, tsk->stime + sig->stime,
  862. - tsk->se.sum_exec_runtime + sig->sum_sched_runtime);
  863. + tsk_seruntime(tsk) + sig->sum_sched_runtime);
  864. }
  865.  
  866. static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
  867. @@ -934,7 +934,7 @@ static void check_thread_timers(struct t
  868. struct cpu_timer_list *t = list_first_entry(timers,
  869. struct cpu_timer_list,
  870. entry);
  871. - if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
  872. + if (!--maxfire || tsk_seruntime(tsk) < t->expires.sched) {
  873. tsk->cputime_expires.sched_exp = t->expires.sched;
  874. break;
  875. }
  876. @@ -951,7 +951,7 @@ static void check_thread_timers(struct t
  877. ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
  878.  
  879. if (hard != RLIM_INFINITY &&
  880. - tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
  881. + tsk_rttimeout(tsk) > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
  882. /*
  883. * At the hard limit, we just die.
  884. * No need to calculate anything else now.
  885. @@ -959,7 +959,7 @@ static void check_thread_timers(struct t
  886. __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
  887. return;
  888. }
  889. - if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
  890. + if (tsk_rttimeout(tsk) > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
  891. /*
  892. * At the soft limit, send a SIGXCPU every second.
  893. */
  894. @@ -1252,7 +1252,7 @@ static inline int fastpath_timer_check(s
  895. struct task_cputime task_sample = {
  896. .utime = tsk->utime,
  897. .stime = tsk->stime,
  898. - .sum_exec_runtime = tsk->se.sum_exec_runtime
  899. + .sum_exec_runtime = tsk_seruntime(tsk)
  900. };
  901.  
  902. if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
  903. Index: linux-3.4-ck3/kernel/sysctl.c
  904. ===================================================================
  905. --- linux-3.4-ck3.orig/kernel/sysctl.c 2012-06-01 21:37:23.000000000 +1000
  906. +++ linux-3.4-ck3/kernel/sysctl.c 2012-07-03 14:00:08.125680859 +1000
  907. @@ -126,7 +126,12 @@ static int __maybe_unused one = 1;
  908. static int __maybe_unused two = 2;
  909. static int __maybe_unused three = 3;
  910. static unsigned long one_ul = 1;
  911. -static int one_hundred = 100;
  912. +static int __maybe_unused one_hundred = 100;
  913. +#ifdef CONFIG_SCHED_BFS
  914. +extern int rr_interval;
  915. +extern int sched_iso_cpu;
  916. +static int __read_mostly one_thousand = 1000;
  917. +#endif
  918. #ifdef CONFIG_PRINTK
  919. static int ten_thousand = 10000;
  920. #endif
  921. @@ -241,7 +246,7 @@ static struct ctl_table sysctl_base_tabl
  922. { }
  923. };
  924.  
  925. -#ifdef CONFIG_SCHED_DEBUG
  926. +#if defined(CONFIG_SCHED_DEBUG) && !defined(CONFIG_SCHED_BFS)
  927. static int min_sched_granularity_ns = 100000; /* 100 usecs */
  928. static int max_sched_granularity_ns = NSEC_PER_SEC; /* 1 second */
  929. static int min_wakeup_granularity_ns; /* 0 usecs */
  930. @@ -256,6 +261,7 @@ static int max_extfrag_threshold = 1000;
  931. #endif
  932.  
  933. static struct ctl_table kern_table[] = {
  934. +#ifndef CONFIG_SCHED_BFS
  935. {
  936. .procname = "sched_child_runs_first",
  937. .data = &sysctl_sched_child_runs_first,
  938. @@ -373,6 +379,7 @@ static struct ctl_table kern_table[] = {
  939. .extra1 = &one,
  940. },
  941. #endif
  942. +#endif /* !CONFIG_SCHED_BFS */
  943. #ifdef CONFIG_PROVE_LOCKING
  944. {
  945. .procname = "prove_locking",
  946. @@ -840,6 +847,26 @@ static struct ctl_table kern_table[] = {
  947. .proc_handler = proc_dointvec,
  948. },
  949. #endif
  950. +#ifdef CONFIG_SCHED_BFS
  951. + {
  952. + .procname = "rr_interval",
  953. + .data = &rr_interval,
  954. + .maxlen = sizeof (int),
  955. + .mode = 0644,
  956. + .proc_handler = &proc_dointvec_minmax,
  957. + .extra1 = &one,
  958. + .extra2 = &one_thousand,
  959. + },
  960. + {
  961. + .procname = "iso_cpu",
  962. + .data = &sched_iso_cpu,
  963. + .maxlen = sizeof (int),
  964. + .mode = 0644,
  965. + .proc_handler = &proc_dointvec_minmax,
  966. + .extra1 = &zero,
  967. + .extra2 = &one_hundred,
  968. + },
  969. +#endif
  970. #if defined(CONFIG_S390) && defined(CONFIG_SMP)
  971. {
  972. .procname = "spin_retry",
  973. Index: linux-3.4-ck3/lib/Kconfig.debug
  974. ===================================================================
  975. --- linux-3.4-ck3.orig/lib/Kconfig.debug 2012-06-01 21:37:23.000000000 +1000
  976. +++ linux-3.4-ck3/lib/Kconfig.debug 2012-07-03 14:00:08.126680878 +1000
  977. @@ -880,7 +880,7 @@ config BOOT_PRINTK_DELAY
  978.  
  979. config RCU_TORTURE_TEST
  980. tristate "torture tests for RCU"
  981. - depends on DEBUG_KERNEL
  982. + depends on DEBUG_KERNEL && !SCHED_BFS
  983. default n
  984. help
  985. This option provides a kernel module that runs torture tests
  986. Index: linux-3.4-ck3/include/linux/jiffies.h
  987. ===================================================================
  988. --- linux-3.4-ck3.orig/include/linux/jiffies.h 2012-01-05 10:55:44.000000000 +1100
  989. +++ linux-3.4-ck3/include/linux/jiffies.h 2012-07-03 14:00:08.126680878 +1000
  990. @@ -164,7 +164,7 @@ static inline u64 get_jiffies_64(void)
  991. * Have the 32 bit jiffies value wrap 5 minutes after boot
  992. * so jiffies wrap bugs show up earlier.
  993. */
  994. -#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
  995. +#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-10*HZ))
  996.  
  997. /*
  998. * Change timeval to jiffies, trying to avoid the
  999. Index: linux-3.4-ck3/drivers/cpufreq/cpufreq.c
  1000. ===================================================================
  1001. --- linux-3.4-ck3.orig/drivers/cpufreq/cpufreq.c 2012-06-01 21:37:21.000000000 +1000
  1002. +++ linux-3.4-ck3/drivers/cpufreq/cpufreq.c 2012-07-03 14:00:08.126680878 +1000
  1003. @@ -28,6 +28,7 @@
  1004. #include <linux/cpu.h>
  1005. #include <linux/completion.h>
  1006. #include <linux/mutex.h>
  1007. +#include <linux/sched.h>
  1008. #include <linux/syscore_ops.h>
  1009.  
  1010. #include <trace/events/power.h>
  1011. @@ -1457,6 +1458,12 @@ int __cpufreq_driver_target(struct cpufr
  1012. target_freq, relation);
  1013. if (cpu_online(policy->cpu) && cpufreq_driver->target)
  1014. retval = cpufreq_driver->target(policy, target_freq, relation);
  1015. + if (likely(retval != -EINVAL)) {
  1016. + if (target_freq == policy->max)
  1017. + cpu_nonscaling(policy->cpu);
  1018. + else
  1019. + cpu_scaling(policy->cpu);
  1020. + }
  1021.  
  1022. return retval;
  1023. }
  1024. Index: linux-3.4-ck3/drivers/cpufreq/cpufreq_ondemand.c
  1025. ===================================================================
  1026. --- linux-3.4-ck3.orig/drivers/cpufreq/cpufreq_ondemand.c 2012-06-01 21:37:21.000000000 +1000
  1027. +++ linux-3.4-ck3/drivers/cpufreq/cpufreq_ondemand.c 2012-07-03 14:00:08.127680897 +1000
  1028. @@ -28,8 +28,8 @@
  1029. * It helps to keep variable names smaller, simpler
  1030. */
  1031.  
  1032. -#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
  1033. -#define DEF_FREQUENCY_UP_THRESHOLD (80)
  1034. +#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (26)
  1035. +#define DEF_FREQUENCY_UP_THRESHOLD (63)
  1036. #define DEF_SAMPLING_DOWN_FACTOR (1)
  1037. #define MAX_SAMPLING_DOWN_FACTOR (100000)
  1038. #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
  1039. @@ -472,10 +472,10 @@ static void dbs_check_cpu(struct cpu_dbs
  1040.  
  1041. /*
  1042. * Every sampling_rate, we check, if current idle time is less
  1043. - * than 20% (default), then we try to increase frequency
  1044. + * than 37% (default), then we try to increase frequency
  1045. * Every sampling_rate, we look for a the lowest
  1046. * frequency which can sustain the load while keeping idle time over
  1047. - * 30%. If such a frequency exist, we try to decrease to this frequency.
  1048. + * 63%. If such a frequency exist, we try to decrease to this frequency.
  1049. *
  1050. * Any frequency increase takes it to the maximum frequency.
  1051. * Frequency reduction happens at minimum steps of
  1052. Index: linux-3.4-ck3/drivers/cpufreq/cpufreq_conservative.c
  1053. ===================================================================
  1054. --- linux-3.4-ck3.orig/drivers/cpufreq/cpufreq_conservative.c 2012-03-20 17:39:41.000000000 +1100
  1055. +++ linux-3.4-ck3/drivers/cpufreq/cpufreq_conservative.c 2012-07-03 14:00:08.127680897 +1000
  1056. @@ -29,8 +29,8 @@
  1057. * It helps to keep variable names smaller, simpler
  1058. */
  1059.  
  1060. -#define DEF_FREQUENCY_UP_THRESHOLD (80)
  1061. -#define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  1062. +#define DEF_FREQUENCY_UP_THRESHOLD (63)
  1063. +#define DEF_FREQUENCY_DOWN_THRESHOLD (26)
  1064.  
  1065. /*
  1066. * The polling frequency of this governor depends on the capability of
  1067. Index: linux-3.4-ck3/arch/x86/Kconfig
  1068. ===================================================================
  1069. --- linux-3.4-ck3.orig/arch/x86/Kconfig 2012-06-01 21:37:21.000000000 +1000
  1070. +++ linux-3.4-ck3/arch/x86/Kconfig 2012-07-03 14:00:42.987334056 +1000
  1071. @@ -790,15 +790,7 @@ config SCHED_MC
  1072. increased overhead in some places. If unsure say N here.
  1073.  
  1074. config IRQ_TIME_ACCOUNTING
  1075. - bool "Fine granularity task level IRQ time accounting"
  1076. - default n
  1077. - ---help---
  1078. - Select this option to enable fine granularity task irq time
  1079. - accounting. This is done by reading a timestamp on each
  1080. - transitions between softirq and hardirq state, so there can be a
  1081. - small performance impact.
  1082. -
  1083. - If in doubt, say N here.
  1084. + def_bool y
  1085.  
  1086. source "kernel/Kconfig.preempt"
  1087.  
  1088. @@ -1096,7 +1088,7 @@ endchoice
  1089.  
  1090. choice
  1091. depends on EXPERIMENTAL
  1092. - prompt "Memory split" if EXPERT
  1093. + prompt "Memory split"
  1094. default VMSPLIT_3G
  1095. depends on X86_32
  1096. ---help---
  1097. @@ -1116,17 +1108,17 @@ choice
  1098. option alone!
  1099.  
  1100. config VMSPLIT_3G
  1101. - bool "3G/1G user/kernel split"
  1102. + bool "Default 896MB lowmem (3G/1G user/kernel split)"
  1103. config VMSPLIT_3G_OPT
  1104. depends on !X86_PAE
  1105. - bool "3G/1G user/kernel split (for full 1G low memory)"
  1106. + bool "1GB lowmem (3G/1G user/kernel split)"
  1107. config VMSPLIT_2G
  1108. - bool "2G/2G user/kernel split"
  1109. + bool "2GB lowmem (2G/2G user/kernel split)"
  1110. config VMSPLIT_2G_OPT
  1111. depends on !X86_PAE
  1112. - bool "2G/2G user/kernel split (for full 2G low memory)"
  1113. + bool "2GB lowmem (2G/2G user/kernel split)"
  1114. config VMSPLIT_1G
  1115. - bool "1G/3G user/kernel split"
  1116. + bool "3GB lowmem (1G/3G user/kernel split)"
  1117. endchoice
  1118.  
  1119. config PAGE_OFFSET
  1120. Index: linux-3.4-ck3/kernel/sched/bfs.c
  1121. ===================================================================
  1122. --- /dev/null 1970-01-01 00:00:00.000000000 +0000
  1123. +++ linux-3.4-ck3/kernel/sched/bfs.c 2012-07-03 14:00:13.316778081 +1000
  1124. @@ -0,0 +1,7326 @@
  1125. +/*
  1126. + * kernel/sched/bfs.c, was kernel/sched.c
  1127. + *
  1128. + * Kernel scheduler and related syscalls
  1129. + *
  1130. + * Copyright (C) 1991-2002 Linus Torvalds
  1131. + *
  1132. + * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
  1133. + * make semaphores SMP safe
  1134. + * 1998-11-19 Implemented schedule_timeout() and related stuff
  1135. + * by Andrea Arcangeli
  1136. + * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
  1137. + * hybrid priority-list and round-robin design with
  1138. + * an array-switch method of distributing timeslices
  1139. + * and per-CPU runqueues. Cleanups and useful suggestions
  1140. + * by Davide Libenzi, preemptible kernel bits by Robert Love.
  1141. + * 2003-09-03 Interactivity tuning by Con Kolivas.
  1142. + * 2004-04-02 Scheduler domains code by Nick Piggin
  1143. + * 2007-04-15 Work begun on replacing all interactivity tuning with a
  1144. + * fair scheduling design by Con Kolivas.
  1145. + * 2007-05-05 Load balancing (smp-nice) and other improvements
  1146. + * by Peter Williams
  1147. + * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
  1148. + * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
  1149. + * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
  1150. + * Thomas Gleixner, Mike Kravetz
  1151. + * now Brainfuck deadline scheduling policy by Con Kolivas deletes
  1152. + * a whole lot of those previous things.
  1153. + */
  1154. +
  1155. +#include <linux/mm.h>
  1156. +#include <linux/module.h>
  1157. +#include <linux/nmi.h>
  1158. +#include <linux/init.h>
  1159. +#include <asm/uaccess.h>
  1160. +#include <linux/highmem.h>
  1161. +#include <asm/mmu_context.h>
  1162. +#include <linux/interrupt.h>
  1163. +#include <linux/capability.h>
  1164. +#include <linux/completion.h>
  1165. +#include <linux/kernel_stat.h>
  1166. +#include <linux/debug_locks.h>
  1167. +#include <linux/perf_event.h>
  1168. +#include <linux/security.h>
  1169. +#include <linux/notifier.h>
  1170. +#include <linux/profile.h>
  1171. +#include <linux/freezer.h>
  1172. +#include <linux/vmalloc.h>
  1173. +#include <linux/blkdev.h>
  1174. +#include <linux/delay.h>
  1175. +#include <linux/smp.h>
  1176. +#include <linux/threads.h>
  1177. +#include <linux/timer.h>
  1178. +#include <linux/rcupdate.h>
  1179. +#include <linux/cpu.h>
  1180. +#include <linux/cpuset.h>
  1181. +#include <linux/cpumask.h>
  1182. +#include <linux/percpu.h>
  1183. +#include <linux/proc_fs.h>
  1184. +#include <linux/seq_file.h>
  1185. +#include <linux/syscalls.h>
  1186. +#include <linux/times.h>
  1187. +#include <linux/tsacct_kern.h>
  1188. +#include <linux/kprobes.h>
  1189. +#include <linux/delayacct.h>
  1190. +#include <linux/log2.h>
  1191. +#include <linux/bootmem.h>
  1192. +#include <linux/ftrace.h>
  1193. +#include <linux/slab.h>
  1194. +#include <linux/init_task.h>
  1195. +#include <linux/binfmts.h>
  1196. +
  1197. +#include <asm/switch_to.h>
  1198. +#include <asm/tlb.h>
  1199. +#include <asm/unistd.h>
  1200. +#include <asm/mutex.h>
  1201. +#ifdef CONFIG_PARAVIRT
  1202. +#include <asm/paravirt.h>
  1203. +#endif
  1204. +
  1205. +#include "cpupri.h"
  1206. +#include "../workqueue_sched.h"
  1207. +
  1208. +#define CREATE_TRACE_POINTS
  1209. +#include <trace/events/sched.h>
  1210. +
  1211. +#define rt_prio(prio) unlikely((prio) < MAX_RT_PRIO)
  1212. +#define rt_task(p) rt_prio((p)->prio)
  1213. +#define rt_queue(rq) rt_prio((rq)->rq_prio)
  1214. +#define batch_task(p) (unlikely((p)->policy == SCHED_BATCH))
  1215. +#define is_rt_policy(policy) ((policy) == SCHED_FIFO || \
  1216. + (policy) == SCHED_RR)
  1217. +#define has_rt_policy(p) unlikely(is_rt_policy((p)->policy))
  1218. +#define idleprio_task(p) unlikely((p)->policy == SCHED_IDLEPRIO)
  1219. +#define iso_task(p) unlikely((p)->policy == SCHED_ISO)
  1220. +#define iso_queue(rq) unlikely((rq)->rq_policy == SCHED_ISO)
  1221. +#define rq_running_iso(rq) ((rq)->rq_prio == ISO_PRIO)
  1222. +
  1223. +#define ISO_PERIOD ((5 * HZ * grq.noc) + 1)
  1224. +
  1225. +/*
  1226. + * Convert user-nice values [ -20 ... 0 ... 19 ]
  1227. + * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  1228. + * and back.
  1229. + */
  1230. +#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  1231. +#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  1232. +#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  1233. +
  1234. +/*
  1235. + * 'User priority' is the nice value converted to something we
  1236. + * can work with better when scaling various scheduler parameters,
  1237. + * it's a [ 0 ... 39 ] range.
  1238. + */
  1239. +#define USER_PRIO(p) ((p) - MAX_RT_PRIO)
  1240. +#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  1241. +#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  1242. +#define SCHED_PRIO(p) ((p) + MAX_RT_PRIO)
  1243. +#define STOP_PRIO (MAX_RT_PRIO - 1)
  1244. +
  1245. +/*
  1246. + * Some helpers for converting to/from various scales. Use shifts to get
  1247. + * approximate multiples of ten for less overhead.
  1248. + */
  1249. +#define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
  1250. +#define JIFFY_NS (1000000000 / HZ)
  1251. +#define HALF_JIFFY_NS (1000000000 / HZ / 2)
  1252. +#define HALF_JIFFY_US (1000000 / HZ / 2)
  1253. +#define MS_TO_NS(TIME) ((TIME) << 20)
  1254. +#define MS_TO_US(TIME) ((TIME) << 10)
  1255. +#define NS_TO_MS(TIME) ((TIME) >> 20)
  1256. +#define NS_TO_US(TIME) ((TIME) >> 10)
  1257. +
  1258. +#define RESCHED_US (100) /* Reschedule if less than this many μs left */
  1259. +
  1260. +void print_scheduler_version(void)
  1261. +{
  1262. + printk(KERN_INFO "BFS CPU scheduler v0.424 by Con Kolivas.\n");
  1263. +}
  1264. +
  1265. +/*
  1266. + * This is the time all tasks within the same priority round robin.
  1267. + * Value is in ms and set to a minimum of 6ms. Scales with number of cpus.
  1268. + * Tunable via /proc interface.
  1269. + */
  1270. +int rr_interval __read_mostly = 6;
  1271. +
  1272. +/*
  1273. + * sched_iso_cpu - sysctl which determines the cpu percentage SCHED_ISO tasks
  1274. + * are allowed to run five seconds as real time tasks. This is the total over
  1275. + * all online cpus.
  1276. + */
  1277. +int sched_iso_cpu __read_mostly = 70;
  1278. +
  1279. +/*
  1280. + * The relative length of deadline for each priority(nice) level.
  1281. + */
  1282. +static int prio_ratios[PRIO_RANGE] __read_mostly;
  1283. +
  1284. +/*
  1285. + * The quota handed out to tasks of all priority levels when refilling their
  1286. + * time_slice.
  1287. + */
  1288. +static inline int timeslice(void)
  1289. +{
  1290. + return MS_TO_US(rr_interval);
  1291. +}
  1292. +
  1293. +/*
  1294. + * The global runqueue data that all CPUs work off. Data is protected either
  1295. + * by the global grq lock, or the discrete lock that precedes the data in this
  1296. + * struct.
  1297. + */
  1298. +struct global_rq {
  1299. + raw_spinlock_t lock;
  1300. + unsigned long nr_running;
  1301. + unsigned long nr_uninterruptible;
  1302. + unsigned long long nr_switches;
  1303. + struct list_head queue[PRIO_LIMIT];
  1304. + DECLARE_BITMAP(prio_bitmap, PRIO_LIMIT + 1);
  1305. +#ifdef CONFIG_SMP
  1306. + unsigned long qnr; /* queued not running */
  1307. + cpumask_t cpu_idle_map;
  1308. + bool idle_cpus;
  1309. +#endif
  1310. + int noc; /* num_online_cpus stored and updated when it changes */
  1311. + u64 niffies; /* Nanosecond jiffies */
  1312. + unsigned long last_jiffy; /* Last jiffy we updated niffies */
  1313. +
  1314. + raw_spinlock_t iso_lock;
  1315. + int iso_ticks;
  1316. + bool iso_refractory;
  1317. +};
  1318. +
  1319. +#ifdef CONFIG_SMP
  1320. +
  1321. +/*
  1322. + * We add the notion of a root-domain which will be used to define per-domain
  1323. + * variables. Each exclusive cpuset essentially defines an island domain by
  1324. + * fully partitioning the member cpus from any other cpuset. Whenever a new
  1325. + * exclusive cpuset is created, we also create and attach a new root-domain
  1326. + * object.
  1327. + *
  1328. + */
  1329. +struct root_domain {
  1330. + atomic_t refcount;
  1331. + atomic_t rto_count;
  1332. + struct rcu_head rcu;
  1333. + cpumask_var_t span;
  1334. + cpumask_var_t online;
  1335. +
  1336. + /*
  1337. + * The "RT overload" flag: it gets set if a CPU has more than
  1338. + * one runnable RT task.
  1339. + */
  1340. + cpumask_var_t rto_mask;
  1341. + struct cpupri cpupri;
  1342. +};
  1343. +
  1344. +/*
  1345. + * By default the system creates a single root-domain with all cpus as
  1346. + * members (mimicking the global state we have today).
  1347. + */
  1348. +static struct root_domain def_root_domain;
  1349. +
  1350. +#endif /* CONFIG_SMP */
  1351. +
  1352. +/* There can be only one */
  1353. +static struct global_rq grq;
  1354. +
  1355. +/*
  1356. + * This is the main, per-CPU runqueue data structure.
  1357. + * This data should only be modified by the local cpu.
  1358. + */
  1359. +struct rq {
  1360. +#ifdef CONFIG_SMP
  1361. +#ifdef CONFIG_NO_HZ
  1362. + u64 nohz_stamp;
  1363. + unsigned char in_nohz_recently;
  1364. +#endif
  1365. +#endif
  1366. +
  1367. + struct task_struct *curr, *idle, *stop;
  1368. + struct mm_struct *prev_mm;
  1369. +
  1370. + /* Stored data about rq->curr to work outside grq lock */
  1371. + u64 rq_deadline;
  1372. + unsigned int rq_policy;
  1373. + int rq_time_slice;
  1374. + u64 rq_last_ran;
  1375. + int rq_prio;
  1376. + bool rq_running; /* There is a task running */
  1377. +
  1378. + /* Accurate timekeeping data */
  1379. + u64 timekeep_clock;
  1380. + unsigned long user_pc, nice_pc, irq_pc, softirq_pc, system_pc,
  1381. + iowait_pc, idle_pc;
  1382. + long account_pc;
  1383. + atomic_t nr_iowait;
  1384. +
  1385. +#ifdef CONFIG_SMP
  1386. + int cpu; /* cpu of this runqueue */
  1387. + bool online;
  1388. + bool scaling; /* This CPU is managed by a scaling CPU freq governor */
  1389. + struct task_struct *sticky_task;
  1390. +
  1391. + struct root_domain *rd;
  1392. + struct sched_domain *sd;
  1393. + int *cpu_locality; /* CPU relative cache distance */
  1394. +#ifdef CONFIG_SCHED_SMT
  1395. + bool (*siblings_idle)(int cpu);
  1396. + /* See if all smt siblings are idle */
  1397. + cpumask_t smt_siblings;
  1398. +#endif
  1399. +#ifdef CONFIG_SCHED_MC
  1400. + bool (*cache_idle)(int cpu);
  1401. + /* See if all cache siblings are idle */
  1402. + cpumask_t cache_siblings;
  1403. +#endif
  1404. + u64 last_niffy; /* Last time this RQ updated grq.niffies */
  1405. +#endif
  1406. +#ifdef CONFIG_IRQ_TIME_ACCOUNTING
  1407. + u64 prev_irq_time;
  1408. +#endif
  1409. +#ifdef CONFIG_PARAVIRT
  1410. + u64 prev_steal_time;
  1411. +#endif
  1412. +#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  1413. + u64 prev_steal_time_rq;
  1414. +#endif
  1415. +
  1416. + u64 clock, old_clock, last_tick;
  1417. + u64 clock_task;
  1418. + bool dither;
  1419. +
  1420. +#ifdef CONFIG_SCHEDSTATS
  1421. +
  1422. + /* latency stats */
  1423. + struct sched_info rq_sched_info;
  1424. + unsigned long long rq_cpu_time;
  1425. + /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
  1426. +
  1427. + /* sys_sched_yield() stats */
  1428. + unsigned int yld_count;
  1429. +
  1430. + /* schedule() stats */
  1431. + unsigned int sched_switch;
  1432. + unsigned int sched_count;
  1433. + unsigned int sched_goidle;
  1434. +
  1435. + /* try_to_wake_up() stats */
  1436. + unsigned int ttwu_count;
  1437. + unsigned int ttwu_local;
  1438. +#endif
  1439. +};
  1440. +
  1441. +DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  1442. +static DEFINE_MUTEX(sched_hotcpu_mutex);
  1443. +
  1444. +#ifdef CONFIG_SMP
  1445. +/*
  1446. + * sched_domains_mutex serialises calls to init_sched_domains,
  1447. + * detach_destroy_domains and partition_sched_domains.
  1448. + */
  1449. +static DEFINE_MUTEX(sched_domains_mutex);
  1450. +
  1451. +/*
  1452. + * By default the system creates a single root-domain with all cpus as
  1453. + * members (mimicking the global state we have today).
  1454. + */
  1455. +static struct root_domain def_root_domain;
  1456. +
  1457. +int __weak arch_sd_sibling_asym_packing(void)
  1458. +{
  1459. + return 0*SD_ASYM_PACKING;
  1460. +}
  1461. +#endif
  1462. +
  1463. +#define rcu_dereference_check_sched_domain(p) \
  1464. + rcu_dereference_check((p), \
  1465. + lockdep_is_held(&sched_domains_mutex))
  1466. +
  1467. +/*
  1468. + * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  1469. + * See detach_destroy_domains: synchronize_sched for details.
  1470. + *
  1471. + * The domain tree of any CPU may only be accessed from within
  1472. + * preempt-disabled sections.
  1473. + */
  1474. +#define for_each_domain(cpu, __sd) \
  1475. + for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
  1476. +
  1477. +static inline void update_rq_clock(struct rq *rq);
  1478. +
  1479. +/*
  1480. + * Sanity check should sched_clock return bogus values. We make sure it does
  1481. + * not appear to go backwards, and use jiffies to determine the maximum and
  1482. + * minimum it could possibly have increased, and round down to the nearest
  1483. + * jiffy when it falls outside this.
  1484. + */
  1485. +static inline void niffy_diff(s64 *niff_diff, int jiff_diff)
  1486. +{
  1487. + unsigned long min_diff, max_diff;
  1488. +
  1489. + if (jiff_diff > 1)
  1490. + min_diff = JIFFIES_TO_NS(jiff_diff - 1);
  1491. + else
  1492. + min_diff = 1;
  1493. + /* Round up to the nearest tick for maximum */
  1494. + max_diff = JIFFIES_TO_NS(jiff_diff + 1);
  1495. +
  1496. + if (unlikely(*niff_diff < min_diff || *niff_diff > max_diff))
  1497. + *niff_diff = min_diff;
  1498. +}
  1499. +
  1500. +#ifdef CONFIG_SMP
  1501. +#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  1502. +#define this_rq() (&__get_cpu_var(runqueues))
  1503. +#define task_rq(p) cpu_rq(task_cpu(p))
  1504. +#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  1505. +static inline int cpu_of(struct rq *rq)
  1506. +{
  1507. + return rq->cpu;
  1508. +}
  1509. +
  1510. +/*
  1511. + * Niffies are a globally increasing nanosecond counter. Whenever a runqueue
  1512. + * clock is updated with the grq.lock held, it is an opportunity to update the
  1513. + * niffies value. Any CPU can update it by adding how much its clock has
  1514. + * increased since it last updated niffies, minus any added niffies by other
  1515. + * CPUs.
  1516. + */
  1517. +static inline void update_clocks(struct rq *rq)
  1518. +{
  1519. + s64 ndiff;
  1520. + long jdiff;
  1521. +
  1522. + update_rq_clock(rq);
  1523. + ndiff = rq->clock - rq->old_clock;
  1524. + /* old_clock is only updated when we are updating niffies */
  1525. + rq->old_clock = rq->clock;
  1526. + ndiff -= grq.niffies - rq->last_niffy;
  1527. + jdiff = jiffies - grq.last_jiffy;
  1528. + niffy_diff(&ndiff, jdiff);
  1529. + grq.last_jiffy += jdiff;
  1530. + grq.niffies += ndiff;
  1531. + rq->last_niffy = grq.niffies;
  1532. +}
  1533. +#else /* CONFIG_SMP */
  1534. +static struct rq *uprq;
  1535. +#define cpu_rq(cpu) (uprq)
  1536. +#define this_rq() (uprq)
  1537. +#define task_rq(p) (uprq)
  1538. +#define cpu_curr(cpu) ((uprq)->curr)
  1539. +static inline int cpu_of(struct rq *rq)
  1540. +{
  1541. + return 0;
  1542. +}
  1543. +
  1544. +static inline void update_clocks(struct rq *rq)
  1545. +{
  1546. + s64 ndiff;
  1547. + long jdiff;
  1548. +
  1549. + update_rq_clock(rq);
  1550. + ndiff = rq->clock - rq->old_clock;
  1551. + rq->old_clock = rq->clock;
  1552. + jdiff = jiffies - grq.last_jiffy;
  1553. + niffy_diff(&ndiff, jdiff);
  1554. + grq.last_jiffy += jdiff;
  1555. + grq.niffies += ndiff;
  1556. +}
  1557. +#endif
  1558. +#define raw_rq() (&__raw_get_cpu_var(runqueues))
  1559. +
  1560. +#include "stats.h"
  1561. +
  1562. +#ifndef prepare_arch_switch
  1563. +# define prepare_arch_switch(next) do { } while (0)
  1564. +#endif
  1565. +#ifndef finish_arch_switch
  1566. +# define finish_arch_switch(prev) do { } while (0)
  1567. +#endif
  1568. +#ifndef finish_arch_post_lock_switch
  1569. +# define finish_arch_post_lock_switch() do { } while (0)
  1570. +#endif
  1571. +
  1572. +/*
  1573. + * All common locking functions performed on grq.lock. rq->clock is local to
  1574. + * the CPU accessing it so it can be modified just with interrupts disabled
  1575. + * when we're not updating niffies.
  1576. + * Looking up task_rq must be done under grq.lock to be safe.
  1577. + */
  1578. +static void update_rq_clock_task(struct rq *rq, s64 delta);
  1579. +
  1580. +static inline void update_rq_clock(struct rq *rq)
  1581. +{
  1582. + s64 delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
  1583. +
  1584. + rq->clock += delta;
  1585. + update_rq_clock_task(rq, delta);
  1586. +}
  1587. +
  1588. +static inline bool task_running(struct task_struct *p)
  1589. +{
  1590. + return p->on_cpu;
  1591. +}
  1592. +
  1593. +static inline void grq_lock(void)
  1594. + __acquires(grq.lock)
  1595. +{
  1596. + raw_spin_lock(&grq.lock);
  1597. +}
  1598. +
  1599. +static inline void grq_unlock(void)
  1600. + __releases(grq.lock)
  1601. +{
  1602. + raw_spin_unlock(&grq.lock);
  1603. +}
  1604. +
  1605. +static inline void grq_lock_irq(void)
  1606. + __acquires(grq.lock)
  1607. +{
  1608. + raw_spin_lock_irq(&grq.lock);
  1609. +}
  1610. +
  1611. +static inline void time_lock_grq(struct rq *rq)
  1612. + __acquires(grq.lock)
  1613. +{
  1614. + grq_lock();
  1615. + update_clocks(rq);
  1616. +}
  1617. +
  1618. +static inline void grq_unlock_irq(void)
  1619. + __releases(grq.lock)
  1620. +{
  1621. + raw_spin_unlock_irq(&grq.lock);
  1622. +}
  1623. +
  1624. +static inline void grq_lock_irqsave(unsigned long *flags)
  1625. + __acquires(grq.lock)
  1626. +{
  1627. + raw_spin_lock_irqsave(&grq.lock, *flags);
  1628. +}
  1629. +
  1630. +static inline void grq_unlock_irqrestore(unsigned long *flags)
  1631. + __releases(grq.lock)
  1632. +{
  1633. + raw_spin_unlock_irqrestore(&grq.lock, *flags);
  1634. +}
  1635. +
  1636. +static inline struct rq
  1637. +*task_grq_lock(struct task_struct *p, unsigned long *flags)
  1638. + __acquires(grq.lock)
  1639. +{
  1640. + grq_lock_irqsave(flags);
  1641. + return task_rq(p);
  1642. +}
  1643. +
  1644. +static inline struct rq
  1645. +*time_task_grq_lock(struct task_struct *p, unsigned long *flags)
  1646. + __acquires(grq.lock)
  1647. +{
  1648. + struct rq *rq = task_grq_lock(p, flags);
  1649. + update_clocks(rq);
  1650. + return rq;
  1651. +}
  1652. +
  1653. +static inline struct rq *task_grq_lock_irq(struct task_struct *p)
  1654. + __acquires(grq.lock)
  1655. +{
  1656. + grq_lock_irq();
  1657. + return task_rq(p);
  1658. +}
  1659. +
  1660. +static inline void time_task_grq_lock_irq(struct task_struct *p)
  1661. + __acquires(grq.lock)
  1662. +{
  1663. + struct rq *rq = task_grq_lock_irq(p);
  1664. + update_clocks(rq);
  1665. +}
  1666. +
  1667. +static inline void task_grq_unlock_irq(void)
  1668. + __releases(grq.lock)
  1669. +{
  1670. + grq_unlock_irq();
  1671. +}
  1672. +
  1673. +static inline void task_grq_unlock(unsigned long *flags)
  1674. + __releases(grq.lock)
  1675. +{
  1676. + grq_unlock_irqrestore(flags);
  1677. +}
  1678. +
  1679. +/**
  1680. + * grunqueue_is_locked
  1681. + *
  1682. + * Returns true if the global runqueue is locked.
  1683. + * This interface allows printk to be called with the runqueue lock
  1684. + * held and know whether or not it is OK to wake up the klogd.
  1685. + */
  1686. +bool grunqueue_is_locked(void)
  1687. +{
  1688. + return raw_spin_is_locked(&grq.lock);
  1689. +}
  1690. +
  1691. +void grq_unlock_wait(void)
  1692. + __releases(grq.lock)
  1693. +{
  1694. + smp_mb(); /* spin-unlock-wait is not a full memory barrier */
  1695. + raw_spin_unlock_wait(&grq.lock);
  1696. +}
  1697. +
  1698. +static inline void time_grq_lock(struct rq *rq, unsigned long *flags)
  1699. + __acquires(grq.lock)
  1700. +{
  1701. + local_irq_save(*flags);
  1702. + time_lock_grq(rq);
  1703. +}
  1704. +
  1705. +static inline struct rq *__task_grq_lock(struct task_struct *p)
  1706. + __acquires(grq.lock)
  1707. +{
  1708. + grq_lock();
  1709. + return task_rq(p);
  1710. +}
  1711. +
  1712. +static inline void __task_grq_unlock(void)
  1713. + __releases(grq.lock)
  1714. +{
  1715. + grq_unlock();
  1716. +}
  1717. +
  1718. +/*
  1719. + * Look for any tasks *anywhere* that are running nice 0 or better. We do
  1720. + * this lockless for overhead reasons since the occasional wrong result
  1721. + * is harmless.
  1722. + */
  1723. +bool above_background_load(void)
  1724. +{
  1725. + int cpu;
  1726. +
  1727. + for_each_online_cpu(cpu) {
  1728. + struct task_struct *cpu_curr = cpu_rq(cpu)->curr;
  1729. +
  1730. + if (unlikely(!cpu_curr))
  1731. + continue;
  1732. + if (PRIO_TO_NICE(cpu_curr->static_prio) < 1) {
  1733. + return true;
  1734. + }
  1735. + }
  1736. + return false;
  1737. +}
  1738. +
  1739. +#ifndef __ARCH_WANT_UNLOCKED_CTXSW
  1740. +static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  1741. +{
  1742. +}
  1743. +
  1744. +static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  1745. +{
  1746. +#ifdef CONFIG_DEBUG_SPINLOCK
  1747. + /* this is a valid case when another task releases the spinlock */
  1748. + grq.lock.owner = current;
  1749. +#endif
  1750. + /*
  1751. + * If we are tracking spinlock dependencies then we have to
  1752. + * fix up the runqueue lock - which gets 'carried over' from
  1753. + * prev into current:
  1754. + */
  1755. + spin_acquire(&grq.lock.dep_map, 0, 0, _THIS_IP_);
  1756. +
  1757. + grq_unlock_irq();
  1758. +}
  1759. +
  1760. +#else /* __ARCH_WANT_UNLOCKED_CTXSW */
  1761. +
  1762. +static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  1763. +{
  1764. +#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  1765. + grq_unlock_irq();
  1766. +#else
  1767. + grq_unlock();
  1768. +#endif
  1769. +}
  1770. +
  1771. +static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  1772. +{
  1773. + smp_wmb();
  1774. +#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  1775. + local_irq_enable();
  1776. +#endif
  1777. +}
  1778. +#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  1779. +
  1780. +static inline bool deadline_before(u64 deadline, u64 time)
  1781. +{
  1782. + return (deadline < time);
  1783. +}
  1784. +
  1785. +static inline bool deadline_after(u64 deadline, u64 time)
  1786. +{
  1787. + return (deadline > time);
  1788. +}
  1789. +
  1790. +/*
  1791. + * A task that is queued but not running will be on the grq run list.
  1792. + * A task that is not running or queued will not be on the grq run list.
  1793. + * A task that is currently running will have ->on_cpu set but not on the
  1794. + * grq run list.
  1795. + */
  1796. +static inline bool task_queued(struct task_struct *p)
  1797. +{
  1798. + return (!list_empty(&p->run_list));
  1799. +}
  1800. +
  1801. +/*
  1802. + * Removing from the global runqueue. Enter with grq locked.
  1803. + */
  1804. +static void dequeue_task(struct task_struct *p)
  1805. +{
  1806. + list_del_init(&p->run_list);
  1807. + if (list_empty(grq.queue + p->prio))
  1808. + __clear_bit(p->prio, grq.prio_bitmap);
  1809. +}
  1810. +
  1811. +/*
  1812. + * To determine if it's safe for a task of SCHED_IDLEPRIO to actually run as
  1813. + * an idle task, we ensure none of the following conditions are met.
  1814. + */
  1815. +static bool idleprio_suitable(struct task_struct *p)
  1816. +{
  1817. + return (!freezing(p) && !signal_pending(p) &&
  1818. + !(task_contributes_to_load(p)) && !(p->flags & (PF_EXITING)));
  1819. +}
  1820. +
  1821. +/*
  1822. + * To determine if a task of SCHED_ISO can run in pseudo-realtime, we check
  1823. + * that the iso_refractory flag is not set.
  1824. + */
  1825. +static bool isoprio_suitable(void)
  1826. +{
  1827. + return !grq.iso_refractory;
  1828. +}
  1829. +
  1830. +/*
  1831. + * Adding to the global runqueue. Enter with grq locked.
  1832. + */
  1833. +static void enqueue_task(struct task_struct *p)
  1834. +{
  1835. + if (!rt_task(p)) {
  1836. + /* Check it hasn't gotten rt from PI */
  1837. + if ((idleprio_task(p) && idleprio_suitable(p)) ||
  1838. + (iso_task(p) && isoprio_suitable()))
  1839. + p->prio = p->normal_prio;
  1840. + else
  1841. + p->prio = NORMAL_PRIO;
  1842. + }
  1843. + __set_bit(p->prio, grq.prio_bitmap);
  1844. + list_add_tail(&p->run_list, grq.queue + p->prio);
  1845. + sched_info_queued(p);
  1846. +}
  1847. +
  1848. +/* Only idle task does this as a real time task*/
  1849. +static inline void enqueue_task_head(struct task_struct *p)
  1850. +{
  1851. + __set_bit(p->prio, grq.prio_bitmap);
  1852. + list_add(&p->run_list, grq.queue + p->prio);
  1853. + sched_info_queued(p);
  1854. +}
  1855. +
  1856. +static inline void requeue_task(struct task_struct *p)
  1857. +{
  1858. + sched_info_queued(p);
  1859. +}
  1860. +
  1861. +/*
  1862. + * Returns the relative length of deadline all compared to the shortest
  1863. + * deadline which is that of nice -20.
  1864. + */
  1865. +static inline int task_prio_ratio(struct task_struct *p)
  1866. +{
  1867. + return prio_ratios[TASK_USER_PRIO(p)];
  1868. +}
  1869. +
  1870. +/*
  1871. + * task_timeslice - all tasks of all priorities get the exact same timeslice
  1872. + * length. CPU distribution is handled by giving different deadlines to
  1873. + * tasks of different priorities. Use 128 as the base value for fast shifts.
  1874. + */
  1875. +static inline int task_timeslice(struct task_struct *p)
  1876. +{
  1877. + return (rr_interval * task_prio_ratio(p) / 128);
  1878. +}
  1879. +
  1880. +#ifdef CONFIG_SMP
  1881. +/*
  1882. + * qnr is the "queued but not running" count which is the total number of
  1883. + * tasks on the global runqueue list waiting for cpu time but not actually
  1884. + * currently running on a cpu.
  1885. + */
  1886. +static inline void inc_qnr(void)
  1887. +{
  1888. + grq.qnr++;
  1889. +}
  1890. +
  1891. +static inline void dec_qnr(void)
  1892. +{
  1893. + grq.qnr--;
  1894. +}
  1895. +
  1896. +static inline int queued_notrunning(void)
  1897. +{
  1898. + return grq.qnr;
  1899. +}
  1900. +
  1901. +/*
  1902. + * The cpu_idle_map stores a bitmap of all the CPUs currently idle to
  1903. + * allow easy lookup of whether any suitable idle CPUs are available.
  1904. + * It's cheaper to maintain a binary yes/no if there are any idle CPUs on the
  1905. + * idle_cpus variable than to do a full bitmask check when we are busy.
  1906. + */
  1907. +static inline void set_cpuidle_map(int cpu)
  1908. +{
  1909. + if (likely(cpu_online(cpu))) {
  1910. + cpu_set(cpu, grq.cpu_idle_map);
  1911. + grq.idle_cpus = true;
  1912. + }
  1913. +}
  1914. +
  1915. +static inline void clear_cpuidle_map(int cpu)
  1916. +{
  1917. + cpu_clear(cpu, grq.cpu_idle_map);
  1918. + if (cpus_empty(grq.cpu_idle_map))
  1919. + grq.idle_cpus = false;
  1920. +}
  1921. +
  1922. +static bool suitable_idle_cpus(struct task_struct *p)
  1923. +{
  1924. + if (!grq.idle_cpus)
  1925. + return false;
  1926. + return (cpus_intersects(p->cpus_allowed, grq.cpu_idle_map));
  1927. +}
  1928. +
  1929. +#define CPUIDLE_DIFF_THREAD (1)
  1930. +#define CPUIDLE_DIFF_CORE (2)
  1931. +#define CPUIDLE_CACHE_BUSY (4)
  1932. +#define CPUIDLE_DIFF_CPU (8)
  1933. +#define CPUIDLE_THREAD_BUSY (16)
  1934. +#define CPUIDLE_DIFF_NODE (32)
  1935. +
  1936. +static void resched_task(struct task_struct *p);
  1937. +
  1938. +/*
  1939. + * The best idle CPU is chosen according to the CPUIDLE ranking above where the
  1940. + * lowest value would give the most suitable CPU to schedule p onto next. The
  1941. + * order works out to be the following:
  1942. + *
  1943. + * Same core, idle or busy cache, idle or busy threads
  1944. + * Other core, same cache, idle or busy cache, idle threads.
  1945. + * Same node, other CPU, idle cache, idle threads.
  1946. + * Same node, other CPU, busy cache, idle threads.
  1947. + * Other core, same cache, busy threads.
  1948. + * Same node, other CPU, busy threads.
  1949. + * Other node, other CPU, idle cache, idle threads.
  1950. + * Other node, other CPU, busy cache, idle threads.
  1951. + * Other node, other CPU, busy threads.
  1952. + */
  1953. +static void
  1954. +resched_best_mask(int best_cpu, struct rq *rq, cpumask_t *tmpmask)
  1955. +{
  1956. + unsigned int best_ranking = CPUIDLE_DIFF_NODE | CPUIDLE_THREAD_BUSY |
  1957. + CPUIDLE_DIFF_CPU | CPUIDLE_CACHE_BUSY | CPUIDLE_DIFF_CORE |
  1958. + CPUIDLE_DIFF_THREAD;
  1959. + int cpu_tmp;
  1960. +
  1961. + if (cpu_isset(best_cpu, *tmpmask))
  1962. + goto out;
  1963. +
  1964. + for_each_cpu_mask(cpu_tmp, *tmpmask) {
  1965. + unsigned int ranking;
  1966. + struct rq *tmp_rq;
  1967. +
  1968. + ranking = 0;
  1969. + tmp_rq = cpu_rq(cpu_tmp);
  1970. +
  1971. +#ifdef CONFIG_NUMA
  1972. + if (rq->cpu_locality[cpu_tmp] > 3)
  1973. + ranking |= CPUIDLE_DIFF_NODE;
  1974. + else
  1975. +#endif
  1976. + if (rq->cpu_locality[cpu_tmp] > 2)
  1977. + ranking |= CPUIDLE_DIFF_CPU;
  1978. +#ifdef CONFIG_SCHED_MC
  1979. + if (rq->cpu_locality[cpu_tmp] == 2)
  1980. + ranking |= CPUIDLE_DIFF_CORE;
  1981. + if (!(tmp_rq->cache_idle(cpu_tmp)))
  1982. + ranking |= CPUIDLE_CACHE_BUSY;
  1983. +#endif
  1984. +#ifdef CONFIG_SCHED_SMT
  1985. + if (rq->cpu_locality[cpu_tmp] == 1)
  1986. + ranking |= CPUIDLE_DIFF_THREAD;
  1987. + if (!(tmp_rq->siblings_idle(cpu_tmp)))
  1988. + ranking |= CPUIDLE_THREAD_BUSY;
  1989. +#endif
  1990. + if (ranking < best_ranking) {
  1991. + best_cpu = cpu_tmp;
  1992. + best_ranking = ranking;
  1993. + }
  1994. + }
  1995. +out:
  1996. + resched_task(cpu_rq(best_cpu)->curr);
  1997. +}
  1998. +
  1999. +bool cpus_share_cache(int this_cpu, int that_cpu)
  2000. +{
  2001. + struct rq *this_rq = cpu_rq(this_cpu);
  2002. +
  2003. + return (this_rq->cpu_locality[that_cpu] < 2);
  2004. +}
  2005. +
  2006. +static void resched_best_idle(struct task_struct *p)
  2007. +{
  2008. + cpumask_t tmpmask;
  2009. +
  2010. + cpus_and(tmpmask, p->cpus_allowed, grq.cpu_idle_map);
  2011. + resched_best_mask(task_cpu(p), task_rq(p), &tmpmask);
  2012. +}
  2013. +
  2014. +static inline void resched_suitable_idle(struct task_struct *p)
  2015. +{
  2016. + if (suitable_idle_cpus(p))
  2017. + resched_best_idle(p);
  2018. +}
  2019. +/*
  2020. + * Flags to tell us whether this CPU is running a CPU frequency governor that
  2021. + * has slowed its speed or not. No locking required as the very rare wrongly
  2022. + * read value would be harmless.
  2023. + */
  2024. +void cpu_scaling(int cpu)
  2025. +{
  2026. + cpu_rq(cpu)->scaling = true;
  2027. +}
  2028. +
  2029. +void cpu_nonscaling(int cpu)
  2030. +{
  2031. + cpu_rq(cpu)->scaling = false;
  2032. +}
  2033. +
  2034. +static inline bool scaling_rq(struct rq *rq)
  2035. +{
  2036. + return rq->scaling;
  2037. +}
  2038. +
  2039. +static inline int locality_diff(struct task_struct *p, struct rq *rq)
  2040. +{
  2041. + return rq->cpu_locality[task_cpu(p)];
  2042. +}
  2043. +#else /* CONFIG_SMP */
  2044. +static inline void inc_qnr(void)
  2045. +{
  2046. +}
  2047. +
  2048. +static inline void dec_qnr(void)
  2049. +{
  2050. +}
  2051. +
  2052. +static inline int queued_notrunning(void)
  2053. +{
  2054. + return grq.nr_running;
  2055. +}
  2056. +
  2057. +static inline void set_cpuidle_map(int cpu)
  2058. +{
  2059. +}
  2060. +
  2061. +static inline void clear_cpuidle_map(int cpu)
  2062. +{
  2063. +}
  2064. +
  2065. +static inline bool suitable_idle_cpus(struct task_struct *p)
  2066. +{
  2067. + return uprq->curr == uprq->idle;
  2068. +}
  2069. +
  2070. +static inline void resched_suitable_idle(struct task_struct *p)
  2071. +{
  2072. +}
  2073. +
  2074. +void cpu_scaling(int __unused)
  2075. +{
  2076. +}
  2077. +
  2078. +void cpu_nonscaling(int __unused)
  2079. +{
  2080. +}
  2081. +
  2082. +/*
  2083. + * Although CPUs can scale in UP, there is nowhere else for tasks to go so this
  2084. + * always returns 0.
  2085. + */
  2086. +static inline bool scaling_rq(struct rq *rq)
  2087. +{
  2088. + return false;
  2089. +}
  2090. +
  2091. +static inline int locality_diff(struct task_struct *p, struct rq *rq)
  2092. +{
  2093. + return 0;
  2094. +}
  2095. +#endif /* CONFIG_SMP */
  2096. +EXPORT_SYMBOL_GPL(cpu_scaling);
  2097. +EXPORT_SYMBOL_GPL(cpu_nonscaling);
  2098. +
  2099. +/*
  2100. + * activate_idle_task - move idle task to the _front_ of runqueue.
  2101. + */
  2102. +static inline void activate_idle_task(struct task_struct *p)
  2103. +{
  2104. + enqueue_task_head(p);
  2105. + grq.nr_running++;
  2106. + inc_qnr();
  2107. +}
  2108. +
  2109. +static inline int normal_prio(struct task_struct *p)
  2110. +{
  2111. + if (has_rt_policy(p))
  2112. + return MAX_RT_PRIO - 1 - p->rt_priority;
  2113. + if (idleprio_task(p))
  2114. + return IDLE_PRIO;
  2115. + if (iso_task(p))
  2116. + return ISO_PRIO;
  2117. + return NORMAL_PRIO;
  2118. +}
  2119. +
  2120. +/*
  2121. + * Calculate the current priority, i.e. the priority
  2122. + * taken into account by the scheduler. This value might
  2123. + * be boosted by RT tasks as it will be RT if the task got
  2124. + * RT-boosted. If not then it returns p->normal_prio.
  2125. + */
  2126. +static int effective_prio(struct task_struct *p)
  2127. +{
  2128. + p->normal_prio = normal_prio(p);
  2129. + /*
  2130. + * If we are RT tasks or we were boosted to RT priority,
  2131. + * keep the priority unchanged. Otherwise, update priority
  2132. + * to the normal priority:
  2133. + */
  2134. + if (!rt_prio(p->prio))
  2135. + return p->normal_prio;
  2136. + return p->prio;
  2137. +}
  2138. +
  2139. +/*
  2140. + * activate_task - move a task to the runqueue. Enter with grq locked.
  2141. + */
  2142. +static void activate_task(struct task_struct *p, struct rq *rq)
  2143. +{
  2144. + update_clocks(rq);
  2145. +
  2146. + /*
  2147. + * Sleep time is in units of nanosecs, so shift by 20 to get a
  2148. + * milliseconds-range estimation of the amount of time that the task
  2149. + * spent sleeping:
  2150. + */
  2151. + if (unlikely(prof_on == SLEEP_PROFILING)) {
  2152. + if (p->state == TASK_UNINTERRUPTIBLE)
  2153. + profile_hits(SLEEP_PROFILING, (void *)get_wchan(p),
  2154. + (rq->clock - p->last_ran) >> 20);
  2155. + }
  2156. +
  2157. + p->prio = effective_prio(p);
  2158. + if (task_contributes_to_load(p))
  2159. + grq.nr_uninterruptible--;
  2160. + enqueue_task(p);
  2161. + grq.nr_running++;
  2162. + inc_qnr();
  2163. +}
  2164. +
  2165. +static inline void clear_sticky(struct task_struct *p);
  2166. +
  2167. +/*
  2168. + * deactivate_task - If it's running, it's not on the grq and we can just
  2169. + * decrement the nr_running. Enter with grq locked.
  2170. + */
  2171. +static inline void deactivate_task(struct task_struct *p)
  2172. +{
  2173. + if (task_contributes_to_load(p))
  2174. + grq.nr_uninterruptible++;
  2175. + grq.nr_running--;
  2176. + clear_sticky(p);
  2177. +}
  2178. +
  2179. +#ifdef CONFIG_SMP
  2180. +void set_task_cpu(struct task_struct *p, unsigned int cpu)
  2181. +{
  2182. +#ifdef CONFIG_LOCKDEP
  2183. + /*
  2184. + * The caller should hold grq lock.
  2185. + */
  2186. + WARN_ON_ONCE(debug_locks && !lockdep_is_held(&grq.lock));
  2187. +#endif
  2188. + trace_sched_migrate_task(p, cpu);
  2189. + if (task_cpu(p) != cpu)
  2190. + perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0);
  2191. +
  2192. + /*
  2193. + * After ->cpu is set up to a new value, task_grq_lock(p, ...) can be
  2194. + * successfully executed on another CPU. We must ensure that updates of
  2195. + * per-task data have been completed by this moment.
  2196. + */
  2197. + smp_wmb();
  2198. + task_thread_info(p)->cpu = cpu;
  2199. +}
  2200. +
  2201. +static inline void clear_sticky(struct task_struct *p)
  2202. +{
  2203. + p->sticky = false;
  2204. +}
  2205. +
  2206. +static inline bool task_sticky(struct task_struct *p)
  2207. +{
  2208. + return p->sticky;
  2209. +}
  2210. +
  2211. +/* Reschedule the best idle CPU that is not this one. */
  2212. +static void
  2213. +resched_closest_idle(struct rq *rq, int cpu, struct task_struct *p)
  2214. +{
  2215. + cpumask_t tmpmask;
  2216. +
  2217. + cpus_and(tmpmask, p->cpus_allowed, grq.cpu_idle_map);
  2218. + cpu_clear(cpu, tmpmask);
  2219. + if (cpus_empty(tmpmask))
  2220. + return;
  2221. + resched_best_mask(cpu, rq, &tmpmask);
  2222. +}
  2223. +
  2224. +/*
  2225. + * We set the sticky flag on a task that is descheduled involuntarily meaning
  2226. + * it is awaiting further CPU time. If the last sticky task is still sticky
  2227. + * but unlucky enough to not be the next task scheduled, we unstick it and try
  2228. + * to find it an idle CPU. Realtime tasks do not stick to minimise their
  2229. + * latency at all times.
  2230. + */
  2231. +static inline void
  2232. +swap_sticky(struct rq *rq, int cpu, struct task_struct *p)
  2233. +{
  2234. + if (rq->sticky_task) {
  2235. + if (rq->sticky_task == p) {
  2236. + p->sticky = true;
  2237. + return;
  2238. + }
  2239. + if (task_sticky(rq->sticky_task)) {
  2240. + clear_sticky(rq->sticky_task);
  2241. + resched_closest_idle(rq, cpu, rq->sticky_task);
  2242. + }
  2243. + }
  2244. + if (!rt_task(p)) {
  2245. + p->sticky = true;
  2246. + rq->sticky_task = p;
  2247. + } else {
  2248. + resched_closest_idle(rq, cpu, p);
  2249. + rq->sticky_task = NULL;
  2250. + }
  2251. +}
  2252. +
  2253. +static inline void unstick_task(struct rq *rq, struct task_struct *p)
  2254. +{
  2255. + rq->sticky_task = NULL;
  2256. + clear_sticky(p);
  2257. +}
  2258. +#else
  2259. +static inline void clear_sticky(struct task_struct *p)
  2260. +{
  2261. +}
  2262. +
  2263. +static inline bool task_sticky(struct task_struct *p)
  2264. +{
  2265. + return false;
  2266. +}
  2267. +
  2268. +static inline void
  2269. +swap_sticky(struct rq *rq, int cpu, struct task_struct *p)
  2270. +{
  2271. +}
  2272. +
  2273. +static inline void unstick_task(struct rq *rq, struct task_struct *p)
  2274. +{
  2275. +}
  2276. +#endif
  2277. +
  2278. +/*
  2279. + * Move a task off the global queue and take it to a cpu for it will
  2280. + * become the running task.
  2281. + */
  2282. +static inline void take_task(int cpu, struct task_struct *p)
  2283. +{
  2284. + set_task_cpu(p, cpu);
  2285. + dequeue_task(p);
  2286. + clear_sticky(p);
  2287. + dec_qnr();
  2288. +}
  2289. +
  2290. +/*
  2291. + * Returns a descheduling task to the grq runqueue unless it is being
  2292. + * deactivated.
  2293. + */
  2294. +static inline void return_task(struct task_struct *p, bool deactivate)
  2295. +{
  2296. + if (deactivate)
  2297. + deactivate_task(p);
  2298. + else {
  2299. + inc_qnr();
  2300. + enqueue_task(p);
  2301. + }
  2302. +}
  2303. +
  2304. +/*
  2305. + * resched_task - mark a task 'to be rescheduled now'.
  2306. + *
  2307. + * On UP this means the setting of the need_resched flag, on SMP it
  2308. + * might also involve a cross-CPU call to trigger the scheduler on
  2309. + * the target CPU.
  2310. + */
  2311. +#ifdef CONFIG_SMP
  2312. +
  2313. +#ifndef tsk_is_polling
  2314. +#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
  2315. +#endif
  2316. +
  2317. +static void resched_task(struct task_struct *p)
  2318. +{
  2319. + int cpu;
  2320. +
  2321. + assert_raw_spin_locked(&grq.lock);
  2322. +
  2323. + if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
  2324. + return;
  2325. +
  2326. + set_tsk_thread_flag(p, TIF_NEED_RESCHED);
  2327. +
  2328. + cpu = task_cpu(p);
  2329. + if (cpu == smp_processor_id())
  2330. + return;
  2331. +
  2332. + /* NEED_RESCHED must be visible before we test polling */
  2333. + smp_mb();
  2334. + if (!tsk_is_polling(p))
  2335. + smp_send_reschedule(cpu);
  2336. +}
  2337. +
  2338. +#else
  2339. +static inline void resched_task(struct task_struct *p)
  2340. +{
  2341. + assert_raw_spin_locked(&grq.lock);
  2342. + set_tsk_need_resched(p);
  2343. +}
  2344. +#endif
  2345. +
  2346. +/**
  2347. + * task_curr - is this task currently executing on a CPU?
  2348. + * @p: the task in question.
  2349. + */
  2350. +inline int task_curr(const struct task_struct *p)
  2351. +{
  2352. + return cpu_curr(task_cpu(p)) == p;
  2353. +}
  2354. +
  2355. +#ifdef CONFIG_SMP
  2356. +struct migration_req {
  2357. + struct task_struct *task;
  2358. + int dest_cpu;
  2359. +};
  2360. +
  2361. +/*
  2362. + * wait_task_inactive - wait for a thread to unschedule.
  2363. + *
  2364. + * If @match_state is nonzero, it's the @p->state value just checked and
  2365. + * not expected to change. If it changes, i.e. @p might have woken up,
  2366. + * then return zero. When we succeed in waiting for @p to be off its CPU,
  2367. + * we return a positive number (its total switch count). If a second call
  2368. + * a short while later returns the same number, the caller can be sure that
  2369. + * @p has remained unscheduled the whole time.
  2370. + *
  2371. + * The caller must ensure that the task *will* unschedule sometime soon,
  2372. + * else this function might spin for a *long* time. This function can't
  2373. + * be called with interrupts off, or it may introduce deadlock with
  2374. + * smp_call_function() if an IPI is sent by the same process we are
  2375. + * waiting to become inactive.
  2376. + */
  2377. +unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  2378. +{
  2379. + unsigned long flags;
  2380. + bool running, on_rq;
  2381. + unsigned long ncsw;
  2382. + struct rq *rq;
  2383. +
  2384. + for (;;) {
  2385. + /*
  2386. + * We do the initial early heuristics without holding
  2387. + * any task-queue locks at all. We'll only try to get
  2388. + * the runqueue lock when things look like they will
  2389. + * work out! In the unlikely event rq is dereferenced
  2390. + * since we're lockless, grab it again.
  2391. + */
  2392. +#ifdef CONFIG_SMP
  2393. +retry_rq:
  2394. + rq = task_rq(p);
  2395. + if (unlikely(!rq))
  2396. + goto retry_rq;
  2397. +#else /* CONFIG_SMP */
  2398. + rq = task_rq(p);
  2399. +#endif
  2400. + /*
  2401. + * If the task is actively running on another CPU
  2402. + * still, just relax and busy-wait without holding
  2403. + * any locks.
  2404. + *
  2405. + * NOTE! Since we don't hold any locks, it's not
  2406. + * even sure that "rq" stays as the right runqueue!
  2407. + * But we don't care, since this will return false
  2408. + * if the runqueue has changed and p is actually now
  2409. + * running somewhere else!
  2410. + */
  2411. + while (task_running(p) && p == rq->curr) {
  2412. + if (match_state && unlikely(p->state != match_state))
  2413. + return 0;
  2414. + cpu_relax();
  2415. + }
  2416. +
  2417. + /*
  2418. + * Ok, time to look more closely! We need the grq
  2419. + * lock now, to be *sure*. If we're wrong, we'll
  2420. + * just go back and repeat.
  2421. + */
  2422. + rq = task_grq_lock(p, &flags);
  2423. + trace_sched_wait_task(p);
  2424. + running = task_running(p);
  2425. + on_rq = task_queued(p);
  2426. + ncsw = 0;
  2427. + if (!match_state || p->state == match_state)
  2428. + ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  2429. + task_grq_unlock(&flags);
  2430. +
  2431. + /*
  2432. + * If it changed from the expected state, bail out now.
  2433. + */
  2434. + if (unlikely(!ncsw))
  2435. + break;
  2436. +
  2437. + /*
  2438. + * Was it really running after all now that we
  2439. + * checked with the proper locks actually held?
  2440. + *
  2441. + * Oops. Go back and try again..
  2442. + */
  2443. + if (unlikely(running)) {
  2444. + cpu_relax();
  2445. + continue;
  2446. + }
  2447. +
  2448. + /*
  2449. + * It's not enough that it's not actively running,
  2450. + * it must be off the runqueue _entirely_, and not
  2451. + * preempted!
  2452. + *
  2453. + * So if it was still runnable (but just not actively
  2454. + * running right now), it's preempted, and we should
  2455. + * yield - it could be a while.
  2456. + */
  2457. + if (unlikely(on_rq)) {
  2458. + ktime_t to = ktime_set(0, NSEC_PER_SEC / HZ);
  2459. +
  2460. + set_current_state(TASK_UNINTERRUPTIBLE);
  2461. + schedule_hrtimeout(&to, HRTIMER_MODE_REL);
  2462. + continue;
  2463. + }
  2464. +
  2465. + /*
  2466. + * Ahh, all good. It wasn't running, and it wasn't
  2467. + * runnable, which means that it will never become
  2468. + * running in the future either. We're all done!
  2469. + */
  2470. + break;
  2471. + }
  2472. +
  2473. + return ncsw;
  2474. +}
  2475. +
  2476. +/***
  2477. + * kick_process - kick a running thread to enter/exit the kernel
  2478. + * @p: the to-be-kicked thread
  2479. + *
  2480. + * Cause a process which is running on another CPU to enter
  2481. + * kernel-mode, without any delay. (to get signals handled.)
  2482. + *
  2483. + * NOTE: this function doesn't have to take the runqueue lock,
  2484. + * because all it wants to ensure is that the remote task enters
  2485. + * the kernel. If the IPI races and the task has been migrated
  2486. + * to another CPU then no harm is done and the purpose has been
  2487. + * achieved as well.
  2488. + */
  2489. +void kick_process(struct task_struct *p)
  2490. +{
  2491. + int cpu;
  2492. +
  2493. + preempt_disable();
  2494. + cpu = task_cpu(p);
  2495. + if ((cpu != smp_processor_id()) && task_curr(p))
  2496. + smp_send_reschedule(cpu);
  2497. + preempt_enable();
  2498. +}
  2499. +EXPORT_SYMBOL_GPL(kick_process);
  2500. +#endif
  2501. +
  2502. +#define rq_idle(rq) ((rq)->rq_prio == PRIO_LIMIT)
  2503. +
  2504. +/*
  2505. + * RT tasks preempt purely on priority. SCHED_NORMAL tasks preempt on the
  2506. + * basis of earlier deadlines. SCHED_IDLEPRIO don't preempt anything else or
  2507. + * between themselves, they cooperatively multitask. An idle rq scores as
  2508. + * prio PRIO_LIMIT so it is always preempted.
  2509. + */
  2510. +static inline bool
  2511. +can_preempt(struct task_struct *p, int prio, u64 deadline)
  2512. +{
  2513. + /* Better static priority RT task or better policy preemption */
  2514. + if (p->prio < prio)
  2515. + return true;
  2516. + if (p->prio > prio)
  2517. + return false;
  2518. + /* SCHED_NORMAL, BATCH and ISO will preempt based on deadline */
  2519. + if (!deadline_before(p->deadline, deadline))
  2520. + return false;
  2521. + return true;
  2522. +}
  2523. +
  2524. +#ifdef CONFIG_SMP
  2525. +#define cpu_online_map (*(cpumask_t *)cpu_online_mask)
  2526. +#ifdef CONFIG_HOTPLUG_CPU
  2527. +/*
  2528. + * Check to see if there is a task that is affined only to offline CPUs but
  2529. + * still wants runtime. This happens to kernel threads during suspend/halt and
  2530. + * disabling of CPUs.
  2531. + */
  2532. +static inline bool online_cpus(struct task_struct *p)
  2533. +{
  2534. + return (likely(cpus_intersects(cpu_online_map, p->cpus_allowed)));
  2535. +}
  2536. +#else /* CONFIG_HOTPLUG_CPU */
  2537. +/* All available CPUs are always online without hotplug. */
  2538. +static inline bool online_cpus(struct task_struct *p)
  2539. +{
  2540. + return true;
  2541. +}
  2542. +#endif
  2543. +
  2544. +/*
  2545. + * Check to see if p can run on cpu, and if not, whether there are any online
  2546. + * CPUs it can run on instead.
  2547. + */
  2548. +static inline bool needs_other_cpu(struct task_struct *p, int cpu)
  2549. +{
  2550. + if (unlikely(!cpu_isset(cpu, p->cpus_allowed)))
  2551. + return true;
  2552. + return false;
  2553. +}
  2554. +
  2555. +/*
  2556. + * When all else is equal, still prefer this_rq.
  2557. + */
  2558. +static void try_preempt(struct task_struct *p, struct rq *this_rq)
  2559. +{
  2560. + struct rq *highest_prio_rq = NULL;
  2561. + int cpu, highest_prio;
  2562. + u64 latest_deadline;
  2563. + cpumask_t tmp;
  2564. +
  2565. + /*
  2566. + * We clear the sticky flag here because for a task to have called
  2567. + * try_preempt with the sticky flag enabled means some complicated
  2568. + * re-scheduling has occurred and we should ignore the sticky flag.
  2569. + */
  2570. + clear_sticky(p);
  2571. +
  2572. + if (suitable_idle_cpus(p)) {
  2573. + resched_best_idle(p);
  2574. + return;
  2575. + }
  2576. +
  2577. + /* IDLEPRIO tasks never preempt anything but idle */
  2578. + if (p->policy == SCHED_IDLEPRIO)
  2579. + return;
  2580. +
  2581. + if (likely(online_cpus(p)))
  2582. + cpus_and(tmp, cpu_online_map, p->cpus_allowed);
  2583. + else
  2584. + return;
  2585. +
  2586. + highest_prio = latest_deadline = 0;
  2587. +
  2588. + for_each_cpu_mask(cpu, tmp) {
  2589. + struct rq *rq;
  2590. + int rq_prio;
  2591. +
  2592. + rq = cpu_rq(cpu);
  2593. + rq_prio = rq->rq_prio;
  2594. + if (rq_prio < highest_prio)
  2595. + continue;
  2596. +
  2597. + if (rq_prio > highest_prio ||
  2598. + deadline_after(rq->rq_deadline, latest_deadline)) {
  2599. + latest_deadline = rq->rq_deadline;
  2600. + highest_prio = rq_prio;
  2601. + highest_prio_rq = rq;
  2602. + }
  2603. + }
  2604. +
  2605. + if (likely(highest_prio_rq)) {
  2606. + if (can_preempt(p, highest_prio, highest_prio_rq->rq_deadline))
  2607. + resched_task(highest_prio_rq->curr);
  2608. + }
  2609. +}
  2610. +#else /* CONFIG_SMP */
  2611. +static inline bool needs_other_cpu(struct task_struct *p, int cpu)
  2612. +{
  2613. + return false;
  2614. +}
  2615. +
  2616. +static void try_preempt(struct task_struct *p, struct rq *this_rq)
  2617. +{
  2618. + if (p->policy == SCHED_IDLEPRIO)
  2619. + return;
  2620. + if (can_preempt(p, uprq->rq_prio, uprq->rq_deadline))
  2621. + resched_task(uprq->curr);
  2622. +}
  2623. +#endif /* CONFIG_SMP */
  2624. +
  2625. +static void
  2626. +ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
  2627. +{
  2628. +#ifdef CONFIG_SCHEDSTATS
  2629. + struct rq *rq = this_rq();
  2630. +
  2631. +#ifdef CONFIG_SMP
  2632. + int this_cpu = smp_processor_id();
  2633. +
  2634. + if (cpu == this_cpu)
  2635. + schedstat_inc(rq, ttwu_local);
  2636. + else {
  2637. + struct sched_domain *sd;
  2638. +
  2639. + rcu_read_lock();
  2640. + for_each_domain(this_cpu, sd) {
  2641. + if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  2642. + schedstat_inc(sd, ttwu_wake_remote);
  2643. + break;
  2644. + }
  2645. + }
  2646. + rcu_read_unlock();
  2647. + }
  2648. +
  2649. +#endif /* CONFIG_SMP */
  2650. +
  2651. + schedstat_inc(rq, ttwu_count);
  2652. +#endif /* CONFIG_SCHEDSTATS */
  2653. +}
  2654. +
  2655. +static inline void ttwu_activate(struct task_struct *p, struct rq *rq,
  2656. + bool is_sync)
  2657. +{
  2658. + activate_task(p, rq);
  2659. +
  2660. + /*
  2661. + * Sync wakeups (i.e. those types of wakeups where the waker
  2662. + * has indicated that it will leave the CPU in short order)
  2663. + * don't trigger a preemption if there are no idle cpus,
  2664. + * instead waiting for current to deschedule.
  2665. + */
  2666. + if (!is_sync || suitable_idle_cpus(p))
  2667. + try_preempt(p, rq);
  2668. +}
  2669. +
  2670. +static inline void ttwu_post_activation(struct task_struct *p, struct rq *rq,
  2671. + bool success)
  2672. +{
  2673. + trace_sched_wakeup(p, success);
  2674. + p->state = TASK_RUNNING;
  2675. +
  2676. + /*
  2677. + * if a worker is waking up, notify workqueue. Note that on BFS, we
  2678. + * don't really know what cpu it will be, so we fake it for
  2679. + * wq_worker_waking_up :/
  2680. + */
  2681. + if ((p->flags & PF_WQ_WORKER) && success)
  2682. + wq_worker_waking_up(p, cpu_of(rq));
  2683. +}
  2684. +
  2685. +#ifdef CONFIG_SMP
  2686. +void scheduler_ipi(void)
  2687. +{
  2688. +}
  2689. +#endif /* CONFIG_SMP */
  2690. +
  2691. +/***
  2692. + * try_to_wake_up - wake up a thread
  2693. + * @p: the thread to be awakened
  2694. + * @state: the mask of task states that can be woken
  2695. + * @wake_flags: wake modifier flags (WF_*)
  2696. + *
  2697. + * Put it on the run-queue if it's not already there. The "current"
  2698. + * thread is always on the run-queue (except when the actual
  2699. + * re-schedule is in progress), and as such you're allowed to do
  2700. + * the simpler "current->state = TASK_RUNNING" to mark yourself
  2701. + * runnable without the overhead of this.
  2702. + *
  2703. + * Returns %true if @p was woken up, %false if it was already running
  2704. + * or @state didn't match @p's state.
  2705. + */
  2706. +static bool try_to_wake_up(struct task_struct *p, unsigned int state,
  2707. + int wake_flags)
  2708. +{
  2709. + bool success = false;
  2710. + unsigned long flags;
  2711. + struct rq *rq;
  2712. + int cpu;
  2713. +
  2714. + get_cpu();
  2715. +
  2716. + /* This barrier is undocumented, probably for p->state? くそ */
  2717. + smp_wmb();
  2718. +
  2719. + /*
  2720. + * No need to do time_lock_grq as we only need to update the rq clock
  2721. + * if we activate the task
  2722. + */
  2723. + rq = task_grq_lock(p, &flags);
  2724. + cpu = task_cpu(p);
  2725. +
  2726. + /* state is a volatile long, どうして、分からない */
  2727. + if (!((unsigned int)p->state & state))
  2728. + goto out_unlock;
  2729. +
  2730. + if (task_queued(p) || task_running(p))
  2731. + goto out_running;
  2732. +
  2733. + ttwu_activate(p, rq, wake_flags & WF_SYNC);
  2734. + success = true;
  2735. +
  2736. +out_running:
  2737. + ttwu_post_activation(p, rq, success);
  2738. +out_unlock:
  2739. + task_grq_unlock(&flags);
  2740. +
  2741. + ttwu_stat(p, cpu, wake_flags);
  2742. +
  2743. + put_cpu();
  2744. +
  2745. + return success;
  2746. +}
  2747. +
  2748. +/**
  2749. + * try_to_wake_up_local - try to wake up a local task with grq lock held
  2750. + * @p: the thread to be awakened
  2751. + *
  2752. + * Put @p on the run-queue if it's not already there. The caller must
  2753. + * ensure that grq is locked and, @p is not the current task.
  2754. + * grq stays locked over invocation.
  2755. + */
  2756. +static void try_to_wake_up_local(struct task_struct *p)
  2757. +{
  2758. + struct rq *rq = task_rq(p);
  2759. + bool success = false;
  2760. +
  2761. + lockdep_assert_held(&grq.lock);
  2762. +
  2763. + if (!(p->state & TASK_NORMAL))
  2764. + return;
  2765. +
  2766. + if (!task_queued(p)) {
  2767. + if (likely(!task_running(p))) {
  2768. + schedstat_inc(rq, ttwu_count);
  2769. + schedstat_inc(rq, ttwu_local);
  2770. + }
  2771. + ttwu_activate(p, rq, false);
  2772. + ttwu_stat(p, smp_processor_id(), 0);
  2773. + success = true;
  2774. + }
  2775. + ttwu_post_activation(p, rq, success);
  2776. +}
  2777. +
  2778. +/**
  2779. + * wake_up_process - Wake up a specific process
  2780. + * @p: The process to be woken up.
  2781. + *
  2782. + * Attempt to wake up the nominated process and move it to the set of runnable
  2783. + * processes. Returns 1 if the process was woken up, 0 if it was already
  2784. + * running.
  2785. + *
  2786. + * It may be assumed that this function implies a write memory barrier before
  2787. + * changing the task state if and only if any tasks are woken up.
  2788. + */
  2789. +int wake_up_process(struct task_struct *p)
  2790. +{
  2791. + return try_to_wake_up(p, TASK_ALL, 0);
  2792. +}
  2793. +EXPORT_SYMBOL(wake_up_process);
  2794. +
  2795. +int wake_up_state(struct task_struct *p, unsigned int state)
  2796. +{
  2797. + return try_to_wake_up(p, state, 0);
  2798. +}
  2799. +
  2800. +static void time_slice_expired(struct task_struct *p);
  2801. +
  2802. +/*
  2803. + * Perform scheduler related setup for a newly forked process p.
  2804. + * p is forked by current.
  2805. + */
  2806. +void sched_fork(struct task_struct *p)
  2807. +{
  2808. + struct task_struct *curr;
  2809. + int cpu = get_cpu();
  2810. + struct rq *rq;
  2811. +
  2812. +#ifdef CONFIG_PREEMPT_NOTIFIERS
  2813. + INIT_HLIST_HEAD(&p->preempt_notifiers);
  2814. +#endif
  2815. + /*
  2816. + * We mark the process as running here. This guarantees that
  2817. + * nobody will actually run it, and a signal or other external
  2818. + * event cannot wake it up and insert it on the runqueue either.
  2819. + */
  2820. + p->state = TASK_RUNNING;
  2821. + set_task_cpu(p, cpu);
  2822. +
  2823. + /* Should be reset in fork.c but done here for ease of bfs patching */
  2824. + p->utime =
  2825. + p->stime =
  2826. + p->utimescaled =
  2827. + p->stimescaled =
  2828. + p->sched_time =
  2829. + p->stime_pc =
  2830. + p->utime_pc = 0;
  2831. +
  2832. + /*
  2833. + * Revert to default priority/policy on fork if requested.
  2834. + */
  2835. + if (unlikely(p->sched_reset_on_fork)) {
  2836. + if (p->policy == SCHED_FIFO || p->policy == SCHED_RR) {
  2837. + p->policy = SCHED_NORMAL;
  2838. + p->normal_prio = normal_prio(p);
  2839. + }
  2840. +
  2841. + if (PRIO_TO_NICE(p->static_prio) < 0) {
  2842. + p->static_prio = NICE_TO_PRIO(0);
  2843. + p->normal_prio = p->static_prio;
  2844. + }
  2845. +
  2846. + /*
  2847. + * We don't need the reset flag anymore after the fork. It has
  2848. + * fulfilled its duty:
  2849. + */
  2850. + p->sched_reset_on_fork = 0;
  2851. + }
  2852. +
  2853. + curr = current;
  2854. + /*
  2855. + * Make sure we do not leak PI boosting priority to the child.
  2856. + */
  2857. + p->prio = curr->normal_prio;
  2858. +
  2859. + INIT_LIST_HEAD(&p->run_list);
  2860. +#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  2861. + if (unlikely(sched_info_on()))
  2862. + memset(&p->sched_info, 0, sizeof(p->sched_info));
  2863. +#endif
  2864. +
  2865. + p->on_cpu = false;
  2866. + clear_sticky(p);
  2867. +
  2868. +#ifdef CONFIG_PREEMPT_COUNT
  2869. + /* Want to start with kernel preemption disabled. */
  2870. + task_thread_info(p)->preempt_count = 1;
  2871. +#endif
  2872. + if (unlikely(p->policy == SCHED_FIFO))
  2873. + goto out;
  2874. + /*
  2875. + * Share the timeslice between parent and child, thus the
  2876. + * total amount of pending timeslices in the system doesn't change,
  2877. + * resulting in more scheduling fairness. If it's negative, it won't
  2878. + * matter since that's the same as being 0. current's time_slice is
  2879. + * actually in rq_time_slice when it's running, as is its last_ran
  2880. + * value. rq->rq_deadline is only modified within schedule() so it
  2881. + * is always equal to current->deadline.
  2882. + */
  2883. + rq = task_grq_lock_irq(curr);
  2884. + if (likely(rq->rq_time_slice >= RESCHED_US * 2)) {
  2885. + rq->rq_time_slice /= 2;
  2886. + p->time_slice = rq->rq_time_slice;
  2887. + } else {
  2888. + /*
  2889. + * Forking task has run out of timeslice. Reschedule it and
  2890. + * start its child with a new time slice and deadline. The
  2891. + * child will end up running first because its deadline will
  2892. + * be slightly earlier.
  2893. + */
  2894. + rq->rq_time_slice = 0;
  2895. + set_tsk_need_resched(curr);
  2896. + time_slice_expired(p);
  2897. + }
  2898. + p->last_ran = rq->rq_last_ran;
  2899. + task_grq_unlock_irq();
  2900. +out:
  2901. + put_cpu();
  2902. +}
  2903. +
  2904. +/*
  2905. + * wake_up_new_task - wake up a newly created task for the first time.
  2906. + *
  2907. + * This function will do some initial scheduler statistics housekeeping
  2908. + * that must be done for every newly created context, then puts the task
  2909. + * on the runqueue and wakes it.
  2910. + */
  2911. +void wake_up_new_task(struct task_struct *p)
  2912. +{
  2913. + struct task_struct *parent;
  2914. + unsigned long flags;
  2915. + struct rq *rq;
  2916. +
  2917. + rq = task_grq_lock(p, &flags);
  2918. + p->state = TASK_RUNNING;
  2919. + parent = p->parent;
  2920. + /* Unnecessary but small chance that the parent changed CPU */
  2921. + set_task_cpu(p, task_cpu(parent));
  2922. + activate_task(p, rq);
  2923. + trace_sched_wakeup_new(p, 1);
  2924. + if (rq->curr == parent && !suitable_idle_cpus(p)) {
  2925. + /*
  2926. + * The VM isn't cloned, so we're in a good position to
  2927. + * do child-runs-first in anticipation of an exec. This
  2928. + * usually avoids a lot of COW overhead.
  2929. + */
  2930. + resched_task(parent);
  2931. + } else
  2932. + try_preempt(p, rq);
  2933. + task_grq_unlock(&flags);
  2934. +}
  2935. +
  2936. +#ifdef CONFIG_PREEMPT_NOTIFIERS
  2937. +
  2938. +/**
  2939. + * preempt_notifier_register - tell me when current is being preempted & rescheduled
  2940. + * @notifier: notifier struct to register
  2941. + */
  2942. +void preempt_notifier_register(struct preempt_notifier *notifier)
  2943. +{
  2944. + hlist_add_head(&notifier->link, &current->preempt_notifiers);
  2945. +}
  2946. +EXPORT_SYMBOL_GPL(preempt_notifier_register);
  2947. +
  2948. +/**
  2949. + * preempt_notifier_unregister - no longer interested in preemption notifications
  2950. + * @notifier: notifier struct to unregister
  2951. + *
  2952. + * This is safe to call from within a preemption notifier.
  2953. + */
  2954. +void preempt_notifier_unregister(struct preempt_notifier *notifier)
  2955. +{
  2956. + hlist_del(&notifier->link);
  2957. +}
  2958. +EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
  2959. +
  2960. +static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  2961. +{
  2962. + struct preempt_notifier *notifier;
  2963. + struct hlist_node *node;
  2964. +
  2965. + hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  2966. + notifier->ops->sched_in(notifier, raw_smp_processor_id());
  2967. +}
  2968. +
  2969. +static void
  2970. +fire_sched_out_preempt_notifiers(struct task_struct *curr,
  2971. + struct task_struct *next)
  2972. +{
  2973. + struct preempt_notifier *notifier;
  2974. + struct hlist_node *node;
  2975. +
  2976. + hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  2977. + notifier->ops->sched_out(notifier, next);
  2978. +}
  2979. +
  2980. +#else /* !CONFIG_PREEMPT_NOTIFIERS */
  2981. +
  2982. +static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  2983. +{
  2984. +}
  2985. +
  2986. +static void
  2987. +fire_sched_out_preempt_notifiers(struct task_struct *curr,
  2988. + struct task_struct *next)
  2989. +{
  2990. +}
  2991. +
  2992. +#endif /* CONFIG_PREEMPT_NOTIFIERS */
  2993. +
  2994. +/**
  2995. + * prepare_task_switch - prepare to switch tasks
  2996. + * @rq: the runqueue preparing to switch
  2997. + * @next: the task we are going to switch to.
  2998. + *
  2999. + * This is called with the rq lock held and interrupts off. It must
  3000. + * be paired with a subsequent finish_task_switch after the context
  3001. + * switch.
  3002. + *
  3003. + * prepare_task_switch sets up locking and calls architecture specific
  3004. + * hooks.
  3005. + */
  3006. +static inline void
  3007. +prepare_task_switch(struct rq *rq, struct task_struct *prev,
  3008. + struct task_struct *next)
  3009. +{
  3010. + sched_info_switch(prev, next);
  3011. + perf_event_task_sched_out(prev, next);
  3012. + fire_sched_out_preempt_notifiers(prev, next);
  3013. + prepare_lock_switch(rq, next);
  3014. + prepare_arch_switch(next);
  3015. + trace_sched_switch(prev, next);
  3016. +}
  3017. +
  3018. +/**
  3019. + * finish_task_switch - clean up after a task-switch
  3020. + * @rq: runqueue associated with task-switch
  3021. + * @prev: the thread we just switched away from.
  3022. + *
  3023. + * finish_task_switch must be called after the context switch, paired
  3024. + * with a prepare_task_switch call before the context switch.
  3025. + * finish_task_switch will reconcile locking set up by prepare_task_switch,
  3026. + * and do any other architecture-specific cleanup actions.
  3027. + *
  3028. + * Note that we may have delayed dropping an mm in context_switch(). If
  3029. + * so, we finish that here outside of the runqueue lock. (Doing it
  3030. + * with the lock held can cause deadlocks; see schedule() for
  3031. + * details.)
  3032. + */
  3033. +static inline void finish_task_switch(struct rq *rq, struct task_struct *prev)
  3034. + __releases(grq.lock)
  3035. +{
  3036. + struct mm_struct *mm = rq->prev_mm;
  3037. + long prev_state;
  3038. +
  3039. + rq->prev_mm = NULL;
  3040. +
  3041. + /*
  3042. + * A task struct has one reference for the use as "current".
  3043. + * If a task dies, then it sets TASK_DEAD in tsk->state and calls
  3044. + * schedule one last time. The schedule call will never return, and
  3045. + * the scheduled task must drop that reference.
  3046. + * The test for TASK_DEAD must occur while the runqueue locks are
  3047. + * still held, otherwise prev could be scheduled on another cpu, die
  3048. + * there before we look at prev->state, and then the reference would
  3049. + * be dropped twice.
  3050. + * Manfred Spraul <manfred@colorfullife.com>
  3051. + */
  3052. + prev_state = prev->state;
  3053. + finish_arch_switch(prev);
  3054. +#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  3055. + local_irq_disable();
  3056. +#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
  3057. + perf_event_task_sched_in(prev, current);
  3058. +#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  3059. + local_irq_enable();
  3060. +#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
  3061. + finish_lock_switch(rq, prev);
  3062. + finish_arch_post_lock_switch();
  3063. +
  3064. + fire_sched_in_preempt_notifiers(current);
  3065. + if (mm)
  3066. + mmdrop(mm);
  3067. + if (unlikely(prev_state == TASK_DEAD)) {
  3068. + /*
  3069. + * Remove function-return probe instances associated with this
  3070. + * task and put them back on the free list.
  3071. + */
  3072. + kprobe_flush_task(prev);
  3073. + put_task_struct(prev);
  3074. + }
  3075. +}
  3076. +
  3077. +/**
  3078. + * schedule_tail - first thing a freshly forked thread must call.
  3079. + * @prev: the thread we just switched away from.
  3080. + */
  3081. +asmlinkage void schedule_tail(struct task_struct *prev)
  3082. + __releases(grq.lock)
  3083. +{
  3084. + struct rq *rq = this_rq();
  3085. +
  3086. + finish_task_switch(rq, prev);
  3087. +#ifdef __ARCH_WANT_UNLOCKED_CTXSW
  3088. + /* In this case, finish_task_switch does not reenable preemption */
  3089. + preempt_enable();
  3090. +#endif
  3091. + if (current->set_child_tid)
  3092. + put_user(current->pid, current->set_child_tid);
  3093. +}
  3094. +
  3095. +/*
  3096. + * context_switch - switch to the new MM and the new
  3097. + * thread's register state.
  3098. + */
  3099. +static inline void
  3100. +context_switch(struct rq *rq, struct task_struct *prev,
  3101. + struct task_struct *next)
  3102. +{
  3103. + struct mm_struct *mm, *oldmm;
  3104. +
  3105. + prepare_task_switch(rq, prev, next);
  3106. +
  3107. + mm = next->mm;
  3108. + oldmm = prev->active_mm;
  3109. + /*
  3110. + * For paravirt, this is coupled with an exit in switch_to to
  3111. + * combine the page table reload and the switch backend into
  3112. + * one hypercall.
  3113. + */
  3114. + arch_start_context_switch(prev);
  3115. +
  3116. + if (!mm) {
  3117. + next->active_mm = oldmm;
  3118. + atomic_inc(&oldmm->mm_count);
  3119. + enter_lazy_tlb(oldmm, next);
  3120. + } else
  3121. + switch_mm(oldmm, mm, next);
  3122. +
  3123. + if (!prev->mm) {
  3124. + prev->active_mm = NULL;
  3125. + rq->prev_mm = oldmm;
  3126. + }
  3127. + /*
  3128. + * Since the runqueue lock will be released by the next
  3129. + * task (which is an invalid locking op but in the case
  3130. + * of the scheduler it's an obvious special-case), so we
  3131. + * do an early lockdep release here:
  3132. + */
  3133. +#ifndef __ARCH_WANT_UNLOCKED_CTXSW
  3134. + spin_release(&grq.lock.dep_map, 1, _THIS_IP_);
  3135. +#endif
  3136. +
  3137. + /* Here we just switch the register state and the stack. */
  3138. + switch_to(prev, next, prev);
  3139. +
  3140. + barrier();
  3141. + /*
  3142. + * this_rq must be evaluated again because prev may have moved
  3143. + * CPUs since it called schedule(), thus the 'rq' on its stack
  3144. + * frame will be invalid.
  3145. + */
  3146. + finish_task_switch(this_rq(), prev);
  3147. +}
  3148. +
  3149. +/*
  3150. + * nr_running, nr_uninterruptible and nr_context_switches:
  3151. + *
  3152. + * externally visible scheduler statistics: current number of runnable
  3153. + * threads, current number of uninterruptible-sleeping threads, total
  3154. + * number of context switches performed since bootup. All are measured
  3155. + * without grabbing the grq lock but the occasional inaccurate result
  3156. + * doesn't matter so long as it's positive.
  3157. + */
  3158. +unsigned long nr_running(void)
  3159. +{
  3160. + long nr = grq.nr_running;
  3161. +
  3162. + if (unlikely(nr < 0))
  3163. + nr = 0;
  3164. + return (unsigned long)nr;
  3165. +}
  3166. +
  3167. +unsigned long nr_uninterruptible(void)
  3168. +{
  3169. + long nu = grq.nr_uninterruptible;
  3170. +
  3171. + if (unlikely(nu < 0))
  3172. + nu = 0;
  3173. + return nu;
  3174. +}
  3175. +
  3176. +unsigned long long nr_context_switches(void)
  3177. +{
  3178. + long long ns = grq.nr_switches;
  3179. +
  3180. + /* This is of course impossible */
  3181. + if (unlikely(ns < 0))
  3182. + ns = 1;
  3183. + return (unsigned long long)ns;
  3184. +}
  3185. +
  3186. +unsigned long nr_iowait(void)
  3187. +{
  3188. + unsigned long i, sum = 0;
  3189. +
  3190. + for_each_possible_cpu(i)
  3191. + sum += atomic_read(&cpu_rq(i)->nr_iowait);
  3192. +
  3193. + return sum;
  3194. +}
  3195. +
  3196. +unsigned long nr_iowait_cpu(int cpu)
  3197. +{
  3198. + struct rq *this = cpu_rq(cpu);
  3199. + return atomic_read(&this->nr_iowait);
  3200. +}
  3201. +
  3202. +unsigned long nr_active(void)
  3203. +{
  3204. + return nr_running() + nr_uninterruptible();
  3205. +}
  3206. +
  3207. +/* Beyond a task running on this CPU, load is equal everywhere on BFS */
  3208. +unsigned long this_cpu_load(void)
  3209. +{
  3210. + return this_rq()->rq_running +
  3211. + ((queued_notrunning() + nr_uninterruptible()) / grq.noc);
  3212. +}
  3213. +
  3214. +/* Variables and functions for calc_load */
  3215. +static unsigned long calc_load_update;
  3216. +unsigned long avenrun[3];
  3217. +EXPORT_SYMBOL(avenrun);
  3218. +
  3219. +/**
  3220. + * get_avenrun - get the load average array
  3221. + * @loads: pointer to dest load array
  3222. + * @offset: offset to add
  3223. + * @shift: shift count to shift the result left
  3224. + *
  3225. + * These values are estimates at best, so no need for locking.
  3226. + */
  3227. +void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
  3228. +{
  3229. + loads[0] = (avenrun[0] + offset) << shift;
  3230. + loads[1] = (avenrun[1] + offset) << shift;
  3231. + loads[2] = (avenrun[2] + offset) << shift;
  3232. +}
  3233. +
  3234. +static unsigned long
  3235. +calc_load(unsigned long load, unsigned long exp, unsigned long active)
  3236. +{
  3237. + load *= exp;
  3238. + load += active * (FIXED_1 - exp);
  3239. + return load >> FSHIFT;
  3240. +}
  3241. +
  3242. +/*
  3243. + * calc_load - update the avenrun load estimates every LOAD_FREQ seconds.
  3244. + */
  3245. +void calc_global_load(unsigned long ticks)
  3246. +{
  3247. + long active;
  3248. +
  3249. + if (time_before(jiffies, calc_load_update))
  3250. + return;
  3251. + active = nr_active() * FIXED_1;
  3252. +
  3253. + avenrun[0] = calc_load(avenrun[0], EXP_1, active);
  3254. + avenrun[1] = calc_load(avenrun[1], EXP_5, active);
  3255. + avenrun[2] = calc_load(avenrun[2], EXP_15, active);
  3256. +
  3257. + calc_load_update = jiffies + LOAD_FREQ;
  3258. +}
  3259. +
  3260. +DEFINE_PER_CPU(struct kernel_stat, kstat);
  3261. +DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
  3262. +
  3263. +EXPORT_PER_CPU_SYMBOL(kstat);
  3264. +EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
  3265. +
  3266. +#ifdef CONFIG_IRQ_TIME_ACCOUNTING
  3267. +
  3268. +/*
  3269. + * There are no locks covering percpu hardirq/softirq time.
  3270. + * They are only modified in account_system_vtime, on corresponding CPU
  3271. + * with interrupts disabled. So, writes are safe.
  3272. + * They are read and saved off onto struct rq in update_rq_clock().
  3273. + * This may result in other CPU reading this CPU's irq time and can
  3274. + * race with irq/account_system_vtime on this CPU. We would either get old
  3275. + * or new value with a side effect of accounting a slice of irq time to wrong
  3276. + * task when irq is in progress while we read rq->clock. That is a worthy
  3277. + * compromise in place of having locks on each irq in account_system_time.
  3278. + */
  3279. +static DEFINE_PER_CPU(u64, cpu_hardirq_time);
  3280. +static DEFINE_PER_CPU(u64, cpu_softirq_time);
  3281. +
  3282. +static DEFINE_PER_CPU(u64, irq_start_time);
  3283. +static int sched_clock_irqtime;
  3284. +
  3285. +void enable_sched_clock_irqtime(void)
  3286. +{
  3287. + sched_clock_irqtime = 1;
  3288. +}
  3289. +
  3290. +void disable_sched_clock_irqtime(void)
  3291. +{
  3292. + sched_clock_irqtime = 0;
  3293. +}
  3294. +
  3295. +#ifndef CONFIG_64BIT
  3296. +static DEFINE_PER_CPU(seqcount_t, irq_time_seq);
  3297. +
  3298. +static inline void irq_time_write_begin(void)
  3299. +{
  3300. + __this_cpu_inc(irq_time_seq.sequence);
  3301. + smp_wmb();
  3302. +}
  3303. +
  3304. +static inline void irq_time_write_end(void)
  3305. +{
  3306. + smp_wmb();
  3307. + __this_cpu_inc(irq_time_seq.sequence);
  3308. +}
  3309. +
  3310. +static inline u64 irq_time_read(int cpu)
  3311. +{
  3312. + u64 irq_time;
  3313. + unsigned seq;
  3314. +
  3315. + do {
  3316. + seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
  3317. + irq_time = per_cpu(cpu_softirq_time, cpu) +
  3318. + per_cpu(cpu_hardirq_time, cpu);
  3319. + } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
  3320. +
  3321. + return irq_time;
  3322. +}
  3323. +#else /* CONFIG_64BIT */
  3324. +static inline void irq_time_write_begin(void)
  3325. +{
  3326. +}
  3327. +
  3328. +static inline void irq_time_write_end(void)
  3329. +{
  3330. +}
  3331. +
  3332. +static inline u64 irq_time_read(int cpu)
  3333. +{
  3334. + return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
  3335. +}
  3336. +#endif /* CONFIG_64BIT */
  3337. +
  3338. +/*
  3339. + * Called before incrementing preempt_count on {soft,}irq_enter
  3340. + * and before decrementing preempt_count on {soft,}irq_exit.
  3341. + */
  3342. +void account_system_vtime(struct task_struct *curr)
  3343. +{
  3344. + unsigned long flags;
  3345. + s64 delta;
  3346. + int cpu;
  3347. +
  3348. + if (!sched_clock_irqtime)
  3349. + return;
  3350. +
  3351. + local_irq_save(flags);
  3352. +
  3353. + cpu = smp_processor_id();
  3354. + delta = sched_clock_cpu(cpu) - __this_cpu_read(irq_start_time);
  3355. + __this_cpu_add(irq_start_time, delta);
  3356. +
  3357. + irq_time_write_begin();
  3358. + /*
  3359. + * We do not account for softirq time from ksoftirqd here.
  3360. + * We want to continue accounting softirq time to ksoftirqd thread
  3361. + * in that case, so as not to confuse scheduler with a special task
  3362. + * that do not consume any time, but still wants to run.
  3363. + */
  3364. + if (hardirq_count())
  3365. + __this_cpu_add(cpu_hardirq_time, delta);
  3366. + else if (in_serving_softirq() && curr != this_cpu_ksoftirqd())
  3367. + __this_cpu_add(cpu_softirq_time, delta);
  3368. +
  3369. + irq_time_write_end();
  3370. + local_irq_restore(flags);
  3371. +}
  3372. +EXPORT_SYMBOL_GPL(account_system_vtime);
  3373. +
  3374. +#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
  3375. +
  3376. +#ifdef CONFIG_PARAVIRT
  3377. +static inline u64 steal_ticks(u64 steal)
  3378. +{
  3379. + if (unlikely(steal > NSEC_PER_SEC))
  3380. + return div_u64(steal, TICK_NSEC);
  3381. +
  3382. + return __iter_div_u64_rem(steal, TICK_NSEC, &steal);
  3383. +}
  3384. +#endif
  3385. +
  3386. +static void update_rq_clock_task(struct rq *rq, s64 delta)
  3387. +{
  3388. +#ifdef CONFIG_IRQ_TIME_ACCOUNTING
  3389. + s64 irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
  3390. +
  3391. + /*
  3392. + * Since irq_time is only updated on {soft,}irq_exit, we might run into
  3393. + * this case when a previous update_rq_clock() happened inside a
  3394. + * {soft,}irq region.
  3395. + *
  3396. + * When this happens, we stop ->clock_task and only update the
  3397. + * prev_irq_time stamp to account for the part that fit, so that a next
  3398. + * update will consume the rest. This ensures ->clock_task is
  3399. + * monotonic.
  3400. + *
  3401. + * It does however cause some slight miss-attribution of {soft,}irq
  3402. + * time, a more accurate solution would be to update the irq_time using
  3403. + * the current rq->clock timestamp, except that would require using
  3404. + * atomic ops.
  3405. + */
  3406. + if (irq_delta > delta)
  3407. + irq_delta = delta;
  3408. +
  3409. + rq->prev_irq_time += irq_delta;
  3410. + delta -= irq_delta;
  3411. +#endif
  3412. +#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  3413. + if (static_branch((&paravirt_steal_rq_enabled))) {
  3414. + u64 st, steal = paravirt_steal_clock(cpu_of(rq));
  3415. +
  3416. + steal -= rq->prev_steal_time_rq;
  3417. +
  3418. + if (unlikely(steal > delta))
  3419. + steal = delta;
  3420. +
  3421. + st = steal_ticks(steal);
  3422. + steal = st * TICK_NSEC;
  3423. +
  3424. + rq->prev_steal_time_rq += steal;
  3425. +
  3426. + delta -= steal;
  3427. + }
  3428. +#endif
  3429. +
  3430. + rq->clock_task += delta;
  3431. +}
  3432. +
  3433. +#ifndef nsecs_to_cputime
  3434. +# define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs)
  3435. +#endif
  3436. +
  3437. +#ifdef CONFIG_IRQ_TIME_ACCOUNTING
  3438. +static void irqtime_account_hi_si(void)
  3439. +{
  3440. + u64 *cpustat = kcpustat_this_cpu->cpustat;
  3441. + u64 latest_ns;
  3442. +
  3443. + latest_ns = nsecs_to_cputime64(this_cpu_read(cpu_hardirq_time));
  3444. + if (latest_ns > cpustat[CPUTIME_IRQ])
  3445. + cpustat[CPUTIME_IRQ] += (__force u64)cputime_one_jiffy;
  3446. +
  3447. + latest_ns = nsecs_to_cputime64(this_cpu_read(cpu_softirq_time));
  3448. + if (latest_ns > cpustat[CPUTIME_SOFTIRQ])
  3449. + cpustat[CPUTIME_SOFTIRQ] += (__force u64)cputime_one_jiffy;
  3450. +}
  3451. +#else /* CONFIG_IRQ_TIME_ACCOUNTING */
  3452. +
  3453. +#define sched_clock_irqtime (0)
  3454. +
  3455. +static inline void irqtime_account_hi_si(void)
  3456. +{
  3457. +}
  3458. +#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
  3459. +
  3460. +static __always_inline bool steal_account_process_tick(void)
  3461. +{
  3462. +#ifdef CONFIG_PARAVIRT
  3463. + if (static_key_false(&paravirt_steal_enabled)) {
  3464. + u64 steal, st = 0;
  3465. +
  3466. + steal = paravirt_steal_clock(smp_processor_id());
  3467. + steal -= this_rq()->prev_steal_time;
  3468. +
  3469. + st = steal_ticks(steal);
  3470. + this_rq()->prev_steal_time += st * TICK_NSEC;
  3471. +
  3472. + account_steal_time(st);
  3473. + return st;
  3474. + }
  3475. +#endif
  3476. + return false;
  3477. +}
  3478. +
  3479. +/*
  3480. + * On each tick, see what percentage of that tick was attributed to each
  3481. + * component and add the percentage to the _pc values. Once a _pc value has
  3482. + * accumulated one tick's worth, account for that. This means the total
  3483. + * percentage of load components will always be 128 (pseudo 100) per tick.
  3484. + */
  3485. +static void pc_idle_time(struct rq *rq, unsigned long pc)
  3486. +{
  3487. + u64 *cpustat = kcpustat_this_cpu->cpustat;
  3488. +
  3489. + if (atomic_read(&rq->nr_iowait) > 0) {
  3490. + rq->iowait_pc += pc;
  3491. + if (rq->iowait_pc >= 128) {
  3492. + rq->iowait_pc %= 128;
  3493. + cpustat[CPUTIME_IOWAIT] += (__force u64)cputime_one_jiffy;
  3494. + }
  3495. + } else {
  3496. + rq->idle_pc += pc;
  3497. + if (rq->idle_pc >= 128) {
  3498. + rq->idle_pc %= 128;
  3499. + cpustat[CPUTIME_IDLE] += (__force u64)cputime_one_jiffy;
  3500. + }
  3501. + }
  3502. +}
  3503. +
  3504. +static void
  3505. +pc_system_time(struct rq *rq, struct task_struct *p, int hardirq_offset,
  3506. + unsigned long pc, unsigned long ns)
  3507. +{
  3508. + u64 *cpustat = kcpustat_this_cpu->cpustat;
  3509. + cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
  3510. +
  3511. + p->stime_pc += pc;
  3512. + if (p->stime_pc >= 128) {
  3513. + p->stime_pc %= 128;
  3514. + p->stime += (__force u64)cputime_one_jiffy;
  3515. + p->stimescaled += one_jiffy_scaled;
  3516. + account_group_system_time(p, cputime_one_jiffy);
  3517. + acct_update_integrals(p);
  3518. + }
  3519. + p->sched_time += ns;
  3520. +
  3521. + if (hardirq_count() - hardirq_offset) {
  3522. + rq->irq_pc += pc;
  3523. + if (rq->irq_pc >= 128) {
  3524. + rq->irq_pc %= 128;
  3525. + cpustat[CPUTIME_IRQ] += (__force u64)cputime_one_jiffy;
  3526. + }
  3527. + } else if (in_serving_softirq()) {
  3528. + rq->softirq_pc += pc;
  3529. + if (rq->softirq_pc >= 128) {
  3530. + rq->softirq_pc %= 128;
  3531. + cpustat[CPUTIME_SOFTIRQ] += (__force u64)cputime_one_jiffy;
  3532. + }
  3533. + } else {
  3534. + rq->system_pc += pc;
  3535. + if (rq->system_pc >= 128) {
  3536. + rq->system_pc %= 128;
  3537. + cpustat[CPUTIME_SYSTEM] += (__force u64)cputime_one_jiffy;
  3538. + }
  3539. + }
  3540. +}
  3541. +
  3542. +static void pc_user_time(struct rq *rq, struct task_struct *p,
  3543. + unsigned long pc, unsigned long ns)
  3544. +{
  3545. + u64 *cpustat = kcpustat_this_cpu->cpustat;
  3546. + cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
  3547. +
  3548. + p->utime_pc += pc;
  3549. + if (p->utime_pc >= 128) {
  3550. + p->utime_pc %= 128;
  3551. + p->utime += (__force u64)cputime_one_jiffy;
  3552. + p->utimescaled += one_jiffy_scaled;
  3553. + account_group_user_time(p, cputime_one_jiffy);
  3554. + acct_update_integrals(p);
  3555. + }
  3556. + p->sched_time += ns;
  3557. +
  3558. + if (this_cpu_ksoftirqd() == p) {
  3559. + /*
  3560. + * ksoftirqd time do not get accounted in cpu_softirq_time.
  3561. + * So, we have to handle it separately here.
  3562. + */
  3563. + rq->softirq_pc += pc;
  3564. + if (rq->softirq_pc >= 128) {
  3565. + rq->softirq_pc %= 128;
  3566. + cpustat[CPUTIME_SOFTIRQ] += (__force u64)cputime_one_jiffy;
  3567. + }
  3568. + }
  3569. +
  3570. + if (TASK_NICE(p) > 0 || idleprio_task(p)) {
  3571. + rq->nice_pc += pc;
  3572. + if (rq->nice_pc >= 128) {
  3573. + rq->nice_pc %= 128;
  3574. + cpustat[CPUTIME_NICE] += (__force u64)cputime_one_jiffy;
  3575. + }
  3576. + } else {
  3577. + rq->user_pc += pc;
  3578. + if (rq->user_pc >= 128) {
  3579. + rq->user_pc %= 128;
  3580. + cpustat[CPUTIME_USER] += (__force u64)cputime_one_jiffy;
  3581. + }
  3582. + }
  3583. +}
  3584. +
  3585. +/*
  3586. + * Convert nanoseconds to pseudo percentage of one tick. Use 128 for fast
  3587. + * shifts instead of 100
  3588. + */
  3589. +#define NS_TO_PC(NS) (NS * 128 / JIFFY_NS)
  3590. +
  3591. +/*
  3592. + * This is called on clock ticks and on context switches.
  3593. + * Bank in p->sched_time the ns elapsed since the last tick or switch.
  3594. + * CPU scheduler quota accounting is also performed here in microseconds.
  3595. + */
  3596. +static void
  3597. +update_cpu_clock(struct rq *rq, struct task_struct *p, bool tick)
  3598. +{
  3599. + long account_ns = rq->clock - rq->timekeep_clock;
  3600. + struct task_struct *idle = rq->idle;
  3601. + unsigned long account_pc;
  3602. +
  3603. + if (unlikely(account_ns < 0))
  3604. + account_ns = 0;
  3605. +
  3606. + account_pc = NS_TO_PC(account_ns);
  3607. +
  3608. + if (tick) {
  3609. + int user_tick;
  3610. +
  3611. + /* Accurate tick timekeeping */
  3612. + rq->account_pc += account_pc - 128;
  3613. + if (rq->account_pc < 0) {
  3614. + /*
  3615. + * Small errors in micro accounting may not make the
  3616. + * accounting add up to 128 each tick so we keep track
  3617. + * of the percentage and round it up when less than 128
  3618. + */
  3619. + account_pc += -rq->account_pc;
  3620. + rq->account_pc = 0;
  3621. + }
  3622. + if (steal_account_process_tick())
  3623. + goto ts_account;
  3624. +
  3625. + user_tick = user_mode(get_irq_regs());
  3626. +
  3627. + if (user_tick)
  3628. + pc_user_time(rq, p, account_pc, account_ns);
  3629. + else if (p != idle || (irq_count() != HARDIRQ_OFFSET))
  3630. + pc_system_time(rq, p, HARDIRQ_OFFSET,
  3631. + account_pc, account_ns);
  3632. + else
  3633. + pc_idle_time(rq, account_pc);
  3634. +
  3635. + if (sched_clock_irqtime)
  3636. + irqtime_account_hi_si();
  3637. + } else {
  3638. + /* Accurate subtick timekeeping */
  3639. + rq->account_pc += account_pc;
  3640. + if (p == idle)
  3641. + pc_idle_time(rq, account_pc);
  3642. + else
  3643. + pc_user_time(rq, p, account_pc, account_ns);
  3644. + }
  3645. +
  3646. +ts_account:
  3647. + /* time_slice accounting is done in usecs to avoid overflow on 32bit */
  3648. + if (rq->rq_policy != SCHED_FIFO && p != idle) {
  3649. + s64 time_diff = rq->clock - rq->rq_last_ran;
  3650. +
  3651. + niffy_diff(&time_diff, 1);
  3652. + rq->rq_time_slice -= NS_TO_US(time_diff);
  3653. + }
  3654. + rq->rq_last_ran = rq->timekeep_clock = rq->clock;
  3655. +}
  3656. +
  3657. +/*
  3658. + * Return any ns on the sched_clock that have not yet been accounted in
  3659. + * @p in case that task is currently running.
  3660. + *
  3661. + * Called with task_grq_lock() held.
  3662. + */
  3663. +static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
  3664. +{
  3665. + u64 ns = 0;
  3666. +
  3667. + if (p == rq->curr) {
  3668. + update_clocks(rq);
  3669. + ns = rq->clock_task - rq->rq_last_ran;
  3670. + if (unlikely((s64)ns < 0))
  3671. + ns = 0;
  3672. + }
  3673. +
  3674. + return ns;
  3675. +}
  3676. +
  3677. +unsigned long long task_delta_exec(struct task_struct *p)
  3678. +{
  3679. + unsigned long flags;
  3680. + struct rq *rq;
  3681. + u64 ns;
  3682. +
  3683. + rq = task_grq_lock(p, &flags);
  3684. + ns = do_task_delta_exec(p, rq);
  3685. + task_grq_unlock(&flags);
  3686. +
  3687. + return ns;
  3688. +}
  3689. +
  3690. +/*
  3691. + * Return accounted runtime for the task.
  3692. + * In case the task is currently running, return the runtime plus current's
  3693. + * pending runtime that have not been accounted yet.
  3694. + */
  3695. +unsigned long long task_sched_runtime(struct task_struct *p)
  3696. +{
  3697. + unsigned long flags;
  3698. + struct rq *rq;
  3699. + u64 ns;
  3700. +
  3701. + rq = task_grq_lock(p, &flags);
  3702. + ns = p->sched_time + do_task_delta_exec(p, rq);
  3703. + task_grq_unlock(&flags);
  3704. +
  3705. + return ns;
  3706. +}
  3707. +
  3708. +/* Compatibility crap for removal */
  3709. +void account_user_time(struct task_struct *p, cputime_t cputime,
  3710. + cputime_t cputime_scaled)
  3711. +{
  3712. +}
  3713. +
  3714. +void account_idle_time(cputime_t cputime)
  3715. +{
  3716. +}
  3717. +
  3718. +/*
  3719. + * Account guest cpu time to a process.
  3720. + * @p: the process that the cpu time gets accounted to
  3721. + * @cputime: the cpu time spent in virtual machine since the last update
  3722. + * @cputime_scaled: cputime scaled by cpu frequency
  3723. + */
  3724. +static void account_guest_time(struct task_struct *p, cputime_t cputime,
  3725. + cputime_t cputime_scaled)
  3726. +{
  3727. + u64 *cpustat = kcpustat_this_cpu->cpustat;
  3728. +
  3729. + /* Add guest time to process. */
  3730. + p->utime += (__force u64)cputime;
  3731. + p->utimescaled += (__force u64)cputime_scaled;
  3732. + account_group_user_time(p, cputime);
  3733. + p->gtime += (__force u64)cputime;
  3734. +
  3735. + /* Add guest time to cpustat. */
  3736. + if (TASK_NICE(p) > 0) {
  3737. + cpustat[CPUTIME_NICE] += (__force u64)cputime;
  3738. + cpustat[CPUTIME_GUEST_NICE] += (__force u64)cputime;
  3739. + } else {
  3740. + cpustat[CPUTIME_USER] += (__force u64)cputime;
  3741. + cpustat[CPUTIME_GUEST] += (__force u64)cputime;
  3742. + }
  3743. +}
  3744. +
  3745. +/*
  3746. + * Account system cpu time to a process and desired cpustat field
  3747. + * @p: the process that the cpu time gets accounted to
  3748. + * @cputime: the cpu time spent in kernel space since the last update
  3749. + * @cputime_scaled: cputime scaled by cpu frequency
  3750. + * @target_cputime64: pointer to cpustat field that has to be updated
  3751. + */
  3752. +static inline
  3753. +void __account_system_time(struct task_struct *p, cputime_t cputime,
  3754. + cputime_t cputime_scaled, cputime64_t *target_cputime64)
  3755. +{
  3756. + /* Add system time to process. */
  3757. + p->stime += (__force u64)cputime;
  3758. + p->stimescaled += (__force u64)cputime_scaled;
  3759. + account_group_system_time(p, cputime);
  3760. +
  3761. + /* Add system time to cpustat. */
  3762. + *target_cputime64 += (__force u64)cputime;
  3763. +
  3764. + /* Account for system time used */
  3765. + acct_update_integrals(p);
  3766. +}
  3767. +
  3768. +/*
  3769. + * Account system cpu time to a process.
  3770. + * @p: the process that the cpu time gets accounted to
  3771. + * @hardirq_offset: the offset to subtract from hardirq_count()
  3772. + * @cputime: the cpu time spent in kernel space since the last update
  3773. + * @cputime_scaled: cputime scaled by cpu frequency
  3774. + * This is for guest only now.
  3775. + */
  3776. +void account_system_time(struct task_struct *p, int hardirq_offset,
  3777. + cputime_t cputime, cputime_t cputime_scaled)
  3778. +{
  3779. +
  3780. + if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
  3781. + account_guest_time(p, cputime, cputime_scaled);
  3782. +}
  3783. +
  3784. +/*
  3785. + * Account for involuntary wait time.
  3786. + * @steal: the cpu time spent in involuntary wait
  3787. + */
  3788. +void account_steal_time(cputime_t cputime)
  3789. +{
  3790. + u64 *cpustat = kcpustat_this_cpu->cpustat;
  3791. +
  3792. + cpustat[CPUTIME_STEAL] += (__force u64)cputime;
  3793. +}
  3794. +
  3795. +/*
  3796. + * Account for idle time.
  3797. + * @cputime: the cpu time spent in idle wait
  3798. + */
  3799. +static void account_idle_times(cputime_t cputime)
  3800. +{
  3801. + u64 *cpustat = kcpustat_this_cpu->cpustat;
  3802. + struct rq *rq = this_rq();
  3803. +
  3804. + if (atomic_read(&rq->nr_iowait) > 0)
  3805. + cpustat[CPUTIME_IOWAIT] += (__force u64)cputime;
  3806. + else
  3807. + cpustat[CPUTIME_IDLE] += (__force u64)cputime;
  3808. +}
  3809. +
  3810. +#ifndef CONFIG_VIRT_CPU_ACCOUNTING
  3811. +
  3812. +void account_process_tick(struct task_struct *p, int user_tick)
  3813. +{
  3814. +}
  3815. +
  3816. +/*
  3817. + * Account multiple ticks of steal time.
  3818. + * @p: the process from which the cpu time has been stolen
  3819. + * @ticks: number of stolen ticks
  3820. + */
  3821. +void account_steal_ticks(unsigned long ticks)
  3822. +{
  3823. + account_steal_time(jiffies_to_cputime(ticks));
  3824. +}
  3825. +
  3826. +/*
  3827. + * Account multiple ticks of idle time.
  3828. + * @ticks: number of stolen ticks
  3829. + */
  3830. +void account_idle_ticks(unsigned long ticks)
  3831. +{
  3832. + account_idle_times(jiffies_to_cputime(ticks));
  3833. +}
  3834. +#endif
  3835. +
  3836. +static inline void grq_iso_lock(void)
  3837. + __acquires(grq.iso_lock)
  3838. +{
  3839. + raw_spin_lock(&grq.iso_lock);
  3840. +}
  3841. +
  3842. +static inline void grq_iso_unlock(void)
  3843. + __releases(grq.iso_lock)
  3844. +{
  3845. + raw_spin_unlock(&grq.iso_lock);
  3846. +}
  3847. +
  3848. +/*
  3849. + * Functions to test for when SCHED_ISO tasks have used their allocated
  3850. + * quota as real time scheduling and convert them back to SCHED_NORMAL.
  3851. + * Where possible, the data is tested lockless, to avoid grabbing iso_lock
  3852. + * because the occasional inaccurate result won't matter. However the
  3853. + * tick data is only ever modified under lock. iso_refractory is only simply
  3854. + * set to 0 or 1 so it's not worth grabbing the lock yet again for that.
  3855. + */
  3856. +static bool set_iso_refractory(void)
  3857. +{
  3858. + grq.iso_refractory = true;
  3859. + return grq.iso_refractory;
  3860. +}
  3861. +
  3862. +static bool clear_iso_refractory(void)
  3863. +{
  3864. + grq.iso_refractory = false;
  3865. + return grq.iso_refractory;
  3866. +}
  3867. +
  3868. +/*
  3869. + * Test if SCHED_ISO tasks have run longer than their alloted period as RT
  3870. + * tasks and set the refractory flag if necessary. There is 10% hysteresis
  3871. + * for unsetting the flag. 115/128 is ~90/100 as a fast shift instead of a
  3872. + * slow division.
  3873. + */
  3874. +static bool test_ret_isorefractory(struct rq *rq)
  3875. +{
  3876. + if (likely(!grq.iso_refractory)) {
  3877. + if (grq.iso_ticks > ISO_PERIOD * sched_iso_cpu)
  3878. + return set_iso_refractory();
  3879. + } else {
  3880. + if (grq.iso_ticks < ISO_PERIOD * (sched_iso_cpu * 115 / 128))
  3881. + return clear_iso_refractory();
  3882. + }
  3883. + return grq.iso_refractory;
  3884. +}
  3885. +
  3886. +static void iso_tick(void)
  3887. +{
  3888. + grq_iso_lock();
  3889. + grq.iso_ticks += 100;
  3890. + grq_iso_unlock();
  3891. +}
  3892. +
  3893. +/* No SCHED_ISO task was running so decrease rq->iso_ticks */
  3894. +static inline void no_iso_tick(void)
  3895. +{
  3896. + if (grq.iso_ticks) {
  3897. + grq_iso_lock();
  3898. + grq.iso_ticks -= grq.iso_ticks / ISO_PERIOD + 1;
  3899. + if (unlikely(grq.iso_refractory && grq.iso_ticks <
  3900. + ISO_PERIOD * (sched_iso_cpu * 115 / 128)))
  3901. + clear_iso_refractory();
  3902. + grq_iso_unlock();
  3903. + }
  3904. +}
  3905. +
  3906. +/* This manages tasks that have run out of timeslice during a scheduler_tick */
  3907. +static void task_running_tick(struct rq *rq)
  3908. +{
  3909. + struct task_struct *p;
  3910. +
  3911. + /*
  3912. + * If a SCHED_ISO task is running we increment the iso_ticks. In
  3913. + * order to prevent SCHED_ISO tasks from causing starvation in the
  3914. + * presence of true RT tasks we account those as iso_ticks as well.
  3915. + */
  3916. + if ((rt_queue(rq) || (iso_queue(rq) && !grq.iso_refractory))) {
  3917. + if (grq.iso_ticks <= (ISO_PERIOD * 128) - 128)
  3918. + iso_tick();
  3919. + } else
  3920. + no_iso_tick();
  3921. +
  3922. + if (iso_queue(rq)) {
  3923. + if (unlikely(test_ret_isorefractory(rq))) {
  3924. + if (rq_running_iso(rq)) {
  3925. + /*
  3926. + * SCHED_ISO task is running as RT and limit
  3927. + * has been hit. Force it to reschedule as
  3928. + * SCHED_NORMAL by zeroing its time_slice
  3929. + */
  3930. + rq->rq_time_slice = 0;
  3931. + }
  3932. + }
  3933. + }
  3934. +
  3935. + /* SCHED_FIFO tasks never run out of timeslice. */
  3936. + if (rq->rq_policy == SCHED_FIFO)
  3937. + return;
  3938. + /*
  3939. + * Tasks that were scheduled in the first half of a tick are not
  3940. + * allowed to run into the 2nd half of the next tick if they will
  3941. + * run out of time slice in the interim. Otherwise, if they have
  3942. + * less than RESCHED_US μs of time slice left they will be rescheduled.
  3943. + */
  3944. + if (rq->dither) {
  3945. + if (rq->rq_time_slice > HALF_JIFFY_US)
  3946. + return;
  3947. + else
  3948. + rq->rq_time_slice = 0;
  3949. + } else if (rq->rq_time_slice >= RESCHED_US)
  3950. + return;
  3951. +
  3952. + /* p->time_slice < RESCHED_US. We only modify task_struct under grq lock */
  3953. + p = rq->curr;
  3954. + grq_lock();
  3955. + requeue_task(p);
  3956. + set_tsk_need_resched(p);
  3957. + grq_unlock();
  3958. +}
  3959. +
  3960. +void wake_up_idle_cpu(int cpu);
  3961. +
  3962. +/*
  3963. + * This function gets called by the timer code, with HZ frequency.
  3964. + * We call it with interrupts disabled. The data modified is all
  3965. + * local to struct rq so we don't need to grab grq lock.
  3966. + */
  3967. +void scheduler_tick(void)
  3968. +{
  3969. + int cpu __maybe_unused = smp_processor_id();
  3970. + struct rq *rq = cpu_rq(cpu);
  3971. +
  3972. + sched_clock_tick();
  3973. + /* grq lock not grabbed, so only update rq clock */
  3974. + update_rq_clock(rq);
  3975. + update_cpu_clock(rq, rq->curr, true);
  3976. + if (!rq_idle(rq))
  3977. + task_running_tick(rq);
  3978. + else
  3979. + no_iso_tick();
  3980. + rq->last_tick = rq->clock;
  3981. + perf_event_task_tick();
  3982. +}
  3983. +
  3984. +notrace unsigned long get_parent_ip(unsigned long addr)
  3985. +{
  3986. + if (in_lock_functions(addr)) {
  3987. + addr = CALLER_ADDR2;
  3988. + if (in_lock_functions(addr))
  3989. + addr = CALLER_ADDR3;
  3990. + }
  3991. + return addr;
  3992. +}
  3993. +
  3994. +#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  3995. + defined(CONFIG_PREEMPT_TRACER))
  3996. +void __kprobes add_preempt_count(int val)
  3997. +{
  3998. +#ifdef CONFIG_DEBUG_PREEMPT
  3999. + /*
  4000. + * Underflow?
  4001. + */
  4002. + if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
  4003. + return;
  4004. +#endif
  4005. + preempt_count() += val;
  4006. +#ifdef CONFIG_DEBUG_PREEMPT
  4007. + /*
  4008. + * Spinlock count overflowing soon?
  4009. + */
  4010. + DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
  4011. + PREEMPT_MASK - 10);
  4012. +#endif
  4013. + if (preempt_count() == val)
  4014. + trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  4015. +}
  4016. +EXPORT_SYMBOL(add_preempt_count);
  4017. +
  4018. +void __kprobes sub_preempt_count(int val)
  4019. +{
  4020. +#ifdef CONFIG_DEBUG_PREEMPT
  4021. + /*
  4022. + * Underflow?
  4023. + */
  4024. + if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
  4025. + return;
  4026. + /*
  4027. + * Is the spinlock portion underflowing?
  4028. + */
  4029. + if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
  4030. + !(preempt_count() & PREEMPT_MASK)))
  4031. + return;
  4032. +#endif
  4033. +
  4034. + if (preempt_count() == val)
  4035. + trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  4036. + preempt_count() -= val;
  4037. +}
  4038. +EXPORT_SYMBOL(sub_preempt_count);
  4039. +#endif
  4040. +
  4041. +/*
  4042. + * Deadline is "now" in niffies + (offset by priority). Setting the deadline
  4043. + * is the key to everything. It distributes cpu fairly amongst tasks of the
  4044. + * same nice value, it proportions cpu according to nice level, it means the
  4045. + * task that last woke up the longest ago has the earliest deadline, thus
  4046. + * ensuring that interactive tasks get low latency on wake up. The CPU
  4047. + * proportion works out to the square of the virtual deadline difference, so
  4048. + * this equation will give nice 19 3% CPU compared to nice 0.
  4049. + */
  4050. +static inline u64 prio_deadline_diff(int user_prio)
  4051. +{
  4052. + return (prio_ratios[user_prio] * rr_interval * (MS_TO_NS(1) / 128));
  4053. +}
  4054. +
  4055. +static inline u64 task_deadline_diff(struct task_struct *p)
  4056. +{
  4057. + return prio_deadline_diff(TASK_USER_PRIO(p));
  4058. +}
  4059. +
  4060. +static inline u64 static_deadline_diff(int static_prio)
  4061. +{
  4062. + return prio_deadline_diff(USER_PRIO(static_prio));
  4063. +}
  4064. +
  4065. +static inline int longest_deadline_diff(void)
  4066. +{
  4067. + return prio_deadline_diff(39);
  4068. +}
  4069. +
  4070. +static inline int ms_longest_deadline_diff(void)
  4071. +{
  4072. + return NS_TO_MS(longest_deadline_diff());
  4073. +}
  4074. +
  4075. +/*
  4076. + * The time_slice is only refilled when it is empty and that is when we set a
  4077. + * new deadline.
  4078. + */
  4079. +static void time_slice_expired(struct task_struct *p)
  4080. +{
  4081. + p->time_slice = timeslice();
  4082. + p->deadline = grq.niffies + task_deadline_diff(p);
  4083. +}
  4084. +
  4085. +/*
  4086. + * Timeslices below RESCHED_US are considered as good as expired as there's no
  4087. + * point rescheduling when there's so little time left. SCHED_BATCH tasks
  4088. + * have been flagged be not latency sensitive and likely to be fully CPU
  4089. + * bound so every time they're rescheduled they have their time_slice
  4090. + * refilled, but get a new later deadline to have little effect on
  4091. + * SCHED_NORMAL tasks.
  4092. +
  4093. + */
  4094. +static inline void check_deadline(struct task_struct *p)
  4095. +{
  4096. + if (p->time_slice < RESCHED_US || batch_task(p))
  4097. + time_slice_expired(p);
  4098. +}
  4099. +
  4100. +#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  4101. +
  4102. +/*
  4103. + * Scheduler queue bitmap specific find next bit.
  4104. + */
  4105. +static inline unsigned long
  4106. +next_sched_bit(const unsigned long *addr, unsigned long offset)
  4107. +{
  4108. + const unsigned long *p;
  4109. + unsigned long result;
  4110. + unsigned long size;
  4111. + unsigned long tmp;
  4112. +
  4113. + size = PRIO_LIMIT;
  4114. + if (offset >= size)
  4115. + return size;
  4116. +
  4117. + p = addr + BITOP_WORD(offset);
  4118. + result = offset & ~(BITS_PER_LONG-1);
  4119. + size -= result;
  4120. + offset %= BITS_PER_LONG;
  4121. + if (offset) {
  4122. + tmp = *(p++);
  4123. + tmp &= (~0UL << offset);
  4124. + if (size < BITS_PER_LONG)
  4125. + goto found_first;
  4126. + if (tmp)
  4127. + goto found_middle;
  4128. + size -= BITS_PER_LONG;
  4129. + result += BITS_PER_LONG;
  4130. + }
  4131. + while (size & ~(BITS_PER_LONG-1)) {
  4132. + if ((tmp = *(p++)))
  4133. + goto found_middle;
  4134. + result += BITS_PER_LONG;
  4135. + size -= BITS_PER_LONG;
  4136. + }
  4137. + if (!size)
  4138. + return result;
  4139. + tmp = *p;
  4140. +
  4141. +found_first:
  4142. + tmp &= (~0UL >> (BITS_PER_LONG - size));
  4143. + if (tmp == 0UL) /* Are any bits set? */
  4144. + return result + size; /* Nope. */
  4145. +found_middle:
  4146. + return result + __ffs(tmp);
  4147. +}
  4148. +
  4149. +/*
  4150. + * O(n) lookup of all tasks in the global runqueue. The real brainfuck
  4151. + * of lock contention and O(n). It's not really O(n) as only the queued,
  4152. + * but not running tasks are scanned, and is O(n) queued in the worst case
  4153. + * scenario only because the right task can be found before scanning all of
  4154. + * them.
  4155. + * Tasks are selected in this order:
  4156. + * Real time tasks are selected purely by their static priority and in the
  4157. + * order they were queued, so the lowest value idx, and the first queued task
  4158. + * of that priority value is chosen.
  4159. + * If no real time tasks are found, the SCHED_ISO priority is checked, and
  4160. + * all SCHED_ISO tasks have the same priority value, so they're selected by
  4161. + * the earliest deadline value.
  4162. + * If no SCHED_ISO tasks are found, SCHED_NORMAL tasks are selected by the
  4163. + * earliest deadline.
  4164. + * Finally if no SCHED_NORMAL tasks are found, SCHED_IDLEPRIO tasks are
  4165. + * selected by the earliest deadline.
  4166. + */
  4167. +static inline struct
  4168. +task_struct *earliest_deadline_task(struct rq *rq, int cpu, struct task_struct *idle)
  4169. +{
  4170. + struct task_struct *edt = NULL;
  4171. + unsigned long idx = -1;
  4172. +
  4173. + do {
  4174. + struct list_head *queue;
  4175. + struct task_struct *p;
  4176. + u64 earliest_deadline;
  4177. +
  4178. + idx = next_sched_bit(grq.prio_bitmap, ++idx);
  4179. + if (idx >= PRIO_LIMIT)
  4180. + return idle;
  4181. + queue = grq.queue + idx;
  4182. +
  4183. + if (idx < MAX_RT_PRIO) {
  4184. + /* We found an rt task */
  4185. + list_for_each_entry(p, queue, run_list) {
  4186. + /* Make sure cpu affinity is ok */
  4187. + if (needs_other_cpu(p, cpu))
  4188. + continue;
  4189. + edt = p;
  4190. + goto out_take;
  4191. + }
  4192. + /*
  4193. + * None of the RT tasks at this priority can run on
  4194. + * this cpu
  4195. + */
  4196. + continue;
  4197. + }
  4198. +
  4199. + /*
  4200. + * No rt tasks. Find the earliest deadline task. Now we're in
  4201. + * O(n) territory.
  4202. + */
  4203. + earliest_deadline = ~0ULL;
  4204. + list_for_each_entry(p, queue, run_list) {
  4205. + u64 dl;
  4206. +
  4207. + /* Make sure cpu affinity is ok */
  4208. + if (needs_other_cpu(p, cpu))
  4209. + continue;
  4210. +
  4211. + /*
  4212. + * Soft affinity happens here by not scheduling a task
  4213. + * with its sticky flag set that ran on a different CPU
  4214. + * last when the CPU is scaling, or by greatly biasing
  4215. + * against its deadline when not, based on cpu cache
  4216. + * locality.
  4217. + */
  4218. + if (task_sticky(p) && task_rq(p) != rq) {
  4219. + if (scaling_rq(rq))
  4220. + continue;
  4221. + dl = p->deadline << locality_diff(p, rq);
  4222. + } else
  4223. + dl = p->deadline;
  4224. +
  4225. + if (deadline_before(dl, earliest_deadline)) {
  4226. + earliest_deadline = dl;
  4227. + edt = p;
  4228. + }
  4229. + }
  4230. + } while (!edt);
  4231. +
  4232. +out_take:
  4233. + take_task(cpu, edt);
  4234. + return edt;
  4235. +}
  4236. +
  4237. +
  4238. +/*
  4239. + * Print scheduling while atomic bug:
  4240. + */
  4241. +static noinline void __schedule_bug(struct task_struct *prev)
  4242. +{
  4243. + if (oops_in_progress)
  4244. + return;
  4245. +
  4246. + printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
  4247. + prev->comm, prev->pid, preempt_count());
  4248. +
  4249. + debug_show_held_locks(prev);
  4250. + print_modules();
  4251. + if (irqs_disabled())
  4252. + print_irqtrace_events(prev);
  4253. + dump_stack();
  4254. +}
  4255. +
  4256. +/*
  4257. + * Various schedule()-time debugging checks and statistics:
  4258. + */
  4259. +static inline void schedule_debug(struct task_struct *prev)
  4260. +{
  4261. + /*
  4262. + * Test if we are atomic. Since do_exit() needs to call into
  4263. + * schedule() atomically, we ignore that path for now.
  4264. + * Otherwise, whine if we are scheduling when we should not be.
  4265. + */
  4266. + if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
  4267. + __schedule_bug(prev);
  4268. + rcu_sleep_check();
  4269. +
  4270. + profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  4271. +
  4272. + schedstat_inc(this_rq(), sched_count);
  4273. +}
  4274. +
  4275. +/*
  4276. + * The currently running task's information is all stored in rq local data
  4277. + * which is only modified by the local CPU, thereby allowing the data to be
  4278. + * changed without grabbing the grq lock.
  4279. + */
  4280. +static inline void set_rq_task(struct rq *rq, struct task_struct *p)
  4281. +{
  4282. + rq->rq_time_slice = p->time_slice;
  4283. + rq->rq_deadline = p->deadline;
  4284. + rq->rq_last_ran = p->last_ran = rq->clock;
  4285. + rq->rq_policy = p->policy;
  4286. + rq->rq_prio = p->prio;
  4287. + if (p != rq->idle)
  4288. + rq->rq_running = true;
  4289. + else
  4290. + rq->rq_running = false;
  4291. +}
  4292. +
  4293. +static void reset_rq_task(struct rq *rq, struct task_struct *p)
  4294. +{
  4295. + rq->rq_policy = p->policy;
  4296. + rq->rq_prio = p->prio;
  4297. +}
  4298. +
  4299. +/*
  4300. + * schedule() is the main scheduler function.
  4301. + */
  4302. +asmlinkage void __sched schedule(void)
  4303. +{
  4304. + struct task_struct *prev, *next, *idle;
  4305. + unsigned long *switch_count;
  4306. + bool deactivate;
  4307. + struct rq *rq;
  4308. + int cpu;
  4309. +
  4310. +need_resched:
  4311. + preempt_disable();
  4312. + cpu = smp_processor_id();
  4313. + rq = cpu_rq(cpu);
  4314. + rcu_note_context_switch(cpu);
  4315. + prev = rq->curr;
  4316. +
  4317. + deactivate = false;
  4318. + schedule_debug(prev);
  4319. +
  4320. + grq_lock_irq();
  4321. +
  4322. + switch_count = &prev->nivcsw;
  4323. + if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  4324. + if (unlikely(signal_pending_state(prev->state, prev))) {
  4325. + prev->state = TASK_RUNNING;
  4326. + } else {
  4327. + deactivate = true;
  4328. + /*
  4329. + * If a worker is going to sleep, notify and
  4330. + * ask workqueue whether it wants to wake up a
  4331. + * task to maintain concurrency. If so, wake
  4332. + * up the task.
  4333. + */
  4334. + if (prev->flags & PF_WQ_WORKER) {
  4335. + struct task_struct *to_wakeup;
  4336. +
  4337. + to_wakeup = wq_worker_sleeping(prev, cpu);
  4338. + if (to_wakeup) {
  4339. + /* This shouldn't happen, but does */
  4340. + if (unlikely(to_wakeup == prev))
  4341. + deactivate = false;
  4342. + else
  4343. + try_to_wake_up_local(to_wakeup);
  4344. + }
  4345. + }
  4346. + }
  4347. + switch_count = &prev->nvcsw;
  4348. + }
  4349. +
  4350. + /*
  4351. + * If we are going to sleep and we have plugged IO queued, make
  4352. + * sure to submit it to avoid deadlocks.
  4353. + */
  4354. + if (unlikely(deactivate && blk_needs_flush_plug(prev))) {
  4355. + grq_unlock_irq();
  4356. + preempt_enable_no_resched();
  4357. + blk_schedule_flush_plug(prev);
  4358. + goto need_resched;
  4359. + }
  4360. +
  4361. + update_clocks(rq);
  4362. + update_cpu_clock(rq, prev, false);
  4363. + if (rq->clock - rq->last_tick > HALF_JIFFY_NS)
  4364. + rq->dither = false;
  4365. + else
  4366. + rq->dither = true;
  4367. +
  4368. + clear_tsk_need_resched(prev);
  4369. +
  4370. + idle = rq->idle;
  4371. + if (idle != prev) {
  4372. + /* Update all the information stored on struct rq */
  4373. + prev->time_slice = rq->rq_time_slice;
  4374. + prev->deadline = rq->rq_deadline;
  4375. + check_deadline(prev);
  4376. + prev->last_ran = rq->clock;
  4377. +
  4378. + /* Task changed affinity off this CPU */
  4379. + if (needs_other_cpu(prev, cpu))
  4380. + resched_suitable_idle(prev);
  4381. + else if (!deactivate) {
  4382. + if (!queued_notrunning()) {
  4383. + /*
  4384. + * We now know prev is the only thing that is
  4385. + * awaiting CPU so we can bypass rechecking for
  4386. + * the earliest deadline task and just run it
  4387. + * again.
  4388. + */
  4389. + set_rq_task(rq, prev);
  4390. + grq_unlock_irq();
  4391. + goto rerun_prev_unlocked;
  4392. + } else
  4393. + swap_sticky(rq, cpu, prev);
  4394. + }
  4395. + return_task(prev, deactivate);
  4396. + }
  4397. +
  4398. + if (unlikely(!queued_notrunning())) {
  4399. + /*
  4400. + * This CPU is now truly idle as opposed to when idle is
  4401. + * scheduled as a high priority task in its own right.
  4402. + */
  4403. + next = idle;
  4404. + schedstat_inc(rq, sched_goidle);
  4405. + set_cpuidle_map(cpu);
  4406. + } else {
  4407. + next = earliest_deadline_task(rq, cpu, idle);
  4408. + if (likely(next->prio != PRIO_LIMIT))
  4409. + clear_cpuidle_map(cpu);
  4410. + else
  4411. + set_cpuidle_map(cpu);
  4412. + }
  4413. +
  4414. + if (likely(prev != next)) {
  4415. + /*
  4416. + * Don't stick tasks when a real time task is going to run as
  4417. + * they may literally get stuck.
  4418. + */
  4419. + if (rt_task(next))
  4420. + unstick_task(rq, prev);
  4421. + set_rq_task(rq, next);
  4422. + grq.nr_switches++;
  4423. + prev->on_cpu = false;
  4424. + next->on_cpu = true;
  4425. + rq->curr = next;
  4426. + ++*switch_count;
  4427. +
  4428. + context_switch(rq, prev, next); /* unlocks the grq */
  4429. + /*
  4430. + * The context switch have flipped the stack from under us
  4431. + * and restored the local variables which were saved when
  4432. + * this task called schedule() in the past. prev == current
  4433. + * is still correct, but it can be moved to another cpu/rq.
  4434. + */
  4435. + cpu = smp_processor_id();
  4436. + rq = cpu_rq(cpu);
  4437. + idle = rq->idle;
  4438. + } else
  4439. + grq_unlock_irq();
  4440. +
  4441. +rerun_prev_unlocked:
  4442. + sched_preempt_enable_no_resched();
  4443. + if (unlikely(need_resched()))
  4444. + goto need_resched;
  4445. +}
  4446. +EXPORT_SYMBOL(schedule);
  4447. +
  4448. +/**
  4449. + * schedule_preempt_disabled - called with preemption disabled
  4450. + *
  4451. + * Returns with preemption disabled. Note: preempt_count must be 1
  4452. + */
  4453. +void __sched schedule_preempt_disabled(void)
  4454. +{
  4455. + sched_preempt_enable_no_resched();
  4456. + schedule();
  4457. + preempt_disable();
  4458. +}
  4459. +
  4460. +#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
  4461. +
  4462. +static inline bool owner_running(struct mutex *lock, struct task_struct *owner)
  4463. +{
  4464. + if (lock->owner != owner)
  4465. + return false;
  4466. +
  4467. + /*
  4468. + * Ensure we emit the owner->on_cpu, dereference _after_ checking
  4469. + * lock->owner still matches owner, if that fails, owner might
  4470. + * point to free()d memory, if it still matches, the rcu_read_lock()
  4471. + * ensures the memory stays valid.
  4472. + */
  4473. + barrier();
  4474. +
  4475. + return owner->on_cpu;
  4476. +}
  4477. +
  4478. +/*
  4479. + * Look out! "owner" is an entirely speculative pointer
  4480. + * access and not reliable.
  4481. + */
  4482. +int mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)
  4483. +{
  4484. + rcu_read_lock();
  4485. + while (owner_running(lock, owner)) {
  4486. + if (need_resched())
  4487. + break;
  4488. +
  4489. + arch_mutex_cpu_relax();
  4490. + }
  4491. + rcu_read_unlock();
  4492. +
  4493. + /*
  4494. + * We break out the loop above on need_resched() and when the
  4495. + * owner changed, which is a sign for heavy contention. Return
  4496. + * success only when lock->owner is NULL.
  4497. + */
  4498. + return lock->owner == NULL;
  4499. +}
  4500. +#endif
  4501. +
  4502. +#ifdef CONFIG_PREEMPT
  4503. +/*
  4504. + * this is the entry point to schedule() from in-kernel preemption
  4505. + * off of preempt_enable. Kernel preemptions off return from interrupt
  4506. + * occur there and call schedule directly.
  4507. + */
  4508. +asmlinkage void __sched notrace preempt_schedule(void)
  4509. +{
  4510. + struct thread_info *ti = current_thread_info();
  4511. +
  4512. + /*
  4513. + * If there is a non-zero preempt_count or interrupts are disabled,
  4514. + * we do not want to preempt the current task. Just return..
  4515. + */
  4516. + if (likely(ti->preempt_count || irqs_disabled()))
  4517. + return;
  4518. +
  4519. + do {
  4520. + add_preempt_count_notrace(PREEMPT_ACTIVE);
  4521. + schedule();
  4522. + sub_preempt_count_notrace(PREEMPT_ACTIVE);
  4523. +
  4524. + /*
  4525. + * Check again in case we missed a preemption opportunity
  4526. + * between schedule and now.
  4527. + */
  4528. + barrier();
  4529. + } while (need_resched());
  4530. +}
  4531. +EXPORT_SYMBOL(preempt_schedule);
  4532. +
  4533. +/*
  4534. + * this is the entry point to schedule() from kernel preemption
  4535. + * off of irq context.
  4536. + * Note, that this is called and return with irqs disabled. This will
  4537. + * protect us against recursive calling from irq.
  4538. + */
  4539. +asmlinkage void __sched preempt_schedule_irq(void)
  4540. +{
  4541. + struct thread_info *ti = current_thread_info();
  4542. +
  4543. + /* Catch callers which need to be fixed */
  4544. + BUG_ON(ti->preempt_count || !irqs_disabled());
  4545. +
  4546. + do {
  4547. + add_preempt_count(PREEMPT_ACTIVE);
  4548. + local_irq_enable();
  4549. + schedule();
  4550. + local_irq_disable();
  4551. + sub_preempt_count(PREEMPT_ACTIVE);
  4552. +
  4553. + /*
  4554. + * Check again in case we missed a preemption opportunity
  4555. + * between schedule and now.
  4556. + */
  4557. + barrier();
  4558. + } while (need_resched());
  4559. +}
  4560. +
  4561. +#endif /* CONFIG_PREEMPT */
  4562. +
  4563. +int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
  4564. + void *key)
  4565. +{
  4566. + return try_to_wake_up(curr->private, mode, wake_flags);
  4567. +}
  4568. +EXPORT_SYMBOL(default_wake_function);
  4569. +
  4570. +/*
  4571. + * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  4572. + * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  4573. + * number) then we wake all the non-exclusive tasks and one exclusive task.
  4574. + *
  4575. + * There are circumstances in which we can try to wake a task which has already
  4576. + * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  4577. + * zero in this (rare) case, and we handle it by continuing to scan the queue.
  4578. + */
  4579. +static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  4580. + int nr_exclusive, int wake_flags, void *key)
  4581. +{
  4582. + struct list_head *tmp, *next;
  4583. +
  4584. + list_for_each_safe(tmp, next, &q->task_list) {
  4585. + wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);
  4586. + unsigned int flags = curr->flags;
  4587. +
  4588. + if (curr->func(curr, mode, wake_flags, key) &&
  4589. + (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  4590. + break;
  4591. + }
  4592. +}
  4593. +
  4594. +/**
  4595. + * __wake_up - wake up threads blocked on a waitqueue.
  4596. + * @q: the waitqueue
  4597. + * @mode: which threads
  4598. + * @nr_exclusive: how many wake-one or wake-many threads to wake up
  4599. + * @key: is directly passed to the wakeup function
  4600. + *
  4601. + * It may be assumed that this function implies a write memory barrier before
  4602. + * changing the task state if and only if any tasks are woken up.
  4603. + */
  4604. +void __wake_up(wait_queue_head_t *q, unsigned int mode,
  4605. + int nr_exclusive, void *key)
  4606. +{
  4607. + unsigned long flags;
  4608. +
  4609. + spin_lock_irqsave(&q->lock, flags);
  4610. + __wake_up_common(q, mode, nr_exclusive, 0, key);
  4611. + spin_unlock_irqrestore(&q->lock, flags);
  4612. +}
  4613. +EXPORT_SYMBOL(__wake_up);
  4614. +
  4615. +/*
  4616. + * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  4617. + */
  4618. +void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr)
  4619. +{
  4620. + __wake_up_common(q, mode, nr, 0, NULL);
  4621. +}
  4622. +EXPORT_SYMBOL_GPL(__wake_up_locked);
  4623. +
  4624. +void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
  4625. +{
  4626. + __wake_up_common(q, mode, 1, 0, key);
  4627. +}
  4628. +EXPORT_SYMBOL_GPL(__wake_up_locked_key);
  4629. +
  4630. +/**
  4631. + * __wake_up_sync_key - wake up threads blocked on a waitqueue.
  4632. + * @q: the waitqueue
  4633. + * @mode: which threads
  4634. + * @nr_exclusive: how many wake-one or wake-many threads to wake up
  4635. + * @key: opaque value to be passed to wakeup targets
  4636. + *
  4637. + * The sync wakeup differs that the waker knows that it will schedule
  4638. + * away soon, so while the target thread will be woken up, it will not
  4639. + * be migrated to another CPU - ie. the two threads are 'synchronised'
  4640. + * with each other. This can prevent needless bouncing between CPUs.
  4641. + *
  4642. + * On UP it can prevent extra preemption.
  4643. + *
  4644. + * It may be assumed that this function implies a write memory barrier before
  4645. + * changing the task state if and only if any tasks are woken up.
  4646. + */
  4647. +void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
  4648. + int nr_exclusive, void *key)
  4649. +{
  4650. + unsigned long flags;
  4651. + int wake_flags = WF_SYNC;
  4652. +
  4653. + if (unlikely(!q))
  4654. + return;
  4655. +
  4656. + if (unlikely(!nr_exclusive))
  4657. + wake_flags = 0;
  4658. +
  4659. + spin_lock_irqsave(&q->lock, flags);
  4660. + __wake_up_common(q, mode, nr_exclusive, wake_flags, key);
  4661. + spin_unlock_irqrestore(&q->lock, flags);
  4662. +}
  4663. +EXPORT_SYMBOL_GPL(__wake_up_sync_key);
  4664. +
  4665. +/**
  4666. + * __wake_up_sync - wake up threads blocked on a waitqueue.
  4667. + * @q: the waitqueue
  4668. + * @mode: which threads
  4669. + * @nr_exclusive: how many wake-one or wake-many threads to wake up
  4670. + *
  4671. + * The sync wakeup differs that the waker knows that it will schedule
  4672. + * away soon, so while the target thread will be woken up, it will not
  4673. + * be migrated to another CPU - ie. the two threads are 'synchronised'
  4674. + * with each other. This can prevent needless bouncing between CPUs.
  4675. + *
  4676. + * On UP it can prevent extra preemption.
  4677. + */
  4678. +void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  4679. +{
  4680. + unsigned long flags;
  4681. + int sync = 1;
  4682. +
  4683. + if (unlikely(!q))
  4684. + return;
  4685. +
  4686. + if (unlikely(!nr_exclusive))
  4687. + sync = 0;
  4688. +
  4689. + spin_lock_irqsave(&q->lock, flags);
  4690. + __wake_up_common(q, mode, nr_exclusive, sync, NULL);
  4691. + spin_unlock_irqrestore(&q->lock, flags);
  4692. +}
  4693. +EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  4694. +
  4695. +/**
  4696. + * complete: - signals a single thread waiting on this completion
  4697. + * @x: holds the state of this particular completion
  4698. + *
  4699. + * This will wake up a single thread waiting on this completion. Threads will be
  4700. + * awakened in the same order in which they were queued.
  4701. + *
  4702. + * See also complete_all(), wait_for_completion() and related routines.
  4703. + *
  4704. + * It may be assumed that this function implies a write memory barrier before
  4705. + * changing the task state if and only if any tasks are woken up.
  4706. + */
  4707. +void complete(struct completion *x)
  4708. +{
  4709. + unsigned long flags;
  4710. +
  4711. + spin_lock_irqsave(&x->wait.lock, flags);
  4712. + x->done++;
  4713. + __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
  4714. + spin_unlock_irqrestore(&x->wait.lock, flags);
  4715. +}
  4716. +EXPORT_SYMBOL(complete);
  4717. +
  4718. +/**
  4719. + * complete_all: - signals all threads waiting on this completion
  4720. + * @x: holds the state of this particular completion
  4721. + *
  4722. + * This will wake up all threads waiting on this particular completion event.
  4723. + *
  4724. + * It may be assumed that this function implies a write memory barrier before
  4725. + * changing the task state if and only if any tasks are woken up.
  4726. + */
  4727. +void complete_all(struct completion *x)
  4728. +{
  4729. + unsigned long flags;
  4730. +
  4731. + spin_lock_irqsave(&x->wait.lock, flags);
  4732. + x->done += UINT_MAX/2;
  4733. + __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
  4734. + spin_unlock_irqrestore(&x->wait.lock, flags);
  4735. +}
  4736. +EXPORT_SYMBOL(complete_all);
  4737. +
  4738. +static inline long __sched
  4739. +do_wait_for_common(struct completion *x, long timeout, int state)
  4740. +{
  4741. + if (!x->done) {
  4742. + DECLARE_WAITQUEUE(wait, current);
  4743. +
  4744. + __add_wait_queue_tail_exclusive(&x->wait, &wait);
  4745. + do {
  4746. + if (signal_pending_state(state, current)) {
  4747. + timeout = -ERESTARTSYS;
  4748. + break;
  4749. + }
  4750. + __set_current_state(state);
  4751. + spin_unlock_irq(&x->wait.lock);
  4752. + timeout = schedule_timeout(timeout);
  4753. + spin_lock_irq(&x->wait.lock);
  4754. + } while (!x->done && timeout);
  4755. + __remove_wait_queue(&x->wait, &wait);
  4756. + if (!x->done)
  4757. + return timeout;
  4758. + }
  4759. + x->done--;
  4760. + return timeout ?: 1;
  4761. +}
  4762. +
  4763. +static long __sched
  4764. +wait_for_common(struct completion *x, long timeout, int state)
  4765. +{
  4766. + might_sleep();
  4767. +
  4768. + spin_lock_irq(&x->wait.lock);
  4769. + timeout = do_wait_for_common(x, timeout, state);
  4770. + spin_unlock_irq(&x->wait.lock);
  4771. + return timeout;
  4772. +}
  4773. +
  4774. +/**
  4775. + * wait_for_completion: - waits for completion of a task
  4776. + * @x: holds the state of this particular completion
  4777. + *
  4778. + * This waits to be signaled for completion of a specific task. It is NOT
  4779. + * interruptible and there is no timeout.
  4780. + *
  4781. + * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
  4782. + * and interrupt capability. Also see complete().
  4783. + */
  4784. +void __sched wait_for_completion(struct completion *x)
  4785. +{
  4786. + wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
  4787. +}
  4788. +EXPORT_SYMBOL(wait_for_completion);
  4789. +
  4790. +/**
  4791. + * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
  4792. + * @x: holds the state of this particular completion
  4793. + * @timeout: timeout value in jiffies
  4794. + *
  4795. + * This waits for either a completion of a specific task to be signaled or for a
  4796. + * specified timeout to expire. The timeout is in jiffies. It is not
  4797. + * interruptible.
  4798. + *
  4799. + * The return value is 0 if timed out, and positive (at least 1, or number of
  4800. + * jiffies left till timeout) if completed.
  4801. + */
  4802. +unsigned long __sched
  4803. +wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  4804. +{
  4805. + return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
  4806. +}
  4807. +EXPORT_SYMBOL(wait_for_completion_timeout);
  4808. +
  4809. +/**
  4810. + * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
  4811. + * @x: holds the state of this particular completion
  4812. + *
  4813. + * This waits for completion of a specific task to be signaled. It is
  4814. + * interruptible.
  4815. + *
  4816. + * The return value is -ERESTARTSYS if interrupted, 0 if completed.
  4817. + */
  4818. +int __sched wait_for_completion_interruptible(struct completion *x)
  4819. +{
  4820. + long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
  4821. + if (t == -ERESTARTSYS)
  4822. + return t;
  4823. + return 0;
  4824. +}
  4825. +EXPORT_SYMBOL(wait_for_completion_interruptible);
  4826. +
  4827. +/**
  4828. + * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
  4829. + * @x: holds the state of this particular completion
  4830. + * @timeout: timeout value in jiffies
  4831. + *
  4832. + * This waits for either a completion of a specific task to be signaled or for a
  4833. + * specified timeout to expire. It is interruptible. The timeout is in jiffies.
  4834. + *
  4835. + * The return value is -ERESTARTSYS if interrupted, 0 if timed out,
  4836. + * positive (at least 1, or number of jiffies left till timeout) if completed.
  4837. + */
  4838. +long __sched
  4839. +wait_for_completion_interruptible_timeout(struct completion *x,
  4840. + unsigned long timeout)
  4841. +{
  4842. + return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
  4843. +}
  4844. +EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  4845. +
  4846. +/**
  4847. + * wait_for_completion_killable: - waits for completion of a task (killable)
  4848. + * @x: holds the state of this particular completion
  4849. + *
  4850. + * This waits to be signaled for completion of a specific task. It can be
  4851. + * interrupted by a kill signal.
  4852. + *
  4853. + * The return value is -ERESTARTSYS if interrupted, 0 if timed out,
  4854. + * positive (at least 1, or number of jiffies left till timeout) if completed.
  4855. + */
  4856. +int __sched wait_for_completion_killable(struct completion *x)
  4857. +{
  4858. + long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
  4859. + if (t == -ERESTARTSYS)
  4860. + return t;
  4861. + return 0;
  4862. +}
  4863. +EXPORT_SYMBOL(wait_for_completion_killable);
  4864. +
  4865. +/**
  4866. + * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
  4867. + * @x: holds the state of this particular completion
  4868. + * @timeout: timeout value in jiffies
  4869. + *
  4870. + * This waits for either a completion of a specific task to be
  4871. + * signaled or for a specified timeout to expire. It can be
  4872. + * interrupted by a kill signal. The timeout is in jiffies.
  4873. + */
  4874. +long __sched
  4875. +wait_for_completion_killable_timeout(struct completion *x,
  4876. + unsigned long timeout)
  4877. +{
  4878. + return wait_for_common(x, timeout, TASK_KILLABLE);
  4879. +}
  4880. +EXPORT_SYMBOL(wait_for_completion_killable_timeout);
  4881. +
  4882. +/**
  4883. + * try_wait_for_completion - try to decrement a completion without blocking
  4884. + * @x: completion structure
  4885. + *
  4886. + * Returns: 0 if a decrement cannot be done without blocking
  4887. + * 1 if a decrement succeeded.
  4888. + *
  4889. + * If a completion is being used as a counting completion,
  4890. + * attempt to decrement the counter without blocking. This
  4891. + * enables us to avoid waiting if the resource the completion
  4892. + * is protecting is not available.
  4893. + */
  4894. +bool try_wait_for_completion(struct completion *x)
  4895. +{
  4896. + unsigned long flags;
  4897. + int ret = 1;
  4898. +
  4899. + spin_lock_irqsave(&x->wait.lock, flags);
  4900. + if (!x->done)
  4901. + ret = 0;
  4902. + else
  4903. + x->done--;
  4904. + spin_unlock_irqrestore(&x->wait.lock, flags);
  4905. + return ret;
  4906. +}
  4907. +EXPORT_SYMBOL(try_wait_for_completion);
  4908. +
  4909. +/**
  4910. + * completion_done - Test to see if a completion has any waiters
  4911. + * @x: completion structure
  4912. + *
  4913. + * Returns: 0 if there are waiters (wait_for_completion() in progress)
  4914. + * 1 if there are no waiters.
  4915. + *
  4916. + */
  4917. +bool completion_done(struct completion *x)
  4918. +{
  4919. + unsigned long flags;
  4920. + int ret = 1;
  4921. +
  4922. + spin_lock_irqsave(&x->wait.lock, flags);
  4923. + if (!x->done)
  4924. + ret = 0;
  4925. + spin_unlock_irqrestore(&x->wait.lock, flags);
  4926. + return ret;
  4927. +}
  4928. +EXPORT_SYMBOL(completion_done);
  4929. +
  4930. +static long __sched
  4931. +sleep_on_common(wait_queue_head_t *q, int state, long timeout)
  4932. +{
  4933. + unsigned long flags;
  4934. + wait_queue_t wait;
  4935. +
  4936. + init_waitqueue_entry(&wait, current);
  4937. +
  4938. + __set_current_state(state);
  4939. +
  4940. + spin_lock_irqsave(&q->lock, flags);
  4941. + __add_wait_queue(q, &wait);
  4942. + spin_unlock(&q->lock);
  4943. + timeout = schedule_timeout(timeout);
  4944. + spin_lock_irq(&q->lock);
  4945. + __remove_wait_queue(q, &wait);
  4946. + spin_unlock_irqrestore(&q->lock, flags);
  4947. +
  4948. + return timeout;
  4949. +}
  4950. +
  4951. +void __sched interruptible_sleep_on(wait_queue_head_t *q)
  4952. +{
  4953. + sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  4954. +}
  4955. +EXPORT_SYMBOL(interruptible_sleep_on);
  4956. +
  4957. +long __sched
  4958. +interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
  4959. +{
  4960. + return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
  4961. +}
  4962. +EXPORT_SYMBOL(interruptible_sleep_on_timeout);
  4963. +
  4964. +void __sched sleep_on(wait_queue_head_t *q)
  4965. +{
  4966. + sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  4967. +}
  4968. +EXPORT_SYMBOL(sleep_on);
  4969. +
  4970. +long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
  4971. +{
  4972. + return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
  4973. +}
  4974. +EXPORT_SYMBOL(sleep_on_timeout);
  4975. +
  4976. +#ifdef CONFIG_RT_MUTEXES
  4977. +
  4978. +/*
  4979. + * rt_mutex_setprio - set the current priority of a task
  4980. + * @p: task
  4981. + * @prio: prio value (kernel-internal form)
  4982. + *
  4983. + * This function changes the 'effective' priority of a task. It does
  4984. + * not touch ->normal_prio like __setscheduler().
  4985. + *
  4986. + * Used by the rt_mutex code to implement priority inheritance logic.
  4987. + */
  4988. +void rt_mutex_setprio(struct task_struct *p, int prio)
  4989. +{
  4990. + unsigned long flags;
  4991. + int queued, oldprio;
  4992. + struct rq *rq;
  4993. +
  4994. + BUG_ON(prio < 0 || prio > MAX_PRIO);
  4995. +
  4996. + rq = task_grq_lock(p, &flags);
  4997. +
  4998. + /*
  4999. + * Idle task boosting is a nono in general. There is one
  5000. + * exception, when PREEMPT_RT and NOHZ is active:
  5001. + *
  5002. + * The idle task calls get_next_timer_interrupt() and holds
  5003. + * the timer wheel base->lock on the CPU and another CPU wants
  5004. + * to access the timer (probably to cancel it). We can safely
  5005. + * ignore the boosting request, as the idle CPU runs this code
  5006. + * with interrupts disabled and will complete the lock
  5007. + * protected section without being interrupted. So there is no
  5008. + * real need to boost.
  5009. + */
  5010. + if (unlikely(p == rq->idle)) {
  5011. + WARN_ON(p != rq->curr);
  5012. + WARN_ON(p->pi_blocked_on);
  5013. + goto out_unlock;
  5014. + }
  5015. +
  5016. + trace_sched_pi_setprio(p, prio);
  5017. + oldprio = p->prio;
  5018. + queued = task_queued(p);
  5019. + if (queued)
  5020. + dequeue_task(p);
  5021. + p->prio = prio;
  5022. + if (task_running(p) && prio > oldprio)
  5023. + resched_task(p);
  5024. + if (queued) {
  5025. + enqueue_task(p);
  5026. + try_preempt(p, rq);
  5027. + }
  5028. +
  5029. +out_unlock:
  5030. + task_grq_unlock(&flags);
  5031. +}
  5032. +
  5033. +#endif
  5034. +
  5035. +/*
  5036. + * Adjust the deadline for when the priority is to change, before it's
  5037. + * changed.
  5038. + */
  5039. +static inline void adjust_deadline(struct task_struct *p, int new_prio)
  5040. +{
  5041. + p->deadline += static_deadline_diff(new_prio) - task_deadline_diff(p);
  5042. +}
  5043. +
  5044. +void set_user_nice(struct task_struct *p, long nice)
  5045. +{
  5046. + int queued, new_static, old_static;
  5047. + unsigned long flags;
  5048. + struct rq *rq;
  5049. +
  5050. + if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
  5051. + return;
  5052. + new_static = NICE_TO_PRIO(nice);
  5053. + /*
  5054. + * We have to be careful, if called from sys_setpriority(),
  5055. + * the task might be in the middle of scheduling on another CPU.
  5056. + */
  5057. + rq = time_task_grq_lock(p, &flags);
  5058. + /*
  5059. + * The RT priorities are set via sched_setscheduler(), but we still
  5060. + * allow the 'normal' nice value to be set - but as expected
  5061. + * it wont have any effect on scheduling until the task is
  5062. + * not SCHED_NORMAL/SCHED_BATCH:
  5063. + */
  5064. + if (has_rt_policy(p)) {
  5065. + p->static_prio = new_static;
  5066. + goto out_unlock;
  5067. + }
  5068. + queued = task_queued(p);
  5069. + if (queued)
  5070. + dequeue_task(p);
  5071. +
  5072. + adjust_deadline(p, new_static);
  5073. + old_static = p->static_prio;
  5074. + p->static_prio = new_static;
  5075. + p->prio = effective_prio(p);
  5076. +
  5077. + if (queued) {
  5078. + enqueue_task(p);
  5079. + if (new_static < old_static)
  5080. + try_preempt(p, rq);
  5081. + } else if (task_running(p)) {
  5082. + reset_rq_task(rq, p);
  5083. + if (old_static < new_static)
  5084. + resched_task(p);
  5085. + }
  5086. +out_unlock:
  5087. + task_grq_unlock(&flags);
  5088. +}
  5089. +EXPORT_SYMBOL(set_user_nice);
  5090. +
  5091. +/*
  5092. + * can_nice - check if a task can reduce its nice value
  5093. + * @p: task
  5094. + * @nice: nice value
  5095. + */
  5096. +int can_nice(const struct task_struct *p, const int nice)
  5097. +{
  5098. + /* convert nice value [19,-20] to rlimit style value [1,40] */
  5099. + int nice_rlim = 20 - nice;
  5100. +
  5101. + return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
  5102. + capable(CAP_SYS_NICE));
  5103. +}
  5104. +
  5105. +#ifdef __ARCH_WANT_SYS_NICE
  5106. +
  5107. +/*
  5108. + * sys_nice - change the priority of the current process.
  5109. + * @increment: priority increment
  5110. + *
  5111. + * sys_setpriority is a more generic, but much slower function that
  5112. + * does similar things.
  5113. + */
  5114. +SYSCALL_DEFINE1(nice, int, increment)
  5115. +{
  5116. + long nice, retval;
  5117. +
  5118. + /*
  5119. + * Setpriority might change our priority at the same moment.
  5120. + * We don't have to worry. Conceptually one call occurs first
  5121. + * and we have a single winner.
  5122. + */
  5123. + if (increment < -40)
  5124. + increment = -40;
  5125. + if (increment > 40)
  5126. + increment = 40;
  5127. +
  5128. + nice = TASK_NICE(current) + increment;
  5129. + if (nice < -20)
  5130. + nice = -20;
  5131. + if (nice > 19)
  5132. + nice = 19;
  5133. +
  5134. + if (increment < 0 && !can_nice(current, nice))
  5135. + return -EPERM;
  5136. +
  5137. + retval = security_task_setnice(current, nice);
  5138. + if (retval)
  5139. + return retval;
  5140. +
  5141. + set_user_nice(current, nice);
  5142. + return 0;
  5143. +}
  5144. +
  5145. +#endif
  5146. +
  5147. +/**
  5148. + * task_prio - return the priority value of a given task.
  5149. + * @p: the task in question.
  5150. + *
  5151. + * This is the priority value as seen by users in /proc.
  5152. + * RT tasks are offset by -100. Normal tasks are centered around 1, value goes
  5153. + * from 0 (SCHED_ISO) up to 82 (nice +19 SCHED_IDLEPRIO).
  5154. + */
  5155. +int task_prio(const struct task_struct *p)
  5156. +{
  5157. + int delta, prio = p->prio - MAX_RT_PRIO;
  5158. +
  5159. + /* rt tasks and iso tasks */
  5160. + if (prio <= 0)
  5161. + goto out;
  5162. +
  5163. + /* Convert to ms to avoid overflows */
  5164. + delta = NS_TO_MS(p->deadline - grq.niffies);
  5165. + delta = delta * 40 / ms_longest_deadline_diff();
  5166. + if (delta > 0 && delta <= 80)
  5167. + prio += delta;
  5168. + if (idleprio_task(p))
  5169. + prio += 40;
  5170. +out:
  5171. + return prio;
  5172. +}
  5173. +
  5174. +/**
  5175. + * task_nice - return the nice value of a given task.
  5176. + * @p: the task in question.
  5177. + */
  5178. +int task_nice(const struct task_struct *p)
  5179. +{
  5180. + return TASK_NICE(p);
  5181. +}
  5182. +EXPORT_SYMBOL_GPL(task_nice);
  5183. +
  5184. +/**
  5185. + * idle_cpu - is a given cpu idle currently?
  5186. + * @cpu: the processor in question.
  5187. + */
  5188. +int idle_cpu(int cpu)
  5189. +{
  5190. + return cpu_curr(cpu) == cpu_rq(cpu)->idle;
  5191. +}
  5192. +
  5193. +/**
  5194. + * idle_task - return the idle task for a given cpu.
  5195. + * @cpu: the processor in question.
  5196. + */
  5197. +struct task_struct *idle_task(int cpu)
  5198. +{
  5199. + return cpu_rq(cpu)->idle;
  5200. +}
  5201. +
  5202. +/**
  5203. + * find_process_by_pid - find a process with a matching PID value.
  5204. + * @pid: the pid in question.
  5205. + */
  5206. +static inline struct task_struct *find_process_by_pid(pid_t pid)
  5207. +{
  5208. + return pid ? find_task_by_vpid(pid) : current;
  5209. +}
  5210. +
  5211. +/* Actually do priority change: must hold grq lock. */
  5212. +static void
  5213. +__setscheduler(struct task_struct *p, struct rq *rq, int policy, int prio)
  5214. +{
  5215. + int oldrtprio, oldprio;
  5216. +
  5217. + p->policy = policy;
  5218. + oldrtprio = p->rt_priority;
  5219. + p->rt_priority = prio;
  5220. + p->normal_prio = normal_prio(p);
  5221. + oldprio = p->prio;
  5222. + /* we are holding p->pi_lock already */
  5223. + p->prio = rt_mutex_getprio(p);
  5224. + if (task_running(p)) {
  5225. + reset_rq_task(rq, p);
  5226. + /* Resched only if we might now be preempted */
  5227. + if (p->prio > oldprio || p->rt_priority > oldrtprio)
  5228. + resched_task(p);
  5229. + }
  5230. +}
  5231. +
  5232. +/*
  5233. + * check the target process has a UID that matches the current process's
  5234. + */
  5235. +static bool check_same_owner(struct task_struct *p)
  5236. +{
  5237. + const struct cred *cred = current_cred(), *pcred;
  5238. + bool match;
  5239. +
  5240. + rcu_read_lock();
  5241. + pcred = __task_cred(p);
  5242. + if (cred->user->user_ns == pcred->user->user_ns)
  5243. + match = (cred->euid == pcred->euid ||
  5244. + cred->euid == pcred->uid);
  5245. + else
  5246. + match = false;
  5247. + rcu_read_unlock();
  5248. + return match;
  5249. +}
  5250. +
  5251. +static int __sched_setscheduler(struct task_struct *p, int policy,
  5252. + const struct sched_param *param, bool user)
  5253. +{
  5254. + struct sched_param zero_param = { .sched_priority = 0 };
  5255. + int queued, retval, oldpolicy = -1;
  5256. + unsigned long flags, rlim_rtprio = 0;
  5257. + int reset_on_fork;
  5258. + struct rq *rq;
  5259. +
  5260. + /* may grab non-irq protected spin_locks */
  5261. + BUG_ON(in_interrupt());
  5262. +
  5263. + if (is_rt_policy(policy) && !capable(CAP_SYS_NICE)) {
  5264. + unsigned long lflags;
  5265. +
  5266. + if (!lock_task_sighand(p, &lflags))
  5267. + return -ESRCH;
  5268. + rlim_rtprio = task_rlimit(p, RLIMIT_RTPRIO);
  5269. + unlock_task_sighand(p, &lflags);
  5270. + if (rlim_rtprio)
  5271. + goto recheck;
  5272. + /*
  5273. + * If the caller requested an RT policy without having the
  5274. + * necessary rights, we downgrade the policy to SCHED_ISO.
  5275. + * We also set the parameter to zero to pass the checks.
  5276. + */
  5277. + policy = SCHED_ISO;
  5278. + param = &zero_param;
  5279. + }
  5280. +recheck:
  5281. + /* double check policy once rq lock held */
  5282. + if (policy < 0) {
  5283. + reset_on_fork = p->sched_reset_on_fork;
  5284. + policy = oldpolicy = p->policy;
  5285. + } else {
  5286. + reset_on_fork = !!(policy & SCHED_RESET_ON_FORK);
  5287. + policy &= ~SCHED_RESET_ON_FORK;
  5288. +
  5289. + if (!SCHED_RANGE(policy))
  5290. + return -EINVAL;
  5291. + }
  5292. +
  5293. + /*
  5294. + * Valid priorities for SCHED_FIFO and SCHED_RR are
  5295. + * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL and
  5296. + * SCHED_BATCH is 0.
  5297. + */
  5298. + if (param->sched_priority < 0 ||
  5299. + (p->mm && param->sched_priority > MAX_USER_RT_PRIO - 1) ||
  5300. + (!p->mm && param->sched_priority > MAX_RT_PRIO - 1))
  5301. + return -EINVAL;
  5302. + if (is_rt_policy(policy) != (param->sched_priority != 0))
  5303. + return -EINVAL;
  5304. +
  5305. + /*
  5306. + * Allow unprivileged RT tasks to decrease priority:
  5307. + */
  5308. + if (user && !capable(CAP_SYS_NICE)) {
  5309. + if (is_rt_policy(policy)) {
  5310. + unsigned long rlim_rtprio =
  5311. + task_rlimit(p, RLIMIT_RTPRIO);
  5312. +
  5313. + /* can't set/change the rt policy */
  5314. + if (policy != p->policy && !rlim_rtprio)
  5315. + return -EPERM;
  5316. +
  5317. + /* can't increase priority */
  5318. + if (param->sched_priority > p->rt_priority &&
  5319. + param->sched_priority > rlim_rtprio)
  5320. + return -EPERM;
  5321. + } else {
  5322. + switch (p->policy) {
  5323. + /*
  5324. + * Can only downgrade policies but not back to
  5325. + * SCHED_NORMAL
  5326. + */
  5327. + case SCHED_ISO:
  5328. + if (policy == SCHED_ISO)
  5329. + goto out;
  5330. + if (policy == SCHED_NORMAL)
  5331. + return -EPERM;
  5332. + break;
  5333. + case SCHED_BATCH:
  5334. + if (policy == SCHED_BATCH)
  5335. + goto out;
  5336. + if (policy != SCHED_IDLEPRIO)
  5337. + return -EPERM;
  5338. + break;
  5339. + case SCHED_IDLEPRIO:
  5340. + if (policy == SCHED_IDLEPRIO)
  5341. + goto out;
  5342. + return -EPERM;
  5343. + default:
  5344. + break;
  5345. + }
  5346. + }
  5347. +
  5348. + /* can't change other user's priorities */
  5349. + if (!check_same_owner(p))
  5350. + return -EPERM;
  5351. +
  5352. + /* Normal users shall not reset the sched_reset_on_fork flag */
  5353. + if (p->sched_reset_on_fork && !reset_on_fork)
  5354. + return -EPERM;
  5355. + }
  5356. +
  5357. + if (user) {
  5358. + retval = security_task_setscheduler(p);
  5359. + if (retval)
  5360. + return retval;
  5361. + }
  5362. +
  5363. + /*
  5364. + * make sure no PI-waiters arrive (or leave) while we are
  5365. + * changing the priority of the task:
  5366. + */
  5367. + raw_spin_lock_irqsave(&p->pi_lock, flags);
  5368. + /*
  5369. + * To be able to change p->policy safely, the grunqueue lock must be
  5370. + * held.
  5371. + */
  5372. + rq = __task_grq_lock(p);
  5373. +
  5374. + /*
  5375. + * Changing the policy of the stop threads its a very bad idea
  5376. + */
  5377. + if (p == rq->stop) {
  5378. + __task_grq_unlock();
  5379. + raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  5380. + return -EINVAL;
  5381. + }
  5382. +
  5383. + /*
  5384. + * If not changing anything there's no need to proceed further:
  5385. + */
  5386. + if (unlikely(policy == p->policy && (!is_rt_policy(policy) ||
  5387. + param->sched_priority == p->rt_priority))) {
  5388. +
  5389. + __task_grq_unlock();
  5390. + raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  5391. + return 0;
  5392. + }
  5393. +
  5394. + /* recheck policy now with rq lock held */
  5395. + if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  5396. + policy = oldpolicy = -1;
  5397. + __task_grq_unlock();
  5398. + raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  5399. + goto recheck;
  5400. + }
  5401. + update_clocks(rq);
  5402. + p->sched_reset_on_fork = reset_on_fork;
  5403. +
  5404. + queued = task_queued(p);
  5405. + if (queued)
  5406. + dequeue_task(p);
  5407. + __setscheduler(p, rq, policy, param->sched_priority);
  5408. + if (queued) {
  5409. + enqueue_task(p);
  5410. + try_preempt(p, rq);
  5411. + }
  5412. + __task_grq_unlock();
  5413. + raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  5414. +
  5415. + rt_mutex_adjust_pi(p);
  5416. +out:
  5417. + return 0;
  5418. +}
  5419. +
  5420. +/**
  5421. + * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
  5422. + * @p: the task in question.
  5423. + * @policy: new policy.
  5424. + * @param: structure containing the new RT priority.
  5425. + *
  5426. + * NOTE that the task may be already dead.
  5427. + */
  5428. +int sched_setscheduler(struct task_struct *p, int policy,
  5429. + const struct sched_param *param)
  5430. +{
  5431. + return __sched_setscheduler(p, policy, param, true);
  5432. +}
  5433. +
  5434. +EXPORT_SYMBOL_GPL(sched_setscheduler);
  5435. +
  5436. +/**
  5437. + * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
  5438. + * @p: the task in question.
  5439. + * @policy: new policy.
  5440. + * @param: structure containing the new RT priority.
  5441. + *
  5442. + * Just like sched_setscheduler, only don't bother checking if the
  5443. + * current context has permission. For example, this is needed in
  5444. + * stop_machine(): we create temporary high priority worker threads,
  5445. + * but our caller might not have that capability.
  5446. + */
  5447. +int sched_setscheduler_nocheck(struct task_struct *p, int policy,
  5448. + const struct sched_param *param)
  5449. +{
  5450. + return __sched_setscheduler(p, policy, param, false);
  5451. +}
  5452. +
  5453. +static int
  5454. +do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  5455. +{
  5456. + struct sched_param lparam;
  5457. + struct task_struct *p;
  5458. + int retval;
  5459. +
  5460. + if (!param || pid < 0)
  5461. + return -EINVAL;
  5462. + if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  5463. + return -EFAULT;
  5464. +
  5465. + rcu_read_lock();
  5466. + retval = -ESRCH;
  5467. + p = find_process_by_pid(pid);
  5468. + if (p != NULL)
  5469. + retval = sched_setscheduler(p, policy, &lparam);
  5470. + rcu_read_unlock();
  5471. +
  5472. + return retval;
  5473. +}
  5474. +
  5475. +/**
  5476. + * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  5477. + * @pid: the pid in question.
  5478. + * @policy: new policy.
  5479. + * @param: structure containing the new RT priority.
  5480. + */
  5481. +asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
  5482. + struct sched_param __user *param)
  5483. +{
  5484. + /* negative values for policy are not valid */
  5485. + if (policy < 0)
  5486. + return -EINVAL;
  5487. +
  5488. + return do_sched_setscheduler(pid, policy, param);
  5489. +}
  5490. +
  5491. +/**
  5492. + * sys_sched_setparam - set/change the RT priority of a thread
  5493. + * @pid: the pid in question.
  5494. + * @param: structure containing the new RT priority.
  5495. + */
  5496. +SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
  5497. +{
  5498. + return do_sched_setscheduler(pid, -1, param);
  5499. +}
  5500. +
  5501. +/**
  5502. + * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  5503. + * @pid: the pid in question.
  5504. + */
  5505. +SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
  5506. +{
  5507. + struct task_struct *p;
  5508. + int retval = -EINVAL;
  5509. +
  5510. + if (pid < 0)
  5511. + goto out_nounlock;
  5512. +
  5513. + retval = -ESRCH;
  5514. + rcu_read_lock();
  5515. + p = find_process_by_pid(pid);
  5516. + if (p) {
  5517. + retval = security_task_getscheduler(p);
  5518. + if (!retval)
  5519. + retval = p->policy;
  5520. + }
  5521. + rcu_read_unlock();
  5522. +
  5523. +out_nounlock:
  5524. + return retval;
  5525. +}
  5526. +
  5527. +/**
  5528. + * sys_sched_getscheduler - get the RT priority of a thread
  5529. + * @pid: the pid in question.
  5530. + * @param: structure containing the RT priority.
  5531. + */
  5532. +SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
  5533. +{
  5534. + struct sched_param lp;
  5535. + struct task_struct *p;
  5536. + int retval = -EINVAL;
  5537. +
  5538. + if (!param || pid < 0)
  5539. + goto out_nounlock;
  5540. +
  5541. + rcu_read_lock();
  5542. + p = find_process_by_pid(pid);
  5543. + retval = -ESRCH;
  5544. + if (!p)
  5545. + goto out_unlock;
  5546. +
  5547. + retval = security_task_getscheduler(p);
  5548. + if (retval)
  5549. + goto out_unlock;
  5550. +
  5551. + lp.sched_priority = p->rt_priority;
  5552. + rcu_read_unlock();
  5553. +
  5554. + /*
  5555. + * This one might sleep, we cannot do it with a spinlock held ...
  5556. + */
  5557. + retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  5558. +
  5559. +out_nounlock:
  5560. + return retval;
  5561. +
  5562. +out_unlock:
  5563. + rcu_read_unlock();
  5564. + return retval;
  5565. +}
  5566. +
  5567. +long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
  5568. +{
  5569. + cpumask_var_t cpus_allowed, new_mask;
  5570. + struct task_struct *p;
  5571. + int retval;
  5572. +
  5573. + get_online_cpus();
  5574. + rcu_read_lock();
  5575. +
  5576. + p = find_process_by_pid(pid);
  5577. + if (!p) {
  5578. + rcu_read_unlock();
  5579. + put_online_cpus();
  5580. + return -ESRCH;
  5581. + }
  5582. +
  5583. + /* Prevent p going away */
  5584. + get_task_struct(p);
  5585. + rcu_read_unlock();
  5586. +
  5587. + if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
  5588. + retval = -ENOMEM;
  5589. + goto out_put_task;
  5590. + }
  5591. + if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
  5592. + retval = -ENOMEM;
  5593. + goto out_free_cpus_allowed;
  5594. + }
  5595. + retval = -EPERM;
  5596. + if (!check_same_owner(p) && !ns_capable(task_user_ns(p), CAP_SYS_NICE))
  5597. + goto out_unlock;
  5598. +
  5599. + retval = security_task_setscheduler(p);
  5600. + if (retval)
  5601. + goto out_unlock;
  5602. +
  5603. + cpuset_cpus_allowed(p, cpus_allowed);
  5604. + cpumask_and(new_mask, in_mask, cpus_allowed);
  5605. +again:
  5606. + retval = set_cpus_allowed_ptr(p, new_mask);
  5607. +
  5608. + if (!retval) {
  5609. + cpuset_cpus_allowed(p, cpus_allowed);
  5610. + if (!cpumask_subset(new_mask, cpus_allowed)) {
  5611. + /*
  5612. + * We must have raced with a concurrent cpuset
  5613. + * update. Just reset the cpus_allowed to the
  5614. + * cpuset's cpus_allowed
  5615. + */
  5616. + cpumask_copy(new_mask, cpus_allowed);
  5617. + goto again;
  5618. + }
  5619. + }
  5620. +out_unlock:
  5621. + free_cpumask_var(new_mask);
  5622. +out_free_cpus_allowed:
  5623. + free_cpumask_var(cpus_allowed);
  5624. +out_put_task:
  5625. + put_task_struct(p);
  5626. + put_online_cpus();
  5627. + return retval;
  5628. +}
  5629. +
  5630. +static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  5631. + cpumask_t *new_mask)
  5632. +{
  5633. + if (len < sizeof(cpumask_t)) {
  5634. + memset(new_mask, 0, sizeof(cpumask_t));
  5635. + } else if (len > sizeof(cpumask_t)) {
  5636. + len = sizeof(cpumask_t);
  5637. + }
  5638. + return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  5639. +}
  5640. +
  5641. +
  5642. +/**
  5643. + * sys_sched_setaffinity - set the cpu affinity of a process
  5644. + * @pid: pid of the process
  5645. + * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  5646. + * @user_mask_ptr: user-space pointer to the new cpu mask
  5647. + */
  5648. +SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
  5649. + unsigned long __user *, user_mask_ptr)
  5650. +{
  5651. + cpumask_var_t new_mask;
  5652. + int retval;
  5653. +
  5654. + if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  5655. + return -ENOMEM;
  5656. +
  5657. + retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
  5658. + if (retval == 0)
  5659. + retval = sched_setaffinity(pid, new_mask);
  5660. + free_cpumask_var(new_mask);
  5661. + return retval;
  5662. +}
  5663. +
  5664. +long sched_getaffinity(pid_t pid, cpumask_t *mask)
  5665. +{
  5666. + struct task_struct *p;
  5667. + unsigned long flags;
  5668. + int retval;
  5669. +
  5670. + get_online_cpus();
  5671. + rcu_read_lock();
  5672. +
  5673. + retval = -ESRCH;
  5674. + p = find_process_by_pid(pid);
  5675. + if (!p)
  5676. + goto out_unlock;
  5677. +
  5678. + retval = security_task_getscheduler(p);
  5679. + if (retval)
  5680. + goto out_unlock;
  5681. +
  5682. + grq_lock_irqsave(&flags);
  5683. + cpumask_and(mask, tsk_cpus_allowed(p), cpu_online_mask);
  5684. + grq_unlock_irqrestore(&flags);
  5685. +
  5686. +out_unlock:
  5687. + rcu_read_unlock();
  5688. + put_online_cpus();
  5689. +
  5690. + return retval;
  5691. +}
  5692. +
  5693. +/**
  5694. + * sys_sched_getaffinity - get the cpu affinity of a process
  5695. + * @pid: pid of the process
  5696. + * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  5697. + * @user_mask_ptr: user-space pointer to hold the current cpu mask
  5698. + */
  5699. +SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
  5700. + unsigned long __user *, user_mask_ptr)
  5701. +{
  5702. + int ret;
  5703. + cpumask_var_t mask;
  5704. +
  5705. + if ((len * BITS_PER_BYTE) < nr_cpu_ids)
  5706. + return -EINVAL;
  5707. + if (len & (sizeof(unsigned long)-1))
  5708. + return -EINVAL;
  5709. +
  5710. + if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  5711. + return -ENOMEM;
  5712. +
  5713. + ret = sched_getaffinity(pid, mask);
  5714. + if (ret == 0) {
  5715. + size_t retlen = min_t(size_t, len, cpumask_size());
  5716. +
  5717. + if (copy_to_user(user_mask_ptr, mask, retlen))
  5718. + ret = -EFAULT;
  5719. + else
  5720. + ret = retlen;
  5721. + }
  5722. + free_cpumask_var(mask);
  5723. +
  5724. + return ret;
  5725. +}
  5726. +
  5727. +/**
  5728. + * sys_sched_yield - yield the current processor to other threads.
  5729. + *
  5730. + * This function yields the current CPU to other tasks. It does this by
  5731. + * scheduling away the current task. If it still has the earliest deadline
  5732. + * it will be scheduled again as the next task.
  5733. + */
  5734. +SYSCALL_DEFINE0(sched_yield)
  5735. +{
  5736. + struct task_struct *p;
  5737. +
  5738. + p = current;
  5739. + grq_lock_irq();
  5740. + schedstat_inc(task_rq(p), yld_count);
  5741. + requeue_task(p);
  5742. +
  5743. + /*
  5744. + * Since we are going to call schedule() anyway, there's
  5745. + * no need to preempt or enable interrupts:
  5746. + */
  5747. + __release(grq.lock);
  5748. + spin_release(&grq.lock.dep_map, 1, _THIS_IP_);
  5749. + do_raw_spin_unlock(&grq.lock);
  5750. + sched_preempt_enable_no_resched();
  5751. +
  5752. + schedule();
  5753. +
  5754. + return 0;
  5755. +}
  5756. +
  5757. +static inline bool should_resched(void)
  5758. +{
  5759. + return need_resched() && !(preempt_count() & PREEMPT_ACTIVE);
  5760. +}
  5761. +
  5762. +static void __cond_resched(void)
  5763. +{
  5764. + /* NOT a real fix but will make voluntary preempt work. 馬鹿な事 */
  5765. + if (unlikely(system_state != SYSTEM_RUNNING))
  5766. + return;
  5767. +
  5768. + add_preempt_count(PREEMPT_ACTIVE);
  5769. + schedule();
  5770. + sub_preempt_count(PREEMPT_ACTIVE);
  5771. +}
  5772. +
  5773. +int __sched _cond_resched(void)
  5774. +{
  5775. + if (should_resched()) {
  5776. + __cond_resched();
  5777. + return 1;
  5778. + }
  5779. + return 0;
  5780. +}
  5781. +EXPORT_SYMBOL(_cond_resched);
  5782. +
  5783. +/*
  5784. + * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
  5785. + * call schedule, and on return reacquire the lock.
  5786. + *
  5787. + * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  5788. + * operations here to prevent schedule() from being called twice (once via
  5789. + * spin_unlock(), once by hand).
  5790. + */
  5791. +int __cond_resched_lock(spinlock_t *lock)
  5792. +{
  5793. + int resched = should_resched();
  5794. + int ret = 0;
  5795. +
  5796. + lockdep_assert_held(lock);
  5797. +
  5798. + if (spin_needbreak(lock) || resched) {
  5799. + spin_unlock(lock);
  5800. + if (resched)
  5801. + __cond_resched();
  5802. + else
  5803. + cpu_relax();
  5804. + ret = 1;
  5805. + spin_lock(lock);
  5806. + }
  5807. + return ret;
  5808. +}
  5809. +EXPORT_SYMBOL(__cond_resched_lock);
  5810. +
  5811. +int __sched __cond_resched_softirq(void)
  5812. +{
  5813. + BUG_ON(!in_softirq());
  5814. +
  5815. + if (should_resched()) {
  5816. + local_bh_enable();
  5817. + __cond_resched();
  5818. + local_bh_disable();
  5819. + return 1;
  5820. + }
  5821. + return 0;
  5822. +}
  5823. +EXPORT_SYMBOL(__cond_resched_softirq);
  5824. +
  5825. +/**
  5826. + * yield - yield the current processor to other threads.
  5827. + *
  5828. + * Do not ever use this function, there's a 99% chance you're doing it wrong.
  5829. + *
  5830. + * The scheduler is at all times free to pick the calling task as the most
  5831. + * eligible task to run, if removing the yield() call from your code breaks
  5832. + * it, its already broken.
  5833. + *
  5834. + * Typical broken usage is:
  5835. + *
  5836. + * while (!event)
  5837. + * yield();
  5838. + *
  5839. + * where one assumes that yield() will let 'the other' process run that will
  5840. + * make event true. If the current task is a SCHED_FIFO task that will never
  5841. + * happen. Never use yield() as a progress guarantee!!
  5842. + *
  5843. + * If you want to use yield() to wait for something, use wait_event().
  5844. + * If you want to use yield() to be 'nice' for others, use cond_resched().
  5845. + * If you still want to use yield(), do not!
  5846. + */
  5847. +void __sched yield(void)
  5848. +{
  5849. + set_current_state(TASK_RUNNING);
  5850. + sys_sched_yield();
  5851. +}
  5852. +EXPORT_SYMBOL(yield);
  5853. +
  5854. +/**
  5855. + * yield_to - yield the current processor to another thread in
  5856. + * your thread group, or accelerate that thread toward the
  5857. + * processor it's on.
  5858. + * @p: target task
  5859. + * @preempt: whether task preemption is allowed or not
  5860. + *
  5861. + * It's the caller's job to ensure that the target task struct
  5862. + * can't go away on us before we can do any checks.
  5863. + *
  5864. + * Returns true if we indeed boosted the target task.
  5865. + */
  5866. +bool __sched yield_to(struct task_struct *p, bool preempt)
  5867. +{
  5868. + unsigned long flags;
  5869. + bool yielded = 0;
  5870. + struct rq *rq;
  5871. +
  5872. + rq = this_rq();
  5873. + grq_lock_irqsave(&flags);
  5874. + if (task_running(p) || p->state)
  5875. + goto out_unlock;
  5876. + yielded = 1;
  5877. + if (p->deadline > rq->rq_deadline)
  5878. + p->deadline = rq->rq_deadline;
  5879. + p->time_slice += rq->rq_time_slice;
  5880. + rq->rq_time_slice = 0;
  5881. + if (p->time_slice > timeslice())
  5882. + p->time_slice = timeslice();
  5883. + set_tsk_need_resched(rq->curr);
  5884. +out_unlock:
  5885. + grq_unlock_irqrestore(&flags);
  5886. +
  5887. + if (yielded)
  5888. + schedule();
  5889. + return yielded;
  5890. +}
  5891. +EXPORT_SYMBOL_GPL(yield_to);
  5892. +
  5893. +/*
  5894. + * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  5895. + * that process accounting knows that this is a task in IO wait state.
  5896. + *
  5897. + * But don't do that if it is a deliberate, throttling IO wait (this task
  5898. + * has set its backing_dev_info: the queue against which it should throttle)
  5899. + */
  5900. +void __sched io_schedule(void)
  5901. +{
  5902. + struct rq *rq = raw_rq();
  5903. +
  5904. + delayacct_blkio_start();
  5905. + atomic_inc(&rq->nr_iowait);
  5906. + blk_flush_plug(current);
  5907. + current->in_iowait = 1;
  5908. + schedule();
  5909. + current->in_iowait = 0;
  5910. + atomic_dec(&rq->nr_iowait);
  5911. + delayacct_blkio_end();
  5912. +}
  5913. +EXPORT_SYMBOL(io_schedule);
  5914. +
  5915. +long __sched io_schedule_timeout(long timeout)
  5916. +{
  5917. + struct rq *rq = raw_rq();
  5918. + long ret;
  5919. +
  5920. + delayacct_blkio_start();
  5921. + atomic_inc(&rq->nr_iowait);
  5922. + blk_flush_plug(current);
  5923. + current->in_iowait = 1;
  5924. + ret = schedule_timeout(timeout);
  5925. + current->in_iowait = 0;
  5926. + atomic_dec(&rq->nr_iowait);
  5927. + delayacct_blkio_end();
  5928. + return ret;
  5929. +}
  5930. +
  5931. +/**
  5932. + * sys_sched_get_priority_max - return maximum RT priority.
  5933. + * @policy: scheduling class.
  5934. + *
  5935. + * this syscall returns the maximum rt_priority that can be used
  5936. + * by a given scheduling class.
  5937. + */
  5938. +SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
  5939. +{
  5940. + int ret = -EINVAL;
  5941. +
  5942. + switch (policy) {
  5943. + case SCHED_FIFO:
  5944. + case SCHED_RR:
  5945. + ret = MAX_USER_RT_PRIO-1;
  5946. + break;
  5947. + case SCHED_NORMAL:
  5948. + case SCHED_BATCH:
  5949. + case SCHED_ISO:
  5950. + case SCHED_IDLEPRIO:
  5951. + ret = 0;
  5952. + break;
  5953. + }
  5954. + return ret;
  5955. +}
  5956. +
  5957. +/**
  5958. + * sys_sched_get_priority_min - return minimum RT priority.
  5959. + * @policy: scheduling class.
  5960. + *
  5961. + * this syscall returns the minimum rt_priority that can be used
  5962. + * by a given scheduling class.
  5963. + */
  5964. +SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
  5965. +{
  5966. + int ret = -EINVAL;
  5967. +
  5968. + switch (policy) {
  5969. + case SCHED_FIFO:
  5970. + case SCHED_RR:
  5971. + ret = 1;
  5972. + break;
  5973. + case SCHED_NORMAL:
  5974. + case SCHED_BATCH:
  5975. + case SCHED_ISO:
  5976. + case SCHED_IDLEPRIO:
  5977. + ret = 0;
  5978. + break;
  5979. + }
  5980. + return ret;
  5981. +}
  5982. +
  5983. +/**
  5984. + * sys_sched_rr_get_interval - return the default timeslice of a process.
  5985. + * @pid: pid of the process.
  5986. + * @interval: userspace pointer to the timeslice value.
  5987. + *
  5988. + * this syscall writes the default timeslice value of a given process
  5989. + * into the user-space timespec buffer. A value of '0' means infinity.
  5990. + */
  5991. +SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
  5992. + struct timespec __user *, interval)
  5993. +{
  5994. + struct task_struct *p;
  5995. + unsigned int time_slice;
  5996. + unsigned long flags;
  5997. + int retval;
  5998. + struct timespec t;
  5999. +
  6000. + if (pid < 0)
  6001. + return -EINVAL;
  6002. +
  6003. + retval = -ESRCH;
  6004. + rcu_read_lock();
  6005. + p = find_process_by_pid(pid);
  6006. + if (!p)
  6007. + goto out_unlock;
  6008. +
  6009. + retval = security_task_getscheduler(p);
  6010. + if (retval)
  6011. + goto out_unlock;
  6012. +
  6013. + grq_lock_irqsave(&flags);
  6014. + time_slice = p->policy == SCHED_FIFO ? 0 : MS_TO_NS(task_timeslice(p));
  6015. + grq_unlock_irqrestore(&flags);
  6016. +
  6017. + rcu_read_unlock();
  6018. + t = ns_to_timespec(time_slice);
  6019. + retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  6020. + return retval;
  6021. +
  6022. +out_unlock:
  6023. + rcu_read_unlock();
  6024. + return retval;
  6025. +}
  6026. +
  6027. +static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
  6028. +
  6029. +void sched_show_task(struct task_struct *p)
  6030. +{
  6031. + unsigned long free = 0;
  6032. + unsigned state;
  6033. +
  6034. + state = p->state ? __ffs(p->state) + 1 : 0;
  6035. + printk(KERN_INFO "%-15.15s %c", p->comm,
  6036. + state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
  6037. +#if BITS_PER_LONG == 32
  6038. + if (state == TASK_RUNNING)
  6039. + printk(KERN_CONT " running ");
  6040. + else
  6041. + printk(KERN_CONT " %08lx ", thread_saved_pc(p));
  6042. +#else
  6043. + if (state == TASK_RUNNING)
  6044. + printk(KERN_CONT " running task ");
  6045. + else
  6046. + printk(KERN_CONT " %016lx ", thread_saved_pc(p));
  6047. +#endif
  6048. +#ifdef CONFIG_DEBUG_STACK_USAGE
  6049. + free = stack_not_used(p);
  6050. +#endif
  6051. + printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
  6052. + task_pid_nr(p), task_pid_nr(p->real_parent),
  6053. + (unsigned long)task_thread_info(p)->flags);
  6054. +
  6055. + show_stack(p, NULL);
  6056. +}
  6057. +
  6058. +void show_state_filter(unsigned long state_filter)
  6059. +{
  6060. + struct task_struct *g, *p;
  6061. +
  6062. +#if BITS_PER_LONG == 32
  6063. + printk(KERN_INFO
  6064. + " task PC stack pid father\n");
  6065. +#else
  6066. + printk(KERN_INFO
  6067. + " task PC stack pid father\n");
  6068. +#endif
  6069. + rcu_read_lock();
  6070. + do_each_thread(g, p) {
  6071. + /*
  6072. + * reset the NMI-timeout, listing all files on a slow
  6073. + * console might take a lot of time:
  6074. + */
  6075. + touch_nmi_watchdog();
  6076. + if (!state_filter || (p->state & state_filter))
  6077. + sched_show_task(p);
  6078. + } while_each_thread(g, p);
  6079. +
  6080. + touch_all_softlockup_watchdogs();
  6081. +
  6082. + rcu_read_unlock();
  6083. + /*
  6084. + * Only show locks if all tasks are dumped:
  6085. + */
  6086. + if (!state_filter)
  6087. + debug_show_all_locks();
  6088. +}
  6089. +
  6090. +#ifdef CONFIG_SMP
  6091. +void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  6092. +{
  6093. + cpumask_copy(tsk_cpus_allowed(p), new_mask);
  6094. +}
  6095. +#endif
  6096. +
  6097. +/**
  6098. + * init_idle - set up an idle thread for a given CPU
  6099. + * @idle: task in question
  6100. + * @cpu: cpu the idle task belongs to
  6101. + *
  6102. + * NOTE: this function does not set the idle thread's NEED_RESCHED
  6103. + * flag, to make booting more robust.
  6104. + */
  6105. +void init_idle(struct task_struct *idle, int cpu)
  6106. +{
  6107. + struct rq *rq = cpu_rq(cpu);
  6108. + unsigned long flags;
  6109. +
  6110. + time_grq_lock(rq, &flags);
  6111. + idle->last_ran = rq->clock;
  6112. + idle->state = TASK_RUNNING;
  6113. + /* Setting prio to illegal value shouldn't matter when never queued */
  6114. + idle->prio = PRIO_LIMIT;
  6115. + set_rq_task(rq, idle);
  6116. + do_set_cpus_allowed(idle, &cpumask_of_cpu(cpu));
  6117. + /* Silence PROVE_RCU */
  6118. + rcu_read_lock();
  6119. + set_task_cpu(idle, cpu);
  6120. + rcu_read_unlock();
  6121. + rq->curr = rq->idle = idle;
  6122. + idle->on_cpu = 1;
  6123. + grq_unlock_irqrestore(&flags);
  6124. +
  6125. + /* Set the preempt count _outside_ the spinlocks! */
  6126. + task_thread_info(idle)->preempt_count = 0;
  6127. +
  6128. + ftrace_graph_init_idle_task(idle, cpu);
  6129. +#if defined(CONFIG_SMP)
  6130. + sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
  6131. +#endif
  6132. +}
  6133. +
  6134. +#ifdef CONFIG_SMP
  6135. +#ifdef CONFIG_NO_HZ
  6136. +void select_nohz_load_balancer(int stop_tick)
  6137. +{
  6138. +}
  6139. +
  6140. +void set_cpu_sd_state_idle(void) {}
  6141. +#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  6142. +/**
  6143. + * lowest_flag_domain - Return lowest sched_domain containing flag.
  6144. + * @cpu: The cpu whose lowest level of sched domain is to
  6145. + * be returned.
  6146. + * @flag: The flag to check for the lowest sched_domain
  6147. + * for the given cpu.
  6148. + *
  6149. + * Returns the lowest sched_domain of a cpu which contains the given flag.
  6150. + */
  6151. +static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
  6152. +{
  6153. + struct sched_domain *sd;
  6154. +
  6155. + for_each_domain(cpu, sd)
  6156. + if (sd && (sd->flags & flag))
  6157. + break;
  6158. +
  6159. + return sd;
  6160. +}
  6161. +
  6162. +/**
  6163. + * for_each_flag_domain - Iterates over sched_domains containing the flag.
  6164. + * @cpu: The cpu whose domains we're iterating over.
  6165. + * @sd: variable holding the value of the power_savings_sd
  6166. + * for cpu.
  6167. + * @flag: The flag to filter the sched_domains to be iterated.
  6168. + *
  6169. + * Iterates over all the scheduler domains for a given cpu that has the 'flag'
  6170. + * set, starting from the lowest sched_domain to the highest.
  6171. + */
  6172. +#define for_each_flag_domain(cpu, sd, flag) \
  6173. + for (sd = lowest_flag_domain(cpu, flag); \
  6174. + (sd && (sd->flags & flag)); sd = sd->parent)
  6175. +
  6176. +#endif /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
  6177. +
  6178. +static inline void resched_cpu(int cpu)
  6179. +{
  6180. + unsigned long flags;
  6181. +
  6182. + grq_lock_irqsave(&flags);
  6183. + resched_task(cpu_curr(cpu));
  6184. + grq_unlock_irqrestore(&flags);
  6185. +}
  6186. +
  6187. +/*
  6188. + * In the semi idle case, use the nearest busy cpu for migrating timers
  6189. + * from an idle cpu. This is good for power-savings.
  6190. + *
  6191. + * We don't do similar optimization for completely idle system, as
  6192. + * selecting an idle cpu will add more delays to the timers than intended
  6193. + * (as that cpu's timer base may not be uptodate wrt jiffies etc).
  6194. + */
  6195. +int get_nohz_timer_target(void)
  6196. +{
  6197. + int cpu = smp_processor_id();
  6198. + int i;
  6199. + struct sched_domain *sd;
  6200. +
  6201. + rcu_read_lock();
  6202. + for_each_domain(cpu, sd) {
  6203. + for_each_cpu(i, sched_domain_span(sd)) {
  6204. + if (!idle_cpu(i))
  6205. + cpu = i;
  6206. + goto unlock;
  6207. + }
  6208. + }
  6209. +unlock:
  6210. + rcu_read_unlock();
  6211. + return cpu;
  6212. +}
  6213. +
  6214. +/*
  6215. + * When add_timer_on() enqueues a timer into the timer wheel of an
  6216. + * idle CPU then this timer might expire before the next timer event
  6217. + * which is scheduled to wake up that CPU. In case of a completely
  6218. + * idle system the next event might even be infinite time into the
  6219. + * future. wake_up_idle_cpu() ensures that the CPU is woken up and
  6220. + * leaves the inner idle loop so the newly added timer is taken into
  6221. + * account when the CPU goes back to idle and evaluates the timer
  6222. + * wheel for the next timer event.
  6223. + */
  6224. +void wake_up_idle_cpu(int cpu)
  6225. +{
  6226. + struct task_struct *idle;
  6227. + struct rq *rq;
  6228. +
  6229. + if (cpu == smp_processor_id())
  6230. + return;
  6231. +
  6232. + rq = cpu_rq(cpu);
  6233. + idle = rq->idle;
  6234. +
  6235. + /*
  6236. + * This is safe, as this function is called with the timer
  6237. + * wheel base lock of (cpu) held. When the CPU is on the way
  6238. + * to idle and has not yet set rq->curr to idle then it will
  6239. + * be serialised on the timer wheel base lock and take the new
  6240. + * timer into account automatically.
  6241. + */
  6242. + if (unlikely(rq->curr != idle))
  6243. + return;
  6244. +
  6245. + /*
  6246. + * We can set TIF_RESCHED on the idle task of the other CPU
  6247. + * lockless. The worst case is that the other CPU runs the
  6248. + * idle task through an additional NOOP schedule()
  6249. + */
  6250. + set_tsk_need_resched(idle);
  6251. +
  6252. + /* NEED_RESCHED must be visible before we test polling */
  6253. + smp_mb();
  6254. + if (!tsk_is_polling(idle))
  6255. + smp_send_reschedule(cpu);
  6256. +}
  6257. +
  6258. +#endif /* CONFIG_NO_HZ */
  6259. +
  6260. +/*
  6261. + * Change a given task's CPU affinity. Migrate the thread to a
  6262. + * proper CPU and schedule it away if the CPU it's executing on
  6263. + * is removed from the allowed bitmask.
  6264. + *
  6265. + * NOTE: the caller must have a valid reference to the task, the
  6266. + * task must not exit() & deallocate itself prematurely. The
  6267. + * call is not atomic; no spinlocks may be held.
  6268. + */
  6269. +int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
  6270. +{
  6271. + bool running_wrong = false;
  6272. + bool queued = false;
  6273. + unsigned long flags;
  6274. + struct rq *rq;
  6275. + int ret = 0;
  6276. +
  6277. + rq = task_grq_lock(p, &flags);
  6278. +
  6279. + if (cpumask_equal(tsk_cpus_allowed(p), new_mask))
  6280. + goto out;
  6281. +
  6282. + if (!cpumask_intersects(new_mask, cpu_active_mask)) {
  6283. + ret = -EINVAL;
  6284. + goto out;
  6285. + }
  6286. +
  6287. + if (unlikely((p->flags & PF_THREAD_BOUND) && p != current)) {
  6288. + ret = -EINVAL;
  6289. + goto out;
  6290. + }
  6291. +
  6292. + queued = task_queued(p);
  6293. +
  6294. + do_set_cpus_allowed(p, new_mask);
  6295. +
  6296. + /* Can the task run on the task's current CPU? If so, we're done */
  6297. + if (cpumask_test_cpu(task_cpu(p), new_mask))
  6298. + goto out;
  6299. +
  6300. + if (task_running(p)) {
  6301. + /* Task is running on the wrong cpu now, reschedule it. */
  6302. + if (rq == this_rq()) {
  6303. + set_tsk_need_resched(p);
  6304. + running_wrong = true;
  6305. + } else
  6306. + resched_task(p);
  6307. + } else
  6308. + set_task_cpu(p, cpumask_any_and(cpu_active_mask, new_mask));
  6309. +
  6310. +out:
  6311. + if (queued)
  6312. + try_preempt(p, rq);
  6313. + task_grq_unlock(&flags);
  6314. +
  6315. + if (running_wrong)
  6316. + _cond_resched();
  6317. +
  6318. + return ret;
  6319. +}
  6320. +EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
  6321. +
  6322. +#ifdef CONFIG_HOTPLUG_CPU
  6323. +/* Run through task list and find tasks affined to just the dead cpu, then
  6324. + * allocate a new affinity */
  6325. +static void break_sole_affinity(int src_cpu, struct task_struct *idle)
  6326. +{
  6327. + struct task_struct *p, *t;
  6328. +
  6329. + do_each_thread(t, p) {
  6330. + if (p != idle && !online_cpus(p)) {
  6331. + cpumask_copy(tsk_cpus_allowed(p), cpu_possible_mask);
  6332. + /*
  6333. + * Don't tell them about moving exiting tasks or
  6334. + * kernel threads (both mm NULL), since they never
  6335. + * leave kernel.
  6336. + */
  6337. + if (p->mm && printk_ratelimit()) {
  6338. + printk(KERN_INFO "process %d (%s) no "
  6339. + "longer affine to cpu %d\n",
  6340. + task_pid_nr(p), p->comm, src_cpu);
  6341. + }
  6342. + }
  6343. + clear_sticky(p);
  6344. + } while_each_thread(t, p);
  6345. +}
  6346. +
  6347. +/*
  6348. + * Schedules idle task to be the next runnable task on current CPU.
  6349. + * It does so by boosting its priority to highest possible.
  6350. + * Used by CPU offline code.
  6351. + */
  6352. +void sched_idle_next(struct rq *rq, int this_cpu, struct task_struct *idle)
  6353. +{
  6354. + /* cpu has to be offline */
  6355. + BUG_ON(cpu_online(this_cpu));
  6356. +
  6357. + __setscheduler(idle, rq, SCHED_FIFO, STOP_PRIO);
  6358. +
  6359. + activate_idle_task(idle);
  6360. + set_tsk_need_resched(rq->curr);
  6361. +}
  6362. +
  6363. +/*
  6364. + * Ensures that the idle task is using init_mm right before its cpu goes
  6365. + * offline.
  6366. + */
  6367. +void idle_task_exit(void)
  6368. +{
  6369. + struct mm_struct *mm = current->active_mm;
  6370. +
  6371. + BUG_ON(cpu_online(smp_processor_id()));
  6372. +
  6373. + if (mm != &init_mm)
  6374. + switch_mm(mm, &init_mm, current);
  6375. + mmdrop(mm);
  6376. +}
  6377. +#endif /* CONFIG_HOTPLUG_CPU */
  6378. +void sched_set_stop_task(int cpu, struct task_struct *stop)
  6379. +{
  6380. + struct sched_param stop_param = { .sched_priority = STOP_PRIO };
  6381. + struct sched_param start_param = { .sched_priority = MAX_USER_RT_PRIO - 1 };
  6382. + struct task_struct *old_stop = cpu_rq(cpu)->stop;
  6383. +
  6384. + if (stop) {
  6385. + /*
  6386. + * Make it appear like a SCHED_FIFO task, its something
  6387. + * userspace knows about and won't get confused about.
  6388. + *
  6389. + * Also, it will make PI more or less work without too
  6390. + * much confusion -- but then, stop work should not
  6391. + * rely on PI working anyway.
  6392. + */
  6393. + sched_setscheduler_nocheck(stop, SCHED_FIFO, &stop_param);
  6394. + }
  6395. +
  6396. + cpu_rq(cpu)->stop = stop;
  6397. +
  6398. + if (old_stop) {
  6399. + /*
  6400. + * Reset it back to a normal rt scheduling prio so that
  6401. + * it can die in pieces.
  6402. + */
  6403. + sched_setscheduler_nocheck(old_stop, SCHED_FIFO, &start_param);
  6404. + }
  6405. +}
  6406. +
  6407. +
  6408. +#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
  6409. +
  6410. +static struct ctl_table sd_ctl_dir[] = {
  6411. + {
  6412. + .procname = "sched_domain",
  6413. + .mode = 0555,
  6414. + },
  6415. + {}
  6416. +};
  6417. +
  6418. +static struct ctl_table sd_ctl_root[] = {
  6419. + {
  6420. + .procname = "kernel",
  6421. + .mode = 0555,
  6422. + .child = sd_ctl_dir,
  6423. + },
  6424. + {}
  6425. +};
  6426. +
  6427. +static struct ctl_table *sd_alloc_ctl_entry(int n)
  6428. +{
  6429. + struct ctl_table *entry =
  6430. + kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
  6431. +
  6432. + return entry;
  6433. +}
  6434. +
  6435. +static void sd_free_ctl_entry(struct ctl_table **tablep)
  6436. +{
  6437. + struct ctl_table *entry;
  6438. +
  6439. + /*
  6440. + * In the intermediate directories, both the child directory and
  6441. + * procname are dynamically allocated and could fail but the mode
  6442. + * will always be set. In the lowest directory the names are
  6443. + * static strings and all have proc handlers.
  6444. + */
  6445. + for (entry = *tablep; entry->mode; entry++) {
  6446. + if (entry->child)
  6447. + sd_free_ctl_entry(&entry->child);
  6448. + if (entry->proc_handler == NULL)
  6449. + kfree(entry->procname);
  6450. + }
  6451. +
  6452. + kfree(*tablep);
  6453. + *tablep = NULL;
  6454. +}
  6455. +
  6456. +static void
  6457. +set_table_entry(struct ctl_table *entry,
  6458. + const char *procname, void *data, int maxlen,
  6459. + mode_t mode, proc_handler *proc_handler)
  6460. +{
  6461. + entry->procname = procname;
  6462. + entry->data = data;
  6463. + entry->maxlen = maxlen;
  6464. + entry->mode = mode;
  6465. + entry->proc_handler = proc_handler;
  6466. +}
  6467. +
  6468. +static struct ctl_table *
  6469. +sd_alloc_ctl_domain_table(struct sched_domain *sd)
  6470. +{
  6471. + struct ctl_table *table = sd_alloc_ctl_entry(13);
  6472. +
  6473. + if (table == NULL)
  6474. + return NULL;
  6475. +
  6476. + set_table_entry(&table[0], "min_interval", &sd->min_interval,
  6477. + sizeof(long), 0644, proc_doulongvec_minmax);
  6478. + set_table_entry(&table[1], "max_interval", &sd->max_interval,
  6479. + sizeof(long), 0644, proc_doulongvec_minmax);
  6480. + set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
  6481. + sizeof(int), 0644, proc_dointvec_minmax);
  6482. + set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
  6483. + sizeof(int), 0644, proc_dointvec_minmax);
  6484. + set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
  6485. + sizeof(int), 0644, proc_dointvec_minmax);
  6486. + set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
  6487. + sizeof(int), 0644, proc_dointvec_minmax);
  6488. + set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
  6489. + sizeof(int), 0644, proc_dointvec_minmax);
  6490. + set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
  6491. + sizeof(int), 0644, proc_dointvec_minmax);
  6492. + set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
  6493. + sizeof(int), 0644, proc_dointvec_minmax);
  6494. + set_table_entry(&table[9], "cache_nice_tries",
  6495. + &sd->cache_nice_tries,
  6496. + sizeof(int), 0644, proc_dointvec_minmax);
  6497. + set_table_entry(&table[10], "flags", &sd->flags,
  6498. + sizeof(int), 0644, proc_dointvec_minmax);
  6499. + set_table_entry(&table[11], "name", sd->name,
  6500. + CORENAME_MAX_SIZE, 0444, proc_dostring);
  6501. + /* &table[12] is terminator */
  6502. +
  6503. + return table;
  6504. +}
  6505. +
  6506. +static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
  6507. +{
  6508. + struct ctl_table *entry, *table;
  6509. + struct sched_domain *sd;
  6510. + int domain_num = 0, i;
  6511. + char buf[32];
  6512. +
  6513. + for_each_domain(cpu, sd)
  6514. + domain_num++;
  6515. + entry = table = sd_alloc_ctl_entry(domain_num + 1);
  6516. + if (table == NULL)
  6517. + return NULL;
  6518. +
  6519. + i = 0;
  6520. + for_each_domain(cpu, sd) {
  6521. + snprintf(buf, 32, "domain%d", i);
  6522. + entry->procname = kstrdup(buf, GFP_KERNEL);
  6523. + entry->mode = 0555;
  6524. + entry->child = sd_alloc_ctl_domain_table(sd);
  6525. + entry++;
  6526. + i++;
  6527. + }
  6528. + return table;
  6529. +}
  6530. +
  6531. +static struct ctl_table_header *sd_sysctl_header;
  6532. +static void register_sched_domain_sysctl(void)
  6533. +{
  6534. + int i, cpu_num = num_possible_cpus();
  6535. + struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
  6536. + char buf[32];
  6537. +
  6538. + WARN_ON(sd_ctl_dir[0].child);
  6539. + sd_ctl_dir[0].child = entry;
  6540. +
  6541. + if (entry == NULL)
  6542. + return;
  6543. +
  6544. + for_each_possible_cpu(i) {
  6545. + snprintf(buf, 32, "cpu%d", i);
  6546. + entry->procname = kstrdup(buf, GFP_KERNEL);
  6547. + entry->mode = 0555;
  6548. + entry->child = sd_alloc_ctl_cpu_table(i);
  6549. + entry++;
  6550. + }
  6551. +
  6552. + WARN_ON(sd_sysctl_header);
  6553. + sd_sysctl_header = register_sysctl_table(sd_ctl_root);
  6554. +}
  6555. +
  6556. +/* may be called multiple times per register */
  6557. +static void unregister_sched_domain_sysctl(void)
  6558. +{
  6559. + if (sd_sysctl_header)
  6560. + unregister_sysctl_table(sd_sysctl_header);
  6561. + sd_sysctl_header = NULL;
  6562. + if (sd_ctl_dir[0].child)
  6563. + sd_free_ctl_entry(&sd_ctl_dir[0].child);
  6564. +}
  6565. +#else
  6566. +static void register_sched_domain_sysctl(void)
  6567. +{
  6568. +}
  6569. +static void unregister_sched_domain_sysctl(void)
  6570. +{
  6571. +}
  6572. +#endif
  6573. +
  6574. +static void set_rq_online(struct rq *rq)
  6575. +{
  6576. + if (!rq->online) {
  6577. + cpumask_set_cpu(cpu_of(rq), rq->rd->online);
  6578. + rq->online = true;
  6579. + }
  6580. +}
  6581. +
  6582. +static void set_rq_offline(struct rq *rq)
  6583. +{
  6584. + if (rq->online) {
  6585. + cpumask_clear_cpu(cpu_of(rq), rq->rd->online);
  6586. + rq->online = false;
  6587. + }
  6588. +}
  6589. +
  6590. +/*
  6591. + * migration_call - callback that gets triggered when a CPU is added.
  6592. + */
  6593. +static int __cpuinit
  6594. +migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  6595. +{
  6596. + int cpu = (long)hcpu;
  6597. + unsigned long flags;
  6598. + struct rq *rq = cpu_rq(cpu);
  6599. +#ifdef CONFIG_HOTPLUG_CPU
  6600. + struct task_struct *idle = rq->idle;
  6601. +#endif
  6602. +
  6603. + switch (action & ~CPU_TASKS_FROZEN) {
  6604. +
  6605. + case CPU_UP_PREPARE:
  6606. + break;
  6607. +
  6608. + case CPU_ONLINE:
  6609. + /* Update our root-domain */
  6610. + grq_lock_irqsave(&flags);
  6611. + if (rq->rd) {
  6612. + BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  6613. +
  6614. + set_rq_online(rq);
  6615. + }
  6616. + grq.noc = num_online_cpus();
  6617. + grq_unlock_irqrestore(&flags);
  6618. + break;
  6619. +
  6620. +#ifdef CONFIG_HOTPLUG_CPU
  6621. + case CPU_DEAD:
  6622. + /* Idle task back to normal (off runqueue, low prio) */
  6623. + grq_lock_irq();
  6624. + return_task(idle, true);
  6625. + idle->static_prio = MAX_PRIO;
  6626. + __setscheduler(idle, rq, SCHED_NORMAL, 0);
  6627. + idle->prio = PRIO_LIMIT;
  6628. + set_rq_task(rq, idle);
  6629. + update_clocks(rq);
  6630. + grq_unlock_irq();
  6631. + break;
  6632. +
  6633. + case CPU_DYING:
  6634. + /* Update our root-domain */
  6635. + grq_lock_irqsave(&flags);
  6636. + sched_idle_next(rq, cpu, idle);
  6637. + if (rq->rd) {
  6638. + BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  6639. + set_rq_offline(rq);
  6640. + }
  6641. + break_sole_affinity(cpu, idle);
  6642. + grq.noc = num_online_cpus();
  6643. + grq_unlock_irqrestore(&flags);
  6644. + break;
  6645. +#endif
  6646. + }
  6647. + return NOTIFY_OK;
  6648. +}
  6649. +
  6650. +/*
  6651. + * Register at high priority so that task migration (migrate_all_tasks)
  6652. + * happens before everything else. This has to be lower priority than
  6653. + * the notifier in the perf_counter subsystem, though.
  6654. + */
  6655. +static struct notifier_block __cpuinitdata migration_notifier = {
  6656. + .notifier_call = migration_call,
  6657. + .priority = CPU_PRI_MIGRATION,
  6658. +};
  6659. +
  6660. +static int __cpuinit sched_cpu_active(struct notifier_block *nfb,
  6661. + unsigned long action, void *hcpu)
  6662. +{
  6663. + switch (action & ~CPU_TASKS_FROZEN) {
  6664. + case CPU_STARTING:
  6665. + case CPU_DOWN_FAILED:
  6666. + set_cpu_active((long)hcpu, true);
  6667. + return NOTIFY_OK;
  6668. + default:
  6669. + return NOTIFY_DONE;
  6670. + }
  6671. +}
  6672. +
  6673. +static int __cpuinit sched_cpu_inactive(struct notifier_block *nfb,
  6674. + unsigned long action, void *hcpu)
  6675. +{
  6676. + switch (action & ~CPU_TASKS_FROZEN) {
  6677. + case CPU_DOWN_PREPARE:
  6678. + set_cpu_active((long)hcpu, false);
  6679. + return NOTIFY_OK;
  6680. + default:
  6681. + return NOTIFY_DONE;
  6682. + }
  6683. +}
  6684. +
  6685. +int __init migration_init(void)
  6686. +{
  6687. + void *cpu = (void *)(long)smp_processor_id();
  6688. + int err;
  6689. +
  6690. + /* Initialise migration for the boot CPU */
  6691. + err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  6692. + BUG_ON(err == NOTIFY_BAD);
  6693. + migration_call(&migration_notifier, CPU_ONLINE, cpu);
  6694. + register_cpu_notifier(&migration_notifier);
  6695. +
  6696. + /* Register cpu active notifiers */
  6697. + cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
  6698. + cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
  6699. +
  6700. + return 0;
  6701. +}
  6702. +early_initcall(migration_init);
  6703. +#endif
  6704. +
  6705. +#ifdef CONFIG_SMP
  6706. +
  6707. +static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
  6708. +
  6709. +#ifdef CONFIG_SCHED_DEBUG
  6710. +
  6711. +static __read_mostly int sched_domain_debug_enabled;
  6712. +
  6713. +static int __init sched_domain_debug_setup(char *str)
  6714. +{
  6715. + sched_domain_debug_enabled = 1;
  6716. +
  6717. + return 0;
  6718. +}
  6719. +early_param("sched_debug", sched_domain_debug_setup);
  6720. +
  6721. +static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
  6722. + struct cpumask *groupmask)
  6723. +{
  6724. + struct sched_group *group = sd->groups;
  6725. + char str[256];
  6726. +
  6727. + cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
  6728. + cpumask_clear(groupmask);
  6729. +
  6730. + printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
  6731. +
  6732. + if (!(sd->flags & SD_LOAD_BALANCE)) {
  6733. + printk("does not load-balance\n");
  6734. + if (sd->parent)
  6735. + printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
  6736. + " has parent");
  6737. + return -1;
  6738. + }
  6739. +
  6740. + printk(KERN_CONT "span %s level %s\n", str, sd->name);
  6741. +
  6742. + if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  6743. + printk(KERN_ERR "ERROR: domain->span does not contain "
  6744. + "CPU%d\n", cpu);
  6745. + }
  6746. + if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
  6747. + printk(KERN_ERR "ERROR: domain->groups does not contain"
  6748. + " CPU%d\n", cpu);
  6749. + }
  6750. +
  6751. + printk(KERN_DEBUG "%*s groups:", level + 1, "");
  6752. + do {
  6753. + if (!group) {
  6754. + printk("\n");
  6755. + printk(KERN_ERR "ERROR: group is NULL\n");
  6756. + break;
  6757. + }
  6758. +
  6759. + if (!group->sgp->power) {
  6760. + printk(KERN_CONT "\n");
  6761. + printk(KERN_ERR "ERROR: domain->cpu_power not "
  6762. + "set\n");
  6763. + break;
  6764. + }
  6765. +
  6766. + if (!cpumask_weight(sched_group_cpus(group))) {
  6767. + printk(KERN_CONT "\n");
  6768. + printk(KERN_ERR "ERROR: empty group\n");
  6769. + break;
  6770. + }
  6771. +
  6772. + if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
  6773. + printk(KERN_CONT "\n");
  6774. + printk(KERN_ERR "ERROR: repeated CPUs\n");
  6775. + break;
  6776. + }
  6777. +
  6778. + cpumask_or(groupmask, groupmask, sched_group_cpus(group));
  6779. +
  6780. + cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
  6781. +
  6782. + printk(KERN_CONT " %s", str);
  6783. + if (group->sgp->power != SCHED_POWER_SCALE) {
  6784. + printk(KERN_CONT " (cpu_power = %d)",
  6785. + group->sgp->power);
  6786. + }
  6787. +
  6788. + group = group->next;
  6789. + } while (group != sd->groups);
  6790. + printk(KERN_CONT "\n");
  6791. +
  6792. + if (!cpumask_equal(sched_domain_span(sd), groupmask))
  6793. + printk(KERN_ERR "ERROR: groups don't span domain->span\n");
  6794. +
  6795. + if (sd->parent &&
  6796. + !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
  6797. + printk(KERN_ERR "ERROR: parent span is not a superset "
  6798. + "of domain->span\n");
  6799. + return 0;
  6800. +}
  6801. +
  6802. +static void sched_domain_debug(struct sched_domain *sd, int cpu)
  6803. +{
  6804. + int level = 0;
  6805. +
  6806. + if (!sched_domain_debug_enabled)
  6807. + return;
  6808. +
  6809. + if (!sd) {
  6810. + printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  6811. + return;
  6812. + }
  6813. +
  6814. + printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  6815. +
  6816. + for (;;) {
  6817. + if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
  6818. + break;
  6819. + level++;
  6820. + sd = sd->parent;
  6821. + if (!sd)
  6822. + break;
  6823. + }
  6824. +}
  6825. +#else /* !CONFIG_SCHED_DEBUG */
  6826. +# define sched_domain_debug(sd, cpu) do { } while (0)
  6827. +#endif /* CONFIG_SCHED_DEBUG */
  6828. +
  6829. +static int sd_degenerate(struct sched_domain *sd)
  6830. +{
  6831. + if (cpumask_weight(sched_domain_span(sd)) == 1)
  6832. + return 1;
  6833. +
  6834. + /* Following flags need at least 2 groups */
  6835. + if (sd->flags & (SD_LOAD_BALANCE |
  6836. + SD_BALANCE_NEWIDLE |
  6837. + SD_BALANCE_FORK |
  6838. + SD_BALANCE_EXEC |
  6839. + SD_SHARE_CPUPOWER |
  6840. + SD_SHARE_PKG_RESOURCES)) {
  6841. + if (sd->groups != sd->groups->next)
  6842. + return 0;
  6843. + }
  6844. +
  6845. + /* Following flags don't use groups */
  6846. + if (sd->flags & (SD_WAKE_AFFINE))
  6847. + return 0;
  6848. +
  6849. + return 1;
  6850. +}
  6851. +
  6852. +static int
  6853. +sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  6854. +{
  6855. + unsigned long cflags = sd->flags, pflags = parent->flags;
  6856. +
  6857. + if (sd_degenerate(parent))
  6858. + return 1;
  6859. +
  6860. + if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
  6861. + return 0;
  6862. +
  6863. + /* Flags needing groups don't count if only 1 group in parent */
  6864. + if (parent->groups == parent->groups->next) {
  6865. + pflags &= ~(SD_LOAD_BALANCE |
  6866. + SD_BALANCE_NEWIDLE |
  6867. + SD_BALANCE_FORK |
  6868. + SD_BALANCE_EXEC |
  6869. + SD_SHARE_CPUPOWER |
  6870. + SD_SHARE_PKG_RESOURCES);
  6871. + if (nr_node_ids == 1)
  6872. + pflags &= ~SD_SERIALIZE;
  6873. + }
  6874. + if (~cflags & pflags)
  6875. + return 0;
  6876. +
  6877. + return 1;
  6878. +}
  6879. +
  6880. +static void free_rootdomain(struct rcu_head *rcu)
  6881. +{
  6882. + struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
  6883. +
  6884. + cpupri_cleanup(&rd->cpupri);
  6885. + free_cpumask_var(rd->rto_mask);
  6886. + free_cpumask_var(rd->online);
  6887. + free_cpumask_var(rd->span);
  6888. + kfree(rd);
  6889. +}
  6890. +
  6891. +static void rq_attach_root(struct rq *rq, struct root_domain *rd)
  6892. +{
  6893. + struct root_domain *old_rd = NULL;
  6894. + unsigned long flags;
  6895. +
  6896. + grq_lock_irqsave(&flags);
  6897. +
  6898. + if (rq->rd) {
  6899. + old_rd = rq->rd;
  6900. +
  6901. + if (cpumask_test_cpu(rq->cpu, old_rd->online))
  6902. + set_rq_offline(rq);
  6903. +
  6904. + cpumask_clear_cpu(rq->cpu, old_rd->span);
  6905. +
  6906. + /*
  6907. + * If we dont want to free the old_rt yet then
  6908. + * set old_rd to NULL to skip the freeing later
  6909. + * in this function:
  6910. + */
  6911. + if (!atomic_dec_and_test(&old_rd->refcount))
  6912. + old_rd = NULL;
  6913. + }
  6914. +
  6915. + atomic_inc(&rd->refcount);
  6916. + rq->rd = rd;
  6917. +
  6918. + cpumask_set_cpu(rq->cpu, rd->span);
  6919. + if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
  6920. + set_rq_online(rq);
  6921. +
  6922. + grq_unlock_irqrestore(&flags);
  6923. +
  6924. + if (old_rd)
  6925. + call_rcu_sched(&old_rd->rcu, free_rootdomain);
  6926. +}
  6927. +
  6928. +static int init_rootdomain(struct root_domain *rd)
  6929. +{
  6930. + memset(rd, 0, sizeof(*rd));
  6931. +
  6932. + if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
  6933. + goto out;
  6934. + if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
  6935. + goto free_span;
  6936. + if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
  6937. + goto free_online;
  6938. +
  6939. + if (cpupri_init(&rd->cpupri) != 0)
  6940. + goto free_rto_mask;
  6941. + return 0;
  6942. +
  6943. +free_rto_mask:
  6944. + free_cpumask_var(rd->rto_mask);
  6945. +free_online:
  6946. + free_cpumask_var(rd->online);
  6947. +free_span:
  6948. + free_cpumask_var(rd->span);
  6949. +out:
  6950. + return -ENOMEM;
  6951. +}
  6952. +
  6953. +static void init_defrootdomain(void)
  6954. +{
  6955. + init_rootdomain(&def_root_domain);
  6956. +
  6957. + atomic_set(&def_root_domain.refcount, 1);
  6958. +}
  6959. +
  6960. +static struct root_domain *alloc_rootdomain(void)
  6961. +{
  6962. + struct root_domain *rd;
  6963. +
  6964. + rd = kmalloc(sizeof(*rd), GFP_KERNEL);
  6965. + if (!rd)
  6966. + return NULL;
  6967. +
  6968. + if (init_rootdomain(rd) != 0) {
  6969. + kfree(rd);
  6970. + return NULL;
  6971. + }
  6972. +
  6973. + return rd;
  6974. +}
  6975. +
  6976. +static void free_sched_groups(struct sched_group *sg, int free_sgp)
  6977. +{
  6978. + struct sched_group *tmp, *first;
  6979. +
  6980. + if (!sg)
  6981. + return;
  6982. +
  6983. + first = sg;
  6984. + do {
  6985. + tmp = sg->next;
  6986. +
  6987. + if (free_sgp && atomic_dec_and_test(&sg->sgp->ref))
  6988. + kfree(sg->sgp);
  6989. +
  6990. + kfree(sg);
  6991. + sg = tmp;
  6992. + } while (sg != first);
  6993. +}
  6994. +
  6995. +static void free_sched_domain(struct rcu_head *rcu)
  6996. +{
  6997. + struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
  6998. +
  6999. + /*
  7000. + * If its an overlapping domain it has private groups, iterate and
  7001. + * nuke them all.
  7002. + */
  7003. + if (sd->flags & SD_OVERLAP) {
  7004. + free_sched_groups(sd->groups, 1);
  7005. + } else if (atomic_dec_and_test(&sd->groups->ref)) {
  7006. + kfree(sd->groups->sgp);
  7007. + kfree(sd->groups);
  7008. + }
  7009. + kfree(sd);
  7010. +}
  7011. +
  7012. +static void destroy_sched_domain(struct sched_domain *sd, int cpu)
  7013. +{
  7014. + call_rcu(&sd->rcu, free_sched_domain);
  7015. +}
  7016. +
  7017. +static void destroy_sched_domains(struct sched_domain *sd, int cpu)
  7018. +{
  7019. + for (; sd; sd = sd->parent)
  7020. + destroy_sched_domain(sd, cpu);
  7021. +}
  7022. +
  7023. +/*
  7024. + * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  7025. + * hold the hotplug lock.
  7026. + */
  7027. +static void
  7028. +cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
  7029. +{
  7030. + struct rq *rq = cpu_rq(cpu);
  7031. + struct sched_domain *tmp;
  7032. +
  7033. + /* Remove the sched domains which do not contribute to scheduling. */
  7034. + for (tmp = sd; tmp; ) {
  7035. + struct sched_domain *parent = tmp->parent;
  7036. + if (!parent)
  7037. + break;
  7038. +
  7039. + if (sd_parent_degenerate(tmp, parent)) {
  7040. + tmp->parent = parent->parent;
  7041. + if (parent->parent)
  7042. + parent->parent->child = tmp;
  7043. + destroy_sched_domain(parent, cpu);
  7044. + } else
  7045. + tmp = tmp->parent;
  7046. + }
  7047. +
  7048. + if (sd && sd_degenerate(sd)) {
  7049. + tmp = sd;
  7050. + sd = sd->parent;
  7051. + destroy_sched_domain(tmp, cpu);
  7052. + if (sd)
  7053. + sd->child = NULL;
  7054. + }
  7055. +
  7056. + sched_domain_debug(sd, cpu);
  7057. +
  7058. + rq_attach_root(rq, rd);
  7059. + tmp = rq->sd;
  7060. + rcu_assign_pointer(rq->sd, sd);
  7061. + destroy_sched_domains(tmp, cpu);
  7062. +}
  7063. +
  7064. +/* cpus with isolated domains */
  7065. +static cpumask_var_t cpu_isolated_map;
  7066. +
  7067. +/* Setup the mask of cpus configured for isolated domains */
  7068. +static int __init isolated_cpu_setup(char *str)
  7069. +{
  7070. + alloc_bootmem_cpumask_var(&cpu_isolated_map);
  7071. + cpulist_parse(str, cpu_isolated_map);
  7072. + return 1;
  7073. +}
  7074. +
  7075. +__setup("isolcpus=", isolated_cpu_setup);
  7076. +
  7077. +#define SD_NODES_PER_DOMAIN 16
  7078. +
  7079. +#ifdef CONFIG_NUMA
  7080. +
  7081. +/**
  7082. + * find_next_best_node - find the next node to include in a sched_domain
  7083. + * @node: node whose sched_domain we're building
  7084. + * @used_nodes: nodes already in the sched_domain
  7085. + *
  7086. + * Find the next node to include in a given scheduling domain. Simply
  7087. + * finds the closest node not already in the @used_nodes map.
  7088. + *
  7089. + * Should use nodemask_t.
  7090. + */
  7091. +static int find_next_best_node(int node, nodemask_t *used_nodes)
  7092. +{
  7093. + int i, n, val, min_val, best_node = -1;
  7094. +
  7095. + min_val = INT_MAX;
  7096. +
  7097. + for (i = 0; i < nr_node_ids; i++) {
  7098. + /* Start at @node */
  7099. + n = (node + i) % nr_node_ids;
  7100. +
  7101. + if (!nr_cpus_node(n))
  7102. + continue;
  7103. +
  7104. + /* Skip already used nodes */
  7105. + if (node_isset(n, *used_nodes))
  7106. + continue;
  7107. +
  7108. + /* Simple min distance search */
  7109. + val = node_distance(node, n);
  7110. +
  7111. + if (val < min_val) {
  7112. + min_val = val;
  7113. + best_node = n;
  7114. + }
  7115. + }
  7116. +
  7117. + if (best_node != -1)
  7118. + node_set(best_node, *used_nodes);
  7119. + return best_node;
  7120. +}
  7121. +
  7122. +/**
  7123. + * sched_domain_node_span - get a cpumask for a node's sched_domain
  7124. + * @node: node whose cpumask we're constructing
  7125. + * @span: resulting cpumask
  7126. + *
  7127. + * Given a node, construct a good cpumask for its sched_domain to span. It
  7128. + * should be one that prevents unnecessary balancing, but also spreads tasks
  7129. + * out optimally.
  7130. + */
  7131. +static void sched_domain_node_span(int node, struct cpumask *span)
  7132. +{
  7133. + nodemask_t used_nodes;
  7134. + int i;
  7135. +
  7136. + cpumask_clear(span);
  7137. + nodes_clear(used_nodes);
  7138. +
  7139. + cpumask_or(span, span, cpumask_of_node(node));
  7140. + node_set(node, used_nodes);
  7141. +
  7142. + for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
  7143. + int next_node = find_next_best_node(node, &used_nodes);
  7144. + if (next_node < 0)
  7145. + break;
  7146. + cpumask_or(span, span, cpumask_of_node(next_node));
  7147. + }
  7148. +}
  7149. +
  7150. +static const struct cpumask *cpu_node_mask(int cpu)
  7151. +{
  7152. + lockdep_assert_held(&sched_domains_mutex);
  7153. +
  7154. + sched_domain_node_span(cpu_to_node(cpu), sched_domains_tmpmask);
  7155. +
  7156. + return sched_domains_tmpmask;
  7157. +}
  7158. +
  7159. +static const struct cpumask *cpu_allnodes_mask(int cpu)
  7160. +{
  7161. + return cpu_possible_mask;
  7162. +}
  7163. +#endif /* CONFIG_NUMA */
  7164. +
  7165. +static const struct cpumask *cpu_cpu_mask(int cpu)
  7166. +{
  7167. + return cpumask_of_node(cpu_to_node(cpu));
  7168. +}
  7169. +
  7170. +int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
  7171. +
  7172. +struct sd_data {
  7173. + struct sched_domain **__percpu sd;
  7174. + struct sched_group **__percpu sg;
  7175. + struct sched_group_power **__percpu sgp;
  7176. +};
  7177. +
  7178. +struct s_data {
  7179. + struct sched_domain ** __percpu sd;
  7180. + struct root_domain *rd;
  7181. +};
  7182. +
  7183. +enum s_alloc {
  7184. + sa_rootdomain,
  7185. + sa_sd,
  7186. + sa_sd_storage,
  7187. + sa_none,
  7188. +};
  7189. +
  7190. +struct sched_domain_topology_level;
  7191. +
  7192. +typedef struct sched_domain *(*sched_domain_init_f)(struct sched_domain_topology_level *tl, int cpu);
  7193. +typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
  7194. +
  7195. +#define SDTL_OVERLAP 0x01
  7196. +
  7197. +struct sched_domain_topology_level {
  7198. + sched_domain_init_f init;
  7199. + sched_domain_mask_f mask;
  7200. + int flags;
  7201. + struct sd_data data;
  7202. +};
  7203. +
  7204. +static int
  7205. +build_overlap_sched_groups(struct sched_domain *sd, int cpu)
  7206. +{
  7207. + struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
  7208. + const struct cpumask *span = sched_domain_span(sd);
  7209. + struct cpumask *covered = sched_domains_tmpmask;
  7210. + struct sd_data *sdd = sd->private;
  7211. + struct sched_domain *child;
  7212. + int i;
  7213. +
  7214. + cpumask_clear(covered);
  7215. +
  7216. + for_each_cpu(i, span) {
  7217. + struct cpumask *sg_span;
  7218. +
  7219. + if (cpumask_test_cpu(i, covered))
  7220. + continue;
  7221. +
  7222. + sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
  7223. + GFP_KERNEL, cpu_to_node(i));
  7224. +
  7225. + if (!sg)
  7226. + goto fail;
  7227. +
  7228. + sg_span = sched_group_cpus(sg);
  7229. +
  7230. + child = *per_cpu_ptr(sdd->sd, i);
  7231. + if (child->child) {
  7232. + child = child->child;
  7233. + cpumask_copy(sg_span, sched_domain_span(child));
  7234. + } else
  7235. + cpumask_set_cpu(i, sg_span);
  7236. +
  7237. + cpumask_or(covered, covered, sg_span);
  7238. +
  7239. + sg->sgp = *per_cpu_ptr(sdd->sgp, cpumask_first(sg_span));
  7240. + atomic_inc(&sg->sgp->ref);
  7241. +
  7242. + if (cpumask_test_cpu(cpu, sg_span))
  7243. + groups = sg;
  7244. +
  7245. + if (!first)
  7246. + first = sg;
  7247. + if (last)
  7248. + last->next = sg;
  7249. + last = sg;
  7250. + last->next = first;
  7251. + }
  7252. + sd->groups = groups;
  7253. +
  7254. + return 0;
  7255. +
  7256. +fail:
  7257. + free_sched_groups(first, 0);
  7258. +
  7259. + return -ENOMEM;
  7260. +}
  7261. +
  7262. +static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
  7263. +{
  7264. + struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
  7265. + struct sched_domain *child = sd->child;
  7266. +
  7267. + if (child)
  7268. + cpu = cpumask_first(sched_domain_span(child));
  7269. +
  7270. + if (sg) {
  7271. + *sg = *per_cpu_ptr(sdd->sg, cpu);
  7272. + (*sg)->sgp = *per_cpu_ptr(sdd->sgp, cpu);
  7273. + atomic_set(&(*sg)->sgp->ref, 1); /* for claim_allocations */
  7274. + }
  7275. +
  7276. + return cpu;
  7277. +}
  7278. +
  7279. +/*
  7280. + * build_sched_groups will build a circular linked list of the groups
  7281. + * covered by the given span, and will set each group's ->cpumask correctly,
  7282. + * and ->cpu_power to 0.
  7283. + *
  7284. + * Assumes the sched_domain tree is fully constructed
  7285. + */
  7286. +static int
  7287. +build_sched_groups(struct sched_domain *sd, int cpu)
  7288. +{
  7289. + struct sched_group *first = NULL, *last = NULL;
  7290. + struct sd_data *sdd = sd->private;
  7291. + const struct cpumask *span = sched_domain_span(sd);
  7292. + struct cpumask *covered;
  7293. + int i;
  7294. +
  7295. + get_group(cpu, sdd, &sd->groups);
  7296. + atomic_inc(&sd->groups->ref);
  7297. +
  7298. + if (cpu != cpumask_first(sched_domain_span(sd)))
  7299. + return 0;
  7300. +
  7301. + lockdep_assert_held(&sched_domains_mutex);
  7302. + covered = sched_domains_tmpmask;
  7303. +
  7304. + cpumask_clear(covered);
  7305. +
  7306. + for_each_cpu(i, span) {
  7307. + struct sched_group *sg;
  7308. + int group = get_group(i, sdd, &sg);
  7309. + int j;
  7310. +
  7311. + if (cpumask_test_cpu(i, covered))
  7312. + continue;
  7313. +
  7314. + cpumask_clear(sched_group_cpus(sg));
  7315. + sg->sgp->power = 0;
  7316. +
  7317. + for_each_cpu(j, span) {
  7318. + if (get_group(j, sdd, NULL) != group)
  7319. + continue;
  7320. +
  7321. + cpumask_set_cpu(j, covered);
  7322. + cpumask_set_cpu(j, sched_group_cpus(sg));
  7323. + }
  7324. +
  7325. + if (!first)
  7326. + first = sg;
  7327. + if (last)
  7328. + last->next = sg;
  7329. + last = sg;
  7330. + }
  7331. + last->next = first;
  7332. +
  7333. + return 0;
  7334. +}
  7335. +
  7336. +/*
  7337. + * Initializers for schedule domains
  7338. + * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
  7339. + */
  7340. +
  7341. +#ifdef CONFIG_SCHED_DEBUG
  7342. +# define SD_INIT_NAME(sd, type) sd->name = #type
  7343. +#else
  7344. +# define SD_INIT_NAME(sd, type) do { } while (0)
  7345. +#endif
  7346. +
  7347. +#define SD_INIT_FUNC(type) \
  7348. +static noinline struct sched_domain * \
  7349. +sd_init_##type(struct sched_domain_topology_level *tl, int cpu) \
  7350. +{ \
  7351. + struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu); \
  7352. + *sd = SD_##type##_INIT; \
  7353. + SD_INIT_NAME(sd, type); \
  7354. + sd->private = &tl->data; \
  7355. + return sd; \
  7356. +}
  7357. +
  7358. +SD_INIT_FUNC(CPU)
  7359. +#ifdef CONFIG_NUMA
  7360. + SD_INIT_FUNC(ALLNODES)
  7361. + SD_INIT_FUNC(NODE)
  7362. +#endif
  7363. +#ifdef CONFIG_SCHED_SMT
  7364. + SD_INIT_FUNC(SIBLING)
  7365. +#endif
  7366. +#ifdef CONFIG_SCHED_MC
  7367. + SD_INIT_FUNC(MC)
  7368. +#endif
  7369. +#ifdef CONFIG_SCHED_BOOK
  7370. + SD_INIT_FUNC(BOOK)
  7371. +#endif
  7372. +
  7373. +static int default_relax_domain_level = -1;
  7374. +int sched_domain_level_max;
  7375. +
  7376. +static int __init setup_relax_domain_level(char *str)
  7377. +{
  7378. + unsigned long val;
  7379. +
  7380. + val = simple_strtoul(str, NULL, 0);
  7381. + if (val < sched_domain_level_max)
  7382. + default_relax_domain_level = val;
  7383. +
  7384. + return 1;
  7385. +}
  7386. +__setup("relax_domain_level=", setup_relax_domain_level);
  7387. +
  7388. +static void set_domain_attribute(struct sched_domain *sd,
  7389. + struct sched_domain_attr *attr)
  7390. +{
  7391. + int request;
  7392. +
  7393. + if (!attr || attr->relax_domain_level < 0) {
  7394. + if (default_relax_domain_level < 0)
  7395. + return;
  7396. + else
  7397. + request = default_relax_domain_level;
  7398. + } else
  7399. + request = attr->relax_domain_level;
  7400. + if (request < sd->level) {
  7401. + /* turn off idle balance on this domain */
  7402. + sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
  7403. + } else {
  7404. + /* turn on idle balance on this domain */
  7405. + sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
  7406. + }
  7407. +}
  7408. +
  7409. +static void __sdt_free(const struct cpumask *cpu_map);
  7410. +static int __sdt_alloc(const struct cpumask *cpu_map);
  7411. +
  7412. +static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
  7413. + const struct cpumask *cpu_map)
  7414. +{
  7415. + switch (what) {
  7416. + case sa_rootdomain:
  7417. + if (!atomic_read(&d->rd->refcount))
  7418. + free_rootdomain(&d->rd->rcu); /* fall through */
  7419. + case sa_sd:
  7420. + free_percpu(d->sd); /* fall through */
  7421. + case sa_sd_storage:
  7422. + __sdt_free(cpu_map); /* fall through */
  7423. + case sa_none:
  7424. + break;
  7425. + }
  7426. +}
  7427. +
  7428. +static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
  7429. + const struct cpumask *cpu_map)
  7430. +{
  7431. + memset(d, 0, sizeof(*d));
  7432. +
  7433. + if (__sdt_alloc(cpu_map))
  7434. + return sa_sd_storage;
  7435. + d->sd = alloc_percpu(struct sched_domain *);
  7436. + if (!d->sd)
  7437. + return sa_sd_storage;
  7438. + d->rd = alloc_rootdomain();
  7439. + if (!d->rd)
  7440. + return sa_sd;
  7441. + return sa_rootdomain;
  7442. +}
  7443. +
  7444. +/*
  7445. + * NULL the sd_data elements we've used to build the sched_domain and
  7446. + * sched_group structure so that the subsequent __free_domain_allocs()
  7447. + * will not free the data we're using.
  7448. + */
  7449. +static void claim_allocations(int cpu, struct sched_domain *sd)
  7450. +{
  7451. + struct sd_data *sdd = sd->private;
  7452. +
  7453. + WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
  7454. + *per_cpu_ptr(sdd->sd, cpu) = NULL;
  7455. +
  7456. + if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
  7457. + *per_cpu_ptr(sdd->sg, cpu) = NULL;
  7458. +
  7459. + if (atomic_read(&(*per_cpu_ptr(sdd->sgp, cpu))->ref))
  7460. + *per_cpu_ptr(sdd->sgp, cpu) = NULL;
  7461. +}
  7462. +
  7463. +#ifdef CONFIG_SCHED_SMT
  7464. +static const struct cpumask *cpu_smt_mask(int cpu)
  7465. +{
  7466. + return topology_thread_cpumask(cpu);
  7467. +}
  7468. +#endif
  7469. +
  7470. +/*
  7471. + * Topology list, bottom-up.
  7472. + */
  7473. +static struct sched_domain_topology_level default_topology[] = {
  7474. +#ifdef CONFIG_SCHED_SMT
  7475. + { sd_init_SIBLING, cpu_smt_mask, },
  7476. +#endif
  7477. +#ifdef CONFIG_SCHED_MC
  7478. + { sd_init_MC, cpu_coregroup_mask, },
  7479. +#endif
  7480. +#ifdef CONFIG_SCHED_BOOK
  7481. + { sd_init_BOOK, cpu_book_mask, },
  7482. +#endif
  7483. + { sd_init_CPU, cpu_cpu_mask, },
  7484. +#ifdef CONFIG_NUMA
  7485. + { sd_init_NODE, cpu_node_mask, SDTL_OVERLAP, },
  7486. + { sd_init_ALLNODES, cpu_allnodes_mask, },
  7487. +#endif
  7488. + { NULL, },
  7489. +};
  7490. +
  7491. +static struct sched_domain_topology_level *sched_domain_topology = default_topology;
  7492. +
  7493. +static int __sdt_alloc(const struct cpumask *cpu_map)
  7494. +{
  7495. + struct sched_domain_topology_level *tl;
  7496. + int j;
  7497. +
  7498. + for (tl = sched_domain_topology; tl->init; tl++) {
  7499. + struct sd_data *sdd = &tl->data;
  7500. +
  7501. + sdd->sd = alloc_percpu(struct sched_domain *);
  7502. + if (!sdd->sd)
  7503. + return -ENOMEM;
  7504. +
  7505. + sdd->sg = alloc_percpu(struct sched_group *);
  7506. + if (!sdd->sg)
  7507. + return -ENOMEM;
  7508. +
  7509. + sdd->sgp = alloc_percpu(struct sched_group_power *);
  7510. + if (!sdd->sgp)
  7511. + return -ENOMEM;
  7512. +
  7513. + for_each_cpu(j, cpu_map) {
  7514. + struct sched_domain *sd;
  7515. + struct sched_group *sg;
  7516. + struct sched_group_power *sgp;
  7517. +
  7518. + sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
  7519. + GFP_KERNEL, cpu_to_node(j));
  7520. + if (!sd)
  7521. + return -ENOMEM;
  7522. +
  7523. + *per_cpu_ptr(sdd->sd, j) = sd;
  7524. +
  7525. + sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
  7526. + GFP_KERNEL, cpu_to_node(j));
  7527. + if (!sg)
  7528. + return -ENOMEM;
  7529. +
  7530. + sg->next = sg;
  7531. +
  7532. + *per_cpu_ptr(sdd->sg, j) = sg;
  7533. +
  7534. + sgp = kzalloc_node(sizeof(struct sched_group_power),
  7535. + GFP_KERNEL, cpu_to_node(j));
  7536. + if (!sgp)
  7537. + return -ENOMEM;
  7538. +
  7539. + *per_cpu_ptr(sdd->sgp, j) = sgp;
  7540. + }
  7541. + }
  7542. +
  7543. + return 0;
  7544. +}
  7545. +
  7546. +static void __sdt_free(const struct cpumask *cpu_map)
  7547. +{
  7548. + struct sched_domain_topology_level *tl;
  7549. + int j;
  7550. +
  7551. + for (tl = sched_domain_topology; tl->init; tl++) {
  7552. + struct sd_data *sdd = &tl->data;
  7553. +
  7554. + for_each_cpu(j, cpu_map) {
  7555. + struct sched_domain *sd;
  7556. +
  7557. + if (sdd->sd) {
  7558. + sd = *per_cpu_ptr(sdd->sd, j);
  7559. + if (sd && (sd->flags & SD_OVERLAP))
  7560. + free_sched_groups(sd->groups, 0);
  7561. + kfree(*per_cpu_ptr(sdd->sd, j));
  7562. + }
  7563. +
  7564. + if (sdd->sg)
  7565. + kfree(*per_cpu_ptr(sdd->sg, j));
  7566. + if (sdd->sgp)
  7567. + kfree(*per_cpu_ptr(sdd->sgp, j));
  7568. + }
  7569. + free_percpu(sdd->sd);
  7570. + sdd->sd = NULL;
  7571. + free_percpu(sdd->sg);
  7572. + sdd->sg = NULL;
  7573. + free_percpu(sdd->sgp);
  7574. + sdd->sgp = NULL;
  7575. + }
  7576. +}
  7577. +
  7578. +struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
  7579. + struct s_data *d, const struct cpumask *cpu_map,
  7580. + struct sched_domain_attr *attr, struct sched_domain *child,
  7581. + int cpu)
  7582. +{
  7583. + struct sched_domain *sd = tl->init(tl, cpu);
  7584. + if (!sd)
  7585. + return child;
  7586. +
  7587. + set_domain_attribute(sd, attr);
  7588. + cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
  7589. + if (child) {
  7590. + sd->level = child->level + 1;
  7591. + sched_domain_level_max = max(sched_domain_level_max, sd->level);
  7592. + child->parent = sd;
  7593. + }
  7594. + sd->child = child;
  7595. +
  7596. + return sd;
  7597. +}
  7598. +
  7599. +/*
  7600. + * Build sched domains for a given set of cpus and attach the sched domains
  7601. + * to the individual cpus
  7602. + */
  7603. +static int build_sched_domains(const struct cpumask *cpu_map,
  7604. + struct sched_domain_attr *attr)
  7605. +{
  7606. + enum s_alloc alloc_state = sa_none;
  7607. + struct sched_domain *sd;
  7608. + struct s_data d;
  7609. + int i, ret = -ENOMEM;
  7610. +
  7611. + alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
  7612. + if (alloc_state != sa_rootdomain)
  7613. + goto error;
  7614. +
  7615. + /* Set up domains for cpus specified by the cpu_map. */
  7616. + for_each_cpu(i, cpu_map) {
  7617. + struct sched_domain_topology_level *tl;
  7618. +
  7619. + sd = NULL;
  7620. + for (tl = sched_domain_topology; tl->init; tl++) {
  7621. + sd = build_sched_domain(tl, &d, cpu_map, attr, sd, i);
  7622. + if (tl->flags & SDTL_OVERLAP)
  7623. + sd->flags |= SD_OVERLAP;
  7624. + if (cpumask_equal(cpu_map, sched_domain_span(sd)))
  7625. + break;
  7626. + }
  7627. +
  7628. + while (sd->child)
  7629. + sd = sd->child;
  7630. +
  7631. + *per_cpu_ptr(d.sd, i) = sd;
  7632. + }
  7633. +
  7634. + /* Build the groups for the domains */
  7635. + for_each_cpu(i, cpu_map) {
  7636. + for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
  7637. + sd->span_weight = cpumask_weight(sched_domain_span(sd));
  7638. + if (sd->flags & SD_OVERLAP) {
  7639. + if (build_overlap_sched_groups(sd, i))
  7640. + goto error;
  7641. + } else {
  7642. + if (build_sched_groups(sd, i))
  7643. + goto error;
  7644. + }
  7645. + }
  7646. + }
  7647. +
  7648. + /* Calculate CPU power for physical packages and nodes */
  7649. + for (i = nr_cpumask_bits-1; i >= 0; i--) {
  7650. + if (!cpumask_test_cpu(i, cpu_map))
  7651. + continue;
  7652. +
  7653. + for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
  7654. + claim_allocations(i, sd);
  7655. + }
  7656. + }
  7657. +
  7658. + /* Attach the domains */
  7659. + rcu_read_lock();
  7660. + for_each_cpu(i, cpu_map) {
  7661. + sd = *per_cpu_ptr(d.sd, i);
  7662. + cpu_attach_domain(sd, d.rd, i);
  7663. + }
  7664. + rcu_read_unlock();
  7665. +
  7666. + ret = 0;
  7667. +error:
  7668. + __free_domain_allocs(&d, alloc_state, cpu_map);
  7669. + return ret;
  7670. +}
  7671. +
  7672. +static cpumask_var_t *doms_cur; /* current sched domains */
  7673. +static int ndoms_cur; /* number of sched domains in 'doms_cur' */
  7674. +static struct sched_domain_attr *dattr_cur;
  7675. + /* attribues of custom domains in 'doms_cur' */
  7676. +
  7677. +/*
  7678. + * Special case: If a kmalloc of a doms_cur partition (array of
  7679. + * cpumask) fails, then fallback to a single sched domain,
  7680. + * as determined by the single cpumask fallback_doms.
  7681. + */
  7682. +static cpumask_var_t fallback_doms;
  7683. +
  7684. +/*
  7685. + * arch_update_cpu_topology lets virtualized architectures update the
  7686. + * cpu core maps. It is supposed to return 1 if the topology changed
  7687. + * or 0 if it stayed the same.
  7688. + */
  7689. +int __attribute__((weak)) arch_update_cpu_topology(void)
  7690. +{
  7691. + return 0;
  7692. +}
  7693. +
  7694. +cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
  7695. +{
  7696. + int i;
  7697. + cpumask_var_t *doms;
  7698. +
  7699. + doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
  7700. + if (!doms)
  7701. + return NULL;
  7702. + for (i = 0; i < ndoms; i++) {
  7703. + if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
  7704. + free_sched_domains(doms, i);
  7705. + return NULL;
  7706. + }
  7707. + }
  7708. + return doms;
  7709. +}
  7710. +
  7711. +void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
  7712. +{
  7713. + unsigned int i;
  7714. + for (i = 0; i < ndoms; i++)
  7715. + free_cpumask_var(doms[i]);
  7716. + kfree(doms);
  7717. +}
  7718. +
  7719. +/*
  7720. + * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  7721. + * For now this just excludes isolated cpus, but could be used to
  7722. + * exclude other special cases in the future.
  7723. + */
  7724. +static int init_sched_domains(const struct cpumask *cpu_map)
  7725. +{
  7726. + int err;
  7727. +
  7728. + arch_update_cpu_topology();
  7729. + ndoms_cur = 1;
  7730. + doms_cur = alloc_sched_domains(ndoms_cur);
  7731. + if (!doms_cur)
  7732. + doms_cur = &fallback_doms;
  7733. + cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
  7734. + dattr_cur = NULL;
  7735. + err = build_sched_domains(doms_cur[0], NULL);
  7736. + register_sched_domain_sysctl();
  7737. +
  7738. + return err;
  7739. +}
  7740. +
  7741. +/*
  7742. + * Detach sched domains from a group of cpus specified in cpu_map
  7743. + * These cpus will now be attached to the NULL domain
  7744. + */
  7745. +static void detach_destroy_domains(const struct cpumask *cpu_map)
  7746. +{
  7747. + int i;
  7748. +
  7749. + rcu_read_lock();
  7750. + for_each_cpu(i, cpu_map)
  7751. + cpu_attach_domain(NULL, &def_root_domain, i);
  7752. + rcu_read_unlock();
  7753. +}
  7754. +
  7755. +/* handle null as "default" */
  7756. +static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
  7757. + struct sched_domain_attr *new, int idx_new)
  7758. +{
  7759. + struct sched_domain_attr tmp;
  7760. +
  7761. + /* fast path */
  7762. + if (!new && !cur)
  7763. + return 1;
  7764. +
  7765. + tmp = SD_ATTR_INIT;
  7766. + return !memcmp(cur ? (cur + idx_cur) : &tmp,
  7767. + new ? (new + idx_new) : &tmp,
  7768. + sizeof(struct sched_domain_attr));
  7769. +}
  7770. +
  7771. +/*
  7772. + * Partition sched domains as specified by the 'ndoms_new'
  7773. + * cpumasks in the array doms_new[] of cpumasks. This compares
  7774. + * doms_new[] to the current sched domain partitioning, doms_cur[].
  7775. + * It destroys each deleted domain and builds each new domain.
  7776. + *
  7777. + * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
  7778. + * The masks don't intersect (don't overlap.) We should setup one
  7779. + * sched domain for each mask. CPUs not in any of the cpumasks will
  7780. + * not be load balanced. If the same cpumask appears both in the
  7781. + * current 'doms_cur' domains and in the new 'doms_new', we can leave
  7782. + * it as it is.
  7783. + *
  7784. + * The passed in 'doms_new' should be allocated using
  7785. + * alloc_sched_domains. This routine takes ownership of it and will
  7786. + * free_sched_domains it when done with it. If the caller failed the
  7787. + * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
  7788. + * and partition_sched_domains() will fallback to the single partition
  7789. + * 'fallback_doms', it also forces the domains to be rebuilt.
  7790. + *
  7791. + * If doms_new == NULL it will be replaced with cpu_online_mask.
  7792. + * ndoms_new == 0 is a special case for destroying existing domains,
  7793. + * and it will not create the default domain.
  7794. + *
  7795. + * Call with hotplug lock held
  7796. + */
  7797. +void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
  7798. + struct sched_domain_attr *dattr_new)
  7799. +{
  7800. + int i, j, n;
  7801. + int new_topology;
  7802. +
  7803. + mutex_lock(&sched_domains_mutex);
  7804. +
  7805. + /* always unregister in case we don't destroy any domains */
  7806. + unregister_sched_domain_sysctl();
  7807. +
  7808. + /* Let architecture update cpu core mappings. */
  7809. + new_topology = arch_update_cpu_topology();
  7810. +
  7811. + n = doms_new ? ndoms_new : 0;
  7812. +
  7813. + /* Destroy deleted domains */
  7814. + for (i = 0; i < ndoms_cur; i++) {
  7815. + for (j = 0; j < n && !new_topology; j++) {
  7816. + if (cpumask_equal(doms_cur[i], doms_new[j])
  7817. + && dattrs_equal(dattr_cur, i, dattr_new, j))
  7818. + goto match1;
  7819. + }
  7820. + /* no match - a current sched domain not in new doms_new[] */
  7821. + detach_destroy_domains(doms_cur[i]);
  7822. +match1:
  7823. + ;
  7824. + }
  7825. +
  7826. + if (doms_new == NULL) {
  7827. + ndoms_cur = 0;
  7828. + doms_new = &fallback_doms;
  7829. + cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
  7830. + WARN_ON_ONCE(dattr_new);
  7831. + }
  7832. +
  7833. + /* Build new domains */
  7834. + for (i = 0; i < ndoms_new; i++) {
  7835. + for (j = 0; j < ndoms_cur && !new_topology; j++) {
  7836. + if (cpumask_equal(doms_new[i], doms_cur[j])
  7837. + && dattrs_equal(dattr_new, i, dattr_cur, j))
  7838. + goto match2;
  7839. + }
  7840. + /* no match - add a new doms_new */
  7841. + build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
  7842. +match2:
  7843. + ;
  7844. + }
  7845. +
  7846. + /* Remember the new sched domains */
  7847. + if (doms_cur != &fallback_doms)
  7848. + free_sched_domains(doms_cur, ndoms_cur);
  7849. + kfree(dattr_cur); /* kfree(NULL) is safe */
  7850. + doms_cur = doms_new;
  7851. + dattr_cur = dattr_new;
  7852. + ndoms_cur = ndoms_new;
  7853. +
  7854. + register_sched_domain_sysctl();
  7855. +
  7856. + mutex_unlock(&sched_domains_mutex);
  7857. +}
  7858. +
  7859. +#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  7860. +static void reinit_sched_domains(void)
  7861. +{
  7862. + get_online_cpus();
  7863. +
  7864. + /* Destroy domains first to force the rebuild */
  7865. + partition_sched_domains(0, NULL, NULL);
  7866. +
  7867. + rebuild_sched_domains();
  7868. + put_online_cpus();
  7869. +}
  7870. +
  7871. +static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
  7872. +{
  7873. + unsigned int level = 0;
  7874. +
  7875. + if (sscanf(buf, "%u", &level) != 1)
  7876. + return -EINVAL;
  7877. +
  7878. + /*
  7879. + * level is always be positive so don't check for
  7880. + * level < POWERSAVINGS_BALANCE_NONE which is 0
  7881. + * What happens on 0 or 1 byte write,
  7882. + * need to check for count as well?
  7883. + */
  7884. +
  7885. + if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
  7886. + return -EINVAL;
  7887. +
  7888. + if (smt)
  7889. + sched_smt_power_savings = level;
  7890. + else
  7891. + sched_mc_power_savings = level;
  7892. +
  7893. + reinit_sched_domains();
  7894. +
  7895. + return count;
  7896. +}
  7897. +
  7898. +#ifdef CONFIG_SCHED_MC
  7899. +static ssize_t sched_mc_power_savings_show(struct device *dev,
  7900. + struct device_attribute *attr,
  7901. + char *buf)
  7902. +{
  7903. + return sprintf(buf, "%u\n", sched_mc_power_savings);
  7904. +}
  7905. +static ssize_t sched_mc_power_savings_store(struct device *dev,
  7906. + struct device_attribute *attr,
  7907. + const char *buf, size_t count)
  7908. +{
  7909. + return sched_power_savings_store(buf, count, 0);
  7910. +}
  7911. +static DEVICE_ATTR(sched_mc_power_savings, 0644,
  7912. + sched_mc_power_savings_show,
  7913. + sched_mc_power_savings_store);
  7914. +#endif
  7915. +
  7916. +#ifdef CONFIG_SCHED_SMT
  7917. +static ssize_t sched_smt_power_savings_show(struct device *dev,
  7918. + struct device_attribute *attr,
  7919. + char *buf)
  7920. +{
  7921. + return sprintf(buf, "%u\n", sched_smt_power_savings);
  7922. +}
  7923. +static ssize_t sched_smt_power_savings_store(struct device *dev,
  7924. + struct device_attribute *attr,
  7925. + const char *buf, size_t count)
  7926. +{
  7927. + return sched_power_savings_store(buf, count, 1);
  7928. +}
  7929. +static DEVICE_ATTR(sched_smt_power_savings, 0644,
  7930. + sched_smt_power_savings_show,
  7931. + sched_smt_power_savings_store);
  7932. +#endif
  7933. +
  7934. +int __init sched_create_sysfs_power_savings_entries(struct device *dev)
  7935. +{
  7936. + int err = 0;
  7937. +
  7938. +#ifdef CONFIG_SCHED_SMT
  7939. + if (smt_capable())
  7940. + err = device_create_file(dev, &dev_attr_sched_smt_power_savings);
  7941. +#endif
  7942. +#ifdef CONFIG_SCHED_MC
  7943. + if (!err && mc_capable())
  7944. + err = device_create_file(dev, &dev_attr_sched_mc_power_savings);
  7945. +#endif
  7946. + return err;
  7947. +}
  7948. +#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
  7949. +
  7950. +/*
  7951. + * Update cpusets according to cpu_active mask. If cpusets are
  7952. + * disabled, cpuset_update_active_cpus() becomes a simple wrapper
  7953. + * around partition_sched_domains().
  7954. + */
  7955. +static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
  7956. + void *hcpu)
  7957. +{
  7958. + switch (action & ~CPU_TASKS_FROZEN) {
  7959. + case CPU_ONLINE:
  7960. + case CPU_DOWN_FAILED:
  7961. + cpuset_update_active_cpus();
  7962. + return NOTIFY_OK;
  7963. + default:
  7964. + return NOTIFY_DONE;
  7965. + }
  7966. +}
  7967. +
  7968. +static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
  7969. + void *hcpu)
  7970. +{
  7971. + switch (action & ~CPU_TASKS_FROZEN) {
  7972. + case CPU_DOWN_PREPARE:
  7973. + cpuset_update_active_cpus();
  7974. + return NOTIFY_OK;
  7975. + default:
  7976. + return NOTIFY_DONE;
  7977. + }
  7978. +}
  7979. +
  7980. +#if defined(CONFIG_SCHED_SMT) || defined(CONFIG_SCHED_MC)
  7981. +/*
  7982. + * Cheaper version of the below functions in case support for SMT and MC is
  7983. + * compiled in but CPUs have no siblings.
  7984. + */
  7985. +static bool sole_cpu_idle(int cpu)
  7986. +{
  7987. + return rq_idle(cpu_rq(cpu));
  7988. +}
  7989. +#endif
  7990. +#ifdef CONFIG_SCHED_SMT
  7991. +/* All this CPU's SMT siblings are idle */
  7992. +static bool siblings_cpu_idle(int cpu)
  7993. +{
  7994. + return cpumask_subset(&(cpu_rq(cpu)->smt_siblings),
  7995. + &grq.cpu_idle_map);
  7996. +}
  7997. +#endif
  7998. +#ifdef CONFIG_SCHED_MC
  7999. +/* All this CPU's shared cache siblings are idle */
  8000. +static bool cache_cpu_idle(int cpu)
  8001. +{
  8002. + return cpumask_subset(&(cpu_rq(cpu)->cache_siblings),
  8003. + &grq.cpu_idle_map);
  8004. +}
  8005. +#endif
  8006. +
  8007. +enum sched_domain_level {
  8008. + SD_LV_NONE = 0,
  8009. + SD_LV_SIBLING,
  8010. + SD_LV_MC,
  8011. + SD_LV_BOOK,
  8012. + SD_LV_CPU,
  8013. + SD_LV_NODE,
  8014. + SD_LV_ALLNODES,
  8015. + SD_LV_MAX
  8016. +};
  8017. +
  8018. +void __init sched_init_smp(void)
  8019. +{
  8020. + struct sched_domain *sd;
  8021. + int cpu;
  8022. +
  8023. + cpumask_var_t non_isolated_cpus;
  8024. +
  8025. + alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
  8026. + alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
  8027. +
  8028. + get_online_cpus();
  8029. + mutex_lock(&sched_domains_mutex);
  8030. + init_sched_domains(cpu_active_mask);
  8031. + cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
  8032. + if (cpumask_empty(non_isolated_cpus))
  8033. + cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
  8034. + mutex_unlock(&sched_domains_mutex);
  8035. + put_online_cpus();
  8036. +
  8037. + hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
  8038. + hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
  8039. +
  8040. + /* Move init over to a non-isolated CPU */
  8041. + if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
  8042. + BUG();
  8043. + free_cpumask_var(non_isolated_cpus);
  8044. +
  8045. + grq_lock_irq();
  8046. + /*
  8047. + * Set up the relative cache distance of each online cpu from each
  8048. + * other in a simple array for quick lookup. Locality is determined
  8049. + * by the closest sched_domain that CPUs are separated by. CPUs with
  8050. + * shared cache in SMT and MC are treated as local. Separate CPUs
  8051. + * (within the same package or physically) within the same node are
  8052. + * treated as not local. CPUs not even in the same domain (different
  8053. + * nodes) are treated as very distant.
  8054. + */
  8055. + for_each_online_cpu(cpu) {
  8056. + struct rq *rq = cpu_rq(cpu);
  8057. + for_each_domain(cpu, sd) {
  8058. + int locality, other_cpu;
  8059. +
  8060. +#ifdef CONFIG_SCHED_SMT
  8061. + if (sd->level == SD_LV_SIBLING) {
  8062. + for_each_cpu_mask(other_cpu, *sched_domain_span(sd))
  8063. + cpumask_set_cpu(other_cpu, &rq->smt_siblings);
  8064. + }
  8065. +#endif
  8066. +#ifdef CONFIG_SCHED_MC
  8067. + if (sd->level == SD_LV_MC) {
  8068. + for_each_cpu_mask(other_cpu, *sched_domain_span(sd))
  8069. + cpumask_set_cpu(other_cpu, &rq->cache_siblings);
  8070. + }
  8071. +#endif
  8072. + if (sd->level <= SD_LV_SIBLING)
  8073. + locality = 1;
  8074. + else if (sd->level <= SD_LV_MC)
  8075. + locality = 2;
  8076. + else if (sd->level <= SD_LV_NODE)
  8077. + locality = 3;
  8078. + else
  8079. + continue;
  8080. +
  8081. + for_each_cpu_mask(other_cpu, *sched_domain_span(sd)) {
  8082. + if (locality < rq->cpu_locality[other_cpu])
  8083. + rq->cpu_locality[other_cpu] = locality;
  8084. + }
  8085. + }
  8086. +
  8087. +/*
  8088. + * Each runqueue has its own function in case it doesn't have
  8089. + * siblings of its own allowing mixed topologies.
  8090. + */
  8091. +#ifdef CONFIG_SCHED_SMT
  8092. + if (cpus_weight(rq->smt_siblings) > 1)
  8093. + rq->siblings_idle = siblings_cpu_idle;
  8094. +#endif
  8095. +#ifdef CONFIG_SCHED_MC
  8096. + if (cpus_weight(rq->cache_siblings) > 1)
  8097. + rq->cache_idle = cache_cpu_idle;
  8098. +#endif
  8099. + }
  8100. + grq_unlock_irq();
  8101. +}
  8102. +#else
  8103. +void __init sched_init_smp(void)
  8104. +{
  8105. +}
  8106. +#endif /* CONFIG_SMP */
  8107. +
  8108. +unsigned int sysctl_timer_migration = 1;
  8109. +
  8110. +int in_sched_functions(unsigned long addr)
  8111. +{
  8112. + return in_lock_functions(addr) ||
  8113. + (addr >= (unsigned long)__sched_text_start
  8114. + && addr < (unsigned long)__sched_text_end);
  8115. +}
  8116. +
  8117. +void __init sched_init(void)
  8118. +{
  8119. + int i;
  8120. + struct rq *rq;
  8121. +
  8122. + prio_ratios[0] = 128;
  8123. + for (i = 1 ; i < PRIO_RANGE ; i++)
  8124. + prio_ratios[i] = prio_ratios[i - 1] * 11 / 10;
  8125. +
  8126. + raw_spin_lock_init(&grq.lock);
  8127. + grq.nr_running = grq.nr_uninterruptible = grq.nr_switches = 0;
  8128. + grq.niffies = 0;
  8129. + grq.last_jiffy = jiffies;
  8130. + raw_spin_lock_init(&grq.iso_lock);
  8131. + grq.iso_ticks = 0;
  8132. + grq.iso_refractory = false;
  8133. + grq.noc = 1;
  8134. +#ifdef CONFIG_SMP
  8135. + init_defrootdomain();
  8136. + grq.qnr = grq.idle_cpus = 0;
  8137. + cpumask_clear(&grq.cpu_idle_map);
  8138. +#else
  8139. + uprq = &per_cpu(runqueues, 0);
  8140. +#endif
  8141. + for_each_possible_cpu(i) {
  8142. + rq = cpu_rq(i);
  8143. + rq->user_pc = rq->nice_pc = rq->softirq_pc = rq->system_pc =
  8144. + rq->iowait_pc = rq->idle_pc = 0;
  8145. + rq->dither = false;
  8146. +#ifdef CONFIG_SMP
  8147. + rq->sticky_task = NULL;
  8148. + rq->last_niffy = 0;
  8149. + rq->sd = NULL;
  8150. + rq->rd = NULL;
  8151. + rq->online = false;
  8152. + rq->cpu = i;
  8153. + rq_attach_root(rq, &def_root_domain);
  8154. +#endif
  8155. + atomic_set(&rq->nr_iowait, 0);
  8156. + }
  8157. +
  8158. +#ifdef CONFIG_SMP
  8159. + nr_cpu_ids = i;
  8160. + /*
  8161. + * Set the base locality for cpu cache distance calculation to
  8162. + * "distant" (3). Make sure the distance from a CPU to itself is 0.
  8163. + */
  8164. + for_each_possible_cpu(i) {
  8165. + int j;
  8166. +
  8167. + rq = cpu_rq(i);
  8168. +#ifdef CONFIG_SCHED_SMT
  8169. + cpumask_clear(&rq->smt_siblings);
  8170. + cpumask_set_cpu(i, &rq->smt_siblings);
  8171. + rq->siblings_idle = sole_cpu_idle;
  8172. + cpumask_set_cpu(i, &rq->smt_siblings);
  8173. +#endif
  8174. +#ifdef CONFIG_SCHED_MC
  8175. + cpumask_clear(&rq->cache_siblings);
  8176. + cpumask_set_cpu(i, &rq->cache_siblings);
  8177. + rq->cache_idle = sole_cpu_idle;
  8178. + cpumask_set_cpu(i, &rq->cache_siblings);
  8179. +#endif
  8180. + rq->cpu_locality = kmalloc(nr_cpu_ids * sizeof(int *), GFP_ATOMIC);
  8181. + for_each_possible_cpu(j) {
  8182. + if (i == j)
  8183. + rq->cpu_locality[j] = 0;
  8184. + else
  8185. + rq->cpu_locality[j] = 4;
  8186. + }
  8187. + }
  8188. +#endif
  8189. +
  8190. + for (i = 0; i < PRIO_LIMIT; i++)
  8191. + INIT_LIST_HEAD(grq.queue + i);
  8192. + /* delimiter for bitsearch */
  8193. + __set_bit(PRIO_LIMIT, grq.prio_bitmap);
  8194. +
  8195. +#ifdef CONFIG_PREEMPT_NOTIFIERS
  8196. + INIT_HLIST_HEAD(&init_task.preempt_notifiers);
  8197. +#endif
  8198. +
  8199. +#ifdef CONFIG_RT_MUTEXES
  8200. + plist_head_init(&init_task.pi_waiters);
  8201. +#endif
  8202. +
  8203. + /*
  8204. + * The boot idle thread does lazy MMU switching as well:
  8205. + */
  8206. + atomic_inc(&init_mm.mm_count);
  8207. + enter_lazy_tlb(&init_mm, current);
  8208. +
  8209. + /*
  8210. + * Make us the idle thread. Technically, schedule() should not be
  8211. + * called from this thread, however somewhere below it might be,
  8212. + * but because we are the idle thread, we just pick up running again
  8213. + * when this runqueue becomes "idle".
  8214. + */
  8215. + init_idle(current, smp_processor_id());
  8216. +
  8217. +#ifdef CONFIG_SMP
  8218. + zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
  8219. + /* May be allocated at isolcpus cmdline parse time */
  8220. + if (cpu_isolated_map == NULL)
  8221. + zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
  8222. +#endif /* SMP */
  8223. +}
  8224. +
  8225. +#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  8226. +static inline int preempt_count_equals(int preempt_offset)
  8227. +{
  8228. + int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
  8229. +
  8230. + return (nested == preempt_offset);
  8231. +}
  8232. +
  8233. +void __might_sleep(const char *file, int line, int preempt_offset)
  8234. +{
  8235. + static unsigned long prev_jiffy; /* ratelimiting */
  8236. +
  8237. + rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
  8238. + if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) ||
  8239. + system_state != SYSTEM_RUNNING || oops_in_progress)
  8240. + return;
  8241. + if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  8242. + return;
  8243. + prev_jiffy = jiffies;
  8244. +
  8245. + printk(KERN_ERR
  8246. + "BUG: sleeping function called from invalid context at %s:%d\n",
  8247. + file, line);
  8248. + printk(KERN_ERR
  8249. + "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
  8250. + in_atomic(), irqs_disabled(),
  8251. + current->pid, current->comm);
  8252. +
  8253. + debug_show_held_locks(current);
  8254. + if (irqs_disabled())
  8255. + print_irqtrace_events(current);
  8256. + dump_stack();
  8257. +}
  8258. +EXPORT_SYMBOL(__might_sleep);
  8259. +#endif
  8260. +
  8261. +#ifdef CONFIG_MAGIC_SYSRQ
  8262. +void normalize_rt_tasks(void)
  8263. +{
  8264. + struct task_struct *g, *p;
  8265. + unsigned long flags;
  8266. + struct rq *rq;
  8267. + int queued;
  8268. +
  8269. + read_lock_irq(&tasklist_lock);
  8270. +
  8271. + do_each_thread(g, p) {
  8272. + if (!rt_task(p) && !iso_task(p))
  8273. + continue;
  8274. +
  8275. + raw_spin_lock_irqsave(&p->pi_lock, flags);
  8276. + rq = __task_grq_lock(p);
  8277. +
  8278. + queued = task_queued(p);
  8279. + if (queued)
  8280. + dequeue_task(p);
  8281. + __setscheduler(p, rq, SCHED_NORMAL, 0);
  8282. + if (queued) {
  8283. + enqueue_task(p);
  8284. + try_preempt(p, rq);
  8285. + }
  8286. +
  8287. + __task_grq_unlock();
  8288. + raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  8289. + } while_each_thread(g, p);
  8290. +
  8291. + read_unlock_irq(&tasklist_lock);
  8292. +}
  8293. +#endif /* CONFIG_MAGIC_SYSRQ */
  8294. +
  8295. +#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
  8296. +/*
  8297. + * These functions are only useful for the IA64 MCA handling, or kdb.
  8298. + *
  8299. + * They can only be called when the whole system has been
  8300. + * stopped - every CPU needs to be quiescent, and no scheduling
  8301. + * activity can take place. Using them for anything else would
  8302. + * be a serious bug, and as a result, they aren't even visible
  8303. + * under any other configuration.
  8304. + */
  8305. +
  8306. +/**
  8307. + * curr_task - return the current task for a given cpu.
  8308. + * @cpu: the processor in question.
  8309. + *
  8310. + * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  8311. + */
  8312. +struct task_struct *curr_task(int cpu)
  8313. +{
  8314. + return cpu_curr(cpu);
  8315. +}
  8316. +
  8317. +#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
  8318. +
  8319. +#ifdef CONFIG_IA64
  8320. +/**
  8321. + * set_curr_task - set the current task for a given cpu.
  8322. + * @cpu: the processor in question.
  8323. + * @p: the task pointer to set.
  8324. + *
  8325. + * Description: This function must only be used when non-maskable interrupts
  8326. + * are serviced on a separate stack. It allows the architecture to switch the
  8327. + * notion of the current task on a cpu in a non-blocking manner. This function
  8328. + * must be called with all CPU's synchronised, and interrupts disabled, the
  8329. + * and caller must save the original value of the current task (see
  8330. + * curr_task() above) and restore that value before reenabling interrupts and
  8331. + * re-starting the system.
  8332. + *
  8333. + * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  8334. + */
  8335. +void set_curr_task(int cpu, struct task_struct *p)
  8336. +{
  8337. + cpu_curr(cpu) = p;
  8338. +}
  8339. +
  8340. +#endif
  8341. +
  8342. +/*
  8343. + * Use precise platform statistics if available:
  8344. + */
  8345. +#ifdef CONFIG_VIRT_CPU_ACCOUNTING
  8346. +void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
  8347. +{
  8348. + *ut = p->utime;
  8349. + *st = p->stime;
  8350. +}
  8351. +
  8352. +void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
  8353. +{
  8354. + struct task_cputime cputime;
  8355. +
  8356. + thread_group_cputime(p, &cputime);
  8357. +
  8358. + *ut = cputime.utime;
  8359. + *st = cputime.stime;
  8360. +}
  8361. +#else
  8362. +
  8363. +void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
  8364. +{
  8365. + cputime_t rtime, utime = p->utime, total = utime + p->stime;
  8366. +
  8367. + rtime = nsecs_to_cputime(p->sched_time);
  8368. +
  8369. + if (total) {
  8370. + u64 temp;
  8371. +
  8372. + temp = (u64)(rtime * utime);
  8373. + do_div(temp, total);
  8374. + utime = (cputime_t)temp;
  8375. + } else
  8376. + utime = rtime;
  8377. +
  8378. + /*
  8379. + * Compare with previous values, to keep monotonicity:
  8380. + */
  8381. + p->prev_utime = max(p->prev_utime, utime);
  8382. + p->prev_stime = max(p->prev_stime, (rtime - p->prev_utime));
  8383. +
  8384. + *ut = p->prev_utime;
  8385. + *st = p->prev_stime;
  8386. +}
  8387. +
  8388. +/*
  8389. + * Must be called with siglock held.
  8390. + */
  8391. +void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
  8392. +{
  8393. + struct signal_struct *sig = p->signal;
  8394. + struct task_cputime cputime;
  8395. + cputime_t rtime, utime, total;
  8396. +
  8397. + thread_group_cputime(p, &cputime);
  8398. +
  8399. + total = cputime.utime + cputime.stime;
  8400. + rtime = nsecs_to_cputime(cputime.sum_exec_runtime);
  8401. +
  8402. + if (total) {
  8403. + u64 temp;
  8404. +
  8405. + temp = (u64)(rtime * cputime.utime);
  8406. + do_div(temp, total);
  8407. + utime = (cputime_t)temp;
  8408. + } else
  8409. + utime = rtime;
  8410. +
  8411. + sig->prev_utime = max(sig->prev_utime, utime);
  8412. + sig->prev_stime = max(sig->prev_stime, (rtime - sig->prev_utime));
  8413. +
  8414. + *ut = sig->prev_utime;
  8415. + *st = sig->prev_stime;
  8416. +}
  8417. +#endif
  8418. +
  8419. +inline cputime_t task_gtime(struct task_struct *p)
  8420. +{
  8421. + return p->gtime;
  8422. +}
  8423. +
  8424. +void __cpuinit init_idle_bootup_task(struct task_struct *idle)
  8425. +{}
  8426. +
  8427. +#ifdef CONFIG_SCHED_DEBUG
  8428. +void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  8429. +{}
  8430. +
  8431. +void proc_sched_set_task(struct task_struct *p)
  8432. +{}
  8433. +#endif
  8434. +
  8435. +#ifdef CONFIG_SMP
  8436. +unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
  8437. +{
  8438. + return SCHED_LOAD_SCALE;
  8439. +}
  8440. +
  8441. +unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
  8442. +{
  8443. + unsigned long weight = cpumask_weight(sched_domain_span(sd));
  8444. + unsigned long smt_gain = sd->smt_gain;
  8445. +
  8446. + smt_gain /= weight;
  8447. +
  8448. + return smt_gain;
  8449. +}
  8450. +#endif
  8451. Index: linux-3.4-ck3/kernel/sched/Makefile
  8452. ===================================================================
  8453. --- linux-3.4-ck3.orig/kernel/sched/Makefile 2012-03-20 17:39:43.000000000 +1100
  8454. +++ linux-3.4-ck3/kernel/sched/Makefile 2012-07-03 14:00:08.131680971 +1000
  8455. @@ -11,10 +11,14 @@ ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER
  8456. CFLAGS_core.o := $(PROFILING) -fno-omit-frame-pointer
  8457. endif
  8458.  
  8459. +ifdef CONFIG_SCHED_BFS
  8460. +obj-y += bfs.o clock.o
  8461. +else
  8462. obj-y += core.o clock.o idle_task.o fair.o rt.o stop_task.o
  8463. -obj-$(CONFIG_SMP) += cpupri.o
  8464. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  8465. -obj-$(CONFIG_SCHEDSTATS) += stats.o
  8466. obj-$(CONFIG_SCHED_DEBUG) += debug.o
  8467. +endif
  8468. +obj-$(CONFIG_SMP) += cpupri.o
  8469. +obj-$(CONFIG_SCHEDSTATS) += stats.o
  8470.  
  8471.  
  8472. Index: linux-3.4-ck3/mm/vmscan.c
  8473. ===================================================================
  8474. --- linux-3.4-ck3.orig/mm/vmscan.c 2012-06-01 21:37:23.000000000 +1000
  8475. +++ linux-3.4-ck3/mm/vmscan.c 2012-07-03 14:00:42.607326933 +1000
  8476. @@ -153,7 +153,7 @@ struct mem_cgroup_zone {
  8477. /*
  8478. * From 0 .. 100. Higher means more swappy.
  8479. */
  8480. -int vm_swappiness = 60;
  8481. +int vm_swappiness = 10;
  8482. long vm_total_pages; /* The total number of pages which the VM controls */
  8483.  
  8484. static LIST_HEAD(shrinker_list);
  8485. @@ -999,7 +999,7 @@ cull_mlocked:
  8486.  
  8487. activate_locked:
  8488. /* Not a candidate for swapping, so reclaim swap space. */
  8489. - if (PageSwapCache(page) && vm_swap_full())
  8490. + if (PageSwapCache(page))
  8491. try_to_free_swap(page);
  8492. VM_BUG_ON(PageActive(page));
  8493. SetPageActive(page);
  8494. @@ -2206,6 +2206,35 @@ static inline bool compaction_ready(stru
  8495. }
  8496.  
  8497. /*
  8498. + * Helper functions to adjust nice level of kswapd, based on the priority of
  8499. + * the task (p) that called it. If it is already higher priority we do not
  8500. + * demote its nice level since it is still working on behalf of a higher
  8501. + * priority task. With kernel threads we leave it at nice 0.
  8502. + *
  8503. + * We don't ever run kswapd real time, so if a real time task calls kswapd we
  8504. + * set it to highest SCHED_NORMAL priority.
  8505. + */
  8506. +static inline int effective_sc_prio(struct task_struct *p)
  8507. +{
  8508. + if (likely(p->mm)) {
  8509. + if (rt_task(p))
  8510. + return -20;
  8511. + if (p->policy == SCHED_IDLEPRIO)
  8512. + return 19;
  8513. + return task_nice(p);
  8514. + }
  8515. + return 0;
  8516. +}
  8517. +
  8518. +static void set_kswapd_nice(struct task_struct *kswapd, int active)
  8519. +{
  8520. + long nice = effective_sc_prio(current);
  8521. +
  8522. + if (task_nice(kswapd) > nice || !active)
  8523. + set_user_nice(kswapd, nice);
  8524. +}
  8525. +
  8526. +/*
  8527. * This is the direct reclaim path, for page-allocating processes. We only
  8528. * try to reclaim pages from zones which will satisfy the caller's allocation
  8529. * request.
  8530. @@ -3134,6 +3163,7 @@ static int kswapd(void *p)
  8531. void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
  8532. {
  8533. pg_data_t *pgdat;
  8534. + int active;
  8535.  
  8536. if (!populated_zone(zone))
  8537. return;
  8538. @@ -3145,7 +3175,9 @@ void wakeup_kswapd(struct zone *zone, in
  8539. pgdat->kswapd_max_order = order;
  8540. pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
  8541. }
  8542. - if (!waitqueue_active(&pgdat->kswapd_wait))
  8543. + active = waitqueue_active(&pgdat->kswapd_wait);
  8544. + set_kswapd_nice(pgdat->kswapd, active);
  8545. + if (!active)
  8546. return;
  8547. if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
  8548. return;
  8549. Index: linux-3.4-ck3/include/linux/swap.h
  8550. ===================================================================
  8551. --- linux-3.4-ck3.orig/include/linux/swap.h 2012-06-01 21:37:23.000000000 +1000
  8552. +++ linux-3.4-ck3/include/linux/swap.h 2012-07-03 14:00:42.201319321 +1000
  8553. @@ -204,7 +204,7 @@ struct swap_list_t {
  8554. int next; /* swapfile to be used next */
  8555. };
  8556.  
  8557. -/* Swap 50% full? Release swapcache more aggressively.. */
  8558. +/* Swap 50% full? */
  8559. #define vm_swap_full() (nr_swap_pages*2 < total_swap_pages)
  8560.  
  8561. /* linux/mm/page_alloc.c */
  8562. @@ -361,9 +361,10 @@ extern void grab_swap_token(struct mm_st
  8563. extern void __put_swap_token(struct mm_struct *);
  8564. extern void disable_swap_token(struct mem_cgroup *memcg);
  8565.  
  8566. +/* Only allow swap token to have effect if swap is full */
  8567. static inline int has_swap_token(struct mm_struct *mm)
  8568. {
  8569. - return (mm == swap_token_mm);
  8570. + return (mm == swap_token_mm && vm_swap_full());
  8571. }
  8572.  
  8573. static inline void put_swap_token(struct mm_struct *mm)
  8574. Index: linux-3.4-ck3/mm/memory.c
  8575. ===================================================================
  8576. --- linux-3.4-ck3.orig/mm/memory.c 2012-06-01 21:37:23.000000000 +1000
  8577. +++ linux-3.4-ck3/mm/memory.c 2012-07-03 14:00:42.202319339 +1000
  8578. @@ -3015,7 +3015,7 @@ static int do_swap_page(struct mm_struct
  8579. mem_cgroup_commit_charge_swapin(page, ptr);
  8580.  
  8581. swap_free(entry);
  8582. - if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
  8583. + if ((vma->vm_flags & VM_LOCKED) || PageMlocked(page))
  8584. try_to_free_swap(page);
  8585. unlock_page(page);
  8586. if (swapcache) {
  8587. Index: linux-3.4-ck3/mm/swapfile.c
  8588. ===================================================================
  8589. --- linux-3.4-ck3.orig/mm/swapfile.c 2012-06-01 21:37:23.000000000 +1000
  8590. +++ linux-3.4-ck3/mm/swapfile.c 2012-07-03 14:00:42.202319339 +1000
  8591. @@ -288,7 +288,7 @@ checks:
  8592. scan_base = offset = si->lowest_bit;
  8593.  
  8594. /* reuse swap entry of cache-only swap if not busy. */
  8595. - if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  8596. + if (si->swap_map[offset] == SWAP_HAS_CACHE) {
  8597. int swap_was_freed;
  8598. spin_unlock(&swap_lock);
  8599. swap_was_freed = __try_to_reclaim_swap(si, offset);
  8600. @@ -377,7 +377,7 @@ scan:
  8601. spin_lock(&swap_lock);
  8602. goto checks;
  8603. }
  8604. - if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  8605. + if (si->swap_map[offset] == SWAP_HAS_CACHE) {
  8606. spin_lock(&swap_lock);
  8607. goto checks;
  8608. }
  8609. @@ -392,7 +392,7 @@ scan:
  8610. spin_lock(&swap_lock);
  8611. goto checks;
  8612. }
  8613. - if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  8614. + if (si->swap_map[offset] == SWAP_HAS_CACHE) {
  8615. spin_lock(&swap_lock);
  8616. goto checks;
  8617. }
  8618. @@ -706,8 +706,7 @@ int free_swap_and_cache(swp_entry_t entr
  8619. * Not mapped elsewhere, or swap space full? Free it!
  8620. * Also recheck PageSwapCache now page is locked (above).
  8621. */
  8622. - if (PageSwapCache(page) && !PageWriteback(page) &&
  8623. - (!page_mapped(page) || vm_swap_full())) {
  8624. + if (PageSwapCache(page) && !PageWriteback(page)) {
  8625. delete_from_swap_cache(page);
  8626. SetPageDirty(page);
  8627. }
  8628. Index: linux-3.4-ck3/mm/page-writeback.c
  8629. ===================================================================
  8630. --- linux-3.4-ck3.orig/mm/page-writeback.c 2012-06-01 21:37:23.000000000 +1000
  8631. +++ linux-3.4-ck3/mm/page-writeback.c 2012-07-03 14:00:42.803330605 +1000
  8632. @@ -65,7 +65,7 @@ static long ratelimit_pages = 32;
  8633. /*
  8634. * Start background writeback (via writeback threads) at this percentage
  8635. */
  8636. -int dirty_background_ratio = 10;
  8637. +int dirty_background_ratio = 1;
  8638.  
  8639. /*
  8640. * dirty_background_bytes starts at 0 (disabled) so that it is a function of
  8641. @@ -82,7 +82,7 @@ int vm_highmem_is_dirtyable;
  8642. /*
  8643. * The generator of dirty data starts writeback at this percentage
  8644. */
  8645. -int vm_dirty_ratio = 20;
  8646. +int vm_dirty_ratio = 1;
  8647.  
  8648. /*
  8649. * vm_dirty_bytes starts at 0 (disabled) so that it is a function of
  8650. Index: linux-3.4-ck3/kernel/Kconfig.hz
  8651. ===================================================================
  8652. --- linux-3.4-ck3.orig/kernel/Kconfig.hz 2012-01-05 10:55:44.000000000 +1100
  8653. +++ linux-3.4-ck3/kernel/Kconfig.hz 2012-07-03 14:00:43.537344366 +1000
  8654. @@ -4,7 +4,7 @@
  8655.  
  8656. choice
  8657. prompt "Timer frequency"
  8658. - default HZ_250
  8659. + default HZ_1000
  8660. help
  8661. Allows the configuration of the timer frequency. It is customary
  8662. to have the timer interrupt run at 1000 Hz but 100 Hz may be more
  8663. @@ -23,13 +23,14 @@ choice
  8664. with lots of processors that may show reduced performance if
  8665. too many timer interrupts are occurring.
  8666.  
  8667. - config HZ_250
  8668. + config HZ_250_NODEFAULT
  8669. bool "250 HZ"
  8670. help
  8671. - 250 Hz is a good compromise choice allowing server performance
  8672. - while also showing good interactive responsiveness even
  8673. - on SMP and NUMA systems. If you are going to be using NTSC video
  8674. - or multimedia, selected 300Hz instead.
  8675. + 250 HZ is a lousy compromise choice allowing server interactivity
  8676. + while also showing desktop throughput and no extra power saving on
  8677. + laptops. No good for anything.
  8678. +
  8679. + Recommend 100 or 1000 instead.
  8680.  
  8681. config HZ_300
  8682. bool "300 HZ"
  8683. @@ -43,16 +44,82 @@ choice
  8684. bool "1000 HZ"
  8685. help
  8686. 1000 Hz is the preferred choice for desktop systems and other
  8687. - systems requiring fast interactive responses to events.
  8688. + systems requiring fast interactive responses to events. Laptops
  8689. + can also benefit from this choice without sacrificing battery life
  8690. + if dynticks is also enabled.
  8691. +
  8692. + config HZ_1500
  8693. + bool "1500 HZ"
  8694. + help
  8695. + 1500 Hz is an insane value to use to run broken software that is Hz
  8696. + limited.
  8697. +
  8698. + Being over 1000, driver breakage is likely.
  8699. +
  8700. + config HZ_2000
  8701. + bool "2000 HZ"
  8702. + help
  8703. + 2000 Hz is an insane value to use to run broken software that is Hz
  8704. + limited.
  8705. +
  8706. + Being over 1000, driver breakage is likely.
  8707. +
  8708. + config HZ_3000
  8709. + bool "3000 HZ"
  8710. + help
  8711. + 3000 Hz is an insane value to use to run broken software that is Hz
  8712. + limited.
  8713. +
  8714. + Being over 1000, driver breakage is likely.
  8715. +
  8716. + config HZ_4000
  8717. + bool "4000 HZ"
  8718. + help
  8719. + 4000 Hz is an insane value to use to run broken software that is Hz
  8720. + limited.
  8721. +
  8722. + Being over 1000, driver breakage is likely.
  8723. +
  8724. + config HZ_5000
  8725. + bool "5000 HZ"
  8726. + help
  8727. + 5000 Hz is an obscene value to use to run broken software that is Hz
  8728. + limited.
  8729. +
  8730. + Being over 1000, driver breakage is likely.
  8731. +
  8732. + config HZ_7500
  8733. + bool "7500 HZ"
  8734. + help
  8735. + 7500 Hz is an obscene value to use to run broken software that is Hz
  8736. + limited.
  8737. +
  8738. + Being over 1000, driver breakage is likely.
  8739. +
  8740. + config HZ_10000
  8741. + bool "10000 HZ"
  8742. + help
  8743. + 10000 Hz is an obscene value to use to run broken software that is Hz
  8744. + limited.
  8745. +
  8746. + Being over 1000, driver breakage is likely.
  8747. +
  8748.  
  8749. endchoice
  8750.  
  8751. config HZ
  8752. int
  8753. default 100 if HZ_100
  8754. - default 250 if HZ_250
  8755. + default 250 if HZ_250_NODEFAULT
  8756. default 300 if HZ_300
  8757. default 1000 if HZ_1000
  8758. + default 1500 if HZ_1500
  8759. + default 2000 if HZ_2000
  8760. + default 3000 if HZ_3000
  8761. + default 4000 if HZ_4000
  8762. + default 5000 if HZ_5000
  8763. + default 7500 if HZ_7500
  8764. + default 10000 if HZ_10000
  8765.  
  8766. config SCHED_HRTICK
  8767. def_bool HIGH_RES_TIMERS && (!SMP || USE_GENERIC_SMP_HELPERS)
  8768. Index: linux-3.4-ck3/arch/x86/kernel/cpu/proc.c
  8769. ===================================================================
  8770. --- linux-3.4-ck3.orig/arch/x86/kernel/cpu/proc.c 2012-03-20 17:39:41.000000000 +1100
  8771. +++ linux-3.4-ck3/arch/x86/kernel/cpu/proc.c 2012-07-03 14:00:43.536344348 +1000
  8772. @@ -109,7 +109,7 @@ static int show_cpuinfo(struct seq_file
  8773.  
  8774. seq_printf(m, "\nbogomips\t: %lu.%02lu\n",
  8775. c->loops_per_jiffy/(500000/HZ),
  8776. - (c->loops_per_jiffy/(5000/HZ)) % 100);
  8777. + (c->loops_per_jiffy * 10 /(50000/HZ)) % 100);
  8778.  
  8779. #ifdef CONFIG_X86_64
  8780. if (c->x86_tlbsize > 0)
  8781. Index: linux-3.4-ck3/arch/x86/kernel/smpboot.c
  8782. ===================================================================
  8783. --- linux-3.4-ck3.orig/arch/x86/kernel/smpboot.c 2012-06-01 21:37:21.000000000 +1000
  8784. +++ linux-3.4-ck3/arch/x86/kernel/smpboot.c 2012-07-03 14:00:43.536344348 +1000
  8785. @@ -420,7 +420,7 @@ static void impress_friends(void)
  8786. "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  8787. num_online_cpus(),
  8788. bogosum/(500000/HZ),
  8789. - (bogosum/(5000/HZ))%100);
  8790. + (bogosum * 10/(50000/HZ))%100);
  8791.  
  8792. pr_debug("Before bogocount - setting activated=1.\n");
  8793. }
  8794. Index: linux-3.4-ck3/include/linux/nfsd/stats.h
  8795. ===================================================================
  8796. --- linux-3.4-ck3.orig/include/linux/nfsd/stats.h 2012-01-05 10:55:44.000000000 +1100
  8797. +++ linux-3.4-ck3/include/linux/nfsd/stats.h 2012-07-03 14:00:43.537344366 +1000
  8798. @@ -11,8 +11,8 @@
  8799.  
  8800. #include <linux/nfs4.h>
  8801.  
  8802. -/* thread usage wraps very million seconds (approx one fortnight) */
  8803. -#define NFSD_USAGE_WRAP (HZ*1000000)
  8804. +/* thread usage wraps every one hundred thousand seconds (approx one day) */
  8805. +#define NFSD_USAGE_WRAP (HZ*100000)
  8806.  
  8807. #ifdef __KERNEL__
  8808.  
  8809. Index: linux-3.4-ck3/include/net/inet_timewait_sock.h
  8810. ===================================================================
  8811. --- linux-3.4-ck3.orig/include/net/inet_timewait_sock.h 2012-03-20 17:39:43.000000000 +1100
  8812. +++ linux-3.4-ck3/include/net/inet_timewait_sock.h 2012-07-03 14:00:43.537344366 +1000
  8813. @@ -38,8 +38,8 @@ struct inet_hashinfo;
  8814. * If time > 4sec, it is "slow" path, no recycling is required,
  8815. * so that we select tick to get range about 4 seconds.
  8816. */
  8817. -#if HZ <= 16 || HZ > 4096
  8818. -# error Unsupported: HZ <= 16 or HZ > 4096
  8819. +#if HZ <= 16 || HZ > 16384
  8820. +# error Unsupported: HZ <= 16 or HZ > 16384
  8821. #elif HZ <= 32
  8822. # define INET_TWDR_RECYCLE_TICK (5 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  8823. #elif HZ <= 64
  8824. @@ -54,8 +54,12 @@ struct inet_hashinfo;
  8825. # define INET_TWDR_RECYCLE_TICK (10 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  8826. #elif HZ <= 2048
  8827. # define INET_TWDR_RECYCLE_TICK (11 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  8828. -#else
  8829. +#elif HZ <= 4096
  8830. # define INET_TWDR_RECYCLE_TICK (12 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  8831. +#elif HZ <= 8192
  8832. +# define INET_TWDR_RECYCLE_TICK (13 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  8833. +#else
  8834. +# define INET_TWDR_RECYCLE_TICK (14 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  8835. #endif
  8836.  
  8837. /* TIME_WAIT reaping mechanism. */
  8838. Index: linux-3.4-ck3/init/calibrate.c
  8839. ===================================================================
  8840. --- linux-3.4-ck3.orig/init/calibrate.c 2012-06-01 21:37:23.000000000 +1000
  8841. +++ linux-3.4-ck3/init/calibrate.c 2012-07-03 14:00:43.537344366 +1000
  8842. @@ -294,7 +294,7 @@ void __cpuinit calibrate_delay(void)
  8843. if (!printed)
  8844. pr_cont("%lu.%02lu BogoMIPS (lpj=%lu)\n",
  8845. lpj/(500000/HZ),
  8846. - (lpj/(5000/HZ)) % 100, lpj);
  8847. + (lpj * 10 /(50000 / HZ)) % 100, lpj);
  8848.  
  8849. loops_per_jiffy = lpj;
  8850. printed = true;
  8851. Index: linux-3.4-ck3/kernel/Kconfig.preempt
  8852. ===================================================================
  8853. --- linux-3.4-ck3.orig/kernel/Kconfig.preempt 2012-06-01 21:37:23.000000000 +1000
  8854. +++ linux-3.4-ck3/kernel/Kconfig.preempt 2012-07-03 14:00:43.782348961 +1000
  8855. @@ -1,7 +1,7 @@
  8856.  
  8857. choice
  8858. prompt "Preemption Model"
  8859. - default PREEMPT_NONE
  8860. + default PREEMPT
  8861.  
  8862. config PREEMPT_NONE
  8863. bool "No Forced Preemption (Server)"
  8864. @@ -17,7 +17,7 @@ config PREEMPT_NONE
  8865. latencies.
  8866.  
  8867. config PREEMPT_VOLUNTARY
  8868. - bool "Voluntary Kernel Preemption (Desktop)"
  8869. + bool "Voluntary Kernel Preemption (Nothing)"
  8870. help
  8871. This option reduces the latency of the kernel by adding more
  8872. "explicit preemption points" to the kernel code. These new
  8873. @@ -31,7 +31,8 @@ config PREEMPT_VOLUNTARY
  8874. applications to run more 'smoothly' even when the system is
  8875. under load.
  8876.  
  8877. - Select this if you are building a kernel for a desktop system.
  8878. + Select this for no system in particular (choose Preemptible
  8879. + instead on a desktop if you know what's good for you).
  8880.  
  8881. config PREEMPT
  8882. bool "Preemptible Kernel (Low-Latency Desktop)"
  8883. Index: linux-3.4-ck3/Makefile
  8884. ===================================================================
  8885. --- linux-3.4-ck3.orig/Makefile 2012-06-01 21:37:20.000000000 +1000
  8886. +++ linux-3.4-ck3/Makefile 2012-07-03 14:00:55.681572079 +1000
  8887. @@ -10,6 +10,10 @@ NAME = Saber-toothed Squirrel
  8888. # Comments in this file are targeted only to the developer, do not
  8889. # expect to learn how to build the kernel reading this file.
  8890.  
  8891. +CKVERSION = -ck3
  8892. +CKNAME = BFS Powered
  8893. +EXTRAVERSION := $(EXTRAVERSION)$(CKVERSION)
  8894. +
  8895. # Do not:
  8896. # o use make's built-in rules and variables
  8897. # (this increases performance and avoids hard-to-debug behaviour);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement