posix-cpu-timers.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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(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(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 = 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(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(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(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(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 inline 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_le(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(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(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(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. if (p->tgid == current->tgid) {
  222. /*
  223. * We're sampling ourselves, so include the
  224. * cycles not yet banked. We still omit
  225. * other threads running on other CPUs,
  226. * so the total can always be behind as
  227. * much as max(nthreads-1,ncpus) * (NSEC_PER_SEC/HZ).
  228. */
  229. cpu->sched += current_sched_time(current);
  230. } else {
  231. cpu->sched += p->sched_time;
  232. }
  233. break;
  234. }
  235. return 0;
  236. }
  237. /*
  238. * Sample a process (thread group) clock for the given group_leader task.
  239. * Must be called with tasklist_lock held for reading.
  240. */
  241. static int cpu_clock_sample_group(clockid_t which_clock,
  242. struct task_struct *p,
  243. union cpu_time_count *cpu)
  244. {
  245. int ret;
  246. unsigned long flags;
  247. spin_lock_irqsave(&p->sighand->siglock, flags);
  248. ret = cpu_clock_sample_group_locked(CPUCLOCK_WHICH(which_clock), p,
  249. cpu);
  250. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  251. return ret;
  252. }
  253. int posix_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
  254. {
  255. const pid_t pid = CPUCLOCK_PID(which_clock);
  256. int error = -EINVAL;
  257. union cpu_time_count rtn;
  258. if (pid == 0) {
  259. /*
  260. * Special case constant value for our own clocks.
  261. * We don't have to do any lookup to find ourselves.
  262. */
  263. if (CPUCLOCK_PERTHREAD(which_clock)) {
  264. /*
  265. * Sampling just ourselves we can do with no locking.
  266. */
  267. error = cpu_clock_sample(which_clock,
  268. current, &rtn);
  269. } else {
  270. read_lock(&tasklist_lock);
  271. error = cpu_clock_sample_group(which_clock,
  272. current, &rtn);
  273. read_unlock(&tasklist_lock);
  274. }
  275. } else {
  276. /*
  277. * Find the given PID, and validate that the caller
  278. * should be able to see it.
  279. */
  280. struct task_struct *p;
  281. read_lock(&tasklist_lock);
  282. p = find_task_by_pid(pid);
  283. if (p) {
  284. if (CPUCLOCK_PERTHREAD(which_clock)) {
  285. if (p->tgid == current->tgid) {
  286. error = cpu_clock_sample(which_clock,
  287. p, &rtn);
  288. }
  289. } else if (p->tgid == pid && p->signal) {
  290. error = cpu_clock_sample_group(which_clock,
  291. p, &rtn);
  292. }
  293. }
  294. read_unlock(&tasklist_lock);
  295. }
  296. if (error)
  297. return error;
  298. sample_to_timespec(which_clock, rtn, tp);
  299. return 0;
  300. }
  301. /*
  302. * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
  303. * This is called from sys_timer_create with the new timer already locked.
  304. */
  305. int posix_cpu_timer_create(struct k_itimer *new_timer)
  306. {
  307. int ret = 0;
  308. const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
  309. struct task_struct *p;
  310. if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
  311. return -EINVAL;
  312. INIT_LIST_HEAD(&new_timer->it.cpu.entry);
  313. new_timer->it.cpu.incr.sched = 0;
  314. new_timer->it.cpu.expires.sched = 0;
  315. read_lock(&tasklist_lock);
  316. if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
  317. if (pid == 0) {
  318. p = current;
  319. } else {
  320. p = find_task_by_pid(pid);
  321. if (p && p->tgid != current->tgid)
  322. p = NULL;
  323. }
  324. } else {
  325. if (pid == 0) {
  326. p = current->group_leader;
  327. } else {
  328. p = find_task_by_pid(pid);
  329. if (p && p->tgid != pid)
  330. p = NULL;
  331. }
  332. }
  333. new_timer->it.cpu.task = p;
  334. if (p) {
  335. get_task_struct(p);
  336. } else {
  337. ret = -EINVAL;
  338. }
  339. read_unlock(&tasklist_lock);
  340. return ret;
  341. }
  342. /*
  343. * Clean up a CPU-clock timer that is about to be destroyed.
  344. * This is called from timer deletion with the timer already locked.
  345. * If we return TIMER_RETRY, it's necessary to release the timer's lock
  346. * and try again. (This happens when the timer is in the middle of firing.)
  347. */
  348. int posix_cpu_timer_del(struct k_itimer *timer)
  349. {
  350. struct task_struct *p = timer->it.cpu.task;
  351. if (timer->it.cpu.firing)
  352. return TIMER_RETRY;
  353. if (unlikely(p == NULL))
  354. return 0;
  355. if (!list_empty(&timer->it.cpu.entry)) {
  356. read_lock(&tasklist_lock);
  357. if (unlikely(p->signal == NULL)) {
  358. /*
  359. * We raced with the reaping of the task.
  360. * The deletion should have cleared us off the list.
  361. */
  362. BUG_ON(!list_empty(&timer->it.cpu.entry));
  363. } else {
  364. /*
  365. * Take us off the task's timer list.
  366. */
  367. spin_lock(&p->sighand->siglock);
  368. list_del(&timer->it.cpu.entry);
  369. spin_unlock(&p->sighand->siglock);
  370. }
  371. read_unlock(&tasklist_lock);
  372. }
  373. put_task_struct(p);
  374. return 0;
  375. }
  376. /*
  377. * Clean out CPU timers still ticking when a thread exited. The task
  378. * pointer is cleared, and the expiry time is replaced with the residual
  379. * time for later timer_gettime calls to return.
  380. * This must be called with the siglock held.
  381. */
  382. static void cleanup_timers(struct list_head *head,
  383. cputime_t utime, cputime_t stime,
  384. unsigned long long sched_time)
  385. {
  386. struct cpu_timer_list *timer, *next;
  387. cputime_t ptime = cputime_add(utime, stime);
  388. list_for_each_entry_safe(timer, next, head, entry) {
  389. put_task_struct(timer->task);
  390. timer->task = NULL;
  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. put_task_struct(timer->task);
  402. timer->task = NULL;
  403. list_del_init(&timer->entry);
  404. if (cputime_lt(timer->expires.cpu, utime)) {
  405. timer->expires.cpu = cputime_zero;
  406. } else {
  407. timer->expires.cpu = cputime_sub(timer->expires.cpu,
  408. utime);
  409. }
  410. }
  411. ++head;
  412. list_for_each_entry_safe(timer, next, head, entry) {
  413. put_task_struct(timer->task);
  414. timer->task = NULL;
  415. list_del_init(&timer->entry);
  416. if (timer->expires.sched < sched_time) {
  417. timer->expires.sched = 0;
  418. } else {
  419. timer->expires.sched -= sched_time;
  420. }
  421. }
  422. }
  423. /*
  424. * These are both called with the siglock held, when the current thread
  425. * is being reaped. When the final (leader) thread in the group is reaped,
  426. * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
  427. */
  428. void posix_cpu_timers_exit(struct task_struct *tsk)
  429. {
  430. cleanup_timers(tsk->cpu_timers,
  431. tsk->utime, tsk->stime, tsk->sched_time);
  432. }
  433. void posix_cpu_timers_exit_group(struct task_struct *tsk)
  434. {
  435. cleanup_timers(tsk->signal->cpu_timers,
  436. cputime_add(tsk->utime, tsk->signal->utime),
  437. cputime_add(tsk->stime, tsk->signal->stime),
  438. tsk->sched_time + tsk->signal->sched_time);
  439. }
  440. /*
  441. * Set the expiry times of all the threads in the process so one of them
  442. * will go off before the process cumulative expiry total is reached.
  443. */
  444. static void process_timer_rebalance(struct task_struct *p,
  445. unsigned int clock_idx,
  446. union cpu_time_count expires,
  447. union cpu_time_count val)
  448. {
  449. cputime_t ticks, left;
  450. unsigned long long ns, nsleft;
  451. struct task_struct *t = p;
  452. unsigned int nthreads = atomic_read(&p->signal->live);
  453. switch (clock_idx) {
  454. default:
  455. BUG();
  456. break;
  457. case CPUCLOCK_PROF:
  458. left = cputime_div(cputime_sub(expires.cpu, val.cpu),
  459. nthreads);
  460. do {
  461. if (!unlikely(t->exit_state)) {
  462. ticks = cputime_add(prof_ticks(t), left);
  463. if (cputime_eq(t->it_prof_expires,
  464. cputime_zero) ||
  465. cputime_gt(t->it_prof_expires, ticks)) {
  466. t->it_prof_expires = ticks;
  467. }
  468. }
  469. t = next_thread(t);
  470. } while (t != p);
  471. break;
  472. case CPUCLOCK_VIRT:
  473. left = cputime_div(cputime_sub(expires.cpu, val.cpu),
  474. nthreads);
  475. do {
  476. if (!unlikely(t->exit_state)) {
  477. ticks = cputime_add(virt_ticks(t), left);
  478. if (cputime_eq(t->it_virt_expires,
  479. cputime_zero) ||
  480. cputime_gt(t->it_virt_expires, ticks)) {
  481. t->it_virt_expires = ticks;
  482. }
  483. }
  484. t = next_thread(t);
  485. } while (t != p);
  486. break;
  487. case CPUCLOCK_SCHED:
  488. nsleft = expires.sched - val.sched;
  489. do_div(nsleft, nthreads);
  490. do {
  491. if (!unlikely(t->exit_state)) {
  492. ns = t->sched_time + nsleft;
  493. if (t->it_sched_expires == 0 ||
  494. t->it_sched_expires > ns) {
  495. t->it_sched_expires = ns;
  496. }
  497. }
  498. t = next_thread(t);
  499. } while (t != p);
  500. break;
  501. }
  502. }
  503. static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
  504. {
  505. /*
  506. * That's all for this thread or process.
  507. * We leave our residual in expires to be reported.
  508. */
  509. put_task_struct(timer->it.cpu.task);
  510. timer->it.cpu.task = NULL;
  511. timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
  512. timer->it.cpu.expires,
  513. now);
  514. }
  515. /*
  516. * Insert the timer on the appropriate list before any timers that
  517. * expire later. This must be called with the tasklist_lock held
  518. * for reading, and interrupts disabled.
  519. */
  520. static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
  521. {
  522. struct task_struct *p = timer->it.cpu.task;
  523. struct list_head *head, *listpos;
  524. struct cpu_timer_list *const nt = &timer->it.cpu;
  525. struct cpu_timer_list *next;
  526. unsigned long i;
  527. head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
  528. p->cpu_timers : p->signal->cpu_timers);
  529. head += CPUCLOCK_WHICH(timer->it_clock);
  530. BUG_ON(!irqs_disabled());
  531. spin_lock(&p->sighand->siglock);
  532. listpos = head;
  533. if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
  534. list_for_each_entry(next, head, entry) {
  535. if (next->expires.sched > nt->expires.sched) {
  536. listpos = &next->entry;
  537. break;
  538. }
  539. }
  540. } else {
  541. list_for_each_entry(next, head, entry) {
  542. if (cputime_gt(next->expires.cpu, nt->expires.cpu)) {
  543. listpos = &next->entry;
  544. break;
  545. }
  546. }
  547. }
  548. list_add(&nt->entry, listpos);
  549. if (listpos == head) {
  550. /*
  551. * We are the new earliest-expiring timer.
  552. * If we are a thread timer, there can always
  553. * be a process timer telling us to stop earlier.
  554. */
  555. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  556. switch (CPUCLOCK_WHICH(timer->it_clock)) {
  557. default:
  558. BUG();
  559. case CPUCLOCK_PROF:
  560. if (cputime_eq(p->it_prof_expires,
  561. cputime_zero) ||
  562. cputime_gt(p->it_prof_expires,
  563. nt->expires.cpu))
  564. p->it_prof_expires = nt->expires.cpu;
  565. break;
  566. case CPUCLOCK_VIRT:
  567. if (cputime_eq(p->it_virt_expires,
  568. cputime_zero) ||
  569. cputime_gt(p->it_virt_expires,
  570. nt->expires.cpu))
  571. p->it_virt_expires = nt->expires.cpu;
  572. break;
  573. case CPUCLOCK_SCHED:
  574. if (p->it_sched_expires == 0 ||
  575. p->it_sched_expires > nt->expires.sched)
  576. p->it_sched_expires = nt->expires.sched;
  577. break;
  578. }
  579. } else {
  580. /*
  581. * For a process timer, we must balance
  582. * all the live threads' expirations.
  583. */
  584. switch (CPUCLOCK_WHICH(timer->it_clock)) {
  585. default:
  586. BUG();
  587. case CPUCLOCK_VIRT:
  588. if (!cputime_eq(p->signal->it_virt_expires,
  589. cputime_zero) &&
  590. cputime_lt(p->signal->it_virt_expires,
  591. timer->it.cpu.expires.cpu))
  592. break;
  593. goto rebalance;
  594. case CPUCLOCK_PROF:
  595. if (!cputime_eq(p->signal->it_prof_expires,
  596. cputime_zero) &&
  597. cputime_lt(p->signal->it_prof_expires,
  598. timer->it.cpu.expires.cpu))
  599. break;
  600. i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
  601. if (i != RLIM_INFINITY &&
  602. i <= cputime_to_secs(timer->it.cpu.expires.cpu))
  603. break;
  604. goto rebalance;
  605. case CPUCLOCK_SCHED:
  606. rebalance:
  607. process_timer_rebalance(
  608. timer->it.cpu.task,
  609. CPUCLOCK_WHICH(timer->it_clock),
  610. timer->it.cpu.expires, now);
  611. break;
  612. }
  613. }
  614. }
  615. spin_unlock(&p->sighand->siglock);
  616. }
  617. /*
  618. * The timer is locked, fire it and arrange for its reload.
  619. */
  620. static void cpu_timer_fire(struct k_itimer *timer)
  621. {
  622. if (unlikely(timer->sigq == NULL)) {
  623. /*
  624. * This a special case for clock_nanosleep,
  625. * not a normal timer from sys_timer_create.
  626. */
  627. wake_up_process(timer->it_process);
  628. timer->it.cpu.expires.sched = 0;
  629. } else if (timer->it.cpu.incr.sched == 0) {
  630. /*
  631. * One-shot timer. Clear it as soon as it's fired.
  632. */
  633. posix_timer_event(timer, 0);
  634. timer->it.cpu.expires.sched = 0;
  635. } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
  636. /*
  637. * The signal did not get queued because the signal
  638. * was ignored, so we won't get any callback to
  639. * reload the timer. But we need to keep it
  640. * ticking in case the signal is deliverable next time.
  641. */
  642. posix_cpu_timer_schedule(timer);
  643. }
  644. }
  645. /*
  646. * Guts of sys_timer_settime for CPU timers.
  647. * This is called with the timer locked and interrupts disabled.
  648. * If we return TIMER_RETRY, it's necessary to release the timer's lock
  649. * and try again. (This happens when the timer is in the middle of firing.)
  650. */
  651. int posix_cpu_timer_set(struct k_itimer *timer, int flags,
  652. struct itimerspec *new, struct itimerspec *old)
  653. {
  654. struct task_struct *p = timer->it.cpu.task;
  655. union cpu_time_count old_expires, new_expires, val;
  656. int ret;
  657. if (unlikely(p == NULL)) {
  658. /*
  659. * Timer refers to a dead task's clock.
  660. */
  661. return -ESRCH;
  662. }
  663. new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
  664. read_lock(&tasklist_lock);
  665. /*
  666. * We need the tasklist_lock to protect against reaping that
  667. * clears p->signal. If p has just been reaped, we can no
  668. * longer get any information about it at all.
  669. */
  670. if (unlikely(p->signal == NULL)) {
  671. read_unlock(&tasklist_lock);
  672. put_task_struct(p);
  673. timer->it.cpu.task = NULL;
  674. return -ESRCH;
  675. }
  676. /*
  677. * Disarm any old timer after extracting its expiry time.
  678. */
  679. BUG_ON(!irqs_disabled());
  680. spin_lock(&p->sighand->siglock);
  681. old_expires = timer->it.cpu.expires;
  682. list_del_init(&timer->it.cpu.entry);
  683. spin_unlock(&p->sighand->siglock);
  684. /*
  685. * We need to sample the current value to convert the new
  686. * value from to relative and absolute, and to convert the
  687. * old value from absolute to relative. To set a process
  688. * timer, we need a sample to balance the thread expiry
  689. * times (in arm_timer). With an absolute time, we must
  690. * check if it's already passed. In short, we need a sample.
  691. */
  692. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  693. cpu_clock_sample(timer->it_clock, p, &val);
  694. } else {
  695. cpu_clock_sample_group(timer->it_clock, p, &val);
  696. }
  697. if (old) {
  698. if (old_expires.sched == 0) {
  699. old->it_value.tv_sec = 0;
  700. old->it_value.tv_nsec = 0;
  701. } else {
  702. /*
  703. * Update the timer in case it has
  704. * overrun already. If it has,
  705. * we'll report it as having overrun
  706. * and with the next reloaded timer
  707. * already ticking, though we are
  708. * swallowing that pending
  709. * notification here to install the
  710. * new setting.
  711. */
  712. bump_cpu_timer(timer, val);
  713. if (cpu_time_before(timer->it_clock, val,
  714. timer->it.cpu.expires)) {
  715. old_expires = cpu_time_sub(
  716. timer->it_clock,
  717. timer->it.cpu.expires, val);
  718. sample_to_timespec(timer->it_clock,
  719. old_expires,
  720. &old->it_value);
  721. } else {
  722. old->it_value.tv_nsec = 1;
  723. old->it_value.tv_sec = 0;
  724. }
  725. }
  726. }
  727. if (unlikely(timer->it.cpu.firing)) {
  728. /*
  729. * We are colliding with the timer actually firing.
  730. * Punt after filling in the timer's old value, and
  731. * disable this firing since we are already reporting
  732. * it as an overrun (thanks to bump_cpu_timer above).
  733. */
  734. read_unlock(&tasklist_lock);
  735. timer->it.cpu.firing = -1;
  736. ret = TIMER_RETRY;
  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. struct list_head *timers = tsk->cpu_timers;
  887. tsk->it_prof_expires = cputime_zero;
  888. while (!list_empty(timers)) {
  889. struct cpu_timer_list *t = list_entry(timers->next,
  890. struct cpu_timer_list,
  891. entry);
  892. if (cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
  893. tsk->it_prof_expires = t->expires.cpu;
  894. break;
  895. }
  896. t->firing = 1;
  897. list_move_tail(&t->entry, firing);
  898. }
  899. ++timers;
  900. tsk->it_virt_expires = cputime_zero;
  901. while (!list_empty(timers)) {
  902. struct cpu_timer_list *t = list_entry(timers->next,
  903. struct cpu_timer_list,
  904. entry);
  905. if (cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
  906. tsk->it_virt_expires = t->expires.cpu;
  907. break;
  908. }
  909. t->firing = 1;
  910. list_move_tail(&t->entry, firing);
  911. }
  912. ++timers;
  913. tsk->it_sched_expires = 0;
  914. while (!list_empty(timers)) {
  915. struct cpu_timer_list *t = list_entry(timers->next,
  916. struct cpu_timer_list,
  917. entry);
  918. if (tsk->sched_time < t->expires.sched) {
  919. tsk->it_sched_expires = t->expires.sched;
  920. break;
  921. }
  922. t->firing = 1;
  923. list_move_tail(&t->entry, firing);
  924. }
  925. }
  926. /*
  927. * Check for any per-thread CPU timers that have fired and move them
  928. * off the tsk->*_timers list onto the firing list. Per-thread timers
  929. * have already been taken off.
  930. */
  931. static void check_process_timers(struct task_struct *tsk,
  932. struct list_head *firing)
  933. {
  934. struct signal_struct *const sig = tsk->signal;
  935. cputime_t utime, stime, ptime, virt_expires, prof_expires;
  936. unsigned long long sched_time, sched_expires;
  937. struct task_struct *t;
  938. struct list_head *timers = sig->cpu_timers;
  939. /*
  940. * Don't sample the current process CPU clocks if there are no timers.
  941. */
  942. if (list_empty(&timers[CPUCLOCK_PROF]) &&
  943. cputime_eq(sig->it_prof_expires, cputime_zero) &&
  944. sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
  945. list_empty(&timers[CPUCLOCK_VIRT]) &&
  946. cputime_eq(sig->it_virt_expires, cputime_zero) &&
  947. list_empty(&timers[CPUCLOCK_SCHED]))
  948. return;
  949. /*
  950. * Collect the current process totals.
  951. */
  952. utime = sig->utime;
  953. stime = sig->stime;
  954. sched_time = sig->sched_time;
  955. t = tsk;
  956. do {
  957. utime = cputime_add(utime, t->utime);
  958. stime = cputime_add(stime, t->stime);
  959. sched_time += t->sched_time;
  960. t = next_thread(t);
  961. } while (t != tsk);
  962. ptime = cputime_add(utime, stime);
  963. prof_expires = cputime_zero;
  964. while (!list_empty(timers)) {
  965. struct cpu_timer_list *t = list_entry(timers->next,
  966. struct cpu_timer_list,
  967. entry);
  968. if (cputime_lt(ptime, t->expires.cpu)) {
  969. prof_expires = t->expires.cpu;
  970. break;
  971. }
  972. t->firing = 1;
  973. list_move_tail(&t->entry, firing);
  974. }
  975. ++timers;
  976. virt_expires = cputime_zero;
  977. while (!list_empty(timers)) {
  978. struct cpu_timer_list *t = list_entry(timers->next,
  979. struct cpu_timer_list,
  980. entry);
  981. if (cputime_lt(utime, t->expires.cpu)) {
  982. virt_expires = t->expires.cpu;
  983. break;
  984. }
  985. t->firing = 1;
  986. list_move_tail(&t->entry, firing);
  987. }
  988. ++timers;
  989. sched_expires = 0;
  990. while (!list_empty(timers)) {
  991. struct cpu_timer_list *t = list_entry(timers->next,
  992. struct cpu_timer_list,
  993. entry);
  994. if (sched_time < t->expires.sched) {
  995. sched_expires = t->expires.sched;
  996. break;
  997. }
  998. t->firing = 1;
  999. list_move_tail(&t->entry, firing);
  1000. }
  1001. /*
  1002. * Check for the special case process timers.
  1003. */
  1004. if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
  1005. if (cputime_ge(ptime, sig->it_prof_expires)) {
  1006. /* ITIMER_PROF fires and reloads. */
  1007. sig->it_prof_expires = sig->it_prof_incr;
  1008. if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
  1009. sig->it_prof_expires = cputime_add(
  1010. sig->it_prof_expires, ptime);
  1011. }
  1012. __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
  1013. }
  1014. if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
  1015. (cputime_eq(prof_expires, cputime_zero) ||
  1016. cputime_lt(sig->it_prof_expires, prof_expires))) {
  1017. prof_expires = sig->it_prof_expires;
  1018. }
  1019. }
  1020. if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
  1021. if (cputime_ge(utime, sig->it_virt_expires)) {
  1022. /* ITIMER_VIRTUAL fires and reloads. */
  1023. sig->it_virt_expires = sig->it_virt_incr;
  1024. if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
  1025. sig->it_virt_expires = cputime_add(
  1026. sig->it_virt_expires, utime);
  1027. }
  1028. __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
  1029. }
  1030. if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
  1031. (cputime_eq(virt_expires, cputime_zero) ||
  1032. cputime_lt(sig->it_virt_expires, virt_expires))) {
  1033. virt_expires = sig->it_virt_expires;
  1034. }
  1035. }
  1036. if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
  1037. unsigned long psecs = cputime_to_secs(ptime);
  1038. cputime_t x;
  1039. if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
  1040. /*
  1041. * At the hard limit, we just die.
  1042. * No need to calculate anything else now.
  1043. */
  1044. __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
  1045. return;
  1046. }
  1047. if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
  1048. /*
  1049. * At the soft limit, send a SIGXCPU every second.
  1050. */
  1051. __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
  1052. if (sig->rlim[RLIMIT_CPU].rlim_cur
  1053. < sig->rlim[RLIMIT_CPU].rlim_max) {
  1054. sig->rlim[RLIMIT_CPU].rlim_cur++;
  1055. }
  1056. }
  1057. x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
  1058. if (cputime_eq(prof_expires, cputime_zero) ||
  1059. cputime_lt(x, prof_expires)) {
  1060. prof_expires = x;
  1061. }
  1062. }
  1063. if (!cputime_eq(prof_expires, cputime_zero) ||
  1064. !cputime_eq(virt_expires, cputime_zero) ||
  1065. sched_expires != 0) {
  1066. /*
  1067. * Rebalance the threads' expiry times for the remaining
  1068. * process CPU timers.
  1069. */
  1070. cputime_t prof_left, virt_left, ticks;
  1071. unsigned long long sched_left, sched;
  1072. const unsigned int nthreads = atomic_read(&sig->live);
  1073. prof_left = cputime_sub(prof_expires, utime);
  1074. prof_left = cputime_sub(prof_left, stime);
  1075. prof_left = cputime_div(prof_left, nthreads);
  1076. virt_left = cputime_sub(virt_expires, utime);
  1077. virt_left = cputime_div(virt_left, nthreads);
  1078. if (sched_expires) {
  1079. sched_left = sched_expires - sched_time;
  1080. do_div(sched_left, nthreads);
  1081. } else {
  1082. sched_left = 0;
  1083. }
  1084. t = tsk;
  1085. do {
  1086. ticks = cputime_add(cputime_add(t->utime, t->stime),
  1087. prof_left);
  1088. if (!cputime_eq(prof_expires, cputime_zero) &&
  1089. (cputime_eq(t->it_prof_expires, cputime_zero) ||
  1090. cputime_gt(t->it_prof_expires, ticks))) {
  1091. t->it_prof_expires = ticks;
  1092. }
  1093. ticks = cputime_add(t->utime, virt_left);
  1094. if (!cputime_eq(virt_expires, cputime_zero) &&
  1095. (cputime_eq(t->it_virt_expires, cputime_zero) ||
  1096. cputime_gt(t->it_virt_expires, ticks))) {
  1097. t->it_virt_expires = ticks;
  1098. }
  1099. sched = t->sched_time + sched_left;
  1100. if (sched_expires && (t->it_sched_expires == 0 ||
  1101. t->it_sched_expires > sched)) {
  1102. t->it_sched_expires = sched;
  1103. }
  1104. do {
  1105. t = next_thread(t);
  1106. } while (unlikely(t->exit_state));
  1107. } while (t != tsk);
  1108. }
  1109. }
  1110. /*
  1111. * This is called from the signal code (via do_schedule_next_timer)
  1112. * when the last timer signal was delivered and we have to reload the timer.
  1113. */
  1114. void posix_cpu_timer_schedule(struct k_itimer *timer)
  1115. {
  1116. struct task_struct *p = timer->it.cpu.task;
  1117. union cpu_time_count now;
  1118. if (unlikely(p == NULL))
  1119. /*
  1120. * The task was cleaned up already, no future firings.
  1121. */
  1122. return;
  1123. /*
  1124. * Fetch the current sample and update the timer's expiry time.
  1125. */
  1126. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  1127. cpu_clock_sample(timer->it_clock, p, &now);
  1128. bump_cpu_timer(timer, now);
  1129. if (unlikely(p->exit_state)) {
  1130. clear_dead_task(timer, now);
  1131. return;
  1132. }
  1133. read_lock(&tasklist_lock); /* arm_timer needs it. */
  1134. } else {
  1135. read_lock(&tasklist_lock);
  1136. if (unlikely(p->signal == NULL)) {
  1137. /*
  1138. * The process has been reaped.
  1139. * We can't even collect a sample any more.
  1140. */
  1141. put_task_struct(p);
  1142. timer->it.cpu.task = p = NULL;
  1143. timer->it.cpu.expires.sched = 0;
  1144. read_unlock(&tasklist_lock);
  1145. return;
  1146. } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
  1147. /*
  1148. * We've noticed that the thread is dead, but
  1149. * not yet reaped. Take this opportunity to
  1150. * drop our task ref.
  1151. */
  1152. clear_dead_task(timer, now);
  1153. read_unlock(&tasklist_lock);
  1154. return;
  1155. }
  1156. cpu_clock_sample_group(timer->it_clock, p, &now);
  1157. bump_cpu_timer(timer, now);
  1158. /* Leave the tasklist_lock locked for the call below. */
  1159. }
  1160. /*
  1161. * Now re-arm for the new expiry time.
  1162. */
  1163. arm_timer(timer, now);
  1164. read_unlock(&tasklist_lock);
  1165. }
  1166. /*
  1167. * This is called from the timer interrupt handler. The irq handler has
  1168. * already updated our counts. We need to check if any timers fire now.
  1169. * Interrupts are disabled.
  1170. */
  1171. void run_posix_cpu_timers(struct task_struct *tsk)
  1172. {
  1173. LIST_HEAD(firing);
  1174. struct k_itimer *timer, *next;
  1175. BUG_ON(!irqs_disabled());
  1176. #define UNEXPIRED(clock) \
  1177. (cputime_eq(tsk->it_##clock##_expires, cputime_zero) || \
  1178. cputime_lt(clock##_ticks(tsk), tsk->it_##clock##_expires))
  1179. if (UNEXPIRED(prof) && UNEXPIRED(virt) &&
  1180. (tsk->it_sched_expires == 0 ||
  1181. tsk->sched_time < tsk->it_sched_expires))
  1182. return;
  1183. #undef UNEXPIRED
  1184. BUG_ON(tsk->exit_state);
  1185. /*
  1186. * Double-check with locks held.
  1187. */
  1188. read_lock(&tasklist_lock);
  1189. spin_lock(&tsk->sighand->siglock);
  1190. /*
  1191. * Here we take off tsk->cpu_timers[N] and tsk->signal->cpu_timers[N]
  1192. * all the timers that are firing, and put them on the firing list.
  1193. */
  1194. check_thread_timers(tsk, &firing);
  1195. check_process_timers(tsk, &firing);
  1196. /*
  1197. * We must release these locks before taking any timer's lock.
  1198. * There is a potential race with timer deletion here, as the
  1199. * siglock now protects our private firing list. We have set
  1200. * the firing flag in each timer, so that a deletion attempt
  1201. * that gets the timer lock before we do will give it up and
  1202. * spin until we've taken care of that timer below.
  1203. */
  1204. spin_unlock(&tsk->sighand->siglock);
  1205. read_unlock(&tasklist_lock);
  1206. /*
  1207. * Now that all the timers on our list have the firing flag,
  1208. * noone will touch their list entries but us. We'll take
  1209. * each timer's lock before clearing its firing flag, so no
  1210. * timer call will interfere.
  1211. */
  1212. list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
  1213. int firing;
  1214. spin_lock(&timer->it_lock);
  1215. list_del_init(&timer->it.cpu.entry);
  1216. firing = timer->it.cpu.firing;
  1217. timer->it.cpu.firing = 0;
  1218. /*
  1219. * The firing flag is -1 if we collided with a reset
  1220. * of the timer, which already reported this
  1221. * almost-firing as an overrun. So don't generate an event.
  1222. */
  1223. if (likely(firing >= 0)) {
  1224. cpu_timer_fire(timer);
  1225. }
  1226. spin_unlock(&timer->it_lock);
  1227. }
  1228. }
  1229. /*
  1230. * Set one of the process-wide special case CPU timers.
  1231. * The tasklist_lock and tsk->sighand->siglock must be held by the caller.
  1232. * The oldval argument is null for the RLIMIT_CPU timer, where *newval is
  1233. * absolute; non-null for ITIMER_*, where *newval is relative and we update
  1234. * it to be absolute, *oldval is absolute and we update it to be relative.
  1235. */
  1236. void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
  1237. cputime_t *newval, cputime_t *oldval)
  1238. {
  1239. union cpu_time_count now;
  1240. struct list_head *head;
  1241. BUG_ON(clock_idx == CPUCLOCK_SCHED);
  1242. cpu_clock_sample_group_locked(clock_idx, tsk, &now);
  1243. if (oldval) {
  1244. if (!cputime_eq(*oldval, cputime_zero)) {
  1245. if (cputime_le(*oldval, now.cpu)) {
  1246. /* Just about to fire. */
  1247. *oldval = jiffies_to_cputime(1);
  1248. } else {
  1249. *oldval = cputime_sub(*oldval, now.cpu);
  1250. }
  1251. }
  1252. if (cputime_eq(*newval, cputime_zero))
  1253. return;
  1254. *newval = cputime_add(*newval, now.cpu);
  1255. /*
  1256. * If the RLIMIT_CPU timer will expire before the
  1257. * ITIMER_PROF timer, we have nothing else to do.
  1258. */
  1259. if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
  1260. < cputime_to_secs(*newval))
  1261. return;
  1262. }
  1263. /*
  1264. * Check whether there are any process timers already set to fire
  1265. * before this one. If so, we don't have anything more to do.
  1266. */
  1267. head = &tsk->signal->cpu_timers[clock_idx];
  1268. if (list_empty(head) ||
  1269. cputime_ge(list_entry(head->next,
  1270. struct cpu_timer_list, entry)->expires.cpu,
  1271. *newval)) {
  1272. /*
  1273. * Rejigger each thread's expiry time so that one will
  1274. * notice before we hit the process-cumulative expiry time.
  1275. */
  1276. union cpu_time_count expires = { .sched = 0 };
  1277. expires.cpu = *newval;
  1278. process_timer_rebalance(tsk, clock_idx, expires, now);
  1279. }
  1280. }
  1281. static long posix_cpu_clock_nanosleep_restart(struct restart_block *);
  1282. int posix_cpu_nsleep(clockid_t which_clock, int flags,
  1283. struct timespec *rqtp)
  1284. {
  1285. struct restart_block *restart_block =
  1286. &current_thread_info()->restart_block;
  1287. struct k_itimer timer;
  1288. int error;
  1289. /*
  1290. * Diagnose required errors first.
  1291. */
  1292. if (CPUCLOCK_PERTHREAD(which_clock) &&
  1293. (CPUCLOCK_PID(which_clock) == 0 ||
  1294. CPUCLOCK_PID(which_clock) == current->pid))
  1295. return -EINVAL;
  1296. /*
  1297. * Set up a temporary timer and then wait for it to go off.
  1298. */
  1299. memset(&timer, 0, sizeof timer);
  1300. spin_lock_init(&timer.it_lock);
  1301. timer.it_clock = which_clock;
  1302. timer.it_overrun = -1;
  1303. error = posix_cpu_timer_create(&timer);
  1304. timer.it_process = current;
  1305. if (!error) {
  1306. struct timespec __user *rmtp;
  1307. static struct itimerspec zero_it;
  1308. struct itimerspec it = { .it_value = *rqtp,
  1309. .it_interval = {} };
  1310. spin_lock_irq(&timer.it_lock);
  1311. error = posix_cpu_timer_set(&timer, flags, &it, NULL);
  1312. if (error) {
  1313. spin_unlock_irq(&timer.it_lock);
  1314. return error;
  1315. }
  1316. while (!signal_pending(current)) {
  1317. if (timer.it.cpu.expires.sched == 0) {
  1318. /*
  1319. * Our timer fired and was reset.
  1320. */
  1321. spin_unlock_irq(&timer.it_lock);
  1322. return 0;
  1323. }
  1324. /*
  1325. * Block until cpu_timer_fire (or a signal) wakes us.
  1326. */
  1327. __set_current_state(TASK_INTERRUPTIBLE);
  1328. spin_unlock_irq(&timer.it_lock);
  1329. schedule();
  1330. spin_lock_irq(&timer.it_lock);
  1331. }
  1332. /*
  1333. * We were interrupted by a signal.
  1334. */
  1335. sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
  1336. posix_cpu_timer_set(&timer, 0, &zero_it, &it);
  1337. spin_unlock_irq(&timer.it_lock);
  1338. if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
  1339. /*
  1340. * It actually did fire already.
  1341. */
  1342. return 0;
  1343. }
  1344. /*
  1345. * Report back to the user the time still remaining.
  1346. */
  1347. rmtp = (struct timespec __user *) restart_block->arg1;
  1348. if (rmtp != NULL && !(flags & TIMER_ABSTIME) &&
  1349. copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
  1350. return -EFAULT;
  1351. restart_block->fn = posix_cpu_clock_nanosleep_restart;
  1352. /* Caller already set restart_block->arg1 */
  1353. restart_block->arg0 = which_clock;
  1354. restart_block->arg2 = rqtp->tv_sec;
  1355. restart_block->arg3 = rqtp->tv_nsec;
  1356. error = -ERESTART_RESTARTBLOCK;
  1357. }
  1358. return error;
  1359. }
  1360. static long
  1361. posix_cpu_clock_nanosleep_restart(struct restart_block *restart_block)
  1362. {
  1363. clockid_t which_clock = restart_block->arg0;
  1364. struct timespec t = { .tv_sec = restart_block->arg2,
  1365. .tv_nsec = restart_block->arg3 };
  1366. restart_block->fn = do_no_restart_syscall;
  1367. return posix_cpu_nsleep(which_clock, TIMER_ABSTIME, &t);
  1368. }
  1369. #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
  1370. #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
  1371. static int process_cpu_clock_getres(clockid_t which_clock, struct timespec *tp)
  1372. {
  1373. return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
  1374. }
  1375. static int process_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
  1376. {
  1377. return posix_cpu_clock_get(PROCESS_CLOCK, tp);
  1378. }
  1379. static int process_cpu_timer_create(struct k_itimer *timer)
  1380. {
  1381. timer->it_clock = PROCESS_CLOCK;
  1382. return posix_cpu_timer_create(timer);
  1383. }
  1384. static int process_cpu_nsleep(clockid_t which_clock, int flags,
  1385. struct timespec *rqtp)
  1386. {
  1387. return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
  1388. }
  1389. static int thread_cpu_clock_getres(clockid_t which_clock, struct timespec *tp)
  1390. {
  1391. return posix_cpu_clock_getres(THREAD_CLOCK, tp);
  1392. }
  1393. static int thread_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
  1394. {
  1395. return posix_cpu_clock_get(THREAD_CLOCK, tp);
  1396. }
  1397. static int thread_cpu_timer_create(struct k_itimer *timer)
  1398. {
  1399. timer->it_clock = THREAD_CLOCK;
  1400. return posix_cpu_timer_create(timer);
  1401. }
  1402. static int thread_cpu_nsleep(clockid_t which_clock, int flags,
  1403. struct timespec *rqtp)
  1404. {
  1405. return -EINVAL;
  1406. }
  1407. static __init int init_posix_cpu_timers(void)
  1408. {
  1409. struct k_clock process = {
  1410. .clock_getres = process_cpu_clock_getres,
  1411. .clock_get = process_cpu_clock_get,
  1412. .clock_set = do_posix_clock_nosettime,
  1413. .timer_create = process_cpu_timer_create,
  1414. .nsleep = process_cpu_nsleep,
  1415. };
  1416. struct k_clock thread = {
  1417. .clock_getres = thread_cpu_clock_getres,
  1418. .clock_get = thread_cpu_clock_get,
  1419. .clock_set = do_posix_clock_nosettime,
  1420. .timer_create = thread_cpu_timer_create,
  1421. .nsleep = thread_cpu_nsleep,
  1422. };
  1423. register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
  1424. register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
  1425. return 0;
  1426. }
  1427. __initcall(init_posix_cpu_timers);