itimer.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * linux/kernel/itimer.c
  3. *
  4. * Copyright (C) 1992 Darren Senn
  5. */
  6. /* These are all the functions necessary to implement itimers */
  7. #include <linux/mm.h>
  8. #include <linux/smp_lock.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/syscalls.h>
  11. #include <linux/time.h>
  12. #include <linux/posix-timers.h>
  13. #include <linux/hrtimer.h>
  14. #include <asm/uaccess.h>
  15. /**
  16. * itimer_get_remtime - get remaining time for the timer
  17. *
  18. * @timer: the timer to read
  19. *
  20. * Returns the delta between the expiry time and now, which can be
  21. * less than zero or 1usec for an pending expired timer
  22. */
  23. static struct timeval itimer_get_remtime(struct hrtimer *timer)
  24. {
  25. ktime_t rem = hrtimer_get_remaining(timer);
  26. /*
  27. * Racy but safe: if the itimer expires after the above
  28. * hrtimer_get_remtime() call but before this condition
  29. * then we return 0 - which is correct.
  30. */
  31. if (hrtimer_active(timer)) {
  32. if (rem.tv64 <= 0)
  33. rem.tv64 = NSEC_PER_USEC;
  34. } else
  35. rem.tv64 = 0;
  36. return ktime_to_timeval(rem);
  37. }
  38. int do_getitimer(int which, struct itimerval *value)
  39. {
  40. struct task_struct *tsk = current;
  41. cputime_t cinterval, cval;
  42. switch (which) {
  43. case ITIMER_REAL:
  44. spin_lock_irq(&tsk->sighand->siglock);
  45. value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
  46. value->it_interval =
  47. ktime_to_timeval(tsk->signal->it_real_incr);
  48. spin_unlock_irq(&tsk->sighand->siglock);
  49. break;
  50. case ITIMER_VIRTUAL:
  51. read_lock(&tasklist_lock);
  52. spin_lock_irq(&tsk->sighand->siglock);
  53. cval = tsk->signal->it_virt_expires;
  54. cinterval = tsk->signal->it_virt_incr;
  55. if (!cputime_eq(cval, cputime_zero)) {
  56. struct task_struct *t = tsk;
  57. cputime_t utime = tsk->signal->utime;
  58. do {
  59. utime = cputime_add(utime, t->utime);
  60. t = next_thread(t);
  61. } while (t != tsk);
  62. if (cputime_le(cval, utime)) { /* about to fire */
  63. cval = jiffies_to_cputime(1);
  64. } else {
  65. cval = cputime_sub(cval, utime);
  66. }
  67. }
  68. spin_unlock_irq(&tsk->sighand->siglock);
  69. read_unlock(&tasklist_lock);
  70. cputime_to_timeval(cval, &value->it_value);
  71. cputime_to_timeval(cinterval, &value->it_interval);
  72. break;
  73. case ITIMER_PROF:
  74. read_lock(&tasklist_lock);
  75. spin_lock_irq(&tsk->sighand->siglock);
  76. cval = tsk->signal->it_prof_expires;
  77. cinterval = tsk->signal->it_prof_incr;
  78. if (!cputime_eq(cval, cputime_zero)) {
  79. struct task_struct *t = tsk;
  80. cputime_t ptime = cputime_add(tsk->signal->utime,
  81. tsk->signal->stime);
  82. do {
  83. ptime = cputime_add(ptime,
  84. cputime_add(t->utime,
  85. t->stime));
  86. t = next_thread(t);
  87. } while (t != tsk);
  88. if (cputime_le(cval, ptime)) { /* about to fire */
  89. cval = jiffies_to_cputime(1);
  90. } else {
  91. cval = cputime_sub(cval, ptime);
  92. }
  93. }
  94. spin_unlock_irq(&tsk->sighand->siglock);
  95. read_unlock(&tasklist_lock);
  96. cputime_to_timeval(cval, &value->it_value);
  97. cputime_to_timeval(cinterval, &value->it_interval);
  98. break;
  99. default:
  100. return(-EINVAL);
  101. }
  102. return 0;
  103. }
  104. asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
  105. {
  106. int error = -EFAULT;
  107. struct itimerval get_buffer;
  108. if (value) {
  109. error = do_getitimer(which, &get_buffer);
  110. if (!error &&
  111. copy_to_user(value, &get_buffer, sizeof(get_buffer)))
  112. error = -EFAULT;
  113. }
  114. return error;
  115. }
  116. /*
  117. * The timer is automagically restarted, when interval != 0
  118. */
  119. enum hrtimer_restart it_real_fn(struct hrtimer *timer)
  120. {
  121. struct signal_struct *sig =
  122. container_of(timer, struct signal_struct, real_timer);
  123. send_group_sig_info(SIGALRM, SEND_SIG_PRIV, sig->tsk);
  124. if (sig->it_real_incr.tv64 != 0) {
  125. hrtimer_forward(timer, hrtimer_cb_get_time(timer),
  126. sig->it_real_incr);
  127. return HRTIMER_RESTART;
  128. }
  129. return HRTIMER_NORESTART;
  130. }
  131. /*
  132. * We do not care about correctness. We just sanitize the values so
  133. * the ktime_t operations which expect normalized values do not
  134. * break. This converts negative values to long timeouts similar to
  135. * the code in kernel versions < 2.6.16
  136. *
  137. * Print a limited number of warning messages when an invalid timeval
  138. * is detected.
  139. */
  140. static void fixup_timeval(struct timeval *tv, int interval)
  141. {
  142. static int warnlimit = 10;
  143. unsigned long tmp;
  144. if (warnlimit > 0) {
  145. warnlimit--;
  146. printk(KERN_WARNING
  147. "setitimer: %s (pid = %d) provided "
  148. "invalid timeval %s: tv_sec = %ld tv_usec = %ld\n",
  149. current->comm, current->pid,
  150. interval ? "it_interval" : "it_value",
  151. tv->tv_sec, (long) tv->tv_usec);
  152. }
  153. tmp = tv->tv_usec;
  154. if (tmp >= USEC_PER_SEC) {
  155. tv->tv_usec = tmp % USEC_PER_SEC;
  156. tv->tv_sec += tmp / USEC_PER_SEC;
  157. }
  158. tmp = tv->tv_sec;
  159. if (tmp > LONG_MAX)
  160. tv->tv_sec = LONG_MAX;
  161. }
  162. /*
  163. * Returns true if the timeval is in canonical form
  164. */
  165. #define timeval_valid(t) \
  166. (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
  167. /*
  168. * Check for invalid timevals, sanitize them and print a limited
  169. * number of warnings.
  170. */
  171. static void check_itimerval(struct itimerval *value) {
  172. if (unlikely(!timeval_valid(&value->it_value)))
  173. fixup_timeval(&value->it_value, 0);
  174. if (unlikely(!timeval_valid(&value->it_interval)))
  175. fixup_timeval(&value->it_interval, 1);
  176. }
  177. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  178. {
  179. struct task_struct *tsk = current;
  180. struct hrtimer *timer;
  181. ktime_t expires;
  182. cputime_t cval, cinterval, nval, ninterval;
  183. /*
  184. * Validate the timevals in value.
  185. *
  186. * Note: Although the spec requires that invalid values shall
  187. * return -EINVAL, we just fixup the value and print a limited
  188. * number of warnings in order not to break users of this
  189. * historical misfeature.
  190. *
  191. * Scheduled for replacement in March 2007
  192. */
  193. check_itimerval(value);
  194. switch (which) {
  195. case ITIMER_REAL:
  196. again:
  197. spin_lock_irq(&tsk->sighand->siglock);
  198. timer = &tsk->signal->real_timer;
  199. if (ovalue) {
  200. ovalue->it_value = itimer_get_remtime(timer);
  201. ovalue->it_interval
  202. = ktime_to_timeval(tsk->signal->it_real_incr);
  203. }
  204. /* We are sharing ->siglock with it_real_fn() */
  205. if (hrtimer_try_to_cancel(timer) < 0) {
  206. spin_unlock_irq(&tsk->sighand->siglock);
  207. goto again;
  208. }
  209. tsk->signal->it_real_incr =
  210. timeval_to_ktime(value->it_interval);
  211. expires = timeval_to_ktime(value->it_value);
  212. if (expires.tv64 != 0)
  213. hrtimer_start(timer, expires, HRTIMER_MODE_REL);
  214. spin_unlock_irq(&tsk->sighand->siglock);
  215. break;
  216. case ITIMER_VIRTUAL:
  217. nval = timeval_to_cputime(&value->it_value);
  218. ninterval = timeval_to_cputime(&value->it_interval);
  219. read_lock(&tasklist_lock);
  220. spin_lock_irq(&tsk->sighand->siglock);
  221. cval = tsk->signal->it_virt_expires;
  222. cinterval = tsk->signal->it_virt_incr;
  223. if (!cputime_eq(cval, cputime_zero) ||
  224. !cputime_eq(nval, cputime_zero)) {
  225. if (cputime_gt(nval, cputime_zero))
  226. nval = cputime_add(nval,
  227. jiffies_to_cputime(1));
  228. set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
  229. &nval, &cval);
  230. }
  231. tsk->signal->it_virt_expires = nval;
  232. tsk->signal->it_virt_incr = ninterval;
  233. spin_unlock_irq(&tsk->sighand->siglock);
  234. read_unlock(&tasklist_lock);
  235. if (ovalue) {
  236. cputime_to_timeval(cval, &ovalue->it_value);
  237. cputime_to_timeval(cinterval, &ovalue->it_interval);
  238. }
  239. break;
  240. case ITIMER_PROF:
  241. nval = timeval_to_cputime(&value->it_value);
  242. ninterval = timeval_to_cputime(&value->it_interval);
  243. read_lock(&tasklist_lock);
  244. spin_lock_irq(&tsk->sighand->siglock);
  245. cval = tsk->signal->it_prof_expires;
  246. cinterval = tsk->signal->it_prof_incr;
  247. if (!cputime_eq(cval, cputime_zero) ||
  248. !cputime_eq(nval, cputime_zero)) {
  249. if (cputime_gt(nval, cputime_zero))
  250. nval = cputime_add(nval,
  251. jiffies_to_cputime(1));
  252. set_process_cpu_timer(tsk, CPUCLOCK_PROF,
  253. &nval, &cval);
  254. }
  255. tsk->signal->it_prof_expires = nval;
  256. tsk->signal->it_prof_incr = ninterval;
  257. spin_unlock_irq(&tsk->sighand->siglock);
  258. read_unlock(&tasklist_lock);
  259. if (ovalue) {
  260. cputime_to_timeval(cval, &ovalue->it_value);
  261. cputime_to_timeval(cinterval, &ovalue->it_interval);
  262. }
  263. break;
  264. default:
  265. return -EINVAL;
  266. }
  267. return 0;
  268. }
  269. /**
  270. * alarm_setitimer - set alarm in seconds
  271. *
  272. * @seconds: number of seconds until alarm
  273. * 0 disables the alarm
  274. *
  275. * Returns the remaining time in seconds of a pending timer or 0 when
  276. * the timer is not active.
  277. *
  278. * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
  279. * negative timeval settings which would cause immediate expiry.
  280. */
  281. unsigned int alarm_setitimer(unsigned int seconds)
  282. {
  283. struct itimerval it_new, it_old;
  284. #if BITS_PER_LONG < 64
  285. if (seconds > INT_MAX)
  286. seconds = INT_MAX;
  287. #endif
  288. it_new.it_value.tv_sec = seconds;
  289. it_new.it_value.tv_usec = 0;
  290. it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
  291. do_setitimer(ITIMER_REAL, &it_new, &it_old);
  292. /*
  293. * We can't return 0 if we have an alarm pending ... And we'd
  294. * better return too much than too little anyway
  295. */
  296. if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
  297. it_old.it_value.tv_usec >= 500000)
  298. it_old.it_value.tv_sec++;
  299. return it_old.it_value.tv_sec;
  300. }
  301. asmlinkage long sys_setitimer(int which,
  302. struct itimerval __user *value,
  303. struct itimerval __user *ovalue)
  304. {
  305. struct itimerval set_buffer, get_buffer;
  306. int error;
  307. if (value) {
  308. if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
  309. return -EFAULT;
  310. } else
  311. memset((char *) &set_buffer, 0, sizeof(set_buffer));
  312. error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  313. if (error || !ovalue)
  314. return error;
  315. if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  316. return -EFAULT;
  317. return 0;
  318. }