posix-cpu-timers.c 41 KB

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