posix-cpu-timers.c 42 KB

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