posix-cpu-timers.c 42 KB

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