posix-cpu-timers.c 45 KB

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