posix-cpu-timers.c 43 KB

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