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