posix-cpu-timers.c 44 KB

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