posix-cpu-timers.c 41 KB

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