1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25cee9645SThomas Gleixner /*
35cee9645SThomas Gleixner * Copyright (C) 1992 Darren Senn
45cee9645SThomas Gleixner */
55cee9645SThomas Gleixner
65cee9645SThomas Gleixner /* These are all the functions necessary to implement itimers */
75cee9645SThomas Gleixner
85cee9645SThomas Gleixner #include <linux/mm.h>
95cee9645SThomas Gleixner #include <linux/interrupt.h>
105cee9645SThomas Gleixner #include <linux/syscalls.h>
115cee9645SThomas Gleixner #include <linux/time.h>
123f07c014SIngo Molnar #include <linux/sched/signal.h>
1332ef5517SIngo Molnar #include <linux/sched/cputime.h>
145cee9645SThomas Gleixner #include <linux/posix-timers.h>
155cee9645SThomas Gleixner #include <linux/hrtimer.h>
165cee9645SThomas Gleixner #include <trace/events/timer.h>
1754ad9c46SAl Viro #include <linux/compat.h>
185cee9645SThomas Gleixner
197c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
205cee9645SThomas Gleixner
215cee9645SThomas Gleixner /**
225cee9645SThomas Gleixner * itimer_get_remtime - get remaining time for the timer
235cee9645SThomas Gleixner *
245cee9645SThomas Gleixner * @timer: the timer to read
255cee9645SThomas Gleixner *
265cee9645SThomas Gleixner * Returns the delta between the expiry time and now, which can be
275cee9645SThomas Gleixner * less than zero or 1usec for an pending expired timer
285cee9645SThomas Gleixner */
itimer_get_remtime(struct hrtimer * timer)29bd40a175SArnd Bergmann static struct timespec64 itimer_get_remtime(struct hrtimer *timer)
305cee9645SThomas Gleixner {
3151cbb524SThomas Gleixner ktime_t rem = __hrtimer_get_remaining(timer, true);
325cee9645SThomas Gleixner
335cee9645SThomas Gleixner /*
345cee9645SThomas Gleixner * Racy but safe: if the itimer expires after the above
355cee9645SThomas Gleixner * hrtimer_get_remtime() call but before this condition
365cee9645SThomas Gleixner * then we return 0 - which is correct.
375cee9645SThomas Gleixner */
385cee9645SThomas Gleixner if (hrtimer_active(timer)) {
392456e855SThomas Gleixner if (rem <= 0)
402456e855SThomas Gleixner rem = NSEC_PER_USEC;
415cee9645SThomas Gleixner } else
422456e855SThomas Gleixner rem = 0;
435cee9645SThomas Gleixner
44bd40a175SArnd Bergmann return ktime_to_timespec64(rem);
455cee9645SThomas Gleixner }
465cee9645SThomas Gleixner
get_cpu_itimer(struct task_struct * tsk,unsigned int clock_id,struct itimerspec64 * const value)475cee9645SThomas Gleixner static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
48bd40a175SArnd Bergmann struct itimerspec64 *const value)
495cee9645SThomas Gleixner {
50858cf3a8SFrederic Weisbecker u64 val, interval;
515cee9645SThomas Gleixner struct cpu_itimer *it = &tsk->signal->it[clock_id];
525cee9645SThomas Gleixner
535cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock);
545cee9645SThomas Gleixner
55858cf3a8SFrederic Weisbecker val = it->expires;
56858cf3a8SFrederic Weisbecker interval = it->incr;
57858cf3a8SFrederic Weisbecker if (val) {
58b7be4ef1SThomas Gleixner u64 t, samples[CPUCLOCK_MAX];
595cee9645SThomas Gleixner
60b7be4ef1SThomas Gleixner thread_group_sample_cputime(tsk, samples);
61b7be4ef1SThomas Gleixner t = samples[clock_id];
625cee9645SThomas Gleixner
63858cf3a8SFrederic Weisbecker if (val < t)
645cee9645SThomas Gleixner /* about to fire */
65858cf3a8SFrederic Weisbecker val = TICK_NSEC;
665cee9645SThomas Gleixner else
67858cf3a8SFrederic Weisbecker val -= t;
685cee9645SThomas Gleixner }
695cee9645SThomas Gleixner
705cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock);
715cee9645SThomas Gleixner
72bd40a175SArnd Bergmann value->it_value = ns_to_timespec64(val);
73bd40a175SArnd Bergmann value->it_interval = ns_to_timespec64(interval);
745cee9645SThomas Gleixner }
755cee9645SThomas Gleixner
do_getitimer(int which,struct itimerspec64 * value)76bd40a175SArnd Bergmann static int do_getitimer(int which, struct itimerspec64 *value)
775cee9645SThomas Gleixner {
785cee9645SThomas Gleixner struct task_struct *tsk = current;
795cee9645SThomas Gleixner
805cee9645SThomas Gleixner switch (which) {
815cee9645SThomas Gleixner case ITIMER_REAL:
825cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock);
835cee9645SThomas Gleixner value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
845cee9645SThomas Gleixner value->it_interval =
85bd40a175SArnd Bergmann ktime_to_timespec64(tsk->signal->it_real_incr);
865cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock);
875cee9645SThomas Gleixner break;
885cee9645SThomas Gleixner case ITIMER_VIRTUAL:
895cee9645SThomas Gleixner get_cpu_itimer(tsk, CPUCLOCK_VIRT, value);
905cee9645SThomas Gleixner break;
915cee9645SThomas Gleixner case ITIMER_PROF:
925cee9645SThomas Gleixner get_cpu_itimer(tsk, CPUCLOCK_PROF, value);
935cee9645SThomas Gleixner break;
945cee9645SThomas Gleixner default:
955cee9645SThomas Gleixner return(-EINVAL);
965cee9645SThomas Gleixner }
975cee9645SThomas Gleixner return 0;
985cee9645SThomas Gleixner }
995cee9645SThomas Gleixner
put_itimerval(struct __kernel_old_itimerval __user * o,const struct itimerspec64 * i)1004f9fbd89SArnd Bergmann static int put_itimerval(struct __kernel_old_itimerval __user *o,
101bd40a175SArnd Bergmann const struct itimerspec64 *i)
102bd40a175SArnd Bergmann {
1034f9fbd89SArnd Bergmann struct __kernel_old_itimerval v;
104bd40a175SArnd Bergmann
105bd40a175SArnd Bergmann v.it_interval.tv_sec = i->it_interval.tv_sec;
106bd40a175SArnd Bergmann v.it_interval.tv_usec = i->it_interval.tv_nsec / NSEC_PER_USEC;
107bd40a175SArnd Bergmann v.it_value.tv_sec = i->it_value.tv_sec;
108bd40a175SArnd Bergmann v.it_value.tv_usec = i->it_value.tv_nsec / NSEC_PER_USEC;
1094f9fbd89SArnd Bergmann return copy_to_user(o, &v, sizeof(struct __kernel_old_itimerval)) ? -EFAULT : 0;
110bd40a175SArnd Bergmann }
111bd40a175SArnd Bergmann
112bd40a175SArnd Bergmann
SYSCALL_DEFINE2(getitimer,int,which,struct __kernel_old_itimerval __user *,value)1134f9fbd89SArnd Bergmann SYSCALL_DEFINE2(getitimer, int, which, struct __kernel_old_itimerval __user *, value)
1145cee9645SThomas Gleixner {
115bd40a175SArnd Bergmann struct itimerspec64 get_buffer;
116bd40a175SArnd Bergmann int error = do_getitimer(which, &get_buffer);
1175cee9645SThomas Gleixner
118bd40a175SArnd Bergmann if (!error && put_itimerval(value, &get_buffer))
1195cee9645SThomas Gleixner error = -EFAULT;
1205cee9645SThomas Gleixner return error;
1215cee9645SThomas Gleixner }
1225cee9645SThomas Gleixner
1234c22ea2bSArnd Bergmann #if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA)
124c1745f84SArnd Bergmann struct old_itimerval32 {
125c1745f84SArnd Bergmann struct old_timeval32 it_interval;
126c1745f84SArnd Bergmann struct old_timeval32 it_value;
127c1745f84SArnd Bergmann };
128c1745f84SArnd Bergmann
put_old_itimerval32(struct old_itimerval32 __user * o,const struct itimerspec64 * i)129bd40a175SArnd Bergmann static int put_old_itimerval32(struct old_itimerval32 __user *o,
130bd40a175SArnd Bergmann const struct itimerspec64 *i)
131c1745f84SArnd Bergmann {
132c1745f84SArnd Bergmann struct old_itimerval32 v32;
133c1745f84SArnd Bergmann
134c1745f84SArnd Bergmann v32.it_interval.tv_sec = i->it_interval.tv_sec;
135bd40a175SArnd Bergmann v32.it_interval.tv_usec = i->it_interval.tv_nsec / NSEC_PER_USEC;
136c1745f84SArnd Bergmann v32.it_value.tv_sec = i->it_value.tv_sec;
137bd40a175SArnd Bergmann v32.it_value.tv_usec = i->it_value.tv_nsec / NSEC_PER_USEC;
138c1745f84SArnd Bergmann return copy_to_user(o, &v32, sizeof(struct old_itimerval32)) ? -EFAULT : 0;
139c1745f84SArnd Bergmann }
140c1745f84SArnd Bergmann
COMPAT_SYSCALL_DEFINE2(getitimer,int,which,struct old_itimerval32 __user *,value)14154ad9c46SAl Viro COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
142bd40a175SArnd Bergmann struct old_itimerval32 __user *, value)
14354ad9c46SAl Viro {
144bd40a175SArnd Bergmann struct itimerspec64 get_buffer;
145bd40a175SArnd Bergmann int error = do_getitimer(which, &get_buffer);
14654ad9c46SAl Viro
147bd40a175SArnd Bergmann if (!error && put_old_itimerval32(value, &get_buffer))
14854ad9c46SAl Viro error = -EFAULT;
14954ad9c46SAl Viro return error;
15054ad9c46SAl Viro }
15154ad9c46SAl Viro #endif
15254ad9c46SAl Viro
1535cee9645SThomas Gleixner /*
154*68f99be2SThomas Gleixner * Invoked from dequeue_signal() when SIG_ALRM is delivered.
155*68f99be2SThomas Gleixner *
156*68f99be2SThomas Gleixner * Restart the ITIMER_REAL timer if it is armed as periodic timer. Doing
157*68f99be2SThomas Gleixner * this in the signal delivery path instead of self rearming prevents a DoS
158*68f99be2SThomas Gleixner * with small increments in the high reolution timer case and reduces timer
159*68f99be2SThomas Gleixner * noise in general.
160*68f99be2SThomas Gleixner */
posixtimer_rearm_itimer(struct task_struct * tsk)161*68f99be2SThomas Gleixner void posixtimer_rearm_itimer(struct task_struct *tsk)
162*68f99be2SThomas Gleixner {
163*68f99be2SThomas Gleixner struct hrtimer *tmr = &tsk->signal->real_timer;
164*68f99be2SThomas Gleixner
165*68f99be2SThomas Gleixner if (!hrtimer_is_queued(tmr) && tsk->signal->it_real_incr != 0) {
166*68f99be2SThomas Gleixner hrtimer_forward(tmr, tmr->base->get_time(),
167*68f99be2SThomas Gleixner tsk->signal->it_real_incr);
168*68f99be2SThomas Gleixner hrtimer_restart(tmr);
169*68f99be2SThomas Gleixner }
170*68f99be2SThomas Gleixner }
171*68f99be2SThomas Gleixner
172*68f99be2SThomas Gleixner /*
173*68f99be2SThomas Gleixner * Interval timers are restarted in the signal delivery path. See
174*68f99be2SThomas Gleixner * posixtimer_rearm_itimer().
1755cee9645SThomas Gleixner */
it_real_fn(struct hrtimer * timer)1765cee9645SThomas Gleixner enum hrtimer_restart it_real_fn(struct hrtimer *timer)
1775cee9645SThomas Gleixner {
1785cee9645SThomas Gleixner struct signal_struct *sig =
1795cee9645SThomas Gleixner container_of(timer, struct signal_struct, real_timer);
1806883f81aSEric W. Biederman struct pid *leader_pid = sig->pids[PIDTYPE_TGID];
1815cee9645SThomas Gleixner
1826883f81aSEric W. Biederman trace_itimer_expire(ITIMER_REAL, leader_pid, 0);
1836883f81aSEric W. Biederman kill_pid_info(SIGALRM, SEND_SIG_PRIV, leader_pid);
1845cee9645SThomas Gleixner
1855cee9645SThomas Gleixner return HRTIMER_NORESTART;
1865cee9645SThomas Gleixner }
1875cee9645SThomas Gleixner
set_cpu_itimer(struct task_struct * tsk,unsigned int clock_id,const struct itimerspec64 * const value,struct itimerspec64 * const ovalue)1885cee9645SThomas Gleixner static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
189bd40a175SArnd Bergmann const struct itimerspec64 *const value,
190bd40a175SArnd Bergmann struct itimerspec64 *const ovalue)
1915cee9645SThomas Gleixner {
192858cf3a8SFrederic Weisbecker u64 oval, nval, ointerval, ninterval;
1935cee9645SThomas Gleixner struct cpu_itimer *it = &tsk->signal->it[clock_id];
1945cee9645SThomas Gleixner
195bd40a175SArnd Bergmann nval = timespec64_to_ns(&value->it_value);
196bd40a175SArnd Bergmann ninterval = timespec64_to_ns(&value->it_interval);
1975cee9645SThomas Gleixner
1985cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock);
1995cee9645SThomas Gleixner
200858cf3a8SFrederic Weisbecker oval = it->expires;
201858cf3a8SFrederic Weisbecker ointerval = it->incr;
202858cf3a8SFrederic Weisbecker if (oval || nval) {
2035cee9645SThomas Gleixner if (nval > 0)
204858cf3a8SFrederic Weisbecker nval += TICK_NSEC;
205858cf3a8SFrederic Weisbecker set_process_cpu_timer(tsk, clock_id, &nval, &oval);
2065cee9645SThomas Gleixner }
2075cee9645SThomas Gleixner it->expires = nval;
2085cee9645SThomas Gleixner it->incr = ninterval;
2095cee9645SThomas Gleixner trace_itimer_state(clock_id == CPUCLOCK_VIRT ?
2105cee9645SThomas Gleixner ITIMER_VIRTUAL : ITIMER_PROF, value, nval);
2115cee9645SThomas Gleixner
2125cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock);
2135cee9645SThomas Gleixner
2145cee9645SThomas Gleixner if (ovalue) {
215bd40a175SArnd Bergmann ovalue->it_value = ns_to_timespec64(oval);
216bd40a175SArnd Bergmann ovalue->it_interval = ns_to_timespec64(ointerval);
2175cee9645SThomas Gleixner }
2185cee9645SThomas Gleixner }
2195cee9645SThomas Gleixner
2205cee9645SThomas Gleixner /*
2215cee9645SThomas Gleixner * Returns true if the timeval is in canonical form
2225cee9645SThomas Gleixner */
2235cee9645SThomas Gleixner #define timeval_valid(t) \
2245cee9645SThomas Gleixner (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
2255cee9645SThomas Gleixner
do_setitimer(int which,struct itimerspec64 * value,struct itimerspec64 * ovalue)226bd40a175SArnd Bergmann static int do_setitimer(int which, struct itimerspec64 *value,
227bd40a175SArnd Bergmann struct itimerspec64 *ovalue)
2285cee9645SThomas Gleixner {
2295cee9645SThomas Gleixner struct task_struct *tsk = current;
2305cee9645SThomas Gleixner struct hrtimer *timer;
2315cee9645SThomas Gleixner ktime_t expires;
2325cee9645SThomas Gleixner
2335cee9645SThomas Gleixner switch (which) {
2345cee9645SThomas Gleixner case ITIMER_REAL:
2355cee9645SThomas Gleixner again:
2365cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock);
2375cee9645SThomas Gleixner timer = &tsk->signal->real_timer;
2385cee9645SThomas Gleixner if (ovalue) {
2395cee9645SThomas Gleixner ovalue->it_value = itimer_get_remtime(timer);
2405cee9645SThomas Gleixner ovalue->it_interval
241bd40a175SArnd Bergmann = ktime_to_timespec64(tsk->signal->it_real_incr);
2425cee9645SThomas Gleixner }
2435cee9645SThomas Gleixner /* We are sharing ->siglock with it_real_fn() */
2445cee9645SThomas Gleixner if (hrtimer_try_to_cancel(timer) < 0) {
2455cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock);
246c7e6d704SAnna-Maria Gleixner hrtimer_cancel_wait_running(timer);
2475cee9645SThomas Gleixner goto again;
2485cee9645SThomas Gleixner }
249bd40a175SArnd Bergmann expires = timespec64_to_ktime(value->it_value);
2502456e855SThomas Gleixner if (expires != 0) {
2515cee9645SThomas Gleixner tsk->signal->it_real_incr =
252bd40a175SArnd Bergmann timespec64_to_ktime(value->it_interval);
2535cee9645SThomas Gleixner hrtimer_start(timer, expires, HRTIMER_MODE_REL);
2545cee9645SThomas Gleixner } else
2552456e855SThomas Gleixner tsk->signal->it_real_incr = 0;
2565cee9645SThomas Gleixner
2575cee9645SThomas Gleixner trace_itimer_state(ITIMER_REAL, value, 0);
2585cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock);
2595cee9645SThomas Gleixner break;
2605cee9645SThomas Gleixner case ITIMER_VIRTUAL:
2615cee9645SThomas Gleixner set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue);
2625cee9645SThomas Gleixner break;
2635cee9645SThomas Gleixner case ITIMER_PROF:
2645cee9645SThomas Gleixner set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue);
2655cee9645SThomas Gleixner break;
2665cee9645SThomas Gleixner default:
2675cee9645SThomas Gleixner return -EINVAL;
2685cee9645SThomas Gleixner }
2695cee9645SThomas Gleixner return 0;
2705cee9645SThomas Gleixner }
2715cee9645SThomas Gleixner
272ddbc7d06SArnd Bergmann #ifdef CONFIG_SECURITY_SELINUX
clear_itimer(void)273ddbc7d06SArnd Bergmann void clear_itimer(void)
274ddbc7d06SArnd Bergmann {
275bd40a175SArnd Bergmann struct itimerspec64 v = {};
276ddbc7d06SArnd Bergmann int i;
277ddbc7d06SArnd Bergmann
278ddbc7d06SArnd Bergmann for (i = 0; i < 3; i++)
279ddbc7d06SArnd Bergmann do_setitimer(i, &v, NULL);
280ddbc7d06SArnd Bergmann }
281ddbc7d06SArnd Bergmann #endif
282ddbc7d06SArnd Bergmann
28374ba181eSNicolas Pitre #ifdef __ARCH_WANT_SYS_ALARM
28474ba181eSNicolas Pitre
2855cee9645SThomas Gleixner /**
2865cee9645SThomas Gleixner * alarm_setitimer - set alarm in seconds
2875cee9645SThomas Gleixner *
2885cee9645SThomas Gleixner * @seconds: number of seconds until alarm
2895cee9645SThomas Gleixner * 0 disables the alarm
2905cee9645SThomas Gleixner *
2915cee9645SThomas Gleixner * Returns the remaining time in seconds of a pending timer or 0 when
2925cee9645SThomas Gleixner * the timer is not active.
2935cee9645SThomas Gleixner *
2945cee9645SThomas Gleixner * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
2955cee9645SThomas Gleixner * negative timeval settings which would cause immediate expiry.
2965cee9645SThomas Gleixner */
alarm_setitimer(unsigned int seconds)29774ba181eSNicolas Pitre static unsigned int alarm_setitimer(unsigned int seconds)
2985cee9645SThomas Gleixner {
299bd40a175SArnd Bergmann struct itimerspec64 it_new, it_old;
3005cee9645SThomas Gleixner
3015cee9645SThomas Gleixner #if BITS_PER_LONG < 64
3025cee9645SThomas Gleixner if (seconds > INT_MAX)
3035cee9645SThomas Gleixner seconds = INT_MAX;
3045cee9645SThomas Gleixner #endif
3055cee9645SThomas Gleixner it_new.it_value.tv_sec = seconds;
306bd40a175SArnd Bergmann it_new.it_value.tv_nsec = 0;
307bd40a175SArnd Bergmann it_new.it_interval.tv_sec = it_new.it_interval.tv_nsec = 0;
3085cee9645SThomas Gleixner
3095cee9645SThomas Gleixner do_setitimer(ITIMER_REAL, &it_new, &it_old);
3105cee9645SThomas Gleixner
3115cee9645SThomas Gleixner /*
3125cee9645SThomas Gleixner * We can't return 0 if we have an alarm pending ... And we'd
3135cee9645SThomas Gleixner * better return too much than too little anyway
3145cee9645SThomas Gleixner */
315bd40a175SArnd Bergmann if ((!it_old.it_value.tv_sec && it_old.it_value.tv_nsec) ||
316b111df84SArnd Bergmann it_old.it_value.tv_nsec >= (NSEC_PER_SEC / 2))
3175cee9645SThomas Gleixner it_old.it_value.tv_sec++;
3185cee9645SThomas Gleixner
3195cee9645SThomas Gleixner return it_old.it_value.tv_sec;
3205cee9645SThomas Gleixner }
3215cee9645SThomas Gleixner
32274ba181eSNicolas Pitre /*
32374ba181eSNicolas Pitre * For backwards compatibility? This can be done in libc so Alpha
32474ba181eSNicolas Pitre * and all newer ports shouldn't need it.
32574ba181eSNicolas Pitre */
SYSCALL_DEFINE1(alarm,unsigned int,seconds)32674ba181eSNicolas Pitre SYSCALL_DEFINE1(alarm, unsigned int, seconds)
32774ba181eSNicolas Pitre {
32874ba181eSNicolas Pitre return alarm_setitimer(seconds);
32974ba181eSNicolas Pitre }
33074ba181eSNicolas Pitre
33174ba181eSNicolas Pitre #endif
33274ba181eSNicolas Pitre
get_itimerval(struct itimerspec64 * o,const struct __kernel_old_itimerval __user * i)3334f9fbd89SArnd Bergmann static int get_itimerval(struct itimerspec64 *o, const struct __kernel_old_itimerval __user *i)
334bd40a175SArnd Bergmann {
3354f9fbd89SArnd Bergmann struct __kernel_old_itimerval v;
336bd40a175SArnd Bergmann
3374f9fbd89SArnd Bergmann if (copy_from_user(&v, i, sizeof(struct __kernel_old_itimerval)))
338bd40a175SArnd Bergmann return -EFAULT;
339bd40a175SArnd Bergmann
340bd40a175SArnd Bergmann /* Validate the timevals in value. */
341bd40a175SArnd Bergmann if (!timeval_valid(&v.it_value) ||
342bd40a175SArnd Bergmann !timeval_valid(&v.it_interval))
343bd40a175SArnd Bergmann return -EINVAL;
344bd40a175SArnd Bergmann
345bd40a175SArnd Bergmann o->it_interval.tv_sec = v.it_interval.tv_sec;
346bd40a175SArnd Bergmann o->it_interval.tv_nsec = v.it_interval.tv_usec * NSEC_PER_USEC;
347bd40a175SArnd Bergmann o->it_value.tv_sec = v.it_value.tv_sec;
348bd40a175SArnd Bergmann o->it_value.tv_nsec = v.it_value.tv_usec * NSEC_PER_USEC;
349bd40a175SArnd Bergmann return 0;
350bd40a175SArnd Bergmann }
351bd40a175SArnd Bergmann
SYSCALL_DEFINE3(setitimer,int,which,struct __kernel_old_itimerval __user *,value,struct __kernel_old_itimerval __user *,ovalue)3524f9fbd89SArnd Bergmann SYSCALL_DEFINE3(setitimer, int, which, struct __kernel_old_itimerval __user *, value,
3534f9fbd89SArnd Bergmann struct __kernel_old_itimerval __user *, ovalue)
3545cee9645SThomas Gleixner {
355bd40a175SArnd Bergmann struct itimerspec64 set_buffer, get_buffer;
3565cee9645SThomas Gleixner int error;
3575cee9645SThomas Gleixner
3585cee9645SThomas Gleixner if (value) {
359bd40a175SArnd Bergmann error = get_itimerval(&set_buffer, value);
360bd40a175SArnd Bergmann if (error)
361bd40a175SArnd Bergmann return error;
3625cee9645SThomas Gleixner } else {
3635cee9645SThomas Gleixner memset(&set_buffer, 0, sizeof(set_buffer));
3645cee9645SThomas Gleixner printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
3655cee9645SThomas Gleixner " Misfeature support will be removed\n",
3665cee9645SThomas Gleixner current->comm);
3675cee9645SThomas Gleixner }
3685cee9645SThomas Gleixner
3695cee9645SThomas Gleixner error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
3705cee9645SThomas Gleixner if (error || !ovalue)
3715cee9645SThomas Gleixner return error;
3725cee9645SThomas Gleixner
373bd40a175SArnd Bergmann if (put_itimerval(ovalue, &get_buffer))
3745cee9645SThomas Gleixner return -EFAULT;
3755cee9645SThomas Gleixner return 0;
3765cee9645SThomas Gleixner }
37754ad9c46SAl Viro
3784c22ea2bSArnd Bergmann #if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA)
get_old_itimerval32(struct itimerspec64 * o,const struct old_itimerval32 __user * i)379bd40a175SArnd Bergmann static int get_old_itimerval32(struct itimerspec64 *o, const struct old_itimerval32 __user *i)
380c1745f84SArnd Bergmann {
381c1745f84SArnd Bergmann struct old_itimerval32 v32;
382c1745f84SArnd Bergmann
383c1745f84SArnd Bergmann if (copy_from_user(&v32, i, sizeof(struct old_itimerval32)))
384c1745f84SArnd Bergmann return -EFAULT;
385bd40a175SArnd Bergmann
386bd40a175SArnd Bergmann /* Validate the timevals in value. */
387bd40a175SArnd Bergmann if (!timeval_valid(&v32.it_value) ||
388bd40a175SArnd Bergmann !timeval_valid(&v32.it_interval))
389bd40a175SArnd Bergmann return -EINVAL;
390bd40a175SArnd Bergmann
391c1745f84SArnd Bergmann o->it_interval.tv_sec = v32.it_interval.tv_sec;
392bd40a175SArnd Bergmann o->it_interval.tv_nsec = v32.it_interval.tv_usec * NSEC_PER_USEC;
393c1745f84SArnd Bergmann o->it_value.tv_sec = v32.it_value.tv_sec;
394bd40a175SArnd Bergmann o->it_value.tv_nsec = v32.it_value.tv_usec * NSEC_PER_USEC;
395c1745f84SArnd Bergmann return 0;
396c1745f84SArnd Bergmann }
397c1745f84SArnd Bergmann
COMPAT_SYSCALL_DEFINE3(setitimer,int,which,struct old_itimerval32 __user *,value,struct old_itimerval32 __user *,ovalue)39854ad9c46SAl Viro COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
399bd40a175SArnd Bergmann struct old_itimerval32 __user *, value,
400bd40a175SArnd Bergmann struct old_itimerval32 __user *, ovalue)
40154ad9c46SAl Viro {
402bd40a175SArnd Bergmann struct itimerspec64 set_buffer, get_buffer;
40354ad9c46SAl Viro int error;
40454ad9c46SAl Viro
405bd40a175SArnd Bergmann if (value) {
406bd40a175SArnd Bergmann error = get_old_itimerval32(&set_buffer, value);
407bd40a175SArnd Bergmann if (error)
408bd40a175SArnd Bergmann return error;
40954ad9c46SAl Viro } else {
410bd40a175SArnd Bergmann memset(&set_buffer, 0, sizeof(set_buffer));
411bd40a175SArnd Bergmann printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
412bd40a175SArnd Bergmann " Misfeature support will be removed\n",
413bd40a175SArnd Bergmann current->comm);
41454ad9c46SAl Viro }
41554ad9c46SAl Viro
416bd40a175SArnd Bergmann error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
417bd40a175SArnd Bergmann if (error || !ovalue)
41854ad9c46SAl Viro return error;
419bd40a175SArnd Bergmann if (put_old_itimerval32(ovalue, &get_buffer))
42054ad9c46SAl Viro return -EFAULT;
42154ad9c46SAl Viro return 0;
42254ad9c46SAl Viro }
42354ad9c46SAl Viro #endif
424