posix-cpu-timers.c 41 KB

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