posix-cpu-timers.c 38 KB

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