posix-cpu-timers.c 41 KB

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