posix-cpu-timers.c 44 KB

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