itimer.c 9.2 KB

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