posix-cpu-timers.c 43 KB

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