posix-cpu-timers.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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. WARN_ON_ONCE(p == NULL);
  328. /*
  329. * Protect against sighand release/switch in exit/exec and process/
  330. * thread timer list entry concurrent read/writes.
  331. */
  332. sighand = lock_task_sighand(p, &flags);
  333. if (unlikely(sighand == NULL)) {
  334. /*
  335. * We raced with the reaping of the task.
  336. * The deletion should have cleared us off the list.
  337. */
  338. WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry));
  339. } else {
  340. if (timer->it.cpu.firing)
  341. ret = TIMER_RETRY;
  342. else
  343. list_del(&timer->it.cpu.entry);
  344. unlock_task_sighand(p, &flags);
  345. }
  346. if (!ret)
  347. put_task_struct(p);
  348. return ret;
  349. }
  350. static void cleanup_timers_list(struct list_head *head)
  351. {
  352. struct cpu_timer_list *timer, *next;
  353. list_for_each_entry_safe(timer, next, head, entry)
  354. list_del_init(&timer->entry);
  355. }
  356. /*
  357. * Clean out CPU timers still ticking when a thread exited. The task
  358. * pointer is cleared, and the expiry time is replaced with the residual
  359. * time for later timer_gettime calls to return.
  360. * This must be called with the siglock held.
  361. */
  362. static void cleanup_timers(struct list_head *head)
  363. {
  364. cleanup_timers_list(head);
  365. cleanup_timers_list(++head);
  366. cleanup_timers_list(++head);
  367. }
  368. /*
  369. * These are both called with the siglock held, when the current thread
  370. * is being reaped. When the final (leader) thread in the group is reaped,
  371. * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
  372. */
  373. void posix_cpu_timers_exit(struct task_struct *tsk)
  374. {
  375. cleanup_timers(tsk->cpu_timers);
  376. }
  377. void posix_cpu_timers_exit_group(struct task_struct *tsk)
  378. {
  379. cleanup_timers(tsk->signal->cpu_timers);
  380. }
  381. static inline int expires_gt(u64 expires, u64 new_exp)
  382. {
  383. return expires == 0 || expires > new_exp;
  384. }
  385. /*
  386. * Insert the timer on the appropriate list before any timers that
  387. * expire later. This must be called with the sighand lock held.
  388. */
  389. static void arm_timer(struct k_itimer *timer)
  390. {
  391. struct task_struct *p = timer->it.cpu.task;
  392. struct list_head *head, *listpos;
  393. struct task_cputime *cputime_expires;
  394. struct cpu_timer_list *const nt = &timer->it.cpu;
  395. struct cpu_timer_list *next;
  396. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  397. head = p->cpu_timers;
  398. cputime_expires = &p->cputime_expires;
  399. } else {
  400. head = p->signal->cpu_timers;
  401. cputime_expires = &p->signal->cputime_expires;
  402. }
  403. head += CPUCLOCK_WHICH(timer->it_clock);
  404. listpos = head;
  405. list_for_each_entry(next, head, entry) {
  406. if (nt->expires < next->expires)
  407. break;
  408. listpos = &next->entry;
  409. }
  410. list_add(&nt->entry, listpos);
  411. if (listpos == head) {
  412. u64 exp = nt->expires;
  413. /*
  414. * We are the new earliest-expiring POSIX 1.b timer, hence
  415. * need to update expiration cache. Take into account that
  416. * for process timers we share expiration cache with itimers
  417. * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
  418. */
  419. switch (CPUCLOCK_WHICH(timer->it_clock)) {
  420. case CPUCLOCK_PROF:
  421. if (expires_gt(cputime_expires->prof_exp, exp))
  422. cputime_expires->prof_exp = exp;
  423. break;
  424. case CPUCLOCK_VIRT:
  425. if (expires_gt(cputime_expires->virt_exp, exp))
  426. cputime_expires->virt_exp = exp;
  427. break;
  428. case CPUCLOCK_SCHED:
  429. if (expires_gt(cputime_expires->sched_exp, exp))
  430. cputime_expires->sched_exp = exp;
  431. break;
  432. }
  433. if (CPUCLOCK_PERTHREAD(timer->it_clock))
  434. tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
  435. else
  436. tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER);
  437. }
  438. }
  439. /*
  440. * The timer is locked, fire it and arrange for its reload.
  441. */
  442. static void cpu_timer_fire(struct k_itimer *timer)
  443. {
  444. if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
  445. /*
  446. * User don't want any signal.
  447. */
  448. timer->it.cpu.expires = 0;
  449. } else if (unlikely(timer->sigq == NULL)) {
  450. /*
  451. * This a special case for clock_nanosleep,
  452. * not a normal timer from sys_timer_create.
  453. */
  454. wake_up_process(timer->it_process);
  455. timer->it.cpu.expires = 0;
  456. } else if (timer->it.cpu.incr == 0) {
  457. /*
  458. * One-shot timer. Clear it as soon as it's fired.
  459. */
  460. posix_timer_event(timer, 0);
  461. timer->it.cpu.expires = 0;
  462. } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
  463. /*
  464. * The signal did not get queued because the signal
  465. * was ignored, so we won't get any callback to
  466. * reload the timer. But we need to keep it
  467. * ticking in case the signal is deliverable next time.
  468. */
  469. posix_cpu_timer_rearm(timer);
  470. ++timer->it_requeue_pending;
  471. }
  472. }
  473. /*
  474. * Sample a process (thread group) timer for the given group_leader task.
  475. * Must be called with task sighand lock held for safe while_each_thread()
  476. * traversal.
  477. */
  478. static int cpu_timer_sample_group(const clockid_t which_clock,
  479. struct task_struct *p, u64 *sample)
  480. {
  481. struct task_cputime cputime;
  482. thread_group_cputimer(p, &cputime);
  483. switch (CPUCLOCK_WHICH(which_clock)) {
  484. default:
  485. return -EINVAL;
  486. case CPUCLOCK_PROF:
  487. *sample = cputime.utime + cputime.stime;
  488. break;
  489. case CPUCLOCK_VIRT:
  490. *sample = cputime.utime;
  491. break;
  492. case CPUCLOCK_SCHED:
  493. *sample = cputime.sum_exec_runtime;
  494. break;
  495. }
  496. return 0;
  497. }
  498. /*
  499. * Guts of sys_timer_settime for CPU timers.
  500. * This is called with the timer locked and interrupts disabled.
  501. * If we return TIMER_RETRY, it's necessary to release the timer's lock
  502. * and try again. (This happens when the timer is in the middle of firing.)
  503. */
  504. static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
  505. struct itimerspec64 *new, struct itimerspec64 *old)
  506. {
  507. unsigned long flags;
  508. struct sighand_struct *sighand;
  509. struct task_struct *p = timer->it.cpu.task;
  510. u64 old_expires, new_expires, old_incr, val;
  511. int ret;
  512. WARN_ON_ONCE(p == NULL);
  513. /*
  514. * Use the to_ktime conversion because that clamps the maximum
  515. * value to KTIME_MAX and avoid multiplication overflows.
  516. */
  517. new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
  518. /*
  519. * Protect against sighand release/switch in exit/exec and p->cpu_timers
  520. * and p->signal->cpu_timers read/write in arm_timer()
  521. */
  522. sighand = lock_task_sighand(p, &flags);
  523. /*
  524. * If p has just been reaped, we can no
  525. * longer get any information about it at all.
  526. */
  527. if (unlikely(sighand == NULL)) {
  528. return -ESRCH;
  529. }
  530. /*
  531. * Disarm any old timer after extracting its expiry time.
  532. */
  533. ret = 0;
  534. old_incr = timer->it.cpu.incr;
  535. old_expires = timer->it.cpu.expires;
  536. if (unlikely(timer->it.cpu.firing)) {
  537. timer->it.cpu.firing = -1;
  538. ret = TIMER_RETRY;
  539. } else
  540. list_del_init(&timer->it.cpu.entry);
  541. /*
  542. * We need to sample the current value to convert the new
  543. * value from to relative and absolute, and to convert the
  544. * old value from absolute to relative. To set a process
  545. * timer, we need a sample to balance the thread expiry
  546. * times (in arm_timer). With an absolute time, we must
  547. * check if it's already passed. In short, we need a sample.
  548. */
  549. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  550. cpu_clock_sample(timer->it_clock, p, &val);
  551. } else {
  552. cpu_timer_sample_group(timer->it_clock, p, &val);
  553. }
  554. if (old) {
  555. if (old_expires == 0) {
  556. old->it_value.tv_sec = 0;
  557. old->it_value.tv_nsec = 0;
  558. } else {
  559. /*
  560. * Update the timer in case it has
  561. * overrun already. If it has,
  562. * we'll report it as having overrun
  563. * and with the next reloaded timer
  564. * already ticking, though we are
  565. * swallowing that pending
  566. * notification here to install the
  567. * new setting.
  568. */
  569. bump_cpu_timer(timer, val);
  570. if (val < timer->it.cpu.expires) {
  571. old_expires = timer->it.cpu.expires - val;
  572. old->it_value = ns_to_timespec64(old_expires);
  573. } else {
  574. old->it_value.tv_nsec = 1;
  575. old->it_value.tv_sec = 0;
  576. }
  577. }
  578. }
  579. if (unlikely(ret)) {
  580. /*
  581. * We are colliding with the timer actually firing.
  582. * Punt after filling in the timer's old value, and
  583. * disable this firing since we are already reporting
  584. * it as an overrun (thanks to bump_cpu_timer above).
  585. */
  586. unlock_task_sighand(p, &flags);
  587. goto out;
  588. }
  589. if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) {
  590. new_expires += val;
  591. }
  592. /*
  593. * Install the new expiry time (or zero).
  594. * For a timer with no notification action, we don't actually
  595. * arm the timer (we'll just fake it for timer_gettime).
  596. */
  597. timer->it.cpu.expires = new_expires;
  598. if (new_expires != 0 && val < new_expires) {
  599. arm_timer(timer);
  600. }
  601. unlock_task_sighand(p, &flags);
  602. /*
  603. * Install the new reload setting, and
  604. * set up the signal and overrun bookkeeping.
  605. */
  606. timer->it.cpu.incr = timespec64_to_ns(&new->it_interval);
  607. timer->it_interval = ns_to_ktime(timer->it.cpu.incr);
  608. /*
  609. * This acts as a modification timestamp for the timer,
  610. * so any automatic reload attempt will punt on seeing
  611. * that we have reset the timer manually.
  612. */
  613. timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
  614. ~REQUEUE_PENDING;
  615. timer->it_overrun_last = 0;
  616. timer->it_overrun = -1;
  617. if (new_expires != 0 && !(val < new_expires)) {
  618. /*
  619. * The designated time already passed, so we notify
  620. * immediately, even if the thread never runs to
  621. * accumulate more time on this clock.
  622. */
  623. cpu_timer_fire(timer);
  624. }
  625. ret = 0;
  626. out:
  627. if (old)
  628. old->it_interval = ns_to_timespec64(old_incr);
  629. return ret;
  630. }
  631. static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp)
  632. {
  633. u64 now;
  634. struct task_struct *p = timer->it.cpu.task;
  635. WARN_ON_ONCE(p == NULL);
  636. /*
  637. * Easy part: convert the reload time.
  638. */
  639. itp->it_interval = ns_to_timespec64(timer->it.cpu.incr);
  640. if (!timer->it.cpu.expires)
  641. return;
  642. /*
  643. * Sample the clock to take the difference with the expiry time.
  644. */
  645. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  646. cpu_clock_sample(timer->it_clock, p, &now);
  647. } else {
  648. struct sighand_struct *sighand;
  649. unsigned long flags;
  650. /*
  651. * Protect against sighand release/switch in exit/exec and
  652. * also make timer sampling safe if it ends up calling
  653. * thread_group_cputime().
  654. */
  655. sighand = lock_task_sighand(p, &flags);
  656. if (unlikely(sighand == NULL)) {
  657. /*
  658. * The process has been reaped.
  659. * We can't even collect a sample any more.
  660. * Call the timer disarmed, nothing else to do.
  661. */
  662. timer->it.cpu.expires = 0;
  663. return;
  664. } else {
  665. cpu_timer_sample_group(timer->it_clock, p, &now);
  666. unlock_task_sighand(p, &flags);
  667. }
  668. }
  669. if (now < timer->it.cpu.expires) {
  670. itp->it_value = ns_to_timespec64(timer->it.cpu.expires - now);
  671. } else {
  672. /*
  673. * The timer should have expired already, but the firing
  674. * hasn't taken place yet. Say it's just about to expire.
  675. */
  676. itp->it_value.tv_nsec = 1;
  677. itp->it_value.tv_sec = 0;
  678. }
  679. }
  680. static unsigned long long
  681. check_timers_list(struct list_head *timers,
  682. struct list_head *firing,
  683. unsigned long long curr)
  684. {
  685. int maxfire = 20;
  686. while (!list_empty(timers)) {
  687. struct cpu_timer_list *t;
  688. t = list_first_entry(timers, struct cpu_timer_list, entry);
  689. if (!--maxfire || curr < t->expires)
  690. return t->expires;
  691. t->firing = 1;
  692. list_move_tail(&t->entry, firing);
  693. }
  694. return 0;
  695. }
  696. static inline void check_dl_overrun(struct task_struct *tsk)
  697. {
  698. if (tsk->dl.dl_overrun) {
  699. tsk->dl.dl_overrun = 0;
  700. __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
  701. }
  702. }
  703. /*
  704. * Check for any per-thread CPU timers that have fired and move them off
  705. * the tsk->cpu_timers[N] list onto the firing list. Here we update the
  706. * tsk->it_*_expires values to reflect the remaining thread CPU timers.
  707. */
  708. static void check_thread_timers(struct task_struct *tsk,
  709. struct list_head *firing)
  710. {
  711. struct list_head *timers = tsk->cpu_timers;
  712. struct task_cputime *tsk_expires = &tsk->cputime_expires;
  713. u64 expires;
  714. unsigned long soft;
  715. if (dl_task(tsk))
  716. check_dl_overrun(tsk);
  717. /*
  718. * If cputime_expires is zero, then there are no active
  719. * per thread CPU timers.
  720. */
  721. if (task_cputime_zero(&tsk->cputime_expires))
  722. return;
  723. expires = check_timers_list(timers, firing, prof_ticks(tsk));
  724. tsk_expires->prof_exp = expires;
  725. expires = check_timers_list(++timers, firing, virt_ticks(tsk));
  726. tsk_expires->virt_exp = expires;
  727. tsk_expires->sched_exp = check_timers_list(++timers, firing,
  728. tsk->se.sum_exec_runtime);
  729. /*
  730. * Check for the special case thread timers.
  731. */
  732. soft = task_rlimit(tsk, RLIMIT_RTTIME);
  733. if (soft != RLIM_INFINITY) {
  734. unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME);
  735. if (hard != RLIM_INFINITY &&
  736. tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
  737. /*
  738. * At the hard limit, we just die.
  739. * No need to calculate anything else now.
  740. */
  741. if (print_fatal_signals) {
  742. pr_info("CPU Watchdog Timeout (hard): %s[%d]\n",
  743. tsk->comm, task_pid_nr(tsk));
  744. }
  745. __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
  746. return;
  747. }
  748. if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
  749. /*
  750. * At the soft limit, send a SIGXCPU every second.
  751. */
  752. if (soft < hard) {
  753. soft += USEC_PER_SEC;
  754. tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur =
  755. soft;
  756. }
  757. if (print_fatal_signals) {
  758. pr_info("RT Watchdog Timeout (soft): %s[%d]\n",
  759. tsk->comm, task_pid_nr(tsk));
  760. }
  761. __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
  762. }
  763. }
  764. if (task_cputime_zero(tsk_expires))
  765. tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
  766. }
  767. static inline void stop_process_timers(struct signal_struct *sig)
  768. {
  769. struct thread_group_cputimer *cputimer = &sig->cputimer;
  770. /* Turn off cputimer->running. This is done without locking. */
  771. WRITE_ONCE(cputimer->running, false);
  772. tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
  773. }
  774. static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
  775. u64 *expires, u64 cur_time, int signo)
  776. {
  777. if (!it->expires)
  778. return;
  779. if (cur_time >= it->expires) {
  780. if (it->incr)
  781. it->expires += it->incr;
  782. else
  783. it->expires = 0;
  784. trace_itimer_expire(signo == SIGPROF ?
  785. ITIMER_PROF : ITIMER_VIRTUAL,
  786. task_tgid(tsk), cur_time);
  787. __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
  788. }
  789. if (it->expires && (!*expires || it->expires < *expires))
  790. *expires = it->expires;
  791. }
  792. /*
  793. * Check for any per-thread CPU timers that have fired and move them
  794. * off the tsk->*_timers list onto the firing list. Per-thread timers
  795. * have already been taken off.
  796. */
  797. static void check_process_timers(struct task_struct *tsk,
  798. struct list_head *firing)
  799. {
  800. struct signal_struct *const sig = tsk->signal;
  801. u64 utime, ptime, virt_expires, prof_expires;
  802. u64 sum_sched_runtime, sched_expires;
  803. struct list_head *timers = sig->cpu_timers;
  804. struct task_cputime cputime;
  805. unsigned long soft;
  806. if (dl_task(tsk))
  807. check_dl_overrun(tsk);
  808. /*
  809. * If cputimer is not running, then there are no active
  810. * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU).
  811. */
  812. if (!READ_ONCE(tsk->signal->cputimer.running))
  813. return;
  814. /*
  815. * Signify that a thread is checking for process timers.
  816. * Write access to this field is protected by the sighand lock.
  817. */
  818. sig->cputimer.checking_timer = true;
  819. /*
  820. * Collect the current process totals.
  821. */
  822. thread_group_cputimer(tsk, &cputime);
  823. utime = cputime.utime;
  824. ptime = utime + cputime.stime;
  825. sum_sched_runtime = cputime.sum_exec_runtime;
  826. prof_expires = check_timers_list(timers, firing, ptime);
  827. virt_expires = check_timers_list(++timers, firing, utime);
  828. sched_expires = check_timers_list(++timers, firing, sum_sched_runtime);
  829. /*
  830. * Check for the special case process timers.
  831. */
  832. check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
  833. SIGPROF);
  834. check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
  835. SIGVTALRM);
  836. soft = task_rlimit(tsk, RLIMIT_CPU);
  837. if (soft != RLIM_INFINITY) {
  838. unsigned long psecs = div_u64(ptime, NSEC_PER_SEC);
  839. unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU);
  840. u64 x;
  841. if (psecs >= hard) {
  842. /*
  843. * At the hard limit, we just die.
  844. * No need to calculate anything else now.
  845. */
  846. if (print_fatal_signals) {
  847. pr_info("RT Watchdog Timeout (hard): %s[%d]\n",
  848. tsk->comm, task_pid_nr(tsk));
  849. }
  850. __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
  851. return;
  852. }
  853. if (psecs >= soft) {
  854. /*
  855. * At the soft limit, send a SIGXCPU every second.
  856. */
  857. if (print_fatal_signals) {
  858. pr_info("CPU Watchdog Timeout (soft): %s[%d]\n",
  859. tsk->comm, task_pid_nr(tsk));
  860. }
  861. __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
  862. if (soft < hard) {
  863. soft++;
  864. sig->rlim[RLIMIT_CPU].rlim_cur = soft;
  865. }
  866. }
  867. x = soft * NSEC_PER_SEC;
  868. if (!prof_expires || x < prof_expires)
  869. prof_expires = x;
  870. }
  871. sig->cputime_expires.prof_exp = prof_expires;
  872. sig->cputime_expires.virt_exp = virt_expires;
  873. sig->cputime_expires.sched_exp = sched_expires;
  874. if (task_cputime_zero(&sig->cputime_expires))
  875. stop_process_timers(sig);
  876. sig->cputimer.checking_timer = false;
  877. }
  878. /*
  879. * This is called from the signal code (via posixtimer_rearm)
  880. * when the last timer signal was delivered and we have to reload the timer.
  881. */
  882. static void posix_cpu_timer_rearm(struct k_itimer *timer)
  883. {
  884. struct sighand_struct *sighand;
  885. unsigned long flags;
  886. struct task_struct *p = timer->it.cpu.task;
  887. u64 now;
  888. WARN_ON_ONCE(p == NULL);
  889. /*
  890. * Fetch the current sample and update the timer's expiry time.
  891. */
  892. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  893. cpu_clock_sample(timer->it_clock, p, &now);
  894. bump_cpu_timer(timer, now);
  895. if (unlikely(p->exit_state))
  896. return;
  897. /* Protect timer list r/w in arm_timer() */
  898. sighand = lock_task_sighand(p, &flags);
  899. if (!sighand)
  900. return;
  901. } else {
  902. /*
  903. * Protect arm_timer() and timer sampling in case of call to
  904. * thread_group_cputime().
  905. */
  906. sighand = lock_task_sighand(p, &flags);
  907. if (unlikely(sighand == NULL)) {
  908. /*
  909. * The process has been reaped.
  910. * We can't even collect a sample any more.
  911. */
  912. timer->it.cpu.expires = 0;
  913. return;
  914. } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
  915. /* If the process is dying, no need to rearm */
  916. goto unlock;
  917. }
  918. cpu_timer_sample_group(timer->it_clock, p, &now);
  919. bump_cpu_timer(timer, now);
  920. /* Leave the sighand locked for the call below. */
  921. }
  922. /*
  923. * Now re-arm for the new expiry time.
  924. */
  925. arm_timer(timer);
  926. unlock:
  927. unlock_task_sighand(p, &flags);
  928. }
  929. /**
  930. * task_cputime_expired - Compare two task_cputime entities.
  931. *
  932. * @sample: The task_cputime structure to be checked for expiration.
  933. * @expires: Expiration times, against which @sample will be checked.
  934. *
  935. * Checks @sample against @expires to see if any field of @sample has expired.
  936. * Returns true if any field of the former is greater than the corresponding
  937. * field of the latter if the latter field is set. Otherwise returns false.
  938. */
  939. static inline int task_cputime_expired(const struct task_cputime *sample,
  940. const struct task_cputime *expires)
  941. {
  942. if (expires->utime && sample->utime >= expires->utime)
  943. return 1;
  944. if (expires->stime && sample->utime + sample->stime >= expires->stime)
  945. return 1;
  946. if (expires->sum_exec_runtime != 0 &&
  947. sample->sum_exec_runtime >= expires->sum_exec_runtime)
  948. return 1;
  949. return 0;
  950. }
  951. /**
  952. * fastpath_timer_check - POSIX CPU timers fast path.
  953. *
  954. * @tsk: The task (thread) being checked.
  955. *
  956. * Check the task and thread group timers. If both are zero (there are no
  957. * timers set) return false. Otherwise snapshot the task and thread group
  958. * timers and compare them with the corresponding expiration times. Return
  959. * true if a timer has expired, else return false.
  960. */
  961. static inline int fastpath_timer_check(struct task_struct *tsk)
  962. {
  963. struct signal_struct *sig;
  964. if (!task_cputime_zero(&tsk->cputime_expires)) {
  965. struct task_cputime task_sample;
  966. task_cputime(tsk, &task_sample.utime, &task_sample.stime);
  967. task_sample.sum_exec_runtime = tsk->se.sum_exec_runtime;
  968. if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
  969. return 1;
  970. }
  971. sig = tsk->signal;
  972. /*
  973. * Check if thread group timers expired when the cputimer is
  974. * running and no other thread in the group is already checking
  975. * for thread group cputimers. These fields are read without the
  976. * sighand lock. However, this is fine because this is meant to
  977. * be a fastpath heuristic to determine whether we should try to
  978. * acquire the sighand lock to check/handle timers.
  979. *
  980. * In the worst case scenario, if 'running' or 'checking_timer' gets
  981. * set but the current thread doesn't see the change yet, we'll wait
  982. * until the next thread in the group gets a scheduler interrupt to
  983. * handle the timer. This isn't an issue in practice because these
  984. * types of delays with signals actually getting sent are expected.
  985. */
  986. if (READ_ONCE(sig->cputimer.running) &&
  987. !READ_ONCE(sig->cputimer.checking_timer)) {
  988. struct task_cputime group_sample;
  989. sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic);
  990. if (task_cputime_expired(&group_sample, &sig->cputime_expires))
  991. return 1;
  992. }
  993. if (dl_task(tsk) && tsk->dl.dl_overrun)
  994. return 1;
  995. return 0;
  996. }
  997. /*
  998. * This is called from the timer interrupt handler. The irq handler has
  999. * already updated our counts. We need to check if any timers fire now.
  1000. * Interrupts are disabled.
  1001. */
  1002. void run_posix_cpu_timers(struct task_struct *tsk)
  1003. {
  1004. LIST_HEAD(firing);
  1005. struct k_itimer *timer, *next;
  1006. unsigned long flags;
  1007. lockdep_assert_irqs_disabled();
  1008. /*
  1009. * The fast path checks that there are no expired thread or thread
  1010. * group timers. If that's so, just return.
  1011. */
  1012. if (!fastpath_timer_check(tsk))
  1013. return;
  1014. if (!lock_task_sighand(tsk, &flags))
  1015. return;
  1016. /*
  1017. * Here we take off tsk->signal->cpu_timers[N] and
  1018. * tsk->cpu_timers[N] all the timers that are firing, and
  1019. * put them on the firing list.
  1020. */
  1021. check_thread_timers(tsk, &firing);
  1022. check_process_timers(tsk, &firing);
  1023. /*
  1024. * We must release these locks before taking any timer's lock.
  1025. * There is a potential race with timer deletion here, as the
  1026. * siglock now protects our private firing list. We have set
  1027. * the firing flag in each timer, so that a deletion attempt
  1028. * that gets the timer lock before we do will give it up and
  1029. * spin until we've taken care of that timer below.
  1030. */
  1031. unlock_task_sighand(tsk, &flags);
  1032. /*
  1033. * Now that all the timers on our list have the firing flag,
  1034. * no one will touch their list entries but us. We'll take
  1035. * each timer's lock before clearing its firing flag, so no
  1036. * timer call will interfere.
  1037. */
  1038. list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
  1039. int cpu_firing;
  1040. spin_lock(&timer->it_lock);
  1041. list_del_init(&timer->it.cpu.entry);
  1042. cpu_firing = timer->it.cpu.firing;
  1043. timer->it.cpu.firing = 0;
  1044. /*
  1045. * The firing flag is -1 if we collided with a reset
  1046. * of the timer, which already reported this
  1047. * almost-firing as an overrun. So don't generate an event.
  1048. */
  1049. if (likely(cpu_firing >= 0))
  1050. cpu_timer_fire(timer);
  1051. spin_unlock(&timer->it_lock);
  1052. }
  1053. }
  1054. /*
  1055. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  1056. * The tsk->sighand->siglock must be held by the caller.
  1057. */
  1058. void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
  1059. u64 *newval, u64 *oldval)
  1060. {
  1061. u64 now;
  1062. int ret;
  1063. WARN_ON_ONCE(clock_idx == CPUCLOCK_SCHED);
  1064. ret = cpu_timer_sample_group(clock_idx, tsk, &now);
  1065. if (oldval && ret != -EINVAL) {
  1066. /*
  1067. * We are setting itimer. The *oldval is absolute and we update
  1068. * it to be relative, *newval argument is relative and we update
  1069. * it to be absolute.
  1070. */
  1071. if (*oldval) {
  1072. if (*oldval <= now) {
  1073. /* Just about to fire. */
  1074. *oldval = TICK_NSEC;
  1075. } else {
  1076. *oldval -= now;
  1077. }
  1078. }
  1079. if (!*newval)
  1080. return;
  1081. *newval += now;
  1082. }
  1083. /*
  1084. * Update expiration cache if we are the earliest timer, or eventually
  1085. * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
  1086. */
  1087. switch (clock_idx) {
  1088. case CPUCLOCK_PROF:
  1089. if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
  1090. tsk->signal->cputime_expires.prof_exp = *newval;
  1091. break;
  1092. case CPUCLOCK_VIRT:
  1093. if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
  1094. tsk->signal->cputime_expires.virt_exp = *newval;
  1095. break;
  1096. }
  1097. tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
  1098. }
  1099. static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
  1100. const struct timespec64 *rqtp)
  1101. {
  1102. struct itimerspec64 it;
  1103. struct k_itimer timer;
  1104. u64 expires;
  1105. int error;
  1106. /*
  1107. * Set up a temporary timer and then wait for it to go off.
  1108. */
  1109. memset(&timer, 0, sizeof timer);
  1110. spin_lock_init(&timer.it_lock);
  1111. timer.it_clock = which_clock;
  1112. timer.it_overrun = -1;
  1113. error = posix_cpu_timer_create(&timer);
  1114. timer.it_process = current;
  1115. if (!error) {
  1116. static struct itimerspec64 zero_it;
  1117. struct restart_block *restart;
  1118. memset(&it, 0, sizeof(it));
  1119. it.it_value = *rqtp;
  1120. spin_lock_irq(&timer.it_lock);
  1121. error = posix_cpu_timer_set(&timer, flags, &it, NULL);
  1122. if (error) {
  1123. spin_unlock_irq(&timer.it_lock);
  1124. return error;
  1125. }
  1126. while (!signal_pending(current)) {
  1127. if (timer.it.cpu.expires == 0) {
  1128. /*
  1129. * Our timer fired and was reset, below
  1130. * deletion can not fail.
  1131. */
  1132. posix_cpu_timer_del(&timer);
  1133. spin_unlock_irq(&timer.it_lock);
  1134. return 0;
  1135. }
  1136. /*
  1137. * Block until cpu_timer_fire (or a signal) wakes us.
  1138. */
  1139. __set_current_state(TASK_INTERRUPTIBLE);
  1140. spin_unlock_irq(&timer.it_lock);
  1141. schedule();
  1142. spin_lock_irq(&timer.it_lock);
  1143. }
  1144. /*
  1145. * We were interrupted by a signal.
  1146. */
  1147. expires = timer.it.cpu.expires;
  1148. error = posix_cpu_timer_set(&timer, 0, &zero_it, &it);
  1149. if (!error) {
  1150. /*
  1151. * Timer is now unarmed, deletion can not fail.
  1152. */
  1153. posix_cpu_timer_del(&timer);
  1154. }
  1155. spin_unlock_irq(&timer.it_lock);
  1156. while (error == TIMER_RETRY) {
  1157. /*
  1158. * We need to handle case when timer was or is in the
  1159. * middle of firing. In other cases we already freed
  1160. * resources.
  1161. */
  1162. spin_lock_irq(&timer.it_lock);
  1163. error = posix_cpu_timer_del(&timer);
  1164. spin_unlock_irq(&timer.it_lock);
  1165. }
  1166. if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
  1167. /*
  1168. * It actually did fire already.
  1169. */
  1170. return 0;
  1171. }
  1172. error = -ERESTART_RESTARTBLOCK;
  1173. /*
  1174. * Report back to the user the time still remaining.
  1175. */
  1176. restart = &current->restart_block;
  1177. restart->nanosleep.expires = expires;
  1178. if (restart->nanosleep.type != TT_NONE)
  1179. error = nanosleep_copyout(restart, &it.it_value);
  1180. }
  1181. return error;
  1182. }
  1183. static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
  1184. static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
  1185. const struct timespec64 *rqtp)
  1186. {
  1187. struct restart_block *restart_block = &current->restart_block;
  1188. int error;
  1189. /*
  1190. * Diagnose required errors first.
  1191. */
  1192. if (CPUCLOCK_PERTHREAD(which_clock) &&
  1193. (CPUCLOCK_PID(which_clock) == 0 ||
  1194. CPUCLOCK_PID(which_clock) == task_pid_vnr(current)))
  1195. return -EINVAL;
  1196. error = do_cpu_nanosleep(which_clock, flags, rqtp);
  1197. if (error == -ERESTART_RESTARTBLOCK) {
  1198. if (flags & TIMER_ABSTIME)
  1199. return -ERESTARTNOHAND;
  1200. restart_block->fn = posix_cpu_nsleep_restart;
  1201. restart_block->nanosleep.clockid = which_clock;
  1202. }
  1203. return error;
  1204. }
  1205. static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
  1206. {
  1207. clockid_t which_clock = restart_block->nanosleep.clockid;
  1208. struct timespec64 t;
  1209. t = ns_to_timespec64(restart_block->nanosleep.expires);
  1210. return do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t);
  1211. }
  1212. #define PROCESS_CLOCK make_process_cpuclock(0, CPUCLOCK_SCHED)
  1213. #define THREAD_CLOCK make_thread_cpuclock(0, CPUCLOCK_SCHED)
  1214. static int process_cpu_clock_getres(const clockid_t which_clock,
  1215. struct timespec64 *tp)
  1216. {
  1217. return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
  1218. }
  1219. static int process_cpu_clock_get(const clockid_t which_clock,
  1220. struct timespec64 *tp)
  1221. {
  1222. return posix_cpu_clock_get(PROCESS_CLOCK, tp);
  1223. }
  1224. static int process_cpu_timer_create(struct k_itimer *timer)
  1225. {
  1226. timer->it_clock = PROCESS_CLOCK;
  1227. return posix_cpu_timer_create(timer);
  1228. }
  1229. static int process_cpu_nsleep(const clockid_t which_clock, int flags,
  1230. const struct timespec64 *rqtp)
  1231. {
  1232. return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
  1233. }
  1234. static int thread_cpu_clock_getres(const clockid_t which_clock,
  1235. struct timespec64 *tp)
  1236. {
  1237. return posix_cpu_clock_getres(THREAD_CLOCK, tp);
  1238. }
  1239. static int thread_cpu_clock_get(const clockid_t which_clock,
  1240. struct timespec64 *tp)
  1241. {
  1242. return posix_cpu_clock_get(THREAD_CLOCK, tp);
  1243. }
  1244. static int thread_cpu_timer_create(struct k_itimer *timer)
  1245. {
  1246. timer->it_clock = THREAD_CLOCK;
  1247. return posix_cpu_timer_create(timer);
  1248. }
  1249. const struct k_clock clock_posix_cpu = {
  1250. .clock_getres = posix_cpu_clock_getres,
  1251. .clock_set = posix_cpu_clock_set,
  1252. .clock_get = posix_cpu_clock_get,
  1253. .timer_create = posix_cpu_timer_create,
  1254. .nsleep = posix_cpu_nsleep,
  1255. .timer_set = posix_cpu_timer_set,
  1256. .timer_del = posix_cpu_timer_del,
  1257. .timer_get = posix_cpu_timer_get,
  1258. .timer_rearm = posix_cpu_timer_rearm,
  1259. };
  1260. const struct k_clock clock_process = {
  1261. .clock_getres = process_cpu_clock_getres,
  1262. .clock_get = process_cpu_clock_get,
  1263. .timer_create = process_cpu_timer_create,
  1264. .nsleep = process_cpu_nsleep,
  1265. };
  1266. const struct k_clock clock_thread = {
  1267. .clock_getres = thread_cpu_clock_getres,
  1268. .clock_get = thread_cpu_clock_get,
  1269. .timer_create = thread_cpu_timer_create,
  1270. };