posix-cpu-timers.c 42 KB

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