posix-cpu-timers.c 44 KB

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