posix-cpu-timers.c 38 KB

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