posix-cpu-timers.c 42 KB

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