posix-cpu-timers.c 41 KB

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