posix-cpu-timers.c 45 KB

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