posix-cpu-timers.c 37 KB

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