posix-cpu-timers.c 43 KB

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