posix-cpu-timers.c 43 KB

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