posix-cpu-timers.c 43 KB

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