posix-cpu-timers.c 42 KB

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