posix-cpu-timers.c 42 KB

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