posix-cpu-timers.c 41 KB

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