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. int it_real_fn(void *data)
  120. {
  121. struct task_struct *tsk = (struct task_struct *) data;
  122. send_group_sig_info(SIGALRM, SEND_SIG_PRIV, tsk);
  123. if (tsk->signal->it_real_incr.tv64 != 0) {
  124. hrtimer_forward(&tsk->signal->real_timer,
  125. tsk->signal->it_real_incr);
  126. return HRTIMER_RESTART;
  127. }
  128. return HRTIMER_NORESTART;
  129. }
  130. /*
  131. * We do not care about correctness. We just sanitize the values so
  132. * the ktime_t operations which expect normalized values do not
  133. * break. This converts negative values to long timeouts similar to
  134. * the code in kernel versions < 2.6.16
  135. *
  136. * Print a limited number of warning messages when an invalid timeval
  137. * is detected.
  138. */
  139. static void fixup_timeval(struct timeval *tv, int interval)
  140. {
  141. static int warnlimit = 10;
  142. unsigned long tmp;
  143. if (warnlimit > 0) {
  144. warnlimit--;
  145. printk(KERN_WARNING
  146. "setitimer: %s (pid = %d) provided "
  147. "invalid timeval %s: tv_sec = %ld tv_usec = %ld\n",
  148. current->comm, current->pid,
  149. interval ? "it_interval" : "it_value",
  150. tv->tv_sec, (long) tv->tv_usec);
  151. }
  152. tmp = tv->tv_usec;
  153. if (tmp >= USEC_PER_SEC) {
  154. tv->tv_usec = tmp % USEC_PER_SEC;
  155. tv->tv_sec += tmp / USEC_PER_SEC;
  156. }
  157. tmp = tv->tv_sec;
  158. if (tmp > LONG_MAX)
  159. tv->tv_sec = LONG_MAX;
  160. }
  161. /*
  162. * Returns true if the timeval is in canonical form
  163. */
  164. #define timeval_valid(t) \
  165. (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
  166. /*
  167. * Check for invalid timevals, sanitize them and print a limited
  168. * number of warnings.
  169. */
  170. static void check_itimerval(struct itimerval *value) {
  171. if (unlikely(!timeval_valid(&value->it_value)))
  172. fixup_timeval(&value->it_value, 0);
  173. if (unlikely(!timeval_valid(&value->it_interval)))
  174. fixup_timeval(&value->it_interval, 1);
  175. }
  176. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  177. {
  178. struct task_struct *tsk = current;
  179. struct hrtimer *timer;
  180. ktime_t expires;
  181. cputime_t cval, cinterval, nval, ninterval;
  182. /*
  183. * Validate the timevals in value.
  184. *
  185. * Note: Although the spec requires that invalid values shall
  186. * return -EINVAL, we just fixup the value and print a limited
  187. * number of warnings in order not to break users of this
  188. * historical misfeature.
  189. *
  190. * Scheduled for replacement in March 2007
  191. */
  192. check_itimerval(value);
  193. switch (which) {
  194. case ITIMER_REAL:
  195. again:
  196. spin_lock_irq(&tsk->sighand->siglock);
  197. timer = &tsk->signal->real_timer;
  198. if (ovalue) {
  199. ovalue->it_value = itimer_get_remtime(timer);
  200. ovalue->it_interval
  201. = ktime_to_timeval(tsk->signal->it_real_incr);
  202. }
  203. /* We are sharing ->siglock with it_real_fn() */
  204. if (hrtimer_try_to_cancel(timer) < 0) {
  205. spin_unlock_irq(&tsk->sighand->siglock);
  206. goto again;
  207. }
  208. tsk->signal->it_real_incr =
  209. timeval_to_ktime(value->it_interval);
  210. expires = timeval_to_ktime(value->it_value);
  211. if (expires.tv64 != 0)
  212. hrtimer_start(timer, expires, HRTIMER_REL);
  213. spin_unlock_irq(&tsk->sighand->siglock);
  214. break;
  215. case ITIMER_VIRTUAL:
  216. nval = timeval_to_cputime(&value->it_value);
  217. ninterval = timeval_to_cputime(&value->it_interval);
  218. read_lock(&tasklist_lock);
  219. spin_lock_irq(&tsk->sighand->siglock);
  220. cval = tsk->signal->it_virt_expires;
  221. cinterval = tsk->signal->it_virt_incr;
  222. if (!cputime_eq(cval, cputime_zero) ||
  223. !cputime_eq(nval, cputime_zero)) {
  224. if (cputime_gt(nval, cputime_zero))
  225. nval = cputime_add(nval,
  226. jiffies_to_cputime(1));
  227. set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
  228. &nval, &cval);
  229. }
  230. tsk->signal->it_virt_expires = nval;
  231. tsk->signal->it_virt_incr = ninterval;
  232. spin_unlock_irq(&tsk->sighand->siglock);
  233. read_unlock(&tasklist_lock);
  234. if (ovalue) {
  235. cputime_to_timeval(cval, &ovalue->it_value);
  236. cputime_to_timeval(cinterval, &ovalue->it_interval);
  237. }
  238. break;
  239. case ITIMER_PROF:
  240. nval = timeval_to_cputime(&value->it_value);
  241. ninterval = timeval_to_cputime(&value->it_interval);
  242. read_lock(&tasklist_lock);
  243. spin_lock_irq(&tsk->sighand->siglock);
  244. cval = tsk->signal->it_prof_expires;
  245. cinterval = tsk->signal->it_prof_incr;
  246. if (!cputime_eq(cval, cputime_zero) ||
  247. !cputime_eq(nval, cputime_zero)) {
  248. if (cputime_gt(nval, cputime_zero))
  249. nval = cputime_add(nval,
  250. jiffies_to_cputime(1));
  251. set_process_cpu_timer(tsk, CPUCLOCK_PROF,
  252. &nval, &cval);
  253. }
  254. tsk->signal->it_prof_expires = nval;
  255. tsk->signal->it_prof_incr = ninterval;
  256. spin_unlock_irq(&tsk->sighand->siglock);
  257. read_unlock(&tasklist_lock);
  258. if (ovalue) {
  259. cputime_to_timeval(cval, &ovalue->it_value);
  260. cputime_to_timeval(cinterval, &ovalue->it_interval);
  261. }
  262. break;
  263. default:
  264. return -EINVAL;
  265. }
  266. return 0;
  267. }
  268. /**
  269. * alarm_setitimer - set alarm in seconds
  270. *
  271. * @seconds: number of seconds until alarm
  272. * 0 disables the alarm
  273. *
  274. * Returns the remaining time in seconds of a pending timer or 0 when
  275. * the timer is not active.
  276. *
  277. * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
  278. * negative timeval settings which would cause immediate expiry.
  279. */
  280. unsigned int alarm_setitimer(unsigned int seconds)
  281. {
  282. struct itimerval it_new, it_old;
  283. #if BITS_PER_LONG < 64
  284. if (seconds > INT_MAX)
  285. seconds = INT_MAX;
  286. #endif
  287. it_new.it_value.tv_sec = seconds;
  288. it_new.it_value.tv_usec = 0;
  289. it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
  290. do_setitimer(ITIMER_REAL, &it_new, &it_old);
  291. /*
  292. * We can't return 0 if we have an alarm pending ... And we'd
  293. * better return too much than too little anyway
  294. */
  295. if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
  296. it_old.it_value.tv_usec >= 500000)
  297. it_old.it_value.tv_sec++;
  298. return it_old.it_value.tv_sec;
  299. }
  300. asmlinkage long sys_setitimer(int which,
  301. struct itimerval __user *value,
  302. struct itimerval __user *ovalue)
  303. {
  304. struct itimerval set_buffer, get_buffer;
  305. int error;
  306. if (value) {
  307. if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
  308. return -EFAULT;
  309. } else
  310. memset((char *) &set_buffer, 0, sizeof(set_buffer));
  311. error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  312. if (error || !ovalue)
  313. return error;
  314. if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  315. return -EFAULT;
  316. return 0;
  317. }