posix-cpu-timers.c 42 KB

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