posix-cpu-timers.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /*
  2. * Implement CPU time clocks for the POSIX clock interface.
  3. */
  4. #include <linux/sched.h>
  5. #include <linux/posix-timers.h>
  6. #include <linux/errno.h>
  7. #include <linux/math64.h>
  8. #include <asm/uaccess.h>
  9. static int check_clock(const clockid_t which_clock)
  10. {
  11. int error = 0;
  12. struct task_struct *p;
  13. const pid_t pid = CPUCLOCK_PID(which_clock);
  14. if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
  15. return -EINVAL;
  16. if (pid == 0)
  17. return 0;
  18. read_lock(&tasklist_lock);
  19. p = find_task_by_vpid(pid);
  20. if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
  21. same_thread_group(p, current) : thread_group_leader(p))) {
  22. error = -EINVAL;
  23. }
  24. read_unlock(&tasklist_lock);
  25. return error;
  26. }
  27. static inline union cpu_time_count
  28. timespec_to_sample(const clockid_t which_clock, const struct timespec *tp)
  29. {
  30. union cpu_time_count ret;
  31. ret.sched = 0; /* high half always zero when .cpu used */
  32. if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  33. ret.sched = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
  34. } else {
  35. ret.cpu = timespec_to_cputime(tp);
  36. }
  37. return ret;
  38. }
  39. static void sample_to_timespec(const clockid_t which_clock,
  40. union cpu_time_count cpu,
  41. struct timespec *tp)
  42. {
  43. if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED)
  44. *tp = ns_to_timespec(cpu.sched);
  45. else
  46. cputime_to_timespec(cpu.cpu, tp);
  47. }
  48. static inline int cpu_time_before(const clockid_t which_clock,
  49. union cpu_time_count now,
  50. union cpu_time_count then)
  51. {
  52. if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  53. return now.sched < then.sched;
  54. } else {
  55. return cputime_lt(now.cpu, then.cpu);
  56. }
  57. }
  58. static inline void cpu_time_add(const clockid_t which_clock,
  59. union cpu_time_count *acc,
  60. union cpu_time_count val)
  61. {
  62. if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  63. acc->sched += val.sched;
  64. } else {
  65. acc->cpu = cputime_add(acc->cpu, val.cpu);
  66. }
  67. }
  68. static inline union cpu_time_count cpu_time_sub(const clockid_t which_clock,
  69. union cpu_time_count a,
  70. union cpu_time_count b)
  71. {
  72. if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  73. a.sched -= b.sched;
  74. } else {
  75. a.cpu = cputime_sub(a.cpu, b.cpu);
  76. }
  77. return a;
  78. }
  79. /*
  80. * Divide and limit the result to res >= 1
  81. *
  82. * This is necessary to prevent signal delivery starvation, when the result of
  83. * the division would be rounded down to 0.
  84. */
  85. static inline cputime_t cputime_div_non_zero(cputime_t time, unsigned long div)
  86. {
  87. cputime_t res = cputime_div(time, div);
  88. return max_t(cputime_t, res, 1);
  89. }
  90. /*
  91. * Update expiry time from increment, and increase overrun count,
  92. * given the current clock sample.
  93. */
  94. static void bump_cpu_timer(struct k_itimer *timer,
  95. union cpu_time_count now)
  96. {
  97. int i;
  98. if (timer->it.cpu.incr.sched == 0)
  99. return;
  100. if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
  101. unsigned long long delta, incr;
  102. if (now.sched < timer->it.cpu.expires.sched)
  103. return;
  104. incr = timer->it.cpu.incr.sched;
  105. delta = now.sched + incr - timer->it.cpu.expires.sched;
  106. /* Don't use (incr*2 < delta), incr*2 might overflow. */
  107. for (i = 0; incr < delta - incr; i++)
  108. incr = incr << 1;
  109. for (; i >= 0; incr >>= 1, i--) {
  110. if (delta < incr)
  111. continue;
  112. timer->it.cpu.expires.sched += incr;
  113. timer->it_overrun += 1 << i;
  114. delta -= incr;
  115. }
  116. } else {
  117. cputime_t delta, incr;
  118. if (cputime_lt(now.cpu, timer->it.cpu.expires.cpu))
  119. return;
  120. incr = timer->it.cpu.incr.cpu;
  121. delta = cputime_sub(cputime_add(now.cpu, incr),
  122. timer->it.cpu.expires.cpu);
  123. /* Don't use (incr*2 < delta), incr*2 might overflow. */
  124. for (i = 0; cputime_lt(incr, cputime_sub(delta, incr)); i++)
  125. incr = cputime_add(incr, incr);
  126. for (; i >= 0; incr = cputime_halve(incr), i--) {
  127. if (cputime_lt(delta, incr))
  128. continue;
  129. timer->it.cpu.expires.cpu =
  130. cputime_add(timer->it.cpu.expires.cpu, incr);
  131. timer->it_overrun += 1 << i;
  132. delta = cputime_sub(delta, incr);
  133. }
  134. }
  135. }
  136. static inline cputime_t prof_ticks(struct task_struct *p)
  137. {
  138. return cputime_add(p->utime, p->stime);
  139. }
  140. static inline cputime_t virt_ticks(struct task_struct *p)
  141. {
  142. return p->utime;
  143. }
  144. static inline unsigned long long sched_ns(struct task_struct *p)
  145. {
  146. return task_sched_runtime(p);
  147. }
  148. int posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
  149. {
  150. int error = check_clock(which_clock);
  151. if (!error) {
  152. tp->tv_sec = 0;
  153. tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
  154. if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  155. /*
  156. * If sched_clock is using a cycle counter, we
  157. * don't have any idea of its true resolution
  158. * exported, but it is much more than 1s/HZ.
  159. */
  160. tp->tv_nsec = 1;
  161. }
  162. }
  163. return error;
  164. }
  165. int posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
  166. {
  167. /*
  168. * You can never reset a CPU clock, but we check for other errors
  169. * in the call before failing with EPERM.
  170. */
  171. int error = check_clock(which_clock);
  172. if (error == 0) {
  173. error = -EPERM;
  174. }
  175. return error;
  176. }
  177. /*
  178. * Sample a per-thread clock for the given task.
  179. */
  180. static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
  181. union cpu_time_count *cpu)
  182. {
  183. switch (CPUCLOCK_WHICH(which_clock)) {
  184. default:
  185. return -EINVAL;
  186. case CPUCLOCK_PROF:
  187. cpu->cpu = prof_ticks(p);
  188. break;
  189. case CPUCLOCK_VIRT:
  190. cpu->cpu = virt_ticks(p);
  191. break;
  192. case CPUCLOCK_SCHED:
  193. cpu->sched = sched_ns(p);
  194. break;
  195. }
  196. return 0;
  197. }
  198. /*
  199. * Sample a process (thread group) clock for the given group_leader task.
  200. * Must be called with tasklist_lock held for reading.
  201. * Must be called with tasklist_lock held for reading, and p->sighand->siglock.
  202. */
  203. static int cpu_clock_sample_group_locked(unsigned int clock_idx,
  204. struct task_struct *p,
  205. union cpu_time_count *cpu)
  206. {
  207. struct task_struct *t = p;
  208. switch (clock_idx) {
  209. default:
  210. return -EINVAL;
  211. case CPUCLOCK_PROF:
  212. cpu->cpu = cputime_add(p->signal->utime, p->signal->stime);
  213. do {
  214. cpu->cpu = cputime_add(cpu->cpu, prof_ticks(t));
  215. t = next_thread(t);
  216. } while (t != p);
  217. break;
  218. case CPUCLOCK_VIRT:
  219. cpu->cpu = p->signal->utime;
  220. do {
  221. cpu->cpu = cputime_add(cpu->cpu, virt_ticks(t));
  222. t = next_thread(t);
  223. } while (t != p);
  224. break;
  225. case CPUCLOCK_SCHED:
  226. cpu->sched = p->signal->sum_sched_runtime;
  227. /* Add in each other live thread. */
  228. while ((t = next_thread(t)) != p) {
  229. cpu->sched += t->se.sum_exec_runtime;
  230. }
  231. cpu->sched += sched_ns(p);
  232. break;
  233. }
  234. return 0;
  235. }
  236. /*
  237. * Sample a process (thread group) clock for the given group_leader task.
  238. * Must be called with tasklist_lock held for reading.
  239. */
  240. static int cpu_clock_sample_group(const clockid_t which_clock,
  241. struct task_struct *p,
  242. union cpu_time_count *cpu)
  243. {
  244. int ret;
  245. unsigned long flags;
  246. spin_lock_irqsave(&p->sighand->siglock, flags);
  247. ret = cpu_clock_sample_group_locked(CPUCLOCK_WHICH(which_clock), p,
  248. cpu);
  249. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  250. return ret;
  251. }
  252. int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
  253. {
  254. const pid_t pid = CPUCLOCK_PID(which_clock);
  255. int error = -EINVAL;
  256. union cpu_time_count rtn;
  257. if (pid == 0) {
  258. /*
  259. * Special case constant value for our own clocks.
  260. * We don't have to do any lookup to find ourselves.
  261. */
  262. if (CPUCLOCK_PERTHREAD(which_clock)) {
  263. /*
  264. * Sampling just ourselves we can do with no locking.
  265. */
  266. error = cpu_clock_sample(which_clock,
  267. current, &rtn);
  268. } else {
  269. read_lock(&tasklist_lock);
  270. error = cpu_clock_sample_group(which_clock,
  271. current, &rtn);
  272. read_unlock(&tasklist_lock);
  273. }
  274. } else {
  275. /*
  276. * Find the given PID, and validate that the caller
  277. * should be able to see it.
  278. */
  279. struct task_struct *p;
  280. rcu_read_lock();
  281. p = find_task_by_vpid(pid);
  282. if (p) {
  283. if (CPUCLOCK_PERTHREAD(which_clock)) {
  284. if (same_thread_group(p, current)) {
  285. error = cpu_clock_sample(which_clock,
  286. p, &rtn);
  287. }
  288. } else {
  289. read_lock(&tasklist_lock);
  290. if (thread_group_leader(p) && p->signal) {
  291. error =
  292. cpu_clock_sample_group(which_clock,
  293. p, &rtn);
  294. }
  295. read_unlock(&tasklist_lock);
  296. }
  297. }
  298. rcu_read_unlock();
  299. }
  300. if (error)
  301. return error;
  302. sample_to_timespec(which_clock, rtn, tp);
  303. return 0;
  304. }
  305. /*
  306. * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
  307. * This is called from sys_timer_create with the new timer already locked.
  308. */
  309. int posix_cpu_timer_create(struct k_itimer *new_timer)
  310. {
  311. int ret = 0;
  312. const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
  313. struct task_struct *p;
  314. if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
  315. return -EINVAL;
  316. INIT_LIST_HEAD(&new_timer->it.cpu.entry);
  317. new_timer->it.cpu.incr.sched = 0;
  318. new_timer->it.cpu.expires.sched = 0;
  319. read_lock(&tasklist_lock);
  320. if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
  321. if (pid == 0) {
  322. p = current;
  323. } else {
  324. p = find_task_by_vpid(pid);
  325. if (p && !same_thread_group(p, current))
  326. p = NULL;
  327. }
  328. } else {
  329. if (pid == 0) {
  330. p = current->group_leader;
  331. } else {
  332. p = find_task_by_vpid(pid);
  333. if (p && !thread_group_leader(p))
  334. p = NULL;
  335. }
  336. }
  337. new_timer->it.cpu.task = p;
  338. if (p) {
  339. get_task_struct(p);
  340. } else {
  341. ret = -EINVAL;
  342. }
  343. read_unlock(&tasklist_lock);
  344. return ret;
  345. }
  346. /*
  347. * Clean up a CPU-clock timer that is about to be destroyed.
  348. * This is called from timer deletion with the timer already locked.
  349. * If we return TIMER_RETRY, it's necessary to release the timer's lock
  350. * and try again. (This happens when the timer is in the middle of firing.)
  351. */
  352. int posix_cpu_timer_del(struct k_itimer *timer)
  353. {
  354. struct task_struct *p = timer->it.cpu.task;
  355. int ret = 0;
  356. if (likely(p != NULL)) {
  357. read_lock(&tasklist_lock);
  358. if (unlikely(p->signal == NULL)) {
  359. /*
  360. * We raced with the reaping of the task.
  361. * The deletion should have cleared us off the list.
  362. */
  363. BUG_ON(!list_empty(&timer->it.cpu.entry));
  364. } else {
  365. spin_lock(&p->sighand->siglock);
  366. if (timer->it.cpu.firing)
  367. ret = TIMER_RETRY;
  368. else
  369. list_del(&timer->it.cpu.entry);
  370. spin_unlock(&p->sighand->siglock);
  371. }
  372. read_unlock(&tasklist_lock);
  373. if (!ret)
  374. put_task_struct(p);
  375. }
  376. return ret;
  377. }
  378. /*
  379. * Clean out CPU timers still ticking when a thread exited. The task
  380. * pointer is cleared, and the expiry time is replaced with the residual
  381. * time for later timer_gettime calls to return.
  382. * This must be called with the siglock held.
  383. */
  384. static void cleanup_timers(struct list_head *head,
  385. cputime_t utime, cputime_t stime,
  386. unsigned long long sum_exec_runtime)
  387. {
  388. struct cpu_timer_list *timer, *next;
  389. cputime_t ptime = cputime_add(utime, stime);
  390. list_for_each_entry_safe(timer, next, head, entry) {
  391. list_del_init(&timer->entry);
  392. if (cputime_lt(timer->expires.cpu, ptime)) {
  393. timer->expires.cpu = cputime_zero;
  394. } else {
  395. timer->expires.cpu = cputime_sub(timer->expires.cpu,
  396. ptime);
  397. }
  398. }
  399. ++head;
  400. list_for_each_entry_safe(timer, next, head, entry) {
  401. list_del_init(&timer->entry);
  402. if (cputime_lt(timer->expires.cpu, utime)) {
  403. timer->expires.cpu = cputime_zero;
  404. } else {
  405. timer->expires.cpu = cputime_sub(timer->expires.cpu,
  406. utime);
  407. }
  408. }
  409. ++head;
  410. list_for_each_entry_safe(timer, next, head, entry) {
  411. list_del_init(&timer->entry);
  412. if (timer->expires.sched < sum_exec_runtime) {
  413. timer->expires.sched = 0;
  414. } else {
  415. timer->expires.sched -= sum_exec_runtime;
  416. }
  417. }
  418. }
  419. /*
  420. * These are both called with the siglock held, when the current thread
  421. * is being reaped. When the final (leader) thread in the group is reaped,
  422. * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
  423. */
  424. void posix_cpu_timers_exit(struct task_struct *tsk)
  425. {
  426. cleanup_timers(tsk->cpu_timers,
  427. tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
  428. }
  429. void posix_cpu_timers_exit_group(struct task_struct *tsk)
  430. {
  431. cleanup_timers(tsk->signal->cpu_timers,
  432. cputime_add(tsk->utime, tsk->signal->utime),
  433. cputime_add(tsk->stime, tsk->signal->stime),
  434. tsk->se.sum_exec_runtime + tsk->signal->sum_sched_runtime);
  435. }
  436. /*
  437. * Set the expiry times of all the threads in the process so one of them
  438. * will go off before the process cumulative expiry total is reached.
  439. */
  440. static void process_timer_rebalance(struct task_struct *p,
  441. unsigned int clock_idx,
  442. union cpu_time_count expires,
  443. union cpu_time_count val)
  444. {
  445. cputime_t ticks, left;
  446. unsigned long long ns, nsleft;
  447. struct task_struct *t = p;
  448. unsigned int nthreads = atomic_read(&p->signal->live);
  449. if (!nthreads)
  450. return;
  451. switch (clock_idx) {
  452. default:
  453. BUG();
  454. break;
  455. case CPUCLOCK_PROF:
  456. left = cputime_div_non_zero(cputime_sub(expires.cpu, val.cpu),
  457. nthreads);
  458. do {
  459. if (likely(!(t->flags & PF_EXITING))) {
  460. ticks = cputime_add(prof_ticks(t), left);
  461. if (cputime_eq(t->it_prof_expires,
  462. cputime_zero) ||
  463. cputime_gt(t->it_prof_expires, ticks)) {
  464. t->it_prof_expires = ticks;
  465. }
  466. }
  467. t = next_thread(t);
  468. } while (t != p);
  469. break;
  470. case CPUCLOCK_VIRT:
  471. left = cputime_div_non_zero(cputime_sub(expires.cpu, val.cpu),
  472. nthreads);
  473. do {
  474. if (likely(!(t->flags & PF_EXITING))) {
  475. ticks = cputime_add(virt_ticks(t), left);
  476. if (cputime_eq(t->it_virt_expires,
  477. cputime_zero) ||
  478. cputime_gt(t->it_virt_expires, ticks)) {
  479. t->it_virt_expires = ticks;
  480. }
  481. }
  482. t = next_thread(t);
  483. } while (t != p);
  484. break;
  485. case CPUCLOCK_SCHED:
  486. nsleft = expires.sched - val.sched;
  487. do_div(nsleft, nthreads);
  488. nsleft = max_t(unsigned long long, nsleft, 1);
  489. do {
  490. if (likely(!(t->flags & PF_EXITING))) {
  491. ns = t->se.sum_exec_runtime + nsleft;
  492. if (t->it_sched_expires == 0 ||
  493. t->it_sched_expires > ns) {
  494. t->it_sched_expires = ns;
  495. }
  496. }
  497. t = next_thread(t);
  498. } while (t != p);
  499. break;
  500. }
  501. }
  502. static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
  503. {
  504. /*
  505. * That's all for this thread or process.
  506. * We leave our residual in expires to be reported.
  507. */
  508. put_task_struct(timer->it.cpu.task);
  509. timer->it.cpu.task = NULL;
  510. timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
  511. timer->it.cpu.expires,
  512. now);
  513. }
  514. /*
  515. * Insert the timer on the appropriate list before any timers that
  516. * expire later. This must be called with the tasklist_lock held
  517. * for reading, and interrupts disabled.
  518. */
  519. static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
  520. {
  521. struct task_struct *p = timer->it.cpu.task;
  522. struct list_head *head, *listpos;
  523. struct cpu_timer_list *const nt = &timer->it.cpu;
  524. struct cpu_timer_list *next;
  525. unsigned long i;
  526. head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
  527. p->cpu_timers : p->signal->cpu_timers);
  528. head += CPUCLOCK_WHICH(timer->it_clock);
  529. BUG_ON(!irqs_disabled());
  530. spin_lock(&p->sighand->siglock);
  531. listpos = head;
  532. if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
  533. list_for_each_entry(next, head, entry) {
  534. if (next->expires.sched > nt->expires.sched)
  535. break;
  536. listpos = &next->entry;
  537. }
  538. } else {
  539. list_for_each_entry(next, head, entry) {
  540. if (cputime_gt(next->expires.cpu, nt->expires.cpu))
  541. break;
  542. listpos = &next->entry;
  543. }
  544. }
  545. list_add(&nt->entry, listpos);
  546. if (listpos == head) {
  547. /*
  548. * We are the new earliest-expiring timer.
  549. * If we are a thread timer, there can always
  550. * be a process timer telling us to stop earlier.
  551. */
  552. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  553. switch (CPUCLOCK_WHICH(timer->it_clock)) {
  554. default:
  555. BUG();
  556. case CPUCLOCK_PROF:
  557. if (cputime_eq(p->it_prof_expires,
  558. cputime_zero) ||
  559. cputime_gt(p->it_prof_expires,
  560. nt->expires.cpu))
  561. p->it_prof_expires = nt->expires.cpu;
  562. break;
  563. case CPUCLOCK_VIRT:
  564. if (cputime_eq(p->it_virt_expires,
  565. cputime_zero) ||
  566. cputime_gt(p->it_virt_expires,
  567. nt->expires.cpu))
  568. p->it_virt_expires = nt->expires.cpu;
  569. break;
  570. case CPUCLOCK_SCHED:
  571. if (p->it_sched_expires == 0 ||
  572. p->it_sched_expires > nt->expires.sched)
  573. p->it_sched_expires = nt->expires.sched;
  574. break;
  575. }
  576. } else {
  577. /*
  578. * For a process timer, we must balance
  579. * all the live threads' expirations.
  580. */
  581. switch (CPUCLOCK_WHICH(timer->it_clock)) {
  582. default:
  583. BUG();
  584. case CPUCLOCK_VIRT:
  585. if (!cputime_eq(p->signal->it_virt_expires,
  586. cputime_zero) &&
  587. cputime_lt(p->signal->it_virt_expires,
  588. timer->it.cpu.expires.cpu))
  589. break;
  590. goto rebalance;
  591. case CPUCLOCK_PROF:
  592. if (!cputime_eq(p->signal->it_prof_expires,
  593. cputime_zero) &&
  594. cputime_lt(p->signal->it_prof_expires,
  595. timer->it.cpu.expires.cpu))
  596. break;
  597. i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
  598. if (i != RLIM_INFINITY &&
  599. i <= cputime_to_secs(timer->it.cpu.expires.cpu))
  600. break;
  601. goto rebalance;
  602. case CPUCLOCK_SCHED:
  603. rebalance:
  604. process_timer_rebalance(
  605. timer->it.cpu.task,
  606. CPUCLOCK_WHICH(timer->it_clock),
  607. timer->it.cpu.expires, now);
  608. break;
  609. }
  610. }
  611. }
  612. spin_unlock(&p->sighand->siglock);
  613. }
  614. /*
  615. * The timer is locked, fire it and arrange for its reload.
  616. */
  617. static void cpu_timer_fire(struct k_itimer *timer)
  618. {
  619. if (unlikely(timer->sigq == NULL)) {
  620. /*
  621. * This a special case for clock_nanosleep,
  622. * not a normal timer from sys_timer_create.
  623. */
  624. wake_up_process(timer->it_process);
  625. timer->it.cpu.expires.sched = 0;
  626. } else if (timer->it.cpu.incr.sched == 0) {
  627. /*
  628. * One-shot timer. Clear it as soon as it's fired.
  629. */
  630. posix_timer_event(timer, 0);
  631. timer->it.cpu.expires.sched = 0;
  632. } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
  633. /*
  634. * The signal did not get queued because the signal
  635. * was ignored, so we won't get any callback to
  636. * reload the timer. But we need to keep it
  637. * ticking in case the signal is deliverable next time.
  638. */
  639. posix_cpu_timer_schedule(timer);
  640. }
  641. }
  642. /*
  643. * Guts of sys_timer_settime for CPU timers.
  644. * This is called with the timer locked and interrupts disabled.
  645. * If we return TIMER_RETRY, it's necessary to release the timer's lock
  646. * and try again. (This happens when the timer is in the middle of firing.)
  647. */
  648. int posix_cpu_timer_set(struct k_itimer *timer, int flags,
  649. struct itimerspec *new, struct itimerspec *old)
  650. {
  651. struct task_struct *p = timer->it.cpu.task;
  652. union cpu_time_count old_expires, new_expires, val;
  653. int ret;
  654. if (unlikely(p == NULL)) {
  655. /*
  656. * Timer refers to a dead task's clock.
  657. */
  658. return -ESRCH;
  659. }
  660. new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
  661. read_lock(&tasklist_lock);
  662. /*
  663. * We need the tasklist_lock to protect against reaping that
  664. * clears p->signal. If p has just been reaped, we can no
  665. * longer get any information about it at all.
  666. */
  667. if (unlikely(p->signal == NULL)) {
  668. read_unlock(&tasklist_lock);
  669. put_task_struct(p);
  670. timer->it.cpu.task = NULL;
  671. return -ESRCH;
  672. }
  673. /*
  674. * Disarm any old timer after extracting its expiry time.
  675. */
  676. BUG_ON(!irqs_disabled());
  677. ret = 0;
  678. spin_lock(&p->sighand->siglock);
  679. old_expires = timer->it.cpu.expires;
  680. if (unlikely(timer->it.cpu.firing)) {
  681. timer->it.cpu.firing = -1;
  682. ret = TIMER_RETRY;
  683. } else
  684. list_del_init(&timer->it.cpu.entry);
  685. spin_unlock(&p->sighand->siglock);
  686. /*
  687. * We need to sample the current value to convert the new
  688. * value from to relative and absolute, and to convert the
  689. * old value from absolute to relative. To set a process
  690. * timer, we need a sample to balance the thread expiry
  691. * times (in arm_timer). With an absolute time, we must
  692. * check if it's already passed. In short, we need a sample.
  693. */
  694. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  695. cpu_clock_sample(timer->it_clock, p, &val);
  696. } else {
  697. cpu_clock_sample_group(timer->it_clock, p, &val);
  698. }
  699. if (old) {
  700. if (old_expires.sched == 0) {
  701. old->it_value.tv_sec = 0;
  702. old->it_value.tv_nsec = 0;
  703. } else {
  704. /*
  705. * Update the timer in case it has
  706. * overrun already. If it has,
  707. * we'll report it as having overrun
  708. * and with the next reloaded timer
  709. * already ticking, though we are
  710. * swallowing that pending
  711. * notification here to install the
  712. * new setting.
  713. */
  714. bump_cpu_timer(timer, val);
  715. if (cpu_time_before(timer->it_clock, val,
  716. timer->it.cpu.expires)) {
  717. old_expires = cpu_time_sub(
  718. timer->it_clock,
  719. timer->it.cpu.expires, val);
  720. sample_to_timespec(timer->it_clock,
  721. old_expires,
  722. &old->it_value);
  723. } else {
  724. old->it_value.tv_nsec = 1;
  725. old->it_value.tv_sec = 0;
  726. }
  727. }
  728. }
  729. if (unlikely(ret)) {
  730. /*
  731. * We are colliding with the timer actually firing.
  732. * Punt after filling in the timer's old value, and
  733. * disable this firing since we are already reporting
  734. * it as an overrun (thanks to bump_cpu_timer above).
  735. */
  736. read_unlock(&tasklist_lock);
  737. goto out;
  738. }
  739. if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
  740. cpu_time_add(timer->it_clock, &new_expires, val);
  741. }
  742. /*
  743. * Install the new expiry time (or zero).
  744. * For a timer with no notification action, we don't actually
  745. * arm the timer (we'll just fake it for timer_gettime).
  746. */
  747. timer->it.cpu.expires = new_expires;
  748. if (new_expires.sched != 0 &&
  749. (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
  750. cpu_time_before(timer->it_clock, val, new_expires)) {
  751. arm_timer(timer, val);
  752. }
  753. read_unlock(&tasklist_lock);
  754. /*
  755. * Install the new reload setting, and
  756. * set up the signal and overrun bookkeeping.
  757. */
  758. timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
  759. &new->it_interval);
  760. /*
  761. * This acts as a modification timestamp for the timer,
  762. * so any automatic reload attempt will punt on seeing
  763. * that we have reset the timer manually.
  764. */
  765. timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
  766. ~REQUEUE_PENDING;
  767. timer->it_overrun_last = 0;
  768. timer->it_overrun = -1;
  769. if (new_expires.sched != 0 &&
  770. (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
  771. !cpu_time_before(timer->it_clock, val, new_expires)) {
  772. /*
  773. * The designated time already passed, so we notify
  774. * immediately, even if the thread never runs to
  775. * accumulate more time on this clock.
  776. */
  777. cpu_timer_fire(timer);
  778. }
  779. ret = 0;
  780. out:
  781. if (old) {
  782. sample_to_timespec(timer->it_clock,
  783. timer->it.cpu.incr, &old->it_interval);
  784. }
  785. return ret;
  786. }
  787. void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
  788. {
  789. union cpu_time_count now;
  790. struct task_struct *p = timer->it.cpu.task;
  791. int clear_dead;
  792. /*
  793. * Easy part: convert the reload time.
  794. */
  795. sample_to_timespec(timer->it_clock,
  796. timer->it.cpu.incr, &itp->it_interval);
  797. if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
  798. itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
  799. return;
  800. }
  801. if (unlikely(p == NULL)) {
  802. /*
  803. * This task already died and the timer will never fire.
  804. * In this case, expires is actually the dead value.
  805. */
  806. dead:
  807. sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
  808. &itp->it_value);
  809. return;
  810. }
  811. /*
  812. * Sample the clock to take the difference with the expiry time.
  813. */
  814. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  815. cpu_clock_sample(timer->it_clock, p, &now);
  816. clear_dead = p->exit_state;
  817. } else {
  818. read_lock(&tasklist_lock);
  819. if (unlikely(p->signal == NULL)) {
  820. /*
  821. * The process has been reaped.
  822. * We can't even collect a sample any more.
  823. * Call the timer disarmed, nothing else to do.
  824. */
  825. put_task_struct(p);
  826. timer->it.cpu.task = NULL;
  827. timer->it.cpu.expires.sched = 0;
  828. read_unlock(&tasklist_lock);
  829. goto dead;
  830. } else {
  831. cpu_clock_sample_group(timer->it_clock, p, &now);
  832. clear_dead = (unlikely(p->exit_state) &&
  833. thread_group_empty(p));
  834. }
  835. read_unlock(&tasklist_lock);
  836. }
  837. if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
  838. if (timer->it.cpu.incr.sched == 0 &&
  839. cpu_time_before(timer->it_clock,
  840. timer->it.cpu.expires, now)) {
  841. /*
  842. * Do-nothing timer expired and has no reload,
  843. * so it's as if it was never set.
  844. */
  845. timer->it.cpu.expires.sched = 0;
  846. itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
  847. return;
  848. }
  849. /*
  850. * Account for any expirations and reloads that should
  851. * have happened.
  852. */
  853. bump_cpu_timer(timer, now);
  854. }
  855. if (unlikely(clear_dead)) {
  856. /*
  857. * We've noticed that the thread is dead, but
  858. * not yet reaped. Take this opportunity to
  859. * drop our task ref.
  860. */
  861. clear_dead_task(timer, now);
  862. goto dead;
  863. }
  864. if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
  865. sample_to_timespec(timer->it_clock,
  866. cpu_time_sub(timer->it_clock,
  867. timer->it.cpu.expires, now),
  868. &itp->it_value);
  869. } else {
  870. /*
  871. * The timer should have expired already, but the firing
  872. * hasn't taken place yet. Say it's just about to expire.
  873. */
  874. itp->it_value.tv_nsec = 1;
  875. itp->it_value.tv_sec = 0;
  876. }
  877. }
  878. /*
  879. * Check for any per-thread CPU timers that have fired and move them off
  880. * the tsk->cpu_timers[N] list onto the firing list. Here we update the
  881. * tsk->it_*_expires values to reflect the remaining thread CPU timers.
  882. */
  883. static void check_thread_timers(struct task_struct *tsk,
  884. struct list_head *firing)
  885. {
  886. int maxfire;
  887. struct list_head *timers = tsk->cpu_timers;
  888. struct signal_struct *const sig = tsk->signal;
  889. maxfire = 20;
  890. tsk->it_prof_expires = cputime_zero;
  891. while (!list_empty(timers)) {
  892. struct cpu_timer_list *t = list_first_entry(timers,
  893. struct cpu_timer_list,
  894. entry);
  895. if (!--maxfire || cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
  896. tsk->it_prof_expires = t->expires.cpu;
  897. break;
  898. }
  899. t->firing = 1;
  900. list_move_tail(&t->entry, firing);
  901. }
  902. ++timers;
  903. maxfire = 20;
  904. tsk->it_virt_expires = cputime_zero;
  905. while (!list_empty(timers)) {
  906. struct cpu_timer_list *t = list_first_entry(timers,
  907. struct cpu_timer_list,
  908. entry);
  909. if (!--maxfire || cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
  910. tsk->it_virt_expires = t->expires.cpu;
  911. break;
  912. }
  913. t->firing = 1;
  914. list_move_tail(&t->entry, firing);
  915. }
  916. ++timers;
  917. maxfire = 20;
  918. tsk->it_sched_expires = 0;
  919. while (!list_empty(timers)) {
  920. struct cpu_timer_list *t = list_first_entry(timers,
  921. struct cpu_timer_list,
  922. entry);
  923. if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
  924. tsk->it_sched_expires = t->expires.sched;
  925. break;
  926. }
  927. t->firing = 1;
  928. list_move_tail(&t->entry, firing);
  929. }
  930. /*
  931. * Check for the special case thread timers.
  932. */
  933. if (sig->rlim[RLIMIT_RTTIME].rlim_cur != RLIM_INFINITY) {
  934. unsigned long hard = sig->rlim[RLIMIT_RTTIME].rlim_max;
  935. unsigned long *soft = &sig->rlim[RLIMIT_RTTIME].rlim_cur;
  936. if (hard != RLIM_INFINITY &&
  937. tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
  938. /*
  939. * At the hard limit, we just die.
  940. * No need to calculate anything else now.
  941. */
  942. __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
  943. return;
  944. }
  945. if (tsk->rt.timeout > DIV_ROUND_UP(*soft, USEC_PER_SEC/HZ)) {
  946. /*
  947. * At the soft limit, send a SIGXCPU every second.
  948. */
  949. if (sig->rlim[RLIMIT_RTTIME].rlim_cur
  950. < sig->rlim[RLIMIT_RTTIME].rlim_max) {
  951. sig->rlim[RLIMIT_RTTIME].rlim_cur +=
  952. USEC_PER_SEC;
  953. }
  954. printk(KERN_INFO
  955. "RT Watchdog Timeout: %s[%d]\n",
  956. tsk->comm, task_pid_nr(tsk));
  957. __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
  958. }
  959. }
  960. }
  961. /*
  962. * Check for any per-thread CPU timers that have fired and move them
  963. * off the tsk->*_timers list onto the firing list. Per-thread timers
  964. * have already been taken off.
  965. */
  966. static void check_process_timers(struct task_struct *tsk,
  967. struct list_head *firing)
  968. {
  969. int maxfire;
  970. struct signal_struct *const sig = tsk->signal;
  971. cputime_t utime, stime, ptime, virt_expires, prof_expires;
  972. unsigned long long sum_sched_runtime, sched_expires;
  973. struct task_struct *t;
  974. struct list_head *timers = sig->cpu_timers;
  975. /*
  976. * Don't sample the current process CPU clocks if there are no timers.
  977. */
  978. if (list_empty(&timers[CPUCLOCK_PROF]) &&
  979. cputime_eq(sig->it_prof_expires, cputime_zero) &&
  980. sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
  981. list_empty(&timers[CPUCLOCK_VIRT]) &&
  982. cputime_eq(sig->it_virt_expires, cputime_zero) &&
  983. list_empty(&timers[CPUCLOCK_SCHED]))
  984. return;
  985. /*
  986. * Collect the current process totals.
  987. */
  988. utime = sig->utime;
  989. stime = sig->stime;
  990. sum_sched_runtime = sig->sum_sched_runtime;
  991. t = tsk;
  992. do {
  993. utime = cputime_add(utime, t->utime);
  994. stime = cputime_add(stime, t->stime);
  995. sum_sched_runtime += t->se.sum_exec_runtime;
  996. t = next_thread(t);
  997. } while (t != tsk);
  998. ptime = cputime_add(utime, stime);
  999. maxfire = 20;
  1000. prof_expires = cputime_zero;
  1001. while (!list_empty(timers)) {
  1002. struct cpu_timer_list *tl = list_first_entry(timers,
  1003. struct cpu_timer_list,
  1004. entry);
  1005. if (!--maxfire || cputime_lt(ptime, tl->expires.cpu)) {
  1006. prof_expires = tl->expires.cpu;
  1007. break;
  1008. }
  1009. tl->firing = 1;
  1010. list_move_tail(&tl->entry, firing);
  1011. }
  1012. ++timers;
  1013. maxfire = 20;
  1014. virt_expires = cputime_zero;
  1015. while (!list_empty(timers)) {
  1016. struct cpu_timer_list *tl = list_first_entry(timers,
  1017. struct cpu_timer_list,
  1018. entry);
  1019. if (!--maxfire || cputime_lt(utime, tl->expires.cpu)) {
  1020. virt_expires = tl->expires.cpu;
  1021. break;
  1022. }
  1023. tl->firing = 1;
  1024. list_move_tail(&tl->entry, firing);
  1025. }
  1026. ++timers;
  1027. maxfire = 20;
  1028. sched_expires = 0;
  1029. while (!list_empty(timers)) {
  1030. struct cpu_timer_list *tl = list_first_entry(timers,
  1031. struct cpu_timer_list,
  1032. entry);
  1033. if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
  1034. sched_expires = tl->expires.sched;
  1035. break;
  1036. }
  1037. tl->firing = 1;
  1038. list_move_tail(&tl->entry, firing);
  1039. }
  1040. /*
  1041. * Check for the special case process timers.
  1042. */
  1043. if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
  1044. if (cputime_ge(ptime, sig->it_prof_expires)) {
  1045. /* ITIMER_PROF fires and reloads. */
  1046. sig->it_prof_expires = sig->it_prof_incr;
  1047. if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
  1048. sig->it_prof_expires = cputime_add(
  1049. sig->it_prof_expires, ptime);
  1050. }
  1051. __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
  1052. }
  1053. if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
  1054. (cputime_eq(prof_expires, cputime_zero) ||
  1055. cputime_lt(sig->it_prof_expires, prof_expires))) {
  1056. prof_expires = sig->it_prof_expires;
  1057. }
  1058. }
  1059. if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
  1060. if (cputime_ge(utime, sig->it_virt_expires)) {
  1061. /* ITIMER_VIRTUAL fires and reloads. */
  1062. sig->it_virt_expires = sig->it_virt_incr;
  1063. if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
  1064. sig->it_virt_expires = cputime_add(
  1065. sig->it_virt_expires, utime);
  1066. }
  1067. __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
  1068. }
  1069. if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
  1070. (cputime_eq(virt_expires, cputime_zero) ||
  1071. cputime_lt(sig->it_virt_expires, virt_expires))) {
  1072. virt_expires = sig->it_virt_expires;
  1073. }
  1074. }
  1075. if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
  1076. unsigned long psecs = cputime_to_secs(ptime);
  1077. cputime_t x;
  1078. if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
  1079. /*
  1080. * At the hard limit, we just die.
  1081. * No need to calculate anything else now.
  1082. */
  1083. __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
  1084. return;
  1085. }
  1086. if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
  1087. /*
  1088. * At the soft limit, send a SIGXCPU every second.
  1089. */
  1090. __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
  1091. if (sig->rlim[RLIMIT_CPU].rlim_cur
  1092. < sig->rlim[RLIMIT_CPU].rlim_max) {
  1093. sig->rlim[RLIMIT_CPU].rlim_cur++;
  1094. }
  1095. }
  1096. x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
  1097. if (cputime_eq(prof_expires, cputime_zero) ||
  1098. cputime_lt(x, prof_expires)) {
  1099. prof_expires = x;
  1100. }
  1101. }
  1102. if (!cputime_eq(prof_expires, cputime_zero) ||
  1103. !cputime_eq(virt_expires, cputime_zero) ||
  1104. sched_expires != 0) {
  1105. /*
  1106. * Rebalance the threads' expiry times for the remaining
  1107. * process CPU timers.
  1108. */
  1109. cputime_t prof_left, virt_left, ticks;
  1110. unsigned long long sched_left, sched;
  1111. const unsigned int nthreads = atomic_read(&sig->live);
  1112. if (!nthreads)
  1113. return;
  1114. prof_left = cputime_sub(prof_expires, utime);
  1115. prof_left = cputime_sub(prof_left, stime);
  1116. prof_left = cputime_div_non_zero(prof_left, nthreads);
  1117. virt_left = cputime_sub(virt_expires, utime);
  1118. virt_left = cputime_div_non_zero(virt_left, nthreads);
  1119. if (sched_expires) {
  1120. sched_left = sched_expires - sum_sched_runtime;
  1121. do_div(sched_left, nthreads);
  1122. sched_left = max_t(unsigned long long, sched_left, 1);
  1123. } else {
  1124. sched_left = 0;
  1125. }
  1126. t = tsk;
  1127. do {
  1128. if (unlikely(t->flags & PF_EXITING))
  1129. continue;
  1130. ticks = cputime_add(cputime_add(t->utime, t->stime),
  1131. prof_left);
  1132. if (!cputime_eq(prof_expires, cputime_zero) &&
  1133. (cputime_eq(t->it_prof_expires, cputime_zero) ||
  1134. cputime_gt(t->it_prof_expires, ticks))) {
  1135. t->it_prof_expires = ticks;
  1136. }
  1137. ticks = cputime_add(t->utime, virt_left);
  1138. if (!cputime_eq(virt_expires, cputime_zero) &&
  1139. (cputime_eq(t->it_virt_expires, cputime_zero) ||
  1140. cputime_gt(t->it_virt_expires, ticks))) {
  1141. t->it_virt_expires = ticks;
  1142. }
  1143. sched = t->se.sum_exec_runtime + sched_left;
  1144. if (sched_expires && (t->it_sched_expires == 0 ||
  1145. t->it_sched_expires > sched)) {
  1146. t->it_sched_expires = sched;
  1147. }
  1148. } while ((t = next_thread(t)) != tsk);
  1149. }
  1150. }
  1151. /*
  1152. * This is called from the signal code (via do_schedule_next_timer)
  1153. * when the last timer signal was delivered and we have to reload the timer.
  1154. */
  1155. void posix_cpu_timer_schedule(struct k_itimer *timer)
  1156. {
  1157. struct task_struct *p = timer->it.cpu.task;
  1158. union cpu_time_count now;
  1159. if (unlikely(p == NULL))
  1160. /*
  1161. * The task was cleaned up already, no future firings.
  1162. */
  1163. goto out;
  1164. /*
  1165. * Fetch the current sample and update the timer's expiry time.
  1166. */
  1167. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  1168. cpu_clock_sample(timer->it_clock, p, &now);
  1169. bump_cpu_timer(timer, now);
  1170. if (unlikely(p->exit_state)) {
  1171. clear_dead_task(timer, now);
  1172. goto out;
  1173. }
  1174. read_lock(&tasklist_lock); /* arm_timer needs it. */
  1175. } else {
  1176. read_lock(&tasklist_lock);
  1177. if (unlikely(p->signal == NULL)) {
  1178. /*
  1179. * The process has been reaped.
  1180. * We can't even collect a sample any more.
  1181. */
  1182. put_task_struct(p);
  1183. timer->it.cpu.task = p = NULL;
  1184. timer->it.cpu.expires.sched = 0;
  1185. goto out_unlock;
  1186. } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
  1187. /*
  1188. * We've noticed that the thread is dead, but
  1189. * not yet reaped. Take this opportunity to
  1190. * drop our task ref.
  1191. */
  1192. clear_dead_task(timer, now);
  1193. goto out_unlock;
  1194. }
  1195. cpu_clock_sample_group(timer->it_clock, p, &now);
  1196. bump_cpu_timer(timer, now);
  1197. /* Leave the tasklist_lock locked for the call below. */
  1198. }
  1199. /*
  1200. * Now re-arm for the new expiry time.
  1201. */
  1202. arm_timer(timer, now);
  1203. out_unlock:
  1204. read_unlock(&tasklist_lock);
  1205. out:
  1206. timer->it_overrun_last = timer->it_overrun;
  1207. timer->it_overrun = -1;
  1208. ++timer->it_requeue_pending;
  1209. }
  1210. /*
  1211. * This is called from the timer interrupt handler. The irq handler has
  1212. * already updated our counts. We need to check if any timers fire now.
  1213. * Interrupts are disabled.
  1214. */
  1215. void run_posix_cpu_timers(struct task_struct *tsk)
  1216. {
  1217. LIST_HEAD(firing);
  1218. struct k_itimer *timer, *next;
  1219. BUG_ON(!irqs_disabled());
  1220. #define UNEXPIRED(clock) \
  1221. (cputime_eq(tsk->it_##clock##_expires, cputime_zero) || \
  1222. cputime_lt(clock##_ticks(tsk), tsk->it_##clock##_expires))
  1223. if (UNEXPIRED(prof) && UNEXPIRED(virt) &&
  1224. (tsk->it_sched_expires == 0 ||
  1225. tsk->se.sum_exec_runtime < tsk->it_sched_expires))
  1226. return;
  1227. #undef UNEXPIRED
  1228. /*
  1229. * Double-check with locks held.
  1230. */
  1231. read_lock(&tasklist_lock);
  1232. if (likely(tsk->signal != NULL)) {
  1233. spin_lock(&tsk->sighand->siglock);
  1234. /*
  1235. * Here we take off tsk->cpu_timers[N] and tsk->signal->cpu_timers[N]
  1236. * all the timers that are firing, and put them on the firing list.
  1237. */
  1238. check_thread_timers(tsk, &firing);
  1239. check_process_timers(tsk, &firing);
  1240. /*
  1241. * We must release these locks before taking any timer's lock.
  1242. * There is a potential race with timer deletion here, as the
  1243. * siglock now protects our private firing list. We have set
  1244. * the firing flag in each timer, so that a deletion attempt
  1245. * that gets the timer lock before we do will give it up and
  1246. * spin until we've taken care of that timer below.
  1247. */
  1248. spin_unlock(&tsk->sighand->siglock);
  1249. }
  1250. read_unlock(&tasklist_lock);
  1251. /*
  1252. * Now that all the timers on our list have the firing flag,
  1253. * noone will touch their list entries but us. We'll take
  1254. * each timer's lock before clearing its firing flag, so no
  1255. * timer call will interfere.
  1256. */
  1257. list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
  1258. int firing;
  1259. spin_lock(&timer->it_lock);
  1260. list_del_init(&timer->it.cpu.entry);
  1261. firing = timer->it.cpu.firing;
  1262. timer->it.cpu.firing = 0;
  1263. /*
  1264. * The firing flag is -1 if we collided with a reset
  1265. * of the timer, which already reported this
  1266. * almost-firing as an overrun. So don't generate an event.
  1267. */
  1268. if (likely(firing >= 0)) {
  1269. cpu_timer_fire(timer);
  1270. }
  1271. spin_unlock(&timer->it_lock);
  1272. }
  1273. }
  1274. /*
  1275. * Set one of the process-wide special case CPU timers.
  1276. * The tasklist_lock and tsk->sighand->siglock must be held by the caller.
  1277. * The oldval argument is null for the RLIMIT_CPU timer, where *newval is
  1278. * absolute; non-null for ITIMER_*, where *newval is relative and we update
  1279. * it to be absolute, *oldval is absolute and we update it to be relative.
  1280. */
  1281. void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
  1282. cputime_t *newval, cputime_t *oldval)
  1283. {
  1284. union cpu_time_count now;
  1285. struct list_head *head;
  1286. BUG_ON(clock_idx == CPUCLOCK_SCHED);
  1287. cpu_clock_sample_group_locked(clock_idx, tsk, &now);
  1288. if (oldval) {
  1289. if (!cputime_eq(*oldval, cputime_zero)) {
  1290. if (cputime_le(*oldval, now.cpu)) {
  1291. /* Just about to fire. */
  1292. *oldval = jiffies_to_cputime(1);
  1293. } else {
  1294. *oldval = cputime_sub(*oldval, now.cpu);
  1295. }
  1296. }
  1297. if (cputime_eq(*newval, cputime_zero))
  1298. return;
  1299. *newval = cputime_add(*newval, now.cpu);
  1300. /*
  1301. * If the RLIMIT_CPU timer will expire before the
  1302. * ITIMER_PROF timer, we have nothing else to do.
  1303. */
  1304. if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
  1305. < cputime_to_secs(*newval))
  1306. return;
  1307. }
  1308. /*
  1309. * Check whether there are any process timers already set to fire
  1310. * before this one. If so, we don't have anything more to do.
  1311. */
  1312. head = &tsk->signal->cpu_timers[clock_idx];
  1313. if (list_empty(head) ||
  1314. cputime_ge(list_first_entry(head,
  1315. struct cpu_timer_list, entry)->expires.cpu,
  1316. *newval)) {
  1317. /*
  1318. * Rejigger each thread's expiry time so that one will
  1319. * notice before we hit the process-cumulative expiry time.
  1320. */
  1321. union cpu_time_count expires = { .sched = 0 };
  1322. expires.cpu = *newval;
  1323. process_timer_rebalance(tsk, clock_idx, expires, now);
  1324. }
  1325. }
  1326. static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
  1327. struct timespec *rqtp, struct itimerspec *it)
  1328. {
  1329. struct k_itimer timer;
  1330. int error;
  1331. /*
  1332. * Set up a temporary timer and then wait for it to go off.
  1333. */
  1334. memset(&timer, 0, sizeof timer);
  1335. spin_lock_init(&timer.it_lock);
  1336. timer.it_clock = which_clock;
  1337. timer.it_overrun = -1;
  1338. error = posix_cpu_timer_create(&timer);
  1339. timer.it_process = current;
  1340. if (!error) {
  1341. static struct itimerspec zero_it;
  1342. memset(it, 0, sizeof *it);
  1343. it->it_value = *rqtp;
  1344. spin_lock_irq(&timer.it_lock);
  1345. error = posix_cpu_timer_set(&timer, flags, it, NULL);
  1346. if (error) {
  1347. spin_unlock_irq(&timer.it_lock);
  1348. return error;
  1349. }
  1350. while (!signal_pending(current)) {
  1351. if (timer.it.cpu.expires.sched == 0) {
  1352. /*
  1353. * Our timer fired and was reset.
  1354. */
  1355. spin_unlock_irq(&timer.it_lock);
  1356. return 0;
  1357. }
  1358. /*
  1359. * Block until cpu_timer_fire (or a signal) wakes us.
  1360. */
  1361. __set_current_state(TASK_INTERRUPTIBLE);
  1362. spin_unlock_irq(&timer.it_lock);
  1363. schedule();
  1364. spin_lock_irq(&timer.it_lock);
  1365. }
  1366. /*
  1367. * We were interrupted by a signal.
  1368. */
  1369. sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
  1370. posix_cpu_timer_set(&timer, 0, &zero_it, it);
  1371. spin_unlock_irq(&timer.it_lock);
  1372. if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
  1373. /*
  1374. * It actually did fire already.
  1375. */
  1376. return 0;
  1377. }
  1378. error = -ERESTART_RESTARTBLOCK;
  1379. }
  1380. return error;
  1381. }
  1382. int posix_cpu_nsleep(const clockid_t which_clock, int flags,
  1383. struct timespec *rqtp, struct timespec __user *rmtp)
  1384. {
  1385. struct restart_block *restart_block =
  1386. &current_thread_info()->restart_block;
  1387. struct itimerspec it;
  1388. int error;
  1389. /*
  1390. * Diagnose required errors first.
  1391. */
  1392. if (CPUCLOCK_PERTHREAD(which_clock) &&
  1393. (CPUCLOCK_PID(which_clock) == 0 ||
  1394. CPUCLOCK_PID(which_clock) == current->pid))
  1395. return -EINVAL;
  1396. error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
  1397. if (error == -ERESTART_RESTARTBLOCK) {
  1398. if (flags & TIMER_ABSTIME)
  1399. return -ERESTARTNOHAND;
  1400. /*
  1401. * Report back to the user the time still remaining.
  1402. */
  1403. if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
  1404. return -EFAULT;
  1405. restart_block->fn = posix_cpu_nsleep_restart;
  1406. restart_block->arg0 = which_clock;
  1407. restart_block->arg1 = (unsigned long) rmtp;
  1408. restart_block->arg2 = rqtp->tv_sec;
  1409. restart_block->arg3 = rqtp->tv_nsec;
  1410. }
  1411. return error;
  1412. }
  1413. long posix_cpu_nsleep_restart(struct restart_block *restart_block)
  1414. {
  1415. clockid_t which_clock = restart_block->arg0;
  1416. struct timespec __user *rmtp;
  1417. struct timespec t;
  1418. struct itimerspec it;
  1419. int error;
  1420. rmtp = (struct timespec __user *) restart_block->arg1;
  1421. t.tv_sec = restart_block->arg2;
  1422. t.tv_nsec = restart_block->arg3;
  1423. restart_block->fn = do_no_restart_syscall;
  1424. error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
  1425. if (error == -ERESTART_RESTARTBLOCK) {
  1426. /*
  1427. * Report back to the user the time still remaining.
  1428. */
  1429. if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
  1430. return -EFAULT;
  1431. restart_block->fn = posix_cpu_nsleep_restart;
  1432. restart_block->arg0 = which_clock;
  1433. restart_block->arg1 = (unsigned long) rmtp;
  1434. restart_block->arg2 = t.tv_sec;
  1435. restart_block->arg3 = t.tv_nsec;
  1436. }
  1437. return error;
  1438. }
  1439. #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
  1440. #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
  1441. static int process_cpu_clock_getres(const clockid_t which_clock,
  1442. struct timespec *tp)
  1443. {
  1444. return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
  1445. }
  1446. static int process_cpu_clock_get(const clockid_t which_clock,
  1447. struct timespec *tp)
  1448. {
  1449. return posix_cpu_clock_get(PROCESS_CLOCK, tp);
  1450. }
  1451. static int process_cpu_timer_create(struct k_itimer *timer)
  1452. {
  1453. timer->it_clock = PROCESS_CLOCK;
  1454. return posix_cpu_timer_create(timer);
  1455. }
  1456. static int process_cpu_nsleep(const clockid_t which_clock, int flags,
  1457. struct timespec *rqtp,
  1458. struct timespec __user *rmtp)
  1459. {
  1460. return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
  1461. }
  1462. static long process_cpu_nsleep_restart(struct restart_block *restart_block)
  1463. {
  1464. return -EINVAL;
  1465. }
  1466. static int thread_cpu_clock_getres(const clockid_t which_clock,
  1467. struct timespec *tp)
  1468. {
  1469. return posix_cpu_clock_getres(THREAD_CLOCK, tp);
  1470. }
  1471. static int thread_cpu_clock_get(const clockid_t which_clock,
  1472. struct timespec *tp)
  1473. {
  1474. return posix_cpu_clock_get(THREAD_CLOCK, tp);
  1475. }
  1476. static int thread_cpu_timer_create(struct k_itimer *timer)
  1477. {
  1478. timer->it_clock = THREAD_CLOCK;
  1479. return posix_cpu_timer_create(timer);
  1480. }
  1481. static int thread_cpu_nsleep(const clockid_t which_clock, int flags,
  1482. struct timespec *rqtp, struct timespec __user *rmtp)
  1483. {
  1484. return -EINVAL;
  1485. }
  1486. static long thread_cpu_nsleep_restart(struct restart_block *restart_block)
  1487. {
  1488. return -EINVAL;
  1489. }
  1490. static __init int init_posix_cpu_timers(void)
  1491. {
  1492. struct k_clock process = {
  1493. .clock_getres = process_cpu_clock_getres,
  1494. .clock_get = process_cpu_clock_get,
  1495. .clock_set = do_posix_clock_nosettime,
  1496. .timer_create = process_cpu_timer_create,
  1497. .nsleep = process_cpu_nsleep,
  1498. .nsleep_restart = process_cpu_nsleep_restart,
  1499. };
  1500. struct k_clock thread = {
  1501. .clock_getres = thread_cpu_clock_getres,
  1502. .clock_get = thread_cpu_clock_get,
  1503. .clock_set = do_posix_clock_nosettime,
  1504. .timer_create = thread_cpu_timer_create,
  1505. .nsleep = thread_cpu_nsleep,
  1506. .nsleep_restart = thread_cpu_nsleep_restart,
  1507. };
  1508. register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
  1509. register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
  1510. return 0;
  1511. }
  1512. __initcall(init_posix_cpu_timers);