posix-cpu-timers.c 41 KB

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