xref: /openbmc/linux/kernel/time/posix-timers.c (revision 5c499410)
15cee9645SThomas Gleixner /*
25cee9645SThomas Gleixner  * linux/kernel/posix-timers.c
35cee9645SThomas Gleixner  *
45cee9645SThomas Gleixner  *
55cee9645SThomas Gleixner  * 2002-10-15  Posix Clocks & timers
65cee9645SThomas Gleixner  *                           by George Anzinger george@mvista.com
75cee9645SThomas Gleixner  *
85cee9645SThomas Gleixner  *			     Copyright (C) 2002 2003 by MontaVista Software.
95cee9645SThomas Gleixner  *
105cee9645SThomas Gleixner  * 2004-06-01  Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
115cee9645SThomas Gleixner  *			     Copyright (C) 2004 Boris Hu
125cee9645SThomas Gleixner  *
135cee9645SThomas Gleixner  * This program is free software; you can redistribute it and/or modify
145cee9645SThomas Gleixner  * it under the terms of the GNU General Public License as published by
155cee9645SThomas Gleixner  * the Free Software Foundation; either version 2 of the License, or (at
165cee9645SThomas Gleixner  * your option) any later version.
175cee9645SThomas Gleixner  *
185cee9645SThomas Gleixner  * This program is distributed in the hope that it will be useful, but
195cee9645SThomas Gleixner  * WITHOUT ANY WARRANTY; without even the implied warranty of
205cee9645SThomas Gleixner  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
215cee9645SThomas Gleixner  * General Public License for more details.
225cee9645SThomas Gleixner 
235cee9645SThomas Gleixner  * You should have received a copy of the GNU General Public License
245cee9645SThomas Gleixner  * along with this program; if not, write to the Free Software
255cee9645SThomas Gleixner  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
265cee9645SThomas Gleixner  *
275cee9645SThomas Gleixner  * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA
285cee9645SThomas Gleixner  */
295cee9645SThomas Gleixner 
305cee9645SThomas Gleixner /* These are all the functions necessary to implement
315cee9645SThomas Gleixner  * POSIX clocks & timers
325cee9645SThomas Gleixner  */
335cee9645SThomas Gleixner #include <linux/mm.h>
345cee9645SThomas Gleixner #include <linux/interrupt.h>
355cee9645SThomas Gleixner #include <linux/slab.h>
365cee9645SThomas Gleixner #include <linux/time.h>
375cee9645SThomas Gleixner #include <linux/mutex.h>
3861855b6bSIngo Molnar #include <linux/sched/task.h>
395cee9645SThomas Gleixner 
407c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
415cee9645SThomas Gleixner #include <linux/list.h>
425cee9645SThomas Gleixner #include <linux/init.h>
435cee9645SThomas Gleixner #include <linux/compiler.h>
445cee9645SThomas Gleixner #include <linux/hash.h>
455cee9645SThomas Gleixner #include <linux/posix-clock.h>
465cee9645SThomas Gleixner #include <linux/posix-timers.h>
475cee9645SThomas Gleixner #include <linux/syscalls.h>
485cee9645SThomas Gleixner #include <linux/wait.h>
495cee9645SThomas Gleixner #include <linux/workqueue.h>
505cee9645SThomas Gleixner #include <linux/export.h>
515cee9645SThomas Gleixner #include <linux/hashtable.h>
52edbeda46SAl Viro #include <linux/compat.h>
535cee9645SThomas Gleixner 
548b094cd0SThomas Gleixner #include "timekeeping.h"
55bab0aae9SThomas Gleixner #include "posix-timers.h"
568b094cd0SThomas Gleixner 
575cee9645SThomas Gleixner /*
585cee9645SThomas Gleixner  * Management arrays for POSIX timers. Timers are now kept in static hash table
595cee9645SThomas Gleixner  * with 512 entries.
605cee9645SThomas Gleixner  * Timer ids are allocated by local routine, which selects proper hash head by
615cee9645SThomas Gleixner  * key, constructed from current->signal address and per signal struct counter.
625cee9645SThomas Gleixner  * This keeps timer ids unique per process, but now they can intersect between
635cee9645SThomas Gleixner  * processes.
645cee9645SThomas Gleixner  */
655cee9645SThomas Gleixner 
665cee9645SThomas Gleixner /*
675cee9645SThomas Gleixner  * Lets keep our timers in a slab cache :-)
685cee9645SThomas Gleixner  */
695cee9645SThomas Gleixner static struct kmem_cache *posix_timers_cache;
705cee9645SThomas Gleixner 
715cee9645SThomas Gleixner static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
725cee9645SThomas Gleixner static DEFINE_SPINLOCK(hash_lock);
735cee9645SThomas Gleixner 
746631fa12SThomas Gleixner static const struct k_clock * const posix_clocks[];
756631fa12SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id);
7667edab48SThomas Gleixner static const struct k_clock clock_realtime, clock_monotonic;
776631fa12SThomas Gleixner 
785cee9645SThomas Gleixner /*
795cee9645SThomas Gleixner  * we assume that the new SIGEV_THREAD_ID shares no bits with the other
805cee9645SThomas Gleixner  * SIGEV values.  Here we put out an error if this assumption fails.
815cee9645SThomas Gleixner  */
825cee9645SThomas Gleixner #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
835cee9645SThomas Gleixner                        ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
845cee9645SThomas Gleixner #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
855cee9645SThomas Gleixner #endif
865cee9645SThomas Gleixner 
875cee9645SThomas Gleixner /*
885cee9645SThomas Gleixner  * parisc wants ENOTSUP instead of EOPNOTSUPP
895cee9645SThomas Gleixner  */
905cee9645SThomas Gleixner #ifndef ENOTSUP
915cee9645SThomas Gleixner # define ENANOSLEEP_NOTSUP EOPNOTSUPP
925cee9645SThomas Gleixner #else
935cee9645SThomas Gleixner # define ENANOSLEEP_NOTSUP ENOTSUP
945cee9645SThomas Gleixner #endif
955cee9645SThomas Gleixner 
965cee9645SThomas Gleixner /*
975cee9645SThomas Gleixner  * The timer ID is turned into a timer address by idr_find().
985cee9645SThomas Gleixner  * Verifying a valid ID consists of:
995cee9645SThomas Gleixner  *
1005cee9645SThomas Gleixner  * a) checking that idr_find() returns other than -1.
1015cee9645SThomas Gleixner  * b) checking that the timer id matches the one in the timer itself.
1025cee9645SThomas Gleixner  * c) that the timer owner is in the callers thread group.
1035cee9645SThomas Gleixner  */
1045cee9645SThomas Gleixner 
1055cee9645SThomas Gleixner /*
1065cee9645SThomas Gleixner  * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
1075cee9645SThomas Gleixner  *	    to implement others.  This structure defines the various
1085cee9645SThomas Gleixner  *	    clocks.
1095cee9645SThomas Gleixner  *
1105cee9645SThomas Gleixner  * RESOLUTION: Clock resolution is used to round up timer and interval
1115cee9645SThomas Gleixner  *	    times, NOT to report clock times, which are reported with as
1125cee9645SThomas Gleixner  *	    much resolution as the system can muster.  In some cases this
1135cee9645SThomas Gleixner  *	    resolution may depend on the underlying clock hardware and
1145cee9645SThomas Gleixner  *	    may not be quantifiable until run time, and only then is the
1155cee9645SThomas Gleixner  *	    necessary code is written.	The standard says we should say
1165cee9645SThomas Gleixner  *	    something about this issue in the documentation...
1175cee9645SThomas Gleixner  *
1185cee9645SThomas Gleixner  * FUNCTIONS: The CLOCKs structure defines possible functions to
1195cee9645SThomas Gleixner  *	    handle various clock functions.
1205cee9645SThomas Gleixner  *
1215cee9645SThomas Gleixner  *	    The standard POSIX timer management code assumes the
1225cee9645SThomas Gleixner  *	    following: 1.) The k_itimer struct (sched.h) is used for
1235cee9645SThomas Gleixner  *	    the timer.  2.) The list, it_lock, it_clock, it_id and
1245cee9645SThomas Gleixner  *	    it_pid fields are not modified by timer code.
1255cee9645SThomas Gleixner  *
1265cee9645SThomas Gleixner  * Permissions: It is assumed that the clock_settime() function defined
1275cee9645SThomas Gleixner  *	    for each clock will take care of permission checks.	 Some
1285cee9645SThomas Gleixner  *	    clocks may be set able by any user (i.e. local process
1295cee9645SThomas Gleixner  *	    clocks) others not.	 Currently the only set able clock we
1305cee9645SThomas Gleixner  *	    have is CLOCK_REALTIME and its high res counter part, both of
1315cee9645SThomas Gleixner  *	    which we beg off on and pass to do_sys_settimeofday().
1325cee9645SThomas Gleixner  */
1335cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
1345cee9645SThomas Gleixner 
1355cee9645SThomas Gleixner #define lock_timer(tid, flags)						   \
1365cee9645SThomas Gleixner ({	struct k_itimer *__timr;					   \
1375cee9645SThomas Gleixner 	__cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags));  \
1385cee9645SThomas Gleixner 	__timr;								   \
1395cee9645SThomas Gleixner })
1405cee9645SThomas Gleixner 
1415cee9645SThomas Gleixner static int hash(struct signal_struct *sig, unsigned int nr)
1425cee9645SThomas Gleixner {
1435cee9645SThomas Gleixner 	return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
1445cee9645SThomas Gleixner }
1455cee9645SThomas Gleixner 
1465cee9645SThomas Gleixner static struct k_itimer *__posix_timers_find(struct hlist_head *head,
1475cee9645SThomas Gleixner 					    struct signal_struct *sig,
1485cee9645SThomas Gleixner 					    timer_t id)
1495cee9645SThomas Gleixner {
1505cee9645SThomas Gleixner 	struct k_itimer *timer;
1515cee9645SThomas Gleixner 
1525cee9645SThomas Gleixner 	hlist_for_each_entry_rcu(timer, head, t_hash) {
1535cee9645SThomas Gleixner 		if ((timer->it_signal == sig) && (timer->it_id == id))
1545cee9645SThomas Gleixner 			return timer;
1555cee9645SThomas Gleixner 	}
1565cee9645SThomas Gleixner 	return NULL;
1575cee9645SThomas Gleixner }
1585cee9645SThomas Gleixner 
1595cee9645SThomas Gleixner static struct k_itimer *posix_timer_by_id(timer_t id)
1605cee9645SThomas Gleixner {
1615cee9645SThomas Gleixner 	struct signal_struct *sig = current->signal;
1625cee9645SThomas Gleixner 	struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
1635cee9645SThomas Gleixner 
1645cee9645SThomas Gleixner 	return __posix_timers_find(head, sig, id);
1655cee9645SThomas Gleixner }
1665cee9645SThomas Gleixner 
1675cee9645SThomas Gleixner static int posix_timer_add(struct k_itimer *timer)
1685cee9645SThomas Gleixner {
1695cee9645SThomas Gleixner 	struct signal_struct *sig = current->signal;
1705cee9645SThomas Gleixner 	int first_free_id = sig->posix_timer_id;
1715cee9645SThomas Gleixner 	struct hlist_head *head;
1725cee9645SThomas Gleixner 	int ret = -ENOENT;
1735cee9645SThomas Gleixner 
1745cee9645SThomas Gleixner 	do {
1755cee9645SThomas Gleixner 		spin_lock(&hash_lock);
1765cee9645SThomas Gleixner 		head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
1775cee9645SThomas Gleixner 		if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
1785cee9645SThomas Gleixner 			hlist_add_head_rcu(&timer->t_hash, head);
1795cee9645SThomas Gleixner 			ret = sig->posix_timer_id;
1805cee9645SThomas Gleixner 		}
1815cee9645SThomas Gleixner 		if (++sig->posix_timer_id < 0)
1825cee9645SThomas Gleixner 			sig->posix_timer_id = 0;
1835cee9645SThomas Gleixner 		if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
1845cee9645SThomas Gleixner 			/* Loop over all possible ids completed */
1855cee9645SThomas Gleixner 			ret = -EAGAIN;
1865cee9645SThomas Gleixner 		spin_unlock(&hash_lock);
1875cee9645SThomas Gleixner 	} while (ret == -ENOENT);
1885cee9645SThomas Gleixner 	return ret;
1895cee9645SThomas Gleixner }
1905cee9645SThomas Gleixner 
1915cee9645SThomas Gleixner static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
1925cee9645SThomas Gleixner {
1935cee9645SThomas Gleixner 	spin_unlock_irqrestore(&timr->it_lock, flags);
1945cee9645SThomas Gleixner }
1955cee9645SThomas Gleixner 
1965cee9645SThomas Gleixner /* Get clock_realtime */
1973c9c12f4SDeepa Dinamani static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp)
1985cee9645SThomas Gleixner {
1993c9c12f4SDeepa Dinamani 	ktime_get_real_ts64(tp);
2005cee9645SThomas Gleixner 	return 0;
2015cee9645SThomas Gleixner }
2025cee9645SThomas Gleixner 
2035cee9645SThomas Gleixner /* Set clock_realtime */
2045cee9645SThomas Gleixner static int posix_clock_realtime_set(const clockid_t which_clock,
2050fe6afe3SDeepa Dinamani 				    const struct timespec64 *tp)
2065cee9645SThomas Gleixner {
2070fe6afe3SDeepa Dinamani 	return do_sys_settimeofday64(tp, NULL);
2085cee9645SThomas Gleixner }
2095cee9645SThomas Gleixner 
2105cee9645SThomas Gleixner static int posix_clock_realtime_adj(const clockid_t which_clock,
2115cee9645SThomas Gleixner 				    struct timex *t)
2125cee9645SThomas Gleixner {
2135cee9645SThomas Gleixner 	return do_adjtimex(t);
2145cee9645SThomas Gleixner }
2155cee9645SThomas Gleixner 
2165cee9645SThomas Gleixner /*
2175cee9645SThomas Gleixner  * Get monotonic time for posix timers
2185cee9645SThomas Gleixner  */
2193c9c12f4SDeepa Dinamani static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
2205cee9645SThomas Gleixner {
2213c9c12f4SDeepa Dinamani 	ktime_get_ts64(tp);
2225cee9645SThomas Gleixner 	return 0;
2235cee9645SThomas Gleixner }
2245cee9645SThomas Gleixner 
2255cee9645SThomas Gleixner /*
2265cee9645SThomas Gleixner  * Get monotonic-raw time for posix timers
2275cee9645SThomas Gleixner  */
2283c9c12f4SDeepa Dinamani static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
2295cee9645SThomas Gleixner {
2303c9c12f4SDeepa Dinamani 	getrawmonotonic64(tp);
2315cee9645SThomas Gleixner 	return 0;
2325cee9645SThomas Gleixner }
2335cee9645SThomas Gleixner 
2345cee9645SThomas Gleixner 
2353c9c12f4SDeepa Dinamani static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
2365cee9645SThomas Gleixner {
2373c9c12f4SDeepa Dinamani 	*tp = current_kernel_time64();
2385cee9645SThomas Gleixner 	return 0;
2395cee9645SThomas Gleixner }
2405cee9645SThomas Gleixner 
2415cee9645SThomas Gleixner static int posix_get_monotonic_coarse(clockid_t which_clock,
2423c9c12f4SDeepa Dinamani 						struct timespec64 *tp)
2435cee9645SThomas Gleixner {
2443c9c12f4SDeepa Dinamani 	*tp = get_monotonic_coarse64();
2455cee9645SThomas Gleixner 	return 0;
2465cee9645SThomas Gleixner }
2475cee9645SThomas Gleixner 
248d2e3e0caSDeepa Dinamani static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
2495cee9645SThomas Gleixner {
250d2e3e0caSDeepa Dinamani 	*tp = ktime_to_timespec64(KTIME_LOW_RES);
2515cee9645SThomas Gleixner 	return 0;
2525cee9645SThomas Gleixner }
2535cee9645SThomas Gleixner 
2543c9c12f4SDeepa Dinamani static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
2555cee9645SThomas Gleixner {
2563c9c12f4SDeepa Dinamani 	get_monotonic_boottime64(tp);
2575cee9645SThomas Gleixner 	return 0;
2585cee9645SThomas Gleixner }
2595cee9645SThomas Gleixner 
2603c9c12f4SDeepa Dinamani static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
2615cee9645SThomas Gleixner {
2623c9c12f4SDeepa Dinamani 	timekeeping_clocktai64(tp);
2635cee9645SThomas Gleixner 	return 0;
2645cee9645SThomas Gleixner }
2655cee9645SThomas Gleixner 
266d2e3e0caSDeepa Dinamani static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
267056a3cacSThomas Gleixner {
268056a3cacSThomas Gleixner 	tp->tv_sec = 0;
269056a3cacSThomas Gleixner 	tp->tv_nsec = hrtimer_resolution;
270056a3cacSThomas Gleixner 	return 0;
271056a3cacSThomas Gleixner }
272056a3cacSThomas Gleixner 
273d3ba5a9aSChristoph Hellwig /*
274d3ba5a9aSChristoph Hellwig  * Initialize everything, well, just everything in Posix clocks/timers ;)
275d3ba5a9aSChristoph Hellwig  */
276d3ba5a9aSChristoph Hellwig static __init int init_posix_timers(void)
277d3ba5a9aSChristoph Hellwig {
2785cee9645SThomas Gleixner 	posix_timers_cache = kmem_cache_create("posix_timers_cache",
2795cee9645SThomas Gleixner 					sizeof (struct k_itimer), 0, SLAB_PANIC,
2805cee9645SThomas Gleixner 					NULL);
2815cee9645SThomas Gleixner 	return 0;
2825cee9645SThomas Gleixner }
2835cee9645SThomas Gleixner __initcall(init_posix_timers);
2845cee9645SThomas Gleixner 
285f37fb0aaSThomas Gleixner static void common_hrtimer_rearm(struct k_itimer *timr)
2865cee9645SThomas Gleixner {
2875cee9645SThomas Gleixner 	struct hrtimer *timer = &timr->it.real.timer;
2885cee9645SThomas Gleixner 
28980105cd0SThomas Gleixner 	if (!timr->it_interval)
2905cee9645SThomas Gleixner 		return;
2915cee9645SThomas Gleixner 
2925cee9645SThomas Gleixner 	timr->it_overrun += (unsigned int) hrtimer_forward(timer,
2935cee9645SThomas Gleixner 						timer->base->get_time(),
29480105cd0SThomas Gleixner 						timr->it_interval);
2955cee9645SThomas Gleixner 	hrtimer_restart(timer);
2965cee9645SThomas Gleixner }
2975cee9645SThomas Gleixner 
2985cee9645SThomas Gleixner /*
2995cee9645SThomas Gleixner  * This function is exported for use by the signal deliver code.  It is
3005cee9645SThomas Gleixner  * called just prior to the info block being released and passes that
3015cee9645SThomas Gleixner  * block to us.  It's function is to update the overrun entry AND to
3025cee9645SThomas Gleixner  * restart the timer.  It should only be called if the timer is to be
3035cee9645SThomas Gleixner  * restarted (i.e. we have flagged this in the sys_private entry of the
3045cee9645SThomas Gleixner  * info block).
3055cee9645SThomas Gleixner  *
3065cee9645SThomas Gleixner  * To protect against the timer going away while the interrupt is queued,
3075cee9645SThomas Gleixner  * we require that the it_requeue_pending flag be set.
3085cee9645SThomas Gleixner  */
30996fe3b07SThomas Gleixner void posixtimer_rearm(struct siginfo *info)
3105cee9645SThomas Gleixner {
3115cee9645SThomas Gleixner 	struct k_itimer *timr;
3125cee9645SThomas Gleixner 	unsigned long flags;
3135cee9645SThomas Gleixner 
3145cee9645SThomas Gleixner 	timr = lock_timer(info->si_tid, &flags);
315af888d67SThomas Gleixner 	if (!timr)
316af888d67SThomas Gleixner 		return;
3175cee9645SThomas Gleixner 
318af888d67SThomas Gleixner 	if (timr->it_requeue_pending == info->si_sys_private) {
319f37fb0aaSThomas Gleixner 		timr->kclock->timer_rearm(timr);
3205cee9645SThomas Gleixner 
32121e55c1fSThomas Gleixner 		timr->it_active = 1;
322af888d67SThomas Gleixner 		timr->it_overrun_last = timr->it_overrun;
323af888d67SThomas Gleixner 		timr->it_overrun = -1;
324af888d67SThomas Gleixner 		++timr->it_requeue_pending;
325af888d67SThomas Gleixner 
3265cee9645SThomas Gleixner 		info->si_overrun += timr->it_overrun_last;
3275cee9645SThomas Gleixner 	}
3285cee9645SThomas Gleixner 
3295cee9645SThomas Gleixner 	unlock_timer(timr, flags);
3305cee9645SThomas Gleixner }
3315cee9645SThomas Gleixner 
3325cee9645SThomas Gleixner int posix_timer_event(struct k_itimer *timr, int si_private)
3335cee9645SThomas Gleixner {
3345cee9645SThomas Gleixner 	struct task_struct *task;
3355cee9645SThomas Gleixner 	int shared, ret = -1;
3365cee9645SThomas Gleixner 	/*
3375cee9645SThomas Gleixner 	 * FIXME: if ->sigq is queued we can race with
33896fe3b07SThomas Gleixner 	 * dequeue_signal()->posixtimer_rearm().
3395cee9645SThomas Gleixner 	 *
3405cee9645SThomas Gleixner 	 * If dequeue_signal() sees the "right" value of
34196fe3b07SThomas Gleixner 	 * si_sys_private it calls posixtimer_rearm().
3425cee9645SThomas Gleixner 	 * We re-queue ->sigq and drop ->it_lock().
34396fe3b07SThomas Gleixner 	 * posixtimer_rearm() locks the timer
3445cee9645SThomas Gleixner 	 * and re-schedules it while ->sigq is pending.
3455cee9645SThomas Gleixner 	 * Not really bad, but not that we want.
3465cee9645SThomas Gleixner 	 */
3475cee9645SThomas Gleixner 	timr->sigq->info.si_sys_private = si_private;
3485cee9645SThomas Gleixner 
3495cee9645SThomas Gleixner 	rcu_read_lock();
3505cee9645SThomas Gleixner 	task = pid_task(timr->it_pid, PIDTYPE_PID);
3515cee9645SThomas Gleixner 	if (task) {
3525cee9645SThomas Gleixner 		shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
3535cee9645SThomas Gleixner 		ret = send_sigqueue(timr->sigq, task, shared);
3545cee9645SThomas Gleixner 	}
3555cee9645SThomas Gleixner 	rcu_read_unlock();
3565cee9645SThomas Gleixner 	/* If we failed to send the signal the timer stops. */
3575cee9645SThomas Gleixner 	return ret > 0;
3585cee9645SThomas Gleixner }
3595cee9645SThomas Gleixner 
3605cee9645SThomas Gleixner /*
3615cee9645SThomas Gleixner  * This function gets called when a POSIX.1b interval timer expires.  It
3625cee9645SThomas Gleixner  * is used as a callback from the kernel internal timer.  The
3635cee9645SThomas Gleixner  * run_timer_list code ALWAYS calls with interrupts on.
3645cee9645SThomas Gleixner 
3655cee9645SThomas Gleixner  * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
3665cee9645SThomas Gleixner  */
3675cee9645SThomas Gleixner static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
3685cee9645SThomas Gleixner {
3695cee9645SThomas Gleixner 	struct k_itimer *timr;
3705cee9645SThomas Gleixner 	unsigned long flags;
3715cee9645SThomas Gleixner 	int si_private = 0;
3725cee9645SThomas Gleixner 	enum hrtimer_restart ret = HRTIMER_NORESTART;
3735cee9645SThomas Gleixner 
3745cee9645SThomas Gleixner 	timr = container_of(timer, struct k_itimer, it.real.timer);
3755cee9645SThomas Gleixner 	spin_lock_irqsave(&timr->it_lock, flags);
3765cee9645SThomas Gleixner 
37721e55c1fSThomas Gleixner 	timr->it_active = 0;
37880105cd0SThomas Gleixner 	if (timr->it_interval != 0)
3795cee9645SThomas Gleixner 		si_private = ++timr->it_requeue_pending;
3805cee9645SThomas Gleixner 
3815cee9645SThomas Gleixner 	if (posix_timer_event(timr, si_private)) {
3825cee9645SThomas Gleixner 		/*
3835cee9645SThomas Gleixner 		 * signal was not sent because of sig_ignor
3845cee9645SThomas Gleixner 		 * we will not get a call back to restart it AND
3855cee9645SThomas Gleixner 		 * it should be restarted.
3865cee9645SThomas Gleixner 		 */
38780105cd0SThomas Gleixner 		if (timr->it_interval != 0) {
3885cee9645SThomas Gleixner 			ktime_t now = hrtimer_cb_get_time(timer);
3895cee9645SThomas Gleixner 
3905cee9645SThomas Gleixner 			/*
3915cee9645SThomas Gleixner 			 * FIXME: What we really want, is to stop this
3925cee9645SThomas Gleixner 			 * timer completely and restart it in case the
3935cee9645SThomas Gleixner 			 * SIG_IGN is removed. This is a non trivial
3945cee9645SThomas Gleixner 			 * change which involves sighand locking
3955cee9645SThomas Gleixner 			 * (sigh !), which we don't want to do late in
3965cee9645SThomas Gleixner 			 * the release cycle.
3975cee9645SThomas Gleixner 			 *
3985cee9645SThomas Gleixner 			 * For now we just let timers with an interval
3995cee9645SThomas Gleixner 			 * less than a jiffie expire every jiffie to
4005cee9645SThomas Gleixner 			 * avoid softirq starvation in case of SIG_IGN
4015cee9645SThomas Gleixner 			 * and a very small interval, which would put
4025cee9645SThomas Gleixner 			 * the timer right back on the softirq pending
4035cee9645SThomas Gleixner 			 * list. By moving now ahead of time we trick
4045cee9645SThomas Gleixner 			 * hrtimer_forward() to expire the timer
4055cee9645SThomas Gleixner 			 * later, while we still maintain the overrun
4065cee9645SThomas Gleixner 			 * accuracy, but have some inconsistency in
4075cee9645SThomas Gleixner 			 * the timer_gettime() case. This is at least
4085cee9645SThomas Gleixner 			 * better than a starved softirq. A more
4095cee9645SThomas Gleixner 			 * complex fix which solves also another related
4105cee9645SThomas Gleixner 			 * inconsistency is already in the pipeline.
4115cee9645SThomas Gleixner 			 */
4125cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
4135cee9645SThomas Gleixner 			{
4148b0e1953SThomas Gleixner 				ktime_t kj = NSEC_PER_SEC / HZ;
4155cee9645SThomas Gleixner 
41680105cd0SThomas Gleixner 				if (timr->it_interval < kj)
4175cee9645SThomas Gleixner 					now = ktime_add(now, kj);
4185cee9645SThomas Gleixner 			}
4195cee9645SThomas Gleixner #endif
4205cee9645SThomas Gleixner 			timr->it_overrun += (unsigned int)
4215cee9645SThomas Gleixner 				hrtimer_forward(timer, now,
42280105cd0SThomas Gleixner 						timr->it_interval);
4235cee9645SThomas Gleixner 			ret = HRTIMER_RESTART;
4245cee9645SThomas Gleixner 			++timr->it_requeue_pending;
42521e55c1fSThomas Gleixner 			timr->it_active = 1;
4265cee9645SThomas Gleixner 		}
4275cee9645SThomas Gleixner 	}
4285cee9645SThomas Gleixner 
4295cee9645SThomas Gleixner 	unlock_timer(timr, flags);
4305cee9645SThomas Gleixner 	return ret;
4315cee9645SThomas Gleixner }
4325cee9645SThomas Gleixner 
4335cee9645SThomas Gleixner static struct pid *good_sigevent(sigevent_t * event)
4345cee9645SThomas Gleixner {
4355cee9645SThomas Gleixner 	struct task_struct *rtn = current->group_leader;
4365cee9645SThomas Gleixner 
4375cee9645SThomas Gleixner 	if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
4385cee9645SThomas Gleixner 		(!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
4395cee9645SThomas Gleixner 		 !same_thread_group(rtn, current) ||
4405cee9645SThomas Gleixner 		 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
4415cee9645SThomas Gleixner 		return NULL;
4425cee9645SThomas Gleixner 
4435cee9645SThomas Gleixner 	if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
4445cee9645SThomas Gleixner 	    ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
4455cee9645SThomas Gleixner 		return NULL;
4465cee9645SThomas Gleixner 
4475cee9645SThomas Gleixner 	return task_pid(rtn);
4485cee9645SThomas Gleixner }
4495cee9645SThomas Gleixner 
4505cee9645SThomas Gleixner static struct k_itimer * alloc_posix_timer(void)
4515cee9645SThomas Gleixner {
4525cee9645SThomas Gleixner 	struct k_itimer *tmr;
4535cee9645SThomas Gleixner 	tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
4545cee9645SThomas Gleixner 	if (!tmr)
4555cee9645SThomas Gleixner 		return tmr;
4565cee9645SThomas Gleixner 	if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
4575cee9645SThomas Gleixner 		kmem_cache_free(posix_timers_cache, tmr);
4585cee9645SThomas Gleixner 		return NULL;
4595cee9645SThomas Gleixner 	}
4605cee9645SThomas Gleixner 	memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
4615cee9645SThomas Gleixner 	return tmr;
4625cee9645SThomas Gleixner }
4635cee9645SThomas Gleixner 
4645cee9645SThomas Gleixner static void k_itimer_rcu_free(struct rcu_head *head)
4655cee9645SThomas Gleixner {
4665cee9645SThomas Gleixner 	struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
4675cee9645SThomas Gleixner 
4685cee9645SThomas Gleixner 	kmem_cache_free(posix_timers_cache, tmr);
4695cee9645SThomas Gleixner }
4705cee9645SThomas Gleixner 
4715cee9645SThomas Gleixner #define IT_ID_SET	1
4725cee9645SThomas Gleixner #define IT_ID_NOT_SET	0
4735cee9645SThomas Gleixner static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
4745cee9645SThomas Gleixner {
4755cee9645SThomas Gleixner 	if (it_id_set) {
4765cee9645SThomas Gleixner 		unsigned long flags;
4775cee9645SThomas Gleixner 		spin_lock_irqsave(&hash_lock, flags);
4785cee9645SThomas Gleixner 		hlist_del_rcu(&tmr->t_hash);
4795cee9645SThomas Gleixner 		spin_unlock_irqrestore(&hash_lock, flags);
4805cee9645SThomas Gleixner 	}
4815cee9645SThomas Gleixner 	put_pid(tmr->it_pid);
4825cee9645SThomas Gleixner 	sigqueue_free(tmr->sigq);
4835cee9645SThomas Gleixner 	call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
4845cee9645SThomas Gleixner }
4855cee9645SThomas Gleixner 
4865cee9645SThomas Gleixner static int common_timer_create(struct k_itimer *new_timer)
4875cee9645SThomas Gleixner {
4885cee9645SThomas Gleixner 	hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
4895cee9645SThomas Gleixner 	return 0;
4905cee9645SThomas Gleixner }
4915cee9645SThomas Gleixner 
4925cee9645SThomas Gleixner /* Create a POSIX.1b interval timer. */
4932482097cSAl Viro static int do_timer_create(clockid_t which_clock, struct sigevent *event,
4942482097cSAl Viro 			   timer_t __user *created_timer_id)
4955cee9645SThomas Gleixner {
496d3ba5a9aSChristoph Hellwig 	const struct k_clock *kc = clockid_to_kclock(which_clock);
4975cee9645SThomas Gleixner 	struct k_itimer *new_timer;
4985cee9645SThomas Gleixner 	int error, new_timer_id;
4995cee9645SThomas Gleixner 	int it_id_set = IT_ID_NOT_SET;
5005cee9645SThomas Gleixner 
5015cee9645SThomas Gleixner 	if (!kc)
5025cee9645SThomas Gleixner 		return -EINVAL;
5035cee9645SThomas Gleixner 	if (!kc->timer_create)
5045cee9645SThomas Gleixner 		return -EOPNOTSUPP;
5055cee9645SThomas Gleixner 
5065cee9645SThomas Gleixner 	new_timer = alloc_posix_timer();
5075cee9645SThomas Gleixner 	if (unlikely(!new_timer))
5085cee9645SThomas Gleixner 		return -EAGAIN;
5095cee9645SThomas Gleixner 
5105cee9645SThomas Gleixner 	spin_lock_init(&new_timer->it_lock);
5115cee9645SThomas Gleixner 	new_timer_id = posix_timer_add(new_timer);
5125cee9645SThomas Gleixner 	if (new_timer_id < 0) {
5135cee9645SThomas Gleixner 		error = new_timer_id;
5145cee9645SThomas Gleixner 		goto out;
5155cee9645SThomas Gleixner 	}
5165cee9645SThomas Gleixner 
5175cee9645SThomas Gleixner 	it_id_set = IT_ID_SET;
5185cee9645SThomas Gleixner 	new_timer->it_id = (timer_t) new_timer_id;
5195cee9645SThomas Gleixner 	new_timer->it_clock = which_clock;
520d97bb75dSThomas Gleixner 	new_timer->kclock = kc;
5215cee9645SThomas Gleixner 	new_timer->it_overrun = -1;
5225cee9645SThomas Gleixner 
5232482097cSAl Viro 	if (event) {
5245cee9645SThomas Gleixner 		rcu_read_lock();
5252482097cSAl Viro 		new_timer->it_pid = get_pid(good_sigevent(event));
5265cee9645SThomas Gleixner 		rcu_read_unlock();
5275cee9645SThomas Gleixner 		if (!new_timer->it_pid) {
5285cee9645SThomas Gleixner 			error = -EINVAL;
5295cee9645SThomas Gleixner 			goto out;
5305cee9645SThomas Gleixner 		}
5312482097cSAl Viro 		new_timer->it_sigev_notify     = event->sigev_notify;
5322482097cSAl Viro 		new_timer->sigq->info.si_signo = event->sigev_signo;
5332482097cSAl Viro 		new_timer->sigq->info.si_value = event->sigev_value;
5345cee9645SThomas Gleixner 	} else {
5352482097cSAl Viro 		new_timer->it_sigev_notify     = SIGEV_SIGNAL;
5362482097cSAl Viro 		new_timer->sigq->info.si_signo = SIGALRM;
5372482097cSAl Viro 		memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
5382482097cSAl Viro 		new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
5395cee9645SThomas Gleixner 		new_timer->it_pid = get_pid(task_tgid(current));
5405cee9645SThomas Gleixner 	}
5415cee9645SThomas Gleixner 
5425cee9645SThomas Gleixner 	new_timer->sigq->info.si_tid   = new_timer->it_id;
5435cee9645SThomas Gleixner 	new_timer->sigq->info.si_code  = SI_TIMER;
5445cee9645SThomas Gleixner 
5455cee9645SThomas Gleixner 	if (copy_to_user(created_timer_id,
5465cee9645SThomas Gleixner 			 &new_timer_id, sizeof (new_timer_id))) {
5475cee9645SThomas Gleixner 		error = -EFAULT;
5485cee9645SThomas Gleixner 		goto out;
5495cee9645SThomas Gleixner 	}
5505cee9645SThomas Gleixner 
5515cee9645SThomas Gleixner 	error = kc->timer_create(new_timer);
5525cee9645SThomas Gleixner 	if (error)
5535cee9645SThomas Gleixner 		goto out;
5545cee9645SThomas Gleixner 
5555cee9645SThomas Gleixner 	spin_lock_irq(&current->sighand->siglock);
5565cee9645SThomas Gleixner 	new_timer->it_signal = current->signal;
5575cee9645SThomas Gleixner 	list_add(&new_timer->list, &current->signal->posix_timers);
5585cee9645SThomas Gleixner 	spin_unlock_irq(&current->sighand->siglock);
5595cee9645SThomas Gleixner 
5605cee9645SThomas Gleixner 	return 0;
5615cee9645SThomas Gleixner 	/*
5625cee9645SThomas Gleixner 	 * In the case of the timer belonging to another task, after
5635cee9645SThomas Gleixner 	 * the task is unlocked, the timer is owned by the other task
5645cee9645SThomas Gleixner 	 * and may cease to exist at any time.  Don't use or modify
5655cee9645SThomas Gleixner 	 * new_timer after the unlock call.
5665cee9645SThomas Gleixner 	 */
5675cee9645SThomas Gleixner out:
5685cee9645SThomas Gleixner 	release_posix_timer(new_timer, it_id_set);
5695cee9645SThomas Gleixner 	return error;
5705cee9645SThomas Gleixner }
5715cee9645SThomas Gleixner 
5722482097cSAl Viro SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
5732482097cSAl Viro 		struct sigevent __user *, timer_event_spec,
5742482097cSAl Viro 		timer_t __user *, created_timer_id)
5752482097cSAl Viro {
5762482097cSAl Viro 	if (timer_event_spec) {
5772482097cSAl Viro 		sigevent_t event;
5782482097cSAl Viro 
5792482097cSAl Viro 		if (copy_from_user(&event, timer_event_spec, sizeof (event)))
5802482097cSAl Viro 			return -EFAULT;
5812482097cSAl Viro 		return do_timer_create(which_clock, &event, created_timer_id);
5822482097cSAl Viro 	}
5832482097cSAl Viro 	return do_timer_create(which_clock, NULL, created_timer_id);
5842482097cSAl Viro }
5852482097cSAl Viro 
5862482097cSAl Viro #ifdef CONFIG_COMPAT
5872482097cSAl Viro COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
5882482097cSAl Viro 		       struct compat_sigevent __user *, timer_event_spec,
5892482097cSAl Viro 		       timer_t __user *, created_timer_id)
5902482097cSAl Viro {
5912482097cSAl Viro 	if (timer_event_spec) {
5922482097cSAl Viro 		sigevent_t event;
5932482097cSAl Viro 
5942482097cSAl Viro 		if (get_compat_sigevent(&event, timer_event_spec))
5952482097cSAl Viro 			return -EFAULT;
5962482097cSAl Viro 		return do_timer_create(which_clock, &event, created_timer_id);
5972482097cSAl Viro 	}
5982482097cSAl Viro 	return do_timer_create(which_clock, NULL, created_timer_id);
5992482097cSAl Viro }
6002482097cSAl Viro #endif
6012482097cSAl Viro 
6025cee9645SThomas Gleixner /*
6035cee9645SThomas Gleixner  * Locking issues: We need to protect the result of the id look up until
6045cee9645SThomas Gleixner  * we get the timer locked down so it is not deleted under us.  The
6055cee9645SThomas Gleixner  * removal is done under the idr spinlock so we use that here to bridge
6065cee9645SThomas Gleixner  * the find to the timer lock.  To avoid a dead lock, the timer id MUST
6075cee9645SThomas Gleixner  * be release with out holding the timer lock.
6085cee9645SThomas Gleixner  */
6095cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
6105cee9645SThomas Gleixner {
6115cee9645SThomas Gleixner 	struct k_itimer *timr;
6125cee9645SThomas Gleixner 
6135cee9645SThomas Gleixner 	/*
6145cee9645SThomas Gleixner 	 * timer_t could be any type >= int and we want to make sure any
6155cee9645SThomas Gleixner 	 * @timer_id outside positive int range fails lookup.
6165cee9645SThomas Gleixner 	 */
6175cee9645SThomas Gleixner 	if ((unsigned long long)timer_id > INT_MAX)
6185cee9645SThomas Gleixner 		return NULL;
6195cee9645SThomas Gleixner 
6205cee9645SThomas Gleixner 	rcu_read_lock();
6215cee9645SThomas Gleixner 	timr = posix_timer_by_id(timer_id);
6225cee9645SThomas Gleixner 	if (timr) {
6235cee9645SThomas Gleixner 		spin_lock_irqsave(&timr->it_lock, *flags);
6245cee9645SThomas Gleixner 		if (timr->it_signal == current->signal) {
6255cee9645SThomas Gleixner 			rcu_read_unlock();
6265cee9645SThomas Gleixner 			return timr;
6275cee9645SThomas Gleixner 		}
6285cee9645SThomas Gleixner 		spin_unlock_irqrestore(&timr->it_lock, *flags);
6295cee9645SThomas Gleixner 	}
6305cee9645SThomas Gleixner 	rcu_read_unlock();
6315cee9645SThomas Gleixner 
6325cee9645SThomas Gleixner 	return NULL;
6335cee9645SThomas Gleixner }
6345cee9645SThomas Gleixner 
63591d57baeSThomas Gleixner static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
63691d57baeSThomas Gleixner {
63791d57baeSThomas Gleixner 	struct hrtimer *timer = &timr->it.real.timer;
63891d57baeSThomas Gleixner 
63991d57baeSThomas Gleixner 	return __hrtimer_expires_remaining_adjusted(timer, now);
64091d57baeSThomas Gleixner }
64191d57baeSThomas Gleixner 
64291d57baeSThomas Gleixner static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
64391d57baeSThomas Gleixner {
64491d57baeSThomas Gleixner 	struct hrtimer *timer = &timr->it.real.timer;
64591d57baeSThomas Gleixner 
64691d57baeSThomas Gleixner 	return (int)hrtimer_forward(timer, now, timr->it_interval);
64791d57baeSThomas Gleixner }
64891d57baeSThomas Gleixner 
6495cee9645SThomas Gleixner /*
6505cee9645SThomas Gleixner  * Get the time remaining on a POSIX.1b interval timer.  This function
6515cee9645SThomas Gleixner  * is ALWAYS called with spin_lock_irq on the timer, thus it must not
6525cee9645SThomas Gleixner  * mess with irq.
6535cee9645SThomas Gleixner  *
6545cee9645SThomas Gleixner  * We have a couple of messes to clean up here.  First there is the case
6555cee9645SThomas Gleixner  * of a timer that has a requeue pending.  These timers should appear to
6565cee9645SThomas Gleixner  * be in the timer list with an expiry as if we were to requeue them
6575cee9645SThomas Gleixner  * now.
6585cee9645SThomas Gleixner  *
6595cee9645SThomas Gleixner  * The second issue is the SIGEV_NONE timer which may be active but is
6605cee9645SThomas Gleixner  * not really ever put in the timer list (to save system resources).
6615cee9645SThomas Gleixner  * This timer may be expired, and if so, we will do it here.  Otherwise
6625cee9645SThomas Gleixner  * it is the same as a requeue pending timer WRT to what we should
6635cee9645SThomas Gleixner  * report.
6645cee9645SThomas Gleixner  */
665f2c45807SThomas Gleixner void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
6665cee9645SThomas Gleixner {
66791d57baeSThomas Gleixner 	const struct k_clock *kc = timr->kclock;
6685cee9645SThomas Gleixner 	ktime_t now, remaining, iv;
66991d57baeSThomas Gleixner 	struct timespec64 ts64;
67091d57baeSThomas Gleixner 	bool sig_none;
6715cee9645SThomas Gleixner 
672c6503be5SThomas Gleixner 	sig_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
67380105cd0SThomas Gleixner 	iv = timr->it_interval;
6745cee9645SThomas Gleixner 
6755cee9645SThomas Gleixner 	/* interval timer ? */
67691d57baeSThomas Gleixner 	if (iv) {
6775f252b32SDeepa Dinamani 		cur_setting->it_interval = ktime_to_timespec64(iv);
67891d57baeSThomas Gleixner 	} else if (!timr->it_active) {
67991d57baeSThomas Gleixner 		/*
68091d57baeSThomas Gleixner 		 * SIGEV_NONE oneshot timers are never queued. Check them
68191d57baeSThomas Gleixner 		 * below.
68291d57baeSThomas Gleixner 		 */
68391d57baeSThomas Gleixner 		if (!sig_none)
6845cee9645SThomas Gleixner 			return;
68591d57baeSThomas Gleixner 	}
6865cee9645SThomas Gleixner 
6875cee9645SThomas Gleixner 	/*
68891d57baeSThomas Gleixner 	 * The timespec64 based conversion is suboptimal, but it's not
68991d57baeSThomas Gleixner 	 * worth to implement yet another callback.
6905cee9645SThomas Gleixner 	 */
69191d57baeSThomas Gleixner 	kc->clock_get(timr->it_clock, &ts64);
69291d57baeSThomas Gleixner 	now = timespec64_to_ktime(ts64);
6935cee9645SThomas Gleixner 
69491d57baeSThomas Gleixner 	/*
69591d57baeSThomas Gleixner 	 * When a requeue is pending or this is a SIGEV_NONE timer move the
69691d57baeSThomas Gleixner 	 * expiry time forward by intervals, so expiry is > now.
69791d57baeSThomas Gleixner 	 */
69891d57baeSThomas Gleixner 	if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
69991d57baeSThomas Gleixner 		timr->it_overrun += kc->timer_forward(timr, now);
70091d57baeSThomas Gleixner 
70191d57baeSThomas Gleixner 	remaining = kc->timer_remaining(timr, now);
7025cee9645SThomas Gleixner 	/* Return 0 only, when the timer is expired and not pending */
7032456e855SThomas Gleixner 	if (remaining <= 0) {
7045cee9645SThomas Gleixner 		/*
7055cee9645SThomas Gleixner 		 * A single shot SIGEV_NONE timer must return 0, when
7065cee9645SThomas Gleixner 		 * it is expired !
7075cee9645SThomas Gleixner 		 */
70891d57baeSThomas Gleixner 		if (!sig_none)
7095cee9645SThomas Gleixner 			cur_setting->it_value.tv_nsec = 1;
71091d57baeSThomas Gleixner 	} else {
7115f252b32SDeepa Dinamani 		cur_setting->it_value = ktime_to_timespec64(remaining);
7125cee9645SThomas Gleixner 	}
71391d57baeSThomas Gleixner }
7145cee9645SThomas Gleixner 
7155cee9645SThomas Gleixner /* Get the time remaining on a POSIX.1b interval timer. */
716b0dc1242SAl Viro static int do_timer_gettime(timer_t timer_id,  struct itimerspec64 *setting)
7175cee9645SThomas Gleixner {
7185cee9645SThomas Gleixner 	struct k_itimer *timr;
719d3ba5a9aSChristoph Hellwig 	const struct k_clock *kc;
7205cee9645SThomas Gleixner 	unsigned long flags;
7215cee9645SThomas Gleixner 	int ret = 0;
7225cee9645SThomas Gleixner 
7235cee9645SThomas Gleixner 	timr = lock_timer(timer_id, &flags);
7245cee9645SThomas Gleixner 	if (!timr)
7255cee9645SThomas Gleixner 		return -EINVAL;
7265cee9645SThomas Gleixner 
727b0dc1242SAl Viro 	memset(setting, 0, sizeof(*setting));
728d97bb75dSThomas Gleixner 	kc = timr->kclock;
7295cee9645SThomas Gleixner 	if (WARN_ON_ONCE(!kc || !kc->timer_get))
7305cee9645SThomas Gleixner 		ret = -EINVAL;
7315cee9645SThomas Gleixner 	else
732b0dc1242SAl Viro 		kc->timer_get(timr, setting);
7335cee9645SThomas Gleixner 
7345cee9645SThomas Gleixner 	unlock_timer(timr, flags);
7355cee9645SThomas Gleixner 	return ret;
7365cee9645SThomas Gleixner }
7375cee9645SThomas Gleixner 
738b0dc1242SAl Viro /* Get the time remaining on a POSIX.1b interval timer. */
739b0dc1242SAl Viro SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
740b0dc1242SAl Viro 		struct itimerspec __user *, setting)
741b0dc1242SAl Viro {
742b0dc1242SAl Viro 	struct itimerspec64 cur_setting64;
743b0dc1242SAl Viro 
744b0dc1242SAl Viro 	int ret = do_timer_gettime(timer_id, &cur_setting64);
745b0dc1242SAl Viro 	if (!ret) {
746b0dc1242SAl Viro 		struct itimerspec cur_setting;
747b0dc1242SAl Viro 		cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
748b0dc1242SAl Viro 		if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
749b0dc1242SAl Viro 			ret = -EFAULT;
750b0dc1242SAl Viro 	}
751b0dc1242SAl Viro 	return ret;
752b0dc1242SAl Viro }
753b0dc1242SAl Viro 
754b0dc1242SAl Viro #ifdef CONFIG_COMPAT
755b0dc1242SAl Viro COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
756b0dc1242SAl Viro 		       struct compat_itimerspec __user *, setting)
757b0dc1242SAl Viro {
758b0dc1242SAl Viro 	struct itimerspec64 cur_setting64;
759b0dc1242SAl Viro 
760b0dc1242SAl Viro 	int ret = do_timer_gettime(timer_id, &cur_setting64);
761b0dc1242SAl Viro 	if (!ret) {
762b0dc1242SAl Viro 		struct itimerspec cur_setting;
763b0dc1242SAl Viro 		cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
764b0dc1242SAl Viro 		if (put_compat_itimerspec(setting, &cur_setting))
765b0dc1242SAl Viro 			ret = -EFAULT;
766b0dc1242SAl Viro 	}
767b0dc1242SAl Viro 	return ret;
768b0dc1242SAl Viro }
769b0dc1242SAl Viro #endif
770b0dc1242SAl Viro 
7715cee9645SThomas Gleixner /*
7725cee9645SThomas Gleixner  * Get the number of overruns of a POSIX.1b interval timer.  This is to
7735cee9645SThomas Gleixner  * be the overrun of the timer last delivered.  At the same time we are
7745cee9645SThomas Gleixner  * accumulating overruns on the next timer.  The overrun is frozen when
7755cee9645SThomas Gleixner  * the signal is delivered, either at the notify time (if the info block
7765cee9645SThomas Gleixner  * is not queued) or at the actual delivery time (as we are informed by
77796fe3b07SThomas Gleixner  * the call back to posixtimer_rearm().  So all we need to do is
7785cee9645SThomas Gleixner  * to pick up the frozen overrun.
7795cee9645SThomas Gleixner  */
7805cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
7815cee9645SThomas Gleixner {
7825cee9645SThomas Gleixner 	struct k_itimer *timr;
7835cee9645SThomas Gleixner 	int overrun;
7845cee9645SThomas Gleixner 	unsigned long flags;
7855cee9645SThomas Gleixner 
7865cee9645SThomas Gleixner 	timr = lock_timer(timer_id, &flags);
7875cee9645SThomas Gleixner 	if (!timr)
7885cee9645SThomas Gleixner 		return -EINVAL;
7895cee9645SThomas Gleixner 
7905cee9645SThomas Gleixner 	overrun = timr->it_overrun_last;
7915cee9645SThomas Gleixner 	unlock_timer(timr, flags);
7925cee9645SThomas Gleixner 
7935cee9645SThomas Gleixner 	return overrun;
7945cee9645SThomas Gleixner }
7955cee9645SThomas Gleixner 
796eae1c4aeSThomas Gleixner static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
797eae1c4aeSThomas Gleixner 			       bool absolute, bool sigev_none)
7985cee9645SThomas Gleixner {
7995cee9645SThomas Gleixner 	struct hrtimer *timer = &timr->it.real.timer;
8005cee9645SThomas Gleixner 	enum hrtimer_mode mode;
8015cee9645SThomas Gleixner 
802eae1c4aeSThomas Gleixner 	mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
80367edab48SThomas Gleixner 	/*
80467edab48SThomas Gleixner 	 * Posix magic: Relative CLOCK_REALTIME timers are not affected by
80567edab48SThomas Gleixner 	 * clock modifications, so they become CLOCK_MONOTONIC based under the
80667edab48SThomas Gleixner 	 * hood. See hrtimer_init(). Update timr->kclock, so the generic
80767edab48SThomas Gleixner 	 * functions which use timr->kclock->clock_get() work.
80867edab48SThomas Gleixner 	 *
80967edab48SThomas Gleixner 	 * Note: it_clock stays unmodified, because the next timer_set() might
81067edab48SThomas Gleixner 	 * use ABSTIME, so it needs to switch back.
81167edab48SThomas Gleixner 	 */
81267edab48SThomas Gleixner 	if (timr->it_clock == CLOCK_REALTIME)
81367edab48SThomas Gleixner 		timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
81467edab48SThomas Gleixner 
815eae1c4aeSThomas Gleixner 	hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
816eae1c4aeSThomas Gleixner 	timr->it.real.timer.function = posix_timer_fn;
817eae1c4aeSThomas Gleixner 
818eae1c4aeSThomas Gleixner 	if (!absolute)
819eae1c4aeSThomas Gleixner 		expires = ktime_add_safe(expires, timer->base->get_time());
820eae1c4aeSThomas Gleixner 	hrtimer_set_expires(timer, expires);
821eae1c4aeSThomas Gleixner 
822eae1c4aeSThomas Gleixner 	if (!sigev_none)
823eae1c4aeSThomas Gleixner 		hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
824eae1c4aeSThomas Gleixner }
825eae1c4aeSThomas Gleixner 
826eae1c4aeSThomas Gleixner static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
827eae1c4aeSThomas Gleixner {
828eae1c4aeSThomas Gleixner 	return hrtimer_try_to_cancel(&timr->it.real.timer);
829eae1c4aeSThomas Gleixner }
830eae1c4aeSThomas Gleixner 
831eae1c4aeSThomas Gleixner /* Set a POSIX.1b interval timer. */
832f2c45807SThomas Gleixner int common_timer_set(struct k_itimer *timr, int flags,
833eae1c4aeSThomas Gleixner 		     struct itimerspec64 *new_setting,
834eae1c4aeSThomas Gleixner 		     struct itimerspec64 *old_setting)
835eae1c4aeSThomas Gleixner {
836eae1c4aeSThomas Gleixner 	const struct k_clock *kc = timr->kclock;
837eae1c4aeSThomas Gleixner 	bool sigev_none;
838eae1c4aeSThomas Gleixner 	ktime_t expires;
839eae1c4aeSThomas Gleixner 
8405cee9645SThomas Gleixner 	if (old_setting)
8415cee9645SThomas Gleixner 		common_timer_get(timr, old_setting);
8425cee9645SThomas Gleixner 
843eae1c4aeSThomas Gleixner 	/* Prevent rearming by clearing the interval */
84480105cd0SThomas Gleixner 	timr->it_interval = 0;
8455cee9645SThomas Gleixner 	/*
846eae1c4aeSThomas Gleixner 	 * Careful here. On SMP systems the timer expiry function could be
847eae1c4aeSThomas Gleixner 	 * active and spinning on timr->it_lock.
8485cee9645SThomas Gleixner 	 */
849eae1c4aeSThomas Gleixner 	if (kc->timer_try_to_cancel(timr) < 0)
8505cee9645SThomas Gleixner 		return TIMER_RETRY;
8515cee9645SThomas Gleixner 
85221e55c1fSThomas Gleixner 	timr->it_active = 0;
8535cee9645SThomas Gleixner 	timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
8545cee9645SThomas Gleixner 		~REQUEUE_PENDING;
8555cee9645SThomas Gleixner 	timr->it_overrun_last = 0;
8565cee9645SThomas Gleixner 
857eae1c4aeSThomas Gleixner 	/* Switch off the timer when it_value is zero */
8585cee9645SThomas Gleixner 	if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
8595cee9645SThomas Gleixner 		return 0;
8605cee9645SThomas Gleixner 
86180105cd0SThomas Gleixner 	timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
862eae1c4aeSThomas Gleixner 	expires = timespec64_to_ktime(new_setting->it_value);
863eae1c4aeSThomas Gleixner 	sigev_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
8645cee9645SThomas Gleixner 
865eae1c4aeSThomas Gleixner 	kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
866eae1c4aeSThomas Gleixner 	timr->it_active = !sigev_none;
8675cee9645SThomas Gleixner 	return 0;
8685cee9645SThomas Gleixner }
8695cee9645SThomas Gleixner 
8701acbe770SAl Viro static int do_timer_settime(timer_t timer_id, int flags,
8711acbe770SAl Viro 			    struct itimerspec64 *new_spec64,
8721acbe770SAl Viro 			    struct itimerspec64 *old_spec64)
8735cee9645SThomas Gleixner {
8741acbe770SAl Viro 	const struct k_clock *kc;
8755f252b32SDeepa Dinamani 	struct k_itimer *timr;
8765cee9645SThomas Gleixner 	unsigned long flag;
8775f252b32SDeepa Dinamani 	int error = 0;
8785cee9645SThomas Gleixner 
8791acbe770SAl Viro 	if (!timespec64_valid(&new_spec64->it_interval) ||
8801acbe770SAl Viro 	    !timespec64_valid(&new_spec64->it_value))
8815cee9645SThomas Gleixner 		return -EINVAL;
8825cee9645SThomas Gleixner 
8831acbe770SAl Viro 	if (old_spec64)
8841acbe770SAl Viro 		memset(old_spec64, 0, sizeof(*old_spec64));
8855cee9645SThomas Gleixner retry:
8865cee9645SThomas Gleixner 	timr = lock_timer(timer_id, &flag);
8875cee9645SThomas Gleixner 	if (!timr)
8885cee9645SThomas Gleixner 		return -EINVAL;
8895cee9645SThomas Gleixner 
890d97bb75dSThomas Gleixner 	kc = timr->kclock;
8915cee9645SThomas Gleixner 	if (WARN_ON_ONCE(!kc || !kc->timer_set))
8925cee9645SThomas Gleixner 		error = -EINVAL;
8935cee9645SThomas Gleixner 	else
8941acbe770SAl Viro 		error = kc->timer_set(timr, flags, new_spec64, old_spec64);
8955cee9645SThomas Gleixner 
8965cee9645SThomas Gleixner 	unlock_timer(timr, flag);
8975cee9645SThomas Gleixner 	if (error == TIMER_RETRY) {
8981acbe770SAl Viro 		old_spec64 = NULL;	// We already got the old time...
8995cee9645SThomas Gleixner 		goto retry;
9005cee9645SThomas Gleixner 	}
9015cee9645SThomas Gleixner 
9025cee9645SThomas Gleixner 	return error;
9035cee9645SThomas Gleixner }
9045cee9645SThomas Gleixner 
9051acbe770SAl Viro /* Set a POSIX.1b interval timer */
9061acbe770SAl Viro SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
9071acbe770SAl Viro 		const struct itimerspec __user *, new_setting,
9081acbe770SAl Viro 		struct itimerspec __user *, old_setting)
9091acbe770SAl Viro {
9101acbe770SAl Viro 	struct itimerspec64 new_spec64, old_spec64;
9111acbe770SAl Viro 	struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL;
9121acbe770SAl Viro 	struct itimerspec new_spec;
9131acbe770SAl Viro 	int error = 0;
9141acbe770SAl Viro 
9151acbe770SAl Viro 	if (!new_setting)
9161acbe770SAl Viro 		return -EINVAL;
9171acbe770SAl Viro 
9181acbe770SAl Viro 	if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
9191acbe770SAl Viro 		return -EFAULT;
9201acbe770SAl Viro 	new_spec64 = itimerspec_to_itimerspec64(&new_spec);
9211acbe770SAl Viro 
9221acbe770SAl Viro 	error = do_timer_settime(timer_id, flags, &new_spec64, rtn);
9231acbe770SAl Viro 	if (!error && old_setting) {
9241acbe770SAl Viro 		struct itimerspec old_spec;
9251acbe770SAl Viro 		old_spec = itimerspec64_to_itimerspec(&old_spec64);
9261acbe770SAl Viro 		if (copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
9271acbe770SAl Viro 			error = -EFAULT;
9281acbe770SAl Viro 	}
9291acbe770SAl Viro 	return error;
9301acbe770SAl Viro }
9311acbe770SAl Viro 
9321acbe770SAl Viro #ifdef CONFIG_COMPAT
9331acbe770SAl Viro COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
9341acbe770SAl Viro 		       struct compat_itimerspec __user *, new,
9351acbe770SAl Viro 		       struct compat_itimerspec __user *, old)
9361acbe770SAl Viro {
9371acbe770SAl Viro 	struct itimerspec64 new_spec64, old_spec64;
9381acbe770SAl Viro 	struct itimerspec64 *rtn = old ? &old_spec64 : NULL;
9391acbe770SAl Viro 	struct itimerspec new_spec;
9401acbe770SAl Viro 	int error = 0;
9411acbe770SAl Viro 
9421acbe770SAl Viro 	if (!new)
9431acbe770SAl Viro 		return -EINVAL;
9441acbe770SAl Viro 	if (get_compat_itimerspec(&new_spec, new))
9451acbe770SAl Viro 		return -EFAULT;
9461acbe770SAl Viro 
9471acbe770SAl Viro 	new_spec64 = itimerspec_to_itimerspec64(&new_spec);
9481acbe770SAl Viro 	error = do_timer_settime(timer_id, flags, &new_spec64, rtn);
9491acbe770SAl Viro 	if (!error && old) {
9501acbe770SAl Viro 		struct itimerspec old_spec;
9511acbe770SAl Viro 		old_spec = itimerspec64_to_itimerspec(&old_spec64);
9521acbe770SAl Viro 		if (put_compat_itimerspec(old, &old_spec))
9531acbe770SAl Viro 			error = -EFAULT;
9541acbe770SAl Viro 	}
9551acbe770SAl Viro 	return error;
9561acbe770SAl Viro }
9571acbe770SAl Viro #endif
9581acbe770SAl Viro 
959f2c45807SThomas Gleixner int common_timer_del(struct k_itimer *timer)
9605cee9645SThomas Gleixner {
961eae1c4aeSThomas Gleixner 	const struct k_clock *kc = timer->kclock;
9625cee9645SThomas Gleixner 
963eae1c4aeSThomas Gleixner 	timer->it_interval = 0;
964eae1c4aeSThomas Gleixner 	if (kc->timer_try_to_cancel(timer) < 0)
9655cee9645SThomas Gleixner 		return TIMER_RETRY;
96621e55c1fSThomas Gleixner 	timer->it_active = 0;
9675cee9645SThomas Gleixner 	return 0;
9685cee9645SThomas Gleixner }
9695cee9645SThomas Gleixner 
9705cee9645SThomas Gleixner static inline int timer_delete_hook(struct k_itimer *timer)
9715cee9645SThomas Gleixner {
972d97bb75dSThomas Gleixner 	const struct k_clock *kc = timer->kclock;
9735cee9645SThomas Gleixner 
9745cee9645SThomas Gleixner 	if (WARN_ON_ONCE(!kc || !kc->timer_del))
9755cee9645SThomas Gleixner 		return -EINVAL;
9765cee9645SThomas Gleixner 	return kc->timer_del(timer);
9775cee9645SThomas Gleixner }
9785cee9645SThomas Gleixner 
9795cee9645SThomas Gleixner /* Delete a POSIX.1b interval timer. */
9805cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
9815cee9645SThomas Gleixner {
9825cee9645SThomas Gleixner 	struct k_itimer *timer;
9835cee9645SThomas Gleixner 	unsigned long flags;
9845cee9645SThomas Gleixner 
9855cee9645SThomas Gleixner retry_delete:
9865cee9645SThomas Gleixner 	timer = lock_timer(timer_id, &flags);
9875cee9645SThomas Gleixner 	if (!timer)
9885cee9645SThomas Gleixner 		return -EINVAL;
9895cee9645SThomas Gleixner 
9905cee9645SThomas Gleixner 	if (timer_delete_hook(timer) == TIMER_RETRY) {
9915cee9645SThomas Gleixner 		unlock_timer(timer, flags);
9925cee9645SThomas Gleixner 		goto retry_delete;
9935cee9645SThomas Gleixner 	}
9945cee9645SThomas Gleixner 
9955cee9645SThomas Gleixner 	spin_lock(&current->sighand->siglock);
9965cee9645SThomas Gleixner 	list_del(&timer->list);
9975cee9645SThomas Gleixner 	spin_unlock(&current->sighand->siglock);
9985cee9645SThomas Gleixner 	/*
9995cee9645SThomas Gleixner 	 * This keeps any tasks waiting on the spin lock from thinking
10005cee9645SThomas Gleixner 	 * they got something (see the lock code above).
10015cee9645SThomas Gleixner 	 */
10025cee9645SThomas Gleixner 	timer->it_signal = NULL;
10035cee9645SThomas Gleixner 
10045cee9645SThomas Gleixner 	unlock_timer(timer, flags);
10055cee9645SThomas Gleixner 	release_posix_timer(timer, IT_ID_SET);
10065cee9645SThomas Gleixner 	return 0;
10075cee9645SThomas Gleixner }
10085cee9645SThomas Gleixner 
10095cee9645SThomas Gleixner /*
10105cee9645SThomas Gleixner  * return timer owned by the process, used by exit_itimers
10115cee9645SThomas Gleixner  */
10125cee9645SThomas Gleixner static void itimer_delete(struct k_itimer *timer)
10135cee9645SThomas Gleixner {
10145cee9645SThomas Gleixner 	unsigned long flags;
10155cee9645SThomas Gleixner 
10165cee9645SThomas Gleixner retry_delete:
10175cee9645SThomas Gleixner 	spin_lock_irqsave(&timer->it_lock, flags);
10185cee9645SThomas Gleixner 
10195cee9645SThomas Gleixner 	if (timer_delete_hook(timer) == TIMER_RETRY) {
10205cee9645SThomas Gleixner 		unlock_timer(timer, flags);
10215cee9645SThomas Gleixner 		goto retry_delete;
10225cee9645SThomas Gleixner 	}
10235cee9645SThomas Gleixner 	list_del(&timer->list);
10245cee9645SThomas Gleixner 	/*
10255cee9645SThomas Gleixner 	 * This keeps any tasks waiting on the spin lock from thinking
10265cee9645SThomas Gleixner 	 * they got something (see the lock code above).
10275cee9645SThomas Gleixner 	 */
10285cee9645SThomas Gleixner 	timer->it_signal = NULL;
10295cee9645SThomas Gleixner 
10305cee9645SThomas Gleixner 	unlock_timer(timer, flags);
10315cee9645SThomas Gleixner 	release_posix_timer(timer, IT_ID_SET);
10325cee9645SThomas Gleixner }
10335cee9645SThomas Gleixner 
10345cee9645SThomas Gleixner /*
10355cee9645SThomas Gleixner  * This is called by do_exit or de_thread, only when there are no more
10365cee9645SThomas Gleixner  * references to the shared signal_struct.
10375cee9645SThomas Gleixner  */
10385cee9645SThomas Gleixner void exit_itimers(struct signal_struct *sig)
10395cee9645SThomas Gleixner {
10405cee9645SThomas Gleixner 	struct k_itimer *tmr;
10415cee9645SThomas Gleixner 
10425cee9645SThomas Gleixner 	while (!list_empty(&sig->posix_timers)) {
10435cee9645SThomas Gleixner 		tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
10445cee9645SThomas Gleixner 		itimer_delete(tmr);
10455cee9645SThomas Gleixner 	}
10465cee9645SThomas Gleixner }
10475cee9645SThomas Gleixner 
10485cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
10495cee9645SThomas Gleixner 		const struct timespec __user *, tp)
10505cee9645SThomas Gleixner {
1051d3ba5a9aSChristoph Hellwig 	const struct k_clock *kc = clockid_to_kclock(which_clock);
10525c499410SDeepa Dinamani 	struct timespec64 new_tp;
10535cee9645SThomas Gleixner 
10545cee9645SThomas Gleixner 	if (!kc || !kc->clock_set)
10555cee9645SThomas Gleixner 		return -EINVAL;
10565cee9645SThomas Gleixner 
10575c499410SDeepa Dinamani 	if (get_timespec64(&new_tp, tp))
10585cee9645SThomas Gleixner 		return -EFAULT;
10595cee9645SThomas Gleixner 
10605c499410SDeepa Dinamani 	return kc->clock_set(which_clock, &new_tp);
10615cee9645SThomas Gleixner }
10625cee9645SThomas Gleixner 
10635cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
10645cee9645SThomas Gleixner 		struct timespec __user *,tp)
10655cee9645SThomas Gleixner {
1066d3ba5a9aSChristoph Hellwig 	const struct k_clock *kc = clockid_to_kclock(which_clock);
10675c499410SDeepa Dinamani 	struct timespec64 kernel_tp;
10685cee9645SThomas Gleixner 	int error;
10695cee9645SThomas Gleixner 
10705cee9645SThomas Gleixner 	if (!kc)
10715cee9645SThomas Gleixner 		return -EINVAL;
10725cee9645SThomas Gleixner 
10735c499410SDeepa Dinamani 	error = kc->clock_get(which_clock, &kernel_tp);
10745cee9645SThomas Gleixner 
10755c499410SDeepa Dinamani 	if (!error && put_timespec64(&kernel_tp, tp))
10765cee9645SThomas Gleixner 		error = -EFAULT;
10775cee9645SThomas Gleixner 
10785cee9645SThomas Gleixner 	return error;
10795cee9645SThomas Gleixner }
10805cee9645SThomas Gleixner 
10815cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
10825cee9645SThomas Gleixner 		struct timex __user *, utx)
10835cee9645SThomas Gleixner {
1084d3ba5a9aSChristoph Hellwig 	const struct k_clock *kc = clockid_to_kclock(which_clock);
10855cee9645SThomas Gleixner 	struct timex ktx;
10865cee9645SThomas Gleixner 	int err;
10875cee9645SThomas Gleixner 
10885cee9645SThomas Gleixner 	if (!kc)
10895cee9645SThomas Gleixner 		return -EINVAL;
10905cee9645SThomas Gleixner 	if (!kc->clock_adj)
10915cee9645SThomas Gleixner 		return -EOPNOTSUPP;
10925cee9645SThomas Gleixner 
10935cee9645SThomas Gleixner 	if (copy_from_user(&ktx, utx, sizeof(ktx)))
10945cee9645SThomas Gleixner 		return -EFAULT;
10955cee9645SThomas Gleixner 
10965cee9645SThomas Gleixner 	err = kc->clock_adj(which_clock, &ktx);
10975cee9645SThomas Gleixner 
10985cee9645SThomas Gleixner 	if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
10995cee9645SThomas Gleixner 		return -EFAULT;
11005cee9645SThomas Gleixner 
11015cee9645SThomas Gleixner 	return err;
11025cee9645SThomas Gleixner }
11035cee9645SThomas Gleixner 
1104d822cdccSAl Viro SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1105d822cdccSAl Viro 		struct timespec __user *, tp)
1106d822cdccSAl Viro {
1107d822cdccSAl Viro 	const struct k_clock *kc = clockid_to_kclock(which_clock);
11085c499410SDeepa Dinamani 	struct timespec64 rtn_tp;
1109d822cdccSAl Viro 	int error;
1110d822cdccSAl Viro 
1111d822cdccSAl Viro 	if (!kc)
1112d822cdccSAl Viro 		return -EINVAL;
1113d822cdccSAl Viro 
11145c499410SDeepa Dinamani 	error = kc->clock_getres(which_clock, &rtn_tp);
1115d822cdccSAl Viro 
11165c499410SDeepa Dinamani 	if (!error && tp && put_timespec64(&rtn_tp, tp))
1117d822cdccSAl Viro 		error = -EFAULT;
1118d822cdccSAl Viro 
1119d822cdccSAl Viro 	return error;
1120d822cdccSAl Viro }
1121d822cdccSAl Viro 
11223a4d44b6SAl Viro #ifdef CONFIG_COMPAT
11233a4d44b6SAl Viro 
1124d822cdccSAl Viro COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
1125d822cdccSAl Viro 		       struct compat_timespec __user *, tp)
1126d822cdccSAl Viro {
1127d822cdccSAl Viro 	const struct k_clock *kc = clockid_to_kclock(which_clock);
11285c499410SDeepa Dinamani 	struct timespec64 ts;
1129d822cdccSAl Viro 
1130d822cdccSAl Viro 	if (!kc || !kc->clock_set)
1131d822cdccSAl Viro 		return -EINVAL;
1132d822cdccSAl Viro 
11335c499410SDeepa Dinamani 	if (compat_get_timespec64(&ts, tp))
1134d822cdccSAl Viro 		return -EFAULT;
1135d822cdccSAl Viro 
11365c499410SDeepa Dinamani 	return kc->clock_set(which_clock, &ts);
1137d822cdccSAl Viro }
1138d822cdccSAl Viro 
1139d822cdccSAl Viro COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
1140d822cdccSAl Viro 		       struct compat_timespec __user *, tp)
1141d822cdccSAl Viro {
1142d822cdccSAl Viro 	const struct k_clock *kc = clockid_to_kclock(which_clock);
11435c499410SDeepa Dinamani 	struct timespec64 ts;
11445c499410SDeepa Dinamani 	int err;
1145d822cdccSAl Viro 
1146d822cdccSAl Viro 	if (!kc)
1147d822cdccSAl Viro 		return -EINVAL;
1148d822cdccSAl Viro 
11495c499410SDeepa Dinamani 	err = kc->clock_get(which_clock, &ts);
1150d822cdccSAl Viro 
11515c499410SDeepa Dinamani 	if (!err && compat_put_timespec64(&ts, tp))
11525c499410SDeepa Dinamani 		err = -EFAULT;
1153d822cdccSAl Viro 
11545c499410SDeepa Dinamani 	return err;
1155d822cdccSAl Viro }
1156d822cdccSAl Viro 
11573a4d44b6SAl Viro COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
11583a4d44b6SAl Viro 		       struct compat_timex __user *, utp)
11593a4d44b6SAl Viro {
11603a4d44b6SAl Viro 	const struct k_clock *kc = clockid_to_kclock(which_clock);
11613a4d44b6SAl Viro 	struct timex ktx;
11623a4d44b6SAl Viro 	int err;
11633a4d44b6SAl Viro 
11643a4d44b6SAl Viro 	if (!kc)
11653a4d44b6SAl Viro 		return -EINVAL;
11663a4d44b6SAl Viro 	if (!kc->clock_adj)
11673a4d44b6SAl Viro 		return -EOPNOTSUPP;
11683a4d44b6SAl Viro 
11693a4d44b6SAl Viro 	err = compat_get_timex(&ktx, utp);
11703a4d44b6SAl Viro 	if (err)
11713a4d44b6SAl Viro 		return err;
11723a4d44b6SAl Viro 
11733a4d44b6SAl Viro 	err = kc->clock_adj(which_clock, &ktx);
11743a4d44b6SAl Viro 
11753a4d44b6SAl Viro 	if (err >= 0)
11763a4d44b6SAl Viro 		err = compat_put_timex(utp, &ktx);
11773a4d44b6SAl Viro 
11783a4d44b6SAl Viro 	return err;
11793a4d44b6SAl Viro }
11803a4d44b6SAl Viro 
1181d822cdccSAl Viro COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
1182d822cdccSAl Viro 		       struct compat_timespec __user *, tp)
11835cee9645SThomas Gleixner {
1184d3ba5a9aSChristoph Hellwig 	const struct k_clock *kc = clockid_to_kclock(which_clock);
11855c499410SDeepa Dinamani 	struct timespec64 ts;
11865c499410SDeepa Dinamani 	int err;
11875cee9645SThomas Gleixner 
11885cee9645SThomas Gleixner 	if (!kc)
11895cee9645SThomas Gleixner 		return -EINVAL;
11905cee9645SThomas Gleixner 
11915c499410SDeepa Dinamani 	err = kc->clock_getres(which_clock, &ts);
11925c499410SDeepa Dinamani 	if (!err && tp && compat_put_timespec64(&ts, tp))
11935c499410SDeepa Dinamani 		return -EFAULT;
11945cee9645SThomas Gleixner 
11955c499410SDeepa Dinamani 	return err;
11965cee9645SThomas Gleixner }
11975c499410SDeepa Dinamani 
1198d822cdccSAl Viro #endif
11995cee9645SThomas Gleixner 
12005cee9645SThomas Gleixner /*
12015cee9645SThomas Gleixner  * nanosleep for monotonic and realtime clocks
12025cee9645SThomas Gleixner  */
12035cee9645SThomas Gleixner static int common_nsleep(const clockid_t which_clock, int flags,
1204938e7cf2SThomas Gleixner 			 const struct timespec64 *rqtp)
12055cee9645SThomas Gleixner {
1206938e7cf2SThomas Gleixner 	return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ?
12075cee9645SThomas Gleixner 				 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
12085cee9645SThomas Gleixner 				 which_clock);
12095cee9645SThomas Gleixner }
12105cee9645SThomas Gleixner 
12115cee9645SThomas Gleixner SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
12125cee9645SThomas Gleixner 		const struct timespec __user *, rqtp,
12135cee9645SThomas Gleixner 		struct timespec __user *, rmtp)
12145cee9645SThomas Gleixner {
1215d3ba5a9aSChristoph Hellwig 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1216ad196384SDeepa Dinamani 	struct timespec64 t64;
12175cee9645SThomas Gleixner 	struct timespec t;
12185cee9645SThomas Gleixner 
12195cee9645SThomas Gleixner 	if (!kc)
12205cee9645SThomas Gleixner 		return -EINVAL;
12215cee9645SThomas Gleixner 	if (!kc->nsleep)
12225cee9645SThomas Gleixner 		return -ENANOSLEEP_NOTSUP;
12235cee9645SThomas Gleixner 
12245cee9645SThomas Gleixner 	if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
12255cee9645SThomas Gleixner 		return -EFAULT;
12265cee9645SThomas Gleixner 
1227ad196384SDeepa Dinamani 	t64 = timespec_to_timespec64(t);
1228ad196384SDeepa Dinamani 	if (!timespec64_valid(&t64))
12295cee9645SThomas Gleixner 		return -EINVAL;
123099e6c0e6SAl Viro 	if (flags & TIMER_ABSTIME)
123199e6c0e6SAl Viro 		rmtp = NULL;
1232edbeda46SAl Viro 	current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
123399e6c0e6SAl Viro 	current->restart_block.nanosleep.rmtp = rmtp;
12345cee9645SThomas Gleixner 
123599e6c0e6SAl Viro 	return kc->nsleep(which_clock, flags, &t64);
12365cee9645SThomas Gleixner }
12375cee9645SThomas Gleixner 
1238edbeda46SAl Viro #ifdef CONFIG_COMPAT
1239edbeda46SAl Viro COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
1240edbeda46SAl Viro 		       struct compat_timespec __user *, rqtp,
1241edbeda46SAl Viro 		       struct compat_timespec __user *, rmtp)
12425cee9645SThomas Gleixner {
1243d3ba5a9aSChristoph Hellwig 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1244edbeda46SAl Viro 	struct timespec64 t64;
1245edbeda46SAl Viro 	struct timespec t;
12465cee9645SThomas Gleixner 
1247edbeda46SAl Viro 	if (!kc)
12485cee9645SThomas Gleixner 		return -EINVAL;
1249edbeda46SAl Viro 	if (!kc->nsleep)
1250edbeda46SAl Viro 		return -ENANOSLEEP_NOTSUP;
12515cee9645SThomas Gleixner 
1252edbeda46SAl Viro 	if (compat_get_timespec(&t, rqtp))
1253edbeda46SAl Viro 		return -EFAULT;
1254edbeda46SAl Viro 
1255edbeda46SAl Viro 	t64 = timespec_to_timespec64(t);
1256edbeda46SAl Viro 	if (!timespec64_valid(&t64))
1257edbeda46SAl Viro 		return -EINVAL;
1258edbeda46SAl Viro 	if (flags & TIMER_ABSTIME)
1259edbeda46SAl Viro 		rmtp = NULL;
1260edbeda46SAl Viro 	current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1261edbeda46SAl Viro 	current->restart_block.nanosleep.compat_rmtp = rmtp;
1262edbeda46SAl Viro 
1263edbeda46SAl Viro 	return kc->nsleep(which_clock, flags, &t64);
12645cee9645SThomas Gleixner }
1265edbeda46SAl Viro #endif
12666631fa12SThomas Gleixner 
12676631fa12SThomas Gleixner static const struct k_clock clock_realtime = {
12686631fa12SThomas Gleixner 	.clock_getres		= posix_get_hrtimer_res,
12696631fa12SThomas Gleixner 	.clock_get		= posix_clock_realtime_get,
12706631fa12SThomas Gleixner 	.clock_set		= posix_clock_realtime_set,
12716631fa12SThomas Gleixner 	.clock_adj		= posix_clock_realtime_adj,
12726631fa12SThomas Gleixner 	.nsleep			= common_nsleep,
12736631fa12SThomas Gleixner 	.timer_create		= common_timer_create,
12746631fa12SThomas Gleixner 	.timer_set		= common_timer_set,
12756631fa12SThomas Gleixner 	.timer_get		= common_timer_get,
12766631fa12SThomas Gleixner 	.timer_del		= common_timer_del,
1277f37fb0aaSThomas Gleixner 	.timer_rearm		= common_hrtimer_rearm,
127891d57baeSThomas Gleixner 	.timer_forward		= common_hrtimer_forward,
127991d57baeSThomas Gleixner 	.timer_remaining	= common_hrtimer_remaining,
1280eae1c4aeSThomas Gleixner 	.timer_try_to_cancel	= common_hrtimer_try_to_cancel,
1281eae1c4aeSThomas Gleixner 	.timer_arm		= common_hrtimer_arm,
12826631fa12SThomas Gleixner };
12836631fa12SThomas Gleixner 
12846631fa12SThomas Gleixner static const struct k_clock clock_monotonic = {
12856631fa12SThomas Gleixner 	.clock_getres		= posix_get_hrtimer_res,
12866631fa12SThomas Gleixner 	.clock_get		= posix_ktime_get_ts,
12876631fa12SThomas Gleixner 	.nsleep			= common_nsleep,
12886631fa12SThomas Gleixner 	.timer_create		= common_timer_create,
12896631fa12SThomas Gleixner 	.timer_set		= common_timer_set,
12906631fa12SThomas Gleixner 	.timer_get		= common_timer_get,
12916631fa12SThomas Gleixner 	.timer_del		= common_timer_del,
1292f37fb0aaSThomas Gleixner 	.timer_rearm		= common_hrtimer_rearm,
129391d57baeSThomas Gleixner 	.timer_forward		= common_hrtimer_forward,
129491d57baeSThomas Gleixner 	.timer_remaining	= common_hrtimer_remaining,
1295eae1c4aeSThomas Gleixner 	.timer_try_to_cancel	= common_hrtimer_try_to_cancel,
1296eae1c4aeSThomas Gleixner 	.timer_arm		= common_hrtimer_arm,
12976631fa12SThomas Gleixner };
12986631fa12SThomas Gleixner 
12996631fa12SThomas Gleixner static const struct k_clock clock_monotonic_raw = {
13006631fa12SThomas Gleixner 	.clock_getres		= posix_get_hrtimer_res,
13016631fa12SThomas Gleixner 	.clock_get		= posix_get_monotonic_raw,
13026631fa12SThomas Gleixner };
13036631fa12SThomas Gleixner 
13046631fa12SThomas Gleixner static const struct k_clock clock_realtime_coarse = {
13056631fa12SThomas Gleixner 	.clock_getres		= posix_get_coarse_res,
13066631fa12SThomas Gleixner 	.clock_get		= posix_get_realtime_coarse,
13076631fa12SThomas Gleixner };
13086631fa12SThomas Gleixner 
13096631fa12SThomas Gleixner static const struct k_clock clock_monotonic_coarse = {
13106631fa12SThomas Gleixner 	.clock_getres		= posix_get_coarse_res,
13116631fa12SThomas Gleixner 	.clock_get		= posix_get_monotonic_coarse,
13126631fa12SThomas Gleixner };
13136631fa12SThomas Gleixner 
13146631fa12SThomas Gleixner static const struct k_clock clock_tai = {
13156631fa12SThomas Gleixner 	.clock_getres		= posix_get_hrtimer_res,
13166631fa12SThomas Gleixner 	.clock_get		= posix_get_tai,
13176631fa12SThomas Gleixner 	.nsleep			= common_nsleep,
13186631fa12SThomas Gleixner 	.timer_create		= common_timer_create,
13196631fa12SThomas Gleixner 	.timer_set		= common_timer_set,
13206631fa12SThomas Gleixner 	.timer_get		= common_timer_get,
13216631fa12SThomas Gleixner 	.timer_del		= common_timer_del,
1322f37fb0aaSThomas Gleixner 	.timer_rearm		= common_hrtimer_rearm,
132391d57baeSThomas Gleixner 	.timer_forward		= common_hrtimer_forward,
132491d57baeSThomas Gleixner 	.timer_remaining	= common_hrtimer_remaining,
1325eae1c4aeSThomas Gleixner 	.timer_try_to_cancel	= common_hrtimer_try_to_cancel,
1326eae1c4aeSThomas Gleixner 	.timer_arm		= common_hrtimer_arm,
13276631fa12SThomas Gleixner };
13286631fa12SThomas Gleixner 
13296631fa12SThomas Gleixner static const struct k_clock clock_boottime = {
13306631fa12SThomas Gleixner 	.clock_getres		= posix_get_hrtimer_res,
13316631fa12SThomas Gleixner 	.clock_get		= posix_get_boottime,
13326631fa12SThomas Gleixner 	.nsleep			= common_nsleep,
13336631fa12SThomas Gleixner 	.timer_create		= common_timer_create,
13346631fa12SThomas Gleixner 	.timer_set		= common_timer_set,
13356631fa12SThomas Gleixner 	.timer_get		= common_timer_get,
13366631fa12SThomas Gleixner 	.timer_del		= common_timer_del,
1337f37fb0aaSThomas Gleixner 	.timer_rearm		= common_hrtimer_rearm,
133891d57baeSThomas Gleixner 	.timer_forward		= common_hrtimer_forward,
133991d57baeSThomas Gleixner 	.timer_remaining	= common_hrtimer_remaining,
1340eae1c4aeSThomas Gleixner 	.timer_try_to_cancel	= common_hrtimer_try_to_cancel,
1341eae1c4aeSThomas Gleixner 	.timer_arm		= common_hrtimer_arm,
13426631fa12SThomas Gleixner };
13436631fa12SThomas Gleixner 
13446631fa12SThomas Gleixner static const struct k_clock * const posix_clocks[] = {
13456631fa12SThomas Gleixner 	[CLOCK_REALTIME]		= &clock_realtime,
13466631fa12SThomas Gleixner 	[CLOCK_MONOTONIC]		= &clock_monotonic,
13476631fa12SThomas Gleixner 	[CLOCK_PROCESS_CPUTIME_ID]	= &clock_process,
13486631fa12SThomas Gleixner 	[CLOCK_THREAD_CPUTIME_ID]	= &clock_thread,
13496631fa12SThomas Gleixner 	[CLOCK_MONOTONIC_RAW]		= &clock_monotonic_raw,
13506631fa12SThomas Gleixner 	[CLOCK_REALTIME_COARSE]		= &clock_realtime_coarse,
13516631fa12SThomas Gleixner 	[CLOCK_MONOTONIC_COARSE]	= &clock_monotonic_coarse,
13526631fa12SThomas Gleixner 	[CLOCK_BOOTTIME]		= &clock_boottime,
13536631fa12SThomas Gleixner 	[CLOCK_REALTIME_ALARM]		= &alarm_clock,
13546631fa12SThomas Gleixner 	[CLOCK_BOOTTIME_ALARM]		= &alarm_clock,
13556631fa12SThomas Gleixner 	[CLOCK_TAI]			= &clock_tai,
13566631fa12SThomas Gleixner };
13576631fa12SThomas Gleixner 
13586631fa12SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id)
13596631fa12SThomas Gleixner {
13606631fa12SThomas Gleixner 	if (id < 0)
13616631fa12SThomas Gleixner 		return (id & CLOCKFD_MASK) == CLOCKFD ?
13626631fa12SThomas Gleixner 			&clock_posix_dynamic : &clock_posix_cpu;
13636631fa12SThomas Gleixner 
13646631fa12SThomas Gleixner 	if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id])
13656631fa12SThomas Gleixner 		return NULL;
13666631fa12SThomas Gleixner 	return posix_clocks[id];
13676631fa12SThomas Gleixner }
1368