ia64/linux-2.6.18-xen.hg

annotate kernel/itimer.c @ 912:dd42cdb0ab89

[IA64] Build blktap2 driver by default in x86 builds.

add CONFIG_XEN_BLKDEV_TAP2=y to buildconfigs/linux-defconfig_xen_ia64.

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