posix-cpu-timers.c 38 KB

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