posix-cpu-timers.c 39 KB

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