posix-cpu-timers.c 37 KB

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