posix-cpu-timers.c 45 KB

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