posix-cpu-timers.c 42 KB

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