itimer.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/interrupt.h>
  9. #include <linux/syscalls.h>
  10. #include <linux/time.h>
  11. #include <linux/posix-timers.h>
  12. #include <linux/hrtimer.h>
  13. #include <asm/uaccess.h>
  14. /**
  15. * itimer_get_remtime - get remaining time for the timer
  16. *
  17. * @timer: the timer to read
  18. *
  19. * Returns the delta between the expiry time and now, which can be
  20. * less than zero or 1usec for an pending expired timer
  21. */
  22. static struct timeval itimer_get_remtime(struct hrtimer *timer)
  23. {
  24. ktime_t rem = hrtimer_get_remaining(timer);
  25. /*
  26. * Racy but safe: if the itimer expires after the above
  27. * hrtimer_get_remtime() call but before this condition
  28. * then we return 0 - which is correct.
  29. */
  30. if (hrtimer_active(timer)) {
  31. if (rem.tv64 <= 0)
  32. rem.tv64 = NSEC_PER_USEC;
  33. } else
  34. rem.tv64 = 0;
  35. return ktime_to_timeval(rem);
  36. }
  37. int do_getitimer(int which, struct itimerval *value)
  38. {
  39. struct task_struct *tsk = current;
  40. cputime_t cinterval, cval;
  41. switch (which) {
  42. case ITIMER_REAL:
  43. spin_lock_irq(&tsk->sighand->siglock);
  44. value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
  45. value->it_interval =
  46. ktime_to_timeval(tsk->signal->it_real_incr);
  47. spin_unlock_irq(&tsk->sighand->siglock);
  48. break;
  49. case ITIMER_VIRTUAL:
  50. read_lock(&tasklist_lock);
  51. spin_lock_irq(&tsk->sighand->siglock);
  52. cval = tsk->signal->it_virt_expires;
  53. cinterval = tsk->signal->it_virt_incr;
  54. if (!cputime_eq(cval, cputime_zero)) {
  55. struct task_struct *t = tsk;
  56. cputime_t utime = tsk->signal->utime;
  57. do {
  58. utime = cputime_add(utime, t->utime);
  59. t = next_thread(t);
  60. } while (t != tsk);
  61. if (cputime_le(cval, utime)) { /* about to fire */
  62. cval = jiffies_to_cputime(1);
  63. } else {
  64. cval = cputime_sub(cval, utime);
  65. }
  66. }
  67. spin_unlock_irq(&tsk->sighand->siglock);
  68. read_unlock(&tasklist_lock);
  69. cputime_to_timeval(cval, &value->it_value);
  70. cputime_to_timeval(cinterval, &value->it_interval);
  71. break;
  72. case ITIMER_PROF:
  73. read_lock(&tasklist_lock);
  74. spin_lock_irq(&tsk->sighand->siglock);
  75. cval = tsk->signal->it_prof_expires;
  76. cinterval = tsk->signal->it_prof_incr;
  77. if (!cputime_eq(cval, cputime_zero)) {
  78. struct task_struct *t = tsk;
  79. cputime_t ptime = cputime_add(tsk->signal->utime,
  80. tsk->signal->stime);
  81. do {
  82. ptime = cputime_add(ptime,
  83. cputime_add(t->utime,
  84. t->stime));
  85. t = next_thread(t);
  86. } while (t != tsk);
  87. if (cputime_le(cval, ptime)) { /* about to fire */
  88. cval = jiffies_to_cputime(1);
  89. } else {
  90. cval = cputime_sub(cval, ptime);
  91. }
  92. }
  93. spin_unlock_irq(&tsk->sighand->siglock);
  94. read_unlock(&tasklist_lock);
  95. cputime_to_timeval(cval, &value->it_value);
  96. cputime_to_timeval(cinterval, &value->it_interval);
  97. break;
  98. default:
  99. return(-EINVAL);
  100. }
  101. return 0;
  102. }
  103. asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
  104. {
  105. int error = -EFAULT;
  106. struct itimerval get_buffer;
  107. if (value) {
  108. error = do_getitimer(which, &get_buffer);
  109. if (!error &&
  110. copy_to_user(value, &get_buffer, sizeof(get_buffer)))
  111. error = -EFAULT;
  112. }
  113. return error;
  114. }
  115. /*
  116. * The timer is automagically restarted, when interval != 0
  117. */
  118. enum hrtimer_restart it_real_fn(struct hrtimer *timer)
  119. {
  120. struct signal_struct *sig =
  121. container_of(timer, struct signal_struct, real_timer);
  122. kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid);
  123. return HRTIMER_NORESTART;
  124. }
  125. /*
  126. * Returns true if the timeval is in canonical form
  127. */
  128. #define timeval_valid(t) \
  129. (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
  130. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  131. {
  132. struct task_struct *tsk = current;
  133. struct hrtimer *timer;
  134. ktime_t expires;
  135. cputime_t cval, cinterval, nval, ninterval;
  136. /*
  137. * Validate the timevals in value.
  138. */
  139. if (!timeval_valid(&value->it_value) ||
  140. !timeval_valid(&value->it_interval))
  141. return -EINVAL;
  142. switch (which) {
  143. case ITIMER_REAL:
  144. again:
  145. spin_lock_irq(&tsk->sighand->siglock);
  146. timer = &tsk->signal->real_timer;
  147. if (ovalue) {
  148. ovalue->it_value = itimer_get_remtime(timer);
  149. ovalue->it_interval
  150. = ktime_to_timeval(tsk->signal->it_real_incr);
  151. }
  152. /* We are sharing ->siglock with it_real_fn() */
  153. if (hrtimer_try_to_cancel(timer) < 0) {
  154. spin_unlock_irq(&tsk->sighand->siglock);
  155. goto again;
  156. }
  157. expires = timeval_to_ktime(value->it_value);
  158. if (expires.tv64 != 0) {
  159. tsk->signal->it_real_incr =
  160. timeval_to_ktime(value->it_interval);
  161. hrtimer_start(timer, expires, HRTIMER_MODE_REL);
  162. } else
  163. tsk->signal->it_real_incr.tv64 = 0;
  164. spin_unlock_irq(&tsk->sighand->siglock);
  165. break;
  166. case ITIMER_VIRTUAL:
  167. nval = timeval_to_cputime(&value->it_value);
  168. ninterval = timeval_to_cputime(&value->it_interval);
  169. read_lock(&tasklist_lock);
  170. spin_lock_irq(&tsk->sighand->siglock);
  171. cval = tsk->signal->it_virt_expires;
  172. cinterval = tsk->signal->it_virt_incr;
  173. if (!cputime_eq(cval, cputime_zero) ||
  174. !cputime_eq(nval, cputime_zero)) {
  175. if (cputime_gt(nval, cputime_zero))
  176. nval = cputime_add(nval,
  177. jiffies_to_cputime(1));
  178. set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
  179. &nval, &cval);
  180. }
  181. tsk->signal->it_virt_expires = nval;
  182. tsk->signal->it_virt_incr = ninterval;
  183. spin_unlock_irq(&tsk->sighand->siglock);
  184. read_unlock(&tasklist_lock);
  185. if (ovalue) {
  186. cputime_to_timeval(cval, &ovalue->it_value);
  187. cputime_to_timeval(cinterval, &ovalue->it_interval);
  188. }
  189. break;
  190. case ITIMER_PROF:
  191. nval = timeval_to_cputime(&value->it_value);
  192. ninterval = timeval_to_cputime(&value->it_interval);
  193. read_lock(&tasklist_lock);
  194. spin_lock_irq(&tsk->sighand->siglock);
  195. cval = tsk->signal->it_prof_expires;
  196. cinterval = tsk->signal->it_prof_incr;
  197. if (!cputime_eq(cval, cputime_zero) ||
  198. !cputime_eq(nval, cputime_zero)) {
  199. if (cputime_gt(nval, cputime_zero))
  200. nval = cputime_add(nval,
  201. jiffies_to_cputime(1));
  202. set_process_cpu_timer(tsk, CPUCLOCK_PROF,
  203. &nval, &cval);
  204. }
  205. tsk->signal->it_prof_expires = nval;
  206. tsk->signal->it_prof_incr = ninterval;
  207. spin_unlock_irq(&tsk->sighand->siglock);
  208. read_unlock(&tasklist_lock);
  209. if (ovalue) {
  210. cputime_to_timeval(cval, &ovalue->it_value);
  211. cputime_to_timeval(cinterval, &ovalue->it_interval);
  212. }
  213. break;
  214. default:
  215. return -EINVAL;
  216. }
  217. return 0;
  218. }
  219. /**
  220. * alarm_setitimer - set alarm in seconds
  221. *
  222. * @seconds: number of seconds until alarm
  223. * 0 disables the alarm
  224. *
  225. * Returns the remaining time in seconds of a pending timer or 0 when
  226. * the timer is not active.
  227. *
  228. * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
  229. * negative timeval settings which would cause immediate expiry.
  230. */
  231. unsigned int alarm_setitimer(unsigned int seconds)
  232. {
  233. struct itimerval it_new, it_old;
  234. #if BITS_PER_LONG < 64
  235. if (seconds > INT_MAX)
  236. seconds = INT_MAX;
  237. #endif
  238. it_new.it_value.tv_sec = seconds;
  239. it_new.it_value.tv_usec = 0;
  240. it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
  241. do_setitimer(ITIMER_REAL, &it_new, &it_old);
  242. /*
  243. * We can't return 0 if we have an alarm pending ... And we'd
  244. * better return too much than too little anyway
  245. */
  246. if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
  247. it_old.it_value.tv_usec >= 500000)
  248. it_old.it_value.tv_sec++;
  249. return it_old.it_value.tv_sec;
  250. }
  251. asmlinkage long sys_setitimer(int which,
  252. struct itimerval __user *value,
  253. struct itimerval __user *ovalue)
  254. {
  255. struct itimerval set_buffer, get_buffer;
  256. int error;
  257. if (value) {
  258. if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
  259. return -EFAULT;
  260. } else
  261. memset((char *) &set_buffer, 0, sizeof(set_buffer));
  262. error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  263. if (error || !ovalue)
  264. return error;
  265. if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  266. return -EFAULT;
  267. return 0;
  268. }