posix-cpu-timers.c 38 KB

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