posix-cpu-timers.c 44 KB

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