posix-cpu-timers.c 39 KB

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