posix-cpu-timers.c 45 KB

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