posix-cpu-timers.c 37 KB

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