posix-cpu-timers.c 41 KB

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