posix-cpu-timers.c 41 KB

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