posix-cpu-timers.c 38 KB

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