posix-cpu-timers.c 45 KB

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