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