posix-cpu-timers.c 41 KB

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