posix-cpu-timers.c 45 KB

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