posix-cpu-timers.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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 += 1LL << 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. }
  453. }
  454. /*
  455. * The timer is locked, fire it and arrange for its reload.
  456. */
  457. static void cpu_timer_fire(struct k_itimer *timer)
  458. {
  459. if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
  460. /*
  461. * User don't want any signal.
  462. */
  463. timer->it.cpu.expires = 0;
  464. } else if (unlikely(timer->sigq == NULL)) {
  465. /*
  466. * This a special case for clock_nanosleep,
  467. * not a normal timer from sys_timer_create.
  468. */
  469. wake_up_process(timer->it_process);
  470. timer->it.cpu.expires = 0;
  471. } else if (timer->it.cpu.incr == 0) {
  472. /*
  473. * One-shot timer. Clear it as soon as it's fired.
  474. */
  475. posix_timer_event(timer, 0);
  476. timer->it.cpu.expires = 0;
  477. } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
  478. /*
  479. * The signal did not get queued because the signal
  480. * was ignored, so we won't get any callback to
  481. * reload the timer. But we need to keep it
  482. * ticking in case the signal is deliverable next time.
  483. */
  484. posix_cpu_timer_schedule(timer);
  485. }
  486. }
  487. /*
  488. * Sample a process (thread group) timer for the given group_leader task.
  489. * Must be called with task sighand lock held for safe while_each_thread()
  490. * traversal.
  491. */
  492. static int cpu_timer_sample_group(const clockid_t which_clock,
  493. struct task_struct *p,
  494. unsigned long long *sample)
  495. {
  496. struct task_cputime cputime;
  497. thread_group_cputimer(p, &cputime);
  498. switch (CPUCLOCK_WHICH(which_clock)) {
  499. default:
  500. return -EINVAL;
  501. case CPUCLOCK_PROF:
  502. *sample = cputime_to_expires(cputime.utime + cputime.stime);
  503. break;
  504. case CPUCLOCK_VIRT:
  505. *sample = cputime_to_expires(cputime.utime);
  506. break;
  507. case CPUCLOCK_SCHED:
  508. *sample = cputime.sum_exec_runtime;
  509. break;
  510. }
  511. return 0;
  512. }
  513. #ifdef CONFIG_NO_HZ_FULL
  514. static void nohz_kick_work_fn(struct work_struct *work)
  515. {
  516. tick_nohz_full_kick_all();
  517. }
  518. static DECLARE_WORK(nohz_kick_work, nohz_kick_work_fn);
  519. /*
  520. * We need the IPIs to be sent from sane process context.
  521. * The posix cpu timers are always set with irqs disabled.
  522. */
  523. static void posix_cpu_timer_kick_nohz(void)
  524. {
  525. if (context_tracking_is_enabled())
  526. schedule_work(&nohz_kick_work);
  527. }
  528. bool posix_cpu_timers_can_stop_tick(struct task_struct *tsk)
  529. {
  530. if (!task_cputime_zero(&tsk->cputime_expires))
  531. return false;
  532. /* Check if cputimer is running. This is accessed without locking. */
  533. if (READ_ONCE(tsk->signal->cputimer.running))
  534. return false;
  535. return true;
  536. }
  537. #else
  538. static inline void posix_cpu_timer_kick_nohz(void) { }
  539. #endif
  540. /*
  541. * Guts of sys_timer_settime for CPU timers.
  542. * This is called with the timer locked and interrupts disabled.
  543. * If we return TIMER_RETRY, it's necessary to release the timer's lock
  544. * and try again. (This happens when the timer is in the middle of firing.)
  545. */
  546. static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
  547. struct itimerspec *new, struct itimerspec *old)
  548. {
  549. unsigned long flags;
  550. struct sighand_struct *sighand;
  551. struct task_struct *p = timer->it.cpu.task;
  552. unsigned long long old_expires, new_expires, old_incr, val;
  553. int ret;
  554. WARN_ON_ONCE(p == NULL);
  555. new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
  556. /*
  557. * Protect against sighand release/switch in exit/exec and p->cpu_timers
  558. * and p->signal->cpu_timers read/write in arm_timer()
  559. */
  560. sighand = lock_task_sighand(p, &flags);
  561. /*
  562. * If p has just been reaped, we can no
  563. * longer get any information about it at all.
  564. */
  565. if (unlikely(sighand == NULL)) {
  566. return -ESRCH;
  567. }
  568. /*
  569. * Disarm any old timer after extracting its expiry time.
  570. */
  571. WARN_ON_ONCE(!irqs_disabled());
  572. ret = 0;
  573. old_incr = timer->it.cpu.incr;
  574. old_expires = timer->it.cpu.expires;
  575. if (unlikely(timer->it.cpu.firing)) {
  576. timer->it.cpu.firing = -1;
  577. ret = TIMER_RETRY;
  578. } else
  579. list_del_init(&timer->it.cpu.entry);
  580. /*
  581. * We need to sample the current value to convert the new
  582. * value from to relative and absolute, and to convert the
  583. * old value from absolute to relative. To set a process
  584. * timer, we need a sample to balance the thread expiry
  585. * times (in arm_timer). With an absolute time, we must
  586. * check if it's already passed. In short, we need a sample.
  587. */
  588. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  589. cpu_clock_sample(timer->it_clock, p, &val);
  590. } else {
  591. cpu_timer_sample_group(timer->it_clock, p, &val);
  592. }
  593. if (old) {
  594. if (old_expires == 0) {
  595. old->it_value.tv_sec = 0;
  596. old->it_value.tv_nsec = 0;
  597. } else {
  598. /*
  599. * Update the timer in case it has
  600. * overrun already. If it has,
  601. * we'll report it as having overrun
  602. * and with the next reloaded timer
  603. * already ticking, though we are
  604. * swallowing that pending
  605. * notification here to install the
  606. * new setting.
  607. */
  608. bump_cpu_timer(timer, val);
  609. if (val < timer->it.cpu.expires) {
  610. old_expires = timer->it.cpu.expires - val;
  611. sample_to_timespec(timer->it_clock,
  612. old_expires,
  613. &old->it_value);
  614. } else {
  615. old->it_value.tv_nsec = 1;
  616. old->it_value.tv_sec = 0;
  617. }
  618. }
  619. }
  620. if (unlikely(ret)) {
  621. /*
  622. * We are colliding with the timer actually firing.
  623. * Punt after filling in the timer's old value, and
  624. * disable this firing since we are already reporting
  625. * it as an overrun (thanks to bump_cpu_timer above).
  626. */
  627. unlock_task_sighand(p, &flags);
  628. goto out;
  629. }
  630. if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) {
  631. new_expires += val;
  632. }
  633. /*
  634. * Install the new expiry time (or zero).
  635. * For a timer with no notification action, we don't actually
  636. * arm the timer (we'll just fake it for timer_gettime).
  637. */
  638. timer->it.cpu.expires = new_expires;
  639. if (new_expires != 0 && val < new_expires) {
  640. arm_timer(timer);
  641. }
  642. unlock_task_sighand(p, &flags);
  643. /*
  644. * Install the new reload setting, and
  645. * set up the signal and overrun bookkeeping.
  646. */
  647. timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
  648. &new->it_interval);
  649. /*
  650. * This acts as a modification timestamp for the timer,
  651. * so any automatic reload attempt will punt on seeing
  652. * that we have reset the timer manually.
  653. */
  654. timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
  655. ~REQUEUE_PENDING;
  656. timer->it_overrun_last = 0;
  657. timer->it_overrun = -1;
  658. if (new_expires != 0 && !(val < new_expires)) {
  659. /*
  660. * The designated time already passed, so we notify
  661. * immediately, even if the thread never runs to
  662. * accumulate more time on this clock.
  663. */
  664. cpu_timer_fire(timer);
  665. }
  666. ret = 0;
  667. out:
  668. if (old) {
  669. sample_to_timespec(timer->it_clock,
  670. old_incr, &old->it_interval);
  671. }
  672. if (!ret)
  673. posix_cpu_timer_kick_nohz();
  674. return ret;
  675. }
  676. static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
  677. {
  678. unsigned long long now;
  679. struct task_struct *p = timer->it.cpu.task;
  680. WARN_ON_ONCE(p == NULL);
  681. /*
  682. * Easy part: convert the reload time.
  683. */
  684. sample_to_timespec(timer->it_clock,
  685. timer->it.cpu.incr, &itp->it_interval);
  686. if (timer->it.cpu.expires == 0) { /* Timer not armed at all. */
  687. itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
  688. return;
  689. }
  690. /*
  691. * Sample the clock to take the difference with the expiry time.
  692. */
  693. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  694. cpu_clock_sample(timer->it_clock, p, &now);
  695. } else {
  696. struct sighand_struct *sighand;
  697. unsigned long flags;
  698. /*
  699. * Protect against sighand release/switch in exit/exec and
  700. * also make timer sampling safe if it ends up calling
  701. * thread_group_cputime().
  702. */
  703. sighand = lock_task_sighand(p, &flags);
  704. if (unlikely(sighand == NULL)) {
  705. /*
  706. * The process has been reaped.
  707. * We can't even collect a sample any more.
  708. * Call the timer disarmed, nothing else to do.
  709. */
  710. timer->it.cpu.expires = 0;
  711. sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
  712. &itp->it_value);
  713. return;
  714. } else {
  715. cpu_timer_sample_group(timer->it_clock, p, &now);
  716. unlock_task_sighand(p, &flags);
  717. }
  718. }
  719. if (now < timer->it.cpu.expires) {
  720. sample_to_timespec(timer->it_clock,
  721. timer->it.cpu.expires - now,
  722. &itp->it_value);
  723. } else {
  724. /*
  725. * The timer should have expired already, but the firing
  726. * hasn't taken place yet. Say it's just about to expire.
  727. */
  728. itp->it_value.tv_nsec = 1;
  729. itp->it_value.tv_sec = 0;
  730. }
  731. }
  732. static unsigned long long
  733. check_timers_list(struct list_head *timers,
  734. struct list_head *firing,
  735. unsigned long long curr)
  736. {
  737. int maxfire = 20;
  738. while (!list_empty(timers)) {
  739. struct cpu_timer_list *t;
  740. t = list_first_entry(timers, struct cpu_timer_list, entry);
  741. if (!--maxfire || curr < t->expires)
  742. return t->expires;
  743. t->firing = 1;
  744. list_move_tail(&t->entry, firing);
  745. }
  746. return 0;
  747. }
  748. /*
  749. * Check for any per-thread CPU timers that have fired and move them off
  750. * the tsk->cpu_timers[N] list onto the firing list. Here we update the
  751. * tsk->it_*_expires values to reflect the remaining thread CPU timers.
  752. */
  753. static void check_thread_timers(struct task_struct *tsk,
  754. struct list_head *firing)
  755. {
  756. struct list_head *timers = tsk->cpu_timers;
  757. struct signal_struct *const sig = tsk->signal;
  758. struct task_cputime *tsk_expires = &tsk->cputime_expires;
  759. unsigned long long expires;
  760. unsigned long soft;
  761. /*
  762. * If cputime_expires is zero, then there are no active
  763. * per thread CPU timers.
  764. */
  765. if (task_cputime_zero(&tsk->cputime_expires))
  766. return;
  767. expires = check_timers_list(timers, firing, prof_ticks(tsk));
  768. tsk_expires->prof_exp = expires_to_cputime(expires);
  769. expires = check_timers_list(++timers, firing, virt_ticks(tsk));
  770. tsk_expires->virt_exp = expires_to_cputime(expires);
  771. tsk_expires->sched_exp = check_timers_list(++timers, firing,
  772. tsk->se.sum_exec_runtime);
  773. /*
  774. * Check for the special case thread timers.
  775. */
  776. soft = READ_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
  777. if (soft != RLIM_INFINITY) {
  778. unsigned long hard =
  779. READ_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
  780. if (hard != RLIM_INFINITY &&
  781. tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
  782. /*
  783. * At the hard limit, we just die.
  784. * No need to calculate anything else now.
  785. */
  786. __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
  787. return;
  788. }
  789. if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
  790. /*
  791. * At the soft limit, send a SIGXCPU every second.
  792. */
  793. if (soft < hard) {
  794. soft += USEC_PER_SEC;
  795. sig->rlim[RLIMIT_RTTIME].rlim_cur = soft;
  796. }
  797. printk(KERN_INFO
  798. "RT Watchdog Timeout: %s[%d]\n",
  799. tsk->comm, task_pid_nr(tsk));
  800. __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
  801. }
  802. }
  803. }
  804. static inline void stop_process_timers(struct signal_struct *sig)
  805. {
  806. struct thread_group_cputimer *cputimer = &sig->cputimer;
  807. /* Turn off cputimer->running. This is done without locking. */
  808. WRITE_ONCE(cputimer->running, false);
  809. }
  810. static u32 onecputick;
  811. static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
  812. unsigned long long *expires,
  813. unsigned long long cur_time, int signo)
  814. {
  815. if (!it->expires)
  816. return;
  817. if (cur_time >= it->expires) {
  818. if (it->incr) {
  819. it->expires += it->incr;
  820. it->error += it->incr_error;
  821. if (it->error >= onecputick) {
  822. it->expires -= cputime_one_jiffy;
  823. it->error -= onecputick;
  824. }
  825. } else {
  826. it->expires = 0;
  827. }
  828. trace_itimer_expire(signo == SIGPROF ?
  829. ITIMER_PROF : ITIMER_VIRTUAL,
  830. tsk->signal->leader_pid, cur_time);
  831. __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
  832. }
  833. if (it->expires && (!*expires || it->expires < *expires)) {
  834. *expires = it->expires;
  835. }
  836. }
  837. /*
  838. * Check for any per-thread CPU timers that have fired and move them
  839. * off the tsk->*_timers list onto the firing list. Per-thread timers
  840. * have already been taken off.
  841. */
  842. static void check_process_timers(struct task_struct *tsk,
  843. struct list_head *firing)
  844. {
  845. struct signal_struct *const sig = tsk->signal;
  846. unsigned long long utime, ptime, virt_expires, prof_expires;
  847. unsigned long long sum_sched_runtime, sched_expires;
  848. struct list_head *timers = sig->cpu_timers;
  849. struct task_cputime cputime;
  850. unsigned long soft;
  851. /*
  852. * If cputimer is not running, then there are no active
  853. * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU).
  854. */
  855. if (!READ_ONCE(tsk->signal->cputimer.running))
  856. return;
  857. /*
  858. * Signify that a thread is checking for process timers.
  859. * Write access to this field is protected by the sighand lock.
  860. */
  861. sig->cputimer.checking_timer = true;
  862. /*
  863. * Collect the current process totals.
  864. */
  865. thread_group_cputimer(tsk, &cputime);
  866. utime = cputime_to_expires(cputime.utime);
  867. ptime = utime + cputime_to_expires(cputime.stime);
  868. sum_sched_runtime = cputime.sum_exec_runtime;
  869. prof_expires = check_timers_list(timers, firing, ptime);
  870. virt_expires = check_timers_list(++timers, firing, utime);
  871. sched_expires = check_timers_list(++timers, firing, sum_sched_runtime);
  872. /*
  873. * Check for the special case process timers.
  874. */
  875. check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
  876. SIGPROF);
  877. check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
  878. SIGVTALRM);
  879. soft = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
  880. if (soft != RLIM_INFINITY) {
  881. unsigned long psecs = cputime_to_secs(ptime);
  882. unsigned long hard =
  883. READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
  884. cputime_t x;
  885. if (psecs >= hard) {
  886. /*
  887. * At the hard limit, we just die.
  888. * No need to calculate anything else now.
  889. */
  890. __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
  891. return;
  892. }
  893. if (psecs >= soft) {
  894. /*
  895. * At the soft limit, send a SIGXCPU every second.
  896. */
  897. __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
  898. if (soft < hard) {
  899. soft++;
  900. sig->rlim[RLIMIT_CPU].rlim_cur = soft;
  901. }
  902. }
  903. x = secs_to_cputime(soft);
  904. if (!prof_expires || x < prof_expires) {
  905. prof_expires = x;
  906. }
  907. }
  908. sig->cputime_expires.prof_exp = expires_to_cputime(prof_expires);
  909. sig->cputime_expires.virt_exp = expires_to_cputime(virt_expires);
  910. sig->cputime_expires.sched_exp = sched_expires;
  911. if (task_cputime_zero(&sig->cputime_expires))
  912. stop_process_timers(sig);
  913. sig->cputimer.checking_timer = false;
  914. }
  915. /*
  916. * This is called from the signal code (via do_schedule_next_timer)
  917. * when the last timer signal was delivered and we have to reload the timer.
  918. */
  919. void posix_cpu_timer_schedule(struct k_itimer *timer)
  920. {
  921. struct sighand_struct *sighand;
  922. unsigned long flags;
  923. struct task_struct *p = timer->it.cpu.task;
  924. unsigned long long now;
  925. WARN_ON_ONCE(p == NULL);
  926. /*
  927. * Fetch the current sample and update the timer's expiry time.
  928. */
  929. if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
  930. cpu_clock_sample(timer->it_clock, p, &now);
  931. bump_cpu_timer(timer, now);
  932. if (unlikely(p->exit_state))
  933. goto out;
  934. /* Protect timer list r/w in arm_timer() */
  935. sighand = lock_task_sighand(p, &flags);
  936. if (!sighand)
  937. goto out;
  938. } else {
  939. /*
  940. * Protect arm_timer() and timer sampling in case of call to
  941. * thread_group_cputime().
  942. */
  943. sighand = lock_task_sighand(p, &flags);
  944. if (unlikely(sighand == NULL)) {
  945. /*
  946. * The process has been reaped.
  947. * We can't even collect a sample any more.
  948. */
  949. timer->it.cpu.expires = 0;
  950. goto out;
  951. } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
  952. unlock_task_sighand(p, &flags);
  953. /* Optimizations: if the process is dying, no need to rearm */
  954. goto out;
  955. }
  956. cpu_timer_sample_group(timer->it_clock, p, &now);
  957. bump_cpu_timer(timer, now);
  958. /* Leave the sighand locked for the call below. */
  959. }
  960. /*
  961. * Now re-arm for the new expiry time.
  962. */
  963. WARN_ON_ONCE(!irqs_disabled());
  964. arm_timer(timer);
  965. unlock_task_sighand(p, &flags);
  966. /* Kick full dynticks CPUs in case they need to tick on the new timer */
  967. posix_cpu_timer_kick_nohz();
  968. out:
  969. timer->it_overrun_last = timer->it_overrun;
  970. timer->it_overrun = -1;
  971. ++timer->it_requeue_pending;
  972. }
  973. /**
  974. * task_cputime_expired - Compare two task_cputime entities.
  975. *
  976. * @sample: The task_cputime structure to be checked for expiration.
  977. * @expires: Expiration times, against which @sample will be checked.
  978. *
  979. * Checks @sample against @expires to see if any field of @sample has expired.
  980. * Returns true if any field of the former is greater than the corresponding
  981. * field of the latter if the latter field is set. Otherwise returns false.
  982. */
  983. static inline int task_cputime_expired(const struct task_cputime *sample,
  984. const struct task_cputime *expires)
  985. {
  986. if (expires->utime && sample->utime >= expires->utime)
  987. return 1;
  988. if (expires->stime && sample->utime + sample->stime >= expires->stime)
  989. return 1;
  990. if (expires->sum_exec_runtime != 0 &&
  991. sample->sum_exec_runtime >= expires->sum_exec_runtime)
  992. return 1;
  993. return 0;
  994. }
  995. /**
  996. * fastpath_timer_check - POSIX CPU timers fast path.
  997. *
  998. * @tsk: The task (thread) being checked.
  999. *
  1000. * Check the task and thread group timers. If both are zero (there are no
  1001. * timers set) return false. Otherwise snapshot the task and thread group
  1002. * timers and compare them with the corresponding expiration times. Return
  1003. * true if a timer has expired, else return false.
  1004. */
  1005. static inline int fastpath_timer_check(struct task_struct *tsk)
  1006. {
  1007. struct signal_struct *sig;
  1008. if (!task_cputime_zero(&tsk->cputime_expires)) {
  1009. struct task_cputime task_sample;
  1010. task_cputime(tsk, &task_sample.utime, &task_sample.stime);
  1011. task_sample.sum_exec_runtime = tsk->se.sum_exec_runtime;
  1012. if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
  1013. return 1;
  1014. }
  1015. sig = tsk->signal;
  1016. /*
  1017. * Check if thread group timers expired when the cputimer is
  1018. * running and no other thread in the group is already checking
  1019. * for thread group cputimers. These fields are read without the
  1020. * sighand lock. However, this is fine because this is meant to
  1021. * be a fastpath heuristic to determine whether we should try to
  1022. * acquire the sighand lock to check/handle timers.
  1023. *
  1024. * In the worst case scenario, if 'running' or 'checking_timer' gets
  1025. * set but the current thread doesn't see the change yet, we'll wait
  1026. * until the next thread in the group gets a scheduler interrupt to
  1027. * handle the timer. This isn't an issue in practice because these
  1028. * types of delays with signals actually getting sent are expected.
  1029. */
  1030. if (READ_ONCE(sig->cputimer.running) &&
  1031. !READ_ONCE(sig->cputimer.checking_timer)) {
  1032. struct task_cputime group_sample;
  1033. sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic);
  1034. if (task_cputime_expired(&group_sample, &sig->cputime_expires))
  1035. return 1;
  1036. }
  1037. return 0;
  1038. }
  1039. /*
  1040. * This is called from the timer interrupt handler. The irq handler has
  1041. * already updated our counts. We need to check if any timers fire now.
  1042. * Interrupts are disabled.
  1043. */
  1044. void run_posix_cpu_timers(struct task_struct *tsk)
  1045. {
  1046. LIST_HEAD(firing);
  1047. struct k_itimer *timer, *next;
  1048. unsigned long flags;
  1049. WARN_ON_ONCE(!irqs_disabled());
  1050. /*
  1051. * The fast path checks that there are no expired thread or thread
  1052. * group timers. If that's so, just return.
  1053. */
  1054. if (!fastpath_timer_check(tsk))
  1055. return;
  1056. if (!lock_task_sighand(tsk, &flags))
  1057. return;
  1058. /*
  1059. * Here we take off tsk->signal->cpu_timers[N] and
  1060. * tsk->cpu_timers[N] all the timers that are firing, and
  1061. * put them on the firing list.
  1062. */
  1063. check_thread_timers(tsk, &firing);
  1064. check_process_timers(tsk, &firing);
  1065. /*
  1066. * We must release these locks before taking any timer's lock.
  1067. * There is a potential race with timer deletion here, as the
  1068. * siglock now protects our private firing list. We have set
  1069. * the firing flag in each timer, so that a deletion attempt
  1070. * that gets the timer lock before we do will give it up and
  1071. * spin until we've taken care of that timer below.
  1072. */
  1073. unlock_task_sighand(tsk, &flags);
  1074. /*
  1075. * Now that all the timers on our list have the firing flag,
  1076. * no one will touch their list entries but us. We'll take
  1077. * each timer's lock before clearing its firing flag, so no
  1078. * timer call will interfere.
  1079. */
  1080. list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
  1081. int cpu_firing;
  1082. spin_lock(&timer->it_lock);
  1083. list_del_init(&timer->it.cpu.entry);
  1084. cpu_firing = timer->it.cpu.firing;
  1085. timer->it.cpu.firing = 0;
  1086. /*
  1087. * The firing flag is -1 if we collided with a reset
  1088. * of the timer, which already reported this
  1089. * almost-firing as an overrun. So don't generate an event.
  1090. */
  1091. if (likely(cpu_firing >= 0))
  1092. cpu_timer_fire(timer);
  1093. spin_unlock(&timer->it_lock);
  1094. }
  1095. }
  1096. /*
  1097. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  1098. * The tsk->sighand->siglock must be held by the caller.
  1099. */
  1100. void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
  1101. cputime_t *newval, cputime_t *oldval)
  1102. {
  1103. unsigned long long now;
  1104. WARN_ON_ONCE(clock_idx == CPUCLOCK_SCHED);
  1105. cpu_timer_sample_group(clock_idx, tsk, &now);
  1106. if (oldval) {
  1107. /*
  1108. * We are setting itimer. The *oldval is absolute and we update
  1109. * it to be relative, *newval argument is relative and we update
  1110. * it to be absolute.
  1111. */
  1112. if (*oldval) {
  1113. if (*oldval <= now) {
  1114. /* Just about to fire. */
  1115. *oldval = cputime_one_jiffy;
  1116. } else {
  1117. *oldval -= now;
  1118. }
  1119. }
  1120. if (!*newval)
  1121. goto out;
  1122. *newval += now;
  1123. }
  1124. /*
  1125. * Update expiration cache if we are the earliest timer, or eventually
  1126. * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
  1127. */
  1128. switch (clock_idx) {
  1129. case CPUCLOCK_PROF:
  1130. if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
  1131. tsk->signal->cputime_expires.prof_exp = *newval;
  1132. break;
  1133. case CPUCLOCK_VIRT:
  1134. if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
  1135. tsk->signal->cputime_expires.virt_exp = *newval;
  1136. break;
  1137. }
  1138. out:
  1139. posix_cpu_timer_kick_nohz();
  1140. }
  1141. static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
  1142. struct timespec *rqtp, struct itimerspec *it)
  1143. {
  1144. struct k_itimer timer;
  1145. int error;
  1146. /*
  1147. * Set up a temporary timer and then wait for it to go off.
  1148. */
  1149. memset(&timer, 0, sizeof timer);
  1150. spin_lock_init(&timer.it_lock);
  1151. timer.it_clock = which_clock;
  1152. timer.it_overrun = -1;
  1153. error = posix_cpu_timer_create(&timer);
  1154. timer.it_process = current;
  1155. if (!error) {
  1156. static struct itimerspec zero_it;
  1157. memset(it, 0, sizeof *it);
  1158. it->it_value = *rqtp;
  1159. spin_lock_irq(&timer.it_lock);
  1160. error = posix_cpu_timer_set(&timer, flags, it, NULL);
  1161. if (error) {
  1162. spin_unlock_irq(&timer.it_lock);
  1163. return error;
  1164. }
  1165. while (!signal_pending(current)) {
  1166. if (timer.it.cpu.expires == 0) {
  1167. /*
  1168. * Our timer fired and was reset, below
  1169. * deletion can not fail.
  1170. */
  1171. posix_cpu_timer_del(&timer);
  1172. spin_unlock_irq(&timer.it_lock);
  1173. return 0;
  1174. }
  1175. /*
  1176. * Block until cpu_timer_fire (or a signal) wakes us.
  1177. */
  1178. __set_current_state(TASK_INTERRUPTIBLE);
  1179. spin_unlock_irq(&timer.it_lock);
  1180. schedule();
  1181. spin_lock_irq(&timer.it_lock);
  1182. }
  1183. /*
  1184. * We were interrupted by a signal.
  1185. */
  1186. sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
  1187. error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
  1188. if (!error) {
  1189. /*
  1190. * Timer is now unarmed, deletion can not fail.
  1191. */
  1192. posix_cpu_timer_del(&timer);
  1193. }
  1194. spin_unlock_irq(&timer.it_lock);
  1195. while (error == TIMER_RETRY) {
  1196. /*
  1197. * We need to handle case when timer was or is in the
  1198. * middle of firing. In other cases we already freed
  1199. * resources.
  1200. */
  1201. spin_lock_irq(&timer.it_lock);
  1202. error = posix_cpu_timer_del(&timer);
  1203. spin_unlock_irq(&timer.it_lock);
  1204. }
  1205. if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
  1206. /*
  1207. * It actually did fire already.
  1208. */
  1209. return 0;
  1210. }
  1211. error = -ERESTART_RESTARTBLOCK;
  1212. }
  1213. return error;
  1214. }
  1215. static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
  1216. static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
  1217. struct timespec *rqtp, struct timespec __user *rmtp)
  1218. {
  1219. struct restart_block *restart_block = &current->restart_block;
  1220. struct itimerspec it;
  1221. int error;
  1222. /*
  1223. * Diagnose required errors first.
  1224. */
  1225. if (CPUCLOCK_PERTHREAD(which_clock) &&
  1226. (CPUCLOCK_PID(which_clock) == 0 ||
  1227. CPUCLOCK_PID(which_clock) == current->pid))
  1228. return -EINVAL;
  1229. error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
  1230. if (error == -ERESTART_RESTARTBLOCK) {
  1231. if (flags & TIMER_ABSTIME)
  1232. return -ERESTARTNOHAND;
  1233. /*
  1234. * Report back to the user the time still remaining.
  1235. */
  1236. if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
  1237. return -EFAULT;
  1238. restart_block->fn = posix_cpu_nsleep_restart;
  1239. restart_block->nanosleep.clockid = which_clock;
  1240. restart_block->nanosleep.rmtp = rmtp;
  1241. restart_block->nanosleep.expires = timespec_to_ns(rqtp);
  1242. }
  1243. return error;
  1244. }
  1245. static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
  1246. {
  1247. clockid_t which_clock = restart_block->nanosleep.clockid;
  1248. struct timespec t;
  1249. struct itimerspec it;
  1250. int error;
  1251. t = ns_to_timespec(restart_block->nanosleep.expires);
  1252. error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
  1253. if (error == -ERESTART_RESTARTBLOCK) {
  1254. struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
  1255. /*
  1256. * Report back to the user the time still remaining.
  1257. */
  1258. if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
  1259. return -EFAULT;
  1260. restart_block->nanosleep.expires = timespec_to_ns(&t);
  1261. }
  1262. return error;
  1263. }
  1264. #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
  1265. #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
  1266. static int process_cpu_clock_getres(const clockid_t which_clock,
  1267. struct timespec *tp)
  1268. {
  1269. return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
  1270. }
  1271. static int process_cpu_clock_get(const clockid_t which_clock,
  1272. struct timespec *tp)
  1273. {
  1274. return posix_cpu_clock_get(PROCESS_CLOCK, tp);
  1275. }
  1276. static int process_cpu_timer_create(struct k_itimer *timer)
  1277. {
  1278. timer->it_clock = PROCESS_CLOCK;
  1279. return posix_cpu_timer_create(timer);
  1280. }
  1281. static int process_cpu_nsleep(const clockid_t which_clock, int flags,
  1282. struct timespec *rqtp,
  1283. struct timespec __user *rmtp)
  1284. {
  1285. return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
  1286. }
  1287. static long process_cpu_nsleep_restart(struct restart_block *restart_block)
  1288. {
  1289. return -EINVAL;
  1290. }
  1291. static int thread_cpu_clock_getres(const clockid_t which_clock,
  1292. struct timespec *tp)
  1293. {
  1294. return posix_cpu_clock_getres(THREAD_CLOCK, tp);
  1295. }
  1296. static int thread_cpu_clock_get(const clockid_t which_clock,
  1297. struct timespec *tp)
  1298. {
  1299. return posix_cpu_clock_get(THREAD_CLOCK, tp);
  1300. }
  1301. static int thread_cpu_timer_create(struct k_itimer *timer)
  1302. {
  1303. timer->it_clock = THREAD_CLOCK;
  1304. return posix_cpu_timer_create(timer);
  1305. }
  1306. struct k_clock clock_posix_cpu = {
  1307. .clock_getres = posix_cpu_clock_getres,
  1308. .clock_set = posix_cpu_clock_set,
  1309. .clock_get = posix_cpu_clock_get,
  1310. .timer_create = posix_cpu_timer_create,
  1311. .nsleep = posix_cpu_nsleep,
  1312. .nsleep_restart = posix_cpu_nsleep_restart,
  1313. .timer_set = posix_cpu_timer_set,
  1314. .timer_del = posix_cpu_timer_del,
  1315. .timer_get = posix_cpu_timer_get,
  1316. };
  1317. static __init int init_posix_cpu_timers(void)
  1318. {
  1319. struct k_clock process = {
  1320. .clock_getres = process_cpu_clock_getres,
  1321. .clock_get = process_cpu_clock_get,
  1322. .timer_create = process_cpu_timer_create,
  1323. .nsleep = process_cpu_nsleep,
  1324. .nsleep_restart = process_cpu_nsleep_restart,
  1325. };
  1326. struct k_clock thread = {
  1327. .clock_getres = thread_cpu_clock_getres,
  1328. .clock_get = thread_cpu_clock_get,
  1329. .timer_create = thread_cpu_timer_create,
  1330. };
  1331. struct timespec ts;
  1332. posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
  1333. posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
  1334. cputime_to_timespec(cputime_one_jiffy, &ts);
  1335. onecputick = ts.tv_nsec;
  1336. WARN_ON(ts.tv_sec != 0);
  1337. return 0;
  1338. }
  1339. __initcall(init_posix_cpu_timers);