posix-cpu-timers.c 42 KB

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