posix-cpu-timers.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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. timer->task = NULL;
  390. list_del_init(&timer->entry);
  391. if (cputime_lt(timer->expires.cpu, ptime)) {
  392. timer->expires.cpu = cputime_zero;
  393. } else {
  394. timer->expires.cpu = cputime_sub(timer->expires.cpu,
  395. ptime);
  396. }
  397. }
  398. ++head;
  399. list_for_each_entry_safe(timer, next, head, entry) {
  400. timer->task = NULL;
  401. list_del_init(&timer->entry);
  402. if (cputime_lt(timer->expires.cpu, utime)) {
  403. timer->expires.cpu = cputime_zero;
  404. } else {
  405. timer->expires.cpu = cputime_sub(timer->expires.cpu,
  406. utime);
  407. }
  408. }
  409. ++head;
  410. list_for_each_entry_safe(timer, next, head, entry) {
  411. timer->task = NULL;
  412. list_del_init(&timer->entry);
  413. if (timer->expires.sched < sched_time) {
  414. timer->expires.sched = 0;
  415. } else {
  416. timer->expires.sched -= sched_time;
  417. }
  418. }
  419. }
  420. /*
  421. * These are both called with the siglock held, when the current thread
  422. * is being reaped. When the final (leader) thread in the group is reaped,
  423. * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
  424. */
  425. void posix_cpu_timers_exit(struct task_struct *tsk)
  426. {
  427. cleanup_timers(tsk->cpu_timers,
  428. tsk->utime, tsk->stime, tsk->sched_time);
  429. }
  430. void posix_cpu_timers_exit_group(struct task_struct *tsk)
  431. {
  432. cleanup_timers(tsk->signal->cpu_timers,
  433. cputime_add(tsk->utime, tsk->signal->utime),
  434. cputime_add(tsk->stime, tsk->signal->stime),
  435. tsk->sched_time + tsk->signal->sched_time);
  436. }
  437. /*
  438. * Set the expiry times of all the threads in the process so one of them
  439. * will go off before the process cumulative expiry total is reached.
  440. */
  441. static void process_timer_rebalance(struct task_struct *p,
  442. unsigned int clock_idx,
  443. union cpu_time_count expires,
  444. union cpu_time_count val)
  445. {
  446. cputime_t ticks, left;
  447. unsigned long long ns, nsleft;
  448. struct task_struct *t = p;
  449. unsigned int nthreads = atomic_read(&p->signal->live);
  450. switch (clock_idx) {
  451. default:
  452. BUG();
  453. break;
  454. case CPUCLOCK_PROF:
  455. left = cputime_div(cputime_sub(expires.cpu, val.cpu),
  456. nthreads);
  457. do {
  458. if (!unlikely(t->exit_state)) {
  459. ticks = cputime_add(prof_ticks(t), left);
  460. if (cputime_eq(t->it_prof_expires,
  461. cputime_zero) ||
  462. cputime_gt(t->it_prof_expires, ticks)) {
  463. t->it_prof_expires = ticks;
  464. }
  465. }
  466. t = next_thread(t);
  467. } while (t != p);
  468. break;
  469. case CPUCLOCK_VIRT:
  470. left = cputime_div(cputime_sub(expires.cpu, val.cpu),
  471. nthreads);
  472. do {
  473. if (!unlikely(t->exit_state)) {
  474. ticks = cputime_add(virt_ticks(t), left);
  475. if (cputime_eq(t->it_virt_expires,
  476. cputime_zero) ||
  477. cputime_gt(t->it_virt_expires, ticks)) {
  478. t->it_virt_expires = ticks;
  479. }
  480. }
  481. t = next_thread(t);
  482. } while (t != p);
  483. break;
  484. case CPUCLOCK_SCHED:
  485. nsleft = expires.sched - val.sched;
  486. do_div(nsleft, nthreads);
  487. do {
  488. if (!unlikely(t->exit_state)) {
  489. ns = t->sched_time + nsleft;
  490. if (t->it_sched_expires == 0 ||
  491. t->it_sched_expires > ns) {
  492. t->it_sched_expires = ns;
  493. }
  494. }
  495. t = next_thread(t);
  496. } while (t != p);
  497. break;
  498. }
  499. }
  500. static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
  501. {
  502. /*
  503. * That's all for this thread or process.
  504. * We leave our residual in expires to be reported.
  505. */
  506. put_task_struct(timer->it.cpu.task);
  507. timer->it.cpu.task = NULL;
  508. timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
  509. timer->it.cpu.expires,
  510. now);
  511. }
  512. /*
  513. * Insert the timer on the appropriate list before any timers that
  514. * expire later. This must be called with the tasklist_lock held
  515. * for reading, and interrupts disabled.
  516. */
  517. static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
  518. {
  519. struct task_struct *p = timer->it.cpu.task;
  520. struct list_head *head, *listpos;
  521. struct cpu_timer_list *const nt = &timer->it.cpu;
  522. struct cpu_timer_list *next;
  523. unsigned long i;
  524. head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
  525. p->cpu_timers : p->signal->cpu_timers);
  526. head += CPUCLOCK_WHICH(timer->it_clock);
  527. BUG_ON(!irqs_disabled());
  528. spin_lock(&p->sighand->siglock);
  529. listpos = head;
  530. if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
  531. list_for_each_entry(next, head, entry) {
  532. if (next->expires.sched > nt->expires.sched) {
  533. listpos = &next->entry;
  534. break;
  535. }
  536. }
  537. } else {
  538. list_for_each_entry(next, head, entry) {
  539. if (cputime_gt(next->expires.cpu, nt->expires.cpu)) {
  540. listpos = &next->entry;
  541. break;
  542. }
  543. }
  544. }
  545. list_add(&nt->entry, listpos);
  546. if (listpos == head) {
  547. /*
  548. * We are the new earliest-expiring timer.
  549. * If we are a thread timer, there can always
  550. * be a process timer telling us to stop earlier.
  551. */
  552. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  553. switch (CPUCLOCK_WHICH(timer->it_clock)) {
  554. default:
  555. BUG();
  556. case CPUCLOCK_PROF:
  557. if (cputime_eq(p->it_prof_expires,
  558. cputime_zero) ||
  559. cputime_gt(p->it_prof_expires,
  560. nt->expires.cpu))
  561. p->it_prof_expires = nt->expires.cpu;
  562. break;
  563. case CPUCLOCK_VIRT:
  564. if (cputime_eq(p->it_virt_expires,
  565. cputime_zero) ||
  566. cputime_gt(p->it_virt_expires,
  567. nt->expires.cpu))
  568. p->it_virt_expires = nt->expires.cpu;
  569. break;
  570. case CPUCLOCK_SCHED:
  571. if (p->it_sched_expires == 0 ||
  572. p->it_sched_expires > nt->expires.sched)
  573. p->it_sched_expires = nt->expires.sched;
  574. break;
  575. }
  576. } else {
  577. /*
  578. * For a process timer, we must balance
  579. * all the live threads' expirations.
  580. */
  581. switch (CPUCLOCK_WHICH(timer->it_clock)) {
  582. default:
  583. BUG();
  584. case CPUCLOCK_VIRT:
  585. if (!cputime_eq(p->signal->it_virt_expires,
  586. cputime_zero) &&
  587. cputime_lt(p->signal->it_virt_expires,
  588. timer->it.cpu.expires.cpu))
  589. break;
  590. goto rebalance;
  591. case CPUCLOCK_PROF:
  592. if (!cputime_eq(p->signal->it_prof_expires,
  593. cputime_zero) &&
  594. cputime_lt(p->signal->it_prof_expires,
  595. timer->it.cpu.expires.cpu))
  596. break;
  597. i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
  598. if (i != RLIM_INFINITY &&
  599. i <= cputime_to_secs(timer->it.cpu.expires.cpu))
  600. break;
  601. goto rebalance;
  602. case CPUCLOCK_SCHED:
  603. rebalance:
  604. process_timer_rebalance(
  605. timer->it.cpu.task,
  606. CPUCLOCK_WHICH(timer->it_clock),
  607. timer->it.cpu.expires, now);
  608. break;
  609. }
  610. }
  611. }
  612. spin_unlock(&p->sighand->siglock);
  613. }
  614. /*
  615. * The timer is locked, fire it and arrange for its reload.
  616. */
  617. static void cpu_timer_fire(struct k_itimer *timer)
  618. {
  619. if (unlikely(timer->sigq == NULL)) {
  620. /*
  621. * This a special case for clock_nanosleep,
  622. * not a normal timer from sys_timer_create.
  623. */
  624. wake_up_process(timer->it_process);
  625. timer->it.cpu.expires.sched = 0;
  626. } else if (timer->it.cpu.incr.sched == 0) {
  627. /*
  628. * One-shot timer. Clear it as soon as it's fired.
  629. */
  630. posix_timer_event(timer, 0);
  631. timer->it.cpu.expires.sched = 0;
  632. } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
  633. /*
  634. * The signal did not get queued because the signal
  635. * was ignored, so we won't get any callback to
  636. * reload the timer. But we need to keep it
  637. * ticking in case the signal is deliverable next time.
  638. */
  639. posix_cpu_timer_schedule(timer);
  640. }
  641. }
  642. /*
  643. * Guts of sys_timer_settime for CPU timers.
  644. * This is called with the timer locked and interrupts disabled.
  645. * If we return TIMER_RETRY, it's necessary to release the timer's lock
  646. * and try again. (This happens when the timer is in the middle of firing.)
  647. */
  648. int posix_cpu_timer_set(struct k_itimer *timer, int flags,
  649. struct itimerspec *new, struct itimerspec *old)
  650. {
  651. struct task_struct *p = timer->it.cpu.task;
  652. union cpu_time_count old_expires, new_expires, val;
  653. int ret;
  654. if (unlikely(p == NULL)) {
  655. /*
  656. * Timer refers to a dead task's clock.
  657. */
  658. return -ESRCH;
  659. }
  660. new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
  661. read_lock(&tasklist_lock);
  662. /*
  663. * We need the tasklist_lock to protect against reaping that
  664. * clears p->signal. If p has just been reaped, we can no
  665. * longer get any information about it at all.
  666. */
  667. if (unlikely(p->signal == NULL)) {
  668. read_unlock(&tasklist_lock);
  669. put_task_struct(p);
  670. timer->it.cpu.task = NULL;
  671. return -ESRCH;
  672. }
  673. /*
  674. * Disarm any old timer after extracting its expiry time.
  675. */
  676. BUG_ON(!irqs_disabled());
  677. spin_lock(&p->sighand->siglock);
  678. old_expires = timer->it.cpu.expires;
  679. list_del_init(&timer->it.cpu.entry);
  680. spin_unlock(&p->sighand->siglock);
  681. /*
  682. * We need to sample the current value to convert the new
  683. * value from to relative and absolute, and to convert the
  684. * old value from absolute to relative. To set a process
  685. * timer, we need a sample to balance the thread expiry
  686. * times (in arm_timer). With an absolute time, we must
  687. * check if it's already passed. In short, we need a sample.
  688. */
  689. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  690. cpu_clock_sample(timer->it_clock, p, &val);
  691. } else {
  692. cpu_clock_sample_group(timer->it_clock, p, &val);
  693. }
  694. if (old) {
  695. if (old_expires.sched == 0) {
  696. old->it_value.tv_sec = 0;
  697. old->it_value.tv_nsec = 0;
  698. } else {
  699. /*
  700. * Update the timer in case it has
  701. * overrun already. If it has,
  702. * we'll report it as having overrun
  703. * and with the next reloaded timer
  704. * already ticking, though we are
  705. * swallowing that pending
  706. * notification here to install the
  707. * new setting.
  708. */
  709. bump_cpu_timer(timer, val);
  710. if (cpu_time_before(timer->it_clock, val,
  711. timer->it.cpu.expires)) {
  712. old_expires = cpu_time_sub(
  713. timer->it_clock,
  714. timer->it.cpu.expires, val);
  715. sample_to_timespec(timer->it_clock,
  716. old_expires,
  717. &old->it_value);
  718. } else {
  719. old->it_value.tv_nsec = 1;
  720. old->it_value.tv_sec = 0;
  721. }
  722. }
  723. }
  724. if (unlikely(timer->it.cpu.firing)) {
  725. /*
  726. * We are colliding with the timer actually firing.
  727. * Punt after filling in the timer's old value, and
  728. * disable this firing since we are already reporting
  729. * it as an overrun (thanks to bump_cpu_timer above).
  730. */
  731. read_unlock(&tasklist_lock);
  732. timer->it.cpu.firing = -1;
  733. ret = TIMER_RETRY;
  734. goto out;
  735. }
  736. if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
  737. cpu_time_add(timer->it_clock, &new_expires, val);
  738. }
  739. /*
  740. * Install the new expiry time (or zero).
  741. * For a timer with no notification action, we don't actually
  742. * arm the timer (we'll just fake it for timer_gettime).
  743. */
  744. timer->it.cpu.expires = new_expires;
  745. if (new_expires.sched != 0 &&
  746. (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
  747. cpu_time_before(timer->it_clock, val, new_expires)) {
  748. arm_timer(timer, val);
  749. }
  750. read_unlock(&tasklist_lock);
  751. /*
  752. * Install the new reload setting, and
  753. * set up the signal and overrun bookkeeping.
  754. */
  755. timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
  756. &new->it_interval);
  757. /*
  758. * This acts as a modification timestamp for the timer,
  759. * so any automatic reload attempt will punt on seeing
  760. * that we have reset the timer manually.
  761. */
  762. timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
  763. ~REQUEUE_PENDING;
  764. timer->it_overrun_last = 0;
  765. timer->it_overrun = -1;
  766. if (new_expires.sched != 0 &&
  767. (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
  768. !cpu_time_before(timer->it_clock, val, new_expires)) {
  769. /*
  770. * The designated time already passed, so we notify
  771. * immediately, even if the thread never runs to
  772. * accumulate more time on this clock.
  773. */
  774. cpu_timer_fire(timer);
  775. }
  776. ret = 0;
  777. out:
  778. if (old) {
  779. sample_to_timespec(timer->it_clock,
  780. timer->it.cpu.incr, &old->it_interval);
  781. }
  782. return ret;
  783. }
  784. void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
  785. {
  786. union cpu_time_count now;
  787. struct task_struct *p = timer->it.cpu.task;
  788. int clear_dead;
  789. /*
  790. * Easy part: convert the reload time.
  791. */
  792. sample_to_timespec(timer->it_clock,
  793. timer->it.cpu.incr, &itp->it_interval);
  794. if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
  795. itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
  796. return;
  797. }
  798. if (unlikely(p == NULL)) {
  799. /*
  800. * This task already died and the timer will never fire.
  801. * In this case, expires is actually the dead value.
  802. */
  803. dead:
  804. sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
  805. &itp->it_value);
  806. return;
  807. }
  808. /*
  809. * Sample the clock to take the difference with the expiry time.
  810. */
  811. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  812. cpu_clock_sample(timer->it_clock, p, &now);
  813. clear_dead = p->exit_state;
  814. } else {
  815. read_lock(&tasklist_lock);
  816. if (unlikely(p->signal == NULL)) {
  817. /*
  818. * The process has been reaped.
  819. * We can't even collect a sample any more.
  820. * Call the timer disarmed, nothing else to do.
  821. */
  822. put_task_struct(p);
  823. timer->it.cpu.task = NULL;
  824. timer->it.cpu.expires.sched = 0;
  825. read_unlock(&tasklist_lock);
  826. goto dead;
  827. } else {
  828. cpu_clock_sample_group(timer->it_clock, p, &now);
  829. clear_dead = (unlikely(p->exit_state) &&
  830. thread_group_empty(p));
  831. }
  832. read_unlock(&tasklist_lock);
  833. }
  834. if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
  835. if (timer->it.cpu.incr.sched == 0 &&
  836. cpu_time_before(timer->it_clock,
  837. timer->it.cpu.expires, now)) {
  838. /*
  839. * Do-nothing timer expired and has no reload,
  840. * so it's as if it was never set.
  841. */
  842. timer->it.cpu.expires.sched = 0;
  843. itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
  844. return;
  845. }
  846. /*
  847. * Account for any expirations and reloads that should
  848. * have happened.
  849. */
  850. bump_cpu_timer(timer, now);
  851. }
  852. if (unlikely(clear_dead)) {
  853. /*
  854. * We've noticed that the thread is dead, but
  855. * not yet reaped. Take this opportunity to
  856. * drop our task ref.
  857. */
  858. clear_dead_task(timer, now);
  859. goto dead;
  860. }
  861. if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
  862. sample_to_timespec(timer->it_clock,
  863. cpu_time_sub(timer->it_clock,
  864. timer->it.cpu.expires, now),
  865. &itp->it_value);
  866. } else {
  867. /*
  868. * The timer should have expired already, but the firing
  869. * hasn't taken place yet. Say it's just about to expire.
  870. */
  871. itp->it_value.tv_nsec = 1;
  872. itp->it_value.tv_sec = 0;
  873. }
  874. }
  875. /*
  876. * Check for any per-thread CPU timers that have fired and move them off
  877. * the tsk->cpu_timers[N] list onto the firing list. Here we update the
  878. * tsk->it_*_expires values to reflect the remaining thread CPU timers.
  879. */
  880. static void check_thread_timers(struct task_struct *tsk,
  881. struct list_head *firing)
  882. {
  883. struct list_head *timers = tsk->cpu_timers;
  884. tsk->it_prof_expires = cputime_zero;
  885. while (!list_empty(timers)) {
  886. struct cpu_timer_list *t = list_entry(timers->next,
  887. struct cpu_timer_list,
  888. entry);
  889. if (cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
  890. tsk->it_prof_expires = t->expires.cpu;
  891. break;
  892. }
  893. t->firing = 1;
  894. list_move_tail(&t->entry, firing);
  895. }
  896. ++timers;
  897. tsk->it_virt_expires = cputime_zero;
  898. while (!list_empty(timers)) {
  899. struct cpu_timer_list *t = list_entry(timers->next,
  900. struct cpu_timer_list,
  901. entry);
  902. if (cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
  903. tsk->it_virt_expires = t->expires.cpu;
  904. break;
  905. }
  906. t->firing = 1;
  907. list_move_tail(&t->entry, firing);
  908. }
  909. ++timers;
  910. tsk->it_sched_expires = 0;
  911. while (!list_empty(timers)) {
  912. struct cpu_timer_list *t = list_entry(timers->next,
  913. struct cpu_timer_list,
  914. entry);
  915. if (tsk->sched_time < t->expires.sched) {
  916. tsk->it_sched_expires = t->expires.sched;
  917. break;
  918. }
  919. t->firing = 1;
  920. list_move_tail(&t->entry, firing);
  921. }
  922. }
  923. /*
  924. * Check for any per-thread CPU timers that have fired and move them
  925. * off the tsk->*_timers list onto the firing list. Per-thread timers
  926. * have already been taken off.
  927. */
  928. static void check_process_timers(struct task_struct *tsk,
  929. struct list_head *firing)
  930. {
  931. struct signal_struct *const sig = tsk->signal;
  932. cputime_t utime, stime, ptime, virt_expires, prof_expires;
  933. unsigned long long sched_time, sched_expires;
  934. struct task_struct *t;
  935. struct list_head *timers = sig->cpu_timers;
  936. /*
  937. * Don't sample the current process CPU clocks if there are no timers.
  938. */
  939. if (list_empty(&timers[CPUCLOCK_PROF]) &&
  940. cputime_eq(sig->it_prof_expires, cputime_zero) &&
  941. sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
  942. list_empty(&timers[CPUCLOCK_VIRT]) &&
  943. cputime_eq(sig->it_virt_expires, cputime_zero) &&
  944. list_empty(&timers[CPUCLOCK_SCHED]))
  945. return;
  946. /*
  947. * Collect the current process totals.
  948. */
  949. utime = sig->utime;
  950. stime = sig->stime;
  951. sched_time = sig->sched_time;
  952. t = tsk;
  953. do {
  954. utime = cputime_add(utime, t->utime);
  955. stime = cputime_add(stime, t->stime);
  956. sched_time += t->sched_time;
  957. t = next_thread(t);
  958. } while (t != tsk);
  959. ptime = cputime_add(utime, stime);
  960. prof_expires = cputime_zero;
  961. while (!list_empty(timers)) {
  962. struct cpu_timer_list *t = list_entry(timers->next,
  963. struct cpu_timer_list,
  964. entry);
  965. if (cputime_lt(ptime, t->expires.cpu)) {
  966. prof_expires = t->expires.cpu;
  967. break;
  968. }
  969. t->firing = 1;
  970. list_move_tail(&t->entry, firing);
  971. }
  972. ++timers;
  973. virt_expires = cputime_zero;
  974. while (!list_empty(timers)) {
  975. struct cpu_timer_list *t = list_entry(timers->next,
  976. struct cpu_timer_list,
  977. entry);
  978. if (cputime_lt(utime, t->expires.cpu)) {
  979. virt_expires = t->expires.cpu;
  980. break;
  981. }
  982. t->firing = 1;
  983. list_move_tail(&t->entry, firing);
  984. }
  985. ++timers;
  986. sched_expires = 0;
  987. while (!list_empty(timers)) {
  988. struct cpu_timer_list *t = list_entry(timers->next,
  989. struct cpu_timer_list,
  990. entry);
  991. if (sched_time < t->expires.sched) {
  992. sched_expires = t->expires.sched;
  993. break;
  994. }
  995. t->firing = 1;
  996. list_move_tail(&t->entry, firing);
  997. }
  998. /*
  999. * Check for the special case process timers.
  1000. */
  1001. if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
  1002. if (cputime_ge(ptime, sig->it_prof_expires)) {
  1003. /* ITIMER_PROF fires and reloads. */
  1004. sig->it_prof_expires = sig->it_prof_incr;
  1005. if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
  1006. sig->it_prof_expires = cputime_add(
  1007. sig->it_prof_expires, ptime);
  1008. }
  1009. __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
  1010. }
  1011. if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
  1012. (cputime_eq(prof_expires, cputime_zero) ||
  1013. cputime_lt(sig->it_prof_expires, prof_expires))) {
  1014. prof_expires = sig->it_prof_expires;
  1015. }
  1016. }
  1017. if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
  1018. if (cputime_ge(utime, sig->it_virt_expires)) {
  1019. /* ITIMER_VIRTUAL fires and reloads. */
  1020. sig->it_virt_expires = sig->it_virt_incr;
  1021. if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
  1022. sig->it_virt_expires = cputime_add(
  1023. sig->it_virt_expires, utime);
  1024. }
  1025. __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
  1026. }
  1027. if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
  1028. (cputime_eq(virt_expires, cputime_zero) ||
  1029. cputime_lt(sig->it_virt_expires, virt_expires))) {
  1030. virt_expires = sig->it_virt_expires;
  1031. }
  1032. }
  1033. if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
  1034. unsigned long psecs = cputime_to_secs(ptime);
  1035. cputime_t x;
  1036. if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
  1037. /*
  1038. * At the hard limit, we just die.
  1039. * No need to calculate anything else now.
  1040. */
  1041. __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
  1042. return;
  1043. }
  1044. if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
  1045. /*
  1046. * At the soft limit, send a SIGXCPU every second.
  1047. */
  1048. __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
  1049. if (sig->rlim[RLIMIT_CPU].rlim_cur
  1050. < sig->rlim[RLIMIT_CPU].rlim_max) {
  1051. sig->rlim[RLIMIT_CPU].rlim_cur++;
  1052. }
  1053. }
  1054. x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
  1055. if (cputime_eq(prof_expires, cputime_zero) ||
  1056. cputime_lt(x, prof_expires)) {
  1057. prof_expires = x;
  1058. }
  1059. }
  1060. if (!cputime_eq(prof_expires, cputime_zero) ||
  1061. !cputime_eq(virt_expires, cputime_zero) ||
  1062. sched_expires != 0) {
  1063. /*
  1064. * Rebalance the threads' expiry times for the remaining
  1065. * process CPU timers.
  1066. */
  1067. cputime_t prof_left, virt_left, ticks;
  1068. unsigned long long sched_left, sched;
  1069. const unsigned int nthreads = atomic_read(&sig->live);
  1070. prof_left = cputime_sub(prof_expires, utime);
  1071. prof_left = cputime_sub(prof_left, stime);
  1072. prof_left = cputime_div(prof_left, nthreads);
  1073. virt_left = cputime_sub(virt_expires, utime);
  1074. virt_left = cputime_div(virt_left, nthreads);
  1075. if (sched_expires) {
  1076. sched_left = sched_expires - sched_time;
  1077. do_div(sched_left, nthreads);
  1078. } else {
  1079. sched_left = 0;
  1080. }
  1081. t = tsk;
  1082. do {
  1083. ticks = cputime_add(cputime_add(t->utime, t->stime),
  1084. prof_left);
  1085. if (!cputime_eq(prof_expires, cputime_zero) &&
  1086. (cputime_eq(t->it_prof_expires, cputime_zero) ||
  1087. cputime_gt(t->it_prof_expires, ticks))) {
  1088. t->it_prof_expires = ticks;
  1089. }
  1090. ticks = cputime_add(t->utime, virt_left);
  1091. if (!cputime_eq(virt_expires, cputime_zero) &&
  1092. (cputime_eq(t->it_virt_expires, cputime_zero) ||
  1093. cputime_gt(t->it_virt_expires, ticks))) {
  1094. t->it_virt_expires = ticks;
  1095. }
  1096. sched = t->sched_time + sched_left;
  1097. if (sched_expires && (t->it_sched_expires == 0 ||
  1098. t->it_sched_expires > sched)) {
  1099. t->it_sched_expires = sched;
  1100. }
  1101. do {
  1102. t = next_thread(t);
  1103. } while (unlikely(t->exit_state));
  1104. } while (t != tsk);
  1105. }
  1106. }
  1107. /*
  1108. * This is called from the signal code (via do_schedule_next_timer)
  1109. * when the last timer signal was delivered and we have to reload the timer.
  1110. */
  1111. void posix_cpu_timer_schedule(struct k_itimer *timer)
  1112. {
  1113. struct task_struct *p = timer->it.cpu.task;
  1114. union cpu_time_count now;
  1115. if (unlikely(p == NULL))
  1116. /*
  1117. * The task was cleaned up already, no future firings.
  1118. */
  1119. return;
  1120. /*
  1121. * Fetch the current sample and update the timer's expiry time.
  1122. */
  1123. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  1124. cpu_clock_sample(timer->it_clock, p, &now);
  1125. bump_cpu_timer(timer, now);
  1126. if (unlikely(p->exit_state)) {
  1127. clear_dead_task(timer, now);
  1128. return;
  1129. }
  1130. read_lock(&tasklist_lock); /* arm_timer needs it. */
  1131. } else {
  1132. read_lock(&tasklist_lock);
  1133. if (unlikely(p->signal == NULL)) {
  1134. /*
  1135. * The process has been reaped.
  1136. * We can't even collect a sample any more.
  1137. */
  1138. put_task_struct(p);
  1139. timer->it.cpu.task = p = NULL;
  1140. timer->it.cpu.expires.sched = 0;
  1141. read_unlock(&tasklist_lock);
  1142. return;
  1143. } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
  1144. /*
  1145. * We've noticed that the thread is dead, but
  1146. * not yet reaped. Take this opportunity to
  1147. * drop our task ref.
  1148. */
  1149. clear_dead_task(timer, now);
  1150. read_unlock(&tasklist_lock);
  1151. return;
  1152. }
  1153. cpu_clock_sample_group(timer->it_clock, p, &now);
  1154. bump_cpu_timer(timer, now);
  1155. /* Leave the tasklist_lock locked for the call below. */
  1156. }
  1157. /*
  1158. * Now re-arm for the new expiry time.
  1159. */
  1160. arm_timer(timer, now);
  1161. read_unlock(&tasklist_lock);
  1162. }
  1163. /*
  1164. * This is called from the timer interrupt handler. The irq handler has
  1165. * already updated our counts. We need to check if any timers fire now.
  1166. * Interrupts are disabled.
  1167. */
  1168. void run_posix_cpu_timers(struct task_struct *tsk)
  1169. {
  1170. LIST_HEAD(firing);
  1171. struct k_itimer *timer, *next;
  1172. BUG_ON(!irqs_disabled());
  1173. #define UNEXPIRED(clock) \
  1174. (cputime_eq(tsk->it_##clock##_expires, cputime_zero) || \
  1175. cputime_lt(clock##_ticks(tsk), tsk->it_##clock##_expires))
  1176. if (UNEXPIRED(prof) && UNEXPIRED(virt) &&
  1177. (tsk->it_sched_expires == 0 ||
  1178. tsk->sched_time < tsk->it_sched_expires))
  1179. return;
  1180. #undef UNEXPIRED
  1181. BUG_ON(tsk->exit_state);
  1182. /*
  1183. * Double-check with locks held.
  1184. */
  1185. read_lock(&tasklist_lock);
  1186. spin_lock(&tsk->sighand->siglock);
  1187. /*
  1188. * Here we take off tsk->cpu_timers[N] and tsk->signal->cpu_timers[N]
  1189. * all the timers that are firing, and put them on the firing list.
  1190. */
  1191. check_thread_timers(tsk, &firing);
  1192. check_process_timers(tsk, &firing);
  1193. /*
  1194. * We must release these locks before taking any timer's lock.
  1195. * There is a potential race with timer deletion here, as the
  1196. * siglock now protects our private firing list. We have set
  1197. * the firing flag in each timer, so that a deletion attempt
  1198. * that gets the timer lock before we do will give it up and
  1199. * spin until we've taken care of that timer below.
  1200. */
  1201. spin_unlock(&tsk->sighand->siglock);
  1202. read_unlock(&tasklist_lock);
  1203. /*
  1204. * Now that all the timers on our list have the firing flag,
  1205. * noone will touch their list entries but us. We'll take
  1206. * each timer's lock before clearing its firing flag, so no
  1207. * timer call will interfere.
  1208. */
  1209. list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
  1210. int firing;
  1211. spin_lock(&timer->it_lock);
  1212. list_del_init(&timer->it.cpu.entry);
  1213. firing = timer->it.cpu.firing;
  1214. timer->it.cpu.firing = 0;
  1215. /*
  1216. * The firing flag is -1 if we collided with a reset
  1217. * of the timer, which already reported this
  1218. * almost-firing as an overrun. So don't generate an event.
  1219. */
  1220. if (likely(firing >= 0)) {
  1221. cpu_timer_fire(timer);
  1222. }
  1223. spin_unlock(&timer->it_lock);
  1224. }
  1225. }
  1226. /*
  1227. * Set one of the process-wide special case CPU timers.
  1228. * The tasklist_lock and tsk->sighand->siglock must be held by the caller.
  1229. * The oldval argument is null for the RLIMIT_CPU timer, where *newval is
  1230. * absolute; non-null for ITIMER_*, where *newval is relative and we update
  1231. * it to be absolute, *oldval is absolute and we update it to be relative.
  1232. */
  1233. void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
  1234. cputime_t *newval, cputime_t *oldval)
  1235. {
  1236. union cpu_time_count now;
  1237. struct list_head *head;
  1238. BUG_ON(clock_idx == CPUCLOCK_SCHED);
  1239. cpu_clock_sample_group_locked(clock_idx, tsk, &now);
  1240. if (oldval) {
  1241. if (!cputime_eq(*oldval, cputime_zero)) {
  1242. if (cputime_le(*oldval, now.cpu)) {
  1243. /* Just about to fire. */
  1244. *oldval = jiffies_to_cputime(1);
  1245. } else {
  1246. *oldval = cputime_sub(*oldval, now.cpu);
  1247. }
  1248. }
  1249. if (cputime_eq(*newval, cputime_zero))
  1250. return;
  1251. *newval = cputime_add(*newval, now.cpu);
  1252. /*
  1253. * If the RLIMIT_CPU timer will expire before the
  1254. * ITIMER_PROF timer, we have nothing else to do.
  1255. */
  1256. if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
  1257. < cputime_to_secs(*newval))
  1258. return;
  1259. }
  1260. /*
  1261. * Check whether there are any process timers already set to fire
  1262. * before this one. If so, we don't have anything more to do.
  1263. */
  1264. head = &tsk->signal->cpu_timers[clock_idx];
  1265. if (list_empty(head) ||
  1266. cputime_ge(list_entry(head->next,
  1267. struct cpu_timer_list, entry)->expires.cpu,
  1268. *newval)) {
  1269. /*
  1270. * Rejigger each thread's expiry time so that one will
  1271. * notice before we hit the process-cumulative expiry time.
  1272. */
  1273. union cpu_time_count expires = { .sched = 0 };
  1274. expires.cpu = *newval;
  1275. process_timer_rebalance(tsk, clock_idx, expires, now);
  1276. }
  1277. }
  1278. static long posix_cpu_clock_nanosleep_restart(struct restart_block *);
  1279. int posix_cpu_nsleep(clockid_t which_clock, int flags,
  1280. struct timespec *rqtp)
  1281. {
  1282. struct restart_block *restart_block =
  1283. &current_thread_info()->restart_block;
  1284. struct k_itimer timer;
  1285. int error;
  1286. /*
  1287. * Diagnose required errors first.
  1288. */
  1289. if (CPUCLOCK_PERTHREAD(which_clock) &&
  1290. (CPUCLOCK_PID(which_clock) == 0 ||
  1291. CPUCLOCK_PID(which_clock) == current->pid))
  1292. return -EINVAL;
  1293. /*
  1294. * Set up a temporary timer and then wait for it to go off.
  1295. */
  1296. memset(&timer, 0, sizeof timer);
  1297. spin_lock_init(&timer.it_lock);
  1298. timer.it_clock = which_clock;
  1299. timer.it_overrun = -1;
  1300. error = posix_cpu_timer_create(&timer);
  1301. timer.it_process = current;
  1302. if (!error) {
  1303. struct timespec __user *rmtp;
  1304. static struct itimerspec zero_it;
  1305. struct itimerspec it = { .it_value = *rqtp,
  1306. .it_interval = {} };
  1307. spin_lock_irq(&timer.it_lock);
  1308. error = posix_cpu_timer_set(&timer, flags, &it, NULL);
  1309. if (error) {
  1310. spin_unlock_irq(&timer.it_lock);
  1311. return error;
  1312. }
  1313. while (!signal_pending(current)) {
  1314. if (timer.it.cpu.expires.sched == 0) {
  1315. /*
  1316. * Our timer fired and was reset.
  1317. */
  1318. spin_unlock_irq(&timer.it_lock);
  1319. return 0;
  1320. }
  1321. /*
  1322. * Block until cpu_timer_fire (or a signal) wakes us.
  1323. */
  1324. __set_current_state(TASK_INTERRUPTIBLE);
  1325. spin_unlock_irq(&timer.it_lock);
  1326. schedule();
  1327. spin_lock_irq(&timer.it_lock);
  1328. }
  1329. /*
  1330. * We were interrupted by a signal.
  1331. */
  1332. sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
  1333. posix_cpu_timer_set(&timer, 0, &zero_it, &it);
  1334. spin_unlock_irq(&timer.it_lock);
  1335. if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
  1336. /*
  1337. * It actually did fire already.
  1338. */
  1339. return 0;
  1340. }
  1341. /*
  1342. * Report back to the user the time still remaining.
  1343. */
  1344. rmtp = (struct timespec __user *) restart_block->arg1;
  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->arg2 = rqtp->tv_sec;
  1352. restart_block->arg3 = rqtp->tv_nsec;
  1353. error = -ERESTART_RESTARTBLOCK;
  1354. }
  1355. return error;
  1356. }
  1357. static long
  1358. posix_cpu_clock_nanosleep_restart(struct restart_block *restart_block)
  1359. {
  1360. clockid_t which_clock = restart_block->arg0;
  1361. struct timespec t = { .tv_sec = restart_block->arg2,
  1362. .tv_nsec = restart_block->arg3 };
  1363. restart_block->fn = do_no_restart_syscall;
  1364. return posix_cpu_nsleep(which_clock, TIMER_ABSTIME, &t);
  1365. }
  1366. #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
  1367. #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
  1368. static int process_cpu_clock_getres(clockid_t which_clock, struct timespec *tp)
  1369. {
  1370. return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
  1371. }
  1372. static int process_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
  1373. {
  1374. return posix_cpu_clock_get(PROCESS_CLOCK, tp);
  1375. }
  1376. static int process_cpu_timer_create(struct k_itimer *timer)
  1377. {
  1378. timer->it_clock = PROCESS_CLOCK;
  1379. return posix_cpu_timer_create(timer);
  1380. }
  1381. static int process_cpu_nsleep(clockid_t which_clock, int flags,
  1382. struct timespec *rqtp)
  1383. {
  1384. return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
  1385. }
  1386. static int thread_cpu_clock_getres(clockid_t which_clock, struct timespec *tp)
  1387. {
  1388. return posix_cpu_clock_getres(THREAD_CLOCK, tp);
  1389. }
  1390. static int thread_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
  1391. {
  1392. return posix_cpu_clock_get(THREAD_CLOCK, tp);
  1393. }
  1394. static int thread_cpu_timer_create(struct k_itimer *timer)
  1395. {
  1396. timer->it_clock = THREAD_CLOCK;
  1397. return posix_cpu_timer_create(timer);
  1398. }
  1399. static int thread_cpu_nsleep(clockid_t which_clock, int flags,
  1400. struct timespec *rqtp)
  1401. {
  1402. return -EINVAL;
  1403. }
  1404. static __init int init_posix_cpu_timers(void)
  1405. {
  1406. struct k_clock process = {
  1407. .clock_getres = process_cpu_clock_getres,
  1408. .clock_get = process_cpu_clock_get,
  1409. .clock_set = do_posix_clock_nosettime,
  1410. .timer_create = process_cpu_timer_create,
  1411. .nsleep = process_cpu_nsleep,
  1412. };
  1413. struct k_clock thread = {
  1414. .clock_getres = thread_cpu_clock_getres,
  1415. .clock_get = thread_cpu_clock_get,
  1416. .clock_set = do_posix_clock_nosettime,
  1417. .timer_create = thread_cpu_timer_create,
  1418. .nsleep = thread_cpu_nsleep,
  1419. };
  1420. register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
  1421. register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
  1422. return 0;
  1423. }
  1424. __initcall(init_posix_cpu_timers);