posix-cpu-timers.c 38 KB

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