Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 1992 Darren Senn
0004  */
0005 
0006 /* These are all the functions necessary to implement itimers */
0007 
0008 #include <linux/mm.h>
0009 #include <linux/interrupt.h>
0010 #include <linux/syscalls.h>
0011 #include <linux/time.h>
0012 #include <linux/sched/signal.h>
0013 #include <linux/sched/cputime.h>
0014 #include <linux/posix-timers.h>
0015 #include <linux/hrtimer.h>
0016 #include <trace/events/timer.h>
0017 #include <linux/compat.h>
0018 
0019 #include <linux/uaccess.h>
0020 
0021 /**
0022  * itimer_get_remtime - get remaining time for the timer
0023  *
0024  * @timer: the timer to read
0025  *
0026  * Returns the delta between the expiry time and now, which can be
0027  * less than zero or 1usec for an pending expired timer
0028  */
0029 static struct timespec64 itimer_get_remtime(struct hrtimer *timer)
0030 {
0031     ktime_t rem = __hrtimer_get_remaining(timer, true);
0032 
0033     /*
0034      * Racy but safe: if the itimer expires after the above
0035      * hrtimer_get_remtime() call but before this condition
0036      * then we return 0 - which is correct.
0037      */
0038     if (hrtimer_active(timer)) {
0039         if (rem <= 0)
0040             rem = NSEC_PER_USEC;
0041     } else
0042         rem = 0;
0043 
0044     return ktime_to_timespec64(rem);
0045 }
0046 
0047 static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
0048                struct itimerspec64 *const value)
0049 {
0050     u64 val, interval;
0051     struct cpu_itimer *it = &tsk->signal->it[clock_id];
0052 
0053     spin_lock_irq(&tsk->sighand->siglock);
0054 
0055     val = it->expires;
0056     interval = it->incr;
0057     if (val) {
0058         u64 t, samples[CPUCLOCK_MAX];
0059 
0060         thread_group_sample_cputime(tsk, samples);
0061         t = samples[clock_id];
0062 
0063         if (val < t)
0064             /* about to fire */
0065             val = TICK_NSEC;
0066         else
0067             val -= t;
0068     }
0069 
0070     spin_unlock_irq(&tsk->sighand->siglock);
0071 
0072     value->it_value = ns_to_timespec64(val);
0073     value->it_interval = ns_to_timespec64(interval);
0074 }
0075 
0076 static int do_getitimer(int which, struct itimerspec64 *value)
0077 {
0078     struct task_struct *tsk = current;
0079 
0080     switch (which) {
0081     case ITIMER_REAL:
0082         spin_lock_irq(&tsk->sighand->siglock);
0083         value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
0084         value->it_interval =
0085             ktime_to_timespec64(tsk->signal->it_real_incr);
0086         spin_unlock_irq(&tsk->sighand->siglock);
0087         break;
0088     case ITIMER_VIRTUAL:
0089         get_cpu_itimer(tsk, CPUCLOCK_VIRT, value);
0090         break;
0091     case ITIMER_PROF:
0092         get_cpu_itimer(tsk, CPUCLOCK_PROF, value);
0093         break;
0094     default:
0095         return(-EINVAL);
0096     }
0097     return 0;
0098 }
0099 
0100 static int put_itimerval(struct __kernel_old_itimerval __user *o,
0101              const struct itimerspec64 *i)
0102 {
0103     struct __kernel_old_itimerval v;
0104 
0105     v.it_interval.tv_sec = i->it_interval.tv_sec;
0106     v.it_interval.tv_usec = i->it_interval.tv_nsec / NSEC_PER_USEC;
0107     v.it_value.tv_sec = i->it_value.tv_sec;
0108     v.it_value.tv_usec = i->it_value.tv_nsec / NSEC_PER_USEC;
0109     return copy_to_user(o, &v, sizeof(struct __kernel_old_itimerval)) ? -EFAULT : 0;
0110 }
0111 
0112 
0113 SYSCALL_DEFINE2(getitimer, int, which, struct __kernel_old_itimerval __user *, value)
0114 {
0115     struct itimerspec64 get_buffer;
0116     int error = do_getitimer(which, &get_buffer);
0117 
0118     if (!error && put_itimerval(value, &get_buffer))
0119         error = -EFAULT;
0120     return error;
0121 }
0122 
0123 #if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA)
0124 struct old_itimerval32 {
0125     struct old_timeval32    it_interval;
0126     struct old_timeval32    it_value;
0127 };
0128 
0129 static int put_old_itimerval32(struct old_itimerval32 __user *o,
0130                    const struct itimerspec64 *i)
0131 {
0132     struct old_itimerval32 v32;
0133 
0134     v32.it_interval.tv_sec = i->it_interval.tv_sec;
0135     v32.it_interval.tv_usec = i->it_interval.tv_nsec / NSEC_PER_USEC;
0136     v32.it_value.tv_sec = i->it_value.tv_sec;
0137     v32.it_value.tv_usec = i->it_value.tv_nsec / NSEC_PER_USEC;
0138     return copy_to_user(o, &v32, sizeof(struct old_itimerval32)) ? -EFAULT : 0;
0139 }
0140 
0141 COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
0142                struct old_itimerval32 __user *, value)
0143 {
0144     struct itimerspec64 get_buffer;
0145     int error = do_getitimer(which, &get_buffer);
0146 
0147     if (!error && put_old_itimerval32(value, &get_buffer))
0148         error = -EFAULT;
0149     return error;
0150 }
0151 #endif
0152 
0153 /*
0154  * The timer is automagically restarted, when interval != 0
0155  */
0156 enum hrtimer_restart it_real_fn(struct hrtimer *timer)
0157 {
0158     struct signal_struct *sig =
0159         container_of(timer, struct signal_struct, real_timer);
0160     struct pid *leader_pid = sig->pids[PIDTYPE_TGID];
0161 
0162     trace_itimer_expire(ITIMER_REAL, leader_pid, 0);
0163     kill_pid_info(SIGALRM, SEND_SIG_PRIV, leader_pid);
0164 
0165     return HRTIMER_NORESTART;
0166 }
0167 
0168 static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
0169                const struct itimerspec64 *const value,
0170                struct itimerspec64 *const ovalue)
0171 {
0172     u64 oval, nval, ointerval, ninterval;
0173     struct cpu_itimer *it = &tsk->signal->it[clock_id];
0174 
0175     nval = timespec64_to_ns(&value->it_value);
0176     ninterval = timespec64_to_ns(&value->it_interval);
0177 
0178     spin_lock_irq(&tsk->sighand->siglock);
0179 
0180     oval = it->expires;
0181     ointerval = it->incr;
0182     if (oval || nval) {
0183         if (nval > 0)
0184             nval += TICK_NSEC;
0185         set_process_cpu_timer(tsk, clock_id, &nval, &oval);
0186     }
0187     it->expires = nval;
0188     it->incr = ninterval;
0189     trace_itimer_state(clock_id == CPUCLOCK_VIRT ?
0190                ITIMER_VIRTUAL : ITIMER_PROF, value, nval);
0191 
0192     spin_unlock_irq(&tsk->sighand->siglock);
0193 
0194     if (ovalue) {
0195         ovalue->it_value = ns_to_timespec64(oval);
0196         ovalue->it_interval = ns_to_timespec64(ointerval);
0197     }
0198 }
0199 
0200 /*
0201  * Returns true if the timeval is in canonical form
0202  */
0203 #define timeval_valid(t) \
0204     (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
0205 
0206 static int do_setitimer(int which, struct itimerspec64 *value,
0207             struct itimerspec64 *ovalue)
0208 {
0209     struct task_struct *tsk = current;
0210     struct hrtimer *timer;
0211     ktime_t expires;
0212 
0213     switch (which) {
0214     case ITIMER_REAL:
0215 again:
0216         spin_lock_irq(&tsk->sighand->siglock);
0217         timer = &tsk->signal->real_timer;
0218         if (ovalue) {
0219             ovalue->it_value = itimer_get_remtime(timer);
0220             ovalue->it_interval
0221                 = ktime_to_timespec64(tsk->signal->it_real_incr);
0222         }
0223         /* We are sharing ->siglock with it_real_fn() */
0224         if (hrtimer_try_to_cancel(timer) < 0) {
0225             spin_unlock_irq(&tsk->sighand->siglock);
0226             hrtimer_cancel_wait_running(timer);
0227             goto again;
0228         }
0229         expires = timespec64_to_ktime(value->it_value);
0230         if (expires != 0) {
0231             tsk->signal->it_real_incr =
0232                 timespec64_to_ktime(value->it_interval);
0233             hrtimer_start(timer, expires, HRTIMER_MODE_REL);
0234         } else
0235             tsk->signal->it_real_incr = 0;
0236 
0237         trace_itimer_state(ITIMER_REAL, value, 0);
0238         spin_unlock_irq(&tsk->sighand->siglock);
0239         break;
0240     case ITIMER_VIRTUAL:
0241         set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue);
0242         break;
0243     case ITIMER_PROF:
0244         set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue);
0245         break;
0246     default:
0247         return -EINVAL;
0248     }
0249     return 0;
0250 }
0251 
0252 #ifdef CONFIG_SECURITY_SELINUX
0253 void clear_itimer(void)
0254 {
0255     struct itimerspec64 v = {};
0256     int i;
0257 
0258     for (i = 0; i < 3; i++)
0259         do_setitimer(i, &v, NULL);
0260 }
0261 #endif
0262 
0263 #ifdef __ARCH_WANT_SYS_ALARM
0264 
0265 /**
0266  * alarm_setitimer - set alarm in seconds
0267  *
0268  * @seconds:    number of seconds until alarm
0269  *      0 disables the alarm
0270  *
0271  * Returns the remaining time in seconds of a pending timer or 0 when
0272  * the timer is not active.
0273  *
0274  * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
0275  * negative timeval settings which would cause immediate expiry.
0276  */
0277 static unsigned int alarm_setitimer(unsigned int seconds)
0278 {
0279     struct itimerspec64 it_new, it_old;
0280 
0281 #if BITS_PER_LONG < 64
0282     if (seconds > INT_MAX)
0283         seconds = INT_MAX;
0284 #endif
0285     it_new.it_value.tv_sec = seconds;
0286     it_new.it_value.tv_nsec = 0;
0287     it_new.it_interval.tv_sec = it_new.it_interval.tv_nsec = 0;
0288 
0289     do_setitimer(ITIMER_REAL, &it_new, &it_old);
0290 
0291     /*
0292      * We can't return 0 if we have an alarm pending ...  And we'd
0293      * better return too much than too little anyway
0294      */
0295     if ((!it_old.it_value.tv_sec && it_old.it_value.tv_nsec) ||
0296           it_old.it_value.tv_nsec >= (NSEC_PER_SEC / 2))
0297         it_old.it_value.tv_sec++;
0298 
0299     return it_old.it_value.tv_sec;
0300 }
0301 
0302 /*
0303  * For backwards compatibility?  This can be done in libc so Alpha
0304  * and all newer ports shouldn't need it.
0305  */
0306 SYSCALL_DEFINE1(alarm, unsigned int, seconds)
0307 {
0308     return alarm_setitimer(seconds);
0309 }
0310 
0311 #endif
0312 
0313 static int get_itimerval(struct itimerspec64 *o, const struct __kernel_old_itimerval __user *i)
0314 {
0315     struct __kernel_old_itimerval v;
0316 
0317     if (copy_from_user(&v, i, sizeof(struct __kernel_old_itimerval)))
0318         return -EFAULT;
0319 
0320     /* Validate the timevals in value. */
0321     if (!timeval_valid(&v.it_value) ||
0322         !timeval_valid(&v.it_interval))
0323         return -EINVAL;
0324 
0325     o->it_interval.tv_sec = v.it_interval.tv_sec;
0326     o->it_interval.tv_nsec = v.it_interval.tv_usec * NSEC_PER_USEC;
0327     o->it_value.tv_sec = v.it_value.tv_sec;
0328     o->it_value.tv_nsec = v.it_value.tv_usec * NSEC_PER_USEC;
0329     return 0;
0330 }
0331 
0332 SYSCALL_DEFINE3(setitimer, int, which, struct __kernel_old_itimerval __user *, value,
0333         struct __kernel_old_itimerval __user *, ovalue)
0334 {
0335     struct itimerspec64 set_buffer, get_buffer;
0336     int error;
0337 
0338     if (value) {
0339         error = get_itimerval(&set_buffer, value);
0340         if (error)
0341             return error;
0342     } else {
0343         memset(&set_buffer, 0, sizeof(set_buffer));
0344         printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
0345                 " Misfeature support will be removed\n",
0346                 current->comm);
0347     }
0348 
0349     error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
0350     if (error || !ovalue)
0351         return error;
0352 
0353     if (put_itimerval(ovalue, &get_buffer))
0354         return -EFAULT;
0355     return 0;
0356 }
0357 
0358 #if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA)
0359 static int get_old_itimerval32(struct itimerspec64 *o, const struct old_itimerval32 __user *i)
0360 {
0361     struct old_itimerval32 v32;
0362 
0363     if (copy_from_user(&v32, i, sizeof(struct old_itimerval32)))
0364         return -EFAULT;
0365 
0366     /* Validate the timevals in value.  */
0367     if (!timeval_valid(&v32.it_value) ||
0368         !timeval_valid(&v32.it_interval))
0369         return -EINVAL;
0370 
0371     o->it_interval.tv_sec = v32.it_interval.tv_sec;
0372     o->it_interval.tv_nsec = v32.it_interval.tv_usec * NSEC_PER_USEC;
0373     o->it_value.tv_sec = v32.it_value.tv_sec;
0374     o->it_value.tv_nsec = v32.it_value.tv_usec * NSEC_PER_USEC;
0375     return 0;
0376 }
0377 
0378 COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
0379                struct old_itimerval32 __user *, value,
0380                struct old_itimerval32 __user *, ovalue)
0381 {
0382     struct itimerspec64 set_buffer, get_buffer;
0383     int error;
0384 
0385     if (value) {
0386         error = get_old_itimerval32(&set_buffer, value);
0387         if (error)
0388             return error;
0389     } else {
0390         memset(&set_buffer, 0, sizeof(set_buffer));
0391         printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
0392                 " Misfeature support will be removed\n",
0393                 current->comm);
0394     }
0395 
0396     error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
0397     if (error || !ovalue)
0398         return error;
0399     if (put_old_itimerval32(ovalue, &get_buffer))
0400         return -EFAULT;
0401     return 0;
0402 }
0403 #endif